readall.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * readall.c:
  3. * The readall functions - getting a bit big, so split them out.
  4. * Copyright (c) 2012-2018 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. #ifndef TRUE
  36. # define TRUE (1==1)
  37. # define FALSE (1==2)
  38. #endif
  39. /*
  40. * doReadallExternal:
  41. * A relatively crude way to read the pins on an external device.
  42. * We don't know the input/output mode of pins, but we can tell
  43. * if it's an analog pin or a digital one...
  44. *********************************************************************************
  45. */
  46. static void doReadallExternal (void)
  47. {
  48. int pin ;
  49. printf ("+------+---------+--------+\n") ;
  50. printf ("| Pin | Digital | Analog |\n") ;
  51. printf ("+------+---------+--------+\n") ;
  52. for (pin = wiringPiNodes->pinBase ; pin <= wiringPiNodes->pinMax ; ++pin)
  53. printf ("| %4d | %4d | %4d |\n", pin, digitalRead (pin), analogRead (pin)) ;
  54. printf ("+------+---------+--------+\n") ;
  55. }
  56. /*
  57. * doReadall:
  58. * Read all the GPIO pins
  59. * We also want to use this to read the state of pins on an externally
  60. * connected device, so we need to do some fiddling with the internal
  61. * wiringPi node structures - since the gpio command can only use
  62. * one external device at a time, we'll use that to our advantage...
  63. *********************************************************************************
  64. */
  65. static char *alts [] =
  66. {
  67. "IN", "OUT", "ALT5", "ALT4", "ALT0", "ALT1", "ALT2", "ALT3"
  68. } ;
  69. static int physToWpi [64] =
  70. {
  71. -1, // 0
  72. -1, -1, // 1, 2
  73. 8, -1,
  74. 9, -1,
  75. 7, 15,
  76. -1, 16,
  77. 0, 1,
  78. 2, -1,
  79. 3, 4,
  80. -1, 5,
  81. 12, -1,
  82. 13, 6,
  83. 14, 10,
  84. -1, 11, // 25, 26
  85. 30, 31, // Actually I2C, but not used
  86. 21, -1,
  87. 22, 26,
  88. 23, -1,
  89. 24, 27,
  90. 25, 28,
  91. -1, 29,
  92. -1, -1,
  93. -1, -1,
  94. -1, -1,
  95. -1, -1,
  96. -1, -1,
  97. 17, 18,
  98. 19, 20,
  99. -1, -1, -1, -1, -1, -1, -1, -1, -1
  100. } ;
  101. static char *physNames [64] =
  102. {
  103. NULL,
  104. " 3.3v", "5v ",
  105. " SDA.1", "5v ",
  106. " SCL.1", "0v ",
  107. "GPIO. 7", "TxD ",
  108. " 0v", "RxD ",
  109. "GPIO. 0", "GPIO. 1",
  110. "GPIO. 2", "0v ",
  111. "GPIO. 3", "GPIO. 4",
  112. " 3.3v", "GPIO. 5",
  113. " MOSI", "0v ",
  114. " MISO", "GPIO. 6",
  115. " SCLK", "CE0 ",
  116. " 0v", "CE1 ",
  117. " SDA.0", "SCL.0 ",
  118. "GPIO.21", "0v ",
  119. "GPIO.22", "GPIO.26",
  120. "GPIO.23", "0v ",
  121. "GPIO.24", "GPIO.27",
  122. "GPIO.25", "GPIO.28",
  123. " 0v", "GPIO.29",
  124. NULL, NULL,
  125. NULL, NULL,
  126. NULL, NULL,
  127. NULL, NULL,
  128. NULL, NULL,
  129. "GPIO.17", "GPIO.18",
  130. "GPIO.19", "GPIO.20",
  131. NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
  132. } ;
  133. /*
  134. * readallPhys:
  135. * Given a physical pin output the data on it and the next pin:
  136. *| BCM | wPi | Name | Mode | Val| Physical |Val | Mode | Name | wPi | BCM |
  137. *********************************************************************************
  138. */
  139. static void readallPhys (int physPin)
  140. {
  141. int pin ;
  142. if (physPinToGpio (physPin) == -1)
  143. printf (" | | ") ;
  144. else
  145. printf (" | %3d | %3d", physPinToGpio (physPin), physToWpi [physPin]) ;
  146. printf (" | %s", physNames [physPin]) ;
  147. if (physToWpi [physPin] == -1)
  148. printf (" | | ") ;
  149. else
  150. {
  151. /**/ if (wpMode == WPI_MODE_GPIO)
  152. pin = physPinToGpio (physPin) ;
  153. else if (wpMode == WPI_MODE_PHYS)
  154. pin = physPin ;
  155. else
  156. pin = physToWpi [physPin] ;
  157. printf (" | %4s", alts [getAlt (pin)]) ;
  158. printf (" | %d", digitalRead (pin)) ;
  159. }
  160. // Pin numbers:
  161. printf (" | %2d", physPin) ;
  162. ++physPin ;
  163. printf (" || %-2d", physPin) ;
  164. // Same, reversed
  165. if (physToWpi [physPin] == -1)
  166. printf (" | | ") ;
  167. else
  168. {
  169. /**/ if (wpMode == WPI_MODE_GPIO)
  170. pin = physPinToGpio (physPin) ;
  171. else if (wpMode == WPI_MODE_PHYS)
  172. pin = physPin ;
  173. else
  174. pin = physToWpi [physPin] ;
  175. printf (" | %d", digitalRead (pin)) ;
  176. printf (" | %-4s", alts [getAlt (pin)]) ;
  177. }
  178. printf (" | %-5s", physNames [physPin]) ;
  179. if (physToWpi [physPin] == -1)
  180. printf (" | | ") ;
  181. else
  182. printf (" | %-3d | %-3d", physToWpi [physPin], physPinToGpio (physPin)) ;
  183. printf (" |\n") ;
  184. }
  185. /*
  186. * allReadall:
  187. * Read all the pins regardless of the model. Primarily of use for
  188. * the compute module, but handy for other fiddling...
  189. *********************************************************************************
  190. */
  191. static void allReadall (void)
  192. {
  193. int pin ;
  194. printf ("+-----+------+-------+ +-----+------+-------+\n") ;
  195. printf ("| Pin | Mode | Value | | Pin | Mode | Value |\n") ;
  196. printf ("+-----+------+-------+ +-----+------+-------+\n") ;
  197. for (pin = 0 ; pin < 27 ; ++pin)
  198. {
  199. printf ("| %3d ", pin) ;
  200. printf ("| %-4s ", alts [getAlt (pin)]) ;
  201. printf ("| %s ", digitalRead (pin) == HIGH ? "High" : "Low ") ;
  202. printf ("| ") ;
  203. printf ("| %3d ", pin + 27) ;
  204. printf ("| %-4s ", alts [getAlt (pin + 27)]) ;
  205. printf ("| %s ", digitalRead (pin + 27) == HIGH ? "High" : "Low ") ;
  206. printf ("|\n") ;
  207. }
  208. printf ("+-----+------+-------+ +-----+------+-------+\n") ;
  209. }
  210. /*
  211. * abReadall:
  212. * Read all the pins on the model A or B.
  213. *********************************************************************************
  214. */
  215. void abReadall (int model, int rev)
  216. {
  217. int pin ;
  218. char *type ;
  219. if (model == PI_MODEL_A)
  220. type = " A" ;
  221. else
  222. if (rev == PI_VERSION_2)
  223. type = "B2" ;
  224. else
  225. type = "B1" ;
  226. printf (" +-----+-----+---------+------+---+-Model %s-+---+------+---------+-----+-----+\n", type) ;
  227. printf (" | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |\n") ;
  228. printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ;
  229. for (pin = 1 ; pin <= 26 ; pin += 2)
  230. readallPhys (pin) ;
  231. if (rev == PI_VERSION_2) // B version 2
  232. {
  233. printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ;
  234. for (pin = 51 ; pin <= 54 ; pin += 2)
  235. readallPhys (pin) ;
  236. }
  237. printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ;
  238. printf (" | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |\n") ;
  239. printf (" +-----+-----+---------+------+---+-Model %s-+---+------+---------+-----+-----+\n", type) ;
  240. }
  241. /*
  242. * piPlusReadall:
  243. * Read all the pins on the model A+ or the B+ or actually, all 40-pin Pi's
  244. *********************************************************************************
  245. */
  246. static void plus2header (int model)
  247. {
  248. /**/ if (model == PI_MODEL_AP)
  249. printf (" +-----+-----+---------+------+---+---Pi A+--+---+------+---------+-----+-----+\n") ;
  250. else if (model == PI_MODEL_BP)
  251. printf (" +-----+-----+---------+------+---+---Pi B+--+---+------+---------+-----+-----+\n") ;
  252. else if (model == PI_MODEL_ZERO)
  253. printf (" +-----+-----+---------+------+---+-Pi Zero--+---+------+---------+-----+-----+\n") ;
  254. else if (model == PI_MODEL_ZERO_W)
  255. printf (" +-----+-----+---------+------+---+-Pi ZeroW-+---+------+---------+-----+-----+\n") ;
  256. else if (model == PI_MODEL_2)
  257. printf (" +-----+-----+---------+------+---+---Pi 2---+---+------+---------+-----+-----+\n") ;
  258. else if (model == PI_MODEL_3B)
  259. printf (" +-----+-----+---------+------+---+---Pi 3B--+---+------+---------+-----+-----+\n") ;
  260. else if (model == PI_MODEL_3BP)
  261. printf (" +-----+-----+---------+------+---+---Pi 3B+-+---+------+---------+-----+-----+\n") ;
  262. else if (model == PI_MODEL_3AP)
  263. printf (" +-----+-----+---------+------+---+---Pi 3A+-+---+------+---------+-----+-----+\n") ;
  264. else if (model == PI_MODEL_4B)
  265. printf (" +-----+-----+---------+------+---+---Pi 4B--+---+------+---------+-----+-----+\n") ;
  266. else if (model == PI_MODEL_400)
  267. printf (" +-----+-----+---------+------+---+---Pi 400-+---+------+---------+-----+-----+\n") ;
  268. else
  269. printf (" +-----+-----+---------+------+---+---Pi ?---+---+------+---------+-----+-----+\n") ;
  270. }
  271. static void piPlusReadall (int model)
  272. {
  273. int pin ;
  274. plus2header (model) ;
  275. printf (" | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |\n") ;
  276. printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ;
  277. for (pin = 1 ; pin <= 40 ; pin += 2)
  278. readallPhys (pin) ;
  279. printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ;
  280. printf (" | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |\n") ;
  281. plus2header (model) ;
  282. }
  283. /*
  284. * doReadall:
  285. * Generic read all pins called from main program. Works out the Pi type
  286. * and calls the appropriate function.
  287. *********************************************************************************
  288. */
  289. void doReadall (void)
  290. {
  291. int model, rev, mem, maker, overVolted ;
  292. if (wiringPiNodes != NULL) // External readall
  293. {
  294. doReadallExternal () ;
  295. return ;
  296. }
  297. piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
  298. /**/ if ((model == PI_MODEL_A) || (model == PI_MODEL_B))
  299. abReadall (model, rev) ;
  300. else if ((model == PI_MODEL_BP) || (model == PI_MODEL_AP) ||
  301. (model == PI_MODEL_2) ||
  302. (model == PI_MODEL_3AP) ||
  303. (model == PI_MODEL_3B) || (model == PI_MODEL_3BP) ||
  304. (model == PI_MODEL_4B) || (model == PI_MODEL_400) ||
  305. (model == PI_MODEL_ZERO) || (model == PI_MODEL_ZERO_W) || (model == PI_MODEL_CM4))
  306. piPlusReadall (model) ;
  307. else if ((model == PI_MODEL_CM) || (model == PI_MODEL_CM3) || (model == PI_MODEL_CM3P) )
  308. allReadall () ;
  309. else
  310. printf ("Oops - unable to determine board type... model: %d\n", model) ;
  311. }
  312. /*
  313. * doAllReadall:
  314. * Force reading of all pins regardless of Pi model
  315. *********************************************************************************
  316. */
  317. void doAllReadall (void)
  318. {
  319. allReadall () ;
  320. }
  321. /*
  322. * doQmode:
  323. * Query mode on a pin
  324. *********************************************************************************
  325. */
  326. void doQmode (int argc, char *argv [])
  327. {
  328. int pin ;
  329. if (argc != 3)
  330. {
  331. fprintf (stderr, "Usage: %s qmode pin\n", argv [0]) ;
  332. exit (EXIT_FAILURE) ;
  333. }
  334. pin = atoi (argv [2]) ;
  335. printf ("%s\n", alts [getAlt (pin)]) ;
  336. }