Feiling Embedded Technology Post - How to use the GPIO of i.MX9352?

Before, the editor introduced the method of operating GPIO on the Feiling embedded i.MX6ULL development board. In this issue, let's get closer to the i.MX9352 processor and learn more about how the GPIO of this new member of the i.MX series operates, and what are the similarities and differences between it and the predecessor i.MX6ULL processor.

01 Hardware principle analysis

Taking lighting and buttons as an example, open the schematic diagram of the Feiling embedded OK-MX9352-C development board, and you can see an LED light, which is controlled by MX93_PAD_CCM_CLKO4. Since this GPIO belongs to the 1.8V level domain, the GPIO is not directly connected to the LED on the bottom board of the OK-MX9352-C development board, but a MOS tube is controlled by the GPIO, and then the MOS tube is used to control the LED. On and off - when the GPIO level is high, the MOS is turned on and the LED is on; when the GPIO level is low, the MOS is off and the LED is off.

02 Device tree pin multiplexing

Open the device tree of Feiling embedded OK-MX9352-C development board:

OKMX93-linux-kernel/arch/arm64/boot/dts/freescale/OK-MX93-C.dts

Create a new group of pin multiplexing under the iomuxc node. The two multiplexed GPIOs are the LED light D6 and the key K1 on the bottom board.

Next, create a new GPIO node with the following content:

Comment out the LED and KEY parts to prevent multiplexing conflicts:

Recompile the device tree after saving and exiting.

Execute environment variables: 

forlinx@ubuntu:~/ok-mx93/OKMX93-linux-sdk$ . environment-setup-aarch64-toolchain

Compile the device tree separately:

forlinx@ubuntu:~/ok-mx93/OKMX93-linux-sdk/OKMX93-linux-kernel$ make ARCH=arm64 CROSS_COMPILE=aarch64-poky-linux- dtbs

After compiling, update the device tree individually. First insert the U disk into the virtual machine, and copy the generated device tree file to the U disk:

forlinx@ubuntu:~/ok-mx93/OKMX93-linux-sdk/OKMX93-linux-kernel$ cp arch/arm64/boot/dts/freescale/OK-MX93-C.dtb /media/forlinx/2075-A0A7/

Copy the generated dtb file to the OK-MX9352-C development board using a U disk, and replace: /run/media/Boot-mmcblk0p1/OK-MX93-C.dtb

root@ok-mx93:/run/media/Boot-mmcblk0p1# cp /run/media/sda/OK-MX93-C.dtb ./

Restart the OK-MX9352-C development board.

03 Pass command test

In the OKMX6ULL-S development board, the way to operate GPIO is realized by operating the files under /sys/class/gpio. On the OK-MX9352-C, a new Lingpiod method is introduced, and the original sysfs-based operation method is no longer supported.

Libgpiod is a character device interface, and GPIO access control is implemented by manipulating character device files (such as /dev/gpiodchip0). OK-MX9352-C has 4 groups of GPIO, you can check the GPIO device file under /dev.

Libgpiod can be used in two ways through the shell terminal and the C library. This section introduces the method of controlling GPIO in the shell terminal. In the next section, we will introduce the method of using the C library to control GPIO.

3.1 gpiodetect

View all GPIO devices

The gpiochip0-gpiochip4 here correspond to the four groups of GPIOs GPIO1-GPIO4 in the device tree respectively, but the order is not one-to-one correspondence, which is caused by the order of the corresponding register addresses. So how does goiochip0 correspond to the device tree? We can open the dtsi file in the device tree, the file path is:

OKMX93-linux-kernel/arch/arm64/boot/dts/freescale/imx93.dtsi

forlinx@ubuntu:~/ok-mx93/OKMX93-linux-sdk$ vi OKMX93-linux-kernel/arch/arm64/boot/dts/freescale/imx93.dtsi

View the register base address of GPIO3: gpio3: gpio@43820080, corresponding to gpiochip1, and GPIO4 corresponding to gpiochip2.

The corresponding relationship of other GPIOs is as follows, for your reference:

3.2 gpioinfo

It can be seen from 3.1 that the LED light corresponds to GPIO4, which is gpiochip2; the key corresponds to GPIO3, which is gpiochip1. List the pins of the gpiochip2 controller:

3.3 gpiose

This command is used to set the GPIO level, 2 represents gpiochip2, that is, GPIO4, 28 is GPIO pin, when the GPIO is set to 1, D6 on the bottom board lights up.

3.4 gpioget

This command is used to obtain the status of GPIO pins. Take the button as an example, the button corresponds to GPIO3-27, that is, gpiochip1 27. When the button is not pressed, the button status is read as 1, and when the button is pressed, the button status is read as 0.

3.5 gpiomon

Monitor whether the state of GPIO changes, also take the button as an example, when the button is pressed:

04 Programming with the Libgpiod library

Libgpiod is a C library and tool for interacting with Linux GPIO. Linux officially introduced the function of Libgpiod in version 4.8 of Linux. However, in the Linux5.15 kernel version carried on the OK-MX9352-C development board, it no longer supports sysfs to operate GPIO. Compared with sysfs, Libgpiod is more reliable and has more functions, for example, it can read and write multiple GPIO values ​​at a time.

4.1 Source code acquisition

If you want to cross-compile an application that can run on the development board on the PC, the library files linked during cross-compilation should be consistent with those on the development board, and you can directly copy the library on the development board to the development environment for use. The path of the library file on the development board is:

As can be seen from the figure above, the version of the Libgpiod library is libgpiod.so.2.2.2, and the soft link is to libgpiod.so.2.

In the OK-MX9352-C development board information provided by Feiling Embedded, the required library files, header files and related routines have been packaged, and users can use them directly. The data path is: User Data/Application Notes/OK-MX9352-C-GPIO Interface_Linux Application Notes/Libgpiod Test Source Code .

4.2 Compile the test routine

Copy gpiotest.c, gpio-toggle.c, lib.tar.bz2 in the Libgpiod test source directory to the development environment:

Unzip lib.tar.bz2 to this directory, and the gpiod.h file and Libgpiod library file inside will be used when compiling:

Example 1

Loop control LED on and off, the time interval is 1s

Cross compile gpio-toggle.c

Set the environment variable (note. there is a space after it)

cross compile

Copy the executable file gpio-toggle to the development board and execute it to see that the LED (D6) is on for 1s and off for 1s. The input parameters 2 and 28 are: gpiochip2 line28.

Example 2

The button controls the LED to turn on and off, and the state is reversed every time you press it

Cross compile gpio-test.c

Set environment variables (note that there is a space after the point)

cross compile

Copy the executable file gpio-test to the OK-MX9352-C development board and execute it. You can see that every time you press a button, the state of the LED light will flip once. The input parameters 1 and 27 are: gpiochip1 line27; 2, 28 For: gpiochip2 line28.

The above is the process of configuring GPIO for the OK-MX9352-C development board. I hope it can be helpful to the engineers in front of the screen.

Guess you like

Origin blog.csdn.net/m0_46665078/article/details/130702667