🛠 Configuring GPIO and IIO I/O on BPI-5202 Loongson

BPI-5202 Loongson 2K1000LAi Embedded Single Board Industrial Computer · intermediaire · Apprenez à interfacer des capteurs et actionneurs via le bus GPIO et le sous-système IIO sur la carte industrielle BPI-5202.

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

É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).

  1. 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 ``

  1. 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 ``

  1. 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 ``

  1. 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 ``

  1. Use IIO subsystem (for AI/AO modules):

Vérification

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.

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials