readall.c 11 KB

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