Difference between revisions of "How to use GPIOs"

From IGEP - ISEE Wiki

Jump to: navigation, search
 
(32 intermediate revisions by 4 users not shown)
Line 1: Line 1:
= Overview  =
+
__TOC__
 +
== Overview  ==
  
This How-To is meant to be a starting point for people to learn use GPIOs for IGEP v2 devices as quickly and easily as possible. For this how-to i used [http://releases.linaro.org/platform/linaro-m/headless/final/linaro-m-headless-tar-20101108-2.tar.gz Linaro Headless] with [[Linux Kernel 2.6.35.y|Kernel 2.6.35.y]], Ubuntu 10.04 with Linaro Toolchain, IGEP v2 RC5 and GPIO driver ('''NOTE: Upload source code''').  
+
This How-To is meant to be a starting point for people to learn use GPIOs for IGEP devices as quickly and easily as possible.  
  
There are more ways to use GPIOs in IGEP v2, but this one is very simple.<br>
+
There are more ways to use GPIOs. This article show two simple ways to use gpios: bash commandline and C-code.
  
= Feedback and Contributing  =
+
For this How-To I used [http://labs.isee.biz/index.php/IGEP_firmware_Yocto IGEP firmware Yocto]
  
At any point, if you see a mistake you can contribute to this How-To.<br>
+
=== Feedback and Contributing  ===
 +
At any point, if you see a mistake you can contribute to this How-To. Edit yourself !
  
= Compile GPIO driver source code via Host<br>  =
+
== Requirements ==
 +
For this How-to, I used:
 +
* IGEPv2 Board
 +
* Only for C-program example it also needed:
 +
** Add shortcircuit cable between J990:20 and J990:22 pins. By default, GPIO 156 (J990:20) and GPIO 157 (J990:22) are available on these J990 pins.
  
Download GPIO driver ('''NOTE:Upload source code''') and [[Linux Kernel 2.6.35.y|Kernel 2.6.35.y]] source code. Extract files.<br>
+
Another boards tested:  
  
Edit GPIO driver Makefile's:
+
*IGEP COM MODULE
 +
*IGEP COM AQUILA
  
-In files: $/app/Makefile and $/lib/Makefile, make sure that your CROSS_COMPILE path is correct.
+
== How to chek an GPIO ==
  
-In file: $/modules/Makefile, make sure that your CROSS_COMPILE path is correct and&nbsp;type your Kernel 2.6.35.y path.<br>
+
The gpio-int-test.c program shows one way of using the sysfs file /sys/class/gpio/gpioXX/value to block program execution using poll() until the input level on GPIOXX changes.
  
We will use the ncurses program for set up Kernel configuration, if you don't have this program installed then you must install it with this command:  
+
[[File:GPIO_TEST.tar]]
<pre>sudo apt-get install ncurses-dev
 
</pre>
 
-Go to kernel path and type:
 
<pre>make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- menuconfig
 
</pre>
 
Exit Linux Kernel Configuration an return to Bash. Type:
 
<pre>make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- modules_prepare&lt;br&gt;
 
</pre>
 
File $/include/generated/autoconf.h was created
 
  
Finally compile GPIO driver, go to main Makefile path and compile all source code using make command.
+
== Bash commandline ==
 +
Basic gpio operations could be done using bash and sysfs :
 +
* Export GPIOs <pre>echo "GPIO number NN" > /sys/class/gpio/export</pre>
 +
** For example: '''echo "156" > /sys/class/gpio/export'''<br><br>
 +
* Unexport GPIOs  <pre>echo "GPIO number NN" > /sys/class/gpio/unexport</pre>
 +
* Set GPIO direction <pre>echo "out" > /sys/class/gpio/gpioNN/direction</pre>
 +
** For example: '''echo "out" > /sys/class/gpio/gpio156/direction'''<br><br>
 +
* Set GPIO value  <pre>echo "1" > /sys/class/gpio/gpioNN/value</pre>
 +
* Get GPIO value  <pre>cat /sys/class/gpio/gpioNN/value</pre>
 +
* Configure hardware interrupts <pre>echo "rising" > /sys/class/gpio/gpioNN/edge</pre>
  
Send binaries created from Host to Igep v2. <br>
 
  
= Install binaries via IGEP<br>  =
+
== C-program Example ==
  
Log with root user to install binaries.  
+
C-program Example contains some C-functions to control GPIOs. These also can do:
 +
* Export and unexport GPIOs
 +
* Set GPIO direction
 +
* Set GPIO value
 +
* Get GPIO value
 +
* Configure hardware interrupts
  
=== Install module  ===
+
Example program configures a GPIO to wait a hardware interrupt. Once the GPIO value change from 0 value to 1 value (rising), program gives you a message.
  
Go to:$/modules and insert user-gpio-drv.ko into linux kernel with the following command:
+
=== Compile example program ===
<pre>insmod user-gpio-drv.ko
+
[http://labs.isee.biz/index.php/How_to_setup_a_cross_compiler#Download.2FInstall_IGEP_SDK Download an Install] IGEP SDK if you don't have it.
</pre>
 
Check that user-gpio-drv.ko is currently loaded with the following command:
 
<pre>lsmod</pre>
 
The result will be similar at that: <br>
 
<pre>root@localhost:~/gpio-driver/module# lsmod
 
Module                  Size  Used by
 
user_gpio_drv          1639  0
 
omap_wdt                3411  0
 
spidev                  4198  0
 
iommu                  8558  0
 
rtc_twl                4411  0
 
rtc_core              11187  1 rtc_twl
 
twl4030_keypad          2970  0
 
</pre>
 
The module is loaded until system halt.
 
  
=== Install shared library  ===
+
First of all you need to initialize a suitable environment in the bash shell console inside your machine. <br> You can do this sourcing once the environment-setup script.
 +
<pre>jdoe@ubuntu ~ $ source /opt/poky/1.2/environment-setup-armv7a-vfp-neon-poky-linux-gnueabi </pre>
  
Go to:$/lib. libgpio.so is here.  
+
* Download [http://labs.isee.biz/images/6/69/Gpio_examplebeta1.tar.bz2 source code]
 +
* Extract source code
 +
* Build source code:
  
If a program is linked with shared libraries, Kernel seek in specific paths when program is executed. Now is necessary link the libgpio.so path to the environment variable LD_LIBRARY_PATH, use the following command:<br>
+
Cross toolchain tools are available into the built-in virtual machine Poky SDK. You only need open bash terminal prompt and write command:  
<pre>export LD_LIBRARY_PATH=/root/gpio-driver/lib/
+
<pre>jdoe@ubuntu ~/Desktop $ arm-poky-linux-gnueabi-gcc -o gpio_example gpio_examplebeta1.c</pre>
</pre>
 
Check that libgpio.so is linked correctly. Go to:$/gpio-driver/app, gpio program is here. Type next command:  
 
<pre>ldd gpio
 
</pre>
 
ldd command, print shared library dependencies. The result will be similar at that:
 
<pre>root@localhost:~/gpio-driver/app# ldd gpio
 
libgpio.so =&gt; /root/gpio-driver/lib/libgpio.so (0x40197000)
 
libc.so.6 =&gt; /lib/libc.so.6 (0x401a0000)
 
/lib/ld-linux.so.3 (0x4008a000)
 
</pre>  
 
The shared library is linked until system halt. Now you can execute gpio example program.
 
  
= Testing driver  =
+
* Copy binary file to IGEP Board
  
To make sure than driver works well, make the next test. I used GPIO_136(sdmmc2_dat4) and GPIO_137(sdmmc2_dat5) because IGEP v2 RC5(without WIFI) don't use them by default: <br>  
+
=== Execute program ===
 +
Open a remote terminal and locate your program binary, execute program and pass like a parameter 157 value (GPIO 157):
 +
<pre>root@igep00x0:~# ./gpio_example 157 </pre>
 +
Result will be:
 +
<pre>root@igep00x0:~# ./gpio_example 157
 +
gpio/direction: No such file or directory
  
'''NOTE:''' For more information visit this [[Mux configuration|page]]. <br>  
+
poll() GPIO 157 interrupt occurred
 +
............. </pre>
  
=== Configure Mux  ===
+
=== Generate interrupts ===
 +
Open a second remote terminal and type:
 +
<pre>cd /sys/class/gpio/
 +
echo 156 > export
 +
cd gpio156/
 +
echo out > direction
 +
echo 0 > value
 +
echo 1 > value</pre>
  
Go to:/sys/kernel/debug/omap_mux, and change this mux configuration:
+
=== Result ===
<pre>echo 0x104&gt;sdmmc2_dat4
+
At first remote terminal you should read a message similar like this:
echo 0x104&gt;sdmmc2_dat5
+
<pre>poll() GPIO 157 interrupt occurred </pre>
</pre>
 
Use cat command to check it:<br>
 
<pre>cat sdmmc2_dat4
 
cat sdmmc2_dat5
 
</pre>
 
The result will be similar at that:
 
<pre>root@localhost:/sys/kernel/debug/omap_mux# cat sdmmc2_dat4
 
name: sdmmc2_dat4.gpio_136 (0x48002164/0x134 = 0x0104), b ae4, t NA
 
mode: OMAP_PIN_INPUT | OMAP_MUX_MODE4
 
signals: sdmmc2_dat4 | sdmmc2_dir_dat0 | NA | sdmmc3_dat0 | gpio_136 | NA | NA | safe_mode
 
</pre>
 
and
 
<pre>root@localhost:/sys/kernel/debug/omap_mux# cat sdmmc2_dat5
 
name: sdmmc2_dat5.gpio_137 (0x48002166/0x136 = 0x0104), b ah3, t NA
 
mode: OMAP_PIN_INPUT | OMAP_MUX_MODE4
 
signals: sdmmc2_dat5 | sdmmc2_dir_dat1 | cam_global_reset | sdmmc3_dat1 | gpio_137 | hsusb3_tll_stp | mm3_rxdp | safe_mode
 
</pre>
 
Note: OMAP_PIN_INPUT=Input/Output pin and OMAP_PIN_OUTPUT=Output pin, for Read/Write test you need the first one. GPIO is configured in mode 4.<br>
 
 
 
=== Read/Write test  ===
 
 
 
Link GPIO_136 and GPIO_137 with a wire, these pins are located in J990 connector with numbers 7 and 9. I use the next connector to join them:
 
 
 
[[Image:Connector_used_readwrite_test.JPG|left|500x300px]][[Image:All_connected_readwrite_test.JPG|right|500x300px]]<br>
 
 
 
Now type next code:  
 
<pre>root@localhost:~/gpio-driver/app# ./gpio input 136
 
root@localhost:~/gpio-driver/app# ./gpio output 137 0
 
root@localhost:~/gpio-driver/app# ./gpio get 136   
 
0
 
root@localhost:~/gpio-driver/app# ./gpio output 137 1
 
root@localhost:~/gpio-driver/app# ./gpio get 136   
 
1
 
root@localhost:~/gpio-driver/app#
 
</pre>  
 
The code above shows that driver works properly, GPIO_136 is configured like input and GPIO_137 is configured like output with value 0, when read GPIO_136 the result is 0. To make sure that works well, configure GPIO_137 with value 1, now GPIO_136 reads 1. <br> This driver have more options like IRQ, but is not explained here.
 
 
 
<br> Other examples:[[What can I do with IGEP0020#How_to_handle_the_gpio-LED.27s]]
 
 
 
[[Category:Communications]]
 

Latest revision as of 16:59, 22 September 2015

Overview

This How-To is meant to be a starting point for people to learn use GPIOs for IGEP devices as quickly and easily as possible.

There are more ways to use GPIOs. This article show two simple ways to use gpios: bash commandline and C-code.

For this How-To I used IGEP firmware Yocto

Feedback and Contributing

At any point, if you see a mistake you can contribute to this How-To. Edit yourself !

Requirements

For this How-to, I used:

  • IGEPv2 Board
  • Only for C-program example it also needed:
    • Add shortcircuit cable between J990:20 and J990:22 pins. By default, GPIO 156 (J990:20) and GPIO 157 (J990:22) are available on these J990 pins.

Another boards tested:

  • IGEP COM MODULE
  • IGEP COM AQUILA

How to chek an GPIO

The gpio-int-test.c program shows one way of using the sysfs file /sys/class/gpio/gpioXX/value to block program execution using poll() until the input level on GPIOXX changes.

File:GPIO TEST.tar

Bash commandline

Basic gpio operations could be done using bash and sysfs :

  • Export GPIOs
    echo "GPIO number NN" > /sys/class/gpio/export
    • For example: echo "156" > /sys/class/gpio/export

  • Unexport GPIOs
    echo "GPIO number NN" > /sys/class/gpio/unexport
  • Set GPIO direction
    echo "out" > /sys/class/gpio/gpioNN/direction
    • For example: echo "out" > /sys/class/gpio/gpio156/direction

  • Set GPIO value
    echo "1" > /sys/class/gpio/gpioNN/value
  • Get GPIO value
    cat /sys/class/gpio/gpioNN/value
  • Configure hardware interrupts
    echo "rising" > /sys/class/gpio/gpioNN/edge


C-program Example

C-program Example contains some C-functions to control GPIOs. These also can do:

  • Export and unexport GPIOs
  • Set GPIO direction
  • Set GPIO value
  • Get GPIO value
  • Configure hardware interrupts

Example program configures a GPIO to wait a hardware interrupt. Once the GPIO value change from 0 value to 1 value (rising), program gives you a message.

Compile example program

Download an Install IGEP SDK if you don't have it.

First of all you need to initialize a suitable environment in the bash shell console inside your machine.
You can do this sourcing once the environment-setup script.

jdoe@ubuntu ~ $ source /opt/poky/1.2/environment-setup-armv7a-vfp-neon-poky-linux-gnueabi 
  • Download source code
  • Extract source code
  • Build source code:

Cross toolchain tools are available into the built-in virtual machine Poky SDK. You only need open bash terminal prompt and write command:

jdoe@ubuntu ~/Desktop $ arm-poky-linux-gnueabi-gcc -o gpio_example gpio_examplebeta1.c
  • Copy binary file to IGEP Board

Execute program

Open a remote terminal and locate your program binary, execute program and pass like a parameter 157 value (GPIO 157):

root@igep00x0:~# ./gpio_example 157 

Result will be:

root@igep00x0:~# ./gpio_example 157
gpio/direction: No such file or directory

poll() GPIO 157 interrupt occurred
............. 

Generate interrupts

Open a second remote terminal and type:

cd /sys/class/gpio/
echo 156 > export 
cd gpio156/ 
echo out > direction
echo 0 > value
echo 1 > value

Result

At first remote terminal you should read a message similar like this:

poll() GPIO 157 interrupt occurred