User Tools

Site Tools


tutorials:common:gpio_on_lubuntu

This is an old revision of the document!


Using sunxi-gpio module

1. Make sure that the newest firmware has loaded the module gpio-sunxi.ko by default. (Use the command 'lsmod' to see the module in the list.)
2. Modify script.bin that configure cubieboard file.

  • download the sunxi-tools
       $ git clone git://github.com/linux-sunxi/sunxi-tools.git
       $ cd sunxi-tools
       $ make
  • modify script.bin
       
       $ mount /dev/nanda /dev/mnt
       $ cp /mnt/script.bin ./
       $ ./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>
gpio_pin_2 = port: PH21<1><default><default><1>

modify GPIO port want to use i.e.:

[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
$ cp  script.bin /mnt 
$ reboot

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

3. Operate GPIO port i.e.:

  • open the GPIO PD01~03
      $ echo 1 > /sys/class/gpio/export
      $ echo 2 > /sys/class/gpio/export
      $ echo 3 > /sys/class/gpio/export
       </code>
    Below the /sys/class/gpio has appeared directories named gpio1_pd01, gpio1_pd02, gpio1_pd03.
      *set the PD01 por as output <code>
      $ cd /sys/class/gpio/gpio1_pd01
      $ echo out > direction
     </code>
      *set the PD01 port as high<code>
      $ echo 1 > value </code>
      *set the PD01 port as low<code>
      $ echo 0 > value </code>
    
    ====Using raw C program====
    The demo control the PD01 port blink led  :<code>
    #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

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 .

tutorials/common/gpio_on_lubuntu.1380898507.txt.gz · Last modified: 2013/12/23 14:50 (external edit)