User Tools

Site Tools


tutorials:common:gpio_on_lubuntu

This is an old revision of the document!


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

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.1400572027.txt.gz · Last modified: 2014/05/20 15:47 by allen