Difference between revisions of "IoTGateway/BSP/Android/Gettingstarted/How to use GPIO"

From ESS-WIKI
Jump to: navigation, search
 
Line 104: Line 104:
  
 
Export GPIO 27
 
Export GPIO 27
<pre># echo 27 /sys/class/gpio/export
+
<pre># echo 27 > /sys/class/gpio/export
 
</pre>
 
</pre>
  
 
Set GPIO direction to in/out
 
Set GPIO direction to in/out
<pre># echo "in" > /sys/class/gpio/gpio27/direction
+
<pre># echo "out" > /sys/class/gpio/gpio27/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/gpio27/value
 
<pre>#  echo 1 > /sys/class/gpio/gpio27/value
</pre>
 
 
Directly force a GPIO to output and set its initial value(high=1 low=0)
 
<pre># echo high > /sys/class/gpio/gpio27/direction
 
 
</pre>
 
</pre>
  
Line 131: Line 127:
  
 
Unexport GPIO 27
 
Unexport GPIO 27
<pre># echo 27 /sys/class/gpio/unexport
+
<pre># echo 27 > /sys/class/gpio/unexport
 
</pre>
 
</pre>
  

Latest revision as of 11:44, 19 June 2017

Configuration/Test

Configuration

The i.MX 6 bank/bit notation for GPIOs must be formed as "GPIO<GPIO_bank>_IO<gpio_bit>" 

The numeric value of GPIO is calculated as follows:

32 x (gpio_bank- 1) + gpio_bit

E.g. GPIO2_IO04 becomes 36


ROM-3421
GPIO Number GPIO formed Numeric Representation
GPIO0 GPIO_1 1
GPIO1
GPIO2_IO2 34
GPIO2
GPIO2_IO3
35
GPIO3
GPIO_3 3
GPIO4
GPIO7_IO12
204
GPIO5
GPIO7_IO13
205
GPIO6
GPIO7_IO6
198
GPIO7
GPIO6_IO08
168
GPIO8
GPIO2_IO06
38
ROM7420
GPIO Number GPIO formed Numeric Representation
GPIO0 GPIO6_IO11 171
GPIO1
GPIO6_IO14 174
GPIO2
GPIO6_IO15
175
GPIO3
GPIO6_IO16 176
GPIO4
GPIO2_IO25
57
GPIO5
GPIO2_IO23
55
GPIO6
GPIO2_IO24
56
GPIO7
GPIO5_IO02
130

Test

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

Export GPIO 27

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

Set GPIO direction to in/out

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

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

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

Used as IRQ signal

Note:You have to configure GPIO to input

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

Unexport GPIO 27

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

Test

GPIO 27 and GPIO 29 are taken as an example:

  • Connect GPIO 27 and GPIO 29
  • Export GPIO 27 and GPIO 29
# echo 27 /sys/class/gpio/export
# echo 29 /sys/class/gpio/export
  • Set GPIO 27 to output
# echo "out" > /sys/class/gpio/gpio27/direction
  • Set GPIO 29 to input
# echo "in" > /sys/class/gpio/gpio29/direction
  • Change GPIO 27 to 1 and read GPIO 29 value
# echo 1 > /sys/class/gpio/gpio27/value
# cat /sys/class/gpio/gpio29/value
1
  • Change GPIO 27 to 0 and read GPIO 29 value
# echo 0 > /sys/class/gpio/gpio27/value
# cat /sys/class/gpio/gpio29/value
0