alright, i am finding this a bit confusing, do i need to go through other .cpp files to start understanding how this works?
No. If all you want to do is write a driver for OpenPLC to control your GPIOs, then you only need to look at hardware_layer.cpp file. Just remember that if you want to make your changes permanent, it is better if you edit one of the original files inside the /webserver/core/hardware_layers/ folder. Get for example the raspberrypi.cpp file and put your modifications in there. Then, once you're done, go to the hardware tab on the webserver and reapply the Raspberry Pi driver. This will make OpenPLC reload your modified driver and then all will be good.
1. DIO's are accessible through "/sys/class/gpio" so am assuming these are the built in GPIO's, detected and available through kernel and i can read them with simple "cat" , e.g: "cat /sys/class/gpio/gpio18/value":
Q: if for example the DI pins are gpio18, gpio19, gpio20, gpio 21, does that mean:
#define MAX_INPUT
4 & int inBufferPinMask[MAX_INPUT] = {
18, 19, 20 ,21 }; ?
No. The pin numbers are according to the WiringPi library, that bases the numbers on the pin headers of the Raspberry Pi. This is not the same numbering as the one used in the Broadcom chip GPIO. Also, I don't know how your PLC is internally connected. It may be the case that these GPIOs are not even directly connected to the Broadcom chip at all. Toggling pins on the chip directly might cause unexpected behaviors and even harm your device!
It seems that the operating system of your device makes available all the GPIO over SYSFS. If you don't know what it is, here is a quote from Wikipedia: "sysfs is a pseudo file system provided by the Linux kernel that exports information about various kernel subsystems, hardware devices, and associated device drivers from the kernel's device model to user space through virtual files". This means that these virtual files (/sys/class/gpio) are directly linked to a driver that can control your GPIO. For the Linux system, they are treated as files. So your driver must also treat them as files. If you want to read them, just open the file and read it. If you want to write to them, open the file and write. I'm writing an OpenPLC driver for the UniPi Neuron PLC that also makes available the pins over SYSFS. The driver is not finished yet, but I'm attaching it here for your reference. Please ignore the SearchForIO() function as this is specific for the Neuron platform.
2. AI is through I2C ADC chip,"MCP3424" and is accessible through "/sys/bus/i2c/devices/0-006c/iioDevice0" :
Files …raw - for reading analog input values
Files …scale - scale of reading
File in_voltage_scale_available - all available scales
File sampling_frequency_available - all available sampling frequency
Q1. assuming everything else is predefined for simplicity, how is it possible to refer to AI pin through file "/sys/bus/i2c/devices/0-006c/iioDevice0/in_voltage0_raw" in the hardware layer?
Same thing as last question. Use SYSFS to access the data.
Q2. to make this more advance when it comes to scaling, how the other variables/files can be incorporated in the hardware layer? or do i just read the raw and scale it internally in OpenPLC
OpenPLC registers have 16 bits, which means they are integers on the range 0...65535. You can multiply or divide the raw value of your analog input to fit in this range so you don't lose accuracy. All OpenPLC drivers do that, they provide all analog values in the full scale range of 0...65535.