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.
 
 
 
 
 

2537 line
74 KiB

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