|
发表于 2018-3-6 10:03:11
|
显示全部楼层
补充其他gpio控制方法:
以控制 cc-a80 的uart4_tx为例子:
新建一个test.c- #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 0x06000800
- 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;
- //the PG12 is UART4-TX in the board ,set it as output port
- data = *(unsigned int *)(ptr+0xdc);
- data &= ~(7<<16);
- data |= 1<<16;
- *(unsigned int *)(ptr+0xdc) = data;
- //set it high level
- data = *(unsigned int *)(ptr+0xe8);
- data |= 1<<12;
- *(unsigned int *)(ptr+0xe8) = data;
- //set it low level
- //data = *(unsigned int *)(ptr+0xe8);
- //data &= ~(1<<12);
- //*(unsigned int *)(ptr+0xe8) = data;
-
-
- return 0;
- }
复制代码 最后在板子上gcc test.c -o test。只看下注释理解用法。
|
|