How-To Device Management
Description
This how-to focuses mostly on managing the device or making changes to the Raspberry Pi OS to make things easier when working with different hardware components of the Pi-Tron CM5.
Requirements
- No Requirements
How-To
Persistent Device Names
Making programs which have to work with peripherals, it makes sense to use specific device names, regardless of which device names the various system drivers create.
Serial Ports
The following example will give the Pi-Tron CM5s RS232 port the device name ttyrs232 and the RS485 port the name ttyrs485.
Create the file 99-cm5-usb-serial.rules in the folder /etc/udev/rules.d.
sudo -i
cd /etc/udev/rules.d
nano 99-cm5-usb-serial.rules
Add the following two lines to the file.
SUBSYSTEM=="tty", KERNELS=="1-1.3:1.0", SYMLINK+="ttyrs232", RUN+="/usr/bin/logger [udev] ttyRS232 created"
SUBSYSTEM=="tty", KERNELS=="1-1.3:1.1", SYMLINK+="ttyrs485", RUN+="/usr/bin/logger [udev] ttyRS485 created"
To save the changes and leave the editor press Ctrl + O → Ctrl + X.
After the next boot or reboot, udev will create two new devices called ttyrs232 and ttyrs485. Each device name is a so called soft link and will always point to the correct serial port device.
reboot
From now on, always use the serial port device ttyrs232 to send and receive data through the Pi-Tron CM5s RS232 port and the same applies to the ttyrs485 device. Don't use the ttyUSB0 and ttyUSB1 devices any longer.
To check if the rules file worked, looking in the dev folder is one option.
ls -la /dev/
Somewhere in the list of all the device should be the two serial ports with their own (new) names. Also note that the detailed listing also shows to which actual device each soft link is pointing.

Another way to check if the rules file was run, is to check the user.log file under /var/log/ with the following command.
cat /var/log/user.log | grep -w "\[udev]"
The command should yield at least two lines stating that each device was created. The text for each device is the text from the rules file from the latter part of each line starting with RUN+= . The text can be changed to anything if needed.

CODESYS and Serial Ports
When using CODESYS on the Pi-Tron CM5, the serial port names must be in sequence. This contrasts the above shown examples, because CODESYS can only be supplied with one serial port device name and does the counting of the number sequence internally. Give the RS232 port the device name ptcm5serial0 and the RS485 port the name ptcm5serial1 for example. CODESYS can then be configured to work with the name ptcm5serial. Within CODESYS COM1 would then be the RS232 port and COM2 the RS485 port.
I²C Devices
The DIOs of the Pi-Tron CM5 are connected to an MCP23017 I²C GPIO expander, which can be accessed through a gpiochip device. By creating a udev rules file the GPIO expander will receive its own persistent device name. This is important, as with the serial ports the gpiochip devices can change as well. For example, by simply adding one more serial port via the Pi-Tron CM5s USB ports, a new gpiochip device is also created. This can lead to a change in the gpiochip device name sequence amongst many other reasons.
Create the file 99-cm5-gpio.rules in the folder /etc/udev/rules.d:
sudo -i
cd /etc/udev/rules.d
nano 99-cm5-gpio.rules
Add the following line to the file:
SUBSYSTEM=="gpio", KERNELS=="1-0020", ATTRS{name}=="mcp23017", SYMLINK+="pitroncm5gpio"
To save the changes and leave the editor press Ctrl + O → Ctrl + X.
Remember to enable the MCP23017 overlay in the config.txt file. See the How-To Digital Input/Output (DIO) page for more information on this topic.
After the next boot, udev will create one new device called pitroncm5gpio, which allows access to the Pi-Tron CM5s DIOs and other connected peripherals.
Short rule overview:
| Attribute | Description |
|---|---|
| SUBSYSTEM=="gpio" | Which devices or device types should be considered |
| KERNELS=="1-0020" | This is the I²C address of the MCP23017 GPIO expander chip |
| ATTRS{name}=="mcp23017" | Name of the device driver used or chip name |
| SYMLINK+="pitroncm5gpio" | The soft link name to create when a matching device is found |
To check if the device is really there, look in to the /dev/ folder.
ls -la /dev/ | grep pitroncm5gpio
lrwxrwxrwx 1 root root 9 Jul 19 14:58 pitroncm5gpio -> gpiochip14
The command produces one line for the pitroncm5gpio device, but also shows that it's a soft link pointing to gpiochip14 in this case. On other systems this can be different.
From now on only use the device name pitroncm5gpio instead of gpiochipXY.
Device Information
If there is any need to create more rules for other devices, the question is how to find all the information needed to create such a rule.
One way is to use the program udevadm, the udev - management tool to list all available attributes and information available for one device.
The following command will list details about the device gpiochip14. Exchange this name with any other device name to list it's attributes instead, e.g. gpiochip1, ttyUSB0, ttyUSB1, serial0, serial1 and so on.
udevadm info --a --name /dev/gpiochip14
The result in this case is (output shortended):
Udevadm info starts with the device specified by the devpath and then
walks up the chain of parent devices. It prints for every device
found, all possible attributes in the udev rules key format.
A rule to match, can be composed by the attributes of the device
and the attributes from one single parent device.
looking at device '/devices/platform/soc/fe804000.i2c/i2c-1/1-0020/gpiochip14':
KERNEL=="gpiochip14"
SUBSYSTEM=="gpio"
DRIVER==""
ATTR{power/control}=="auto"
ATTR{power/runtime_active_time}=="0"
ATTR{power/runtime_status}=="unsupported"
ATTR{power/runtime_suspended_time}=="0"
ATTR{waiting_for_supplier}=="0"
looking at parent device '/devices/platform/soc/fe804000.i2c/i2c-1/1-0020':
KERNELS=="1-0020"
SUBSYSTEMS=="i2c"
DRIVERS=="mcp230xx"
ATTRS{name}=="mcp23017"
ATTRS{power/control}=="auto"
ATTRS{power/runtime_active_time}=="0"
ATTRS{power/runtime_status}=="unsupported"
ATTRS{power/runtime_suspended_time}=="0"
....
Any of the shown attributes can be used in udev rules, however the more unique an attribute is to identify a particular device the better. For example, the MCP23017 expander chip has the I²C address 0x20, which means the attribute KERNELS=="1-0020" in the subsystem gpio would be enough to identify the chip, as this I²C address is unique on the entire Pi-Tron CM5 system.
Restrictions
- No known Restrictions
Related documentation
- Covering all aspects of the Raspberry Pi, the official documentation: https://www.raspberrypi.com/documentation
- udev wiki: https://wiki.debian.org/udev
- Manpages udev: https://manpages.debian.org/bullseye/udev/udev.7.en.html
- Manpages udevadm: https://manpages.debian.org/bullseye/udev/udevadm.8.en.html
- CODESYS Control for Raspberry Pi Online Help: https://help.codesys.com