Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

readall.c 12 KiB

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