Difference between revisions of "GPIO Driver"

From ArmadeusWiki
Jump to: navigation, search
 
 
(44 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 +
{{Warning| This article describe the method to use gpio with armadeus '''version < 4.0'''. To use gpio on armadeus '''version 4.0''' or more see [[GPIOlib]]}}
 +
 +
 
On this page, you will find all the informations needed to use the Armadeus GPIO driver.
 
On this page, you will find all the informations needed to use the Armadeus GPIO driver.
  
== Module compilation & installation==
+
==Installation==
The first thing you have to do is to compile the driver. The only way to do that actually is to compile it as a module:
+
==Compilation==
# cd driver_directory
+
Since Armadeus-3.0 release, gpio driver is compiled and installed by default in rootfs. See [[Talk:GPIO_Driver]] to know how.
Be sure to have ARMADEUS_KERNEL_DIR environment variable defined. If not:
+
# export ARMADEUS_KERNEL_DIR=/local/julien/Armadeus/Armadeus_SVN_test/armadeus/software/buildroot/build_arm_nofpu/linux-2.6.12/  (for example)
+
Compile it:
+
# make ARCH=arm CROSS_COMPILE=/local/julien/Armadeus/Armadeus_SVN_test/armadeus/software/buildroot/build_arm_nofpu/staging_dir/bin/arm-linux-
+
  
When compiled, you will obtain 3 modules:
+
You have 2 modules:
* gpio.ko: this is the main driver. It should be loaded first and will allow you to easily control gpio pin from user space.
+
* gpio: this is the main driver. It should be loaded first and will allow you to easily control GPIO pins from user space,
* ppdev.ko: this is an extension driver to add PPDEV emulation. This way you will be able to emulate a standard parallel port on one GPIO port (and then for example connect a LCD)
+
* ppdev: this is an extension driver to add PPDEV emulation to gpio.ko. This way you will be able to emulate a standard parallel port on one GPIO port (to connect a textual LCD, for example).
* loadfpga.ko: this is an extension driver to add the possibility to load the fpga on APF boards from linux userspace -> fpga reprogrammation after linux boot.
+
{{Note|The parallel port driver (ppdev) will only work on [[APF9328]] based systems for now !}}
  
Then, you have to:
+
===Loading driver===
* copy these modules on your rootfs in /lib/modules/to_be_defined/
+
<pre class="apf">
* copy loadgpio.sh script on your rootfs in /etc/
+
  # /usr/bin/loadgpio.sh
After that you can launch:
+
</pre>
  # sh /etc/loadgpio.sh
+
or look inside this script if you want to manually enter the module parameters:
or look inside this script if you want to manually enter the module parameters
+
<pre class="apf">
  # insmod /lib/modules/2.6.10-imx/drivers/gpio.ko  portB_init=0,0,0x00FFFF00,0,0,0,0,0,0x0FF00000,0,0,0,0,0,0,0x0FF00000
+
  # modprobe gpio portB_init=0,0,0x00FFFF00,0,0,0,0,0,0x0FF00000,0,0,0,0,0,0,0x0FF00000
gpio .ko module parameters are values for PORTA, PORTB, PORTC, PORTD configuration registers in following order:
+
</pre>
DDIR, OCR1, OCR2, ICONFA1, ICONFA2, ICONFB1, ICONFB2, DR, GIUS, SSR, ICR1, ICR2, IMR, GPR, SWR, PUEN,
+
gpio module parameters are values for PORTA, PORTB, PORTC, PORTD configuration registers in following order:
 +
DDIR, OCR1, OCR2, ICONFA1, ICONFA2, ICONFB1, ICONFB2, DR, GIUS, SSR, ICR1, ICR2, IMR, GPR, SWR, PUEN,<br>
 +
{{Warning|If you don't know what these registers means, load the module without parameters !!}}
  
 
== Driver usage ==
 
== Driver usage ==
GPIO driver is usable through 2 interfaces:
 
* use /proc/drivers/gpio/portXdir to read (cat) or write (echo) pin direction
 
* use /proc/drivers/gpio/portX to read (cat) or write (echo) pin status
 
  
Examples:
+
=== As_devices ===
See which IO-Pins of PortD are configured as inputs, and which are outputs ('1' = output, '0' = input):
+
# cat /proc/driver/gpio/portDdir##
+
  
Configure the IO-Pins 30, 28, 23, 22 and 21 of PortB as outputs, all others are inputs:
+
GPIO can be use with library [[AsDevices]].
# echo 01010000111000000000000000000000 >/proc/driver/gpio/portBdir
+
  
Read the status of the IOs of PortB:
+
=== Directly with shell commands (through /proc & /dev filesystem) ===
 +
 
 +
GPIO driver is usable through its /proc interface:
 +
# use ''/proc/drivers/gpio/portXmode'' to configure a pin as a gpio (with ''echo'') and  where X is your port name: A, B, C or D (E,F for apf27)
 +
# use ''/proc/drivers/gpio/portXdir'' to read (with ''cat'') or set (with ''echo'') pin direction (where X is your port name)
 +
# then use ''/proc/drivers/gpio/portX'' to read (with ''cat'') or write (with ''echo'') pin status
 +
# ''/proc/drivers/gpio/portXirq'': to configure GPIO as an interrupt (blocking read); 0-> no interrupt, 1-> rising edge, 2-> falling edge, 3-> both edges
 +
# ''/proc/drivers/gpio/portXpullup'': (de)activate i.MX internal pull-up for that GPIO
 +
 
 +
====Examples====
 +
* Configure IO-Pin 31 of port A (LCD_OE_ACD on the APF27) as GPIO (not reversible):
 +
<pre class="apf">
 +
# echo -n 10000000000000000000000000000000 > /proc/driver/gpio/portAmode
 +
</pre>
 +
 
 +
* See which IO-Pins of PortD are configured as inputs, and which one are outputs ('1' = output, '0' = input):
 +
<pre class="apf">
 +
# cat /proc/driver/gpio/portDdir
 +
01101100111100000110110011110000      pins order: [31...0]
 +
#
 +
</pre>
 +
 
 +
* Configure the IO-Pins 30, 28, 23, 22 and 21 of PortB as outputs ('1'), all others as inputs ('0'):
 +
<pre class="apf">
 +
# echo -n 01010000111000000000000000000000 > /proc/driver/gpio/portBdir
 +
</pre>
 +
 
 +
* Read the status/values of the IOs of PortB:
 +
<pre class="apf">
 
  # cat /proc/driver/gpio/portB
 
  # cat /proc/driver/gpio/portB
 +
00000000000011111111000000000000
 +
#
 +
</pre>
  
Set bits 30, 28 and 23 of PortB to '1', all other outputs to '0':
+
* Set bits 30, 28 and 23 of PortB to '1', all other outputs to '0': '''<-- DEPRECATED, use /dev/gpio/ instead !'''
  # echo 01010000100000000000000000000000 > /proc/driver/gpio/portB
+
<pre class="apf">
 +
  # echo -n 01010000100000000000000000000000 > /proc/driver/gpio/portB
 +
</pre>
 +
 
 +
* Blinks the LED on [[APF9328DevFull]] (pin 31 of portD):
 +
<pre class="apf">
 +
# cat /proc/driver/gpio/portDmode | sed "s/[0-1]\([0-1]\{31\}\)$/1\1/" > /proc/driver/gpio/portDmode
 +
# cat /proc/driver/gpio/portDdir | sed "s/[0-1]\([0-1]\{31\}\)$/1\1/" > /proc/driver/gpio/portDdir
 +
# echo -ne "\x00" > /dev/gpio/PD31
 +
# echo -ne "\x01" > /dev/gpio/PD31
 +
</pre>
 +
 
 +
* Blinks the LED on [[APF27Dev]] (pin 14 of portF):
 +
<pre class="apf">
 +
# cat /proc/driver/gpio/portFmode | sed "s/[0-1]\([0-1]\{14\}\)$/1\1/" > /proc/driver/gpio/portFmode
 +
# cat /proc/driver/gpio/portFdir | sed "s/[0-1]\([0-1]\{14\}\)$/1\1/" > /proc/driver/gpio/portFdir
 +
# echo -ne "\x01" > /dev/gpio/PF14
 +
# echo -ne "\x00" > /dev/gpio/PF14
 +
</pre>
 +
 
 +
* Sets only bit 5 of portADir to 1:
 +
<pre class="apf">
 +
# cat /proc/driver/gpio/portAdir | sed "s/[0-1]\([0-1]\{'''5'''\}\)$/'''1'''\1/" > /proc/driver/gpio/portAdir
 +
</pre>
 +
 
 +
* Sets only bit 5 of portADir to 0:
 +
<pre class="apf">
 +
# cat /proc/driver/gpio/portAdir | sed "s/[0-1]\([0-1]\{'''5'''\}\)$/'''0'''\1/" > /proc/driver/gpio/portAdir
 +
</pre>
 +
 
 +
* You can put it in a shell function:
 +
<pre class="apf">
 +
setbit()
 +
{
 +
    PORT=$1
 +
    NB=$2
 +
    VAL=$3
 +
    cat $PORT | sed "s/[0-1]\([0-1]\{$NB\}\)$/$VAL\1/" > $PORT
 +
}
 +
 
 +
# setbit /proc/driver/gpio/portAdir 5 0
 +
</pre>
 +
 
 +
* Blocking read on the PortD pin 0:
 +
<pre class="apf">
 +
# cat /dev/gpio/PD0 | less
 +
</pre>
 +
 
 +
* A test script is available in ''target/test/'' : [http://armadeus.git.sourceforge.net/git/gitweb.cgi?p=armadeus/armadeus;a=blob;f=target/test/test_gpio.sh;h=e2e54e855aeb25b0f0cbae5915930e9f904861dc;hb=HEAD test_gpio.sh]
 +
 
 +
=== Directly from a C program with IOCTLs ===
 +
 
 +
There is another way to drive GPIO module: with IOCTLs on the right device node (full port).<br>
 +
ioctl() function is using flags to say what must be done:
 +
* GPIORDDIRECTION for reading direction settings of the corresponding port
 +
* GPIOWRDIRECTION for writing direction settings
 +
* GPIORDDATA for reading data on the corresponding port
 +
* GPIOWRDATA for writing data
 +
* Etc...
 +
 
 +
There is a small C example in ''target/demos/gpio/'' that will give you more explanation: [http://armadeus.git.sourceforge.net/git/gitweb.cgi?p=armadeus/armadeus;a=blob_plain;f=target/demos/gpio/blink_led.c;hb=HEAD blink_led.c]
 +
 
 +
=== Directly with C program: /proc filesystem===
 +
{{Note|For performance please use IOCTL instead (see above)}}
 +
 
 +
After inserting the GPIO module, /proc entries are created in the
 +
filesystem, and so it's possible to directly use some Linux standard functions
 +
inside a C program, such as:
 +
* fopen
 +
* fwrite
 +
* fread
 +
* fclose
 +
 
 +
A little piece of code is the best way to understand quickly:
 +
<source lang="C">
 +
    int i;
 +
    unsigned char dummy;
 +
    FILE *GPIO,*GPIODIR, *GPIOMODE;
 +
    char buffer[32];
 +
    char * bufferMode="00000000011111111000000000000000";
 +
    char * bufferDir= "00000000011111111000000000000000";
 +
    char * buffer1=  "00000000000000011000000000000000";
 +
    char * buffer2=  "00000000000001100000000000000000";
 +
    char * buffer3=  "00000000000110000000000000000000";
 +
    char * buffer4=  "00000000011000000000000000000000";
 +
 +
    GPIOMODE = fopen("/proc/driver/gpio/portDmode","w");
 +
    setvbuf(GPIOMODE,buffer,_IONBF,32);
 +
    fwrite(bufferMode, sizeof(char), strlen(bufferMode), GPIOMODE);
 +
    fclose(GPIOMODE);
 +
    GPIODIR = fopen("/proc/driver/gpio/portDdir","w");
 +
    setvbuf(GPIODIR,buffer,_IONBF,32);
 +
    fwrite(bufferDir, sizeof(char), strlen(bufferDir), GPIODIR);
 +
    fclose(GPIODIR);
 +
    GPIO = fopen("/proc/driver/gpio/portD","w");
 +
    setvbuf(GPIO,buffer,_IONBF,32);
 +
    while(1) {           
 +
        fwrite(buffer1, sizeof(char), strlen(buffer1), GPIO);
 +
        sleep(1);
 +
        fwrite(buffer2, sizeof(char), strlen(buffer2), GPIO);
 +
        sleep(1);
 +
        fwrite(buffer3, sizeof(char), strlen(buffer3), GPIO);
 +
        sleep(1);
 +
        fwrite(buffer4, sizeof(char), strlen(buffer4), GPIO);
 +
        sleep(1);
 +
    }
 +
    fclose(GPIO);
 +
    return (0);
 +
</source>
  
 
== Links ==
 
== Links ==
 
* http://soekris.hejl.de/
 
* http://soekris.hejl.de/
 
* http://people.redhat.com/twaugh/parport/html/ppdev.html
 
* http://people.redhat.com/twaugh/parport/html/ppdev.html
 +
* http://www.krugle.com/examples/p-yckfcEKjgfqvsNcB/drivertest.c
 +
* [http://www.ipbx.ru/rm9200/gpio-RM9200.tar.gz AT91 way of doing it]
 +
* [http://www.avr32linux.org/twiki/bin/view/Main/GpioDevInterface AVR32 way of doing it]
 +
* [http://www.jibee.com/view.pl/OpenRobotik/GpioRoutingDriver JiBee's way of doing it]
 +
 +
[[Category:UserInput]]
 +
[[Category:Linux drivers]]

Latest revision as of 16:41, 12 December 2011

Warning Warning: This article describe the method to use gpio with armadeus version < 4.0. To use gpio on armadeus version 4.0 or more see GPIOlib


On this page, you will find all the informations needed to use the Armadeus GPIO driver.

Installation

Compilation

Since Armadeus-3.0 release, gpio driver is compiled and installed by default in rootfs. See Talk:GPIO_Driver to know how.

You have 2 modules:

  • gpio: this is the main driver. It should be loaded first and will allow you to easily control GPIO pins from user space,
  • ppdev: this is an extension driver to add PPDEV emulation to gpio.ko. This way you will be able to emulate a standard parallel port on one GPIO port (to connect a textual LCD, for example).
Note Note: The parallel port driver (ppdev) will only work on APF9328 based systems for now !


Loading driver

 # /usr/bin/loadgpio.sh

or look inside this script if you want to manually enter the module parameters:

 # modprobe gpio portB_init=0,0,0x00FFFF00,0,0,0,0,0,0x0FF00000,0,0,0,0,0,0,0x0FF00000

gpio module parameters are values for PORTA, PORTB, PORTC, PORTD configuration registers in following order: DDIR, OCR1, OCR2, ICONFA1, ICONFA2, ICONFB1, ICONFB2, DR, GIUS, SSR, ICR1, ICR2, IMR, GPR, SWR, PUEN,

Warning Warning: If you don't know what these registers means, load the module without parameters !!


Driver usage

As_devices

GPIO can be use with library AsDevices.

Directly with shell commands (through /proc & /dev filesystem)

GPIO driver is usable through its /proc interface:

  1. use /proc/drivers/gpio/portXmode to configure a pin as a gpio (with echo) and where X is your port name: A, B, C or D (E,F for apf27)
  2. use /proc/drivers/gpio/portXdir to read (with cat) or set (with echo) pin direction (where X is your port name)
  3. then use /proc/drivers/gpio/portX to read (with cat) or write (with echo) pin status
  4. /proc/drivers/gpio/portXirq: to configure GPIO as an interrupt (blocking read); 0-> no interrupt, 1-> rising edge, 2-> falling edge, 3-> both edges
  5. /proc/drivers/gpio/portXpullup: (de)activate i.MX internal pull-up for that GPIO

Examples

  • Configure IO-Pin 31 of port A (LCD_OE_ACD on the APF27) as GPIO (not reversible):
 # echo -n 10000000000000000000000000000000 > /proc/driver/gpio/portAmode
  • See which IO-Pins of PortD are configured as inputs, and which one are outputs ('1' = output, '0' = input):
 # cat /proc/driver/gpio/portDdir
 01101100111100000110110011110000       pins order: [31...0]
 #
  • Configure the IO-Pins 30, 28, 23, 22 and 21 of PortB as outputs ('1'), all others as inputs ('0'):
 # echo -n 01010000111000000000000000000000 > /proc/driver/gpio/portBdir
  • Read the status/values of the IOs of PortB:
 # cat /proc/driver/gpio/portB
 00000000000011111111000000000000
 #
  • Set bits 30, 28 and 23 of PortB to '1', all other outputs to '0': <-- DEPRECATED, use /dev/gpio/ instead !
 # echo -n 01010000100000000000000000000000 > /proc/driver/gpio/portB
 # cat /proc/driver/gpio/portDmode | sed "s/[0-1]\([0-1]\{31\}\)$/1\1/" > /proc/driver/gpio/portDmode
 # cat /proc/driver/gpio/portDdir | sed "s/[0-1]\([0-1]\{31\}\)$/1\1/" > /proc/driver/gpio/portDdir
 # echo -ne "\x00" > /dev/gpio/PD31
 # echo -ne "\x01" > /dev/gpio/PD31
  • Blinks the LED on APF27Dev (pin 14 of portF):
 # cat /proc/driver/gpio/portFmode | sed "s/[0-1]\([0-1]\{14\}\)$/1\1/" > /proc/driver/gpio/portFmode
 # cat /proc/driver/gpio/portFdir | sed "s/[0-1]\([0-1]\{14\}\)$/1\1/" > /proc/driver/gpio/portFdir
 # echo -ne "\x01" > /dev/gpio/PF14
 # echo -ne "\x00" > /dev/gpio/PF14
  • Sets only bit 5 of portADir to 1:
 # cat /proc/driver/gpio/portAdir | sed "s/[0-1]\([0-1]\{'''5'''\}\)$/'''1'''\1/" > /proc/driver/gpio/portAdir
  • Sets only bit 5 of portADir to 0:
 # cat /proc/driver/gpio/portAdir | sed "s/[0-1]\([0-1]\{'''5'''\}\)$/'''0'''\1/" > /proc/driver/gpio/portAdir
  • You can put it in a shell function:
 setbit()
 {
     PORT=$1
     NB=$2
     VAL=$3
     cat $PORT | sed "s/[0-1]\([0-1]\{$NB\}\)$/$VAL\1/" > $PORT
 }

 # setbit /proc/driver/gpio/portAdir 5 0
  • Blocking read on the PortD pin 0:
 # cat /dev/gpio/PD0 | less

Directly from a C program with IOCTLs

There is another way to drive GPIO module: with IOCTLs on the right device node (full port).
ioctl() function is using flags to say what must be done:

  • GPIORDDIRECTION for reading direction settings of the corresponding port
  • GPIOWRDIRECTION for writing direction settings
  • GPIORDDATA for reading data on the corresponding port
  • GPIOWRDATA for writing data
  • Etc...

There is a small C example in target/demos/gpio/ that will give you more explanation: blink_led.c

Directly with C program: /proc filesystem

Note Note: For performance please use IOCTL instead (see above)


After inserting the GPIO module, /proc entries are created in the filesystem, and so it's possible to directly use some Linux standard functions inside a C program, such as:

  • fopen
  • fwrite
  • fread
  • fclose

A little piece of code is the best way to understand quickly:

    int i;
    unsigned char dummy;
    FILE *GPIO,*GPIODIR, *GPIOMODE;
    char buffer[32];
    char * bufferMode="00000000011111111000000000000000";
    char * bufferDir= "00000000011111111000000000000000";
    char * buffer1=   "00000000000000011000000000000000";
    char * buffer2=   "00000000000001100000000000000000";
    char * buffer3=   "00000000000110000000000000000000";
    char * buffer4=   "00000000011000000000000000000000";
 
    GPIOMODE = fopen("/proc/driver/gpio/portDmode","w");
    setvbuf(GPIOMODE,buffer,_IONBF,32);
    fwrite(bufferMode, sizeof(char), strlen(bufferMode), GPIOMODE);
    fclose(GPIOMODE);
    GPIODIR = fopen("/proc/driver/gpio/portDdir","w");
    setvbuf(GPIODIR,buffer,_IONBF,32);
    fwrite(bufferDir, sizeof(char), strlen(bufferDir), GPIODIR);
    fclose(GPIODIR);
    GPIO = fopen("/proc/driver/gpio/portD","w");
    setvbuf(GPIO,buffer,_IONBF,32);
    while(1) {            
        fwrite(buffer1, sizeof(char), strlen(buffer1), GPIO);
        sleep(1);
        fwrite(buffer2, sizeof(char), strlen(buffer2), GPIO);
        sleep(1);
        fwrite(buffer3, sizeof(char), strlen(buffer3), GPIO);
        sleep(1);
        fwrite(buffer4, sizeof(char), strlen(buffer4), GPIO);
        sleep(1);
    }
    fclose(GPIO);
    return (0);

Links