Use /sys/class/gpio for MCU debugging

Use /sys/class/gpio for MCU debugging

GPIO (General Purpose Input/Output) is a general purpose input/output pin, which is a common communication protocol. In microcontrollers and circuit boards, GPIO is widely used for digital input and output control. In the Linux system, through the operation of the relevant files in the /sys/class/gpio directory, the GPIO port can be read, written and configured.

This article will introduce how to use the /sys/class/gpio directory to debug MCU under Linux system.

  1. Confirm GPIO pin number

Before starting GPIO debugging, you need to confirm the number of GPIO pins. If you are using a development board such as Raspberry Pi, you can view the pin diagram on the corresponding official website. If you are using other microcontrollers or boards, you will need to refer to the relevant literature manual for the pin numbers.

  1. Create GPIO and configure

After confirming the GPIO pin number, we need to create a new GPIO port under the /sys/class/gpio directory. This can be achieved with the following command:

sudo echo [GPIO编号] > /sys/class/gpio/export

Among them, [GPIO number] is the GPIO pin number we confirmed earlier.

After the creation is complete, the system will automatically create a new folder under the /sys/class/gpio directory, named after the number of the GPIO pin. In this folder, we can set the status of GPIO and perform other configurations. for example:

  • Set GPIO direction (input/output)

echo out > direction # set GPIO to output mode
echo in > direction # set GPIO to input mode

  • Set GPIO level (high level/low level)

echo 1 > value #

Guess you like

Origin blog.csdn.net/qq_37934722/article/details/132242682