Difference between revisions of "How to use GPIO"

From ESS-WIKI
Jump to: navigation, search
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
=== Configuration/Test ===
 
  
==== Configuration ====
+
= Configuration =
  
The RK3288 bank/bit notation for GPIOs must be formed as "GPIO<GPIO_bank>_<gpio_bit>" 
+
The RK3399 bank/bit notation for GPIOs must be formed&nbsp;as&nbsp;"GPIO<GPIO_bank>_<gpio_bit>"&nbsp;
  
 
The numeric value of GPIO&nbsp;is calculated as follows:
 
The numeric value of GPIO&nbsp;is calculated as follows:
<pre>32 x (gpio_bank) + gpio_bit - 8
+
<pre>32 x (gpio_bank) + gpio_bit
 
</pre>
 
</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
 +
 
 +
E.g. GPIO2_B0&nbsp;becomes 72
 +
 
 +
== ROM-5780 ==
 +
 
 +
{| border="1" cellpadding="0" cellspacing="1" width="500"
 +
|-
 +
|
 +
GPIO Number
 +
 
 +
|
 +
GPIO formed
 +
 
 +
|
 +
Numeric Representation
 +
 
 +
|-
 +
|
 +
GPIO0
 +
 
 +
|
 +
GPIO1_A2
  
gpio_bit&nbsp;: A0~A7 0-7 B0~B7 8-15 C0~C7 16-23 D0~D7 24-31
+
|
 +
34
 +
 
 +
|-
 +
|
 +
GPIO1
  
E.g. GPIO2_A4 becomes 60
+
|
 +
GPIO1_A3
  
RSB-4680
+
|
 +
35
  
{| cellspacing="1" cellpadding="1" border="1"
 
 
|-
 
|-
| GPIO Number
+
|  
| GPIO formed
+
GPIO2
| Numeric Representation
+
 
 +
|  
 +
GPIO1_B1
 +
 
 +
|  
 +
41
 +
 
 
|-
 
|-
| GPIO0
+
|  
| GPIO7_A3<br/>
+
GPIO3
| 219<br/>
+
 
 +
|
 +
GPIO1_B2
 +
 
 +
|
 +
42
 +
 
 +
|-
 +
|
 +
GPIO4
 +
 
 +
|  
 +
GPIO1_B5
 +
 
 +
|  
 +
45
 +
 
 
|-
 
|-
| GPIO1<br/>
+
|  
| GPIO7_A4<br/>
+
GPIO5
| 220<br/>
+
 
 +
|  
 +
GPIO1_C2
 +
 
 +
|  
 +
50
 +
 
 
|-
 
|-
| GPIO2<br/>
+
|  
| GPIO7_A5<br/>
+
GPIO6
| 221<br/>
+
 
 +
|  
 +
GPIO1_C4
 +
 
 +
|  
 +
52
 +
 
 
|-
 
|-
| GPIO3<br/>
+
|  
| GPIO7_C5<br/>
+
GPIO7
| 237<br/>
+
 
 +
|  
 +
GPIO1_C6
 +
 
 +
|  
 +
54
 +
 
 
|-
 
|-
| GPIO4<br/>
+
|  
| GPIO8_A2<br/>
+
GPIO8
| 250<br/>
+
 
 +
|  
 +
GPIO1_C7
 +
 
 +
|  
 +
55
 +
 
 
|-
 
|-
| GPIO5<br/>
+
|  
| GPIO8_A3<br/>
+
GPIO9
| 251<br/>
+
 
 +
|  
 +
GPIO2_A2
 +
 
 +
|  
 +
66
 +
 
 
|-
 
|-
| GPIO6<br/>
+
|  
| GPIO8_A0<br/>
+
GPIO10
| 248<br/>
+
 
 +
|  
 +
GPIO2_A3
 +
 
 +
|  
 +
67
 +
 
 
|-
 
|-
| GPIO7<br/>
+
|  
| GPIO8_A1<br/>
+
GPIO11
| 249
+
 
 +
|  
 +
GPIO4_C5
 +
 
 +
|  
 +
149
 +
 
 
|}
 
|}
  
Test
 
  
Export GPIO then you can use control GPIO from userr space through sysfs
+
= Test =
 +
 
 +
