Difference between revisions of "How to use GPIO"

From ESS-WIKI
Jump to: navigation, search
Line 3: Line 3:
 
==== Configuration ====
 
==== Configuration ====
  
The general-purpose interface combines eight general-purpose input/output (GPIO) banks.
+
The RK3288 bank/bit notation for GPIOs must be formed as "GPIO<GPIO_bank>_<gpio_bit>" 
  
Each GPIO module provides 32 dedicated general-purpose pins with input and output capabilities; thus, the general-purpose interface supports up to 256 (8 × 32) pins.
+
The numeric value of GPIO is calculated as follows:
 +
<pre>32 x (gpio_bank) + gpio_bit - 8
 +
</pre>
 +
 
 +
Group GPIO0 only 24 gpios,So each GPIO Num subtracts 8.
 +
 
 +
gpio_bit&nbsp;: A0~A7 0-7 B0~B7 8-15 C0~C7 16-23 D0~D7 24-31
  
The numeric value of GPIO&nbsp;is calculated as follows:
+
E.g. GPIO2_A4 becomes 60
<pre>32 x (gpio_bank - 1) + gpio_bit</pre>
 
  
ROM-7510
+
RSB-4680
  
{| cellpadding="1" cellspacing="1" border="1" style="width: 500px;"
+
{| cellspacing="1" cellpadding="1" border="1"
 
|-
 
|-
 
| GPIO Number
 
| GPIO Number
| Linux OS(/sys/class/gpio/)
+
| GPIO formed
| Default direction
+
| Numeric Representation
 
|-
 
|-
 
| GPIO0
 
| GPIO0
| gpio97<br/>
+
| GPIO7_A3<br/>
| in<br/>
+
| 219<br/>
 
|-
 
|-
 
| GPIO1<br/>
 
| GPIO1<br/>
| gpio218<br/>
+
| GPIO7_A4<br/>
| in<br/>
+
| 220<br/>
 
|-
 
|-
 
| GPIO2<br/>
 
| GPIO2<br/>
| gpio98<br/>
+
| GPIO7_A5<br/>
| in<br/>
+
| 221<br/>
 
|-
 
|-
 
| GPIO3<br/>
 
| GPIO3<br/>
| gpio219<br/>
+
| GPIO7_C5<br/>
| in<br/>
+
| 237<br/>
 
|-
 
|-
 
| GPIO4<br/>
 
| GPIO4<br/>
| gpio100<br/>
+
| GPIO8_A2<br/>
| in<br/>
+
| 250<br/>
 
|-
 
|-
 
| GPIO5<br/>
 
| GPIO5<br/>
| gpio68<br/>
+
| GPIO8_A3<br/>
| in<br/>
+
| 251<br/>
 
|-
 
|-
 
| GPIO6<br/>
 
| GPIO6<br/>
| gpio101<br/>
+
| GPIO8_A0<br/>
| in<br/>
+
| 248<br/>
 
|-
 
|-
 
| GPIO7<br/>
 
| GPIO7<br/>
| gpio69<br/>
+
| GPIO8_A1<br/>
| in
+
| 249
 
|}
 
|}
  
Line 56: Line 61:
  
 
Export GPIO0
 
Export GPIO0
<pre># echo 97 > /sys/class/gpio/export
+
<pre># echo 219 > /sys/class/gpio/export
 
</pre>
 
</pre>
  
 
Set GPIO direction to in/out
 
Set GPIO direction to in/out
<pre># echo "out" > /sys/class/gpio/gpio97/direction
+
<pre># echo "out" > /sys/class/gpio/gpio219/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/gpio97/value
+
<pre>#  echo 1 > /sys/class/gpio/gpio219/value
 
</pre>
 
</pre>
  
Line 70: Line 75:
  
 
Note:You have to configure GPIO to input
 
Note:You have to configure GPIO to input
<pre>#  echo "rising" > /sys/class/gpio/gpio97/edge
+
<pre>#  echo "rising" > /sys/class/gpio/gpio219/edge
 
</pre>
 
</pre>
  
Line 79: Line 84:
  
 
Unexport GPIO0
 
Unexport GPIO0
<pre># echo 97 > /sys/class/gpio/unexport
+
<pre># echo 219 > /sys/class/gpio/unexport
 
</pre>
 
</pre>
  
 
==== Test ====
 
==== Test ====
  
