Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 

422 рядки
12 KiB

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