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.
 
 
 
 
 

338 lines
8.8 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. void cmReadall (void)
  186. {
  187. int pin ;
  188. printf ("+-----+------+-------+ +-----+------+-------+\n") ;
  189. printf ("| Pin | Mode | Value | | Pin | Mode | Value |\n") ;
  190. printf ("+-----+------+-------+ +-----+------+-------+\n") ;
  191. for (pin = 0 ; pin < 27 ; ++pin)
  192. {
  193. printf ("| %3d ", pin) ;
  194. printf ("| %-4s ", alts [getAlt (pin)]) ;
  195. printf ("| %s ", digitalRead (pin) == HIGH ? "High" : "Low ") ;
  196. printf ("| ") ;
  197. printf ("| %3d ", pin + 27) ;
  198. printf ("| %-4s ", alts [getAlt (pin + 27)]) ;
  199. printf ("| %s ", digitalRead (pin + 27) == HIGH ? "High" : "Low ") ;
  200. printf ("|\n") ;
  201. }
  202. printf ("+-----+------+-------+ +-----+------+-------+\n") ;
  203. }
  204. /*
  205. * abReadall:
  206. * Read all the pins on the model A or B.
  207. *********************************************************************************
  208. */
  209. void abReadall (int model, int rev)
  210. {
  211. int pin ;
  212. char *type ;
  213. if (model == PI_MODEL_A)
  214. type = " A" ;
  215. else
  216. if (rev == PI_VERSION_2)
  217. type = "B2" ;
  218. else
  219. type = "B1" ;
  220. printf (" +-----+-----+---------+------+---+-Model %s-+---+------+---------+-----+-----+\n", type) ;
  221. printf (" | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |\n") ;
  222. printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ;
  223. for (pin = 1 ; pin <= 26 ; pin += 2)
  224. readallPhys (pin) ;
  225. if (rev == PI_VERSION_2) // B version 2
  226. {
  227. printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ;
  228. for (pin = 51 ; pin <= 54 ; pin += 2)
  229. readallPhys (pin) ;
  230. }
  231. printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ;
  232. printf (" | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |\n") ;
  233. printf (" +-----+-----+---------+------+---+-Model %s-+---+------+---------+-----+-----+\n", type) ;
  234. }
  235. /*
  236. * piPlusReadall:
  237. * Read all the pins on the model A+ or the B+
  238. *********************************************************************************
  239. */
  240. static void plus2header (int model)
  241. {
  242. /**/ if (model == PI_MODEL_AP)
  243. printf (" +-----+-----+---------+------+---+--A Plus--+---+------+---------+-----+-----+\n") ;
  244. else if (model == PI_MODEL_BP)
  245. printf (" +-----+-----+---------+------+---+--B Plus--+---+------+---------+-----+-----+\n") ;
  246. else if (model == PI_MODEL_ZERO)
  247. printf (" +-----+-----+---------+------+---+-Pi Zero--+---+------+---------+-----+-----+\n") ;
  248. else
  249. printf (" +-----+-----+---------+------+---+---Pi 2---+---+------+---------+-----+-----+\n") ;
  250. }
  251. void piPlusReadall (int model)
  252. {
  253. int pin ;
  254. plus2header (model) ;
  255. printf (" | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |\n") ;
  256. printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ;
  257. for (pin = 1 ; pin <= 40 ; pin += 2)
  258. readallPhys (pin) ;
  259. printf (" +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+\n") ;
  260. printf (" | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |\n") ;
  261. plus2header (model) ;
  262. }
  263. void doReadall (void)
  264. {
  265. int model, rev, mem, maker, overVolted ;
  266. if (wiringPiNodes != NULL) // External readall
  267. {
  268. doReadallExternal () ;
  269. return ;
  270. }
  271. piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
  272. /**/ if ((model == PI_MODEL_A) || (model == PI_MODEL_B))
  273. abReadall (model, rev) ;
  274. else if ((model == PI_MODEL_BP) || (model == PI_MODEL_AP) || (model == PI_MODEL_2) || (model == PI_MODEL_ZERO))
  275. piPlusReadall (model) ;
  276. else if (model == PI_MODEL_CM)
  277. cmReadall () ;
  278. else
  279. printf ("Oops - unable to determine board type... model: %d\n", model) ;
  280. }