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.
 
 
 
 
 

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