ctl_gpio.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. #include <sys/mman.h>
  6. #include <stdint.h> // 添加这个头文件来支持 uint32_t
  7. #define MAP_SIZE 0x1000 // 4KB 页大小
  8. #define GPIO1_STATUS 0xFD6D0A40
  9. #define GPIO2_STATUS 0xFD6D0A50
  10. int write_mem_bit(off_t target, int bit, int value)
  11. {
  12. volatile uint32_t *map_base = NULL;
  13. volatile uint32_t *addr = NULL;
  14. int fd;
  15. fd = open("/dev/mem", O_RDWR | O_SYNC);
  16. if (fd < 0)
  17. {
  18. perror("open /dev/mem");
  19. return -1;
  20. }
  21. // 页边界对齐映射
  22. map_base = mmap(NULL, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target & ~(MAP_SIZE - 1));
  23. if (map_base == MAP_FAILED)
  24. {
  25. perror("mmap");
  26. close(fd);
  27. return -1;
  28. }
  29. addr = map_base + ((target & (MAP_SIZE - 1)) / sizeof(uint32_t));
  30. uint32_t reg = *addr;
  31. if (value)
  32. reg |= (1 << bit);
  33. else
  34. reg &= ~(1 << bit);
  35. *addr = reg;
  36. return 0;
  37. }
  38. int read_mem_bit(off_t target, int bit)
  39. {
  40. volatile uint32_t *map_base = NULL;
  41. volatile uint32_t *addr = NULL;
  42. int fd;
  43. fd = open("/dev/mem", O_RDWR | O_SYNC);
  44. if (fd < 0)
  45. {
  46. perror("open /dev/mem");
  47. return -1;
  48. }
  49. // 页边界对齐映射
  50. map_base = mmap(NULL, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target & ~(MAP_SIZE - 1));
  51. if (map_base == MAP_FAILED)
  52. {
  53. perror("mmap");
  54. close(fd);
  55. return -1;
  56. }
  57. addr = map_base + ((target & (MAP_SIZE - 1)) / sizeof(uint32_t));
  58. return (*addr >> bit) & 1;
  59. }
  60. void usage()
  61. {
  62. printf("opt info:\n");
  63. printf(" 1-set gpio 1 output 0\n");
  64. printf(" 2-set gpio 1 output 1\n");
  65. printf(" 3-set gpio 2 output 0\n");
  66. printf(" 4-set gpio 2 output 1\n");
  67. printf(" 5-get gpio 1 status\n");
  68. printf(" 6-get gpio 2 status\n");
  69. printf(" 0-exit\n");
  70. printf("please input select:\n");
  71. }
  72. /*
  73. ./a.out <opt> <GPIO_num> <value>
  74. opt: w-write, r-read
  75. GPIO_num: 1, 2
  76. value: 0, 1
  77. for example:
  78. ./a.out w 1 0 GPIO-1 output 0
  79. ./a.out r 1 Get GPIO1 status
  80. */
  81. int main(int argc, char *argv[])
  82. {
  83. int cmd = -1;
  84. int ret = 0;
  85. while(1)
  86. {
  87. usage();
  88. //scanf("%d", &cmd);
  89. switch(atoi(argv[1]))
  90. {
  91. case 5:
  92. printf("GPIO1 status = %d\n", read_mem_bit(GPIO1_STATUS, 1));
  93. break;
  94. case 6:
  95. printf("GPIO2 status = %d\n", read_mem_bit(GPIO2_STATUS, 1));
  96. break;
  97. case 0:
  98. printf("exit\n");
  99. return 0;
  100. }
  101. if(atoi(argv[1])==5 || atoi(argv[1])==6)
  102. {
  103. break;
  104. }
  105. }
  106. return 0;
  107. }
  108. #if 0
  109. volatile uint32_t *map_base;
  110. int fd;
  111. void gpio_init(off_t target) {
  112. fd = open("/dev/mem", O_RDWR | O_SYNC);
  113. if (fd < 0) {
  114. perror("open /dev/mem");
  115. exit(1);
  116. }
  117. map_base = mmap(NULL, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target & ~(MAP_SIZE - 1));
  118. if (map_base == MAP_FAILED) {
  119. perror("mmap");
  120. close(fd);
  121. exit(1);
  122. }
  123. }
  124. void gpio_write(off_t target, int bit, int value) {
  125. volatile uint32_t *addr = map_base + ((target & (MAP_SIZE - 1)) / sizeof(uint32_t));
  126. uint32_t reg = *addr;
  127. if (value)
  128. reg |= (1 << bit);
  129. else
  130. reg &= ~(1 << bit);
  131. *addr = reg;
  132. }
  133. int gpio_read(off_t target, int bit) {
  134. volatile uint32_t *addr = map_base + ((target & (MAP_SIZE - 1)) / sizeof(uint32_t));
  135. return (*addr >> bit) & 1;
  136. }
  137. void gpio_close() {
  138. munmap((void *)map_base, MAP_SIZE);
  139. close(fd);
  140. }
  141. int main() {
  142. // 示例:控制 GPIO1 输出高电平
  143. printf("设置 GPIO1 输出高电平...\n");
  144. gpio_init(0xFD6D0B50);
  145. gpio_write(0xFD6D0B50, 0, 1);
  146. gpio_close();
  147. // 读取 GPIO1 的状态
  148. printf("读取 GPIO1 状态...\n");
  149. gpio_init(0xFD6D0940);
  150. int val1 = gpio_read(0xFD6D0940, 1);
  151. printf("GPIO1 状态 = %d\n", val1);
  152. gpio_close();
  153. // 控制 GPIO2 输出低电平
  154. printf("设置 GPIO2 输出低电平...\n");
  155. gpio_init(0xFD6D0B60);
  156. gpio_write(0xFD6D0B60, 0, 0);
  157. gpio_close();
  158. // 读取 GPIO2 的状态
  159. printf("读取 GPIO2 状态...\n");
  160. gpio_init(0xFD6D0950);
  161. int val2 = gpio_read(0xFD6D0950, 1);
  162. printf("GPIO2 状态 = %d\n", val2);
  163. gpio_close();
  164. return 0;
  165. }
  166. #endif