Simple Python software to control GPIO
In this part of the lab you will add GPIO ports to your hardware, then you will use it in bash shell and finally in Python3 software.
Add GPIO hardware
In Vivado add the hardware blocks AXI GPIO (in block diagram) to your system and connect them to LEDs and buttons on Zybo board. Note the addresses on AXI bus of the added GPIOs. This will be needed later to identify the GPIO ports in Linux.
Add required layer and software (Python gpiod)
To use the GPIOs in Python, you will need gpiod package for Python.
Login to your Yocto build system on workstation:
- Add new layer needed for this package: meta-openembedded/meta-python:
bitbake-layers add-layer ../meta-openembedded/meta-python
- Build rpm:
bitbake python3-gpiod
First test of GPIOs
Continue on Zybo board.
- Install new RPM on Zybo Linux board
- Identify the GPIO ports on the board:
- Check in sys:
ls /sys/class/gpio/<gpioXXX>/label
- Look at device tree, then see which GPIO chips are available in your system:
tree /sys/class/gpio /sys/class/gpio |-- export |-- gpiochip1016 -> ../../devices/soc0/amba_pl/41210000.gpio/gpio/gpiochip1016 |-- gpiochip1020 -> ../../devices/soc0/amba_pl/41200000.gpio/gpio/gpiochip1020 |-- gpiochip898 -> ../../devices/soc0/axi/e000a000.gpio/gpio/gpiochip898 `-- unexport
Each gpiochipN represents a GPIO controller.
- For each chip read base and number of pins.
cat /sys/class/gpio/gpiochip1020/base cat /sys/class/gpio/gpiochip1020/ngpio
For example for:
base = 1020 ngpio = 4
that means that we can use GPIOs: 1020, 1021, 1022, 1023.
- Export single pin for LED0 … LED3:
echo 1020 > /sys/class/gpio/export 2>/dev/null echo 1021 > /sys/class/gpio/export 2>/dev/null echo 1022 > /sys/class/gpio/export 2>/dev/null echo 1023 > /sys/class/gpio/export 2>/dev/null
- Now you can use the names: gpio1020, gpio1021, gpio1022, gpio1023. Set each pin's direction as output:
echo out > /sys/class/gpio/gpio1020/direction echo out > /sys/class/gpio/gpio1021/direction echo out > /sys/class/gpio/gpio1022/direction echo out > /sys/class/gpio/gpio1023/direction
- Turn on LEDs:
echo "1" > /sys/class/gpio/gpio1020/value echo "1" > /sys/class/gpio/gpio1021/value echo "1" > /sys/class/gpio/gpio1022/value echo "1" > /sys/class/gpio/gpio1023/value
- To turn off LEDs:
echo "0" /sys/class/gpio/gpio1020/value echo "0" /sys/class/gpio/gpio1021/value echo "0" /sys/class/gpio/gpio1022/value echo "0" /sys/class/gpio/gpio1023/value
- Similarly, check the switches. i.e for SW0:
echo 1016 > /sys/class/gpio/export 2>/dev/null echo in > /sys/class/gpio/gpio1016/direction cat /sys/class/gpio/gpio1016/value
Unexport all used GPIO pins before you start using GPIOs in Python:
echo 1020 > /sys/class/gpio/unexport echo 1021 > /sys/class/gpio/unexport echo 1022 > /sys/class/gpio/unexport echo 1023 > /sys/class/gpio/unexport echo 1016 > /sys/class/gpio/unexport
Python3 software to mirror buttons to LEDs
Write a Python3 script to continuously read all 4 switches and reflect their state on 4 LEDs. Use the commands from gpiod package. Here are some hints regarding the commands that can be used:
import gpiod gpiod.Chip(dev) gpiod.LineSettings() gpiod.line.Direction gpiod.line.Value gpiod.line.Value.INACTIVE)
| Previous step: Adding new software packages to your Yocto |




