User Tools

Site Tools


tutorials:common:gpio_on_lubuntu

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

tutorials:common:gpio_on_lubuntu [2013/11/27 00:08]
benn
tutorials:common:gpio_on_lubuntu [2014/07/01 20:03] (current)
allen [Cubieboard pin]
Line 1: Line 1:
 +
 +==== Cubieboard pin ====
 +Cubietruck ​        ​[[http://​docs.cubieboard.org/​a20-cubietruck_gpio_pin]]
 +
 +Cubieboard 1 2 [[http://​docs.cubieboard.org/​cubieboard1_and_cubieboard2_gpio_pin]]
 +
 +Fex Guide   ​[[http://​linux-sunxi.org/​Fex_Guide]]
 +
 +A10 Datasheet ​ [[http://​dl.linux-sunxi.org/​A10/​A10%20Datasheet%20-%20v1.21%20(2012-04-06).pdf|A10 Datasheet(For Pin multiplexing reference)]]
 +
 +A20 Datasheet ​ [[http://​dl.cubieboard.org/​software/​ubuntuone/​public/​cubieboard/​docs/​A20_user manual V1.0 20130322.pdf|A20 user manual V1.0 20130322.pdf(datasheet)]]
 +
 +
 +
 +
  
 ====Using sunxi-gpio kernel module==== ====Using sunxi-gpio kernel 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.)\\ + 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 configure ​cubieboard ​file. + 2. Modify script.bin that configuration file of cubieboard.
-   ​*download the sunxi-tools +
-<​code>​ +
-   $ git clone git://​github.com/​linux-sunxi/​sunxi-tools.git +
-   $ cd sunxi-tools +
-   $ make +
-</​code>​+
    ​*modify script.bin    ​*modify script.bin
 <​code> ​   <​code> ​  
-   $ mount /​dev/​nanda ​/dev/mnt +   $ mount /dev/nanda /mnt 
-   ​$ ​cp /mnt/script.bin ./ +   ​$ ​cd  ​/mnt 
-   ​$ ​./bin2fex script.bin ​>./script.fex+   $ bin2fex script.bin script.fex
    $ vi script.fex    $ vi script.fex
 </​code>​ </​code>​
Line 22: Line 31:
 gpio_used =1 gpio_used =1
 gpio_num = 2 gpio_num = 2
-gpio_pin_1 = port: PH20<​1><​default><​default><​1>​ +gpio_pin_1 = port: PH20<​1><​default><​default><​1> ​               //​PH20 is connecting ​ the green LED 
-gpio_pin_2 = port: PH21<​1><​default><​default><​1>​+gpio_pin_2 = port: PH21<​1><​default><​default><​1> ​               //​PH21 is connecting ​ the blue LED
 </​code>​ </​code>​
-modify GPIO port want to use i.e.:+modify GPIO port want to use e.g.:
 <​code>​ <​code>​
 [gpio_para] [gpio_para]
Line 36: Line 45:
 Save the file, then Save the file, then
 <​code>​ <​code>​
-  $ ./fex2bin script.fex ​>​./​script.bin +  $ fex2bin script.fex script.bin
-  $ cp  ​script.bin ​/mnt +
   $ reboot   $ reboot
 </​code>​ </​code>​
 After rebooted the cubieboard, the new setup will be effective. After rebooted the cubieboard, the new setup will be effective.
  
- 3. Operate GPIO port i.e.:+ 3. Operate GPIO port e.g.:
  
   *open the GPIO PD01~03 ​   *open the GPIO PD01~03 ​
Line 50: Line 58:
   $ echo 3 > /​sys/​class/​gpio/​export ​   $ echo 3 > /​sys/​class/​gpio/​export ​
 </​code>​ </​code>​
-Below the /​sys/​class/​gpio has appeared directories named gpio1_pd01, ​gpio1_pd02gpio1_pd03.+Below the /​sys/​class/​gpio has appeared directories named gpio1_pd01, ​gpio2_pd02gpio3_pd03.
   *set the PD01 por as output ​   *set the PD01 por as output ​
 <​code>​ <​code>​
Line 116: Line 124:
 </​code>​ </​code>​
 Save as gpio.c,​download [[http://​dl.cubieboard.org/​software/​libs/​gpio.tar|gpio_lib]],​then<​code>​ Save as gpio.c,​download [[http://​dl.cubieboard.org/​software/​libs/​gpio.tar|gpio_lib]],​then<​code>​
-gcc gpio_lib.c -c +$sudo apt-get install gcc build-essential 
-gcc gpio.c -c +$tar -xf gpio.tar  
-gcc gpio.o gpio_lib.o -o gpio +$cd gpio/ 
-./gpio+$gcc gpio_lib.c -c 
 +$gcc gpio.c -c 
 +$gcc gpio.o gpio_lib.o -o gpio 
 +$./gpio
 </​code>​ </​code>​
 If you have used a led connect GND and PD01 port,the led is going to blink in cycle time.\\ 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 .\\ Of course you can define and use other port .\\
 Use above the method ​ like high-delay-low-delay ​ to simulate ​ PWM output .\\ Use above the method ​ like high-delay-low-delay ​ to simulate ​ PWM output .\\
 +
 +====Using ​ mmap mapping IO address====
 +<​code>​
 +
 + ///////////////////////////////////////////////////////////////////////////////​
 + #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;
 + }
 +
 +
 +</​code>​
 +
 +save as test.c,​then ​
 +<​code>​
 +$sudo apt-get install gcc build-essential
 +$gcc test.c -o test
 +$./test &
 +
 +
 +</​code>​
 +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.\\
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
  
  
tutorials/common/gpio_on_lubuntu.1385482112.txt.gz · Last modified: 2013/12/23 14:50 (external edit)