Skip to content

CODESYS

Introduction

For some time the CODESYS SoftPLC Runtime has been available for the Raspberry Pi, which poses the question if CODESYS could also run on the Pi-Tron CM3+, as it was build based on the Raspberry Pi Compute Module 3+. The answer is yes, CODESYS does run on the Pi-Tron CM3+, but a few things have to be noted and the Raspberry Pi OS has to be modified in order to get it to work.

The following steps show an example approach on how to get CODESYS running on the Pi-Tron, but please keep in mind, that this guide is in no way complete and the Pi-Tron was not validated against CODESYS PLC standards, there might be a few short comings to keep in mind when running the CODESYS V3 SoftPLC on the Pi-Tron.

Note

The following information does not make any claim to be complete, some steps require advanced knowledge of the Debian Linux operating systemand how to build device tree files Proceed at your own risk.

Restrictions

  • The steps on this page have been done with CODESYS V3.5 SP16 Patch 2
  • The CODESYS Control for Raspberry Pi is currently not compatible with Raspberry Pi OS Bullseye
  • Some device tree files have to be modified and re-compiled (only if DIOs are used)

Preparation

The first step is to prepare an SD card or to flash the Pi-Tron's eMMC memory, depending on the CM3+ model.

  • Get the Raspberry Pi Imager here
  • Choose the Raspberry Pi OS (Legacy) option
  • Write the image to an SD card or the eMMC memory
    • Getting started with SD card see here
    • Writing to the eMMC memory have a look here
  • If the RTC module is required, have a look at the Onboard Real Time Clock - RTC and the Build Linux Kernel pages for more information on this topic. Some additional steps and work are required to get the RTC working.
  • Insert the SD card into the Pi-Tron and power it on or if you have the eMMC model, eject the removable drive, power off the device and unplug the micro USB cable and power it back on.

CODESYS

Once the initial setup of the Raspberry Pi OS is done, CODESYS or rather the CODESYS runtime can be installed on the Raspberry Pi.

Install CODESYS V3.5 SP16 Patch 2 on your Windows development system and then install the CODESYS Control for Raspberry Pi in CODESYS via the Package Manager. After the installation restart CODESYS to make sure the new information is loaded into CODESYS.

codesys_package_manager

To install the CODESYS runtime on the Raspberry Pi, the Pi-Tron's IP address is needed. Open the Raspberry Pi Update page in CODESYS and perform a scan or have a look at your routers DHCP server.

To install CODESYS on the Pi-Tron perform these steps in the Raspberry Pi Update window:

  1. Enter user name, usually pi
  2. Enter the users password, default is raspberry
  3. The the Pi-Tron's IP address in this field
  4. Click the System Info button to first check if a connection to the Pi can be established. If the System Info window appears and shows 4 different textboxes filled with information, the connection worked.
  5. Now install the CODESYS runtime by clicking the Install button below the CODESYS Runtime Package list, not the one in the Additional Packages section.

codesys_raspberry_pi_update_window

After the install completes, a window can appear which asks which version of the CODESYS runtime to start. For most cases the Single Core version is sufficient, but advanced CODESYS developers might want to activate the Multi-Core version of the runtime.
This is important to note, because later on in the CODESYS projects a different PLC device has to be selected depending on the choice made here.

If you want to check if the CODESYS runtime was installed correctly, you have to be fast and perform a network scan as quickly as possible, as the CODESYS runtime closes down. Without modifications to the system, CODESYS does not recognize the Pi-Tron as a valid Raspberry Pi device and stops after a few seconds.

Modifications

To get CODESYS running on the Pi-Tron, the following modifications have to be made. Some files and scripts mentioned here can also be found in the Pi-Tron CM3+'s GitLab Project. If preferred, clone the GitLab project to the Pi before doing the steps.

