Python in Torizon

Hi,

I’m trying to use GPIO on Torizon core with Python in Visual Studio Code. I use the example described in Python in Linux | Toradex Developer Center and I get the following exception:
OSError: [Errno 30] Read-only file system: ‘/sys/class/gpio/export’

I presume that I should access gpio as described in How to Use GPIO on TorizonCore | Toradex Developer Center, modifying the python example to use gpiochip instead of sysfs (changing the gpio files paths).
Do I need to add anything special in VS Code - Configurations - Custom Properties so the container can access the gpiod devices?

Thanks.
Andreu.

Greetings @andy,

Just to clear up a few things here. You can still technically use the first method on Torizon. However you need root access in order to access the sysfs files. Also the sysfs interface for interacting with GPIOs will soon become deprecated by the mainline Linux community. Therefore I don’t recommend using this method for GPIOs in the future.

Now onto the main question. Yes libgpiod is the main generic way to configure GPIOs using Torizon. That being said you are welcome to use any other method that works for your situation, just we may not be knowledgeable about other such methods.

To use this with the Torizon Visual Studio Code extension, you’ll need to configure 2 things in the Custom Properties section.

  • To the extrapackages field you’ll need to add gpiod this will add libgpiod to your container.
  • To the buildcommands field you’ll need to add RUN usermod -a -G gpio torizon this will add the torizon user to the gpio group so that they may use libgpiod without running elevated permissions all the time.

Make sure to check the resulting Dockerfile in the “appconfig_0” folder to see if these changes took place.

Best Regards,
Jeremias

Hi Jeremias,

Thanks for your feedback, I’m starting to understand how Torizon works and I like it.

Regarding to my problem, I had to add gpiod in requirements.txt instead of extrapackages, otherwise it cannot find the module “gpiod”. I understand we need to add any python dependency that needs pip install, right?

Now the error is the following:
OSError: [Errno 0] Success: ‘cannot open GPIO device /dev/gpiochip1’
I’d say the container does not have access to the gpio devices, but I can see the line “RUN usermod -a -G gpio torizon” in the Dockerfile.debug file, so I don’t know why the access is not allowed.

Any idea?

Thanks.
Andreu.

Hi Andreu,

You are correct about the requirements.txt addition, sorry my mind was thinking C/C++ instead of python and forgot there is a gpiod equivalent in python.

Also yes I forgot one more configuration change, you’ll need to add /dev/gpiochip1 or whatever gpiochip you plan to use to the devices field in the Torizon configuration. This should allow the container to access the hardware properly.

Best Regards,
Jeremias

Hi Jeremias,

Thanks very much for your help, it finally worked.

Now off-topic, I have another question, I don’t know if I should place it in another post. If this is the case, please let me know.

We’re evaluating Torizon to use it in our application. It’s a test system, and basically the Toradex board will receive requests/commands over Ethernet (likely a webserver), and will translate back and forth over SPI. In a way, it will be some sort of a gateway. There are many ways to implement such scenario, and also it can be done in many languages. I’m very comfortable with C/C++, but maybe it is not the right approach for the webserver side. My question is, what’s your recommendation?

Thanks for your help.

Andreu.

Hi Andreu,

Based on the information you’ve provided I’d say C/C++ or python would be perfectly acceptable languages for such a device. If the device does not need some sort of UI then it certainly simplifies the choice. I don’t see any immediate issues with just sticking to C/C++ if that is what you are comfortable with.

What I can comment on is our future plans for our IDE extensions since it seems you’ve been trying out our Visual Studio and Visual Studio Code integrations. For Visual Studio Code we plan to bring C/C++ support and for Visual Studio we plan to bring .NET Core support. That being said we appreciate any feedback if you’d like to see other languages supported by either extension.

Best Regards,
Jeremias

Hi Jeremias,

Thanks for your feedback. I’m thinking about having two running containers, and link them to exchange data. I’ll go through the docker documentation on the matter, but I don’t know if I’ll have performance issues as I’ve never done it before.

Regarding the IDE extensions, do you know when the C/C++ support in Visual Studio Code is planned?

Thanks and regards.
Andreu.

Hi Andreu,

In our own internal testing we’ve found that containers should not impact performance in any noticeable way. What might be an issue is the extra storage overhead as containers can result in duplication of some libraries between multiple containers which does take up space. However seeing as you’re using the 1GB i.MX7 variant this should not be an issue as long as you keep track of the size of the containers you construct.

I can’t give any exact dates for the C/C++ support in Visual Studio Code, but currently it’s slated for the next big version/release of the extension which we have planned for around the end of Q2 this year.

Best Regards,
Jeremias

Hi Jeremias, thanks a lot for your comments and feedback.

You’re very much welcome.