| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <sys/mman.h>
- #include <stdint.h> // 添加这个头文件来支持 uint32_t
- #define MAP_SIZE 0x1000 // 4KB 页大小
- #define GPIO1_STATUS 0xFD6D0A40
- #define GPIO2_STATUS 0xFD6D0A50
- 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> <GPIO_num> <value>
- 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(1)
- {
- usage();
- //scanf("%d", &cmd);
- switch(atoi(argv[1]))
- {
- 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;
- }
- if(atoi(argv[1])==5 || atoi(argv[1])==6)
- {
- break;
- }
- }
- 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
|