Você não pode selecionar mais de 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.
 
 
 
 
 

533 linhas
15 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://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", "ALT0", "ALT1", "ALT2", "ALT3", "ALT4", "ALT5"
  68. } ;
  69. static const char *pupd [] =
  70. {
  71. "DSBLD", "P/U", "P/D"
  72. };
  73. static int physToWpi [64] =
  74. {
  75. -1, // 0
  76. -1, -1, // 1, 2
  77. 8, 15,
  78. 9, 29,
  79. 7, -1,
  80. -1, 16,
  81. 0, 1,
  82. -1, -1,
  83. 3, -1,
  84. -1, 5,
  85. 12, 4,
  86. 13, 6,
  87. 14, 10,
  88. 2, 11, // 25, 26
  89. -1, -1, // Actually I2C, but not used
  90. 21, 30,
  91. 22, 26,
  92. 23, 31,
  93. 24, 27,
  94. 25, 28,
  95. -1, -1,
  96. -1, -1,
  97. -1, -1,
  98. -1, -1,
  99. -1, -1,
  100. -1, -1,
  101. 17, 18,
  102. 19, 20,
  103. -1, -1, -1, -1, -1, -1, -1, -1, -1
  104. } ;
  105. /*----------------------------------------------------------------------------*/
  106. static int physToWpi_Edge [64] =
  107. {
  108. -1, // 0
  109. -1, -1, // 1, 2
  110. -1, -1, // -1, 13,
  111. -1, -1, // -1, 12,
  112. -1, -1,
  113. -1, 9,
  114. -1, 8,
  115. -1, -1,
  116. -1, -1,
  117. -1, 0,
  118. -1, 11,
  119. -1, 7,
  120. -1, 2,
  121. 17, 3,
  122. -1, -1,
  123. -1, 6, // 10, 6,
  124. -1, 5, // 14, 5,
  125. -1, 4,
  126. -1, 1,
  127. -1, -1,
  128. -1, -1,
  129. -1, -1,
  130. -1, -1,
  131. -1, -1,
  132. -1, -1,
  133. -1, -1,
  134. -1, -1,
  135. -1, -1,
  136. -1, -1, -1, -1, -1, -1, -1, -1, -1
  137. } ;
  138. /*------------------------------------------------------------------------------------------*/
  139. static const char *physNamesKhadasVim1[64] = {
  140. NULL,
  141. " 5V","GND ",
  142. " 5V","PIN.DV25",
  143. " HUB_DM1","PIN.DV24",
  144. " HUB_DP1","GND ",
  145. " GND","PIN.DV27",
  146. " 5V","PIN.DV26",
  147. " HUB_DM2","3.3V ",
  148. " HUB_DP2","GND ",
  149. " GND","PIN.H7 ",
  150. " ADC.CH0","PIN.H6 ",
  151. " GND","PIN.H9 ",
  152. " ADC.CH2","PIN.H8 ",
  153. " SPDIF","PIN.AO6 ",
  154. " GND","GND ",
  155. " PIN.AO5","PIN.AO3 ",
  156. " PIN.AO4","RTC_CLK ",
  157. " GND","PIN.H5 ",
  158. " PIN.AO1","PWR_EN ",
  159. " PIN.AO2","PWM_F ",
  160. " 3.3V","GND ",
  161. NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
  162. NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
  163. NULL,NULL,NULL,
  164. };
  165. static const char *physNamesKhadasVim2[64] = {
  166. NULL,
  167. " 5V","GND ",
  168. " 5v","PIN.DV25",
  169. " USB_DM","PIN.DV24",
  170. " USB_DP","GND ",
  171. " GND","PIN.DV27",
  172. "PIN.DV21","PIN.DV26",
  173. "PIN.DV22","3.3V ",
  174. "PIN.DV23","GND ",
  175. " GND","PIN.H7 ",
  176. " ADC0","PIN.H6 ",
  177. " 1.8V","PIN.H9 ",
  178. " ADC1","PIN.H8 ",
  179. " SPDIF","PIN.AO6 ",
  180. " GND","GND ",
  181. " PIN.AO5","PIN.DV29",
  182. " PIN.AO4","RTC_CLK ",
  183. " GND","PIN.H5 ",
  184. " PIN.AO1","EXP_INT ",
  185. " PIN.AO0","PIN.DV13",
  186. " 3.3v","GND ",
  187. //Not used
  188. NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
  189. NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
  190. NULL,NULL,NULL,
  191. };
  192. static const char *physNamesKhadasVim3[64] = {
  193. NULL,
  194. " 5V","GND ",
  195. " 5V","PIN.A15 ",
  196. " USB_DM","PIN.A14 ",
  197. " USB_DP","GND ",
  198. " GND","PIN.AO2 ",
  199. " MCU3V3","PIN.AO3 ",
  200. " MCUNRST","3V3 ",
  201. " MCUSWIM","GND ",
  202. " GND","PIN.A1 ",
  203. " ADC0","PIN.A0 ",
  204. " 1V8","PIN.A3 ",
  205. " ADC1","PIN.A2 ",
  206. " SPDIF","PIN.A4 ",
  207. " GND3","GND ",
  208. " PIN.H6","PWM-F ",
  209. " PIN.H7","RTC ",
  210. " GND","PIN.H4 ",
  211. " PIN.AO1","MCU-FA1 ",
  212. " PIN.AO0","PIN.Z15 ",
  213. " 3V3","GND ",
  214. //Not used
  215. NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
  216. NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
  217. NULL,NULL,NULL,
  218. };
  219. /*----------------------------------------------------------------------------*/
  220. static const char *physNamesKhadasEdge [64] =
  221. {
  222. NULL,
  223. " 5V", "GND(0V) ",
  224. " 5V", "SPI3_RXD/I2C0_SCK/GPIO1_C0",
  225. " HOST1_DM", "SPI3_TXD/I2C0_SDA/GPIO1_B7",
  226. " HOST1_DP", "GND(0V) ",
  227. " GND(0V)", "I2C2_SCL/GPIO2_A1 ",
  228. " MCU_TX", "I2C2_SDA/GPIO2_A0 ",
  229. " MCU_NRST", "3.3V ",
  230. " MCU_SWIM", "GND(0V) ",
  231. " GND(0V)", "I2S0_SCLK/GPIO3_D0 ",
  232. " ADC_IN2", "I2S_CLK/GPIO4_A0 ",
  233. " 1.8V", "I2S0_SDO0/GPIO3_D7 ",
  234. " ADC_IN3", "2S0_LRCK_TX/GPIO3_D2 ",
  235. " SPDIF/GPIO3_C0", "I2S0_SDI0/GPIO3_D3 ",
  236. " GND(0V", "GND(0V) ",
  237. " SPI3_CS/GPIO1_C2", "I2S0_SDI3SDO1/GPIO3_D6 ",
  238. "SPI3_CLK/GPIO1_C1", "2S0_SDI2SDO2/GPIO3_D5 ",
  239. " GND(0V)", "I2S0_SDI1SDO3/GPIO3_D4 ",
  240. " UART_RX", "I2S0_LRCK_RX/GPIO3_D1 ",
  241. " UART_TX", "MCU_PA1 ",
  242. " 3.3V", "GND(0V) ",
  243. NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
  244. NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
  245. NULL,NULL,NULL,
  246. } ;
  247. static void readallPhysKhadas (int model, int UNU rev, int physPin, const char *physNames[])
  248. {
  249. int pin;
  250. //GPIO, wPi pin number
  251. if ((physPinToGpio (physPin) == -1) && (physToWpi [physPin] == -1))
  252. printf (" | | ") ;
  253. else if (physPinToGpio (physPin) != -1)
  254. printf (" | %3d | %3d", physPinToGpio (physPin), physToWpi [physPin]);
  255. else
  256. printf (" | | %3d", physToWpi [physPin]);
  257. // GPIO pin name
  258. printf (" | %s", physNames [physPin]);
  259. // GPIO pin mode, value, drive strength, pupd
  260. if ((physToWpi [physPin] == -1) || (physPinToGpio (physPin) == -1)){
  261. printf (" | | | | ") ;
  262. }
  263. else {
  264. if (wpMode == MODE_GPIO)
  265. pin = physPinToGpio (physPin);
  266. else if(wpMode == MODE_PHYS)
  267. pin = physPin;
  268. else{
  269. pin = physToWpi [physPin];
  270. }
  271. printf (" | %4s", alts [getAlt (pin)]);
  272. printf (" | %d", digitalRead (pin));
  273. switch(model) {
  274. case MODEL_KHADAS_VIM1:
  275. case MODEL_KHADAS_VIM2:
  276. case MODEL_KHADAS_VIM3:
  277. printf (" | | %5s", pupd[getPUPD(pin)]);
  278. break;
  279. default:
  280. break;
  281. }
  282. }
  283. // Physical pin number
  284. printf (" | %2d", (physPin+1)/2);
  285. printf (" || %-2d", (physPin+41)/2);
  286. ++physPin;
  287. // GPIO pin mode, value, drive strength, pupd
  288. if ((physToWpi [physPin] == -1) || (physPinToGpio (physPin) == -1))
  289. printf (" | | | | ");
  290. else {
  291. if(wpMode == MODE_GPIO)
  292. pin = physPinToGpio (physPin);
  293. else if(wpMode == MODE_PHYS)
  294. pin = physPin;
  295. else
  296. pin = physToWpi [physPin];
  297. switch(model){
  298. case MODEL_KHADAS_VIM1:
  299. case MODEL_KHADAS_VIM2:
  300. case MODEL_KHADAS_VIM3:
  301. printf (" | %-5s | ", pupd[getPUPD(pin)]);
  302. break;
  303. default:
  304. break;
  305. }
  306. printf (" | %d", digitalRead (pin));
  307. printf (" | %-4s", alts [getAlt (pin)]);
  308. }
  309. // GPIO pin name
  310. printf (" | %-6s", physNames [physPin]);
  311. //GPIO, wPi pin number
  312. if ((physPinToGpio (physPin) == -1) && (physToWpi [physPin] == -1))
  313. printf (" | | ") ;
  314. else if(physPinToGpio (physPin) != -1)
  315. printf (" | %-3d | %-3d", physToWpi [physPin], physPinToGpio (physPin));
  316. else
  317. printf (" | %-3d | ", physToWpi [physPin]);
  318. printf (" |\n") ;
  319. }
  320. static void readallPhysKhadas_edge (int model, int UNU rev, int physPin, const char *physNames[])
  321. {
  322. int pin;
  323. //GPIO, wPi pin number
  324. if ((physPinToGpio (physPin) == -1) && (physToWpi_Edge [physPin] == -1))
  325. printf (" | | ") ;
  326. else if (physPinToGpio (physPin) != -1)
  327. printf (" | %3d | %3d", physPinToGpio (physPin), physToWpi_Edge [physPin]);
  328. else
  329. printf (" | | %3d", physToWpi_Edge [physPin]);
  330. // GPIO pin name
  331. printf (" | %s", physNames [physPin]);
  332. // GPIO pin mode, value, drive strength, pupd
  333. if ((physToWpi_Edge [physPin] == -1) || (physPinToGpio (physPin) == -1)){
  334. printf (" | | | | ") ;
  335. }
  336. else {
  337. if (wpMode == MODE_GPIO)
  338. pin = physPinToGpio (physPin);
  339. else if(wpMode == MODE_PHYS)
  340. pin = physPin;
  341. else{
  342. pin = physToWpi_Edge [physPin];
  343. }
  344. printf (" | %4s", alts [getAlt (pin)]);
  345. printf (" | %d", digitalRead (pin));
  346. switch(model) {
  347. case MODEL_KHADAS_VIM1:
  348. case MODEL_KHADAS_VIM2:
  349. case MODEL_KHADAS_VIM3:
  350. printf (" | | %5s", pupd[getPUPD(pin)]);
  351. break;
  352. case MODEL_KHADAS_EDGE:
  353. printf (" | | ");
  354. break;
  355. default:
  356. break;
  357. }
  358. }
  359. // Physical pin number
  360. printf (" | %2d", (physPin+1)/2);
  361. printf (" || %-2d", (physPin+41)/2);
  362. ++physPin;
  363. // GPIO pin mode, value, drive strength, pupd
  364. if ((physToWpi_Edge [physPin] == -1) || (physPinToGpio (physPin) == -1))
  365. printf (" | | | | ");
  366. else {
  367. if(wpMode == MODE_GPIO)
  368. pin = physPinToGpio (physPin);
  369. else if(wpMode == MODE_PHYS)
  370. pin = physPin;
  371. else
  372. pin = physToWpi_Edge [physPin];
  373. switch(model){
  374. case MODEL_KHADAS_VIM1:
  375. case MODEL_KHADAS_VIM2:
  376. case MODEL_KHADAS_VIM3:
  377. printf (" | %-5s | ", pupd[getPUPD(pin)]);
  378. break;
  379. case MODEL_KHADAS_EDGE:
  380. printf (" | | ");
  381. break;
  382. default:
  383. break;
  384. }
  385. printf (" | %d", digitalRead (pin));
  386. printf (" | %-4s", alts [getAlt (pin)]);
  387. }
  388. // GPIO pin name
  389. printf (" | %-6s", physNames [physPin]);
  390. //GPIO, wPi pin number
  391. if ((physPinToGpio (physPin) == -1) && (physToWpi_Edge [physPin] == -1))
  392. printf (" | | ") ;
  393. else if(physPinToGpio (physPin) != -1)
  394. printf (" | %-3d | %-3d", physToWpi_Edge [physPin], physPinToGpio (physPin));
  395. else
  396. printf (" | %-3d | ", physToWpi_Edge [physPin]);
  397. printf (" |\n") ;
  398. }
  399. /*--------------------------------------------------------------------------------------*/
  400. void ReadallKhadas(int model, int rev, const char *physNames[])
  401. {
  402. int pin;
  403. if(MODEL_KHADAS_EDGE == model){
  404. printf (" | GPIO | wPi | Name | Mode | V | DS | PU/PD | Physical | PU/PD | DS | V | Mode | Name | wPi | GPIO |\n") ;
  405. printf (" +------+-----+-------------------+------+---+----+-------+----++----+-------+----+---+------+----------------------------+-----+------+\n") ;
  406. for (pin = 1 ; pin <= 40 ; pin += 2)
  407. readallPhysKhadas_edge (model, rev, pin, physNames) ;
  408. printf (" +------+-----+-------------------+------+---+----+-------+----++----+-------+----+---+------+----------------------------+-----+------+\n") ;
  409. }
  410. else{
  411. printf (" | GPIO | wPi | Name | Mode | V | DS | PU/PD | Physical | PU/PD | DS | V | Mode | Name | wPi | GPIO |\n");
  412. printf (" +------+-----+----------+------+---+----+-------+----++----+-------+----+---+------+----------+-----+------+\n");
  413. for (pin = 1 ; pin <= 40 ; pin += 2)
  414. readallPhysKhadas (model, rev, pin, physNames);
  415. printf (" +------+-----+----------+------+---+----+-------+----++----+-------+----+---+------+----------+-----+------+\n");
  416. }
  417. }
  418. /*--------------------------------------------------------------------------------------*/
  419. /* doReadall */
  420. /* Read all the GPIO pins */
  421. /* We also want to use this to read the state of pins on an externally */
  422. /* connected device, so we need to do some fiddling with the internal */
  423. /* wiringPi node structures - since the gpio command can only use */
  424. /* one external device at a time, we'll use that to our advantage... */
  425. /*--------------------------------------------------------------------------------------*/
  426. void doReadall(void)
  427. {
  428. int model, rev, mem, maker, overVolted;
  429. const char **physNames;
  430. // External readall
  431. if (wiringPiNodes != NULL) {
  432. doReadallExternal ();
  433. return;
  434. }
  435. piBoardId(&model, &rev, &mem, &maker, &overVolted);
  436. switch(model){
  437. case MODEL_KHADAS_VIM1:
  438. printf (" +------+-----+----------+------+---+----+---- Model KDADAS-VIM1---+----+---+------+----------+-----+------+\n") ;
  439. physNames = physNamesKhadasVim1;
  440. break;
  441. case MODEL_KHADAS_VIM2:
  442. printf (" +------+-----+----------+------+---+----+---- Model KDADAS-VIM2---+----+---+------+----------+-----+------+\n") ;
  443. physNames = physNamesKhadasVim2;
  444. break;
  445. case MODEL_KHADAS_VIM3:
  446. printf (" +------+-----+----------+------+---+----+---- Model KDADAS-VIM3---+----+---+------+----------+-----+------+\n") ;
  447. physNames = physNamesKhadasVim3;
  448. break;
  449. case MODEL_KHADAS_EDGE:
  450. printf (" +------+-----+-------------------+------+---+----+--- Model KHADAS-EDGE ---+----+---+------+----------------------------+-----+------+\n");
  451. physNames = physNamesKhadasEdge;
  452. break;
  453. default:
  454. printf ("Oops - unable to determine board type... model: %d\n", model);
  455. return;
  456. }
  457. ReadallKhadas(model, rev, physNames);
  458. }
  459. /*--------------------------------------------------------------------------------------*/
  460. /* doAllReadall */
  461. /* Force reading of all pins regardless of Pi model */
  462. /*--------------------------------------------------------------------------------------*/
  463. void doAllReadall(void)
  464. {
  465. doReadall();
  466. }
  467. /*--------------------------------------------------------------------------------------*/
  468. void doQmode(int argc, char *argv[])
  469. {
  470. int pin;
  471. if(argc != 3){
  472. fprintf (stderr, "Usage: %s qmode pin\n", argv [0]) ;
  473. exit(EXIT_FAILURE);
  474. }
  475. pin = atoi(argv[2]);
  476. printf("%s\n", alts[getAlt(pin)]);
  477. }
  478. /*--------------------------------------------------------------------------------------*/
  479. /*--------------------------------------------------------------------------------------*/