User Tools

Site Tools


tutorials:common:gpio_on_lubuntu

This is an old revision of the document!


Cubieboard pin

Using sunxi-gpio kernel module

1. Make sure that the newest firmware has loaded the module gpio-sunxi.ko by default and it has added the command include fex2bin bin2fex. (Use the command 'lsmod' to see the module in the list.)
2. Modify script.bin that configuration file of cubieboard.

  • modify script.bin
   
   $ mount /dev/nanda /mnt
   $ cd  /mnt
   $ bin2fex script.bin script.fex
   $ vi script.fex

default GPIO paragraph setup:

[gpio_para]
gpio_used =1
gpio_num = 2
gpio_pin_1 = port: PH20<1><default><default><1>                //PH20 is connecting  the green LED
gpio_pin_2 = port: PH21<1><default><default><1>                //PH21 is connecting  the blue LED

modify GPIO port want to use e.g.:

[gpio_para]
gpio_used =1
gpio_num = 3
gpio_pin_1 = port: PD01<1><default><default><1>
gpio_pin_2 = port: PD02<1><default><default><1>
gpio_pin_3 = port: PD03<1><default><default><1>

Save the file, then

  $ fex2bin script.fex script.bin
  $ reboot

After rebooted the cubieboard, the new setup will be effective.

3. Operate GPIO port e.g.:

  • open the GPIO PD01~03
  $ echo 1 > /sys/class/gpio/export
  $ echo 2 > /sys/class/gpio/export
  $ echo 3 > /sys/class/gpio/export

Below the /sys/class/gpio has appeared directories named gpio1_pd01, gpio2_pd02, gpio3_pd03.

  • set the PD01 por as output
  $ cd /sys/class/gpio/gpio1_pd01
  $ echo out > direction
  • set the PD01 port as high
  $ echo 1 > value
  • set the PD01 port as low
  $ echo 0 > value

Using C program without driver

The demo control the PD01 port blink led :

#include <stdlib.h>
#include <stdio.h>

#include "gpio_lib.h"
#define PD0    SUNXI_GPD(0)
#define PD1    SUNXI_GPD(1)
#define PD2    SUNXI_GPD(2)
#define PD3    SUNXI_GPD(3)
#define PD4    SUNXI_GPD(4)
#define MISO    SUNXI_GPE(3)
#define MOSI    SUNXI_GPE(2)
#define SCK     SUNXI_GPE(1)
#define CS      SUNXI_GPE(0)

int main()
{
    if(SETUP_OK!=sunxi_gpio_init()){
        printf("Failed to initialize GPIO\n");
        return -1;
    }

    if(SETUP_OK!=sunxi_gpio_set_cfgpin(PD01,OUTPUT)){
        printf("Failed to config GPIO pin\n");
        return -1;
    }

    int i;
    for(i=0;i<5;i++){
        if(sunxi_gpio_output(PD01,HIGH)){
            printf("Failed to set GPIO pin value\n");
            return -1;
        }

        usleep(500000);
        if(sunxi_gpio_output(PD01,LOW)){
            printf("Failed to set GPIO pin value\n");
            return -1;
        }
        usleep(500000);
    }

    sunxi_gpio_cleanup();

    return 0;

}

Save as gpio.c,download gpio_lib,then

$sudo apt-get install gcc build-essential
$tar -xf gpio.tar
$cd gpio/
$gcc gpio_lib.c -c
$gcc gpio.c -c
$gcc gpio.o gpio_lib.o -o gpio
$./gpio

If you have used a led connect GND and PD01 port,the led is going to blink in cycle time.
Of course you can define and use other port .
Use above the method like high-delay-low-delay to simulate PWM output .

Using mmap mapping IO address

    ///////////////////////////////////////////////////////////////////////////////
    #include <ctype.h>
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    #include <time.h>
    #include <signal.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <sys/mman.h>
    #include <sys/select.h>
    #include <pthread.h>
    #include <unistd.h>
    #include <sched.h>
    #include <errno.h>


    #define SW_PORTC_IO_BASE  0x01c20800

    int main() {
       unsigned int * pc;
       int fd, i;
       char * ptr;
       unsigned int addr_start, addr_offset, PageSize, PageMask, data;

       PageSize = sysconf(_SC_PAGESIZE);
       PageMask = (~(PageSize-1));
       addr_start = SW_PORTC_IO_BASE & PageMask;
       addr_offset = SW_PORTC_IO_BASE & ~PageMask;

       fd = open("/dev/mem", O_RDWR);
       if(fd < 0) {
          perror("Unable to open /dev/mem");
          return(-1);
       }

       pc = mmap(0, PageSize*2, PROT_READ|PROT_WRITE, MAP_SHARED, fd, addr_start);

       if(pc == MAP_FAILED) {
          perror("Unable to mmap file");
          printf("pc:%lx\n", (unsigned long)pc);
          return(-1);
       }
       printf("PageSize:%8.8x\tPageMask:%8.8x\naddr_start:%8.8x\taddr_offset:%8.8x\n",PageSize,PageMask,addr_start,addr_offset);
       printf("pc:%8.8x\n", *(unsigned int *)pc);
       ptr = (char *)pc + addr_offset;
       data = *(unsigned int *)(ptr+0x10c);
       for(i=0;i<1000;i++){
          data |= 1<<20;                              //green led connect PH20
          *(unsigned int *)(ptr+0x10c) = data;
          usleep(100000);
          data &= ~(1<<20);
          *(unsigned int *)(ptr+0x10c) = data;
          usleep(500000);
       }

       return 0;
    }

save as test.c,then

$sudo apt-get install gcc build-essential
$gcc test.c -o test
$./test &

The green LED is blinking in cycle time.You can modify the data |= 1«20; and data &= ~(1«20); to use PH15 like data |= 1«15; and data &= ~(1«15);.If you have used a led connect VCC and PH15 port,the led is going to blink in cycle time.
Please refer to * A20 user manual V1.0 20130322.pdf(datasheet)

tutorials/common/gpio_on_lubuntu.1404202732.txt.gz · Last modified: 2014/07/01 16:18 by allen