Controlling GPIO from Linux User Space |
This application note shows how to control Vybrid GPIOs from the user level using the standard Linux GPIOLIB interface.
The generic GPIO interface is controlled by the CONFIG_GPIOLIB kernel option enabled by default in the rootfs project installed to each Vyrid System-on-Module shipped by Emcraft. Most of the Vybrid GPIO pins can be used in different multiplexed I/O roles (for instance, some GPIO pins can be also configured as an SPI interface, etc). Depending on the requirements of your application, you need to configure the pins that you want to use as GPIO for the GPIO role and other pins for an alternative I/O function. In this application note, we configure the following pin as GPIO:
The pin multiplexing, as well as the GPIO specific configuration, is performed by the Linux kernel drivers using the information from the rootfs.dts file. Let's edit this file and add the information about the above GPIO:
#define VF610_PAD_PTA12__GPIO_5 0x014 0x000 ALT0 0x0 &iomuxc { After applying the above changes to the rootfs.dts file, rebuild the rootfs project as described in the Installing and Activating Cross Development Environment application notes and install it on the target.
Each GPIO is assigned a unique integer GPIO number within the GPIO chip range of 0 to 134 by Linux. This number is represented by the YYY component of the VF610_PAD_PTXXX__GPIO_YYY definition as described above. The first step is to add (export) the pin to the GPIO array and define it as an input using its index (5 is this example). This is done as follows: ~ # echo 5 > /sys/class/gpio/export Now, all is ready to read the value of the pin. First, connect the pin (P9.18) to the ground (P9.31), so this gpio reads 0: ~ # cat /sys/class/gpio/gpio5/value Next, break the connection of P9.18 with the ground and connect this pin to the VCC3_P instead (P9.1): ~ # cat /sys/class/gpio/gpio5/value To test this pin as output, disconnect everything from it and attach a voltemeter instead. Change the direction (don't need to export a pin that is already exported in this session): ~ # echo out > /sys/class/gpio/gpio5/direction Now run the following shell commands to turn the gpio on and off at a 1Hz frequency, observe the alternating values with the voltmeter: ~ # while [ 1 ]; do echo 1 > /sys/class/gpio/gpio5/value; sleep 1; echo 0 > /sys/class/gpio/gpio5/value; sleep 1; done
Refer to the rootfs.dts file for examples of using the standard Linux facilities for the DS2 and DS3 LEDs and the USER_BTN1 and USER_BTN2 buttons of the LCD-VF6-SOM-BSB baseboard: projects/rootfs/rootfs.dts: The LEDs are accessible via /sys/class/leds/{DS2,DS3}/brightness: ~ # # Turn on the DS2: The push buttons are accessible via the /dev/input/eventX device, and they react on pressing as follows: ~ # evtest /dev/input/event1
In Linux, you may access GPIOs using different approaches, not only the ones described in this application note above. Here are some external links that might be usefull if you decide to try an alternative approach. The following article describes accessing GPIOs from the kernel context: https://lwn.net/Articles/532714/To work with GPIOs from the user space, there are the following possibilities:
|