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/10/04 22:55]
george
tutorials:common:gpio_on_lubuntu [2014/07/01 20:03] (current)
allen [Cubieboard pin]
Line 1: Line 1:
  
-====Using sunxi-gpio module==== +==== Cubieboard pin ==== 
- 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.)\\ +Cubietruck ​        ​[[http://​docs.cubieboard.org/​a20-cubietruck_gpio_pin]] 
- 2. Modify script.bin that configure cubieboard ​file. + 
-   *download the sunxi-tools<​code>​ +Cubieboard 1 2 [[http://​docs.cubieboard.org/​cubieboard1_and_cubieboard2_gpio_pin]] 
-   ​$ ​git clone git://github.com/linux-sunxi/​sunxi-tools.git + 
-   $ cd sunxi-tools +Fex Guide   ​[[http://​linux-sunxi.org/​Fex_Guide]] 
-   ​$ ​make+ 
 +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==== 
 + 
 + 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 
 +<​code> ​  ​ 
 +   ​$ ​mount /dev/nanda /mnt 
 +   $ cd  /mnt 
 +   ​$ ​bin2fex script.bin script.fex 
 +   $ vi script.fex
 </​code>​ </​code>​
-   ​*modify script.bin<​code> ​   +default GPIO paragraph setup: 
-   $ mount /dev/nanda /dev/mnt +<​code>​
-   $ cp /​mnt/​script.bin ./ +
-   $ ./bin2fex script.bin >​./​script.fex +
-   $ vi script.fex</​code>​ +
-default GPIO paragraph setup:<​code>​+
 [gpio_para] [gpio_para]
 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.:<​code>​+modify GPIO port want to use e.g.: 
 +<​code>​
 [gpio_para] [gpio_para]
 gpio_used =1 gpio_used =1
Line 29: Line 44:
 </​code>​ </​code>​
 Save the file, then Save the file, then
-  ​./fex2bin script.fex ​>​./​script.bin +<​code>​ 
-  $ cp  ​script.bin ​/mnt +  ​$ fex2bin script.fex script.bin
   $ reboot   $ reboot
 +</​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 <​code>​+ 
 +  *open the GPIO PD01~03 ​ 
 +<​code>​
   $ echo 1 > /​sys/​class/​gpio/​export ​   $ echo 1 > /​sys/​class/​gpio/​export ​
   $ echo 2 > /​sys/​class/​gpio/​export ​     $ echo 2 > /​sys/​class/​gpio/​export ​  
   $ 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 <​code>​+  *set the PD01 por as output ​ 
 +<​code>​
   $ cd /​sys/​class/​gpio/​gpio1_pd01   $ cd /​sys/​class/​gpio/​gpio1_pd01
   $ echo out > direction   $ echo out > direction
- </​code>​ +</​code>​ 
-  *set the PD01 port as high<​code>​ +  *set the PD01 port as high 
-  $ echo 1 > value </​code>​ +<​code>​ 
-  *set the PD01 port as low<​code>​ +  $ echo 1 > value  
-  $ echo 0 > value </​code>​+</​code>​ 
 +  *set the PD01 port as low 
 +<​code>​ 
 +  $ echo 0 > value 
 +</​code>​ 
 + 
 +====Using C program without driver====
  
-====Using raw C program==== 
 The demo control the PD01 port blink led  :<​code>​ The demo control the PD01 port blink led  :<​code>​
 #include <​stdlib.h>​ #include <​stdlib.h>​
Line 100: 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.1380898507.txt.gz · Last modified: 2013/12/23 14:50 (external edit)