Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

I/O Drivers:

laruso
2021-05-17
2024-02-21
  • laruso - 2021-05-17

    I want to write a (correct) driver for the MCP3204!

    I downloaded a few templates and codes related to the MCP3008 and MCP3202 and merged everything together and it works. But now I wanted to read all the documentation here to fully correct the code and put it on the CODESYS Forge, and I struggle.

    For an SPI I/O driver: do I have to follow the SPI guidelines and/or follow the generic guidelines and/or follow the Driver FB guidelines??? It's very confusing to me. At the beginning there are three sentences, and I don't get it...

    Meanwhile I get very confused about the documentation and the code provided. First of all:
    - The code for ID 0001 "SPI Template" contains the device description and the library with a FB "SPITemplate", that contains a directory "connector". (That's how it's described under SPI Driver) OK
    But in the device description the is a "Connector moduleType="501", whereas it's "500" in the documentation (very important, and has to be...) and in the template "SPI Temperature". Confusing
    - The code for ID 0003 "SPI Temperature" contains the device description and the library with a FB "SPITemperature", that contains a directory "connector". (That's how it's described under SPI Driver) OK
    - The code for ID 0004 "SPI MCP3008" contains the device description and the library with a FB ", that contains a directory "connector" AND additionally a FB "IoDrvMCP3008", that contains directories "IBase", "ICmpIoDrv" and "ICmpIoDrvParameter". (That's a mix described under Generic I/O Driver, SPI Driver and Driver FB.) In addition some code of "Initialized" is missing and the code of "AfterReadInputs" differs unnecessarily.

    Can anybody tell me how to do it correctly?!?

    Hopefully I'll granted some help instead of buying another support ticket to get the easiest things done right, although it's documented very bad...

    If needed, I can also share my already existing code for the MCP3204. The functional one and the non functional one.


    Talk Topic about project #io-drivers

     

    Last edit: laruso 2021-05-17
    • Ingo

      Ingo - 2021-05-17

      Are the drivers really documented so badly? I hoped that they are helpful.

      Your approach was fully correct this far. If you started with the MCP3008, you should already be fine. Just make sure, that you use a unique ID, as described here:
      https://forge.codesys.com/drv/io-drivers/doc/Generic/#device-identification

      If you are curious about a specific concept, we can discuss this separatly.

       
      • laruso - 2021-05-18

        From the description of writing an driver for a ADC via SPI I do only need a FB that contains the connector (also from SPI the template). Within the MCP3008 template there is the connector PLUS a additional FB contains "IBase", "ICmpIoDrv" and "ICmpIoDrvParameter".

        So what is the right way how to do it?
        Are both FBs needed?
        Why the temperature template contains only the connector?

        The unique ID was already considered.

        On the other side there are many deviations regarding the documentation, but that can of course be discussed separately.

        I really want to make sure everything is correct, in between the guideline from the documentation and the first templates and do well without unnecessary/redundant code.

        At the end I want to create a slightly different driver for the MCP33151-05 with the help from documentation, so others can have a guideline/clue what to do, if they want to write their own drivers. The timing diagram is attached. Could you probably guide me to get the docu right and write this driver on my own?

         
        • Ingo

          Ingo - 2021-05-18

          From the description of writing an driver for a ADC via SPI I do only need a FB that
          contains the connector (also from SPI the template). Within the MCP3008 template there is
          the connector PLUS a additional FB contains "IBase", "ICmpIoDrv" and
          "ICmpIoDrvParameter".
          So what is the right way how to do it?
          Are both FBs needed?

          Actually not. But I found, that this is a clever design, as it hides some complexity. You have one function block, which contains the business logic to access the device. And you have one function block, which implements the interfaces, to turn it into a CODESYS I/O driver.

          I really want to make sure everything is correct, in between the guideline from
          the documentation and the first templates and do well without unnecessary/redundant
          code.

          I would not say, that the MCP3008 has unnecessary or redundant code. It implements a clean design from my point of view.

          But if you want to check, that everything is correct:

          I can only recommend to go through the steps in the agenda, and check how you did it.

          That's mainly it.

           
          • laruso - 2021-06-02

            Dear Ingo,

            if I get you right there are two kinds of drivers:

            • Driver (only consists of β€žconnectorβ€œ like in the SPI template)
            • CODESYS I/O driver (consists of β€žconnectorβ€œ for logic access, and in adddition "IBase", "ICmpIoDrv" and "ICmpIoDrvParameter" to properly implement the device into CODESYS)

            The driver should works as it is, but the use could perhaps be a little bit more complicated because addressing the instance calls/variables.
            The CODESYS I/O driver works too, but a little bit more comfortable because of better visualization and integration into the development enviroment (device in device tree and I/O mapping).

            Is this correct?

             

Log in to post a comment.