Enable rs485 mode on iot-gate-imx8

Hello,
we are testing a project on a CompuLab iot-gate-imx8 device and we need to access the onboard serial to comunicate using Modbus protocol.
The onboard serial can be set on 232 or 485 mode, but we cannot find a way to set it on 485.
It should be on /dev/ttymxc0
Using a Debian distro we made it work using command cl_setenv uart_mode rs485
Is there a way to enable and use the port on Balena too?

I found some additional info.
The 232/485 board is based on a SP330 chip.
The /dev/ttymxc0 port is served by IMX-uart driver
How can I switch that port from 232 to 485 and viceversa?
Having to reboot the system is also fine, I just need to set it once.
Is there a U-Boot env variable to set as in the official Debian/Yocto?

Hello @MaxFerretti78 Thanks for posting your issue and researching more about it.

Let me ask my colleagues the solution for your problem.

Hi, in order to access the u-boot shell you can provision a development balena OS image. Once you do that you can hit Enter when the board boots up and then you should have access to the u-boot shell where you can inspect the environment (“pri” should list all the env)

Hi @floion and thank you for your time.
Actually, we are able to access u-boot shell from the boot process and we can set the correct environment variable from there.
The problem seems to be that current BalenaOS image for CompuLab Gateway IMX8 does not recognize the proper env variable set on U-Boot and therefore does not switch serial module to RS485.
We found an issue related to this behaviour: https://github.com/balena-os/balena-iot-gate-imx8/issues/9

Hi, indeed.
Also found another problem and PRed it: meta-balena-imx8mm:layer.conf: Fix typo for u-boot-fw-utils provider by floion · Pull Request #13 · balena-os/balena-iot-gate-imx8 · GitHub
So the next OS release should have it all fixed

I am currently working on a project evaluating the iot-gate-imx8 in combination with BalenaOS. This issue is a showstopper for us. Will the next release be availaible soon? Is there a pre-release available somewhere?

1 Like

Hi @MaxFerretti78 @AndreaAlgeri @pfrederiks , the BalenaOS version v2.80.5+rev5 uses the newer u-boot 2.3, can you check if this new version works for your usecase?

Hi @acostach, thanks for the very nice update.
We’ll try updating with the new OS as soon as we can and get back with a feedback,

Hi @AndreaAlgeri , if you are updating from IOT-GATE-IMX8 v2.71.5+rev6, you’ll need to first enter the webterminal or ssh to the device trough balena cli and execute:
mount -o remount,rw / && rm /etc/profile.d/resize.sh

After that you can trigger the OS update from the dashboard. This is necessary only when updating from 2.71.5+rev6, newer versions don’t need this.

Hi @acotash I have done a clean install with the new image. I am at a loss on how to set the uart_mode permanently. Once booted fw_setenv has no write permission to the boot partition and setting the variable from the boot prompt has no effect after boot. I am new to U-boot and balenaOS so I need a hint here.

Edit: I discovered that cl_setenv does what I need. i just got confused by fw_printenv and fw_setenv existence.

Hey, is the new image working as expected then based on your last message?

@klutchell Well, you’d have to write an U-Boot macro which reads this and potentially other configuration variables from a simple text file on the boot partition for example. For someone fluent in U-Boot, this would in theory be a simple task. (env import with the proper flags for a text file, loaded from a memory, which is loaded from a file on the boot partition), but you need someone to actually do and test it.

Until you don’t add this feature to the U-Boot startup flow of the BalenaOS image, Customers cannot use the RS485 UART mode for production.

@bkaindl so you need to set one or more u-boot variables for your use-case, right? Balena already imports an extra env file, so if you added this file /mnt/boot/extra_uEnv.txt with this content “uart_mode=rs485” it should be set in u-boot’s environment. I haven’t tested but I would expect u-boot to parse and set the uart configuration based on ‘uart_mode’ after this environment variable has been loaded from extra_uEnv.txt.

1 Like

@acostach I don’t have an iot-gate-imx8 yet, but as far as I understood, @pfrederiks asked last summer for how to permanently configure the uart_mode in BalenaOS. The link to the implementation of reading /mnt/boot/extra_uEnv.txt in the Balena U-Boot scripts is very helpful for the future, thanks!

Is this feature also documented somewhere?

Would it also be possible to update the uart_mode by setting a configuration variable in balena Cloud or from inside a container?

Hi @rob27 , managing the uart_mode on the iot-gate-imx8 from the dashboard is currently not available but it is on our roadmap. Once it will become available, setting the uart_mode from a container will be possible trough the cloud API.

That would be nice :).

I was trying to get RS-485 working on a IOT-GATE-iMX8, came across this thread and assumed that uart_mode not being correctly set was causing me problems.

However, after doing a little more digging, setting uart_mode does not appear to affect how the interface is configured (it appears to be always configured as a RS-485 on mine).

Attempting to use /mnt/boot/extra_uEnv.txt to set uart_mode does not appear to have any effect.

That said, setting uart_mode using cl_setenv uart_mode rs485 appears to persist across reboots, so maybe doing that once is sufficient?

shasderias@omaru ~> balena ssh shasderias
? Select a device on application lyc/shasderias jolly-doorway (cd69d22)
=============================================================
    Welcome to balenaOS
=============================================================

# confirmed that modpoll within a container was working

root@cd69d22:~# cat /mnt/boot/extra_uEnv.txt
uart_mode=rs485

root@cd69d22:~# fw_printenv uart_mode
uart_mode=rs232
root@cd69d22:~# reboot
shasderias@omaru ~> balena ssh shasderias
? Select a device on application lyc/shasderias jolly-doorway (cd69d22)
=============================================================
    Welcome to balenaOS
=============================================================
root@cd69d22:~# fw_printenv uart_mode
uart_mode=rs232

# confirmed that modpoll within a container was working

root@cd69d22:~# rm /mnt/boot/extra_uEnv.txt
root@cd69d22:~# reboot
shasderias@omaru ~> balena ssh shasderias
? Select a device on application lyc/shasderias jolly-doorway (cd69d22)
=============================================================
    Welcome to balenaOS
=============================================================
root@cd69d22:~# fw_printenv uart_mode
uart_mode=rs232

# confirmed that modpoll within a container was working

root@cd69d22:~# uname -a
Linux cd69d22 5.10.72-iot-gate-imx8-3.0 #1 SMP PREEMPT Tue Nov 23 06:02:20 UTC 2021 aarch64 aarch64 aarch64 GNU/Linux