GPIO REFERENCE
1. Overview¶
1.1. Summary¶
GPIO adopts a standard LINUX framework and can use a unified interface to operate gpio.
1.2. GPIO NUM And PAD Correspondence Table¶
Please check the PAD name of the GPIO on the hardware circuit diagram, look up the table to find the corresponding num.
One GPIO on the hardware circuit diagram is PAD_FUART_RX, and the corresponding num=14 for operating this GPIO.
Table1-1: opening GPIOs
PAD_GPIO0 | 0 | PAD_TTL9 | 28 | PAD_SD_D3 | 56 | PAD_DP_P3 | 84 |
---|---|---|---|---|---|---|---|
PAD_GPIO1 | 1 | PAD_TTL10 | 29 | PAD_SD_GPIO | 57 | PAD_HSYNC_OUT | 85 |
PAD_GPIO2 | 2 | PAD_TTL11 | 30 | PAD_PM_SD_CDZ | 58 | PAD_VSYNC_OUT | 86 |
PAD_GPIO3 | 3 | PAD_TTL12 | 31 | PAD_PM_IRIN | 59 | PAD_HDMITX_SCL | 87 |
PAD_GPIO4 | 4 | PAD_TTL13 | 32 | PADA_IDAC_OUT_B | 60 | PAD_HDMITX_SDA | 88 |
PAD_GPIO5 | 5 | PAD_TTL14 | 33 | PADA_IDAC_OUT_G | 61 | PAD_HDMITX_HPD | 89 |
PAD_GPIO6 | 6 | PAD_TTL15 | 34 | PADA_IDAC_OUT_R | 62 | PAD_SATA_GPIO | 90 |
PAD_GPIO7 | 7 | PAD_TTL16 | 35 | PAD_PM_SPI_CZ | 63 | ||
PAD_GPIO8 | 8 | PAD_TTL17 | 36 | PAD_PM_SPI_CK | 64 | ||
PAD_GPIO9 | 9 | PAD_TTL18 | 37 | PAD_PM_SPI_DI | 65 | ||
PAD_GPIO12 | 10 | PAD_TTL19 | 38 | PAD_PM_SPI_DO | 66 | ||
PAD_GPIO13 | 11 | PAD_TTL20 | 39 | PAD_PM_SPI_WPZ | 67 | ||
PAD_GPIO14 | 12 | PAD_TTL21 | 40 | PAD_PM_SPI_HLD | 68 | ||
PAD_GPIO15 | 13 | PAD_TTL22 | 41 | PAD_PM_LED0 | 69 | ||
PAD_FUART_RX | 14 | PAD_TTL23 | 42 | PAD_PM_LED1 | 70 | ||
PAD_FUART_RX | 15 | PAD_TTL24 | 43 | PAD_SAR_GPIO0 | 71 | ||
PAD_FUART_TX | 16 | PAD_TTL25 | 44 | PAD_SAR_GPIO1 | 72 | ||
PAD_FUART_CTS | 17 | PAD_TTL26 | 45 | PAD_SAR_GPIO2 | 73 | ||
PAD_FUART_RTS | 18 | PAD_TTL27 | 46 | PAD_SAR_GPIO3 | 74 | ||
PAD_TTL0 | 19 | PAD_UART0_RX | 47 | PAD_ETH_RN | 75 | ||
PAD_TTL1 | 20 | PAD_UART0_TX | 48 | PAD_ETH_RP | 76 | ||
PAD_TTL2 | 21 | PAD_UART1_RX | 49 | PAD_ETH_TN | 77 | ||
PAD_TTL3 | 22 | PAD_UART1_TX | 50 | PAD_ETH_TP | 78 | ||
PAD_TTL4 | 23 | PAD_SD_CLK | 51 | PAD_DM_P1 | 79 | ||
PAD_TTL5 | 24 | PAD_SD_CMD | 52 | PAD_DP_P1 | 80 | ||
PAD_TTL6 | 25 | PAD_SD_D0 | 53 | PAD_DM_P2 | 81 | ||
PAD_TTL7 | 26 | PAD_SD_D1 | 54 | PAD_DP_P2 | 82 | ||
PAD_TTL8 | 27 | PAD_SD_D2 | 55 | PAD_DM_P3 | 83 |
2. GPIO Using In Kernel¶
2.1. Apply AS A GPIO Port¶
-
Purpose
Create the port as GPIO.
-
Syntax
int gpio_request(unsigned gpio, const char *label)
-
Parameter
Parameter Name Description gpio Gpio num label Specific name -
Return Value
Return Value Description 0 Successful. Other Failed.
2.2. Set As Input¶
-
Purpose
Mark gpio as input.
-
Syntax
int gpio_direction_input(unsigned gpio);
-
Parameter
Parameter Name Description gpio Gpio num -
Return Value
Return Value Description 0 Successful. Other Failed.
2.3. Set As Output¶
-
Purpose
Mark gpio as output.
-
Syntax
int gpio_direction_output(unsigned gpio, int value);
-
Parameter
Parameter Name Description gpio Gpio num value Output value -
Return Value
Return Value Description 0 Successful. Other Failed.
2.4. Get Input Level¶
-
Purpose
Get the level of the input pin.
-
Syntax
int gpio_get_value(unsigned gpio);
-
Parameter
Parameter Name Description gpio Gpio num -
Return Value
Return Value Description Int Electrical level value
2.5. Get Output Level¶
-
Purpose
Get the level of the output pin.
-
Syntax
void gpio_set_value(unsigned gpio, int value);
-
Parameter
Parameter Name Description gpio Gpio num value Output value -
Return Value
Return Value Description 0 Successful. Other Failed.
3. GPIO using In User Space¶
User space access gpio, that is, access gpio through the sysfs interface.
The following are three types of files in the /sys/class/gpio directory:
--export/unexport file
--gpioN refers to the specific gpio pin
--gpio_chipN refers to the specific gpio controller
It must be known that the above interfaces do not have standard device files and their links.
Figure3-1
3.1. export/unexport File Interface¶
/sys/class/gpio/export
is write only.
When there is no kernel code to apply for a gpio port, the user program can apply to the kernel to export the control of this gpio to user space by writing the gpio number.
For example: the user applies for the GPIO command numbered 12:
echo 12 > export
The above operation will create a node gpio12 for gpio num12. At this time, a gpio12 directory will be generated under the /sys/class/gpio
, as shown below:
Figure3-2
/sys/class/gpio/unexport
is the opposite of export.
For example, remove the node gpio12:
echo 12 > unexport
Figure3-3 remove the node gpio12
3.2. /sys/class/gpio/gpioN¶
Refers to a specific gpio port, which has the following attributes files:
direction: The direction of the gpio port, the read result is in
or out
. You can also write to the file. When writing out
, the gpio is set to output and the level defaults to low. When writing low or high, it can not only be set to output but also can set the specified output level. If the kernel or kernel code do not support, this attribute will not exist. For example, if the kernel calls gpio_export(N,0), it means that the kernel is unwilling to modify the gpio port direction attribute.
value: The level of the gpio pin, 0: low level, 1: high level; If gpio is configured as an output, this value is writable, and any non-zero value is output as a high level. If a pin is configured as an interrupt, you can call the poll(2) function to monitor. After the interrupt is triggered, poll(2) will be returned.
Figure3-4
3.3. Example¶
Non-interrupt mode
-
Read
int getGpioValue(int port), pass in the port number to be removed, and return the port value (0 or 1);
-
Write
void setGpioValue(int port, int value), pass in the port number and value to be set.
4. UBOOT Use GPIO¶
4.1. CMD:gpio -Config gpio port¶
Usage: gpio (for 2nd parameter, you must type at least 3 characters) gpio output <gpio#> <1/0> : ex: gpio output 69 1 gpio input/get <gpio#> : ex: gpio input 10 (gpio 10 set as input) gpio toggle <gpio#> : ex: gpio tog 49 (toggle) gpio state <gpio#> : ex: gpio sta 49 (get i/o status(direction) & pin status) gpio list [num_of_pins] : ex: gpio list 10 (list GPIO1~GPIO10 status)
4.2. API¶
4.2.1. Set As Input¶
-
Purpose
Mark gpio as input.
-
Syntax
void MDrv_GPIO_Pad_Odn(MS_GPIO_NUM u32IndexGPIO);
-
Parameter
Parameter Name Description u32IndexGPIO Gpio num -
Return Value
Return Value Description void
4.2.2. Set As Output¶
-
Purpose
Mark gpio as output.
-
Syntax
void MDrv_GPIO_Pad_Oen(MS_GPIO_NUM u32IndexGPIO);
-
Parameter
Parameter Name Description u32IndexGPIO Gpio num -
Return Value
Return Value Description Void
4.2.3. Get Input Level¶
-
Purpose
Get level of the input pin.
-
Syntax
U8 MDrv_GPIO_Pad_Read(MS_GPIO_NUM u32IndexGPIO);
-
Parameter
Parameter Name Description u32IndexGPIO Gpio num -
Return Value
Return Value Description unsigned char 电平值
4.2.4. Set Output As High Level¶
-
Purpose
Set the pin as high level.
-
Syntax
void MDrv_GPIO_Pull_High(MS_GPIO_NUM u32IndexGPIO);
-
Parameter
Parameter Name Description gpio Gpio num
4.3. Set Output As Low Level¶
-
Purpose
Set the pin as low level.
-
Syntax
void MDrv_GPIO_Pull_Low(MS_GPIO_NUM u32IndexGPIO);
-
Parameter
Parameter Name Description gpio Gpio num