How to configure GPIO and toggle led on apalis imx 8 (torizoncore)using python

Hi,
I am trying to toggle led using GPIO on apalis imx8(apalis eval board) using python
Please let me know some pointers on how to configure the same.

Thanks,
Shardul

Hi @ShardulC ,

Thanks for writing Toradex Community!

One can install pyhton package for libgpiod that is python3-libgpiod in application container. python3-libgpiod can be used with in python script to manage gpio lines.

sudo apt update
sudo apt install python3-libgpiod

For example, let’s assume we need to access MXM3 X1 pin 5 (Apalis GPIO3) from an Apalis iMX8, which is accessible at LSIO.GPIO0_IO12 . This means we need to access GPIO bank 0 line 12.
Here is the short python script to make the pin logic high

import gpiod

chip=gpiod.Chip('gpiochip0')
lines = chip.get_lines([ 12 ])
lines.request(consumer='gpio', type=gpiod.LINE_REQ_DIR_OUT)
lines.set_values([ 1 ])

Important: While running docker run it is important to pass gpio chip so that can be accessed inside container.

Please check below article for more detail.
https://developer.toradex.com/knowledge-base/libgpiod

Let me know if you face any issue.

Best Regards
Ritesh Kumar

Hey @ShardulC -

I work in C#/C++ but I found the hard part was identifying the bank and line for a given header pin on the carrier board. Here is my recipe using the iMX8 and Ixora board. The algorithm should be the same for your hardware.

// To find bank and line:
// 1. Identify pin on carrier board. Sample code is using GPIO1 which is pin 13 on X27 connector
// on Ixora carrier board.
// 2. From carrier board data sheet, find MXM3 pin, here it’s pin 1
// 3. From the SOM data sheet, identify the GPIO bank and line for the given pin
// Most of the SOM pins have multiple uses. Look for the LSIO.GIPOx.IOyy entry in the
// alternate functionality columns. For pin 1, alt3 has LSIO.GPIO0.IO08 which corresponds
// to bank 0, line 8. ^ ^^

// For the Ixora carrier board:
// Name X27 pin MXM3 pin Bank Line
// GPIO1 13 1 0 8
// GPIO2 14 3 0 9
// GPIO3 15 5 0 12
// GPIO4 16 7 0 13
// GPIO5 17 11 4 1
// GPIO6 18 13 4 2
// GPIO7 19 15 3 26
// GPIO8 20 17 3 28

Also, you will need to expose the appropriate driver to your system. I’m using pins on bank 0 so I exposed /dev/gpiochip0.

Good luck!
Jeff

Hi @ritesh.tx Thanks for the reply. I tried following steps…

  1. Ran the command …
    docker run --rm -it --device /dev/gpiochip0 torizonextras/arm64v8-gpiod

  2. Ran commands you mentioned in the open container…
    sudo apt update
    sudo apt install python3-libgpiod

  3. created a file in nano editor led.py with following…
    import gpiod

chip=gpiod.Chip(‘gpiochip0’)
lines = chip.get_lines([ 12 ])
lines.request(consumer=‘gpio’, type=gpiod.LINE_REQ_DIR_OUT)
lines.set_values(1)

  1. on running python3 led.py… I am getting…
    lines.set_values(1)
    TypeError: Number of values must correspond with the number of lines

Any clues/pointers about this…

Thanks,
Shardul

Hi @ShardulC ,

Please check we have updated the reply for lines.set_values([ 1 ]).

Let us know if this also not working for you.

Best Regards
Ritesh Kumar

@jpinterp Thanks a lot for help…could understand the relation between eval board pins and corresponding imx8 functions with the details provided. Led toggle worked.

Thanks,
Shardul

Thanks a lot @ritesh.tx Led functionality worked with the python sample provided.

Thanks,
Shardul