This documentation describes the process of creating an own I2C driver for CODESYS, based on the I2C Template, which can be found in the Code Repository. The driver consists of a "Device Description" as well as a CODESYS Library. All essential settings in the Library or the Device Description are repeated in this documentation. For more detailed informations about the Device Descriptions, please check the general I/O driver documentation.
You can use a copy of the Device Description I2C_Template.devdesc.xml as a starting point. You need to adapt the following sections of the device description for your needs.
The template of the device description can be downloaded with an ordinary SVN client (like Tortoise SVN) or directly from the repository browser, from the Code Repository.
The version can be freely defined. Just, the format is fix. And you should take care that you never release two different device descriptions with the same Type, ID and Version. As this might produce conflicts in the repositories of other users.
The template library can be checked out with CODESYS SVN. It can be obtained from the CODESYS Store. As well as the device description, it is placed in the code repository.
It is a standard CODESYS Library, which needs the standard information to behave as expected to the user. We describe all bits, which need to be changed in the following documentation.
The function block can be renamed. But make sure, that you change the "FBInstance" parameter in the device description accordingly. It has to be extended from "I2C", which is part of the "Raspberry Pi Peripherals" library.
In this example, the output of this function block is the output of the I2C driver, and the value which will be used by the user in his application. So the value can be used like this in the application:
rMyTemperature := I2CITemplate.rValue;
All methods, which are overloading in your function block, need to call the super method, so that the method of the base I2C function block is still executed.
Body The body of the FB should contain a small state machine. It starts with 0 and has to be set to 10 when you finished your stuff. Here is also a good place to define the I2C Address. This is a member of the base FB and needs to be set here.
BeforeWriteOutputs can be used in a similar way to write the outputs to the I2C device. The output is read from the "input" of the function block.
Initialize is a special method which can be used to read configuration parameters. When the application is loaded, this function is called, and the corresponding "Connector" (s. Device Description documentation) is passed to it. You can then use IoStandard.ConfigGetParameter() to access the configuration parameters.