Control GPIO pins
List the GPIO pins of a board and read or write them through the character devices under /devices/gpio/.
Xila mounts the GPIO pins of a board as character devices under /devices/gpio/. This guide shows how to list the pins of your board and read or write them. Install Xila on a board first, then log in on the serial console. For the features of each board, see Supported hardware.
Before you begin
Section titled “Before you begin”- Xila installed on the board and a logged-in command-line shell. See Install Xila on a board.
- A board whose firmware mounts GPIO devices. The tabs below list the devices of each board.
List the GPIO devices of your board
Section titled “List the GPIO devices of your board”- In the Xila shell, list the mounted GPIO devices:
ls /devices/gpioThe output lists the devices of your board:
| GPIO device | Board resource | Notes |
|---|---|---|
/devices/gpio/d0 to /devices/gpio/d13 |
Header digital pins | |
/devices/gpio/a0 to /devices/gpio/a7 |
Header analog pins | |
/devices/gpio/d13 |
Onboard yellow LED | Active-high, initially off |
/devices/gpio/led_red, /devices/gpio/led_green, /devices/gpio/led_blue |
Onboard RGB LEDs | Active-low, initially off |
Internal power-enable lines (sensor power rails and I2C pull-ups) remain reserved and cannot be altered through GPIO devices.
| GPIO device | Notes |
|---|---|
/devices/gpio/pin2 |
Strapping pin |
/devices/gpio/pin4 |
|
/devices/gpio/pin5 |
Strapping pin |
/devices/gpio/pin12 |
Strapping pin |
/devices/gpio/pin25 |
|
/devices/gpio/pin26 |
|
/devices/gpio/pin27 |
|
/devices/gpio/pin32 |
|
/devices/gpio/pin33 |
|
/devices/gpio/pin34 |
Input only |
/devices/gpio/pin35 |
Input only |
/devices/gpio/pin36 |
Input only |
The firmware withholds every other pin, so that no GPIO device can change the display, the touch panel, the console, the flash or the memory.
The firmware mounts exactly these GPIO devices:
| GPIO device |
|---|
/devices/gpio/pin10 |
/devices/gpio/pin11 |
/devices/gpio/pin12 |
/devices/gpio/pin13 |
/devices/gpio/pin14 |
/devices/gpio/pin21 |
The firmware withholds every other pin, so that no GPIO device can change the display, the touch panel, the console, the flash or the memory.
Read and write a pin
Section titled “Read and write a pin”- In the Xila shell, read the device file of the pin to read its state:
cat /devices/gpio/d13The device outputs 0 or 1 followed by a newline.
- Write to the device file of the pin to update its output level:
# Turn on the yellow LED:echo 1 > /devices/gpio/d13
# Turn off the yellow LED:echo 0 > /devices/gpio/d13For the active-low RGB LEDs, writing 0 turns the LED on, and writing 1 turns it off:
# Turn on the red LED:echo 0 > /devices/gpio/led_red
# Turn off the red LED:echo 1 > /devices/gpio/led_redWriting any character other than 0 or 1 (surrounded by optional whitespace) returns an error.
The command-line shell has no command that sends GPIO control commands. A read or write on a GPIO device fails with DeviceUnavailable until an application configures the pin.
Applications use the CONFIGURE, RELEASE and GET_CAPABILITIES control commands of kind b'g' on /devices/gpio/pin<number>, and then read or write one byte (0 or 1). RELEASE returns the pin to high impedance. Closing the device does not release the pin: the pin keeps its configuration, and an output keeps its level, until an application sends RELEASE or CONFIGURE again. See Peripherals.
The command-line shell has no command that sends GPIO control commands. A read or write on a GPIO device fails with DeviceUnavailable until an application configures the pin.
Applications use the CONFIGURE, RELEASE and GET_CAPABILITIES control commands of kind b'g' on /devices/gpio/pin<number>, and then read or write one byte (0 or 1). RELEASE returns the pin to high impedance. Closing the device does not release the pin: the pin keeps its configuration, and an output keeps its level, until an application sends RELEASE or CONFIGURE again. See Peripherals.
Verify the result
Section titled “Verify the result”- In the serial terminal, verify that the shell prompt (
user@host:directory$) appears after logging in. - Run
ls /devices/gpioand check that the output lists the pins of your board.
- Run
echo 1 > /devices/gpio/d13and observe that the yellow onboard LED lights up. - Run
echo 0 > /devices/gpio/d13and observe that the yellow LED turns off.
- Run
cat /devices/gpio/pin2before an application configures the pin. The read fails withDeviceUnavailable, as expected.
- Run
cat /devices/gpio/pin10before an application configures the pin. The read fails withDeviceUnavailable, as expected.