|
发表于 2014-7-17 16:38:55
|
显示全部楼层
本帖最后由 darson 于 2014-7-17 17:22 编辑
tll 发表于 2013-7-22 13:50
抱歉我把源代码弄丢了……
tll 的 gpio.deb 源码应该是这样的,排版有点乱,凑合用吧,我用的是PH 引脚 ,可以根据需要修改-
- #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(int argc,char *argv[]) {
-
- unsigned int * pc;
- int fd, i;
- char * ptr;
- unsigned int addr_start, addr_offset, PageSize, PageMask, data;
- int a;
- char s[10] ;
- char s1[]="open";
- char s2[]="close";
- printf("Which pin do you want to use (PH?) :");
- scanf("%d",&a);
- if(0<= a && a <=27){
- printf("OK,please enter 'open' or 'close' to open or close this pin:");
-
- scanf("%s",s);
- 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);
- }
- ptr = (char *)pc + addr_offset;
- data = *(unsigned int *)(ptr+0x10c);
- if(strcmp(s,s1)==0){
-
- data |= 1<<a;
- *(unsigned int *)(ptr+0x10c) = data;
- printf("Open successed!") ;
- return 0;
- }
-
- else if(strcmp(s,s2)==0){
- data &= ~(1<<a);
- *(unsigned int *)(ptr+0x10c) = data;
- printf("Close successed!") ;
- return 0;
-
- }
- else {
- printf("Please enter 'open' or 'close',not '%s',okay?\n",s);
- }
- }
- else {
- printf("Hey!Please enter a number from 0 to 27!\n");
- return -1;
- }
- }
复制代码
|
|