Difference between revisions of "GPIOlib"

From ArmadeusWiki
Jump to: navigation, search
(Get APFxxDev i.MX switch status)
m (Installation)
Line 3: Line 3:
  
 
==Installation==
 
==Installation==
* If not already done:
+
* Configure Linux kernel
 
<pre class="host">
 
<pre class="host">
 
$ make linux26-menuconfig
 
$ make linux26-menuconfig
 
</pre>
 
</pre>
 +
* Enable the sysfs interface of the GPIOlib If not already done:
 
<pre class="config">
 
<pre class="config">
 
Device Drivers  --->
 
Device Drivers  --->
Line 12: Line 13:
 
         [*]  /sys/class/gpio/... (sysfs interface)
 
         [*]  /sys/class/gpio/... (sysfs interface)
 
</pre>
 
</pre>
 +
* In order to test the GPIOlib with the user LED and SWITCH of the board you will have to disable the [[GPIO_keys]] and [[GPIO_LEDS]] drivers that are the regular driver for such device.
 +
<pre class="config">
 +
Device Drivers  --->
 +
    Input device support  --->
 +
        [*]  Keyboards  --->
 +
            < >  GPIO Buttons (be sure GPIO Buttons is unchecked)
 +
</pre>
 +
<pre class="config">
 +
Device Drivers  --->
 +
    [ ] LED Support  --->  (be sure "LED Support" or "GPIO connected LEDs" (hereafter) is unchecked)
 +
        < >  LED Support for GPIO connected LEDs
 +
</pre>
 +
* Save your new configuration and rebuild your kernel (if needed)
 
<pre class="host">
 
<pre class="host">
 
$ make
 
$ make
 
</pre>
 
</pre>
* reflash your kernel (if needed)
+
* and reflash your kernel (if needed)
  
 
==Usage==
 
==Usage==

Revision as of 23:36, 13 May 2012

With recent Linux kernel (> 2.6.3x), there is an abstraction layer meant to ease GPIO usage: gpiolib. We will explain here how to use it.

Installation

  • Configure Linux kernel
$ make linux26-menuconfig
  • Enable the sysfs interface of the GPIOlib If not already done:
Device Drivers  --->
    -*- GPIO Support  --->
        [*]   /sys/class/gpio/... (sysfs interface)
  • In order to test the GPIOlib with the user LED and SWITCH of the board you will have to disable the GPIO_keys and GPIO_LEDS drivers that are the regular driver for such device.
Device Drivers  --->
    Input device support  --->
        [*]   Keyboards  --->
            < >   GPIO Buttons (be sure GPIO Buttons is unchecked)
 
Device Drivers  --->
    [ ] LED Support  --->  (be sure "LED Support" or "GPIO connected LEDs" (hereafter) is unchecked)
         < >   LED Support for GPIO connected LEDs
 
  • Save your new configuration and rebuild your kernel (if needed)
$ make
  • and reflash your kernel (if needed)

Usage

Blink APFxxDev i.MX LED

  • Find the GPIO number connected to the LED, eg:
    • for APF27Dev: PF14 -> PortF (n°6) pin 14 -> (6-1)*32 + 14 -> GPIO n°174
    • for APF51Dev: GPIO1_2 -> Port 1 pin 2 -> GPIO n° 2
    • for APF28Dev: Bank 0, pin 21 -> GPIO n° 21 (0*32 + 21)
  • Define envt variable (for portability):
# export LED=174    (APF27Dev)
or
# export LED=2    (APF51Dev)
or
# export LED=21    (APF28Dev)
  • Ask kernel to export the GPIO:
# echo $LED > /sys/class/gpio/export
  • Access functions are created:
# ls /sys/class/gpio/gpio$LED/
active_low  edge        subsystem   value
direction   power       uevent
  • Check GPIO direction and put in output:
# cat /sys/class/gpio/gpio$LED/direction
in
# echo out > /sys/class/gpio/gpio$LED/direction
# cat /sys/class/gpio/gpio$LED/direction
out
  • Play with GPIO value to blink LED:
# echo 1 > /sys/class/gpio/gpio$LED/value
# echo 0 > /sys/class/gpio/gpio$LED/value

Get APFxxDev i.MX switch status

  • Find the GPIO number connected to the switch, eg:
    • for APF27Dev: PF13 -> PortF (n°6) pin 13 -> (6-1)*32 + 13 -> GPIO n°173
    • for APF51Dev: GPIO1_3 -> Port 1 pin 3 -> GPIO n°3 ((1-1)*32 + 3)
    • for APF28Dev: Bank 0, pin 17 -> GPIO n° 17 (0*32 + 17)
  • Define envt variable (for portability):
# export SWITCH=173    (APF27Dev)
or
# export SWITCH=3      (APF51Dev)
or
# export SWITCH=17     (APF28Dev)
  • Ask kernel to export the GPIO:
# echo $SWITCH > /sys/class/gpio/export
  • Access functions are created:
# ls /sys/class/gpio/gpio$SWITCH/
active_low  edge        subsystem   value
direction   power       uevent
  • Check GPIO direction and put in output:
# cat /sys/class/gpio/gpio$SWITCH/direction
in
  • Get switch status:
# cat /sys/class/gpio/gpio$SWITCH/value
1    (released)
...
# cat /sys/class/gpio/gpio$SWITCH/value
0    (pressed)
  • Block until an event occur:
# cat /sys/class/gpio/gpio$SWITCH/edge
none
# echo both > /sys/class/gpio/gpio$SWITCH/edge

Links