#include #include #include #include #include #include // 添加这个头文件来支持 uint32_t #define MAP_SIZE 0x1000 // 4KB 页大小 #define GPIO1_CTL 0xFD6D0B50 #define GPIO1_STATUS 0xFD6D0940 #define GPIO2_CTL 0xFD6D0B60 #define GPIO2_STATUS 0xFD6D0950 int write_mem_bit(off_t target, int bit, int value) { volatile uint32_t *map_base = NULL; volatile uint32_t *addr = NULL; int fd; fd = open("/dev/mem", O_RDWR | O_SYNC); if (fd < 0) { perror("open /dev/mem"); return -1; } // 页边界对齐映射 map_base = mmap(NULL, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target & ~(MAP_SIZE - 1)); if (map_base == MAP_FAILED) { perror("mmap"); close(fd); return -1; } addr = map_base + ((target & (MAP_SIZE - 1)) / sizeof(uint32_t)); uint32_t reg = *addr; if (value) reg |= (1 << bit); else reg &= ~(1 << bit); *addr = reg; return 0; } int read_mem_bit(off_t target, int bit) { volatile uint32_t *map_base = NULL; volatile uint32_t *addr = NULL; int fd; fd = open("/dev/mem", O_RDWR | O_SYNC); if (fd < 0) { perror("open /dev/mem"); return -1; } // 页边界对齐映射 map_base = mmap(NULL, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target & ~(MAP_SIZE - 1)); if (map_base == MAP_FAILED) { perror("mmap"); close(fd); return -1; } addr = map_base + ((target & (MAP_SIZE - 1)) / sizeof(uint32_t)); return (*addr >> bit) & 1; } void usage() { printf("opt info:\n"); printf(" 1-set gpio 1 output 0\n"); printf(" 2-set gpio 1 output 1\n"); printf(" 3-set gpio 2 output 0\n"); printf(" 4-set gpio 2 output 1\n"); printf(" 5-get gpio 1 status\n"); printf(" 6-get gpio 2 status\n"); printf(" 0-exit\n"); printf("please input select:\n"); } /* ./a.out opt: w-write, r-read GPIO_num: 1, 2 value: 0, 1 for example: ./a.out w 1 0 GPIO-1 output 0 ./a.out r 1 Get GPIO1 status */ int main(int argc, char *argv[]) { int cmd = -1; int ret = 0; while(cmd) { usage(); scanf("%d", &cmd); switch(cmd) { case 1: ret = write_mem_bit(GPIO1_CTL, 0, 0); if(ret != 0) { printf("write gpio1 error\n"); } break; case 2: ret = write_mem_bit(GPIO1_CTL, 0, 1); if(ret != 0) { printf("write gpio1 error\n"); } break; case 3: ret = write_mem_bit(GPIO2_CTL, 0, 0); if(ret != 0) { printf("write gpio2 error\n"); } break; case 4: ret = write_mem_bit(GPIO2_CTL, 0, 1); if(ret != 0) { printf("write gpio2 error\n"); } break; case 5: printf("GPIO1 status = %d\n", read_mem_bit(GPIO1_STATUS, 1)); break; case 6: printf("GPIO2 status = %d\n", read_mem_bit(GPIO2_STATUS, 1)); break; case 0: printf("exit\n"); return 0; } } return 0; } #if 0 volatile uint32_t *map_base; int fd; void gpio_init(off_t target) { fd = open("/dev/mem", O_RDWR | O_SYNC); if (fd < 0) { perror("open /dev/mem"); exit(1); } map_base = mmap(NULL, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target & ~(MAP_SIZE - 1)); if (map_base == MAP_FAILED) { perror("mmap"); close(fd); exit(1); } } void gpio_write(off_t target, int bit, int value) { volatile uint32_t *addr = map_base + ((target & (MAP_SIZE - 1)) / sizeof(uint32_t)); uint32_t reg = *addr; if (value) reg |= (1 << bit); else reg &= ~(1 << bit); *addr = reg; } int gpio_read(off_t target, int bit) { volatile uint32_t *addr = map_base + ((target & (MAP_SIZE - 1)) / sizeof(uint32_t)); return (*addr >> bit) & 1; } void gpio_close() { munmap((void *)map_base, MAP_SIZE); close(fd); } int main() { // 示例:控制 GPIO1 输出高电平 printf("设置 GPIO1 输出高电平...\n"); gpio_init(0xFD6D0B50); gpio_write(0xFD6D0B50, 0, 1); gpio_close(); // 读取 GPIO1 的状态 printf("读取 GPIO1 状态...\n"); gpio_init(0xFD6D0940); int val1 = gpio_read(0xFD6D0940, 1); printf("GPIO1 状态 = %d\n", val1); gpio_close(); // 控制 GPIO2 输出低电平 printf("设置 GPIO2 输出低电平...\n"); gpio_init(0xFD6D0B60); gpio_write(0xFD6D0B60, 0, 0); gpio_close(); // 读取 GPIO2 的状态 printf("读取 GPIO2 状态...\n"); gpio_init(0xFD6D0950); int val2 = gpio_read(0xFD6D0950, 1); printf("GPIO2 状态 = %d\n", val2); gpio_close(); return 0; } #endif