This shows you the differences between two versions of the page.
|
tutorials:common:gpio_on_lubuntu [2014/05/20 15:47] allen [Using sunxi-gpio kernel module] |
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==== | ||
| Line 109: | 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.\\ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||