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.
 
 
 
 
 

2044 lines
58 KiB

  1. /*
  2. * wiringPi:
  3. * Arduino look-a-like Wiring library for the Raspberry Pi
  4. * Copyright (c) 2012-2015 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 "softPwm.h"
  70. #include "softTone.h"
  71. #include "wiringPi.h"
  72. #ifndef TRUE
  73. #define TRUE (1==1)
  74. #define FALSE (1==2)
  75. #endif
  76. // Environment Variables
  77. #define ENV_DEBUG "WIRINGPI_DEBUG"
  78. #define ENV_CODES "WIRINGPI_CODES"
  79. // Mask for the bottom 64 pins which belong to the Raspberry Pi
  80. // The others are available for the other devices
  81. #define PI_GPIO_MASK (0xFFFFFFC0)
  82. struct wiringPiNodeStruct *wiringPiNodes = NULL ;
  83. // BCM Magic
  84. #define BCM_PASSWORD 0x5A000000
  85. // The BCM2835 has 54 GPIO pins.
  86. // BCM2835 data sheet, Page 90 onwards.
  87. // There are 6 control registers, each control the functions of a block
  88. // of 10 pins.
  89. // Each control register has 10 sets of 3 bits per GPIO pin - the ALT values
  90. //
  91. // 000 = GPIO Pin X is an input
  92. // 001 = GPIO Pin X is an output
  93. // 100 = GPIO Pin X takes alternate function 0
  94. // 101 = GPIO Pin X takes alternate function 1
  95. // 110 = GPIO Pin X takes alternate function 2
  96. // 111 = GPIO Pin X takes alternate function 3
  97. // 011 = GPIO Pin X takes alternate function 4
  98. // 010 = GPIO Pin X takes alternate function 5
  99. //
  100. // So the 3 bits for port X are:
  101. // X / 10 + ((X % 10) * 3)
  102. // Port function select bits
  103. #define FSEL_INPT 0b000
  104. #define FSEL_OUTP 0b001
  105. #define FSEL_ALT0 0b100
  106. #define FSEL_ALT1 0b101
  107. #define FSEL_ALT2 0b110
  108. #define FSEL_ALT3 0b111
  109. #define FSEL_ALT4 0b011
  110. #define FSEL_ALT5 0b010
  111. // Access from ARM Running Linux
  112. // Taken from Gert/Doms code. Some of this is not in the manual
  113. // that I can find )-:
  114. //
  115. // Updates in September 2015 - all now static variables (and apologies for the caps)
  116. // due to the Pi v2 and the new /dev/gpiomem interface
  117. static volatile unsigned int RASPBERRY_PI_PERI_BASE ;
  118. static volatile unsigned int GPIO_PADS ;
  119. static volatile unsigned int GPIO_CLOCK_BASE ;
  120. static volatile unsigned int GPIO_BASE ;
  121. static volatile unsigned int GPIO_TIMER ;
  122. static volatile unsigned int GPIO_PWM ;
  123. #define PAGE_SIZE (4*1024)
  124. #define BLOCK_SIZE (4*1024)
  125. // PWM
  126. // Word offsets into the PWM control region
  127. #define PWM_CONTROL 0
  128. #define PWM_STATUS 1
  129. #define PWM0_RANGE 4
  130. #define PWM0_DATA 5
  131. #define PWM1_RANGE 8
  132. #define PWM1_DATA 9
  133. // Clock regsiter offsets
  134. #define PWMCLK_CNTL 40
  135. #define PWMCLK_DIV 41
  136. #define PWM0_MS_MODE 0x0080 // Run in MS mode
  137. #define PWM0_USEFIFO 0x0020 // Data from FIFO
  138. #define PWM0_REVPOLAR 0x0010 // Reverse polarity
  139. #define PWM0_OFFSTATE 0x0008 // Ouput Off state
  140. #define PWM0_REPEATFF 0x0004 // Repeat last value if FIFO empty
  141. #define PWM0_SERIAL 0x0002 // Run in serial mode
  142. #define PWM0_ENABLE 0x0001 // Channel Enable
  143. #define PWM1_MS_MODE 0x8000 // Run in MS mode
  144. #define PWM1_USEFIFO 0x2000 // Data from FIFO
  145. #define PWM1_REVPOLAR 0x1000 // Reverse polarity
  146. #define PWM1_OFFSTATE 0x0800 // Ouput Off state
  147. #define PWM1_REPEATFF 0x0400 // Repeat last value if FIFO empty
  148. #define PWM1_SERIAL 0x0200 // Run in serial mode
  149. #define PWM1_ENABLE 0x0100 // Channel Enable
  150. // Timer
  151. // Word offsets
  152. #define TIMER_LOAD (0x400 >> 2)
  153. #define TIMER_VALUE (0x404 >> 2)
  154. #define TIMER_CONTROL (0x408 >> 2)
  155. #define TIMER_IRQ_CLR (0x40C >> 2)
  156. #define TIMER_IRQ_RAW (0x410 >> 2)
  157. #define TIMER_IRQ_MASK (0x414 >> 2)
  158. #define TIMER_RELOAD (0x418 >> 2)
  159. #define TIMER_PRE_DIV (0x41C >> 2)
  160. #define TIMER_COUNTER (0x420 >> 2)
  161. // Locals to hold pointers to the hardware
  162. static volatile uint32_t *gpio ;
  163. static volatile uint32_t *pwm ;
  164. static volatile uint32_t *clk ;
  165. static volatile uint32_t *pads ;
  166. #ifdef USE_TIMER
  167. static volatile uint32_t *timer ;
  168. static volatile uint32_t *timerIrqRaw ;
  169. #endif
  170. // Data for use with the boardId functions.
  171. // The order of entries here to correspond with the PI_MODEL_X
  172. // and PI_VERSION_X defines in wiringPi.h
  173. // Only intended for the gpio command - use at your own risk!
  174. static int piModel2 = FALSE ;
  175. const char *piModelNames [7] =
  176. {
  177. "Unknown",
  178. "Model A",
  179. "Model B",
  180. "Model B+",
  181. "Compute Module",
  182. "Model A+",
  183. "Model 2", // Quad Core
  184. } ;
  185. const char *piRevisionNames [5] =
  186. {
  187. "Unknown",
  188. "1",
  189. "1.1",
  190. "1.2",
  191. "2",
  192. } ;
  193. const char *piMakerNames [5] =
  194. {
  195. "Unknown",
  196. "Egoman",
  197. "Sony",
  198. "Qusda",
  199. "MBest",
  200. } ;
  201. // Time for easy calculations
  202. static uint64_t epochMilli, epochMicro ;
  203. // Misc
  204. static int wiringPiMode = WPI_MODE_UNINITIALISED ;
  205. static volatile int pinPass = -1 ;
  206. static pthread_mutex_t pinMutex ;
  207. // Debugging & Return codes
  208. int wiringPiDebug = FALSE ;
  209. int wiringPiReturnCodes = FALSE ;
  210. // sysFds:
  211. // Map a file descriptor from the /sys/class/gpio/gpioX/value
  212. static int sysFds [64] =
  213. {
  214. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  215. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  216. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  217. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  218. } ;
  219. // ISR Data
  220. static void (*isrFunctions [64])(void) ;
  221. // Doing it the Arduino way with lookup tables...
  222. // Yes, it's probably more innefficient than all the bit-twidling, but it
  223. // does tend to make it all a bit clearer. At least to me!
  224. // pinToGpio:
  225. // Take a Wiring pin (0 through X) and re-map it to the BCM_GPIO pin
  226. // Cope for 3 different board revisions here.
  227. static int *pinToGpio ;
  228. // Revision 1, 1.1:
  229. static int pinToGpioR1 [64] =
  230. {
  231. 17, 18, 21, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7: wpi 0 - 7
  232. 0, 1, // I2C - SDA1, SCL1 wpi 8 - 9
  233. 8, 7, // SPI - CE1, CE0 wpi 10 - 11
  234. 10, 9, 11, // SPI - MOSI, MISO, SCLK wpi 12 - 14
  235. 14, 15, // UART - Tx, Rx wpi 15 - 16
  236. // Padding:
  237. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 31
  238. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
  239. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
  240. } ;
  241. // Revision 2:
  242. static int pinToGpioR2 [64] =
  243. {
  244. 17, 18, 27, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7: wpi 0 - 7
  245. 2, 3, // I2C - SDA0, SCL0 wpi 8 - 9
  246. 8, 7, // SPI - CE1, CE0 wpi 10 - 11
  247. 10, 9, 11, // SPI - MOSI, MISO, SCLK wpi 12 - 14
  248. 14, 15, // UART - Tx, Rx wpi 15 - 16
  249. 28, 29, 30, 31, // Rev 2: New GPIOs 8 though 11 wpi 17 - 20
  250. 5, 6, 13, 19, 26, // B+ wpi 21, 22, 23, 24, 25
  251. 12, 16, 20, 21, // B+ wpi 26, 27, 28, 29
  252. 0, 1, // B+ wpi 30, 31
  253. // Padding:
  254. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
  255. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
  256. } ;
  257. // physToGpio:
  258. // Take a physical pin (1 through 26) and re-map it to the BCM_GPIO pin
  259. // Cope for 2 different board revisions here.
  260. // Also add in the P5 connector, so the P5 pins are 3,4,5,6, so 53,54,55,56
  261. static int *physToGpio ;
  262. static int physToGpioR1 [64] =
  263. {
  264. -1, // 0
  265. -1, -1, // 1, 2
  266. 0, -1,
  267. 1, -1,
  268. 4, 14,
  269. -1, 15,
  270. 17, 18,
  271. 21, -1,
  272. 22, 23,
  273. -1, 24,
  274. 10, -1,
  275. 9, 25,
  276. 11, 8,
  277. -1, 7, // 25, 26
  278. -1, -1, -1, -1, -1, // ... 31
  279. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
  280. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
  281. } ;
  282. static int physToGpioR2 [64] =
  283. {
  284. -1, // 0
  285. -1, -1, // 1, 2
  286. 2, -1,
  287. 3, -1,
  288. 4, 14,
  289. -1, 15,
  290. 17, 18,
  291. 27, -1,
  292. 22, 23,
  293. -1, 24,
  294. 10, -1,
  295. 9, 25,
  296. 11, 8,
  297. -1, 7, // 25, 26
  298. // B+
  299. 0, 1,
  300. 5, -1,
  301. 6, 12,
  302. 13, -1,
  303. 19, 16,
  304. 26, 20,
  305. -1, 21,
  306. // the P5 connector on the Rev 2 boards:
  307. -1, -1,
  308. -1, -1,
  309. -1, -1,
  310. -1, -1,
  311. -1, -1,
  312. 28, 29,
  313. 30, 31,
  314. -1, -1,
  315. -1, -1,
  316. -1, -1,
  317. -1, -1,
  318. } ;
  319. // gpioToGPFSEL:
  320. // Map a BCM_GPIO pin to it's Function Selection
  321. // control port. (GPFSEL 0-5)
  322. // Groups of 10 - 3 bits per Function - 30 bits per port
  323. static uint8_t gpioToGPFSEL [] =
  324. {
  325. 0,0,0,0,0,0,0,0,0,0,
  326. 1,1,1,1,1,1,1,1,1,1,
  327. 2,2,2,2,2,2,2,2,2,2,
  328. 3,3,3,3,3,3,3,3,3,3,
  329. 4,4,4,4,4,4,4,4,4,4,
  330. 5,5,5,5,5,5,5,5,5,5,
  331. } ;
  332. // gpioToShift
  333. // Define the shift up for the 3 bits per pin in each GPFSEL port
  334. static uint8_t gpioToShift [] =
  335. {
  336. 0,3,6,9,12,15,18,21,24,27,
  337. 0,3,6,9,12,15,18,21,24,27,
  338. 0,3,6,9,12,15,18,21,24,27,
  339. 0,3,6,9,12,15,18,21,24,27,
  340. 0,3,6,9,12,15,18,21,24,27,
  341. } ;
  342. // gpioToGPSET:
  343. // (Word) offset to the GPIO Set registers for each GPIO pin
  344. static uint8_t gpioToGPSET [] =
  345. {
  346. 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,
  347. 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,
  348. } ;
  349. // gpioToGPCLR:
  350. // (Word) offset to the GPIO Clear registers for each GPIO pin
  351. static uint8_t gpioToGPCLR [] =
  352. {
  353. 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,
  354. 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,
  355. } ;
  356. // gpioToGPLEV:
  357. // (Word) offset to the GPIO Input level registers for each GPIO pin
  358. static uint8_t gpioToGPLEV [] =
  359. {
  360. 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,
  361. 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,
  362. } ;
  363. #ifdef notYetReady
  364. // gpioToEDS
  365. // (Word) offset to the Event Detect Status
  366. static uint8_t gpioToEDS [] =
  367. {
  368. 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,
  369. 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,
  370. } ;
  371. // gpioToREN
  372. // (Word) offset to the Rising edge ENable register
  373. static uint8_t gpioToREN [] =
  374. {
  375. 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,
  376. 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,
  377. } ;
  378. // gpioToFEN
  379. // (Word) offset to the Falling edgde ENable register
  380. static uint8_t gpioToFEN [] =
  381. {
  382. 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,
  383. 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,
  384. } ;
  385. #endif
  386. // GPPUD:
  387. // GPIO Pin pull up/down register
  388. #define GPPUD 37
  389. // gpioToPUDCLK
  390. // (Word) offset to the Pull Up Down Clock regsiter
  391. static uint8_t gpioToPUDCLK [] =
  392. {
  393. 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,
  394. 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,
  395. } ;
  396. // gpioToPwmALT
  397. // the ALT value to put a GPIO pin into PWM mode
  398. static uint8_t gpioToPwmALT [] =
  399. {
  400. 0, 0, 0, 0, 0, 0, 0, 0, // 0 -> 7
  401. 0, 0, 0, 0, FSEL_ALT0, FSEL_ALT0, 0, 0, // 8 -> 15
  402. 0, 0, FSEL_ALT5, FSEL_ALT5, 0, 0, 0, 0, // 16 -> 23
  403. 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
  404. 0, 0, 0, 0, 0, 0, 0, 0, // 32 -> 39
  405. FSEL_ALT0, FSEL_ALT0, 0, 0, 0, FSEL_ALT0, 0, 0, // 40 -> 47
  406. 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
  407. 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
  408. } ;
  409. // gpioToPwmPort
  410. // The port value to put a GPIO pin into PWM mode
  411. static uint8_t gpioToPwmPort [] =
  412. {
  413. 0, 0, 0, 0, 0, 0, 0, 0, // 0 -> 7
  414. 0, 0, 0, 0, PWM0_DATA, PWM1_DATA, 0, 0, // 8 -> 15
  415. 0, 0, PWM0_DATA, PWM1_DATA, 0, 0, 0, 0, // 16 -> 23
  416. 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
  417. 0, 0, 0, 0, 0, 0, 0, 0, // 32 -> 39
  418. PWM0_DATA, PWM1_DATA, 0, 0, 0, PWM1_DATA, 0, 0, // 40 -> 47
  419. 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
  420. 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
  421. } ;
  422. // gpioToGpClkALT:
  423. // ALT value to put a GPIO pin into GP Clock mode.
  424. // On the Pi we can really only use BCM_GPIO_4 and BCM_GPIO_21
  425. // for clocks 0 and 1 respectively, however I'll include the full
  426. // list for completeness - maybe one day...
  427. #define GPIO_CLOCK_SOURCE 1
  428. // gpioToGpClkALT0:
  429. static uint8_t gpioToGpClkALT0 [] =
  430. {
  431. 0, 0, 0, 0, FSEL_ALT0, FSEL_ALT0, FSEL_ALT0, 0, // 0 -> 7
  432. 0, 0, 0, 0, 0, 0, 0, 0, // 8 -> 15
  433. 0, 0, 0, 0, FSEL_ALT5, FSEL_ALT5, 0, 0, // 16 -> 23
  434. 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
  435. FSEL_ALT0, 0, FSEL_ALT0, 0, 0, 0, 0, 0, // 32 -> 39
  436. 0, 0, FSEL_ALT0, FSEL_ALT0, FSEL_ALT0, 0, 0, 0, // 40 -> 47
  437. 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
  438. 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
  439. } ;
  440. // gpioToClk:
  441. // (word) Offsets to the clock Control and Divisor register
  442. static uint8_t gpioToClkCon [] =
  443. {
  444. -1, -1, -1, -1, 28, 30, 32, -1, // 0 -> 7
  445. -1, -1, -1, -1, -1, -1, -1, -1, // 8 -> 15
  446. -1, -1, -1, -1, 28, 30, -1, -1, // 16 -> 23
  447. -1, -1, -1, -1, -1, -1, -1, -1, // 24 -> 31
  448. 28, -1, 28, -1, -1, -1, -1, -1, // 32 -> 39
  449. -1, -1, 28, 30, 28, -1, -1, -1, // 40 -> 47
  450. -1, -1, -1, -1, -1, -1, -1, -1, // 48 -> 55
  451. -1, -1, -1, -1, -1, -1, -1, -1, // 56 -> 63
  452. } ;
  453. static uint8_t gpioToClkDiv [] =
  454. {
  455. -1, -1, -1, -1, 29, 31, 33, -1, // 0 -> 7
  456. -1, -1, -1, -1, -1, -1, -1, -1, // 8 -> 15
  457. -1, -1, -1, -1, 29, 31, -1, -1, // 16 -> 23
  458. -1, -1, -1, -1, -1, -1, -1, -1, // 24 -> 31
  459. 29, -1, 29, -1, -1, -1, -1, -1, // 32 -> 39
  460. -1, -1, 29, 31, 29, -1, -1, -1, // 40 -> 47
  461. -1, -1, -1, -1, -1, -1, -1, -1, // 48 -> 55
  462. -1, -1, -1, -1, -1, -1, -1, -1, // 56 -> 63
  463. } ;
  464. /*
  465. * Functions
  466. *********************************************************************************
  467. */
  468. /*
  469. * wiringPiFailure:
  470. * Fail. Or not.
  471. *********************************************************************************
  472. */
  473. int wiringPiFailure (int fatal, const char *message, ...)
  474. {
  475. va_list argp ;
  476. char buffer [1024] ;
  477. if (!fatal && wiringPiReturnCodes)
  478. return -1 ;
  479. va_start (argp, message) ;
  480. vsnprintf (buffer, 1023, message, argp) ;
  481. va_end (argp) ;
  482. fprintf (stderr, "%s", buffer) ;
  483. exit (EXIT_FAILURE) ;
  484. return 0 ;
  485. }
  486. /*
  487. * piBoardRev:
  488. * Return a number representing the hardware revision of the board.
  489. *
  490. * Revision 1 really means the early Model B's.
  491. * Revision 2 is everything else - it covers the B, B+ and CM.
  492. * ... and the Pi 2 - which is a B+ ++ ...
  493. *
  494. * Seems there are some boards with 0000 in them (mistake in manufacture)
  495. * So the distinction between boards that I can see is:
  496. * 0000 - Error
  497. * 0001 - Not used
  498. * 0002 - Model B, Rev 1, 256MB, Egoman
  499. * 0003 - Model B, Rev 1.1, 256MB, Egoman, Fuses/D14 removed.
  500. * 0004 - Model B, Rev 2, 256MB, Sony
  501. * 0005 - Model B, Rev 2, 256MB, Qisda
  502. * 0006 - Model B, Rev 2, 256MB, Egoman
  503. * 0007 - Model A, Rev 2, 256MB, Egoman
  504. * 0008 - Model A, Rev 2, 256MB, Sony
  505. * 0009 - Model A, Rev 2, 256MB, Qisda
  506. * 000d - Model B, Rev 2, 512MB, Egoman
  507. * 000e - Model B, Rev 2, 512MB, Sony
  508. * 000f - Model B, Rev 2, 512MB, Qisda
  509. * 0010 - Model B+, Rev 1.2, 512MB, Sony
  510. * 0011 - Pi CM, Rev 1.2, 512MB, Sony
  511. * 0012 - Model A+ Rev 1.2, 256MB, Sony
  512. * 0014 - Pi CM, Rev 1.1, 512MB, Sony (Actual Revision might be different)
  513. * 0015 - Model A+ Rev 1.1, 256MB, Sony
  514. *
  515. * For the Pi 2:
  516. * 0010 - Model 2, Rev 1.1, Quad Core, 1GB, Sony
  517. *
  518. * A small thorn is the olde style overvolting - that will add in
  519. * 1000000
  520. *
  521. * The Pi compute module has an revision of 0011 - since we only check the
  522. * last digit, then it's 1, therefore it'll default to not 2 or 3 for a
  523. * Rev 1, so will appear as a Rev 2. This is fine for the most part, but
  524. * we'll properly detect the Compute Module later and adjust accordingly.
  525. * And the next rev of the CN is 0014 ...
  526. *
  527. *********************************************************************************
  528. */
  529. static void piBoardRevOops (const char *why)
  530. {
  531. fprintf (stderr, "piBoardRev: Unable to determine board revision from /proc/cpuinfo\n") ;
  532. fprintf (stderr, " -> %s\n", why) ;
  533. fprintf (stderr, " -> You may want to check:\n") ;
  534. fprintf (stderr, " -> http://www.raspberrypi.org/phpBB3/viewtopic.php?p=184410#p184410\n") ;
  535. exit (EXIT_FAILURE) ;
  536. }
  537. int piBoardRev (void)
  538. {
  539. FILE *cpuFd ;
  540. char line [120] ;
  541. char *c ;
  542. static int boardRev = -1 ;
  543. if (boardRev != -1) // No point checking twice
  544. return boardRev ;
  545. if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL)
  546. piBoardRevOops ("Unable to open /proc/cpuinfo") ;
  547. // Start by looking for the Architecture, then we can look for a B2 revision....
  548. while (fgets (line, 120, cpuFd) != NULL)
  549. if (strncmp (line, "Hardware", 8) == 0)
  550. break ;
  551. if (strncmp (line, "Hardware", 8) != 0)
  552. piBoardRevOops ("No \"Hardware\" line") ;
  553. if (wiringPiDebug)
  554. printf ("piboardRev: Hardware: %s\n", line) ;
  555. // See if it's BCM2708 or BCM2709
  556. if (strstr (line, "BCM2709") != NULL)
  557. piModel2 = TRUE ;
  558. else if (strstr (line, "BCM2708") == NULL)
  559. {
  560. fprintf (stderr, "Unable to determine hardware version. I see: %s,\n", line) ;
  561. fprintf (stderr, " - expecting BCM2708 or BCM2709.\n") ;
  562. fprintf (stderr, "If this is a genuine Raspberry Pi then please report this\n") ;
  563. fprintf (stderr, "to projects@drogon.net. If this is not a Raspberry Pi then you\n") ;
  564. fprintf (stderr, "are on your own as wiringPi is designed to support the\n") ;
  565. fprintf (stderr, "Raspberry Pi ONLY.\n") ;
  566. exit (EXIT_FAILURE) ;
  567. }
  568. // Now do the rest of it as before
  569. rewind (cpuFd) ;
  570. while (fgets (line, 120, cpuFd) != NULL)
  571. if (strncmp (line, "Revision", 8) == 0)
  572. break ;
  573. fclose (cpuFd) ;
  574. if (strncmp (line, "Revision", 8) != 0)
  575. piBoardRevOops ("No \"Revision\" line") ;
  576. // Chomp trailing CR/NL
  577. for (c = &line [strlen (line) - 1] ; (*c == '\n') || (*c == '\r') ; --c)
  578. *c = 0 ;
  579. if (wiringPiDebug)
  580. printf ("piboardRev: Revision string: %s\n", line) ;
  581. // Scan to first digit
  582. for (c = line ; *c ; ++c)
  583. if (isdigit (*c))
  584. break ;
  585. if (!isdigit (*c))
  586. piBoardRevOops ("No numeric revision string") ;
  587. // Make sure its long enough
  588. if (strlen (c) < 4)
  589. piBoardRevOops ("Bogus \"Revision\" line (too small)") ;
  590. // If you have overvolted the Pi, then it appears that the revision
  591. // has 100000 added to it!
  592. // The actual condition for it being set is:
  593. // (force_turbo || current_limit_override || temp_limit>85) && over_voltage>0
  594. if (wiringPiDebug)
  595. if (strlen (c) != 4)
  596. printf ("piboardRev: This Pi has/is (force_turbo || current_limit_override || temp_limit>85) && over_voltage>0\n") ;
  597. // Isolate last 4 characters:
  598. c = c + strlen (c) - 4 ;
  599. if (wiringPiDebug)
  600. printf ("piboardRev: last4Chars are: \"%s\"\n", c) ;
  601. if ( (strcmp (c, "0002") == 0) || (strcmp (c, "0003") == 0))
  602. boardRev = 1 ;
  603. else
  604. boardRev = 2 ; // Covers everything else from the B revision 2 to the B+, the Pi v2 and CM's.
  605. if (wiringPiDebug)
  606. printf ("piBoardRev: Returning revision: %d\n", boardRev) ;
  607. return boardRev ;
  608. }
  609. /*
  610. * piBoardId:
  611. * Do more digging into the board revision string as above, but return
  612. * as much details as we can.
  613. * This is undocumented and really only intended for the GPIO command.
  614. * Use at your own risk!
  615. *
  616. * for Pi v2:
  617. * [USER:8] [NEW:1] [MEMSIZE:3] [MANUFACTURER:4] [PROCESSOR:4] [TYPE:8] [REV:4]
  618. * NEW 23: will be 1 for the new scheme, 0 for the old scheme
  619. * MEMSIZE 20: 0=256M 1=512M 2=1G
  620. * MANUFACTURER 16: 0=SONY 1=EGOMAN 2=EMBEST
  621. * PROCESSOR 12: 0=2835 1=2836
  622. * TYPE 04: 0=MODELA 1=MODELB 2=MODELA+ 3=MODELB+ 4=Pi2 MODEL B 5=ALPHA 6=CM
  623. * REV 00: 0=REV0 1=REV1 2=REV2
  624. *********************************************************************************
  625. */
  626. void piBoardId (int *model, int *rev, int *mem, int *maker, int *overVolted)
  627. {
  628. FILE *cpuFd ;
  629. char line [120] ;
  630. char *c ;
  631. // Will deal with the properly later on - for now, lets just get it going...
  632. // unsigned int modelNum ;
  633. (void)piBoardRev () ; // Call this first to make sure all's OK. Don't care about the result.
  634. if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL)
  635. piBoardRevOops ("Unable to open /proc/cpuinfo") ;
  636. while (fgets (line, 120, cpuFd) != NULL)
  637. if (strncmp (line, "Revision", 8) == 0)
  638. break ;
  639. fclose (cpuFd) ;
  640. if (strncmp (line, "Revision", 8) != 0)
  641. piBoardRevOops ("No \"Revision\" line") ;
  642. // Chomp trailing CR/NL
  643. for (c = &line [strlen (line) - 1] ; (*c == '\n') || (*c == '\r') ; --c)
  644. *c = 0 ;
  645. if (wiringPiDebug)
  646. printf ("piboardId: Revision string: %s\n", line) ;
  647. if (piModel2)
  648. {
  649. // Scan to the colon
  650. for (c = line ; *c ; ++c)
  651. if (*c == ':')
  652. break ;
  653. if (*c != ':')
  654. piBoardRevOops ("Bogus \"Revision\" line (no colon)") ;
  655. // modelNum = (unsigned int)strtol (++c, NULL, 16) ; // Hex number with no leading 0x
  656. *model = PI_MODEL_2 ;
  657. *rev = PI_VERSION_1_1 ;
  658. *mem = 1024 ;
  659. *maker = PI_MAKER_SONY ;
  660. }
  661. else
  662. {
  663. // Scan to first digit
  664. for (c = line ; *c ; ++c)
  665. if (isdigit (*c))
  666. break ;
  667. // Make sure its long enough
  668. if (strlen (c) < 4)
  669. piBoardRevOops ("Bogus \"Revision\" line (not long enough)") ;
  670. // If longer than 4, we'll assume it's been overvolted
  671. *overVolted = strlen (c) > 4 ;
  672. // Extract last 4 characters:
  673. c = c + strlen (c) - 4 ;
  674. // Fill out the replys as appropriate
  675. /**/ if (strcmp (c, "0002") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1 ; *mem = 256 ; *maker = PI_MAKER_EGOMAN ; }
  676. else if (strcmp (c, "0003") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_1 ; *mem = 256 ; *maker = PI_MAKER_EGOMAN ; }
  677. else if (strcmp (c, "0004") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_SONY ; }
  678. else if (strcmp (c, "0005") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_QISDA ; }
  679. else if (strcmp (c, "0006") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_EGOMAN ; }
  680. else if (strcmp (c, "0007") == 0) { *model = PI_MODEL_A ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_EGOMAN ; }
  681. else if (strcmp (c, "0008") == 0) { *model = PI_MODEL_A ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_SONY ; ; }
  682. else if (strcmp (c, "0009") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_QISDA ; }
  683. else if (strcmp (c, "000d") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 512 ; *maker = PI_MAKER_EGOMAN ; }
  684. else if (strcmp (c, "000e") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 512 ; *maker = PI_MAKER_SONY ; }
  685. else if (strcmp (c, "000f") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 512 ; *maker = PI_MAKER_EGOMAN ; }
  686. else if (strcmp (c, "0010") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 512 ; *maker = PI_MAKER_SONY ; }
  687. else if (strcmp (c, "0011") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_2 ; *mem = 512 ; *maker = PI_MAKER_SONY ; }
  688. else if (strcmp (c, "0012") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_2 ; *mem = 256 ; *maker = PI_MAKER_SONY ; }
  689. else if (strcmp (c, "0013") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 512 ; *maker = PI_MAKER_MBEST ; }
  690. else if (strcmp (c, "0014") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_2 ; *mem = 512 ; *maker = PI_MAKER_SONY ; }
  691. else if (strcmp (c, "0015") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_1 ; *mem = 256 ; *maker = PI_MAKER_SONY ; }
  692. else { *model = 0 ; *rev = 0 ; *mem = 0 ; *maker = 0 ; }
  693. }
  694. }
  695. /*
  696. * wpiPinToGpio:
  697. * Translate a wiringPi Pin number to native GPIO pin number.
  698. * Provided for external support.
  699. *********************************************************************************
  700. */
  701. int wpiPinToGpio (int wpiPin)
  702. {
  703. return pinToGpio [wpiPin & 63] ;
  704. }
  705. /*
  706. * physPinToGpio:
  707. * Translate a physical Pin number to native GPIO pin number.
  708. * Provided for external support.
  709. *********************************************************************************
  710. */
  711. int physPinToGpio (int physPin)
  712. {
  713. return physToGpio [physPin & 63] ;
  714. }
  715. /*
  716. * setPadDrive:
  717. * Set the PAD driver value
  718. *********************************************************************************
  719. */
  720. void setPadDrive (int group, int value)
  721. {
  722. uint32_t wrVal ;
  723. if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
  724. {
  725. if ((group < 0) || (group > 2))
  726. return ;
  727. wrVal = BCM_PASSWORD | 0x18 | (value & 7) ;
  728. *(pads + group + 11) = wrVal ;
  729. if (wiringPiDebug)
  730. {
  731. printf ("setPadDrive: Group: %d, value: %d (%08X)\n", group, value, wrVal) ;
  732. printf ("Read : %08X\n", *(pads + group + 11)) ;
  733. }
  734. }
  735. }
  736. /*
  737. * getAlt:
  738. * Returns the ALT bits for a given port. Only really of-use
  739. * for the gpio readall command (I think)
  740. *********************************************************************************
  741. */
  742. int getAlt (int pin)
  743. {
  744. int fSel, shift, alt ;
  745. pin &= 63 ;
  746. /**/ if (wiringPiMode == WPI_MODE_PINS)
  747. pin = pinToGpio [pin] ;
  748. else if (wiringPiMode == WPI_MODE_PHYS)
  749. pin = physToGpio [pin] ;
  750. else if (wiringPiMode != WPI_MODE_GPIO)
  751. return 0 ;
  752. fSel = gpioToGPFSEL [pin] ;
  753. shift = gpioToShift [pin] ;
  754. alt = (*(gpio + fSel) >> shift) & 7 ;
  755. return alt ;
  756. }
  757. /*
  758. * pwmSetMode:
  759. * Select the native "balanced" mode, or standard mark:space mode
  760. *********************************************************************************
  761. */
  762. void pwmSetMode (int mode)
  763. {
  764. if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
  765. {
  766. if (mode == PWM_MODE_MS)
  767. *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE | PWM0_MS_MODE | PWM1_MS_MODE ;
  768. else
  769. *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ;
  770. }
  771. }
  772. /*
  773. * pwmSetRange:
  774. * Set the PWM range register. We set both range registers to the same
  775. * value. If you want different in your own code, then write your own.
  776. *********************************************************************************
  777. */
  778. void pwmSetRange (unsigned int range)
  779. {
  780. if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
  781. {
  782. *(pwm + PWM0_RANGE) = range ; delayMicroseconds (10) ;
  783. *(pwm + PWM1_RANGE) = range ; delayMicroseconds (10) ;
  784. }
  785. }
  786. /*
  787. * pwmSetClock:
  788. * Set/Change the PWM clock. Originally my code, but changed
  789. * (for the better!) by Chris Hall, <chris@kchall.plus.com>
  790. * after further study of the manual and testing with a 'scope
  791. *********************************************************************************
  792. */
  793. void pwmSetClock (int divisor)
  794. {
  795. uint32_t pwm_control ;
  796. divisor &= 4095 ;
  797. if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
  798. {
  799. if (wiringPiDebug)
  800. printf ("Setting to: %d. Current: 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;
  801. pwm_control = *(pwm + PWM_CONTROL) ; // preserve PWM_CONTROL
  802. // We need to stop PWM prior to stopping PWM clock in MS mode otherwise BUSY
  803. // stays high.
  804. *(pwm + PWM_CONTROL) = 0 ; // Stop PWM
  805. // Stop PWM clock before changing divisor. The delay after this does need to
  806. // this big (95uS occasionally fails, 100uS OK), it's almost as though the BUSY
  807. // flag is not working properly in balanced mode. Without the delay when DIV is
  808. // adjusted the clock sometimes switches to very slow, once slow further DIV
  809. // adjustments do nothing and it's difficult to get out of this mode.
  810. *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x01 ; // Stop PWM Clock
  811. delayMicroseconds (110) ; // prevents clock going sloooow
  812. while ((*(clk + PWMCLK_CNTL) & 0x80) != 0) // Wait for clock to be !BUSY
  813. delayMicroseconds (1) ;
  814. *(clk + PWMCLK_DIV) = BCM_PASSWORD | (divisor << 12) ;
  815. *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x11 ; // Start PWM clock
  816. *(pwm + PWM_CONTROL) = pwm_control ; // restore PWM_CONTROL
  817. if (wiringPiDebug)
  818. printf ("Set to: %d. Now : 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;
  819. }
  820. }
  821. /*
  822. * gpioClockSet:
  823. * Set the freuency on a GPIO clock pin
  824. *********************************************************************************
  825. */
  826. void gpioClockSet (int pin, int freq)
  827. {
  828. int divi, divr, divf ;
  829. pin &= 63 ;
  830. /**/ if (wiringPiMode == WPI_MODE_PINS)
  831. pin = pinToGpio [pin] ;
  832. else if (wiringPiMode == WPI_MODE_PHYS)
  833. pin = physToGpio [pin] ;
  834. else if (wiringPiMode != WPI_MODE_GPIO)
  835. return ;
  836. divi = 19200000 / freq ;
  837. divr = 19200000 % freq ;
  838. divf = (int)((double)divr * 4096.0 / 19200000.0) ;
  839. if (divi > 4095)
  840. divi = 4095 ;
  841. *(clk + gpioToClkCon [pin]) = BCM_PASSWORD | GPIO_CLOCK_SOURCE ; // Stop GPIO Clock
  842. while ((*(clk + gpioToClkCon [pin]) & 0x80) != 0) // ... and wait
  843. ;
  844. *(clk + gpioToClkDiv [pin]) = BCM_PASSWORD | (divi << 12) | divf ; // Set dividers
  845. *(clk + gpioToClkCon [pin]) = BCM_PASSWORD | 0x10 | GPIO_CLOCK_SOURCE ; // Start Clock
  846. }
  847. /*
  848. * wiringPiFindNode:
  849. * Locate our device node
  850. *********************************************************************************
  851. */
  852. struct wiringPiNodeStruct *wiringPiFindNode (int pin)
  853. {
  854. struct wiringPiNodeStruct *node = wiringPiNodes ;
  855. while (node != NULL)
  856. if ((pin >= node->pinBase) && (pin <= node->pinMax))
  857. return node ;
  858. else
  859. node = node->next ;
  860. return NULL ;
  861. }
  862. /*
  863. * wiringPiNewNode:
  864. * Create a new GPIO node into the wiringPi handling system
  865. *********************************************************************************
  866. */
  867. static void pinModeDummy (struct wiringPiNodeStruct *node, int pin, int mode) { return ; }
  868. static void pullUpDnControlDummy (struct wiringPiNodeStruct *node, int pin, int pud) { return ; }
  869. static int digitalReadDummy (struct wiringPiNodeStruct *node, int pin) { return LOW ; }
  870. static void digitalWriteDummy (struct wiringPiNodeStruct *node, int pin, int value) { return ; }
  871. static void pwmWriteDummy (struct wiringPiNodeStruct *node, int pin, int value) { return ; }
  872. static int analogReadDummy (struct wiringPiNodeStruct *node, int pin) { return 0 ; }
  873. static void analogWriteDummy (struct wiringPiNodeStruct *node, int pin, int value) { return ; }
  874. struct wiringPiNodeStruct *wiringPiNewNode (int pinBase, int numPins)
  875. {
  876. int pin ;
  877. struct wiringPiNodeStruct *node ;
  878. // Minimum pin base is 64
  879. if (pinBase < 64)
  880. (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: pinBase of %d is < 64\n", pinBase) ;
  881. // Check all pins in-case there is overlap:
  882. for (pin = pinBase ; pin < (pinBase + numPins) ; ++pin)
  883. if (wiringPiFindNode (pin) != NULL)
  884. (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: Pin %d overlaps with existing definition\n", pin) ;
  885. node = (struct wiringPiNodeStruct *)calloc (sizeof (struct wiringPiNodeStruct), 1) ; // calloc zeros
  886. if (node == NULL)
  887. (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: Unable to allocate memory: %s\n", strerror (errno)) ;
  888. node->pinBase = pinBase ;
  889. node->pinMax = pinBase + numPins - 1 ;
  890. node->pinMode = pinModeDummy ;
  891. node->pullUpDnControl = pullUpDnControlDummy ;
  892. node->digitalRead = digitalReadDummy ;
  893. node->digitalWrite = digitalWriteDummy ;
  894. node->pwmWrite = pwmWriteDummy ;
  895. node->analogRead = analogReadDummy ;
  896. node->analogWrite = analogWriteDummy ;
  897. node->next = wiringPiNodes ;
  898. wiringPiNodes = node ;
  899. return node ;
  900. }
  901. #ifdef notYetReady
  902. /*
  903. * pinED01:
  904. * pinED10:
  905. * Enables edge-detect mode on a pin - from a 0 to a 1 or 1 to 0
  906. * Pin must already be in input mode with appropriate pull up/downs set.
  907. *********************************************************************************
  908. */
  909. void pinEnableED01Pi (int pin)
  910. {
  911. pin = pinToGpio [pin & 63] ;
  912. }
  913. #endif
  914. /*
  915. *********************************************************************************
  916. * Core Functions
  917. *********************************************************************************
  918. */
  919. /*
  920. * pinModeAlt:
  921. * This is an un-documented special to let you set any pin to any mode
  922. *********************************************************************************
  923. */
  924. void pinModeAlt (int pin, int mode)
  925. {
  926. int fSel, shift ;
  927. if ((pin & PI_GPIO_MASK) == 0) // On-board pin
  928. {
  929. /**/ if (wiringPiMode == WPI_MODE_PINS)
  930. pin = pinToGpio [pin] ;
  931. else if (wiringPiMode == WPI_MODE_PHYS)
  932. pin = physToGpio [pin] ;
  933. else if (wiringPiMode != WPI_MODE_GPIO)
  934. return ;
  935. fSel = gpioToGPFSEL [pin] ;
  936. shift = gpioToShift [pin] ;
  937. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | ((mode & 0x7) << shift) ;
  938. }
  939. }
  940. /*
  941. * pinMode:
  942. * Sets the mode of a pin to be input, output or PWM output
  943. *********************************************************************************
  944. */
  945. void pinMode (int pin, int mode)
  946. {
  947. int fSel, shift, alt ;
  948. struct wiringPiNodeStruct *node = wiringPiNodes ;
  949. int origPin = pin ;
  950. if ((pin & PI_GPIO_MASK) == 0) // On-board pin
  951. {
  952. /**/ if (wiringPiMode == WPI_MODE_PINS)
  953. pin = pinToGpio [pin] ;
  954. else if (wiringPiMode == WPI_MODE_PHYS)
  955. pin = physToGpio [pin] ;
  956. else if (wiringPiMode != WPI_MODE_GPIO)
  957. return ;
  958. softPwmStop (origPin) ;
  959. softToneStop (origPin) ;
  960. fSel = gpioToGPFSEL [pin] ;
  961. shift = gpioToShift [pin] ;
  962. /**/ if (mode == INPUT)
  963. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) ; // Sets bits to zero = input
  964. else if (mode == OUTPUT)
  965. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (1 << shift) ;
  966. else if (mode == SOFT_PWM_OUTPUT)
  967. softPwmCreate (origPin, 0, 100) ;
  968. else if (mode == SOFT_TONE_OUTPUT)
  969. softToneCreate (origPin) ;
  970. else if (mode == PWM_TONE_OUTPUT)
  971. {
  972. pinMode (origPin, PWM_OUTPUT) ; // Call myself to enable PWM mode
  973. pwmSetMode (PWM_MODE_MS) ;
  974. }
  975. else if (mode == PWM_OUTPUT)
  976. {
  977. if ((alt = gpioToPwmALT [pin]) == 0) // Not a hardware capable PWM pin
  978. return ;
  979. // Set pin to PWM mode
  980. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ;
  981. delayMicroseconds (110) ; // See comments in pwmSetClockWPi
  982. pwmSetMode (PWM_MODE_BAL) ; // Pi default mode
  983. pwmSetRange (1024) ; // Default range of 1024
  984. pwmSetClock (32) ; // 19.2 / 32 = 600KHz - Also starts the PWM
  985. }
  986. else if (mode == GPIO_CLOCK)
  987. {
  988. if ((alt = gpioToGpClkALT0 [pin]) == 0) // Not a GPIO_CLOCK pin
  989. return ;
  990. // Set pin to GPIO_CLOCK mode and set the clock frequency to 100KHz
  991. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ;
  992. delayMicroseconds (110) ;
  993. gpioClockSet (pin, 100000) ;
  994. }
  995. }
  996. else
  997. {
  998. if ((node = wiringPiFindNode (pin)) != NULL)
  999. node->pinMode (node, pin, mode) ;
  1000. return ;
  1001. }
  1002. }
  1003. /*
  1004. * pullUpDownCtrl:
  1005. * Control the internal pull-up/down resistors on a GPIO pin
  1006. * The Arduino only has pull-ups and these are enabled by writing 1
  1007. * to a port when in input mode - this paradigm doesn't quite apply
  1008. * here though.
  1009. *********************************************************************************
  1010. */
  1011. void pullUpDnControl (int pin, int pud)
  1012. {
  1013. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1014. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1015. {
  1016. /**/ if (wiringPiMode == WPI_MODE_PINS)
  1017. pin = pinToGpio [pin] ;
  1018. else if (wiringPiMode == WPI_MODE_PHYS)
  1019. pin = physToGpio [pin] ;
  1020. else if (wiringPiMode != WPI_MODE_GPIO)
  1021. return ;
  1022. *(gpio + GPPUD) = pud & 3 ; delayMicroseconds (5) ;
  1023. *(gpio + gpioToPUDCLK [pin]) = 1 << (pin & 31) ; delayMicroseconds (5) ;
  1024. *(gpio + GPPUD) = 0 ; delayMicroseconds (5) ;
  1025. *(gpio + gpioToPUDCLK [pin]) = 0 ; delayMicroseconds (5) ;
  1026. }
  1027. else // Extension module
  1028. {
  1029. if ((node = wiringPiFindNode (pin)) != NULL)
  1030. node->pullUpDnControl (node, pin, pud) ;
  1031. return ;
  1032. }
  1033. }
  1034. /*
  1035. * digitalRead:
  1036. * Read the value of a given Pin, returning HIGH or LOW
  1037. *********************************************************************************
  1038. */
  1039. int digitalRead (int pin)
  1040. {
  1041. char c ;
  1042. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1043. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1044. {
  1045. /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS) // Sys mode
  1046. {
  1047. if (sysFds [pin] == -1)
  1048. return LOW ;
  1049. lseek (sysFds [pin], 0L, SEEK_SET) ;
  1050. read (sysFds [pin], &c, 1) ;
  1051. return (c == '0') ? LOW : HIGH ;
  1052. }
  1053. else if (wiringPiMode == WPI_MODE_PINS)
  1054. pin = pinToGpio [pin] ;
  1055. else if (wiringPiMode == WPI_MODE_PHYS)
  1056. pin = physToGpio [pin] ;
  1057. else if (wiringPiMode != WPI_MODE_GPIO)
  1058. return LOW ;
  1059. if ((*(gpio + gpioToGPLEV [pin]) & (1 << (pin & 31))) != 0)
  1060. return HIGH ;
  1061. else
  1062. return LOW ;
  1063. }
  1064. else
  1065. {
  1066. if ((node = wiringPiFindNode (pin)) == NULL)
  1067. return LOW ;
  1068. return node->digitalRead (node, pin) ;
  1069. }
  1070. }
  1071. /*
  1072. * digitalWrite:
  1073. * Set an output bit
  1074. *********************************************************************************
  1075. */
  1076. void digitalWrite (int pin, int value)
  1077. {
  1078. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1079. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1080. {
  1081. /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS) // Sys mode
  1082. {
  1083. if (sysFds [pin] != -1)
  1084. {
  1085. if (value == LOW)
  1086. write (sysFds [pin], "0\n", 2) ;
  1087. else
  1088. write (sysFds [pin], "1\n", 2) ;
  1089. }
  1090. return ;
  1091. }
  1092. else if (wiringPiMode == WPI_MODE_PINS)
  1093. pin = pinToGpio [pin] ;
  1094. else if (wiringPiMode == WPI_MODE_PHYS)
  1095. pin = physToGpio [pin] ;
  1096. else if (wiringPiMode != WPI_MODE_GPIO)
  1097. return ;
  1098. if (value == LOW)
  1099. *(gpio + gpioToGPCLR [pin]) = 1 << (pin & 31) ;
  1100. else
  1101. *(gpio + gpioToGPSET [pin]) = 1 << (pin & 31) ;
  1102. }
  1103. else
  1104. {
  1105. if ((node = wiringPiFindNode (pin)) != NULL)
  1106. node->digitalWrite (node, pin, value) ;
  1107. }
  1108. }
  1109. /*
  1110. * pwmWrite:
  1111. * Set an output PWM value
  1112. *********************************************************************************
  1113. */
  1114. void pwmWrite (int pin, int value)
  1115. {
  1116. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1117. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1118. {
  1119. /**/ if (wiringPiMode == WPI_MODE_PINS)
  1120. pin = pinToGpio [pin] ;
  1121. else if (wiringPiMode == WPI_MODE_PHYS)
  1122. pin = physToGpio [pin] ;
  1123. else if (wiringPiMode != WPI_MODE_GPIO)
  1124. return ;
  1125. *(pwm + gpioToPwmPort [pin]) = value ;
  1126. }
  1127. else
  1128. {
  1129. if ((node = wiringPiFindNode (pin)) != NULL)
  1130. node->pwmWrite (node, pin, value) ;
  1131. }
  1132. }
  1133. /*
  1134. * analogRead:
  1135. * Read the analog value of a given Pin.
  1136. * There is no on-board Pi analog hardware,
  1137. * so this needs to go to a new node.
  1138. *********************************************************************************
  1139. */
  1140. int analogRead (int pin)
  1141. {
  1142. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1143. if ((node = wiringPiFindNode (pin)) == NULL)
  1144. return 0 ;
  1145. else
  1146. return node->analogRead (node, pin) ;
  1147. }
  1148. /*
  1149. * analogWrite:
  1150. * Write the analog value to the given Pin.
  1151. * There is no on-board Pi analog hardware,
  1152. * so this needs to go to a new node.
  1153. *********************************************************************************
  1154. */
  1155. void analogWrite (int pin, int value)
  1156. {
  1157. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1158. if ((node = wiringPiFindNode (pin)) == NULL)
  1159. return ;
  1160. node->analogWrite (node, pin, value) ;
  1161. }
  1162. /*
  1163. * pwmToneWrite:
  1164. * Pi Specific.
  1165. * Output the given frequency on the Pi's PWM pin
  1166. *********************************************************************************
  1167. */
  1168. void pwmToneWrite (int pin, int freq)
  1169. {
  1170. int range ;
  1171. if (freq == 0)
  1172. pwmWrite (pin, 0) ; // Off
  1173. else
  1174. {
  1175. range = 600000 / freq ;
  1176. pwmSetRange (range) ;
  1177. pwmWrite (pin, freq / 2) ;
  1178. }
  1179. }
  1180. /*
  1181. * digitalWriteByte:
  1182. * Pi Specific
  1183. * Write an 8-bit byte to the first 8 GPIO pins - try to do it as
  1184. * fast as possible.
  1185. * However it still needs 2 operations to set the bits, so any external
  1186. * hardware must not rely on seeing a change as there will be a change
  1187. * to set the outputs bits to zero, then another change to set the 1's
  1188. *********************************************************************************
  1189. */
  1190. void digitalWriteByte (int value)
  1191. {
  1192. uint32_t pinSet = 0 ;
  1193. uint32_t pinClr = 0 ;
  1194. int mask = 1 ;
  1195. int pin ;
  1196. /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS)
  1197. {
  1198. for (pin = 0 ; pin < 8 ; ++pin)
  1199. {
  1200. digitalWrite (pin, value & mask) ;
  1201. mask <<= 1 ;
  1202. }
  1203. return ;
  1204. }
  1205. else
  1206. {
  1207. for (pin = 0 ; pin < 8 ; ++pin)
  1208. {
  1209. if ((value & mask) == 0)
  1210. pinClr |= (1 << pinToGpio [pin]) ;
  1211. else
  1212. pinSet |= (1 << pinToGpio [pin]) ;
  1213. mask <<= 1 ;
  1214. }
  1215. *(gpio + gpioToGPCLR [0]) = pinClr ;
  1216. *(gpio + gpioToGPSET [0]) = pinSet ;
  1217. }
  1218. }
  1219. /*
  1220. * waitForInterrupt:
  1221. * Pi Specific.
  1222. * Wait for Interrupt on a GPIO pin.
  1223. * This is actually done via the /sys/class/gpio interface regardless of
  1224. * the wiringPi access mode in-use. Maybe sometime it might get a better
  1225. * way for a bit more efficiency.
  1226. *********************************************************************************
  1227. */
  1228. int waitForInterrupt (int pin, int mS)
  1229. {
  1230. int fd, x ;
  1231. uint8_t c ;
  1232. struct pollfd polls ;
  1233. /**/ if (wiringPiMode == WPI_MODE_PINS)
  1234. pin = pinToGpio [pin] ;
  1235. else if (wiringPiMode == WPI_MODE_PHYS)
  1236. pin = physToGpio [pin] ;
  1237. if ((fd = sysFds [pin]) == -1)
  1238. return -2 ;
  1239. // Setup poll structure
  1240. polls.fd = fd ;
  1241. polls.events = POLLPRI ; // Urgent data!
  1242. // Wait for it ...
  1243. x = poll (&polls, 1, mS) ;
  1244. // Do a dummy read to clear the interrupt
  1245. // A one character read appars to be enough.
  1246. // Followed by a seek to reset it.
  1247. (void)read (fd, &c, 1) ;
  1248. lseek (fd, 0, SEEK_SET) ;
  1249. return x ;
  1250. }
  1251. /*
  1252. * interruptHandler:
  1253. * This is a thread and gets started to wait for the interrupt we're
  1254. * hoping to catch. It will call the user-function when the interrupt
  1255. * fires.
  1256. *********************************************************************************
  1257. */
  1258. static void *interruptHandler (void *arg)
  1259. {
  1260. int myPin ;
  1261. (void)piHiPri (55) ; // Only effective if we run as root
  1262. myPin = pinPass ;
  1263. pinPass = -1 ;
  1264. for (;;)
  1265. if (waitForInterrupt (myPin, -1) > 0)
  1266. isrFunctions [myPin] () ;
  1267. return NULL ;
  1268. }
  1269. /*
  1270. * wiringPiISR:
  1271. * Pi Specific.
  1272. * Take the details and create an interrupt handler that will do a call-
  1273. * back to the user supplied function.
  1274. *********************************************************************************
  1275. */
  1276. int wiringPiISR (int pin, int mode, void (*function)(void))
  1277. {
  1278. pthread_t threadId ;
  1279. const char *modeS ;
  1280. char fName [64] ;
  1281. char pinS [8] ;
  1282. pid_t pid ;
  1283. int count, i ;
  1284. char c ;
  1285. int bcmGpioPin ;
  1286. if ((pin < 0) || (pin > 63))
  1287. return wiringPiFailure (WPI_FATAL, "wiringPiISR: pin must be 0-63 (%d)\n", pin) ;
  1288. /**/ if (wiringPiMode == WPI_MODE_UNINITIALISED)
  1289. return wiringPiFailure (WPI_FATAL, "wiringPiISR: wiringPi has not been initialised. Unable to continue.\n") ;
  1290. else if (wiringPiMode == WPI_MODE_PINS)
  1291. bcmGpioPin = pinToGpio [pin] ;
  1292. else if (wiringPiMode == WPI_MODE_PHYS)
  1293. bcmGpioPin = physToGpio [pin] ;
  1294. else
  1295. bcmGpioPin = pin ;
  1296. // Now export the pin and set the right edge
  1297. // We're going to use the gpio program to do this, so it assumes
  1298. // a full installation of wiringPi. It's a bit 'clunky', but it
  1299. // is a way that will work when we're running in "Sys" mode, as
  1300. // a non-root user. (without sudo)
  1301. if (mode != INT_EDGE_SETUP)
  1302. {
  1303. /**/ if (mode == INT_EDGE_FALLING)
  1304. modeS = "falling" ;
  1305. else if (mode == INT_EDGE_RISING)
  1306. modeS = "rising" ;
  1307. else
  1308. modeS = "both" ;
  1309. sprintf (pinS, "%d", bcmGpioPin) ;
  1310. if ((pid = fork ()) < 0) // Fail
  1311. return wiringPiFailure (WPI_FATAL, "wiringPiISR: fork failed: %s\n", strerror (errno)) ;
  1312. if (pid == 0) // Child, exec
  1313. {
  1314. /**/ if (access ("/usr/local/bin/gpio", X_OK) == 0)
  1315. {
  1316. execl ("/usr/local/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ;
  1317. return wiringPiFailure (WPI_FATAL, "wiringPiISR: execl failed: %s\n", strerror (errno)) ;
  1318. }
  1319. else if (access ("/usr/bin/gpio", X_OK) == 0)
  1320. {
  1321. execl ("/usr/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ;
  1322. return wiringPiFailure (WPI_FATAL, "wiringPiISR: execl failed: %s\n", strerror (errno)) ;
  1323. }
  1324. else
  1325. return wiringPiFailure (WPI_FATAL, "wiringPiISR: Can't find gpio program\n") ;
  1326. }
  1327. else // Parent, wait
  1328. wait (NULL) ;
  1329. }
  1330. // Now pre-open the /sys/class node - but it may already be open if
  1331. // we are in Sys mode...
  1332. if (sysFds [bcmGpioPin] == -1)
  1333. {
  1334. sprintf (fName, "/sys/class/gpio/gpio%d/value", bcmGpioPin) ;
  1335. if ((sysFds [bcmGpioPin] = open (fName, O_RDWR)) < 0)
  1336. return wiringPiFailure (WPI_FATAL, "wiringPiISR: unable to open %s: %s\n", fName, strerror (errno)) ;
  1337. }
  1338. // Clear any initial pending interrupt
  1339. ioctl (sysFds [bcmGpioPin], FIONREAD, &count) ;
  1340. for (i = 0 ; i < count ; ++i)
  1341. read (sysFds [bcmGpioPin], &c, 1) ;
  1342. isrFunctions [pin] = function ;
  1343. pthread_mutex_lock (&pinMutex) ;
  1344. pinPass = pin ;
  1345. pthread_create (&threadId, NULL, interruptHandler, NULL) ;
  1346. while (pinPass != -1)
  1347. delay (1) ;
  1348. pthread_mutex_unlock (&pinMutex) ;
  1349. return 0 ;
  1350. }
  1351. /*
  1352. * initialiseEpoch:
  1353. * Initialise our start-of-time variable to be the current unix
  1354. * time in milliseconds and microseconds.
  1355. *********************************************************************************
  1356. */
  1357. static void initialiseEpoch (void)
  1358. {
  1359. struct timeval tv ;
  1360. gettimeofday (&tv, NULL) ;
  1361. epochMilli = (uint64_t)tv.tv_sec * (uint64_t)1000 + (uint64_t)(tv.tv_usec / 1000) ;
  1362. epochMicro = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)(tv.tv_usec) ;
  1363. }
  1364. /*
  1365. * delay:
  1366. * Wait for some number of milliseconds
  1367. *********************************************************************************
  1368. */
  1369. void delay (unsigned int howLong)
  1370. {
  1371. struct timespec sleeper, dummy ;
  1372. sleeper.tv_sec = (time_t)(howLong / 1000) ;
  1373. sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ;
  1374. nanosleep (&sleeper, &dummy) ;
  1375. }
  1376. /*
  1377. * delayMicroseconds:
  1378. * This is somewhat intersting. It seems that on the Pi, a single call
  1379. * to nanosleep takes some 80 to 130 microseconds anyway, so while
  1380. * obeying the standards (may take longer), it's not always what we
  1381. * want!
  1382. *
  1383. * So what I'll do now is if the delay is less than 100uS we'll do it
  1384. * in a hard loop, watching a built-in counter on the ARM chip. This is
  1385. * somewhat sub-optimal in that it uses 100% CPU, something not an issue
  1386. * in a microcontroller, but under a multi-tasking, multi-user OS, it's
  1387. * wastefull, however we've no real choice )-:
  1388. *
  1389. * Plan B: It seems all might not be well with that plan, so changing it
  1390. * to use gettimeofday () and poll on that instead...
  1391. *********************************************************************************
  1392. */
  1393. void delayMicrosecondsHard (unsigned int howLong)
  1394. {
  1395. struct timeval tNow, tLong, tEnd ;
  1396. gettimeofday (&tNow, NULL) ;
  1397. tLong.tv_sec = howLong / 1000000 ;
  1398. tLong.tv_usec = howLong % 1000000 ;
  1399. timeradd (&tNow, &tLong, &tEnd) ;
  1400. while (timercmp (&tNow, &tEnd, <))
  1401. gettimeofday (&tNow, NULL) ;
  1402. }
  1403. void delayMicroseconds (unsigned int howLong)
  1404. {
  1405. struct timespec sleeper ;
  1406. unsigned int uSecs = howLong % 1000000 ;
  1407. unsigned int wSecs = howLong / 1000000 ;
  1408. /**/ if (howLong == 0)
  1409. return ;
  1410. else if (howLong < 100)
  1411. delayMicrosecondsHard (howLong) ;
  1412. else
  1413. {
  1414. sleeper.tv_sec = wSecs ;
  1415. sleeper.tv_nsec = (long)(uSecs * 1000L) ;
  1416. nanosleep (&sleeper, NULL) ;
  1417. }
  1418. }
  1419. /*
  1420. * millis:
  1421. * Return a number of milliseconds as an unsigned int.
  1422. *********************************************************************************
  1423. */
  1424. unsigned int millis (void)
  1425. {
  1426. struct timeval tv ;
  1427. uint64_t now ;
  1428. gettimeofday (&tv, NULL) ;
  1429. now = (uint64_t)tv.tv_sec * (uint64_t)1000 + (uint64_t)(tv.tv_usec / 1000) ;
  1430. return (uint32_t)(now - epochMilli) ;
  1431. }
  1432. /*
  1433. * micros:
  1434. * Return a number of microseconds as an unsigned int.
  1435. *********************************************************************************
  1436. */
  1437. unsigned int micros (void)
  1438. {
  1439. struct timeval tv ;
  1440. uint64_t now ;
  1441. gettimeofday (&tv, NULL) ;
  1442. now = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)tv.tv_usec ;
  1443. return (uint32_t)(now - epochMicro) ;
  1444. }
  1445. /*
  1446. * wiringPiSetup:
  1447. * Must be called once at the start of your program execution.
  1448. *
  1449. * Default setup: Initialises the system into wiringPi Pin mode and uses the
  1450. * memory mapped hardware directly.
  1451. *
  1452. * Changed now to revert to "gpio" mode if we're running on a Compute Module.
  1453. *********************************************************************************
  1454. */
  1455. int wiringPiSetup (void)
  1456. {
  1457. int fd ;
  1458. int boardRev ;
  1459. int model, rev, mem, maker, overVolted ;
  1460. if (getenv (ENV_DEBUG) != NULL)
  1461. wiringPiDebug = TRUE ;
  1462. if (getenv (ENV_CODES) != NULL)
  1463. wiringPiReturnCodes = TRUE ;
  1464. if (wiringPiDebug)
  1465. printf ("wiringPi: wiringPiSetup called\n") ;
  1466. boardRev = piBoardRev () ;
  1467. /**/ if (boardRev == 1) // A, B, Rev 1, 1.1
  1468. {
  1469. pinToGpio = pinToGpioR1 ;
  1470. physToGpio = physToGpioR1 ;
  1471. }
  1472. else // A, B, Rev 2, B+, CM, Pi2
  1473. {
  1474. pinToGpio = pinToGpioR2 ;
  1475. physToGpio = physToGpioR2 ;
  1476. }
  1477. if (piModel2)
  1478. RASPBERRY_PI_PERI_BASE = 0x3F000000 ;
  1479. else
  1480. RASPBERRY_PI_PERI_BASE = 0x20000000 ;
  1481. // Open the master /dev/ memory control device
  1482. // See if /dev/gpiomem exists and we can open it...
  1483. if ((fd = open ("/dev/gpiomem", O_RDWR | O_SYNC | O_CLOEXEC) ) >= 0)
  1484. RASPBERRY_PI_PERI_BASE = 0 ;
  1485. // ... otherwise fall back to the original /dev/mem which requires root level access
  1486. else
  1487. {
  1488. // This check is here because people are too stupid to check for themselves or read
  1489. // error messages.
  1490. if (geteuid () != 0)
  1491. (void)wiringPiFailure (WPI_FATAL, "wiringPiSetup: Must be root. (Did you forget sudo?)\n") ;
  1492. if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0)
  1493. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: Unable to open /dev/mem: %s\n", strerror (errno)) ;
  1494. }
  1495. // Set the offsets into the memory interface.
  1496. GPIO_PADS = RASPBERRY_PI_PERI_BASE + 0x00100000 ;
  1497. GPIO_CLOCK_BASE = RASPBERRY_PI_PERI_BASE + 0x00101000 ;
  1498. GPIO_BASE = RASPBERRY_PI_PERI_BASE + 0x00200000 ;
  1499. GPIO_TIMER = RASPBERRY_PI_PERI_BASE + 0x0000B000 ;
  1500. GPIO_PWM = RASPBERRY_PI_PERI_BASE + 0x0020C000 ;
  1501. // Map the individual hardware components
  1502. // GPIO:
  1503. gpio = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_BASE) ;
  1504. if ((int32_t)gpio == -1)
  1505. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (GPIO) failed: %s\n", strerror (errno)) ;
  1506. // PWM
  1507. pwm = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PWM) ;
  1508. if ((int32_t)pwm == -1)
  1509. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PWM) failed: %s\n", strerror (errno)) ;
  1510. // Clock control (needed for PWM)
  1511. clk = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_CLOCK_BASE) ;
  1512. if ((int32_t)clk == -1)
  1513. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (CLOCK) failed: %s\n", strerror (errno)) ;
  1514. // The drive pads
  1515. pads = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PADS) ;
  1516. if ((int32_t)pads == -1)
  1517. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PADS) failed: %s\n", strerror (errno)) ;
  1518. #ifdef USE_TIMER
  1519. // The system timer
  1520. timer = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_TIMER) ;
  1521. if ((int32_t)timer == -1)
  1522. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (TIMER) failed: %s\n", strerror (errno)) ;
  1523. // Set the timer to free-running, 1MHz.
  1524. // 0xF9 is 249, the timer divide is base clock / (divide+1)
  1525. // so base clock is 250MHz / 250 = 1MHz.
  1526. *(timer + TIMER_CONTROL) = 0x0000280 ;
  1527. *(timer + TIMER_PRE_DIV) = 0x00000F9 ;
  1528. timerIrqRaw = timer + TIMER_IRQ_RAW ;
  1529. #endif
  1530. initialiseEpoch () ;
  1531. // If we're running on a compute module, then wiringPi pin numbers don't really many anything...
  1532. piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
  1533. if (model == PI_MODEL_CM)
  1534. wiringPiMode = WPI_MODE_GPIO ;
  1535. else
  1536. wiringPiMode = WPI_MODE_PINS ;
  1537. return 0 ;
  1538. }
  1539. /*
  1540. * wiringPiSetupGpio:
  1541. * Must be called once at the start of your program execution.
  1542. *
  1543. * GPIO setup: Initialises the system into GPIO Pin mode and uses the
  1544. * memory mapped hardware directly.
  1545. *********************************************************************************
  1546. */
  1547. int wiringPiSetupGpio (void)
  1548. {
  1549. (void)wiringPiSetup () ;
  1550. if (wiringPiDebug)
  1551. printf ("wiringPi: wiringPiSetupGpio called\n") ;
  1552. wiringPiMode = WPI_MODE_GPIO ;
  1553. return 0 ;
  1554. }
  1555. /*
  1556. * wiringPiSetupPhys:
  1557. * Must be called once at the start of your program execution.
  1558. *
  1559. * Phys setup: Initialises the system into Physical Pin mode and uses the
  1560. * memory mapped hardware directly.
  1561. *********************************************************************************
  1562. */
  1563. int wiringPiSetupPhys (void)
  1564. {
  1565. (void)wiringPiSetup () ;
  1566. if (wiringPiDebug)
  1567. printf ("wiringPi: wiringPiSetupPhys called\n") ;
  1568. wiringPiMode = WPI_MODE_PHYS ;
  1569. return 0 ;
  1570. }
  1571. /*
  1572. * wiringPiSetupSys:
  1573. * Must be called once at the start of your program execution.
  1574. *
  1575. * Initialisation (again), however this time we are using the /sys/class/gpio
  1576. * interface to the GPIO systems - slightly slower, but always usable as
  1577. * a non-root user, assuming the devices are already exported and setup correctly.
  1578. */
  1579. int wiringPiSetupSys (void)
  1580. {
  1581. int boardRev ;
  1582. int pin ;
  1583. char fName [128] ;
  1584. if (getenv (ENV_DEBUG) != NULL)
  1585. wiringPiDebug = TRUE ;
  1586. if (getenv (ENV_CODES) != NULL)
  1587. wiringPiReturnCodes = TRUE ;
  1588. if (wiringPiDebug)
  1589. printf ("wiringPi: wiringPiSetupSys called\n") ;
  1590. boardRev = piBoardRev () ;
  1591. if (boardRev == 1)
  1592. {
  1593. pinToGpio = pinToGpioR1 ;
  1594. physToGpio = physToGpioR1 ;
  1595. }
  1596. else
  1597. {
  1598. pinToGpio = pinToGpioR2 ;
  1599. physToGpio = physToGpioR2 ;
  1600. }
  1601. // Open and scan the directory, looking for exported GPIOs, and pre-open
  1602. // the 'value' interface to speed things up for later
  1603. for (pin = 0 ; pin < 64 ; ++pin)
  1604. {
  1605. sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
  1606. sysFds [pin] = open (fName, O_RDWR) ;
  1607. }
  1608. initialiseEpoch () ;
  1609. wiringPiMode = WPI_MODE_GPIO_SYS ;
  1610. return 0 ;
  1611. }