Advertisement
Guest User

Untitled

a guest
Jul 4th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.24 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <sys/mman.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10.  
  11.  
  12. #define MMAP_PATH "/dev/mem"
  13.  
  14.  
  15. #define RALINK_GPIO_DIR_IN 0
  16. #define RALINK_GPIO_DIR_OUT 1
  17.  
  18. #define RALINK_REG_PIOINT 0x690
  19. #define RALINK_REG_PIOEDGE 0x6A0
  20. #define RALINK_REG_PIORENA 0x650
  21. #define RALINK_REG_PIOFENA 0x660
  22. #define RALINK_REG_PIODATA 0x620
  23. #define RALINK_REG_PIODIR 0x600
  24. #define RALINK_REG_PIOSET 0x630
  25. #define RALINK_REG_PIORESET 0x640
  26.  
  27. #define RALINK_REG_PIO6332INT 0x694
  28. #define RALINK_REG_PIO6332EDGE 0x6A4
  29. #define RALINK_REG_PIO6332RENA 0x654
  30. #define RALINK_REG_PIO6332FENA 0x664
  31. #define RALINK_REG_PIO6332DATA 0x624
  32. #define RALINK_REG_PIO6332DIR 0x604
  33. #define RALINK_REG_PIO6332SET 0x634
  34. #define RALINK_REG_PIO6332RESET 0x644
  35.  
  36. #define RALINK_REG_PIO9564INT 0x698
  37. #define RALINK_REG_PIO9564EDGE 0x6A8
  38. #define RALINK_REG_PIO9564RENA 0x658
  39. #define RALINK_REG_PIO9564FENA 0x668
  40. #define RALINK_REG_PIO9564DATA 0x628
  41. #define RALINK_REG_PIO9564DIR 0x608
  42. #define RALINK_REG_PIO9564SET 0x638
  43. #define RALINK_REG_PIO9564RESET 0x648
  44.  
  45.  
  46. static uint8_t* gpio_mmap_reg = NULL;
  47. static int gpio_mmap_fd = 0;
  48.  
  49. static int gpio_mmap(void)
  50. {
  51. if ((gpio_mmap_fd = open(MMAP_PATH, O_RDWR)) < 0) {
  52. fprintf(stderr, "unable to open mmap file");
  53. return -1;
  54. }
  55.  
  56. gpio_mmap_reg = (uint8_t*) mmap(NULL, 1024, PROT_READ | PROT_WRITE,
  57. MAP_FILE | MAP_SHARED, gpio_mmap_fd, 0x10000000);
  58. if (gpio_mmap_reg == MAP_FAILED) {
  59. perror("foo");
  60. fprintf(stderr, "failed to mmap");
  61. gpio_mmap_reg = NULL;
  62. close(gpio_mmap_fd);
  63. return -1;
  64. }
  65.  
  66. return 0;
  67. }
  68.  
  69. int mt76x8_gpio_get_pin(int pin)
  70. {
  71. uint32_t tmp = 0;
  72.  
  73. /* MT7621, MT7628 */
  74. if (pin <= 31) {
  75. tmp = *(volatile uint32_t *)(gpio_mmap_reg + RALINK_REG_PIODATA);
  76. tmp = (tmp >> pin) & 1u;
  77. } else if (pin <= 63) {
  78. tmp = *(volatile uint32_t *)(gpio_mmap_reg + RALINK_REG_PIO6332DATA);
  79. tmp = (tmp >> (pin-32)) & 1u;
  80. } else if (pin <= 95) {
  81. tmp = *(volatile uint32_t *)(gpio_mmap_reg + RALINK_REG_PIO9564DATA);
  82. tmp = (tmp >> (pin-64)) & 1u;
  83. tmp = (tmp >> (pin-24)) & 1u;
  84. }
  85. return tmp;
  86.  
  87. }
  88.  
  89. void mt76x8_gpio_set_pin_direction(int pin, int is_output)
  90. {
  91. uint32_t tmp;
  92.  
  93. /* MT7621, MT7628 */
  94. if (pin <= 31) {
  95. tmp = *(volatile uint32_t *)(gpio_mmap_reg + RALINK_REG_PIODIR);
  96. if (is_output)
  97. tmp |= (1u << pin);
  98. else
  99. tmp &= ~(1u << pin);
  100. *(volatile uint32_t *)(gpio_mmap_reg + RALINK_REG_PIODIR) = tmp;
  101. } else if (pin <= 63) {
  102. tmp = *(volatile uint32_t *)(gpio_mmap_reg + RALINK_REG_PIO6332DIR);
  103. if (is_output)
  104. tmp |= (1u << (pin-32));
  105. else
  106. tmp &= ~(1u << (pin-32));
  107. *(volatile uint32_t *)(gpio_mmap_reg + RALINK_REG_PIO6332DIR) = tmp;
  108. } else if (pin <= 95) {
  109. tmp = *(volatile uint32_t *)(gpio_mmap_reg + RALINK_REG_PIO9564DIR);
  110. if (is_output)
  111. tmp |= (1u << (pin-64));
  112. else
  113. tmp &= ~(1u << (pin-64));
  114. *(volatile uint32_t *)(gpio_mmap_reg + RALINK_REG_PIO9564DIR) = tmp;
  115. }
  116. }
  117.  
  118. void mt76x8_gpio_set_pin_value(int pin, int value)
  119. {
  120. uint32_t tmp;
  121.  
  122. /* MT7621, MT7628 */
  123. if (pin <= 31) {
  124. tmp = (1u << pin);
  125. if (value)
  126. *(volatile uint32_t *)(gpio_mmap_reg + RALINK_REG_PIOSET) = tmp;
  127. else
  128. *(volatile uint32_t *)(gpio_mmap_reg + RALINK_REG_PIORESET) = tmp;
  129. } else if (pin <= 63) {
  130. tmp = (1u << (pin-32));
  131. if (value)
  132. *(volatile uint32_t *)(gpio_mmap_reg + RALINK_REG_PIO6332SET) = tmp;
  133. else
  134. *(volatile uint32_t *)(gpio_mmap_reg + RALINK_REG_PIO6332RESET) = tmp;
  135. } else if (pin <= 95) {
  136. tmp = (1u << (pin-64));
  137. if (value)
  138. *(volatile uint32_t *)(gpio_mmap_reg + RALINK_REG_PIO9564SET) = tmp;
  139. else
  140. *(volatile uint32_t *)(gpio_mmap_reg + RALINK_REG_PIO9564RESET) = tmp;
  141. }
  142. }
  143.  
  144. int main(int argc, char **argv)
  145. {
  146. int ret = -1;
  147.  
  148. if (gpio_mmap())
  149. return -1;
  150.  
  151. printf("get pin 39 input %d\n", mt76x8_gpio_get_pin(39));
  152. printf("get pin 40 input %d\n", mt76x8_gpio_get_pin(40));
  153. printf("get pin 41 input %d\n", mt76x8_gpio_get_pin(41));
  154. printf("get pin 42 input %d\n", mt76x8_gpio_get_pin(42));
  155.  
  156.  
  157. printf("set pin 39 output 1\n");
  158. mt76x8_gpio_set_pin_direction(39, 1);
  159. mt76x8_gpio_set_pin_value(39, 1);
  160. printf("set pin 40 output 0\n");
  161. mt76x8_gpio_set_pin_direction(40, 1);
  162. mt76x8_gpio_set_pin_value(40, 0);
  163. printf("set pin 41 output 1\n");
  164. mt76x8_gpio_set_pin_direction(41, 1);
  165. mt76x8_gpio_set_pin_value(41, 1);
  166. printf("set pin 42 output 0\n");
  167. mt76x8_gpio_set_pin_direction(44, 1);
  168. mt76x8_gpio_set_pin_value(44, 0);
  169.  
  170. while (1)
  171. {
  172. mt76x8_gpio_set_pin_value(44, 0);
  173. mt76x8_gpio_set_pin_value(44, 1);
  174. }
  175. close(gpio_mmap_fd);
  176.  
  177. return ret;
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement