IO_Test.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * IO_Test.c
  3. *
  4. * Created on: 2020¦~4¤ë21¤é
  5. * Author: Wendell
  6. */
  7. #include "IO_Test.h"
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <unistd.h> //write, close, usleep, read
  11. #include <fcntl.h> //uart
  12. const int System_GPIO_Pin_Table[SYS_GPIO_NUM] =
  13. {
  14. PIN_AM_OK_FLAG, PIN_IO_BD1_1, PIN_IO_BD1_2, PIN_IO_BD2_1, PIN_IO_BD2_2, PIN_ID_BD1_1, PIN_ID_BD1_2, PIN_ID_BD2_1,
  15. PIN_ID_BD2_2, PIN_AM_RFID_RST, PIN_AM_RFID_ICC, PIN_BOARD1_PROXIMITY, PIN_BOARD2_PROXIMITY, PIN_AM_DE_1, PIN_AM_RE_1, PIN_ETHERNET_RESET
  16. };
  17. void gpio_export(int pin)
  18. {
  19. char buffer[64];
  20. snprintf(buffer, sizeof(buffer), "echo %d > /sys/class/gpio/export", pin);
  21. system(buffer);
  22. }
  23. void gpio_unexport(int pin)
  24. {
  25. char buffer[64];
  26. snprintf(buffer, sizeof(buffer), "echo %d > /sys/class/gpio/unexport", pin);
  27. system(buffer);
  28. }
  29. void gpio_set_direction(int pin, unsigned char dir)
  30. {
  31. /*
  32. char buffer[64];
  33. snprintf(buffer, sizeof(buffer), "echo %s > /sys/class/gpio/gpio%d/direction", dir == GPIO_DIR_INPUT ? "in" : "out", pin);
  34. system(buffer);
  35. */
  36. int fd;
  37. char buffer[64];
  38. snprintf(buffer, sizeof(buffer), "/sys/class/gpio/gpio%d/direction", pin);
  39. fd = open(buffer, O_WRONLY);
  40. if (fd < 0)
  41. {
  42. gpio_export(pin);
  43. fd = open(buffer, O_WRONLY);
  44. if(fd < 0)
  45. {
  46. printf("\r\nFailed to open gpio%d direction for writing!", pin);
  47. return;
  48. }
  49. }
  50. write(fd, dir == GPIO_DIR_INPUT ? "in" : "out", dir == GPIO_DIR_INPUT ? 2 : 3);
  51. close(fd);
  52. }
  53. void gpio_write(int pin, unsigned char value)
  54. {
  55. char buffer[64];
  56. snprintf(buffer, sizeof(buffer), "echo %d > /sys/class/gpio/gpio%d/value", value > 0 ? 1 : 0, pin);
  57. system(buffer);
  58. }
  59. int gpio_read(int pin)
  60. {
  61. int fd, value = 0;
  62. char ch;
  63. char buffer[64];
  64. snprintf(buffer, sizeof(buffer), "/sys/class/gpio/gpio%d/value", pin);
  65. fd = open(buffer, O_RDONLY);
  66. if (fd < 0)
  67. {
  68. return -1;
  69. }
  70. if (read(fd, &ch, 4) < 0)
  71. {
  72. return -1;
  73. }
  74. value = atoi(&ch);
  75. close(fd);
  76. return value;
  77. }
  78. int adc_read(int adc_port)
  79. {
  80. int fd, value = 0;
  81. char ch[5];
  82. char buffer[64];
  83. snprintf(buffer,sizeof(buffer), "/sys/bus/iio/devices/iio:device0/in_voltage%d_raw", adc_port);
  84. fd = open(buffer, O_RDONLY);
  85. if(fd < 0)
  86. {
  87. return -1;
  88. }
  89. if(read(fd, ch, 4) < 0)
  90. {
  91. return -1;
  92. }
  93. value = atoi(ch);
  94. close(fd);
  95. return value;
  96. }
  97. void InitIO(void)
  98. {
  99. /* GPMC_AD8 => GPIO0_22 *//*ID BD1_1*/
  100. gpio_set_direction(PIN_ID_BD1_1, GPIO_DIR_INPUT);
  101. /* GPMC_AD9 => GPIO0_23 *//*ID BD1_2*/
  102. gpio_set_direction(PIN_ID_BD1_2, GPIO_DIR_INPUT);
  103. /* GPMC_AD10 => GPIO0_26 *//*IO BD1_1*/
  104. gpio_set_direction(PIN_IO_BD1_1, GPIO_DIR_OUTPUT);
  105. gpio_write(PIN_IO_BD1_1, 0);
  106. /* GPMC_AD11 => GPIO0_27 *//*IO BD1_2*/
  107. gpio_set_direction(PIN_IO_BD1_2, GPIO_DIR_OUTPUT);
  108. gpio_write(PIN_IO_BD1_2, 0);
  109. /*XDMA_EVENT_INTR0 => GPIO0_19 *//*AM_RFID_RST*/
  110. gpio_set_direction(PIN_AM_RFID_RST, GPIO_DIR_OUTPUT);
  111. gpio_write(PIN_AM_RFID_RST, 0);
  112. /*XDMA_EVENT_INTR1 => GPIO0_20 *//*AM_RFID_ICC*/
  113. gpio_set_direction(PIN_AM_RFID_ICC, GPIO_DIR_OUTPUT);
  114. gpio_write(PIN_AM_RFID_ICC, 0);
  115. /* GPMC_AD12 => GPIO1_12 *//*ID BD2_1*/
  116. gpio_set_direction(PIN_ID_BD2_1, GPIO_DIR_INPUT);
  117. /* GPMC_AD13 => GPIO1_13 *//*ID BD2_2*/
  118. gpio_set_direction(PIN_ID_BD2_2, GPIO_DIR_INPUT);
  119. /* GPMC_AD14 => GPIO1_14 *//*IO BD2_1*/
  120. gpio_set_direction(PIN_IO_BD2_1, GPIO_DIR_OUTPUT);
  121. gpio_write(PIN_IO_BD2_1, 0);
  122. /* GPMC_AD15 => GPIO1_15 *//*IO BD2_2*/
  123. gpio_set_direction(PIN_IO_BD2_2, GPIO_DIR_OUTPUT);
  124. gpio_write(PIN_IO_BD2_2, 0);
  125. /* MCASP0_AXR0 => GPIO3_16 *//*CSU board function OK indicator.*/
  126. gpio_set_direction(PIN_AM_OK_FLAG, GPIO_DIR_OUTPUT);
  127. gpio_write(PIN_AM_OK_FLAG, 0);
  128. gpio_set_direction(PIN_BOARD1_PROXIMITY, GPIO_DIR_INPUT);
  129. gpio_set_direction(PIN_BOARD2_PROXIMITY, GPIO_DIR_INPUT);
  130. }
  131. void DeInitIO(void)
  132. {
  133. gpio_unexport(PIN_ID_BD1_1);
  134. gpio_unexport(PIN_ID_BD1_2);
  135. gpio_unexport(PIN_IO_BD1_1);
  136. gpio_unexport(PIN_IO_BD1_2);
  137. gpio_unexport(PIN_AM_RFID_RST);
  138. gpio_unexport(PIN_AM_RFID_ICC);
  139. gpio_unexport(PIN_ID_BD2_1);
  140. gpio_unexport(PIN_ID_BD2_2);
  141. gpio_unexport(PIN_IO_BD2_1);
  142. gpio_unexport(PIN_IO_BD2_2);
  143. gpio_unexport(PIN_AM_OK_FLAG);
  144. }
  145. void DoIOTest(void)
  146. {
  147. InitIO();
  148. gpio_write(PIN_IO_BD1_1, 1);
  149. if(gpio_read(PIN_ID_BD1_1) == 1)
  150. {
  151. gpio_write(PIN_IO_BD1_1, 0);
  152. if(gpio_read(PIN_ID_BD1_1) == 0)
  153. {
  154. printf("\r\nID_BD1_1 Test OK");
  155. }
  156. else
  157. {
  158. printf("\r\nID_BD1_1 Low Test Fail");
  159. return;
  160. }
  161. }
  162. else
  163. {
  164. printf("\r\nID_BD1_1 High Test Fail");
  165. return;
  166. }
  167. gpio_write(PIN_IO_BD1_2, 1);
  168. if(gpio_read(PIN_ID_BD1_2) == 1)
  169. {
  170. gpio_write(PIN_IO_BD1_2, 0);
  171. if(gpio_read(PIN_ID_BD1_2) == 0)
  172. {
  173. printf("\r\nID_BD1_2 Test OK");
  174. }
  175. else
  176. {
  177. printf("\r\nID_BD1_2 Low Test Fail");
  178. return;
  179. }
  180. }
  181. else
  182. {
  183. printf("\r\nID_BD1_2 High Test Fail");
  184. return;
  185. }
  186. gpio_write(PIN_IO_BD2_1, 1);
  187. if(gpio_read(PIN_ID_BD2_1) == 1)
  188. {
  189. gpio_write(PIN_IO_BD2_1, 0);
  190. if(gpio_read(PIN_ID_BD2_1) == 0)
  191. {
  192. printf("\r\nID_BD2_1 Test OK");
  193. }
  194. else
  195. {
  196. printf("\r\nID_BD2_1 Low Test Fail");
  197. return;
  198. }
  199. }
  200. else
  201. {
  202. printf("\r\nID_BD2_1 High Test Fail");
  203. return;
  204. }
  205. gpio_write(PIN_IO_BD2_2, 1);
  206. if(gpio_read(PIN_ID_BD2_2) == 1)
  207. {
  208. gpio_write(PIN_IO_BD2_2, 0);
  209. if(gpio_read(PIN_ID_BD2_2) == 0)
  210. {
  211. printf("\r\nID_BD2_2 Test OK");
  212. }
  213. else
  214. {
  215. printf("\r\nID_BD2_2 Low Test Fail");
  216. return;
  217. }
  218. }
  219. else
  220. {
  221. printf("\r\nID_BD2_2 High Test Fail");
  222. return;
  223. }
  224. gpio_write(PIN_AM_RFID_RST, 1);
  225. if(gpio_read(PIN_BOARD1_PROXIMITY) == 1 && gpio_read(PIN_BOARD2_PROXIMITY) == 1)
  226. {
  227. gpio_write(PIN_AM_RFID_RST, 0);
  228. if(gpio_read(PIN_BOARD1_PROXIMITY) == 0 && gpio_read(PIN_BOARD2_PROXIMITY) == 0)
  229. {
  230. printf("\r\nBoard1 & Board2 Proximity Test OK");
  231. }
  232. else
  233. {
  234. printf("\r\nBoard1 & Board2 Proximity Low Test Fail");
  235. return;
  236. }
  237. }
  238. else
  239. {
  240. printf("\r\nBoard1 & Board2 Proximity High Test Fail");
  241. return;
  242. }
  243. gpio_write(PIN_AM_RFID_ICC, 1);
  244. usleep(100000);
  245. if(adc_read(ADC_AIN0) < 100 && adc_read(ADC_AIN1) < 100 && adc_read(ADC_AIN2) < 100 && adc_read(ADC_AIN3) < 100)
  246. {
  247. gpio_write(PIN_AM_RFID_ICC, 0);
  248. usleep(100000);
  249. if(adc_read(ADC_AIN0) > 4000 && adc_read(ADC_AIN1) > 4000 && adc_read(ADC_AIN2) > 4000 && adc_read(ADC_AIN3) > 4000)
  250. {
  251. printf("\r\nAIN0, AIN1, AIN2, AIN3 Test OK");
  252. }
  253. else
  254. {
  255. printf("\r\nAIN0, AIN1, AIN2, AIN3 High Test Fail");
  256. return;
  257. }
  258. }
  259. else
  260. {
  261. printf("\r\nAIN0, AIN1, AIN2, AIN3 Low Test Fail");
  262. return;
  263. }
  264. gpio_write(PIN_AM_OK_FLAG, 1);
  265. printf("\r\nIO Test Done!");
  266. printf("\r\nSuccess!\r\n");
  267. }