Difference between revisions of "How to use GPIO"
Xingxing.li (talk | contribs) |
Xingxing.li (talk | contribs) |
||
Line 1: | Line 1: | ||
− | |||
− | + | = Configuration = | |
− | The | + | The RK3399 bank/bit notation for GPIOs must be formed as "GPIO<GPIO_bank>_<gpio_bit>" |
The numeric value of GPIO is calculated as follows: | The numeric value of GPIO is calculated as follows: | ||
− | <pre>32 x (gpio_bank) + gpio_bit | + | <pre>32 x (gpio_bank) + gpio_bit |
</pre> | </pre> | ||
− | + | gpio_bit : A0~A7 0-7 B0~B7 8-15 C0~C7 16-23 D0~D7 24-31 | |
+ | |||
+ | E.g. GPIO2_B0 becomes 72 | ||
+ | |||
+ | == ROM-5780 == | ||
+ | |||
+ | {| border="1" cellpadding="0" cellspacing="1" width="500" | ||
+ | |- | ||
+ | | | ||
+ | GPIO Number | ||
+ | |||
+ | | | ||
+ | GPIO formed | ||
+ | |||
+ | | | ||
+ | Numeric Representation | ||
+ | |||
+ | |- | ||
+ | | | ||
+ | GPIO0 | ||
+ | |||
+ | | | ||
+ | GPIO1_A2 | ||
− | + | | | |
+ | 34 | ||
+ | |||
+ | |- | ||
+ | | | ||
+ | GPIO1 | ||
− | + | | | |
+ | GPIO1_A3 | ||
− | + | | | |
+ | 35 | ||
− | |||
|- | |- | ||
− | | | + | | |
− | | | + | GPIO2 |
− | | | + | |
+ | | | ||
+ | GPIO1_B1 | ||
+ | |||
+ | | | ||
+ | 41 | ||
+ | |||
|- | |- | ||
− | | | + | | |
− | | | + | GPIO3 |
− | | | + | |
+ | | | ||
+ | GPIO1_B2 | ||
+ | |||
+ | | | ||
+ | 42 | ||
+ | |||
+ | |- | ||
+ | | | ||
+ | GPIO4 | ||
+ | |||
+ | | | ||
+ | GPIO1_B5 | ||
+ | |||
+ | | | ||
+ | 45 | ||
+ | |||
|- | |- | ||
− | | | + | | |
− | | | + | GPIO5 |
− | | | + | |
+ | | | ||
+ | GPIO1_C2 | ||
+ | |||
+ | | | ||
+ | 50 | ||
+ | |||
|- | |- | ||
− | | | + | | |
− | | | + | GPIO6 |
− | | | + | |
+ | | | ||
+ | GPIO1_C4 | ||
+ | |||
+ | | | ||
+ | 52 | ||
+ | |||
|- | |- | ||
− | | | + | | |
− | | | + | GPIO7 |
− | | | + | |
+ | | | ||
+ | GPIO1_C6 | ||
+ | |||
+ | | | ||
+ | 54 | ||
+ | |||
|- | |- | ||
− | | | + | | |
− | | | + | GPIO8 |
− | | | + | |
+ | | | ||
+ | GPIO1_C7 | ||
+ | |||
+ | | | ||
+ | 55 | ||
+ | |||
|- | |- | ||
− | | | + | | |
− | | | + | GPIO9 |
− | | | + | |
+ | | | ||
+ | GPIO2_A2 | ||
+ | |||
+ | | | ||
+ | 66 | ||
+ | |||
|- | |- | ||
− | | | + | | |
− | | | + | GPIO10 |
− | | | + | |
+ | | | ||
+ | GPIO2_A3 | ||
+ | |||
+ | | | ||
+ | 67 | ||
+ | |||
|- | |- | ||
− | | | + | | |
− | | | + | GPIO11 |
− | | | + | |
+ | | | ||
+ | GPIO4_C5 | ||
+ | |||
+ | | | ||
+ | 149 | ||
+ | |||
|} | |} | ||
− | Test | + | = Test = |
+ | |||
+ | Here we take GPIO1 of 4710 as an example.3710 and 5780 are similar to set up. | ||
Export GPIO then you can use control GPIO from userr space through sysfs | Export GPIO then you can use control GPIO from userr space through sysfs | ||
− | Export | + | Export GPIO1 |
− | <pre># echo | + | <pre># echo 72 > /sys/class/gpio/export |
</pre> | </pre> | ||
Set GPIO direction to in/out | Set GPIO direction to in/out | ||
− | <pre># echo "out" > /sys/class/gpio/ | + | <pre># echo "out" > /sys/class/gpio/gpio72/direction |
</pre> | </pre> | ||
Set GPIO value 0/1 if GPIO pin define is output | Set GPIO value 0/1 if GPIO pin define is output | ||
− | <pre># echo 1 > /sys/class/gpio/ | + | <pre># echo 1 > /sys/class/gpio/gpio72/value |
</pre> | </pre> | ||
Line 75: | Line 168: | ||
Note:You have to configure GPIO to input | Note:You have to configure GPIO to input | ||
− | <pre># echo "rising" > /sys/class/gpio/ | + | <pre># echo "rising" > /sys/class/gpio/gpio72/edge |
</pre> | </pre> | ||
− | *rising: Trigger on rising edge | + | *rising: Trigger on rising edge |
− | *falling: Trigger on falling edge | + | *falling: Trigger on falling edge |
− | *both: Trigger on both edges | + | *both: Trigger on both edges |
− | *none: Disable interrupt on both edges | + | *none: Disable interrupt on both edges |
− | Unexport | + | Unexport GPIO1 |
− | <pre># echo | + | <pre># echo 72 > /sys/class/gpio/unexport |
</pre> | </pre> | ||
− | + | '''Test''' | |
− | + | GPIO1 and GPIO2 are taken as an example: | |
− | *Connect | + | *Connect GPIO1 and GPIO2 |
− | *Export | + | *Export GPIO1 and GPIO2 |
− | <pre># echo | + | <pre># echo 72 > /sys/class/gpio/export |
− | # echo | + | # echo 50 > /sys/class/gpio/export |
</pre> | </pre> | ||
− | *Set GPIO | + | *Set GPIO 1 to output |
− | <pre># echo "out" > /sys/class/gpio/ | + | <pre># echo "out" > /sys/class/gpio/gpio72/direction |
</pre> | </pre> | ||
− | *Set GPIO | + | *Set GPIO 2 to input |
− | <pre># echo "in" > /sys/class/gpio/ | + | <pre># echo "in" > /sys/class/gpio/gpio50/direction |
</pre> | </pre> | ||
− | *Change | + | *Change GPIO1 to 1 and read GPIO2value |
− | <pre># echo 1 > /sys/class/gpio/ | + | <pre># echo 1 > /sys/class/gpio/gpio72/value |
− | # cat /sys/class/gpio/ | + | # cat /sys/class/gpio/gpio50/value |
1 | 1 | ||
</pre> | </pre> | ||
− | *Change | + | *Change GPIO1 to 0 and read GPIO2 value |
− | <pre># echo 0 > /sys/class/gpio/ | + | <pre># echo 0 > /sys/class/gpio/gpio72/value |
− | # cat /sys/class/gpio/ | + | # cat /sys/class/gpio/gpio50/value |
0</pre> | 0</pre> | ||
+ | |||
+ | |
Revision as of 08:07, 1 March 2023
Configuration
The RK3399 bank/bit notation for GPIOs must be formed as "GPIO<GPIO_bank>_<gpio_bit>"
The numeric value of GPIO is calculated as follows:
32 x (gpio_bank) + gpio_bit
gpio_bit : A0~A7 0-7 B0~B7 8-15 C0~C7 16-23 D0~D7 24-31
E.g. GPIO2_B0 becomes 72
ROM-5780
GPIO Number |
GPIO formed |
Numeric Representation |
GPIO0 |
GPIO1_A2 |
34 |
GPIO1 |
GPIO1_A3 |
35 |
GPIO2 |
GPIO1_B1 |
41 |
GPIO3 |
GPIO1_B2 |
42 |
GPIO4 |
GPIO1_B5 |
45 |
GPIO5 |
GPIO1_C2 |
50 |
GPIO6 |
GPIO1_C4 |
52 |
GPIO7 |
GPIO1_C6 |
54 |
GPIO8 |
GPIO1_C7 |
55 |
GPIO9 |
GPIO2_A2 |
66 |
GPIO10 |
GPIO2_A3 |
67 |
GPIO11 |
GPIO4_C5 |
149 |
Test
Here we take GPIO1 of 4710 as an example.3710 and 5780 are similar to set up.
Export GPIO then you can use control GPIO from userr space through sysfs
Export GPIO1
# echo 72 > /sys/class/gpio/export
Set GPIO direction to in/out
# echo "out" > /sys/class/gpio/gpio72/direction
Set GPIO value 0/1 if GPIO pin define is output
# echo 1 > /sys/class/gpio/gpio72/value
Used as IRQ signal
Note:You have to configure GPIO to input
# echo "rising" > /sys/class/gpio/gpio72/edge
- rising: Trigger on rising edge
- falling: Trigger on falling edge
- both: Trigger on both edges
- none: Disable interrupt on both edges
Unexport GPIO1
# echo 72 > /sys/class/gpio/unexport
Test
GPIO1 and GPIO2 are taken as an example:
- Connect GPIO1 and GPIO2
- Export GPIO1 and GPIO2
# echo 72 > /sys/class/gpio/export # echo 50 > /sys/class/gpio/export
- Set GPIO 1 to output
# echo "out" > /sys/class/gpio/gpio72/direction
- Set GPIO 2 to input
# echo "in" > /sys/class/gpio/gpio50/direction
- Change GPIO1 to 1 and read GPIO2value
# echo 1 > /sys/class/gpio/gpio72/value # cat /sys/class/gpio/gpio50/value 1
- Change GPIO1 to 0 and read GPIO2 value
# echo 0 > /sys/class/gpio/gpio72/value # cat /sys/class/gpio/gpio50/value 0