You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

367 lines
9.7 KiB

  1. /*
  2. * readall.c:
  3. * The readall functions - getting a bit big, so split them out.
  4. * Copyright (c) 2012-2015 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+
  244. *********************************************************************************
  245. */
  246. static void plus2header (int model)
  247. {
  248. /**/ if (model == PI_MODEL_AP)
  249. printf (" +-----+-----+---------+------+---+--A Plus--+---+------+---------+-----+-----+\n") ;
  250. else if (model == PI_MODEL_BP)
  251. printf (" +-----+-----+---------+------+---+--B Plus--+---+------+---------+-----+-----+\n") ;
  252. else if (model == PI_MODEL_ZERO)
  253. printf (" +-----+-----+---------+------+---+-Pi Zero--+---+------+---------+-----+-----+\n") ;
  254. else if (model == PI_MODEL_2)
  255. printf (" +-----+-----+---------+------+---+---Pi 2---+---+------+---------+-----+-----+\n") ;
  256. else if (model == PI_MODEL_3)
  257. printf (" +-----+-----+---------+------+---+---Pi 3---+---+------+---------+-----+-----+\n") ;
  258. else
  259. printf (" +-----+-----+---------+------+---+---Pi ?---+---+------+---------+-----+-----+\n") ;
  260. }
  261. static void piPlusReadall (int model)
  262. {
  263. int pin ;
  264. plus2header (model) ;
  265. printf (" | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |\n") ;
  266. printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ;
  267. for (pin = 1 ; pin <= 40 ; pin += 2)
  268. readallPhys (pin) ;
  269. printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ;
  270. printf (" | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |\n") ;
  271. plus2header (model) ;
  272. }
  273. /*
  274. * doReadall:
  275. * Generic read all pins called from main program. Works out the Pi type
  276. * and calls the appropriate function.
  277. *********************************************************************************
  278. */
  279. void doReadall (void)
  280. {
  281. int model, rev, mem, maker, overVolted ;
  282. if (wiringPiNodes != NULL) // External readall
  283. {
  284. doReadallExternal () ;
  285. return ;
  286. }
  287. piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
  288. /**/ if ((model == PI_MODEL_A) || (model == PI_MODEL_B))
  289. abReadall (model, rev) ;
  290. else if ((model == PI_MODEL_BP) || (model == PI_MODEL_AP) || (model == PI_MODEL_2) || (model == PI_MODEL_3) || (model == PI_MODEL_ZERO))
  291. piPlusReadall (model) ;
  292. else if (model == PI_MODEL_CM)
  293. allReadall () ;
  294. else
  295. printf ("Oops - unable to determine board type... model: %d\n", model) ;
  296. }
  297. /*
  298. * doAllReadall:
  299. * Force reading of all pins regardless of Pi model
  300. *********************************************************************************
  301. */
  302. void doAllReadall (void)
  303. {
  304. allReadall () ;
  305. }