'''Export GPIO then you can use control GPIO from user space through sysfs.'''
  
 
Export GPIO0
 
Export GPIO0
<pre># echo 219 > /sys/class/gpio/export
+
<pre># echo 34 > /sys/class/gpio/export
 
</pre>
 
</pre>
  
 
Set GPIO direction to in/out
 
Set GPIO direction to in/out
<pre># echo "out" > /sys/class/gpio/gpio219/direction
+
<pre># echo "out" > /sys/class/gpio/gpio34/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/gpio219/value
+
<pre>#  echo 1 > /sys/class/gpio/gpio34/value
 
</pre>
 
</pre>
  
Line 75: Line 167:
  
 
Note:You have to configure GPIO to input
 
Note:You have to configure GPIO to input
<pre>#  echo "rising" > /sys/class/gpio/gpio219/edge
+
<pre>#  echo "rising" > /sys/class/gpio/gpio34/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 GPIO0
 
Unexport GPIO0
<pre># echo 219 > /sys/class/gpio/unexport
+
<pre># echo 34 > /sys/class/gpio/unexport
 
</pre>
 
</pre>
  
==== Test ====
+
'''Test'''
  
GPIO 219 and GPIO 220 are taken as an example:
+
GPIO0 and GPIO1 are taken as an example:
  
*Connect GPIO 219 and GPIO 220
+
*Connect GPIO0 and GPIO1
*Export GPIO 219 and GPIO 220
+
*Export GPIO0 and GPIO1
<pre># echo 219 > /sys/class/gpio/export
+
<pre># echo 34 > /sys/class/gpio/export
# echo 220 > /sys/class/gpio/export
+
# echo 35 > /sys/class/gpio/export
 
</pre>
 
</pre>
  
*Set GPIO 219 to output
+
*Set GPIO 1 to output  
<pre># echo "out" > /sys/class/gpio/gpio219/direction
+
<pre># echo "out" > /sys/class/gpio/gpio34/direction
 
</pre>
 
</pre>
  
*Set GPIO 220 to input
+
*Set GPIO 2 to input  
<pre># echo "in" > /sys/class/gpio/gpio220/direction
+
<pre># echo "in" > /sys/class/gpio/gpio35/direction
 
</pre>
 
</pre>
  
*Change GPIO 219 to 1 and read GPIO 220 value
+
*Change GPIO 0 to 1 and read GPIO 2 value  
<pre># echo 1 > /sys/class/gpio/gpio219/value
+
<pre># echo 1 > /sys/class/gpio/gpio34/value
# cat /sys/class/gpio/gpio220/value
+
# cat /sys/class/gpio/gpio35/value  
 
1
 
1
 
</pre>
 
</pre>
  
*Change GPIO 219 to 0 and read GPIO 220 value
+
*Change GPIO 0&nbsp;to 0 and read GPIO 2&nbsp;value  
<pre># echo 0 > /sys/class/gpio/gpio219/value
+
<pre># echo 0 > /sys/class/gpio/gpio34/value
# cat /sys/class/gpio/gpio220/value
+
# cat /sys/class/gpio/gpio35/value  
0</pre>
+
0
 +
</pre>

Latest revision as of 02:50, 6 June 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

Export GPIO then you can use control GPIO from user space through sysfs.

Export GPIO0

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

Set GPIO direction to in/out

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

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

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

Used as IRQ signal

Note:You have to configure GPIO to input

#  echo "rising" > /sys/class/gpio/gpio34/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 34 > /sys/class/gpio/unexport

Test

GPIO0 and GPIO1 are taken as an example:

  • Connect GPIO0 and GPIO1
  • Export GPIO0 and GPIO1
# echo 34 > /sys/class/gpio/export
# echo 35 > /sys/class/gpio/export
  • Set GPIO 1 to output
# echo "out" > /sys/class/gpio/gpio34/direction
  • Set GPIO 2 to input
# echo "in" > /sys/class/gpio/gpio35/direction
  • Change GPIO 0 to 1 and read GPIO 2 value
# echo 1 > /sys/class/gpio/gpio34/value
# cat /sys/class/gpio/gpio35/value 
1
  • Change GPIO 0 to 0 and read GPIO 2 value
# echo 0 > /sys/class/gpio/gpio34/value
# cat /sys/class/gpio/gpio35/value 
0