🛠 Mastering GPIO on Banana Pi BPI-RT2

Banana Pi BPI-RT2: AX3000 WiFi 6 Router Board · avance · Un guide avancé pour configurer, lire et écrire sur les broches GPIO de la BPI-RT2 via sysfs et libgpiod.

Objectifs

This tutorial aims to master low-level interaction with the GPIO pins of the Banana Pi BPI-RT2. Unlike standard Linux boards, the Realtek RTL8198D SoC requires a precise understanding of the mapping between physical pins and Linux GPIO numbers. We will learn to identify available pins on the 5-pin and 9-pin connectors, configure directions (input/output), and manipulate logic states to interface with simple sensors or displays.

Matériel nécessaire

Étapes

Log in via SSH. The OpenWrt firmware uses the standard Linux kernel. To know the physical pin mapping, consult the technical schematic or the Device Tree Source (.dts) files provided in the Realtek BSP. The 5-pin and 9-pin connectors expose GPIO and I2C signals. Note that Linux GPIO numbers do not always correspond to the physical pin order.

  1. Access and Mapping Identification

Before using complex libraries, test direct access via the sysfs filesystem. Create a working directory and export a known GPIO (e.g., GPIO 12, verify with your mapping): ``bash echo 12 > /sys/class/gpio/export ls /sys/class/gpio/gpio12/ `` If the directory appears, the GPIO is available. If a "Device or resource busy" error occurs, the GPIO might be used by the kernel or a driver.

  1. Availability Check via sysfs

Configure the GPIO as output and light an LED connected via a resistor between the GPIO pin and Ground (GND): ``bash echo out > /sys/class/gpio/gpio12/direction echo 1 > /sys/class/gpio/gpio12/value ` The LED should light up. To turn it off, write 0. To release the resource: `bash echo 12 > /sys/class/gpio/unexport ``

  1. Output Configuration and LED Test

The sysfs method is deprecated. Install libgpiod-utils via opkg if available, or compile it. Use gpioinfo to list available GPIO chips. Identify the line corresponding to your physical pin: ``bash gpioinfo ` To set a line to high output (assuming chip0, line 12): `bash gpiodetect gpioset gpiochip0 12=1 ``

  1. Using libgpiod (Modern Method)

Configure the GPIO as input with internal pull-up if supported, otherwise use an external resistor to VCC. Read the state: ``bash echo in > /sys/class/gpio/gpio13/direction cat /sys/class/gpio/gpio13/value `` Press the button to see the value change (0 or 1 depending on wiring).

  1. Reading Input (Push-button)

Vérification

Ensure the LED turns on and off in response to gpioset commands or sysfs writes. Verify that reading the button changes value when pressed. If commands fail, check permissions (chmod) and ensure the GPIO is not reserved by the firmware for other functions (like WiFi or Ethernet).

Pour aller plus loin

Explore the I2C interface exposed on the same connectors to connect digital sensors (temperature, humidity). Use i2cdetect to scan the bus. For real-time applications, consider writing custom kernel modules using the Linux kernel GPIO APIs, rather than system calls from user space, to reduce latency.

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials