🛠 Configuring GPIO and IIO I/O on BPI-5202 Loongson
Objectifs
This tutorial aims to master interfacing external peripherals via the 20-pin GPIO port and the IIO (Industrial I/O) subsystem on the BPI-5202. Unlike consumer development boards, this industrial card uses a LoongArch architecture with specific internal bus characteristics for extension modules (DI, DO, AI, AO). The goal is to read a logical state (button) and control an output (LED) or read analog data if an AI module is connected, using standard Linux tools adapted for Loongnix or KylinOS.
Matériel nécessaire
- BPI-5202 board with stable DC 24V power supply.
- Installed OS (Loongnix or KylinOS V10 recommended for native GPIO drivers).
- Compatible GPIO extension module (DI/DO or AI/AO) plugged into the internal bus.
- A push-button, an LED with a resistor (330Ω), and connecting wires (2.54mm pitch).
- A multimeter for voltage verification.
- Isolated RS232 cable for console access.
Étapes
The 20-pin port exposes internal GPIO controller lines. In Linux, these lines are dynamically mapped. Open a terminal and identify available GPIO controllers: ``bash ls /sys/class/gpio/ `` Note the main controller number (e.g., gpiochip0). To know the physical pins exposed on the 20-pin connector, consult the specific technical documentation for the extension module used, as mapping depends on the internal bus (I2C/SPI/Native GPIO).
- Identify GPIO pins:
To control an LED connected to an output pin (assume GPIO 10 for example), export it to user space: ``bash echo 10 > /sys/class/gpio/export ` Set direction to output: `bash echo out > /sys/class/gpio/gpio10/direction ``
- Export a GPIO pin:
Turn on the LED by writing 1 to value: ``bash echo 1 > /sys/class/gpio/gpio10/value ` Turn it off with 0: `bash echo 0 > /sys/class/gpio/gpio10/value ``
- Control output (LED):
For a button connected to an input pin (assume GPIO 11), export and set direction to input: ``bash echo 11 > /sys/class/gpio/export echo in > /sys/class/gpio/gpio11/direction ` Read the logical state (0 or 1): `bash cat /sys/class/gpio/gpio11/value ``
- Read input (Button):
If an Analog extension module is connected, native GPIO is insufficient. Use IIO tools. Install libiio-tools if needed. List available channels: ``bash iio_info ` Read a raw value from an analog channel (e.g., in_voltage0_raw): `bash cat /sys/bus/iio/devices/iio\:device0/in_voltage0_raw ``
- Use IIO subsystem (for AI/AO modules):
Vérification
- GPIO: Physically observe the LED turning on/off according to the
echocommand. Use a multimeter to confirm 3.3V or 5V (depending on module logic voltage) on the output pin. - Input: Press the button and verify the
catcommand returns1(or0depending on pull-up/pull-down wiring). - IIO: Raw values should vary coherently if you modify the input voltage of the AI module (e.g., potentiometer).
Pour aller plus loin
Explore using libgpiod for more robust GPIO management, avoiding sysfs file concurrency issues. For critical real-time applications, consider using the IIO API in C via libiio for reduced latency. Remember to secure GPIO access if your application runs in a multi-user environment.