Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 

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