#include #include #include #include #include #define DEV_NAME "/dev/mem" #define BaseAddress 0x11000000 #define GPX0CON_OFFSET 0x0C00 #define GPX0DAT_OFFSET 0x0C04 #define GPX0_WR_CON 0x11111111 volatile void *Addr_start = NULL; volatile unsigned *GPX0_DAT = NULL; /************************ 内存映射+寄存器地址确定 ***************************/ int port_write( unsigned n, unsigned fd) { Addr_start = (volatile void *)mmap(NULL, 1024*n, PROT_READ | PROT_WRITE, MAP_SHARED, fd, BaseAddress); if (Addr_start == NULL) { printf("内存映射出错!\n"); return -1; } *(volatile unsigned *)(Addr_start+GPX0CON_OFFSET) = GPX0_WR_CON; GPX0_DAT = (volatile unsigned *)(Addr_start+GPX0DAT_OFFSET); return 0; } /********************* 打开一个设备文件,标志符为fd ***************************/ int port_open() { int fd; fd = open(DEV_NAME, O_RDWR); if ( fd < 0 ) { exit(1); } return fd; } /************************ 关闭标志符为fd的设备文件 ***************************/ int port_close(int n, int fd) { munmap( (void *)Addr_start, 1024*n ); close(fd); return 0; }