You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

readall.c 7.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * readall.c:
  3. * The readall functions - getting a bit big, so split them out.
  4. * Copyright (c) 2012-2013 Gordon Henderson
  5. ***********************************************************************
  6. * This file is part of wiringPi:
  7. * https://projects.drogon.net/raspberry-pi/wiringpi/
  8. *
  9. * wiringPi is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * wiringPi is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
  21. ***********************************************************************
  22. */
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <stdint.h>
  26. #include <ctype.h>
  27. #include <string.h>
  28. #include <unistd.h>
  29. #include <errno.h>
  30. #include <fcntl.h>
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #include <wiringPi.h>
  34. extern int wpMode ;
  35. /*
  36. * doReadallExternal:
  37. * A relatively crude way to read the pins on an external device.
  38. * We don't know the input/output mode of pins, but we can tell
  39. * if it's an analog pin or a digital one...
  40. *********************************************************************************
  41. */
  42. static void doReadallExternal (void)
  43. {
  44. int pin ;
  45. printf ("+------+---------+--------+\n") ;
  46. printf ("| Pin | Digital | Analog |\n") ;
  47. printf ("+------+---------+--------+\n") ;
  48. for (pin = wiringPiNodes->pinBase ; pin <= wiringPiNodes->pinMax ; ++pin)
  49. printf ("| %4d | %4d | %4d |\n", pin, digitalRead (pin), analogRead (pin)) ;
  50. printf ("+------+---------+--------+\n") ;
  51. }
  52. /*
  53. * doReadall:
  54. * Read all the GPIO pins
  55. * We also want to use this to read the state of pins on an externally
  56. * connected device, so we need to do some fiddling with the internal
  57. * wiringPi node structures - since the gpio command can only use
  58. * one external device at a time, we'll use that to our advantage...
  59. *********************************************************************************
  60. */
  61. static char *pinNames [] =
  62. {
  63. "GPIO 0", "GPIO 1", "GPIO 2", "GPIO 3", "GPIO 4", "GPIO 5", "GPIO 6", "GPIO 7",
  64. "SDA ", "SCL ",
  65. "CE0 ", "CE1 ", "MOSI ", "MISO ", "SCLK ",
  66. "TxD ", "RxD ",
  67. "GPIO 8", "GPIO 9", "GPIO10", "GPIO11",
  68. } ;
  69. static char *alts [] =
  70. {
  71. "IN", "OUT", "ALT5", "ALT4", "ALT0", "ALT1", "ALT2", "ALT3"
  72. } ;
  73. static int wpiToPhys [64] =
  74. {
  75. 11, 12, 13, 15, 16, 18, 22, 7, // 0...7
  76. 3, 5, // 8...9
  77. 24, 26, 19, 21, 23, // 10..14
  78. 8, 10, // 15..16
  79. 3, 4, 5, 6, // 17..20
  80. 0,0,0,0,0,0,0,0,0,0,0, // 20..31
  81. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 32..47
  82. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 47..63
  83. } ;
  84. // The other mappings needed are in wiringPi.c
  85. static int physToWpi [64] =
  86. {
  87. -1, // 0
  88. -1, -1, // 1, 2
  89. 8, -1,
  90. 9, -1,
  91. 7, 15,
  92. -1, 16,
  93. 0, 1,
  94. 2, -1,
  95. 3, 4,
  96. -1, 5,
  97. 12, -1,
  98. 13, 6,
  99. 14, 10,
  100. -1, 11, // 25, 26
  101. // Padding:
  102. -1, -1, -1, -1, -1, // ... 31
  103. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
  104. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
  105. } ;
  106. static char *physNames [64] =
  107. {
  108. NULL,
  109. "3.3v", "5v",
  110. "SDA", "5V",
  111. "SCL", "0v",
  112. "GPIO7", "TxD",
  113. "0v", "RxD",
  114. "GPIO0", "GPIO1",
  115. "GPIO2", "0v",
  116. "GPIO3", "GPIO4",
  117. "3.3v", "GPIO5",
  118. "MOSI", "0v",
  119. "MISO", "GPIO6",
  120. "SCLK", "CE1",
  121. "0v", "CE1",
  122. NULL,NULL,NULL,NULL,NULL,
  123. NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
  124. NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
  125. } ;
  126. static void readallPhys (int physPin)
  127. {
  128. int pin ;
  129. /**/ if (wpMode == WPI_MODE_GPIO)
  130. {
  131. if (physPinToGpio (physPin) == -1)
  132. printf (" | ") ;
  133. else
  134. printf (" | %3d", physPinToGpio (physPin)) ;
  135. }
  136. else if (wpMode != WPI_MODE_PHYS)
  137. {
  138. if (physToWpi [physPin] == -1)
  139. printf (" | ") ;
  140. else
  141. printf (" | %3d", physToWpi [physPin]) ;
  142. }
  143. printf (" | %5s", physNames [physPin]) ;
  144. if (physToWpi [physPin] == -1)
  145. printf (" | | ") ;
  146. else
  147. {
  148. /**/ if (wpMode == WPI_MODE_GPIO)
  149. pin = physPinToGpio (physPin) ;
  150. else if (wpMode == WPI_MODE_PHYS)
  151. pin = physPin ;
  152. else
  153. pin = physToWpi [physPin] ;
  154. printf (" | %4s", alts [getAlt (pin)]) ;
  155. printf (" | %s", (digitalRead (pin) == 0) ? "Hi" : "Lo") ;
  156. }
  157. // Pin numbers:
  158. printf (" | %2d", physPin) ;
  159. ++physPin ;
  160. printf (" || %-2d", physPin) ;
  161. // Same, reversed
  162. if (physToWpi [physPin] == -1)
  163. printf (" | | ") ;
  164. else
  165. {
  166. /**/ if (wpMode == WPI_MODE_GPIO)
  167. pin = physPinToGpio (physPin) ;
  168. else if (wpMode == WPI_MODE_PHYS)
  169. pin = physPin ;
  170. else
  171. pin = physToWpi [physPin] ;
  172. printf (" | %s", (digitalRead (pin) == 0) ? "Hi" : "Lo") ;
  173. printf (" | %-4s", alts [getAlt (pin)]) ;
  174. }
  175. printf (" | %-5s", physNames [physPin]) ;
  176. /**/ if (wpMode == WPI_MODE_GPIO)
  177. {
  178. if (physPinToGpio (physPin) == -1)
  179. printf (" | ") ;
  180. else
  181. printf (" | %-3d", physPinToGpio (physPin)) ;
  182. }
  183. else if (wpMode != WPI_MODE_PHYS)
  184. {
  185. if (physToWpi [physPin] == -1)
  186. printf (" | ") ;
  187. else
  188. printf (" | %-3d", physToWpi [physPin]) ;
  189. }
  190. printf (" |\n") ;
  191. }
  192. void doReadall (void)
  193. {
  194. int pin ;
  195. if (wiringPiNodes != NULL) // External readall
  196. {
  197. doReadallExternal () ;
  198. return ;
  199. }
  200. /**/ if (wpMode == WPI_MODE_GPIO)
  201. {
  202. printf (" +-----+-------+------+----+-Rev%d-----+----+------+-------+-----+\n", piBoardRev ()) ;
  203. printf (" | BCM | Name | Mode | Val| Physical |Val | Mode | Name | BCM |\n") ;
  204. printf (" +-----+-------+------+----+----++----+----+------+-------+-----+\n") ;
  205. for (pin = 1 ; pin <= 26 ; pin += 2)
  206. readallPhys (pin) ;
  207. printf (" +-----+-------+------+----+----++----+----+------+-------+-----+\n") ;
  208. }
  209. else if (wpMode == WPI_MODE_PHYS)
  210. {
  211. printf (" +-------+------+----+-Rev%d-----+----+------+-------+\n", piBoardRev ()) ;
  212. printf (" | Name | Mode | Val| Physical |Val | Mode | Name |\n") ;
  213. printf (" +-------+------+----+----++----+----+------+-------+\n") ;
  214. for (pin = 1 ; pin <= 26 ; pin += 2)
  215. readallPhys (pin) ;
  216. printf (" +-------+------+----+----++----+----+------+-------+\n") ;
  217. }
  218. else // wiringPi
  219. {
  220. printf (" +-----+-------+------+----+-Rev%d-----+----+------+-------+-----+\n", piBoardRev ()) ;
  221. printf (" | wPi | Name | Mode | Val| Physical |Val | Mode | Name | wPi |\n") ;
  222. printf (" +-----+-------+------+----+----++----+----+------+-------+-----+\n") ;
  223. for (pin = 1 ; pin <= 26 ; pin += 2)
  224. readallPhys (pin) ;
  225. printf (" +-----+-------+------+----+----++----+----+------+-------+-----+\n") ;
  226. }
  227. }
  228. void doReadallOld (void)
  229. {
  230. int pin ;
  231. if (wiringPiNodes != NULL) // External readall
  232. {
  233. doReadallExternal () ;
  234. return ;
  235. }
  236. printf ("+----------+-Rev%d-+------+--------+------+-------+\n", piBoardRev ()) ;
  237. printf ("| wiringPi | GPIO | Phys | Name | Mode | Value |\n") ;
  238. printf ("+----------+------+------+--------+------+-------+\n") ;
  239. for (pin = 0 ; pin < 64 ; ++pin) // Crude, but effective
  240. {
  241. if (wpiPinToGpio (pin) == -1)
  242. continue ;
  243. printf ("| %6d | %3d | %3d | %s | %-4s | %-4s |\n",
  244. pin, wpiPinToGpio (pin), wpiToPhys [pin],
  245. pinNames [pin],
  246. alts [getAlt (pin)],
  247. digitalRead (pin) == HIGH ? "High" : "Low ") ;
  248. }
  249. printf ("+----------+------+------+--------+------+-------+\n") ;
  250. }