Facing issue in GPIO Configuration for Balena OS Beaglebone

I am using GPIO Pins on my Beaglebone. I have configured the Balena OS. I came across the issue that Balena OS has only these GPIO Pins available by default.

root@mydevice:/sys/class/gpio# ls
export	gpio12	gpio13	gpiochip0  gpiochip32  gpiochip64  gpiochip96  unexport

Question:

  1. I need other GPIO pins in my current project. How can I add configure new GPIO Pins on the Host OS? And also will these GPIO Pins be available on the Container?

  2. Also, I wanted to confirm if Balena OS supports all the GPIO Pins, Interfaces, PWM, I2C, and SPIO?

Hi @sharvin26 , if you run a privileged container, you have full access to /sys/class path and can use the standard GPIO as you would usually. we also have an example here for nodejs https://github.com/balena-io-playground/balena-rpi-nodejs-basic-gpio. You can also find a number of Beaglebone specific projects here https://github.com/balena-io-playground?utf8=✓&q=beaglebone . It should be possible to make use of PWM, gpio, i2c and SPI.

Yes I am running the Container under privileged mode. But I am able to see only these GPIO Pins.

In my docker-compose for that service I have given

privileged: true
# cd gpio
# ls
export	gpio12	gpio13	gpiochip0  gpiochip32  gpiochip64  gpiochip96  unexport

I saw the examples that you shared But I couldn’t find example for loading GPIO. I found example for UART.

How can I add other gpio pins that are not present here in this list?

Thanks for the Response

Hi,

To add other GPIO pins, try echo’ing the pin number to /sys/class/gpio/export like so:

echo 60 > /sys/class/gpio/export

This should then expose your pin in the filesystem.

I should add this in the container or Host Os?

If you’re running the container in privileged mode, then from the container is fine.

For Container I am getting /bin/sh: 1: cannot create /sys/class/gpio/export: Read-only file system

@Sharvin26 That is strange.

Are you doing this from inside the container as the root user?

If you grant support access and share the long device uuid, I can try to access the device for further debugging.

Regards
ZubairLK

Yes I am running this inside the container.

The command I am using to access the Container is

balena run -it --rm --entrypoint sh <image-id>

@Sharvin26 Can you try again after passing the --privileged flag as well?
When you run a new container manually, I don’t think it will be privileged by default.

I have already passed privileged in my docker-compose file.

privileged: true

Hi @Sharvin26,

The privileged flag is in the docker-compose file. But you are starting the container from the image id itself using balena run (presumably just to test the container/image?)

I don’t think anything in the docker-compose file is parsed. i.e. labels/volumes/etc. when you start a new container using docker run. So you’ll need to pass the --privileged flag.

If you have priveleged:true in your docker-compose file. And then commit it to git. Then push that to your balenaCloud application. The supervisor will update the application container. Then you can ssh into the application container itself. That will be started by the supervisor with any extra parameters passed via the docker-compose file so it will be privileged.

Hope that makes sense.

Thanks
ZubairLK

I have already tried passing privileged: true for the service in docker-compose and then passed the docker file

RUN echo 60 > /sys/class/gpio/export

It gave me the same error

> /sys/class/gpio/export: Read-only file system

Hi,

I just tested this on an old BBB i had lying around. It is running 2.15.1

root@a0af318:~# balena run --rm -i -t --privileged balenalib/beaglebone-black-debian /bin/bash
root@0865099d57f7:/# ls /sys/class/gpio/
export  gpio12  gpio13  gpiochip0  gpiochip32  gpiochip64  gpiochip96  unexport
root@0865099d57f7:/# echo 60 > /sys/class/gpio/export 
root@0865099d57f7:/# ls /sys/class/gpio/
export  gpio12  gpio13  gpio60  gpiochip0  gpiochip32  gpiochip64  gpiochip96  unexport
root@0865099d57f7:/# 

Can you reproduce that on your BBB?

Regards
ZubairLK

Yes that’s working. Any Idea How can I add that in my docker file Because I am getting error operation not permitted.

Can give us more details about what you mean? I’m suspecting that you are trying to access the device’s GPIOs in the Dockerfile which can’t work because the Dockerfile is used by the builder in order to produce the final image. What you need is to run these kind of commands on the target as a script, service etc.

Yes, exactly I am trying to do that I have created a Dockerfile i.e. Service and passing the command to load the GPIO pin using the RUN command.

At that time it gives the filesystem is read-only.Thus I created a shell script and made that script to executable. After running that script I got the error operation not permitted.

my shell script:

mount -o remount,rw /
echo 60 > /sys/class/gpio/export
./server

I am getting error for this line echo 60 > /sys/class/gpio/export

Inside my docker file I am running shell script as follows:

CMD /usr/src/app/start.sh

Can you share with us a minimum test case that we can see and reproduce?

I have a use case where I am using GPIO pins in my application. I am have docker-compose through which I am running multiple services ( Application )

Note I am running the service in privileged mode by passing privilege=true in my docker-compose file for the service.

I have a shell script that helps me in configuring the GPIO Pin. As the system is mounted as read-only I pass mount -o remount,rw /

mount -o remount,rw /
echo 60 > /sys/class/gpio/export
./server

For Running this shell script I am passing the following command:

CMD /usr/src/app/start.sh

Now When I use command balena push <ip-address>

For echo 60 > /sys/class/gpio/export I get error operation not permitted

Question:

  1. I want to have a container with privileges to read-write operation which I am achieving by passing mount -o remount,rw / Is this right method or should I follow different way?

  2. How can I solve the above issue of setting GPIO Pins for my docker container?

@sharvin26 it sound’s like the container is not being set to privileged for whatever reason. Could you please copy-paste the relevant service section from your compose file here and we can see if there is something there.