GPIO Kernel Driver on Yocto - cu-ecen-aeld/yocto-assignments-base GitHub Wiki

Description

This page helps you integrate a GPIO Device Driver using syscalls into your Yocto build and test it out by controlling an LED.

Links to Implementation

  1. GPIO open source repository
  2. Forked local repository
  3. Load script for driver
  4. Unload script for driver
  5. Recipe for GPIO driver

Finding the GPIO to Use

The following steps rely on knowing which GPIO you want to use and how the kernel refers to the GPIO. To do this, it may be helpful to probe /sys/kernel/debug/gpio on target:

image

The GPIO number used in the following steps is found in the leftmost column; for example, gpio-514 corresponds to GPIO number 514 in the kernel. Once a pin is in use in the kernel, it is assigned a name that is visible here (like RGB_R on gpio-514 or ID_SDA on gpio-512). Pins that already have a name that isn't GPIOx may have reserved usage or be inaccessible for external connections.

It may be helpful to build a test driver that simply toggles the pin to perform some trial-and-error to determine which pin you want to use. It may be helpful to parameterize a test kernel module to test multiple pins quickly; this can be done with module parameters. Alternatively, depending on the version of the kernel, the sysfs interface may be supported to manipulate pins:

image

The above image shows toggling GPIO 517 using the sysfs interface, which may provide faster testing iteration than writing a test kernel module.

Steps to setup the driver repository

  1. Fork from the driver repository from here.

  2. See reference commit at https://github.com/Embetronicx/Tutorials/commit/e225a9ef8ba02afbcf4790d57f78fefba5fd86c7 for changes including:

  • Adding a load and unload script to load and unload at startup. This could alternatively be placed in your rootfs overlay
  • Modifying the build script to align with the makefile.
  1. Create a recipe linked to the forked repository and build your Yocto image with this recipe.
  • See example recipe at here.

Testing GPIO driver on the target

  1. Load the driver using modprobe.
modprobe aesdgpio.ko
  1. Check if driver is loaded using lsmod
  1. Turn the GPIO on/off to control the LED.
echo 1 > /dev/ext_device
echo 0 > /dev/ext_device

A small demonstration of GPIO driver

https://user-images.githubusercontent.com/89494511/235794104-82c22e6c-1915-47c6-a997-4c25bc9b14fe.MOV

References

https://embetronicx.com/tutorials/linux/device-drivers/gpio-driver-basic-using-raspberry-pi/

https://embetronicx.com/tutorials/linux/device-drivers/linux-device-driver-tutorial-part-3-passing-arguments-to-device-driver/#module_param