Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

406 linhas
11 KiB

  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 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_ZERO_2W)
  257. printf (" +-----+-----+---------+------+---+Pi Zero 2W+---+------+---------+-----+-----+\n") ;
  258. else if (model == PI_MODEL_2)
  259. printf (" +-----+-----+---------+------+---+---Pi 2---+---+------+---------+-----+-----+\n") ;
  260. else if (model == PI_MODEL_3B)
  261. printf (" +-----+-----+---------+------+---+---Pi 3B--+---+------+---------+-----+-----+\n") ;
  262. else if (model == PI_MODEL_3BP)
  263. printf (" +-----+-----+---------+------+---+---Pi 3B+-+---+------+---------+-----+-----+\n") ;
  264. else if (model == PI_MODEL_3AP)
  265. printf (" +-----+-----+---------+------+---+---Pi 3A+-+---+------+---------+-----+-----+\n") ;
  266. else if (model == PI_MODEL_4B)
  267. printf (" +-----+-----+---------+------+---+---Pi 4B--+---+------+---------+-----+-----+\n") ;
  268. else if (model == PI_MODEL_400)
  269. printf (" +-----+-----+---------+------+---+---Pi 400-+---+------+---------+-----+-----+\n") ;
  270. else
  271. printf (" +-----+-----+---------+------+---+---Pi ?---+---+------+---------+-----+-----+\n") ;
  272. }
  273. static void piPlusReadall (int model)
  274. {
  275. int pin ;
  276. plus2header (model) ;
  277. printf (" | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |\n") ;
  278. printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ;
  279. for (pin = 1 ; pin <= 40 ; pin += 2)
  280. readallPhys (pin) ;
  281. printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ;
  282. printf (" | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |\n") ;
  283. plus2header (model) ;
  284. }
  285. /*
  286. * doReadall:
  287. * Generic read all pins called from main program. Works out the Pi type
  288. * and calls the appropriate function.
  289. *********************************************************************************
  290. */
  291. void doReadall (void)
  292. {
  293. int model, rev, mem, maker, overVolted ;
  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_4B) || (model == PI_MODEL_400) || (model == PI_MODEL_CM4) ||
  307. (model == PI_MODEL_ZERO) || (model == PI_MODEL_ZERO_W) || (model == PI_MODEL_ZERO_2W))
  308. piPlusReadall (model) ;
  309. else if ((model == PI_MODEL_CM) || (model == PI_MODEL_CM3) || (model == PI_MODEL_CM3P) )
  310. allReadall () ;
  311. else
  312. printf ("Oops - unable to determine board type... model: %d\n", model) ;
  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. }