Log into the Pi via SSH or directly via monitor and keyboard, depending on the Pi-Tron model.

  • Disable CODESYS automatic start, it must be started later via a special script

    • sudo systemctl disable codesyscontrol
  • Run the mac_generator.sh script, to modify the cmdline.txt which sets the eth0 MAC address to a valid Raspberry Pi Foundation MAC address each time the Pi-Tron boot.

    • The shell script is in the script folder in the Pi-Tron's CODESYS GitLab project
    • The mac_generator.sh script probably needs execution rights, sudo chmod +x mac_generator.sh
    • You can check the cmdline.txt and see that it now contains the text smsc95xx.macaddr=XX:XX:XX:XX
  • Now get the shell script startPLCCAN.sh from the GitLab project or create a similar one and place it in the pi user's home folder, usually /home/pi/.

    • This script reads the MAC address stored in the CM3+'s OTP registers and assigns it to the second ethernet port (eth1). Otherwise both ethernet ports would have the same MAC address.
    • Then the script activates the CAN-Bus interface and sets it's bitrate to 125000 bps
    • At the end the CODESYS service is started manually
    • Give the script the needed rights: sudo chmod +x startPLCCAN.sh
  • The startPLCCAN.sh can be run automatically every time the device boots via the rc.local file for example.

    • Make the startPLCCAN.sh shell script executable, if not already
      • sudo chmod +x startPLCCAN.sh
    • Edit the rc.local file with: sudo nano /etc/rc.local
    • Before the line "exit 0" add the command
      • sudo /home/pi/startPLCCAN.sh
  • Now the config.txt file has to be changed, to make the CAN-Bus device and the serial ports available in the OS itself.

    • In the GitLab project in the boot folder, there is an example config.txt which you can use as a basis for your own or have a look at the page RPI-config.txt to see more options.
    • In the example provided, the following interfaces and options are used
      • I2C and SPI are activated
      • The onboard RTC module is used (Note: needs special kernel module, see topic Preparation)
      • The CAN-Bus on SPI 0 with CS 0 is activated on GPIOs 35 to 39
        • Note: GPIO 35 is also used for DOut2, see the hardware page for more information on the GPIO assignments.
      • The UART0 (serial0) is assigned to the RS485 port (Note: Not Modbus RTU compatible)
      • The UART1 (serial1) is assigned to the RS232 port
  • For CODESYS to know which serial devices it can use, the CODESYSControl_User.cfg has to be modified. In the GitLab project in the etc folder is an example CODESYSControl_User.cfg which can be used.

    • In the SysCom section of the file the Linux.Devicefile setting has to point to
      • Linux.Devicefile=/dev/serial
    • Optionally: portnum can be set to COM1 to explicitly set this as the first COM port
      • portnum := COM.SysCom.SYS_COMPORT1
    • Optionally: Allow all system commands in the SysProcess section with
      • Command=AllowAll
  • To be able to control the CAN-Bus interface from CODESYS, a file called rts_set_baud.sh needs to be created in the CODESYS working folder which usually is /var/opt/codesys. This script only contains a few lines. An example can be found in the GitLab project in the codesys folder or it can be created manually with these lines:

    • sudo nano /var/opt/codesys/rts_set_baud.sh
         #!/bin/sh
         BITRATE=`expr $2 \\* 1000`
      
         ip link set $1 down
         ip link set $1 type can bitrate $BITRATE
         ip link set $1 up
      
  • Remember to also make the script executable

    • sudo chmod +x /var/opt/codesys/rts_set_baud.sh

Now the system can be rebooted and CODESYS should start and run in Demo Mode for up to 2 hours. If you have a license you can install it now, however we recommend using a license dongle, which makes license handling much easier in case the OS has to be re-installed at some point during the development process.

It's probably a good idea to make a backup image of the system right now in case something goes in the future or if you continue with the next steps, then the above steps don't have to be repeated again.

Freeing up GPIO 35

By default the GPIO 35 is used by SPI 0 as Chip Select 1, but on the Pi-Tron it is used as DOut2, so it makes sense to modify the original device tree spi-gpio35-39 and remove the GPIO 35 from it's configuration. In the GitLab project in the folder boot/overlays/ is an example spi-gpio35-39-overlay.dts where the GPIO 35 is left out. To be able to use this modified overlay, it has to be compiled to a *.dtbo file.

  • Install device tree compiler
    • sudo apt-get install device-tree-compiler
  • Compile the dts file
    • dtc -@ -I dts -O dtb -o spi-gpio35-39.dtbo spi-gpio35-39-overlay.dts
  • Copy the file to the Pi's overlays folder
    • sudo cp spi-gpio35-39.dtbo /boot/overlays/

Working with the DIOs

The DIOs of the Pi-Tron are connected to CM3+ GPIOs in bank1, which means they are not accessible via GPIOs from the 40 pin header, but from higher GPIO numbers. These higher GPIOs are unfortunately not supported by the Standard CODESYS Raspberry Pi driver, which only includes GPIOs from the 40 pin header.

The Pi-Tron's GitLab project contains a "gpio" folder which includes a modified version of the original CODESYS GPIO driver which allows access to the DIOs. This driver is an example on how to get access to the GPIOs on bank1 on the Compute Module 3+, change and modify it to fit your needs.

First install the library via the "Library Repository..." and then the GPIO device description using the "Device Repository..." menus from the Tools Menu from within CODESYS. After the install, it is recommended to restart CODESYS to reload the hardware and device driver database.

Now you can:

  • Create a new project based on the CODESYS Control for Raspberry Pi
  • Delete the existing GPIO device. Right click on it and choose delete
  • Right click again on the now empty device slot and choose "Plug Device"
  • From the Plug Device window select the Pi-Tron GPIO device and click "Plug Device"
  • Then click "Close"
  • By default all outputs are deactivated and must be manually turned on in the GPIO parameter section of the GPIO device
  • Note: In the GPIO (DIO) input section are also the outputs and in the output section are also the inputs. The outputs cannot be used as inputs and the inputs cannot be used as outputs! This mix stems from the device description of the original CODESYS GPIOs driver.

Below are some screenshots to illustrate the above mentioned steps.

  • Deleting the default GPIO device

codesys_gpio_device_delete

  • Plugging a new device

codesys_gpio_device_plug

  • Choosing the Pi-Tron GPIO device

codesys_gpio_device_plug_device_window

  • The GPIO parameters now have more GPIOs listed, Outputs are set to "not used" by default

codesys_gpio_device_parameters

  • The Pi-Tron DIOs inputs

codesys_gpio_device_inputs

  • The Pi-Tron DIOs outputs

codesys_gpio_device_outputs

Community

For more information on CODESYS or if you are looking for further help on CODESYS, the best place is the CODESYS Forum. It has the most information on how to work with the Pi and CODESYS as well as examples and more. There are also hints and ideas on how to use the different fieldbuses available in CODESYS.

If you are looking for information on licensing CODESYS on the Raspberry Pi and how to backup and restore a license, have a look at these links:

The CODESYS development system can be obtained from the CODESYS Store.