GPIO&nbsp;97 and GPIO&nbsp;218 are taken as an example:
+
GPIO 219 and GPIO 220 are taken as an example:
  
*Connect GPIO&nbsp;97 and GPIO 218
+
*Connect GPIO 219 and GPIO 220
*Export GPIO&nbsp;97 and GPIO 218
+
*Export GPIO 219 and GPIO 220
<pre># echo 97 > /sys/class/gpio/export
+
<pre># echo 219 > /sys/class/gpio/export
# echo 218 > /sys/class/gpio/export
+
# echo 220 > /sys/class/gpio/export
 
</pre>
 
</pre>
  
*Set GPIO&nbsp;97 to output
+
*Set GPIO 219 to output
<pre># echo "out" > /sys/class/gpio/gpio97/direction
+
<pre># echo "out" > /sys/class/gpio/gpio219/direction
 
</pre>
 
</pre>
  
*Set GPIO 218 to input
+
*Set GPIO 220 to input
<pre># echo "in" > /sys/class/gpio/gpio218/direction
+
<pre># echo "in" > /sys/class/gpio/gpio220/direction
 
</pre>
 
</pre>
  
*Change GPIO&nbsp;97 to 1 and read GPIO&nbsp;218 value
+
*Change GPIO 219 to 1 and read GPIO 220 value
<pre># echo 1 > /sys/class/gpio/gpio97/value
+
<pre># echo 1 > /sys/class/gpio/gpio219/value
# cat /sys/class/gpio/gpio218/value
+
# cat /sys/class/gpio/gpio220/value
 
1
 
1
 
</pre>
 
</pre>
  
*Change GPIO&nbsp;97 to 0 and read GPIO 218 value
+
*Change GPIO 219 to 0 and read GPIO 220 value
<pre># echo 0 > /sys/class/gpio/gpio97/value
+
<pre># echo 0 > /sys/class/gpio/gpio219/value
# cat /sys/class/gpio/gpio218/value
+
# cat /sys/class/gpio/gpio220/value
0
+
0</pre>
</pre>
 

Revision as of 02:59, 22 October 2021

Configuration/Test

Configuration

The RK3288 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 - 8

Group GPIO0 only 24 gpios,So each GPIO Num subtracts 8.

gpio_bit : A0~A7 0-7 B0~B7 8-15 C0~C7 16-23 D0~D7 24-31

E.g. GPIO2_A4 becomes 60

RSB-4680

GPIO Number GPIO formed Numeric Representation
GPIO0 GPIO7_A3
219
GPIO1
GPIO7_A4
220
GPIO2
GPIO7_A5
221
GPIO3
GPIO7_C5
237
GPIO4
GPIO8_A2
250
GPIO5
GPIO8_A3
251
GPIO6
GPIO8_A0
248
GPIO7
GPIO8_A1
249

Test

Export GPIO then you can use control GPIO from userr space through sysfs

Export GPIO0

# echo 219 > /sys/class/gpio/export

Set GPIO direction to in/out

# echo "out" > /sys/class/gpio/gpio219/direction

Set GPIO value 0/1 if GPIO pin define is output

#  echo 1 > /sys/class/gpio/gpio219/value

Used as IRQ signal

Note:You have to configure GPIO to input

#  echo "rising" > /sys/class/gpio/gpio219/edge
  • rising: Trigger on rising edge
  • falling: Trigger on falling edge
  • both: Trigger on both edges
  • none: Disable interrupt on both edges

Unexport GPIO0

# echo 219 > /sys/class/gpio/unexport

Test

GPIO 219 and GPIO 220 are taken as an example:

  • Connect GPIO 219 and GPIO 220
  • Export GPIO 219 and GPIO 220
# echo 219 > /sys/class/gpio/export
# echo 220 > /sys/class/gpio/export
  • Set GPIO 219 to output
# echo "out" > /sys/class/gpio/gpio219/direction
  • Set GPIO 220 to input
# echo "in" > /sys/class/gpio/gpio220/direction
  • Change GPIO 219 to 1 and read GPIO 220 value
# echo 1 > /sys/class/gpio/gpio219/value
# cat /sys/class/gpio/gpio220/value
1
  • Change GPIO 219 to 0 and read GPIO 220 value
# echo 0 > /sys/class/gpio/gpio219/value
# cat /sys/class/gpio/gpio220/value
0