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.
 
 
 
 
 

2508 line
71 KiB

  1. /*
  2. * wiringPi:
  3. * Arduino look-a-like Wiring library for the Raspberry Pi
  4. * Copyright (c) 2012-2017 Gordon Henderson
  5. * Additional code for pwmSetClock by Chris Hall <chris@kchall.plus.com>
  6. *
  7. * Thanks to code samples from Gert Jan van Loo and the
  8. * BCM2835 ARM Peripherals manual, however it's missing
  9. * the clock section /grr/mutter/
  10. ***********************************************************************
  11. * This file is part of wiringPi:
  12. * https://github.com/WiringPi/WiringPi
  13. *
  14. * wiringPi is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Lesser General Public License as
  16. * published by the Free Software Foundation, either version 3 of the
  17. * License, or (at your option) any later version.
  18. *
  19. * wiringPi is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Lesser General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Lesser General Public
  25. * License along with wiringPi.
  26. * If not, see <http://www.gnu.org/licenses/>.
  27. ***********************************************************************
  28. */
  29. // Revisions:
  30. // 19 Jul 2012:
  31. // Moved to the LGPL
  32. // Added an abstraction layer to the main routines to save a tiny
  33. // bit of run-time and make the clode a little cleaner (if a little
  34. // larger)
  35. // Added waitForInterrupt code
  36. // Added piHiPri code
  37. //
  38. // 9 Jul 2012:
  39. // Added in support to use the /sys/class/gpio interface.
  40. // 2 Jul 2012:
  41. // Fixed a few more bugs to do with range-checking when in GPIO mode.
  42. // 11 Jun 2012:
  43. // Fixed some typos.
  44. // Added c++ support for the .h file
  45. // Added a new function to allow for using my "pin" numbers, or native
  46. // GPIO pin numbers.
  47. // Removed my busy-loop delay and replaced it with a call to delayMicroseconds
  48. //
  49. // 02 May 2012:
  50. // Added in the 2 UART pins
  51. // Change maxPins to numPins to more accurately reflect purpose
  52. #include <stdio.h>
  53. #include <stdarg.h>
  54. #include <stdint.h>
  55. #include <stdlib.h>
  56. #include <ctype.h>
  57. #include <poll.h>
  58. #include <unistd.h>
  59. #include <errno.h>
  60. #include <string.h>
  61. #include <time.h>
  62. #include <fcntl.h>
  63. #include <pthread.h>
  64. #include <sys/time.h>
  65. #include <sys/mman.h>
  66. #include <sys/stat.h>
  67. #include <sys/wait.h>
  68. #include <sys/ioctl.h>
  69. #include <asm/ioctl.h>
  70. #include "softPwm.h"
  71. #include "softTone.h"
  72. #include "wiringPi.h"
  73. #include "../version.h"
  74. // Environment Variables
  75. #define ENV_DEBUG "WIRINGPI_DEBUG"
  76. #define ENV_CODES "WIRINGPI_CODES"
  77. #define ENV_GPIOMEM "WIRINGPI_GPIOMEM"
  78. // Extend wiringPi with other pin-based devices and keep track of
  79. // them in this structure
  80. struct wiringPiNodeStruct *wiringPiNodes = NULL ;
  81. // BCM Magic
  82. #define BCM_PASSWORD 0x5A000000
  83. // The BCM2835 has 54 GPIO pins.
  84. // BCM2835 data sheet, Page 90 onwards.
  85. // There are 6 control registers, each control the functions of a block
  86. // of 10 pins.
  87. // Each control register has 10 sets of 3 bits per GPIO pin - the ALT values
  88. //
  89. // 000 = GPIO Pin X is an input
  90. // 001 = GPIO Pin X is an output
  91. // 100 = GPIO Pin X takes alternate function 0
  92. // 101 = GPIO Pin X takes alternate function 1
  93. // 110 = GPIO Pin X takes alternate function 2
  94. // 111 = GPIO Pin X takes alternate function 3
  95. // 011 = GPIO Pin X takes alternate function 4
  96. // 010 = GPIO Pin X takes alternate function 5
  97. //
  98. // So the 3 bits for port X are:
  99. // X / 10 + ((X % 10) * 3)
  100. // Port function select bits
  101. #define FSEL_INPT 0b000
  102. #define FSEL_OUTP 0b001
  103. #define FSEL_ALT0 0b100
  104. #define FSEL_ALT1 0b101
  105. #define FSEL_ALT2 0b110
  106. #define FSEL_ALT3 0b111
  107. #define FSEL_ALT4 0b011
  108. #define FSEL_ALT5 0b010
  109. // Access from ARM Running Linux
  110. // Taken from Gert/Doms code. Some of this is not in the manual
  111. // that I can find )-:
  112. //
  113. // Updates in September 2015 - all now static variables (and apologies for the caps)
  114. // due to the Pi v2, v3, etc. and the new /dev/gpiomem interface
  115. static volatile unsigned int GPIO_PADS ;
  116. static volatile unsigned int GPIO_CLOCK_BASE ;
  117. static volatile unsigned int GPIO_BASE ;
  118. static volatile unsigned int GPIO_TIMER ;
  119. static volatile unsigned int GPIO_PWM ;
  120. #define PAGE_SIZE (4*1024)
  121. #define BLOCK_SIZE (4*1024)
  122. static unsigned int usingGpioMem = FALSE ;
  123. static int wiringPiSetuped = FALSE ;
  124. // PWM
  125. // Word offsets into the PWM control region
  126. #define PWM_CONTROL 0
  127. #define PWM_STATUS 1
  128. #define PWM0_RANGE 4
  129. #define PWM0_DATA 5
  130. #define PWM1_RANGE 8
  131. #define PWM1_DATA 9
  132. // Clock regsiter offsets
  133. #define PWMCLK_CNTL 40
  134. #define PWMCLK_DIV 41
  135. #define PWM0_MS_MODE 0x0080 // Run in MS mode
  136. #define PWM0_USEFIFO 0x0020 // Data from FIFO
  137. #define PWM0_REVPOLAR 0x0010 // Reverse polarity
  138. #define PWM0_OFFSTATE 0x0008 // Ouput Off state
  139. #define PWM0_REPEATFF 0x0004 // Repeat last value if FIFO empty
  140. #define PWM0_SERIAL 0x0002 // Run in serial mode
  141. #define PWM0_ENABLE 0x0001 // Channel Enable
  142. #define PWM1_MS_MODE 0x8000 // Run in MS mode
  143. #define PWM1_USEFIFO 0x2000 // Data from FIFO
  144. #define PWM1_REVPOLAR 0x1000 // Reverse polarity
  145. #define PWM1_OFFSTATE 0x0800 // Ouput Off state
  146. #define PWM1_REPEATFF 0x0400 // Repeat last value if FIFO empty
  147. #define PWM1_SERIAL 0x0200 // Run in serial mode
  148. #define PWM1_ENABLE 0x0100 // Channel Enable
  149. // Timer
  150. // Word offsets
  151. #define TIMER_LOAD (0x400 >> 2)
  152. #define TIMER_VALUE (0x404 >> 2)
  153. #define TIMER_CONTROL (0x408 >> 2)
  154. #define TIMER_IRQ_CLR (0x40C >> 2)
  155. #define TIMER_IRQ_RAW (0x410 >> 2)
  156. #define TIMER_IRQ_MASK (0x414 >> 2)
  157. #define TIMER_RELOAD (0x418 >> 2)
  158. #define TIMER_PRE_DIV (0x41C >> 2)
  159. #define TIMER_COUNTER (0x420 >> 2)
  160. // Locals to hold pointers to the hardware
  161. static volatile unsigned int *gpio ;
  162. static volatile unsigned int *pwm ;
  163. static volatile unsigned int *clk ;
  164. static volatile unsigned int *pads ;
  165. static volatile unsigned int *timer ;
  166. static volatile unsigned int *timerIrqRaw ;
  167. // Export variables for the hardware pointers
  168. volatile unsigned int *_wiringPiGpio ;
  169. volatile unsigned int *_wiringPiPwm ;
  170. volatile unsigned int *_wiringPiClk ;
  171. volatile unsigned int *_wiringPiPads ;
  172. volatile unsigned int *_wiringPiTimer ;
  173. volatile unsigned int *_wiringPiTimerIrqRaw ;
  174. // Data for use with the boardId functions.
  175. // The order of entries here to correspond with the PI_MODEL_X
  176. // and PI_VERSION_X defines in wiringPi.h
  177. // Only intended for the gpio command - use at your own risk!
  178. // piGpioBase:
  179. // The base address of the GPIO memory mapped hardware IO
  180. #define GPIO_PERI_BASE_OLD 0x20000000
  181. #define GPIO_PERI_BASE_2835 0x3F000000
  182. #define GPIO_PERI_BASE_2711 0xFE000000
  183. static volatile unsigned int piGpioBase = 0 ;
  184. const char *piModelNames [21] =
  185. {
  186. "Model A", // 0
  187. "Model B", // 1
  188. "Model A+", // 2
  189. "Model B+", // 3
  190. "Pi 2", // 4
  191. "Alpha", // 5
  192. "CM", // 6
  193. "Unknown07", // 07
  194. "Pi 3B", // 08
  195. "Pi Zero", // 09
  196. "CM3", // 10
  197. "Unknown11", // 11
  198. "Pi Zero-W", // 12
  199. "Pi 3B+", // 13
  200. "Pi 3A+", // 14
  201. "Unknown15", // 15
  202. "CM3+", // 16
  203. "Pi 4B", // 17
  204. "Pi Zero2-W", // 18
  205. "Pi 400", // 19
  206. "CM4", // 20
  207. } ;
  208. const char *piRevisionNames [21] =
  209. {
  210. "00",
  211. "01",
  212. "02",
  213. "03",
  214. "04",
  215. "05",
  216. "06",
  217. "07",
  218. "08",
  219. "09",
  220. "10",
  221. "11",
  222. "12",
  223. "13",
  224. "14",
  225. "15",
  226. "16",
  227. "17",
  228. "18",
  229. "19",
  230. "20",
  231. } ;
  232. const char *piMakerNames [16] =
  233. {
  234. "Sony", // 0
  235. "Egoman", // 1
  236. "Embest", // 2
  237. "Unknown", // 3
  238. "Embest", // 4
  239. "Stadium", // 5
  240. "Unknown06", // 6
  241. "Unknown07", // 7
  242. "Unknown08", // 8
  243. "Unknown09", // 9
  244. "Unknown10", // 10
  245. "Unknown11", // 11
  246. "Unknown12", // 12
  247. "Unknown13", // 13
  248. "Unknown14", // 14
  249. "Unknown15", // 15
  250. } ;
  251. const int piMemorySize [8] =
  252. {
  253. 256, // 0
  254. 512, // 1
  255. 1024, // 2
  256. 2048, // 3
  257. 4096, // 4
  258. 8192, // 5
  259. 0, // 6
  260. 0, // 7
  261. } ;
  262. // Time for easy calculations
  263. static uint64_t epochMilli, epochMicro ;
  264. // Misc
  265. static int wiringPiMode = WPI_MODE_UNINITIALISED ;
  266. static volatile int pinPass = -1 ;
  267. static pthread_mutex_t pinMutex ;
  268. // Debugging & Return codes
  269. int wiringPiDebug = FALSE ;
  270. int wiringPiReturnCodes = FALSE ;
  271. // Use /dev/gpiomem ?
  272. int wiringPiTryGpioMem = FALSE ;
  273. // sysFds:
  274. // Map a file descriptor from the /sys/class/gpio/gpioX/value
  275. static int sysFds [64] =
  276. {
  277. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  278. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  279. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  280. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  281. } ;
  282. // ISR Data
  283. static void (*isrFunctions [64])(void) ;
  284. // Doing it the Arduino way with lookup tables...
  285. // Yes, it's probably more innefficient than all the bit-twidling, but it
  286. // does tend to make it all a bit clearer. At least to me!
  287. // pinToGpio:
  288. // Take a Wiring pin (0 through X) and re-map it to the BCM_GPIO pin
  289. // Cope for 3 different board revisions here.
  290. static int *pinToGpio ;
  291. // Revision 1, 1.1:
  292. static int pinToGpioR1 [64] =
  293. {
  294. 17, 18, 21, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7: wpi 0 - 7
  295. 0, 1, // I2C - SDA1, SCL1 wpi 8 - 9
  296. 8, 7, // SPI - CE1, CE0 wpi 10 - 11
  297. 10, 9, 11, // SPI - MOSI, MISO, SCLK wpi 12 - 14
  298. 14, 15, // UART - Tx, Rx wpi 15 - 16
  299. // Padding:
  300. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 31
  301. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
  302. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
  303. } ;
  304. // Revision 2:
  305. static int pinToGpioR2 [64] =
  306. {
  307. 17, 18, 27, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7: wpi 0 - 7
  308. 2, 3, // I2C - SDA0, SCL0 wpi 8 - 9
  309. 8, 7, // SPI - CE1, CE0 wpi 10 - 11
  310. 10, 9, 11, // SPI - MOSI, MISO, SCLK wpi 12 - 14
  311. 14, 15, // UART - Tx, Rx wpi 15 - 16
  312. 28, 29, 30, 31, // Rev 2: New GPIOs 8 though 11 wpi 17 - 20
  313. 5, 6, 13, 19, 26, // B+ wpi 21, 22, 23, 24, 25
  314. 12, 16, 20, 21, // B+ wpi 26, 27, 28, 29
  315. 0, 1, // B+ wpi 30, 31
  316. // Padding:
  317. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
  318. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
  319. } ;
  320. // physToGpio:
  321. // Take a physical pin (1 through 26) and re-map it to the BCM_GPIO pin
  322. // Cope for 2 different board revisions here.
  323. // Also add in the P5 connector, so the P5 pins are 3,4,5,6, so 53,54,55,56
  324. static int *physToGpio ;
  325. static int physToGpioR1 [64] =
  326. {
  327. -1, // 0
  328. -1, -1, // 1, 2
  329. 0, -1,
  330. 1, -1,
  331. 4, 14,
  332. -1, 15,
  333. 17, 18,
  334. 21, -1,
  335. 22, 23,
  336. -1, 24,
  337. 10, -1,
  338. 9, 25,
  339. 11, 8,
  340. -1, 7, // 25, 26
  341. -1, -1, -1, -1, -1, // ... 31
  342. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
  343. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
  344. } ;
  345. static int physToGpioR2 [64] =
  346. {
  347. -1, // 0
  348. -1, -1, // 1, 2
  349. 2, -1,
  350. 3, -1,
  351. 4, 14,
  352. -1, 15,
  353. 17, 18,
  354. 27, -1,
  355. 22, 23,
  356. -1, 24,
  357. 10, -1,
  358. 9, 25,
  359. 11, 8,
  360. -1, 7, // 25, 26
  361. // B+
  362. 0, 1,
  363. 5, -1,
  364. 6, 12,
  365. 13, -1,
  366. 19, 16,
  367. 26, 20,
  368. -1, 21,
  369. // the P5 connector on the Rev 2 boards:
  370. -1, -1,
  371. -1, -1,
  372. -1, -1,
  373. -1, -1,
  374. -1, -1,
  375. 28, 29,
  376. 30, 31,
  377. -1, -1,
  378. -1, -1,
  379. -1, -1,
  380. -1, -1,
  381. } ;
  382. // gpioToGPFSEL:
  383. // Map a BCM_GPIO pin to it's Function Selection
  384. // control port. (GPFSEL 0-5)
  385. // Groups of 10 - 3 bits per Function - 30 bits per port
  386. static uint8_t gpioToGPFSEL [] =
  387. {
  388. 0,0,0,0,0,0,0,0,0,0,
  389. 1,1,1,1,1,1,1,1,1,1,
  390. 2,2,2,2,2,2,2,2,2,2,
  391. 3,3,3,3,3,3,3,3,3,3,
  392. 4,4,4,4,4,4,4,4,4,4,
  393. 5,5,5,5,5,5,5,5,5,5,
  394. } ;
  395. // gpioToShift
  396. // Define the shift up for the 3 bits per pin in each GPFSEL port
  397. static uint8_t gpioToShift [] =
  398. {
  399. 0,3,6,9,12,15,18,21,24,27,
  400. 0,3,6,9,12,15,18,21,24,27,
  401. 0,3,6,9,12,15,18,21,24,27,
  402. 0,3,6,9,12,15,18,21,24,27,
  403. 0,3,6,9,12,15,18,21,24,27,
  404. 0,3,6,9,12,15,18,21,24,27,
  405. } ;
  406. // gpioToGPSET:
  407. // (Word) offset to the GPIO Set registers for each GPIO pin
  408. static uint8_t gpioToGPSET [] =
  409. {
  410. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  411. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  412. } ;
  413. // gpioToGPCLR:
  414. // (Word) offset to the GPIO Clear registers for each GPIO pin
  415. static uint8_t gpioToGPCLR [] =
  416. {
  417. 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
  418. 11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
  419. } ;
  420. // gpioToGPLEV:
  421. // (Word) offset to the GPIO Input level registers for each GPIO pin
  422. static uint8_t gpioToGPLEV [] =
  423. {
  424. 13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
  425. 14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,
  426. } ;
  427. #ifdef notYetReady
  428. // gpioToEDS
  429. // (Word) offset to the Event Detect Status
  430. static uint8_t gpioToEDS [] =
  431. {
  432. 16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,
  433. 17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,
  434. } ;
  435. // gpioToREN
  436. // (Word) offset to the Rising edge ENable register
  437. static uint8_t gpioToREN [] =
  438. {
  439. 19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,
  440. 20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
  441. } ;
  442. // gpioToFEN
  443. // (Word) offset to the Falling edgde ENable register
  444. static uint8_t gpioToFEN [] =
  445. {
  446. 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,
  447. 23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,
  448. } ;
  449. #endif
  450. // GPPUD:
  451. // GPIO Pin pull up/down register
  452. #define GPPUD 37
  453. /* 2711 has a different mechanism for pin pull-up/down/enable */
  454. #define GPPUPPDN0 57 /* Pin pull-up/down for pins 15:0 */
  455. #define GPPUPPDN1 58 /* Pin pull-up/down for pins 31:16 */
  456. #define GPPUPPDN2 59 /* Pin pull-up/down for pins 47:32 */
  457. #define GPPUPPDN3 60 /* Pin pull-up/down for pins 57:48 */
  458. static volatile unsigned int piGpioPupOffset = 0 ;
  459. // gpioToPUDCLK
  460. // (Word) offset to the Pull Up Down Clock regsiter
  461. static uint8_t gpioToPUDCLK [] =
  462. {
  463. 38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,
  464. 39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,
  465. } ;
  466. // gpioToPwmALT
  467. // the ALT value to put a GPIO pin into PWM mode
  468. static uint8_t gpioToPwmALT [] =
  469. {
  470. 0, 0, 0, 0, 0, 0, 0, 0, // 0 -> 7
  471. 0, 0, 0, 0, FSEL_ALT0, FSEL_ALT0, 0, 0, // 8 -> 15
  472. 0, 0, FSEL_ALT5, FSEL_ALT5, 0, 0, 0, 0, // 16 -> 23
  473. 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
  474. 0, 0, 0, 0, 0, 0, 0, 0, // 32 -> 39
  475. FSEL_ALT0, FSEL_ALT0, 0, 0, 0, FSEL_ALT0, 0, 0, // 40 -> 47
  476. 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
  477. 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
  478. } ;
  479. // gpioToPwmPort
  480. // The port value to put a GPIO pin into PWM mode
  481. static uint8_t gpioToPwmPort [] =
  482. {
  483. 0, 0, 0, 0, 0, 0, 0, 0, // 0 -> 7
  484. 0, 0, 0, 0, PWM0_DATA, PWM1_DATA, 0, 0, // 8 -> 15
  485. 0, 0, PWM0_DATA, PWM1_DATA, 0, 0, 0, 0, // 16 -> 23
  486. 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
  487. 0, 0, 0, 0, 0, 0, 0, 0, // 32 -> 39
  488. PWM0_DATA, PWM1_DATA, 0, 0, 0, PWM1_DATA, 0, 0, // 40 -> 47
  489. 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
  490. 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
  491. } ;
  492. // gpioToGpClkALT:
  493. // ALT value to put a GPIO pin into GP Clock mode.
  494. // On the Pi we can really only use BCM_GPIO_4 and BCM_GPIO_21
  495. // for clocks 0 and 1 respectively, however I'll include the full
  496. // list for completeness - maybe one day...
  497. #define GPIO_CLOCK_SOURCE 1
  498. // gpioToGpClkALT0:
  499. static uint8_t gpioToGpClkALT0 [] =
  500. {
  501. 0, 0, 0, 0, FSEL_ALT0, FSEL_ALT0, FSEL_ALT0, 0, // 0 -> 7
  502. 0, 0, 0, 0, 0, 0, 0, 0, // 8 -> 15
  503. 0, 0, 0, 0, FSEL_ALT5, FSEL_ALT5, 0, 0, // 16 -> 23
  504. 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
  505. FSEL_ALT0, 0, FSEL_ALT0, 0, 0, 0, 0, 0, // 32 -> 39
  506. 0, 0, FSEL_ALT0, FSEL_ALT0, FSEL_ALT0, 0, 0, 0, // 40 -> 47
  507. 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
  508. 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
  509. } ;
  510. // gpioToClk:
  511. // (word) Offsets to the clock Control and Divisor register
  512. static uint8_t gpioToClkCon [] =
  513. {
  514. -1, -1, -1, -1, 28, 30, 32, -1, // 0 -> 7
  515. -1, -1, -1, -1, -1, -1, -1, -1, // 8 -> 15
  516. -1, -1, -1, -1, 28, 30, -1, -1, // 16 -> 23
  517. -1, -1, -1, -1, -1, -1, -1, -1, // 24 -> 31
  518. 28, -1, 28, -1, -1, -1, -1, -1, // 32 -> 39
  519. -1, -1, 28, 30, 28, -1, -1, -1, // 40 -> 47
  520. -1, -1, -1, -1, -1, -1, -1, -1, // 48 -> 55
  521. -1, -1, -1, -1, -1, -1, -1, -1, // 56 -> 63
  522. } ;
  523. static uint8_t gpioToClkDiv [] =
  524. {
  525. -1, -1, -1, -1, 29, 31, 33, -1, // 0 -> 7
  526. -1, -1, -1, -1, -1, -1, -1, -1, // 8 -> 15
  527. -1, -1, -1, -1, 29, 31, -1, -1, // 16 -> 23
  528. -1, -1, -1, -1, -1, -1, -1, -1, // 24 -> 31
  529. 29, -1, 29, -1, -1, -1, -1, -1, // 32 -> 39
  530. -1, -1, 29, 31, 29, -1, -1, -1, // 40 -> 47
  531. -1, -1, -1, -1, -1, -1, -1, -1, // 48 -> 55
  532. -1, -1, -1, -1, -1, -1, -1, -1, // 56 -> 63
  533. } ;
  534. /*
  535. * Functions
  536. *********************************************************************************
  537. */
  538. /*
  539. * wiringPiFailure:
  540. * Fail. Or not.
  541. *********************************************************************************
  542. */
  543. int wiringPiFailure (int fatal, const char *message, ...)
  544. {
  545. va_list argp ;
  546. char buffer [1024] ;
  547. if (!fatal && wiringPiReturnCodes)
  548. return -1 ;
  549. va_start (argp, message) ;
  550. vsnprintf (buffer, 1023, message, argp) ;
  551. va_end (argp) ;
  552. fprintf (stderr, "%s", buffer) ;
  553. exit (EXIT_FAILURE) ;
  554. return 0 ;
  555. }
  556. /*
  557. * setupCheck
  558. * Another sanity check because some users forget to call the setup
  559. * function. Mosty because they need feeding C drip by drip )-:
  560. *********************************************************************************
  561. */
  562. static void setupCheck (const char *fName)
  563. {
  564. if (!wiringPiSetuped)
  565. {
  566. fprintf (stderr, "%s: You have not called one of the wiringPiSetup\n"
  567. " functions, so I'm aborting your program before it crashes anyway.\n", fName) ;
  568. exit (EXIT_FAILURE) ;
  569. }
  570. }
  571. /*
  572. * gpioMemCheck:
  573. * See if we're using the /dev/gpiomem interface, if-so then some operations
  574. * can't be done and will crash the Pi.
  575. *********************************************************************************
  576. */
  577. static void usingGpioMemCheck (const char *what)
  578. {
  579. if (usingGpioMem)
  580. {
  581. fprintf (stderr, "%s: Unable to do this when using /dev/gpiomem. Try sudo?\n", what) ;
  582. exit (EXIT_FAILURE) ;
  583. }
  584. }
  585. /*
  586. * piGpioLayout:
  587. * Return a number representing the hardware revision of the board.
  588. * This is not strictly the board revision but is used to check the
  589. * layout of the GPIO connector - and there are 2 types that we are
  590. * really interested in here. The very earliest Pi's and the
  591. * ones that came after that which switched some pins ....
  592. *
  593. * Revision 1 really means the early Model A and B's.
  594. * Revision 2 is everything else - it covers the B, B+ and CM.
  595. * ... and the Pi 2 - which is a B+ ++ ...
  596. * ... and the Pi 0 - which is an A+ ...
  597. *
  598. * The main difference between the revision 1 and 2 system that I use here
  599. * is the mapping of the GPIO pins. From revision 2, the Pi Foundation changed
  600. * 3 GPIO pins on the (original) 26-way header - BCM_GPIO 22 was dropped and
  601. * replaced with 27, and 0 + 1 - I2C bus 0 was changed to 2 + 3; I2C bus 1.
  602. *
  603. * Additionally, here we set the piModel2 flag too. This is again, nothing to
  604. * do with the actual model, but the major version numbers - the GPIO base
  605. * hardware address changed at model 2 and above (not the Zero though)
  606. *
  607. *********************************************************************************
  608. */
  609. static void piGpioLayoutOops (const char *why)
  610. {
  611. fprintf (stderr, "Oops: Unable to determine board revision from /proc/cpuinfo\n") ;
  612. fprintf (stderr, " -> %s\n", why) ;
  613. fprintf (stderr, " -> You'd best google the error to find out why.\n") ;
  614. //fprintf (stderr, " -> http://www.raspberrypi.org/phpBB3/viewtopic.php?p=184410#p184410\n") ;
  615. exit (EXIT_FAILURE) ;
  616. }
  617. int piGpioLayout (void)
  618. {
  619. FILE *cpuFd ;
  620. char line [120] ;
  621. char *c ;
  622. static int gpioLayout = -1 ;
  623. if (gpioLayout != -1) // No point checking twice
  624. return gpioLayout ;
  625. if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL)
  626. piGpioLayoutOops ("Unable to open /proc/cpuinfo") ;
  627. // Start by looking for the Architecture to make sure we're really running
  628. // on a Pi. I'm getting fed-up with people whinging at me because
  629. // they can't get it to work on weirdFruitPi boards...
  630. while (fgets (line, 120, cpuFd) != NULL)
  631. if (strncmp (line, "Hardware", 8) == 0)
  632. break ;
  633. if (strncmp (line, "Hardware", 8) != 0)
  634. piGpioLayoutOops ("No \"Hardware\" line") ;
  635. if (wiringPiDebug)
  636. printf ("piGpioLayout: Hardware: %s\n", line) ;
  637. // See if it's BCM2708 or BCM2709 or the new BCM2835.
  638. // OK. As of Kernel 4.8, we have BCM2835 only, regardless of model.
  639. // However I still want to check because it will trap the cheapskates and rip-
  640. // off merchants who want to use wiringPi on non-Raspberry Pi platforms - which
  641. // I do not support so don't email me your bleating whinges about anything
  642. // other than a genuine Raspberry Pi.
  643. #ifdef DONT_CARE_ANYMORE
  644. if (! (strstr (line, "BCM2708") || strstr (line, "BCM2709") || strstr (line, "BCM2835")))
  645. {
  646. fprintf (stderr, "Unable to determine hardware version. I see: %s,\n", line) ;
  647. fprintf (stderr, " - expecting BCM2708, BCM2709 or BCM2835.\n") ;
  648. fprintf (stderr, "If this is a genuine Raspberry Pi then please report this\n") ;
  649. fprintf (stderr, "at GitHub.com/wiringPi/wiringPi. If this is not a Raspberry Pi then you\n") ;
  650. fprintf (stderr, "are on your own as wiringPi is designed to support the\n") ;
  651. fprintf (stderr, "Raspberry Pi ONLY.\n") ;
  652. exit (EXIT_FAILURE) ;
  653. }
  654. #endif
  655. // Actually... That has caused me more than 10,000 emails so-far. Mosty by
  656. // people who think they know better by creating a statically linked
  657. // version that will not run with a new 4.9 kernel. I utterly hate and
  658. // despise those people.
  659. //
  660. // I also get bleats from people running other than Raspbian with another
  661. // distros compiled kernel rather than a foundation compiled kernel, so
  662. // this might actually help them. It might not - I only have the capacity
  663. // to support Raspbian.
  664. //
  665. // However, I've decided to leave this check out and rely purely on the
  666. // Revision: line for now. It will not work on a non-pi hardware or weird
  667. // kernels that don't give you a suitable revision line.
  668. // So - we're Probably on a Raspberry Pi. Check the revision field for the real
  669. // hardware type
  670. // In-future, I ought to use the device tree as there are now Pi entries in
  671. // /proc/device-tree/ ...
  672. // but I'll leave that for the next revision. Or the next.
  673. // Isolate the Revision line
  674. rewind (cpuFd) ;
  675. while (fgets (line, 120, cpuFd) != NULL)
  676. if (strncmp (line, "Revision", 8) == 0)
  677. break ;
  678. fclose (cpuFd) ;
  679. if (strncmp (line, "Revision", 8) != 0)
  680. piGpioLayoutOops ("No \"Revision\" line") ;
  681. // Chomp trailing CR/NL
  682. for (c = &line [strlen (line) - 1] ; (*c == '\n') || (*c == '\r') ; --c)
  683. *c = 0 ;
  684. if (wiringPiDebug)
  685. printf ("piGpioLayout: Revision string: %s\n", line) ;
  686. // Scan to the first character of the revision number
  687. for (c = line ; *c ; ++c)
  688. if (*c == ':')
  689. break ;
  690. if (*c != ':')
  691. piGpioLayoutOops ("Bogus \"Revision\" line (no colon)") ;
  692. // Chomp spaces
  693. ++c ;
  694. while (isspace (*c))
  695. ++c ;
  696. if (!isxdigit (*c))
  697. piGpioLayoutOops ("Bogus \"Revision\" line (no hex digit at start of revision)") ;
  698. // Make sure its long enough
  699. if (strlen (c) < 4)
  700. piGpioLayoutOops ("Bogus revision line (too small)") ;
  701. // Isolate last 4 characters: (in-case of overvolting or new encoding scheme)
  702. c = c + strlen (c) - 4 ;
  703. if (wiringPiDebug)
  704. printf ("piGpioLayout: last4Chars are: \"%s\"\n", c) ;
  705. if ( (strcmp (c, "0002") == 0) || (strcmp (c, "0003") == 0))
  706. gpioLayout = 1 ;
  707. else
  708. gpioLayout = 2 ; // Covers everything else from the B revision 2 to the B+, the Pi v2, v3, zero and CM's.
  709. if (wiringPiDebug)
  710. printf ("piGpioLayoutOops: Returning revision: %d\n", gpioLayout) ;
  711. return gpioLayout ;
  712. }
  713. /*
  714. * piBoardRev:
  715. * Deprecated, but does the same as piGpioLayout
  716. *********************************************************************************
  717. */
  718. int piBoardRev (void)
  719. {
  720. return piGpioLayout () ;
  721. }
  722. /*
  723. * piBoardId:
  724. * Return the real details of the board we have.
  725. *
  726. * This is undocumented and really only intended for the GPIO command.
  727. * Use at your own risk!
  728. *
  729. * Seems there are some boards with 0000 in them (mistake in manufacture)
  730. * So the distinction between boards that I can see is:
  731. *
  732. * 0000 - Error
  733. * 0001 - Not used
  734. *
  735. * Original Pi boards:
  736. * 0002 - Model B, Rev 1, 256MB, Egoman
  737. * 0003 - Model B, Rev 1.1, 256MB, Egoman, Fuses/D14 removed.
  738. *
  739. * Newer Pi's with remapped GPIO:
  740. * 0004 - Model B, Rev 1.2, 256MB, Sony
  741. * 0005 - Model B, Rev 1.2, 256MB, Egoman
  742. * 0006 - Model B, Rev 1.2, 256MB, Egoman
  743. *
  744. * 0007 - Model A, Rev 1.2, 256MB, Egoman
  745. * 0008 - Model A, Rev 1.2, 256MB, Sony
  746. * 0009 - Model A, Rev 1.2, 256MB, Egoman
  747. *
  748. * 000d - Model B, Rev 1.2, 512MB, Egoman (Red Pi, Blue Pi?)
  749. * 000e - Model B, Rev 1.2, 512MB, Sony
  750. * 000f - Model B, Rev 1.2, 512MB, Egoman
  751. *
  752. * 0010 - Model B+, Rev 1.2, 512MB, Sony
  753. * 0013 - Model B+ Rev 1.2, 512MB, Embest
  754. * 0016 - Model B+ Rev 1.2, 512MB, Sony
  755. * 0019 - Model B+ Rev 1.2, 512MB, Egoman
  756. *
  757. * 0011 - Pi CM, Rev 1.1, 512MB, Sony
  758. * 0014 - Pi CM, Rev 1.1, 512MB, Embest
  759. * 0017 - Pi CM, Rev 1.1, 512MB, Sony
  760. * 001a - Pi CM, Rev 1.1, 512MB, Egoman
  761. *
  762. * 0012 - Model A+ Rev 1.1, 256MB, Sony
  763. * 0015 - Model A+ Rev 1.1, 512MB, Embest
  764. * 0018 - Model A+ Rev 1.1, 256MB, Sony
  765. * 001b - Model A+ Rev 1.1, 256MB, Egoman
  766. *
  767. * A small thorn is the olde style overvolting - that will add in
  768. * 1000000
  769. *
  770. * The Pi compute module has an revision of 0011 or 0014 - since we only
  771. * check the last digit, then it's 1, therefore it'll default to not 2 or
  772. * 3 for a Rev 1, so will appear as a Rev 2. This is fine for the most part, but
  773. * we'll properly detect the Compute Module later and adjust accordingly.
  774. *
  775. * And then things changed with the introduction of the v2...
  776. *
  777. * For Pi v2 and subsequent models - e.g. the Zero:
  778. *
  779. * [USER:8] [NEW:1] [MEMSIZE:3] [MANUFACTURER:4] [PROCESSOR:4] [TYPE:8] [REV:4]
  780. * NEW 23: will be 1 for the new scheme, 0 for the old scheme
  781. * MEMSIZE 20: 0=256M 1=512M 2=1G
  782. * MANUFACTURER 16: 0=SONY 1=EGOMAN 2=EMBEST
  783. * PROCESSOR 12: 0=2835 1=2836
  784. * TYPE 04: 0=MODELA 1=MODELB 2=MODELA+ 3=MODELB+ 4=Pi2 MODEL B 5=ALPHA 6=CM
  785. * REV 00: 0=REV0 1=REV1 2=REV2
  786. *********************************************************************************
  787. */
  788. void piBoardId (int *model, int *rev, int *mem, int *maker, int *warranty)
  789. {
  790. FILE *cpuFd ;
  791. char line [120] ;
  792. char *c ;
  793. unsigned int revision ;
  794. int bRev, bType, bProc, bMfg, bMem, bWarranty ;
  795. // Will deal with the properly later on - for now, lets just get it going...
  796. // unsigned int modelNum ;
  797. (void)piGpioLayout () ; // Call this first to make sure all's OK. Don't care about the result.
  798. if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL)
  799. piGpioLayoutOops ("Unable to open /proc/cpuinfo") ;
  800. while (fgets (line, 120, cpuFd) != NULL)
  801. if (strncmp (line, "Revision", 8) == 0)
  802. break ;
  803. fclose (cpuFd) ;
  804. if (strncmp (line, "Revision", 8) != 0)
  805. piGpioLayoutOops ("No \"Revision\" line") ;
  806. // Chomp trailing CR/NL
  807. for (c = &line [strlen (line) - 1] ; (*c == '\n') || (*c == '\r') ; --c)
  808. *c = 0 ;
  809. if (wiringPiDebug)
  810. printf ("piBoardId: Revision string: %s\n", line) ;
  811. // Need to work out if it's using the new or old encoding scheme:
  812. // Scan to the first character of the revision number
  813. for (c = line ; *c ; ++c)
  814. if (*c == ':')
  815. break ;
  816. if (*c != ':')
  817. piGpioLayoutOops ("Bogus \"Revision\" line (no colon)") ;
  818. // Chomp spaces
  819. ++c ;
  820. while (isspace (*c))
  821. ++c ;
  822. if (!isxdigit (*c))
  823. piGpioLayoutOops ("Bogus \"Revision\" line (no hex digit at start of revision)") ;
  824. revision = (unsigned int)strtol (c, NULL, 16) ; // Hex number with no leading 0x
  825. // Check for new way:
  826. if ((revision & (1 << 23)) != 0) // New way
  827. {
  828. if (wiringPiDebug)
  829. printf ("piBoardId: New Way: revision is: %08X\n", revision) ;
  830. bRev = (revision & (0x0F << 0)) >> 0 ;
  831. bType = (revision & (0xFF << 4)) >> 4 ;
  832. bProc = (revision & (0x0F << 12)) >> 12 ; // Not used for now.
  833. bMfg = (revision & (0x0F << 16)) >> 16 ;
  834. bMem = (revision & (0x07 << 20)) >> 20 ;
  835. bWarranty = (revision & (0x03 << 24)) != 0 ;
  836. *model = bType ;
  837. *rev = bRev ;
  838. *mem = bMem ;
  839. *maker = bMfg ;
  840. *warranty = bWarranty ;
  841. if (wiringPiDebug)
  842. printf ("piBoardId: rev: %d, type: %d, proc: %d, mfg: %d, mem: %d, warranty: %d\n",
  843. bRev, bType, bProc, bMfg, bMem, bWarranty) ;
  844. }
  845. else // Old way
  846. {
  847. if (wiringPiDebug)
  848. printf ("piBoardId: Old Way: revision is: %s\n", c) ;
  849. if (!isdigit (*c))
  850. piGpioLayoutOops ("Bogus \"Revision\" line (no digit at start of revision)") ;
  851. // Make sure its long enough
  852. if (strlen (c) < 4)
  853. piGpioLayoutOops ("Bogus \"Revision\" line (not long enough)") ;
  854. // If longer than 4, we'll assume it's been overvolted
  855. *warranty = strlen (c) > 4 ;
  856. // Extract last 4 characters:
  857. c = c + strlen (c) - 4 ;
  858. // Fill out the replys as appropriate
  859. /**/ if (strcmp (c, "0002") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
  860. else if (strcmp (c, "0003") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_1 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
  861. else if (strcmp (c, "0004") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_2 ; *mem = 0 ; *maker = PI_MAKER_SONY ; }
  862. else if (strcmp (c, "0005") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_2 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
  863. else if (strcmp (c, "0006") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_2 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
  864. else if (strcmp (c, "0007") == 0) { *model = PI_MODEL_A ; *rev = PI_VERSION_1_2 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
  865. else if (strcmp (c, "0008") == 0) { *model = PI_MODEL_A ; *rev = PI_VERSION_1_2 ; *mem = 0 ; *maker = PI_MAKER_SONY ; ; }
  866. else if (strcmp (c, "0009") == 0) { *model = PI_MODEL_A ; *rev = PI_VERSION_1_2 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
  867. else if (strcmp (c, "000d") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_EGOMAN ; }
  868. else if (strcmp (c, "000e") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_SONY ; }
  869. else if (strcmp (c, "000f") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_EGOMAN ; }
  870. else if (strcmp (c, "0010") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_SONY ; }
  871. else if (strcmp (c, "0013") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_EMBEST ; }
  872. else if (strcmp (c, "0016") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_SONY ; }
  873. else if (strcmp (c, "0019") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_EGOMAN ; }
  874. else if (strcmp (c, "0011") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_1 ; *mem = 1 ; *maker = PI_MAKER_SONY ; }
  875. else if (strcmp (c, "0014") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_1 ; *mem = 1 ; *maker = PI_MAKER_EMBEST ; }
  876. else if (strcmp (c, "0017") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_1 ; *mem = 1 ; *maker = PI_MAKER_SONY ; }
  877. else if (strcmp (c, "001a") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_1 ; *mem = 1 ; *maker = PI_MAKER_EGOMAN ; }
  878. else if (strcmp (c, "0012") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_1 ; *mem = 0 ; *maker = PI_MAKER_SONY ; }
  879. else if (strcmp (c, "0015") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_1 ; *mem = 1 ; *maker = PI_MAKER_EMBEST ; }
  880. else if (strcmp (c, "0018") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_1 ; *mem = 0 ; *maker = PI_MAKER_SONY ; }
  881. else if (strcmp (c, "001b") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_1 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
  882. else { *model = 0 ; *rev = 0 ; *mem = 0 ; *maker = 0 ; }
  883. }
  884. }
  885. /*
  886. * wpiPinToGpio:
  887. * Translate a wiringPi Pin number to native GPIO pin number.
  888. * Provided for external support.
  889. *********************************************************************************
  890. */
  891. int wpiPinToGpio (int wpiPin)
  892. {
  893. return pinToGpio [wpiPin & 63] ;
  894. }
  895. /*
  896. * physPinToGpio:
  897. * Translate a physical Pin number to native GPIO pin number.
  898. * Provided for external support.
  899. *********************************************************************************
  900. */
  901. int physPinToGpio (int physPin)
  902. {
  903. return physToGpio [physPin & 63] ;
  904. }
  905. /*
  906. * setPadDrive:
  907. * Set the PAD driver value
  908. *********************************************************************************
  909. */
  910. void setPadDrive (int group, int value)
  911. {
  912. uint32_t wrVal ;
  913. if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
  914. {
  915. if ((group < 0) || (group > 2))
  916. return ;
  917. wrVal = BCM_PASSWORD | 0x18 | (value & 7) ;
  918. *(pads + group + 11) = wrVal ;
  919. if (wiringPiDebug)
  920. {
  921. printf ("setPadDrive: Group: %d, value: %d (%08X)\n", group, value, wrVal) ;
  922. printf ("Read : %08X\n", *(pads + group + 11)) ;
  923. }
  924. }
  925. }
  926. /*
  927. * getAlt:
  928. * Returns the ALT bits for a given port. Only really of-use
  929. * for the gpio readall command (I think)
  930. *********************************************************************************
  931. */
  932. int getAlt (int pin)
  933. {
  934. int fSel, shift, alt ;
  935. pin &= 63 ;
  936. /**/ if (wiringPiMode == WPI_MODE_PINS)
  937. pin = pinToGpio [pin] ;
  938. else if (wiringPiMode == WPI_MODE_PHYS)
  939. pin = physToGpio [pin] ;
  940. else if (wiringPiMode != WPI_MODE_GPIO)
  941. return 0 ;
  942. fSel = gpioToGPFSEL [pin] ;
  943. shift = gpioToShift [pin] ;
  944. alt = (*(gpio + fSel) >> shift) & 7 ;
  945. return alt ;
  946. }
  947. /*
  948. * pwmSetMode:
  949. * Select the native "balanced" mode, or standard mark:space mode
  950. *********************************************************************************
  951. */
  952. void pwmSetMode (int mode)
  953. {
  954. if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
  955. {
  956. if (mode == PWM_MODE_MS)
  957. *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE | PWM0_MS_MODE | PWM1_MS_MODE ;
  958. else
  959. *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ;
  960. }
  961. }
  962. /*
  963. * pwmSetRange:
  964. * Set the PWM range register. We set both range registers to the same
  965. * value. If you want different in your own code, then write your own.
  966. *********************************************************************************
  967. */
  968. void pwmSetRange (unsigned int range)
  969. {
  970. if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
  971. {
  972. *(pwm + PWM0_RANGE) = range ; delayMicroseconds (10) ;
  973. *(pwm + PWM1_RANGE) = range ; delayMicroseconds (10) ;
  974. }
  975. }
  976. /*
  977. * pwmSetClock:
  978. * Set/Change the PWM clock. Originally my code, but changed
  979. * (for the better!) by Chris Hall, <chris@kchall.plus.com>
  980. * after further study of the manual and testing with a 'scope
  981. *********************************************************************************
  982. */
  983. void pwmSetClock (int divisor)
  984. {
  985. uint32_t pwm_control ;
  986. if (piGpioBase == GPIO_PERI_BASE_2711)
  987. {
  988. divisor = 540*divisor/192;
  989. }
  990. divisor &= 4095 ;
  991. if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
  992. {
  993. if (wiringPiDebug)
  994. printf ("Setting to: %d. Current: 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;
  995. pwm_control = *(pwm + PWM_CONTROL) ; // preserve PWM_CONTROL
  996. // We need to stop PWM prior to stopping PWM clock in MS mode otherwise BUSY
  997. // stays high.
  998. *(pwm + PWM_CONTROL) = 0 ; // Stop PWM
  999. // Stop PWM clock before changing divisor. The delay after this does need to
  1000. // this big (95uS occasionally fails, 100uS OK), it's almost as though the BUSY
  1001. // flag is not working properly in balanced mode. Without the delay when DIV is
  1002. // adjusted the clock sometimes switches to very slow, once slow further DIV
  1003. // adjustments do nothing and it's difficult to get out of this mode.
  1004. *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x01 ; // Stop PWM Clock
  1005. delayMicroseconds (110) ; // prevents clock going sloooow
  1006. while ((*(clk + PWMCLK_CNTL) & 0x80) != 0) // Wait for clock to be !BUSY
  1007. delayMicroseconds (1) ;
  1008. *(clk + PWMCLK_DIV) = BCM_PASSWORD | (divisor << 12) ;
  1009. *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x11 ; // Start PWM clock
  1010. *(pwm + PWM_CONTROL) = pwm_control ; // restore PWM_CONTROL
  1011. if (wiringPiDebug)
  1012. printf ("Set to: %d. Now : 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;
  1013. }
  1014. }
  1015. /*
  1016. * gpioClockSet:
  1017. * Set the frequency on a GPIO clock pin
  1018. *********************************************************************************
  1019. */
  1020. void gpioClockSet (int pin, int freq)
  1021. {
  1022. int divi, divr, divf ;
  1023. pin &= 63 ;
  1024. /**/ if (wiringPiMode == WPI_MODE_PINS)
  1025. pin = pinToGpio [pin] ;
  1026. else if (wiringPiMode == WPI_MODE_PHYS)
  1027. pin = physToGpio [pin] ;
  1028. else if (wiringPiMode != WPI_MODE_GPIO)
  1029. return ;
  1030. divi = 19200000 / freq ;
  1031. divr = 19200000 % freq ;
  1032. divf = (int)((double)divr * 4096.0 / 19200000.0) ;
  1033. if (divi > 4095)
  1034. divi = 4095 ;
  1035. *(clk + gpioToClkCon [pin]) = BCM_PASSWORD | GPIO_CLOCK_SOURCE ; // Stop GPIO Clock
  1036. while ((*(clk + gpioToClkCon [pin]) & 0x80) != 0) // ... and wait
  1037. ;
  1038. *(clk + gpioToClkDiv [pin]) = BCM_PASSWORD | (divi << 12) | divf ; // Set dividers
  1039. *(clk + gpioToClkCon [pin]) = BCM_PASSWORD | 0x10 | GPIO_CLOCK_SOURCE ; // Start Clock
  1040. }
  1041. /*
  1042. * wiringPiFindNode:
  1043. * Locate our device node
  1044. *********************************************************************************
  1045. */
  1046. struct wiringPiNodeStruct *wiringPiFindNode (int pin)
  1047. {
  1048. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1049. while (node != NULL)
  1050. if ((pin >= node->pinBase) && (pin <= node->pinMax))
  1051. return node ;
  1052. else
  1053. node = node->next ;
  1054. return NULL ;
  1055. }
  1056. /*
  1057. * wiringPiNewNode:
  1058. * Create a new GPIO node into the wiringPi handling system
  1059. *********************************************************************************
  1060. */
  1061. static void pinModeDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int mode) { return ; }
  1062. static void pullUpDnControlDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int pud) { return ; }
  1063. //static unsigned int digitalRead8Dummy (UNU struct wiringPiNodeStruct *node, UNU int UNU pin) { return 0 ; }
  1064. //static void digitalWrite8Dummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int value) { return ; }
  1065. static int digitalReadDummy (UNU struct wiringPiNodeStruct *node, UNU int UNU pin) { return LOW ; }
  1066. static void digitalWriteDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int value) { return ; }
  1067. static void pwmWriteDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int value) { return ; }
  1068. static int analogReadDummy (UNU struct wiringPiNodeStruct *node, UNU int pin) { return 0 ; }
  1069. static void analogWriteDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int value) { return ; }
  1070. struct wiringPiNodeStruct *wiringPiNewNode (int pinBase, int numPins)
  1071. {
  1072. int pin ;
  1073. struct wiringPiNodeStruct *node ;
  1074. // Minimum pin base is 64
  1075. if (pinBase < 64)
  1076. (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: pinBase of %d is < 64\n", pinBase) ;
  1077. // Check all pins in-case there is overlap:
  1078. for (pin = pinBase ; pin < (pinBase + numPins) ; ++pin)
  1079. if (wiringPiFindNode (pin) != NULL)
  1080. (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: Pin %d overlaps with existing definition\n", pin) ;
  1081. node = (struct wiringPiNodeStruct *)calloc (sizeof (struct wiringPiNodeStruct), 1) ; // calloc zeros
  1082. if (node == NULL)
  1083. (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: Unable to allocate memory: %s\n", strerror (errno)) ;
  1084. node->pinBase = pinBase ;
  1085. node->pinMax = pinBase + numPins - 1 ;
  1086. node->pinMode = pinModeDummy ;
  1087. node->pullUpDnControl = pullUpDnControlDummy ;
  1088. node->digitalRead = digitalReadDummy ;
  1089. //node->digitalRead8 = digitalRead8Dummy ;
  1090. node->digitalWrite = digitalWriteDummy ;
  1091. //node->digitalWrite8 = digitalWrite8Dummy ;
  1092. node->pwmWrite = pwmWriteDummy ;
  1093. node->analogRead = analogReadDummy ;
  1094. node->analogWrite = analogWriteDummy ;
  1095. node->next = wiringPiNodes ;
  1096. wiringPiNodes = node ;
  1097. return node ;
  1098. }
  1099. #ifdef notYetReady
  1100. /*
  1101. * pinED01:
  1102. * pinED10:
  1103. * Enables edge-detect mode on a pin - from a 0 to a 1 or 1 to 0
  1104. * Pin must already be in input mode with appropriate pull up/downs set.
  1105. *********************************************************************************
  1106. */
  1107. void pinEnableED01Pi (int pin)
  1108. {
  1109. pin = pinToGpio [pin & 63] ;
  1110. }
  1111. #endif
  1112. /*
  1113. *********************************************************************************
  1114. * Core Functions
  1115. *********************************************************************************
  1116. */
  1117. /*
  1118. * pinModeAlt:
  1119. * This is an un-documented special to let you set any pin to any mode
  1120. *********************************************************************************
  1121. */
  1122. void pinModeAlt (int pin, int mode)
  1123. {
  1124. int fSel, shift ;
  1125. setupCheck ("pinModeAlt") ;
  1126. if ((pin & PI_GPIO_MASK) == 0) // On-board pin
  1127. {
  1128. /**/ if (wiringPiMode == WPI_MODE_PINS)
  1129. pin = pinToGpio [pin] ;
  1130. else if (wiringPiMode == WPI_MODE_PHYS)
  1131. pin = physToGpio [pin] ;
  1132. else if (wiringPiMode != WPI_MODE_GPIO)
  1133. return ;
  1134. fSel = gpioToGPFSEL [pin] ;
  1135. shift = gpioToShift [pin] ;
  1136. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | ((mode & 0x7) << shift) ;
  1137. }
  1138. }
  1139. /*
  1140. * pinMode:
  1141. * Sets the mode of a pin to be input, output or PWM output
  1142. *********************************************************************************
  1143. */
  1144. void pinMode (int pin, int mode)
  1145. {
  1146. int fSel, shift, alt ;
  1147. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1148. int origPin = pin ;
  1149. setupCheck ("pinMode") ;
  1150. if ((pin & PI_GPIO_MASK) == 0) // On-board pin
  1151. {
  1152. /**/ if (wiringPiMode == WPI_MODE_PINS)
  1153. pin = pinToGpio [pin] ;
  1154. else if (wiringPiMode == WPI_MODE_PHYS)
  1155. pin = physToGpio [pin] ;
  1156. else if (wiringPiMode != WPI_MODE_GPIO)
  1157. return ;
  1158. softPwmStop (origPin) ;
  1159. softToneStop (origPin) ;
  1160. fSel = gpioToGPFSEL [pin] ;
  1161. shift = gpioToShift [pin] ;
  1162. /**/ if (mode == INPUT)
  1163. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) ; // Sets bits to zero = input
  1164. else if (mode == OUTPUT)
  1165. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (1 << shift) ;
  1166. else if (mode == SOFT_PWM_OUTPUT)
  1167. softPwmCreate (origPin, 0, 100) ;
  1168. else if (mode == SOFT_TONE_OUTPUT)
  1169. softToneCreate (origPin) ;
  1170. else if (mode == PWM_TONE_OUTPUT)
  1171. {
  1172. pinMode (origPin, PWM_OUTPUT) ; // Call myself to enable PWM mode
  1173. pwmSetMode (PWM_MODE_MS) ;
  1174. }
  1175. else if (mode == PWM_OUTPUT)
  1176. {
  1177. if ((alt = gpioToPwmALT [pin]) == 0) // Not a hardware capable PWM pin
  1178. return ;
  1179. usingGpioMemCheck ("pinMode PWM") ;
  1180. // Set pin to PWM mode
  1181. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ;
  1182. delayMicroseconds (110) ; // See comments in pwmSetClockWPi
  1183. pwmSetMode (PWM_MODE_BAL) ; // Pi default mode
  1184. pwmSetRange (1024) ; // Default range of 1024
  1185. pwmSetClock (32) ; // 19.2 / 32 = 600KHz - Also starts the PWM
  1186. }
  1187. else if (mode == GPIO_CLOCK)
  1188. {
  1189. if ((alt = gpioToGpClkALT0 [pin]) == 0) // Not a GPIO_CLOCK pin
  1190. return ;
  1191. usingGpioMemCheck ("pinMode CLOCK") ;
  1192. // Set pin to GPIO_CLOCK mode and set the clock frequency to 100KHz
  1193. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ;
  1194. delayMicroseconds (110) ;
  1195. gpioClockSet (pin, 100000) ;
  1196. }
  1197. }
  1198. else
  1199. {
  1200. if ((node = wiringPiFindNode (pin)) != NULL)
  1201. node->pinMode (node, pin, mode) ;
  1202. return ;
  1203. }
  1204. }
  1205. /*
  1206. * pullUpDownCtrl:
  1207. * Control the internal pull-up/down resistors on a GPIO pin.
  1208. *********************************************************************************
  1209. */
  1210. void pullUpDnControl (int pin, int pud)
  1211. {
  1212. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1213. setupCheck ("pullUpDnControl") ;
  1214. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1215. {
  1216. /**/ if (wiringPiMode == WPI_MODE_PINS)
  1217. pin = pinToGpio [pin] ;
  1218. else if (wiringPiMode == WPI_MODE_PHYS)
  1219. pin = physToGpio [pin] ;
  1220. else if (wiringPiMode != WPI_MODE_GPIO)
  1221. return ;
  1222. if (piGpioPupOffset == GPPUPPDN0)
  1223. {
  1224. // Pi 4B pull up/down method
  1225. int pullreg = GPPUPPDN0 + (pin>>4);
  1226. int pullshift = (pin & 0xf) << 1;
  1227. unsigned int pullbits;
  1228. unsigned int pull;
  1229. switch (pud)
  1230. {
  1231. case PUD_OFF: pull = 0; break;
  1232. case PUD_UP: pull = 1; break;
  1233. case PUD_DOWN: pull = 2; break;
  1234. default: return ; /* An illegal value */
  1235. }
  1236. pullbits = *(gpio + pullreg);
  1237. pullbits &= ~(3 << pullshift);
  1238. pullbits |= (pull << pullshift);
  1239. *(gpio + pullreg) = pullbits;
  1240. }
  1241. else
  1242. {
  1243. // legacy pull up/down method
  1244. *(gpio + GPPUD) = pud & 3 ; delayMicroseconds (5) ;
  1245. *(gpio + gpioToPUDCLK [pin]) = 1 << (pin & 31) ; delayMicroseconds (5) ;
  1246. *(gpio + GPPUD) = 0 ; delayMicroseconds (5) ;
  1247. *(gpio + gpioToPUDCLK [pin]) = 0 ; delayMicroseconds (5) ;
  1248. }
  1249. }
  1250. else // Extension module
  1251. {
  1252. if ((node = wiringPiFindNode (pin)) != NULL)
  1253. node->pullUpDnControl (node, pin, pud) ;
  1254. return ;
  1255. }
  1256. }
  1257. /*
  1258. * digitalRead:
  1259. * Read the value of a given Pin, returning HIGH or LOW
  1260. *********************************************************************************
  1261. */
  1262. int digitalRead (int pin)
  1263. {
  1264. char c ;
  1265. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1266. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1267. {
  1268. /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS) // Sys mode
  1269. {
  1270. if (sysFds [pin] == -1)
  1271. return LOW ;
  1272. lseek (sysFds [pin], 0L, SEEK_SET) ;
  1273. read (sysFds [pin], &c, 1) ;
  1274. return (c == '0') ? LOW : HIGH ;
  1275. }
  1276. else if (wiringPiMode == WPI_MODE_PINS)
  1277. pin = pinToGpio [pin] ;
  1278. else if (wiringPiMode == WPI_MODE_PHYS)
  1279. pin = physToGpio [pin] ;
  1280. else if (wiringPiMode != WPI_MODE_GPIO)
  1281. return LOW ;
  1282. if ((*(gpio + gpioToGPLEV [pin]) & (1 << (pin & 31))) != 0)
  1283. return HIGH ;
  1284. else
  1285. return LOW ;
  1286. }
  1287. else
  1288. {
  1289. if ((node = wiringPiFindNode (pin)) == NULL)
  1290. return LOW ;
  1291. return node->digitalRead (node, pin) ;
  1292. }
  1293. }
  1294. /*
  1295. * digitalRead8:
  1296. * Read 8-bits (a byte) from given start pin.
  1297. *********************************************************************************
  1298. unsigned int digitalRead8 (int pin)
  1299. {
  1300. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1301. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1302. return 0 ;
  1303. else
  1304. {
  1305. if ((node = wiringPiFindNode (pin)) == NULL)
  1306. return LOW ;
  1307. return node->digitalRead8 (node, pin) ;
  1308. }
  1309. }
  1310. */
  1311. /*
  1312. * digitalWrite:
  1313. * Set an output bit
  1314. *********************************************************************************
  1315. */
  1316. void digitalWrite (int pin, int value)
  1317. {
  1318. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1319. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1320. {
  1321. /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS) // Sys mode
  1322. {
  1323. if (sysFds [pin] != -1)
  1324. {
  1325. if (value == LOW)
  1326. write (sysFds [pin], "0\n", 2) ;
  1327. else
  1328. write (sysFds [pin], "1\n", 2) ;
  1329. }
  1330. return ;
  1331. }
  1332. else if (wiringPiMode == WPI_MODE_PINS)
  1333. pin = pinToGpio [pin] ;
  1334. else if (wiringPiMode == WPI_MODE_PHYS)
  1335. pin = physToGpio [pin] ;
  1336. else if (wiringPiMode != WPI_MODE_GPIO)
  1337. return ;
  1338. if (value == LOW)
  1339. *(gpio + gpioToGPCLR [pin]) = 1 << (pin & 31) ;
  1340. else
  1341. *(gpio + gpioToGPSET [pin]) = 1 << (pin & 31) ;
  1342. }
  1343. else
  1344. {
  1345. if ((node = wiringPiFindNode (pin)) != NULL)
  1346. node->digitalWrite (node, pin, value) ;
  1347. }
  1348. }
  1349. /*
  1350. * digitalWrite8:
  1351. * Set an output 8-bit byte on the device from the given pin number
  1352. *********************************************************************************
  1353. void digitalWrite8 (int pin, int value)
  1354. {
  1355. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1356. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1357. return ;
  1358. else
  1359. {
  1360. if ((node = wiringPiFindNode (pin)) != NULL)
  1361. node->digitalWrite8 (node, pin, value) ;
  1362. }
  1363. }
  1364. */
  1365. /*
  1366. * pwmWrite:
  1367. * Set an output PWM value
  1368. *********************************************************************************
  1369. */
  1370. void pwmWrite (int pin, int value)
  1371. {
  1372. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1373. setupCheck ("pwmWrite") ;
  1374. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1375. {
  1376. /**/ if (wiringPiMode == WPI_MODE_PINS)
  1377. pin = pinToGpio [pin] ;
  1378. else if (wiringPiMode == WPI_MODE_PHYS)
  1379. pin = physToGpio [pin] ;
  1380. else if (wiringPiMode != WPI_MODE_GPIO)
  1381. return ;
  1382. usingGpioMemCheck ("pwmWrite") ;
  1383. *(pwm + gpioToPwmPort [pin]) = value ;
  1384. }
  1385. else
  1386. {
  1387. if ((node = wiringPiFindNode (pin)) != NULL)
  1388. node->pwmWrite (node, pin, value) ;
  1389. }
  1390. }
  1391. /*
  1392. * analogRead:
  1393. * Read the analog value of a given Pin.
  1394. * There is no on-board Pi analog hardware,
  1395. * so this needs to go to a new node.
  1396. *********************************************************************************
  1397. */
  1398. int analogRead (int pin)
  1399. {
  1400. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1401. if ((node = wiringPiFindNode (pin)) == NULL)
  1402. return 0 ;
  1403. else
  1404. return node->analogRead (node, pin) ;
  1405. }
  1406. /*
  1407. * analogWrite:
  1408. * Write the analog value to the given Pin.
  1409. * There is no on-board Pi analog hardware,
  1410. * so this needs to go to a new node.
  1411. *********************************************************************************
  1412. */
  1413. void analogWrite (int pin, int value)
  1414. {
  1415. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1416. if ((node = wiringPiFindNode (pin)) == NULL)
  1417. return ;
  1418. node->analogWrite (node, pin, value) ;
  1419. }
  1420. /*
  1421. * pwmToneWrite:
  1422. * Pi Specific.
  1423. * Output the given frequency on the Pi's PWM pin
  1424. *********************************************************************************
  1425. */
  1426. void pwmToneWrite (int pin, int freq)
  1427. {
  1428. int range ;
  1429. setupCheck ("pwmToneWrite") ;
  1430. if (freq == 0)
  1431. pwmWrite (pin, 0) ; // Off
  1432. else
  1433. {
  1434. range = 600000 / freq ;
  1435. pwmSetRange (range) ;
  1436. pwmWrite (pin, freq / 2) ;
  1437. }
  1438. }
  1439. /*
  1440. * digitalWriteByte:
  1441. * digitalReadByte:
  1442. * Pi Specific
  1443. * Write an 8-bit byte to the first 8 GPIO pins - try to do it as
  1444. * fast as possible.
  1445. * However it still needs 2 operations to set the bits, so any external
  1446. * hardware must not rely on seeing a change as there will be a change
  1447. * to set the outputs bits to zero, then another change to set the 1's
  1448. * Reading is just bit fiddling.
  1449. * These are wiringPi pin numbers 0..7, or BCM_GPIO pin numbers
  1450. * 17, 18, 22, 23, 24, 24, 4 on a Pi v1 rev 0-3
  1451. * 17, 18, 27, 23, 24, 24, 4 on a Pi v1 rev 3 onwards or B+, 2, 3, zero
  1452. *********************************************************************************
  1453. */
  1454. void digitalWriteByte (const int value)
  1455. {
  1456. uint32_t pinSet = 0 ;
  1457. uint32_t pinClr = 0 ;
  1458. int mask = 1 ;
  1459. int pin ;
  1460. /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS)
  1461. {
  1462. for (pin = 0 ; pin < 8 ; ++pin)
  1463. {
  1464. digitalWrite (pinToGpio [pin], value & mask) ;
  1465. mask <<= 1 ;
  1466. }
  1467. return ;
  1468. }
  1469. else
  1470. {
  1471. for (pin = 0 ; pin < 8 ; ++pin)
  1472. {
  1473. if ((value & mask) == 0)
  1474. pinClr |= (1 << pinToGpio [pin]) ;
  1475. else
  1476. pinSet |= (1 << pinToGpio [pin]) ;
  1477. mask <<= 1 ;
  1478. }
  1479. *(gpio + gpioToGPCLR [0]) = pinClr ;
  1480. *(gpio + gpioToGPSET [0]) = pinSet ;
  1481. }
  1482. }
  1483. unsigned int digitalReadByte (void)
  1484. {
  1485. int pin, x ;
  1486. uint32_t raw ;
  1487. uint32_t data = 0 ;
  1488. /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS)
  1489. {
  1490. for (pin = 0 ; pin < 8 ; ++pin)
  1491. {
  1492. x = digitalRead (pinToGpio [pin]) ;
  1493. data = (data << 1) | x ;
  1494. }
  1495. }
  1496. else
  1497. {
  1498. raw = *(gpio + gpioToGPLEV [0]) ; // First bank for these pins
  1499. for (pin = 0 ; pin < 8 ; ++pin)
  1500. {
  1501. x = pinToGpio [pin] ;
  1502. data = (data << 1) | (((raw & (1 << x)) == 0) ? 0 : 1) ;
  1503. }
  1504. }
  1505. return data ;
  1506. }
  1507. /*
  1508. * digitalWriteByte2:
  1509. * digitalReadByte2:
  1510. * Pi Specific
  1511. * Write an 8-bit byte to the second set of 8 GPIO pins. This is marginally
  1512. * faster than the first lot as these are consecutive BCM_GPIO pin numbers.
  1513. * However they overlap with the original read/write bytes.
  1514. *********************************************************************************
  1515. */
  1516. void digitalWriteByte2 (const int value)
  1517. {
  1518. register int mask = 1 ;
  1519. register int pin ;
  1520. /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS)
  1521. {
  1522. for (pin = 20 ; pin < 28 ; ++pin)
  1523. {
  1524. digitalWrite (pin, value & mask) ;
  1525. mask <<= 1 ;
  1526. }
  1527. return ;
  1528. }
  1529. else
  1530. {
  1531. *(gpio + gpioToGPCLR [0]) = (~value & 0xFF) << 20 ; // 0x0FF00000; ILJ > CHANGE: Old causes glitch
  1532. *(gpio + gpioToGPSET [0]) = ( value & 0xFF) << 20 ;
  1533. }
  1534. }
  1535. unsigned int digitalReadByte2 (void)
  1536. {
  1537. int pin, x ;
  1538. uint32_t data = 0 ;
  1539. /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS)
  1540. {
  1541. for (pin = 20 ; pin < 28 ; ++pin)
  1542. {
  1543. x = digitalRead (pin) ;
  1544. data = (data << 1) | x ;
  1545. }
  1546. }
  1547. else
  1548. data = ((*(gpio + gpioToGPLEV [0])) >> 20) & 0xFF ; // First bank for these pins
  1549. return data ;
  1550. }
  1551. /*
  1552. * waitForInterrupt:
  1553. * Pi Specific.
  1554. * Wait for Interrupt on a GPIO pin.
  1555. * This is actually done via the /sys/class/gpio interface regardless of
  1556. * the wiringPi access mode in-use. Maybe sometime it might get a better
  1557. * way for a bit more efficiency.
  1558. *********************************************************************************
  1559. */
  1560. int waitForInterrupt (int pin, int mS)
  1561. {
  1562. int fd, x ;
  1563. uint8_t c ;
  1564. struct pollfd polls ;
  1565. /**/ if (wiringPiMode == WPI_MODE_PINS)
  1566. pin = pinToGpio [pin] ;
  1567. else if (wiringPiMode == WPI_MODE_PHYS)
  1568. pin = physToGpio [pin] ;
  1569. if ((fd = sysFds [pin]) == -1)
  1570. return -2 ;
  1571. // Setup poll structure
  1572. polls.fd = fd ;
  1573. polls.events = POLLPRI | POLLERR ;
  1574. // Wait for it ...
  1575. x = poll (&polls, 1, mS) ;
  1576. // If no error, do a dummy read to clear the interrupt
  1577. // A one character read appars to be enough.
  1578. if (x > 0)
  1579. {
  1580. lseek (fd, 0, SEEK_SET) ; // Rewind
  1581. (void)read (fd, &c, 1) ; // Read & clear
  1582. }
  1583. return x ;
  1584. }
  1585. /*
  1586. * interruptHandler:
  1587. * This is a thread and gets started to wait for the interrupt we're
  1588. * hoping to catch. It will call the user-function when the interrupt
  1589. * fires.
  1590. *********************************************************************************
  1591. */
  1592. static void *interruptHandler (UNU void *arg)
  1593. {
  1594. int myPin ;
  1595. (void)piHiPri (55) ; // Only effective if we run as root
  1596. myPin = pinPass ;
  1597. pinPass = -1 ;
  1598. for (;;)
  1599. if (waitForInterrupt (myPin, -1) > 0)
  1600. isrFunctions [myPin] () ;
  1601. return NULL ;
  1602. }
  1603. /*
  1604. * wiringPiISR:
  1605. * Pi Specific.
  1606. * Take the details and create an interrupt handler that will do a call-
  1607. * back to the user supplied function.
  1608. *********************************************************************************
  1609. */
  1610. int wiringPiISR (int pin, int mode, void (*function)(void))
  1611. {
  1612. pthread_t threadId ;
  1613. const char *modeS ;
  1614. char fName [64] ;
  1615. char pinS [8] ;
  1616. pid_t pid ;
  1617. int count, i ;
  1618. char c ;
  1619. int bcmGpioPin ;
  1620. if ((pin < 0) || (pin > 63))
  1621. return wiringPiFailure (WPI_FATAL, "wiringPiISR: pin must be 0-63 (%d)\n", pin) ;
  1622. /**/ if (wiringPiMode == WPI_MODE_UNINITIALISED)
  1623. return wiringPiFailure (WPI_FATAL, "wiringPiISR: wiringPi has not been initialised. Unable to continue.\n") ;
  1624. else if (wiringPiMode == WPI_MODE_PINS)
  1625. bcmGpioPin = pinToGpio [pin] ;
  1626. else if (wiringPiMode == WPI_MODE_PHYS)
  1627. bcmGpioPin = physToGpio [pin] ;
  1628. else
  1629. bcmGpioPin = pin ;
  1630. // Now export the pin and set the right edge
  1631. // We're going to use the gpio program to do this, so it assumes
  1632. // a full installation of wiringPi. It's a bit 'clunky', but it
  1633. // is a way that will work when we're running in "Sys" mode, as
  1634. // a non-root user. (without sudo)
  1635. if (mode != INT_EDGE_SETUP)
  1636. {
  1637. /**/ if (mode == INT_EDGE_FALLING)
  1638. modeS = "falling" ;
  1639. else if (mode == INT_EDGE_RISING)
  1640. modeS = "rising" ;
  1641. else
  1642. modeS = "both" ;
  1643. sprintf (pinS, "%d", bcmGpioPin) ;
  1644. if ((pid = fork ()) < 0) // Fail
  1645. return wiringPiFailure (WPI_FATAL, "wiringPiISR: fork failed: %s\n", strerror (errno)) ;
  1646. if (pid == 0) // Child, exec
  1647. {
  1648. /**/ if (access ("/usr/local/bin/gpio", X_OK) == 0)
  1649. {
  1650. execl ("/usr/local/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ;
  1651. return wiringPiFailure (WPI_FATAL, "wiringPiISR: execl failed: %s\n", strerror (errno)) ;
  1652. }
  1653. else if (access ("/usr/bin/gpio", X_OK) == 0)
  1654. {
  1655. execl ("/usr/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ;
  1656. return wiringPiFailure (WPI_FATAL, "wiringPiISR: execl failed: %s\n", strerror (errno)) ;
  1657. }
  1658. else
  1659. return wiringPiFailure (WPI_FATAL, "wiringPiISR: Can't find gpio program\n") ;
  1660. }
  1661. else // Parent, wait
  1662. waitpid (pid, NULL, 0) ;
  1663. }
  1664. // Now pre-open the /sys/class node - but it may already be open if
  1665. // we are in Sys mode...
  1666. if (sysFds [bcmGpioPin] == -1)
  1667. {
  1668. sprintf (fName, "/sys/class/gpio/gpio%d/value", bcmGpioPin) ;
  1669. if ((sysFds [bcmGpioPin] = open (fName, O_RDWR)) < 0)
  1670. return wiringPiFailure (WPI_FATAL, "wiringPiISR: unable to open %s: %s\n", fName, strerror (errno)) ;
  1671. }
  1672. // Clear any initial pending interrupt
  1673. ioctl (sysFds [bcmGpioPin], FIONREAD, &count) ;
  1674. for (i = 0 ; i < count ; ++i)
  1675. read (sysFds [bcmGpioPin], &c, 1) ;
  1676. isrFunctions [pin] = function ;
  1677. pthread_mutex_lock (&pinMutex) ;
  1678. pinPass = pin ;
  1679. pthread_create (&threadId, NULL, interruptHandler, NULL) ;
  1680. while (pinPass != -1)
  1681. delay (1) ;
  1682. pthread_mutex_unlock (&pinMutex) ;
  1683. return 0 ;
  1684. }
  1685. /*
  1686. * initialiseEpoch:
  1687. * Initialise our start-of-time variable to be the current unix
  1688. * time in milliseconds and microseconds.
  1689. *********************************************************************************
  1690. */
  1691. static void initialiseEpoch (void)
  1692. {
  1693. #ifdef OLD_WAY
  1694. struct timeval tv ;
  1695. gettimeofday (&tv, NULL) ;
  1696. epochMilli = (uint64_t)tv.tv_sec * (uint64_t)1000 + (uint64_t)(tv.tv_usec / 1000) ;
  1697. epochMicro = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)(tv.tv_usec) ;
  1698. #else
  1699. struct timespec ts ;
  1700. clock_gettime (CLOCK_MONOTONIC_RAW, &ts) ;
  1701. epochMilli = (uint64_t)ts.tv_sec * (uint64_t)1000 + (uint64_t)(ts.tv_nsec / 1000000L) ;
  1702. epochMicro = (uint64_t)ts.tv_sec * (uint64_t)1000000 + (uint64_t)(ts.tv_nsec / 1000L) ;
  1703. #endif
  1704. }
  1705. /*
  1706. * delay:
  1707. * Wait for some number of milliseconds
  1708. *********************************************************************************
  1709. */
  1710. void delay (unsigned int howLong)
  1711. {
  1712. struct timespec sleeper, dummy ;
  1713. sleeper.tv_sec = (time_t)(howLong / 1000) ;
  1714. sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ;
  1715. nanosleep (&sleeper, &dummy) ;
  1716. }
  1717. /*
  1718. * delayMicroseconds:
  1719. * This is somewhat intersting. It seems that on the Pi, a single call
  1720. * to nanosleep takes some 80 to 130 microseconds anyway, so while
  1721. * obeying the standards (may take longer), it's not always what we
  1722. * want!
  1723. *
  1724. * So what I'll do now is if the delay is less than 100uS we'll do it
  1725. * in a hard loop, watching a built-in counter on the ARM chip. This is
  1726. * somewhat sub-optimal in that it uses 100% CPU, something not an issue
  1727. * in a microcontroller, but under a multi-tasking, multi-user OS, it's
  1728. * wastefull, however we've no real choice )-:
  1729. *
  1730. * Plan B: It seems all might not be well with that plan, so changing it
  1731. * to use gettimeofday () and poll on that instead...
  1732. *********************************************************************************
  1733. */
  1734. void delayMicrosecondsHard (unsigned int howLong)
  1735. {
  1736. struct timeval tNow, tLong, tEnd ;
  1737. gettimeofday (&tNow, NULL) ;
  1738. tLong.tv_sec = howLong / 1000000 ;
  1739. tLong.tv_usec = howLong % 1000000 ;
  1740. timeradd (&tNow, &tLong, &tEnd) ;
  1741. while (timercmp (&tNow, &tEnd, <))
  1742. gettimeofday (&tNow, NULL) ;
  1743. }
  1744. void delayMicroseconds (unsigned int howLong)
  1745. {
  1746. struct timespec sleeper ;
  1747. unsigned int uSecs = howLong % 1000000 ;
  1748. unsigned int wSecs = howLong / 1000000 ;
  1749. /**/ if (howLong == 0)
  1750. return ;
  1751. else if (howLong < 100)
  1752. delayMicrosecondsHard (howLong) ;
  1753. else
  1754. {
  1755. sleeper.tv_sec = wSecs ;
  1756. sleeper.tv_nsec = (long)(uSecs * 1000L) ;
  1757. nanosleep (&sleeper, NULL) ;
  1758. }
  1759. }
  1760. /*
  1761. * millis:
  1762. * Return a number of milliseconds as an unsigned int.
  1763. * Wraps at 49 days.
  1764. *********************************************************************************
  1765. */
  1766. unsigned int millis (void)
  1767. {
  1768. uint64_t now ;
  1769. #ifdef OLD_WAY
  1770. struct timeval tv ;
  1771. gettimeofday (&tv, NULL) ;
  1772. now = (uint64_t)tv.tv_sec * (uint64_t)1000 + (uint64_t)(tv.tv_usec / 1000) ;
  1773. #else
  1774. struct timespec ts ;
  1775. clock_gettime (CLOCK_MONOTONIC_RAW, &ts) ;
  1776. now = (uint64_t)ts.tv_sec * (uint64_t)1000 + (uint64_t)(ts.tv_nsec / 1000000L) ;
  1777. #endif
  1778. return (uint32_t)(now - epochMilli) ;
  1779. }
  1780. /*
  1781. * micros:
  1782. * Return a number of microseconds as an unsigned int.
  1783. * Wraps after 71 minutes.
  1784. *********************************************************************************
  1785. */
  1786. unsigned int micros (void)
  1787. {
  1788. uint64_t now ;
  1789. #ifdef OLD_WAY
  1790. struct timeval tv ;
  1791. gettimeofday (&tv, NULL) ;
  1792. now = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)tv.tv_usec ;
  1793. #else
  1794. struct timespec ts ;
  1795. clock_gettime (CLOCK_MONOTONIC_RAW, &ts) ;
  1796. now = (uint64_t)ts.tv_sec * (uint64_t)1000000 + (uint64_t)(ts.tv_nsec / 1000) ;
  1797. #endif
  1798. return (uint32_t)(now - epochMicro) ;
  1799. }
  1800. /*
  1801. * wiringPiVersion:
  1802. * Return our current version number
  1803. *********************************************************************************
  1804. */
  1805. void wiringPiVersion (int *major, int *minor)
  1806. {
  1807. *major = VERSION_MAJOR ;
  1808. *minor = VERSION_MINOR ;
  1809. }
  1810. /*
  1811. * wiringPiSetup:
  1812. * Must be called once at the start of your program execution.
  1813. *
  1814. * Default setup: Initialises the system into wiringPi Pin mode and uses the
  1815. * memory mapped hardware directly.
  1816. *
  1817. * Changed now to revert to "gpio" mode if we're running on a Compute Module.
  1818. *********************************************************************************
  1819. */
  1820. int wiringPiSetup (void)
  1821. {
  1822. int fd ;
  1823. int model, rev, mem, maker, overVolted ;
  1824. if (wiringPiSetuped)
  1825. return 0 ;
  1826. wiringPiSetuped = TRUE ;
  1827. if (getenv (ENV_DEBUG) != NULL)
  1828. wiringPiDebug = TRUE ;
  1829. if (getenv (ENV_CODES) != NULL)
  1830. wiringPiReturnCodes = TRUE ;
  1831. if (wiringPiDebug)
  1832. printf ("wiringPi: wiringPiSetup called\n") ;
  1833. // Get the board ID information. We're not really using the information here,
  1834. // but it will give us information like the GPIO layout scheme (2 variants
  1835. // on the older 26-pin Pi's) and the GPIO peripheral base address.
  1836. // and if we're running on a compute module, then wiringPi pin numbers
  1837. // don't really many anything, so force native BCM mode anyway.
  1838. piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
  1839. if ((model == PI_MODEL_CM) ||
  1840. (model == PI_MODEL_CM3) ||
  1841. (model == PI_MODEL_CM3P))
  1842. wiringPiMode = WPI_MODE_GPIO ;
  1843. else
  1844. wiringPiMode = WPI_MODE_PINS ;
  1845. /**/ if (piGpioLayout () == 1) // A, B, Rev 1, 1.1
  1846. {
  1847. pinToGpio = pinToGpioR1 ;
  1848. physToGpio = physToGpioR1 ;
  1849. }
  1850. else // A2, B2, A+, B+, CM, Pi2, Pi3, Zero, Zero W, Zero 2 W
  1851. {
  1852. pinToGpio = pinToGpioR2 ;
  1853. physToGpio = physToGpioR2 ;
  1854. }
  1855. // ...
  1856. switch (model)
  1857. {
  1858. case PI_MODEL_A: case PI_MODEL_B:
  1859. case PI_MODEL_AP: case PI_MODEL_BP:
  1860. case PI_ALPHA: case PI_MODEL_CM:
  1861. case PI_MODEL_ZERO: case PI_MODEL_ZERO_W:
  1862. piGpioBase = GPIO_PERI_BASE_OLD ;
  1863. piGpioPupOffset = GPPUD ;
  1864. break ;
  1865. case PI_MODEL_4B:
  1866. case PI_MODEL_400:
  1867. case PI_MODEL_CM4:
  1868. piGpioBase = GPIO_PERI_BASE_2711 ;
  1869. piGpioPupOffset = GPPUPPDN0 ;
  1870. break ;
  1871. default:
  1872. piGpioBase = GPIO_PERI_BASE_2835 ;
  1873. piGpioPupOffset = GPPUD ;
  1874. break ;
  1875. }
  1876. // Open the master /dev/ memory control device
  1877. // Device strategy: December 2016:
  1878. // Try /dev/mem. If that fails, then
  1879. // try /dev/gpiomem. If that fails then game over.
  1880. if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC)) < 0)
  1881. {
  1882. if ((fd = open ("/dev/gpiomem", O_RDWR | O_SYNC | O_CLOEXEC) ) >= 0) // We're using gpiomem
  1883. {
  1884. piGpioBase = 0 ;
  1885. usingGpioMem = TRUE ;
  1886. }
  1887. else
  1888. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: Unable to open /dev/mem or /dev/gpiomem: %s.\n"
  1889. " Aborting your program because if it can not access the GPIO\n"
  1890. " hardware then it most certianly won't work\n"
  1891. " Try running with sudo?\n", strerror (errno)) ;
  1892. }
  1893. // Set the offsets into the memory interface.
  1894. GPIO_PADS = piGpioBase + 0x00100000 ;
  1895. GPIO_CLOCK_BASE = piGpioBase + 0x00101000 ;
  1896. GPIO_BASE = piGpioBase + 0x00200000 ;
  1897. GPIO_TIMER = piGpioBase + 0x0000B000 ;
  1898. GPIO_PWM = piGpioBase + 0x0020C000 ;
  1899. // Map the individual hardware components
  1900. // GPIO:
  1901. gpio = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_BASE) ;
  1902. if (gpio == MAP_FAILED)
  1903. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (GPIO) failed: %s\n", strerror (errno)) ;
  1904. // PWM
  1905. pwm = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PWM) ;
  1906. if (pwm == MAP_FAILED)
  1907. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PWM) failed: %s\n", strerror (errno)) ;
  1908. // Clock control (needed for PWM)
  1909. clk = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_CLOCK_BASE) ;
  1910. if (clk == MAP_FAILED)
  1911. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (CLOCK) failed: %s\n", strerror (errno)) ;
  1912. // The drive pads
  1913. pads = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PADS) ;
  1914. if (pads == MAP_FAILED)
  1915. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PADS) failed: %s\n", strerror (errno)) ;
  1916. // The system timer
  1917. timer = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_TIMER) ;
  1918. if (timer == MAP_FAILED)
  1919. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (TIMER) failed: %s\n", strerror (errno)) ;
  1920. // Set the timer to free-running, 1MHz.
  1921. // 0xF9 is 249, the timer divide is base clock / (divide+1)
  1922. // so base clock is 250MHz / 250 = 1MHz.
  1923. *(timer + TIMER_CONTROL) = 0x0000280 ;
  1924. *(timer + TIMER_PRE_DIV) = 0x00000F9 ;
  1925. timerIrqRaw = timer + TIMER_IRQ_RAW ;
  1926. // Export the base addresses for any external software that might need them
  1927. _wiringPiGpio = gpio ;
  1928. _wiringPiPwm = pwm ;
  1929. _wiringPiClk = clk ;
  1930. _wiringPiPads = pads ;
  1931. _wiringPiTimer = timer ;
  1932. initialiseEpoch () ;
  1933. return 0 ;
  1934. }
  1935. /*
  1936. * wiringPiSetupGpio:
  1937. * Must be called once at the start of your program execution.
  1938. *
  1939. * GPIO setup: Initialises the system into GPIO Pin mode and uses the
  1940. * memory mapped hardware directly.
  1941. *********************************************************************************
  1942. */
  1943. int wiringPiSetupGpio (void)
  1944. {
  1945. (void)wiringPiSetup () ;
  1946. if (wiringPiDebug)
  1947. printf ("wiringPi: wiringPiSetupGpio called\n") ;
  1948. wiringPiMode = WPI_MODE_GPIO ;
  1949. return 0 ;
  1950. }
  1951. /*
  1952. * wiringPiSetupPhys:
  1953. * Must be called once at the start of your program execution.
  1954. *
  1955. * Phys setup: Initialises the system into Physical Pin mode and uses the
  1956. * memory mapped hardware directly.
  1957. *********************************************************************************
  1958. */
  1959. int wiringPiSetupPhys (void)
  1960. {
  1961. (void)wiringPiSetup () ;
  1962. if (wiringPiDebug)
  1963. printf ("wiringPi: wiringPiSetupPhys called\n") ;
  1964. wiringPiMode = WPI_MODE_PHYS ;
  1965. return 0 ;
  1966. }
  1967. /*
  1968. * wiringPiSetupSys:
  1969. * Must be called once at the start of your program execution.
  1970. *
  1971. * Initialisation (again), however this time we are using the /sys/class/gpio
  1972. * interface to the GPIO systems - slightly slower, but always usable as
  1973. * a non-root user, assuming the devices are already exported and setup correctly.
  1974. */
  1975. int wiringPiSetupSys (void)
  1976. {
  1977. int pin ;
  1978. char fName [128] ;
  1979. if (wiringPiSetuped)
  1980. return 0 ;
  1981. wiringPiSetuped = TRUE ;
  1982. if (getenv (ENV_DEBUG) != NULL)
  1983. wiringPiDebug = TRUE ;
  1984. if (getenv (ENV_CODES) != NULL)
  1985. wiringPiReturnCodes = TRUE ;
  1986. if (wiringPiDebug)
  1987. printf ("wiringPi: wiringPiSetupSys called\n") ;
  1988. if (piGpioLayout () == 1)
  1989. {
  1990. pinToGpio = pinToGpioR1 ;
  1991. physToGpio = physToGpioR1 ;
  1992. }
  1993. else
  1994. {
  1995. pinToGpio = pinToGpioR2 ;
  1996. physToGpio = physToGpioR2 ;
  1997. }
  1998. // Open and scan the directory, looking for exported GPIOs, and pre-open
  1999. // the 'value' interface to speed things up for later
  2000. for (pin = 0 ; pin < 64 ; ++pin)
  2001. {
  2002. sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
  2003. sysFds [pin] = open (fName, O_RDWR) ;
  2004. }
  2005. initialiseEpoch () ;
  2006. wiringPiMode = WPI_MODE_GPIO_SYS ;
  2007. return 0 ;
  2008. }