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.
 
 
 
 
 

1520 lines
40 KiB

  1. /*
  2. * wiringPi:
  3. * Arduino compatable (ish) Wiring library for the Raspberry Pi
  4. * Copyright (c) 2012 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 <stdint.h>
  54. #include <stdlib.h>
  55. #include <ctype.h>
  56. #include <poll.h>
  57. #include <unistd.h>
  58. #include <errno.h>
  59. #include <string.h>
  60. #include <time.h>
  61. #include <fcntl.h>
  62. #include <pthread.h>
  63. #include <sys/time.h>
  64. #include <sys/mman.h>
  65. #include <sys/stat.h>
  66. #include <sys/wait.h>
  67. #include <sys/ioctl.h>
  68. #include "wiringPi.h"
  69. // Function stubs
  70. void (*pinMode) (int pin, int mode) ;
  71. int (*getAlt) (int pin) ;
  72. void (*pullUpDnControl) (int pin, int pud) ;
  73. void (*digitalWrite) (int pin, int value) ;
  74. void (*digitalWriteByte) (int value) ;
  75. void (*pwmWrite) (int pin, int value) ;
  76. void (*gpioClockSet) (int pin, int value) ;
  77. void (*setPadDrive) (int group, int value) ;
  78. int (*digitalRead) (int pin) ;
  79. int (*waitForInterrupt) (int pin, int mS) ;
  80. void (*pwmSetMode) (int mode) ;
  81. void (*pwmSetRange) (unsigned int range) ;
  82. void (*pwmSetClock) (int divisor) ;
  83. #ifndef TRUE
  84. #define TRUE (1==1)
  85. #define FALSE (1==2)
  86. #endif
  87. // BCM Magic
  88. #define BCM_PASSWORD 0x5A000000
  89. // The BCM2835 has 54 GPIO pins.
  90. // BCM2835 data sheet, Page 90 onwards.
  91. // There are 6 control registers, each control the functions of a block
  92. // of 10 pins.
  93. // Each control register has 10 sets of 3 bits per GPIO pin - the ALT values
  94. //
  95. // 000 = GPIO Pin X is an input
  96. // 001 = GPIO Pin X is an output
  97. // 100 = GPIO Pin X takes alternate function 0
  98. // 101 = GPIO Pin X takes alternate function 1
  99. // 110 = GPIO Pin X takes alternate function 2
  100. // 111 = GPIO Pin X takes alternate function 3
  101. // 011 = GPIO Pin X takes alternate function 4
  102. // 010 = GPIO Pin X takes alternate function 5
  103. //
  104. // So the 3 bits for port X are:
  105. // X / 10 + ((X % 10) * 3)
  106. // Port function select bits
  107. #define FSEL_INPT 0b000
  108. #define FSEL_OUTP 0b001
  109. #define FSEL_ALT0 0b100
  110. #define FSEL_ALT0 0b100
  111. #define FSEL_ALT1 0b101
  112. #define FSEL_ALT2 0b110
  113. #define FSEL_ALT3 0b111
  114. #define FSEL_ALT4 0b011
  115. #define FSEL_ALT5 0b010
  116. // Access from ARM Running Linux
  117. // Taken from Gert/Doms code. Some of this is not in the manual
  118. // that I can find )-:
  119. #define BCM2708_PERI_BASE 0x20000000
  120. #define GPIO_PADS (BCM2708_PERI_BASE + 0x00100000)
  121. #define CLOCK_BASE (BCM2708_PERI_BASE + 0x00101000)
  122. #define GPIO_BASE (BCM2708_PERI_BASE + 0x00200000)
  123. #define GPIO_TIMER (BCM2708_PERI_BASE + 0x0000B000)
  124. #define GPIO_PWM (BCM2708_PERI_BASE + 0x0020C000)
  125. #define PAGE_SIZE (4*1024)
  126. #define BLOCK_SIZE (4*1024)
  127. // PWM
  128. // Word offsets into the PWM control region
  129. #define PWM_CONTROL 0
  130. #define PWM_STATUS 1
  131. #define PWM0_RANGE 4
  132. #define PWM0_DATA 5
  133. #define PWM1_RANGE 8
  134. #define PWM1_DATA 9
  135. // Clock regsiter offsets
  136. #define PWMCLK_CNTL 40
  137. #define PWMCLK_DIV 41
  138. #define PWM0_MS_MODE 0x0080 // Run in MS mode
  139. #define PWM0_USEFIFO 0x0020 // Data from FIFO
  140. #define PWM0_REVPOLAR 0x0010 // Reverse polarity
  141. #define PWM0_OFFSTATE 0x0008 // Ouput Off state
  142. #define PWM0_REPEATFF 0x0004 // Repeat last value if FIFO empty
  143. #define PWM0_SERIAL 0x0002 // Run in serial mode
  144. #define PWM0_ENABLE 0x0001 // Channel Enable
  145. #define PWM1_MS_MODE 0x8000 // Run in MS mode
  146. #define PWM1_USEFIFO 0x2000 // Data from FIFO
  147. #define PWM1_REVPOLAR 0x1000 // Reverse polarity
  148. #define PWM1_OFFSTATE 0x0800 // Ouput Off state
  149. #define PWM1_REPEATFF 0x0400 // Repeat last value if FIFO empty
  150. #define PWM1_SERIAL 0x0200 // Run in serial mode
  151. #define PWM1_ENABLE 0x0100 // Channel Enable
  152. // Timer
  153. // Word offsets
  154. #define TIMER_LOAD (0x400 >> 2)
  155. #define TIMER_VALUE (0x404 >> 2)
  156. #define TIMER_CONTROL (0x408 >> 2)
  157. #define TIMER_IRQ_CLR (0x40C >> 2)
  158. #define TIMER_IRQ_RAW (0x410 >> 2)
  159. #define TIMER_IRQ_MASK (0x414 >> 2)
  160. #define TIMER_RELOAD (0x418 >> 2)
  161. #define TIMER_PRE_DIV (0x41C >> 2)
  162. #define TIMER_COUNTER (0x420 >> 2)
  163. // Locals to hold pointers to the hardware
  164. static volatile uint32_t *gpio ;
  165. static volatile uint32_t *pwm ;
  166. static volatile uint32_t *clk ;
  167. static volatile uint32_t *pads ;
  168. static volatile uint32_t *timer ;
  169. static volatile uint32_t *timerIrqRaw ;
  170. // Time for easy calculations
  171. static uint64_t epochMilli, epochMicro ;
  172. // Misc
  173. static int wiringPiMode = WPI_MODE_UNINITIALISED ;
  174. // Debugging
  175. int wiringPiDebug = FALSE ;
  176. // sysFds:
  177. // Map a file descriptor from the /sys/class/gpio/gpioX/value
  178. static int sysFds [64] ;
  179. // ISR Data
  180. static void (*isrFunctions [64])(void) ;
  181. // Doing it the Arduino way with lookup tables...
  182. // Yes, it's probably more innefficient than all the bit-twidling, but it
  183. // does tend to make it all a bit clearer. At least to me!
  184. // pinToGpio:
  185. // Take a Wiring pin (0 through X) and re-map it to the BCM_GPIO pin
  186. // Cope for 2 different board revieions here
  187. static int *pinToGpio ;
  188. static int pinToGpioR1 [64] =
  189. {
  190. 17, 18, 21, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7
  191. 0, 1, // I2C - SDA0, SCL0
  192. 8, 7, // SPI - CE1, CE0
  193. 10, 9, 11, // SPI - MOSI, MISO, SCLK
  194. 14, 15, // UART - Tx, Rx
  195. // Padding:
  196. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 31
  197. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
  198. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
  199. } ;
  200. static int pinToGpioR2 [64] =
  201. {
  202. 17, 18, 27, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7: wpi 0 - 7
  203. 2, 3, // I2C - SDA0, SCL0 wpi 8 - 9
  204. 8, 7, // SPI - CE1, CE0 wpi 10 - 11
  205. 10, 9, 11, // SPI - MOSI, MISO, SCLK wpi 12 - 14
  206. 14, 15, // UART - Tx, Rx wpi 15 - 16
  207. 28, 29, 30, 31, // New GPIOs 8 though 11 wpi 17 - 20
  208. // Padding:
  209. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 31
  210. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
  211. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
  212. } ;
  213. // gpioToGPFSEL:
  214. // Map a BCM_GPIO pin to it's control port. (GPFSEL 0-5)
  215. static uint8_t gpioToGPFSEL [] =
  216. {
  217. 0,0,0,0,0,0,0,0,0,0,
  218. 1,1,1,1,1,1,1,1,1,1,
  219. 2,2,2,2,2,2,2,2,2,2,
  220. 3,3,3,3,3,3,3,3,3,3,
  221. 4,4,4,4,4,4,4,4,4,4,
  222. 5,5,5,5,5,5,5,5,5,5,
  223. } ;
  224. // gpioToShift
  225. // Define the shift up for the 3 bits per pin in each GPFSEL port
  226. static uint8_t gpioToShift [] =
  227. {
  228. 0,3,6,9,12,15,18,21,24,27,
  229. 0,3,6,9,12,15,18,21,24,27,
  230. 0,3,6,9,12,15,18,21,24,27,
  231. 0,3,6,9,12,15,18,21,24,27,
  232. 0,3,6,9,12,15,18,21,24,27,
  233. } ;
  234. // gpioToGPSET:
  235. // (Word) offset to the GPIO Set registers for each GPIO pin
  236. static uint8_t gpioToGPSET [] =
  237. {
  238. 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,
  239. 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,
  240. } ;
  241. // gpioToGPCLR:
  242. // (Word) offset to the GPIO Clear registers for each GPIO pin
  243. static uint8_t gpioToGPCLR [] =
  244. {
  245. 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,
  246. 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,
  247. } ;
  248. // gpioToGPLEV:
  249. // (Word) offset to the GPIO Input level registers for each GPIO pin
  250. static uint8_t gpioToGPLEV [] =
  251. {
  252. 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,
  253. 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,
  254. } ;
  255. #ifdef notYetReady
  256. // gpioToEDS
  257. // (Word) offset to the Event Detect Status
  258. static uint8_t gpioToEDS [] =
  259. {
  260. 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,
  261. 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,
  262. } ;
  263. // gpioToREN
  264. // (Word) offset to the Rising edgde ENable register
  265. static uint8_t gpioToREN [] =
  266. {
  267. 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,
  268. 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,
  269. } ;
  270. // gpioToFEN
  271. // (Word) offset to the Falling edgde ENable register
  272. static uint8_t gpioToFEN [] =
  273. {
  274. 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,
  275. 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,
  276. } ;
  277. #endif
  278. // GPPUD:
  279. // GPIO Pin pull up/down register
  280. #define GPPUD 37
  281. // gpioToPUDCLK
  282. // (Word) offset to the Pull Up Down Clock regsiter
  283. static uint8_t gpioToPUDCLK [] =
  284. {
  285. 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,
  286. 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,
  287. } ;
  288. // gpioToPwmALT
  289. // the ALT value to put a GPIO pin into PWM mode
  290. static uint8_t gpioToPwmALT [] =
  291. {
  292. 0, 0, 0, 0, 0, 0, 0, 0, // 0 -> 7
  293. 0, 0, 0, 0, FSEL_ALT0, FSEL_ALT0, 0, 0, // 8 -> 15
  294. 0, 0, FSEL_ALT5, FSEL_ALT5, 0, 0, 0, 0, // 16 -> 23
  295. 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
  296. 0, 0, 0, 0, 0, 0, 0, 0, // 32 -> 39
  297. FSEL_ALT0, FSEL_ALT0, 0, 0, 0, FSEL_ALT0, 0, 0, // 40 -> 47
  298. 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
  299. 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
  300. } ;
  301. // gpioToPwmPort
  302. // The port value to put a GPIO pin into PWM mode
  303. static uint8_t gpioToPwmPort [] =
  304. {
  305. 0, 0, 0, 0, 0, 0, 0, 0, // 0 -> 7
  306. 0, 0, 0, 0, PWM0_DATA, PWM1_DATA, 0, 0, // 8 -> 15
  307. 0, 0, PWM0_DATA, PWM1_DATA, 0, 0, 0, 0, // 16 -> 23
  308. 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
  309. 0, 0, 0, 0, 0, 0, 0, 0, // 32 -> 39
  310. PWM0_DATA, PWM1_DATA, 0, 0, 0, PWM1_DATA, 0, 0, // 40 -> 47
  311. 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
  312. 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
  313. } ;
  314. // gpioToGpClkALT:
  315. // ALT value to put a GPIO pin into GP Clock mode.
  316. // On the Pi we can really only use BCM_GPIO_4 and BCM_GPIO_21
  317. // for clocks 0 and 1 respectivey, however I'll include the full
  318. // list for completeness - maybe one day...
  319. #define GPIO_CLOCK_SOURCE 1
  320. // gpioToGpClkALT0:
  321. static uint8_t gpioToGpClkALT0 [] =
  322. {
  323. 0, 0, 0, 0, FSEL_ALT0, FSEL_ALT0, FSEL_ALT0, 0, // 0 -> 7
  324. 0, 0, 0, 0, 0, 0, 0, 0, // 8 -> 15
  325. 0, 0, 0, 0, FSEL_ALT5, FSEL_ALT5, 0, 0, // 16 -> 23
  326. 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
  327. FSEL_ALT0, 0, FSEL_ALT0, 0, 0, 0, 0, 0, // 32 -> 39
  328. 0, 0, FSEL_ALT0, FSEL_ALT0, FSEL_ALT0, 0, 0, 0, // 40 -> 47
  329. 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
  330. 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
  331. } ;
  332. // gpioToClk:
  333. // (word) Offsets to the clock Control and Divisor register
  334. static uint8_t gpioToClkCon [] =
  335. {
  336. -1, -1, -1, -1, 28, 30, 32, -1, // 0 -> 7
  337. -1, -1, -1, -1, -1, -1, -1, -1, // 8 -> 15
  338. -1, -1, -1, -1, 28, 30, -1, -1, // 16 -> 23
  339. -1, -1, -1, -1, -1, -1, -1, -1, // 24 -> 31
  340. 28, -1, 28, -1, -1, -1, -1, -1, // 32 -> 39
  341. -1, -1, 28, 30, 28, -1, -1, -1, // 40 -> 47
  342. -1, -1, -1, -1, -1, -1, -1, -1, // 48 -> 55
  343. -1, -1, -1, -1, -1, -1, -1, -1, // 56 -> 63
  344. } ;
  345. static uint8_t gpioToClkDiv [] =
  346. {
  347. -1, -1, -1, -1, 29, 31, 33, -1, // 0 -> 7
  348. -1, -1, -1, -1, -1, -1, -1, -1, // 8 -> 15
  349. -1, -1, -1, -1, 29, 31, -1, -1, // 16 -> 23
  350. -1, -1, -1, -1, -1, -1, -1, -1, // 24 -> 31
  351. 29, -1, 29, -1, -1, -1, -1, -1, // 32 -> 39
  352. -1, -1, 29, 31, 29, -1, -1, -1, // 40 -> 47
  353. -1, -1, -1, -1, -1, -1, -1, -1, // 48 -> 55
  354. -1, -1, -1, -1, -1, -1, -1, -1, // 56 -> 63
  355. } ;
  356. /*
  357. * Functions
  358. *********************************************************************************
  359. */
  360. /*
  361. * wpiPinToGpio:
  362. * Translate a wiringPi Pin number to native GPIO pin number.
  363. * (We don't use this here, prefering to just do the lookup directly,
  364. * but it's been requested!)
  365. *********************************************************************************
  366. */
  367. int wpiPinToGpio (int wpiPin)
  368. {
  369. return pinToGpio [wpiPin & 63] ;
  370. }
  371. /*
  372. * piBoardRev:
  373. * Return a number representing the hardware revision of the board.
  374. * Revision is currently 1 or 2. -1 is returned on error.
  375. *
  376. * Much confusion here )-:
  377. * Seems there are some boards with 0000 in them (mistake in manufacture)
  378. * and some board with 0005 in them (another mistake in manufacture?)
  379. * So the distinction between boards that I can see is:
  380. * 0000 - Error
  381. * 0001 - Not used
  382. * 0002 - Rev 1
  383. * 0003 - Rev 1
  384. * 0004 - Rev 2 (Early reports?
  385. * 0005 - Rev 2 (but error?)
  386. * 0006 - Rev 2
  387. * 0008 - Rev 2 - Model A
  388. * 000e - Rev 2 + 512MB
  389. * 000f - Rev 2 + 512MB
  390. *
  391. * A small thorn is the olde style overvolting - that will add in
  392. * 1000000
  393. *
  394. *********************************************************************************
  395. */
  396. static void piBoardRevOops (char *why)
  397. {
  398. fprintf (stderr, "piBoardRev: Unable to determine board revision from /proc/cpuinfo\n") ;
  399. fprintf (stderr, " -> %s\n", why) ;
  400. fprintf (stderr, " -> You may want to check:\n") ;
  401. fprintf (stderr, " -> http://www.raspberrypi.org/phpBB3/viewtopic.php?p=184410#p184410\n") ;
  402. exit (EXIT_FAILURE) ;
  403. }
  404. int piBoardRev (void)
  405. {
  406. FILE *cpuFd ;
  407. char line [120] ;
  408. char *c, lastChar ;
  409. static int boardRev = -1 ;
  410. if (boardRev != -1) // No point checking twice
  411. return boardRev ;
  412. if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL)
  413. piBoardRevOops ("Unable to open /proc/cpuinfo") ;
  414. while (fgets (line, 120, cpuFd) != NULL)
  415. if (strncmp (line, "Revision", 8) == 0)
  416. break ;
  417. fclose (cpuFd) ;
  418. if (strncmp (line, "Revision", 8) != 0)
  419. piBoardRevOops ("No \"Revision\" line") ;
  420. for (c = &line [strlen (line) - 1] ; (*c == '\n') || (*c == '\r') ; --c)
  421. *c = 0 ;
  422. if (wiringPiDebug)
  423. printf ("piboardRev: Revision string: %s\n", line) ;
  424. for (c = line ; *c ; ++c)
  425. if (isdigit (*c))
  426. break ;
  427. if (!isdigit (*c))
  428. piBoardRevOops ("No numeric revision string") ;
  429. // If you have overvolted the Pi, then it appears that the revision
  430. // has 100000 added to it!
  431. if (wiringPiDebug)
  432. if (strlen (c) != 4)
  433. printf ("piboardRev: This Pi has/is overvolted!\n") ;
  434. lastChar = line [strlen (line) - 1] ;
  435. if (wiringPiDebug)
  436. printf ("piboardRev: lastChar is: '%c' (%d, 0x%02X)\n", lastChar, lastChar, lastChar) ;
  437. /**/ if ((lastChar == '2') || (lastChar == '3'))
  438. boardRev = 1 ;
  439. else
  440. boardRev = 2 ;
  441. if (wiringPiDebug)
  442. printf ("piBoardRev: Returning revision: %d\n", boardRev) ;
  443. return boardRev ;
  444. }
  445. /*
  446. * getAlt:
  447. * Returns the ALT bits for a given port. Only really of-use
  448. * for the gpio readall command (I think)
  449. *********************************************************************************
  450. */
  451. int getAltGpio (int pin)
  452. {
  453. int fSel, shift, alt ;
  454. pin &= 63 ;
  455. fSel = gpioToGPFSEL [pin] ;
  456. shift = gpioToShift [pin] ;
  457. alt = (*(gpio + fSel) >> shift) & 7 ;
  458. return alt ;
  459. }
  460. int getAltWPi (int pin)
  461. {
  462. return getAltGpio (pinToGpio [pin & 63]) ;
  463. }
  464. int getAltSys (int pin)
  465. {
  466. return 0 ;
  467. }
  468. /*
  469. * pwmControl:
  470. * Allow the user to control some of the PWM functions
  471. *********************************************************************************
  472. */
  473. void pwmSetModeWPi (int mode)
  474. {
  475. if (mode == PWM_MODE_MS)
  476. *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE | PWM0_MS_MODE | PWM1_MS_MODE ;
  477. else
  478. *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ;
  479. }
  480. void pwmSetModeSys (int mode)
  481. {
  482. return ;
  483. }
  484. void pwmSetRangeWPi (unsigned int range)
  485. {
  486. *(pwm + PWM0_RANGE) = range ; delayMicroseconds (10) ;
  487. *(pwm + PWM1_RANGE) = range ; delayMicroseconds (10) ;
  488. }
  489. void pwmSetRangeSys (unsigned int range)
  490. {
  491. return ;
  492. }
  493. /*
  494. * pwmSetClockWPi:
  495. * Set/Change the PWM clock. Originally my code, but changed
  496. * (for the better!) by Chris Hall, <chris@kchall.plus.com>
  497. * after further study of the manual and testing with a 'scope
  498. *********************************************************************************
  499. */
  500. void pwmSetClockWPi (int divisor)
  501. {
  502. uint32_t pwm_control ;
  503. divisor &= 4095 ;
  504. if (wiringPiDebug)
  505. printf ("Setting to: %d. Current: 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;
  506. pwm_control = *(pwm + PWM_CONTROL) ; // preserve PWM_CONTROL
  507. // We need to stop PWM prior to stopping PWM clock in MS mode otherwise BUSY
  508. // stays high.
  509. *(pwm + PWM_CONTROL) = 0 ; // Stop PWM
  510. // Stop PWM clock before changing divisor. The delay after this does need to
  511. // this big (95uS occasionally fails, 100uS OK), it's almost as though the BUSY
  512. // flag is not working properly in balanced mode. Without the delay when DIV is
  513. // adjusted the clock sometimes switches to very slow, once slow further DIV
  514. // adjustments do nothing and it's difficult to get out of this mode.
  515. *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x01 ; // Stop PWM Clock
  516. delayMicroseconds (110) ; // prevents clock going sloooow
  517. while ((*(clk + PWMCLK_CNTL) & 0x80) != 0) // Wait for clock to be !BUSY
  518. delayMicroseconds (1) ;
  519. *(clk + PWMCLK_DIV) = BCM_PASSWORD | (divisor << 12) ;
  520. *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x11 ; // Start PWM clock
  521. *(pwm + PWM_CONTROL) = pwm_control ; // restore PWM_CONTROL
  522. if (wiringPiDebug)
  523. printf ("Set to: %d. Now : 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;
  524. }
  525. void pwmSetClockSys (int divisor)
  526. {
  527. return ;
  528. }
  529. #ifdef notYetReady
  530. /*
  531. * pinED01:
  532. * pinED10:
  533. * Enables edge-detect mode on a pin - from a 0 to a 1 or 1 to 0
  534. * Pin must already be in input mode with appropriate pull up/downs set.
  535. *********************************************************************************
  536. */
  537. void pinEnableED01Pi (int pin)
  538. {
  539. pin = pinToGpio [pin & 63] ;
  540. }
  541. #endif
  542. /*
  543. * digitalWrite:
  544. * Set an output bit
  545. *********************************************************************************
  546. */
  547. void digitalWriteWPi (int pin, int value)
  548. {
  549. pin = pinToGpio [pin & 63] ;
  550. if (value == LOW)
  551. *(gpio + gpioToGPCLR [pin]) = 1 << (pin & 31) ;
  552. else
  553. *(gpio + gpioToGPSET [pin]) = 1 << (pin & 31) ;
  554. }
  555. void digitalWriteGpio (int pin, int value)
  556. {
  557. pin &= 63 ;
  558. if (value == LOW)
  559. *(gpio + gpioToGPCLR [pin]) = 1 << (pin & 31) ;
  560. else
  561. *(gpio + gpioToGPSET [pin]) = 1 << (pin & 31) ;
  562. }
  563. void digitalWriteSys (int pin, int value)
  564. {
  565. pin &= 63 ;
  566. if (sysFds [pin] != -1)
  567. {
  568. if (value == LOW)
  569. write (sysFds [pin], "0\n", 2) ;
  570. else
  571. write (sysFds [pin], "1\n", 2) ;
  572. }
  573. }
  574. /*
  575. * digitalWriteByte:
  576. * Write an 8-bit byte to the first 8 GPIO pins - try to do it as
  577. * fast as possible.
  578. * However it still needs 2 operations to set the bits, so any external
  579. * hardware must not rely on seeing a change as there will be a change
  580. * to set the outputs bits to zero, then another change to set the 1's
  581. *********************************************************************************
  582. */
  583. void digitalWriteByteGpio (int value)
  584. {
  585. uint32_t pinSet = 0 ;
  586. uint32_t pinClr = 0 ;
  587. int mask = 1 ;
  588. int pin ;
  589. for (pin = 0 ; pin < 8 ; ++pin)
  590. {
  591. if ((value & mask) == 0)
  592. pinClr |= (1 << pinToGpio [pin]) ;
  593. else
  594. pinSet |= (1 << pinToGpio [pin]) ;
  595. mask <<= 1 ;
  596. }
  597. *(gpio + gpioToGPCLR [0]) = pinClr ;
  598. *(gpio + gpioToGPSET [0]) = pinSet ;
  599. }
  600. void digitalWriteByteSys (int value)
  601. {
  602. int mask = 1 ;
  603. int pin ;
  604. for (pin = 0 ; pin < 8 ; ++pin)
  605. {
  606. digitalWriteSys (pinToGpio [pin], value & mask) ;
  607. mask <<= 1 ;
  608. }
  609. }
  610. /*
  611. * pwmWrite:
  612. * Set an output PWM value
  613. *********************************************************************************
  614. */
  615. void pwmWriteGpio (int pin, int value)
  616. {
  617. int port ;
  618. pin = pin & 63 ;
  619. port = gpioToPwmPort [pin] ;
  620. *(pwm + port) = value ;
  621. }
  622. void pwmWriteWPi (int pin, int value)
  623. {
  624. pwmWriteGpio (pinToGpio [pin & 63], value) ;
  625. }
  626. void pwmWriteSys (int pin, int value)
  627. {
  628. return ;
  629. }
  630. /*
  631. * gpioClockSet:
  632. * Set the freuency on a GPIO clock pin
  633. *********************************************************************************
  634. */
  635. void gpioClockSetGpio (int pin, int freq)
  636. {
  637. int divi, divr, divf ;
  638. pin &= 63 ;
  639. divi = 19200000 / freq ;
  640. divr = 19200000 % freq ;
  641. divf = (int)((double)divr * 4096.0 / 19200000.0) ;
  642. if (divi > 4095)
  643. divi = 4095 ;
  644. *(clk + gpioToClkCon [pin]) = BCM_PASSWORD | GPIO_CLOCK_SOURCE ; // Stop GPIO Clock
  645. while ((*(clk + gpioToClkCon [pin]) & 0x80) != 0) // ... and wait
  646. ;
  647. *(clk + gpioToClkDiv [pin]) = BCM_PASSWORD | (divi << 12) | divf ; // Set dividers
  648. *(clk + gpioToClkCon [pin]) = BCM_PASSWORD | 0x10 | GPIO_CLOCK_SOURCE ; // Start Clock
  649. }
  650. void gpioClockSetWPi (int pin, int freq)
  651. {
  652. gpioClockSetGpio (pinToGpio [pin & 63], freq) ;
  653. }
  654. void gpioClockSetSys (int pin, int freq)
  655. {
  656. return ;
  657. }
  658. /*
  659. * setPadDrive:
  660. * Set the PAD driver value
  661. *********************************************************************************
  662. */
  663. void setPadDriveWPi (int group, int value)
  664. {
  665. uint32_t wrVal ;
  666. if ((group < 0) || (group > 2))
  667. return ;
  668. wrVal = BCM_PASSWORD | 0x18 | (value & 7) ;
  669. *(pads + group + 11) = wrVal ;
  670. if (wiringPiDebug)
  671. {
  672. printf ("setPadDrive: Group: %d, value: %d (%08X)\n", group, value, wrVal) ;
  673. printf ("Read : %08X\n", *(pads + group + 11)) ;
  674. }
  675. }
  676. void setPadDriveGpio (int group, int value)
  677. {
  678. setPadDriveWPi (group, value) ;
  679. }
  680. void setPadDriveSys (int group, int value)
  681. {
  682. return ;
  683. }
  684. /*
  685. * digitalRead:
  686. * Read the value of a given Pin, returning HIGH or LOW
  687. *********************************************************************************
  688. */
  689. int digitalReadWPi (int pin)
  690. {
  691. pin = pinToGpio [pin & 63] ;
  692. if ((*(gpio + gpioToGPLEV [pin]) & (1 << (pin & 31))) != 0)
  693. return HIGH ;
  694. else
  695. return LOW ;
  696. }
  697. int digitalReadGpio (int pin)
  698. {
  699. pin &= 63 ;
  700. if ((*(gpio + gpioToGPLEV [pin]) & (1 << (pin & 31))) != 0)
  701. return HIGH ;
  702. else
  703. return LOW ;
  704. }
  705. int digitalReadSys (int pin)
  706. {
  707. char c ;
  708. pin &= 63 ;
  709. if (sysFds [pin] == -1)
  710. return 0 ;
  711. lseek (sysFds [pin], 0L, SEEK_SET) ;
  712. read (sysFds [pin], &c, 1) ;
  713. return (c == '0') ? 0 : 1 ;
  714. }
  715. /*
  716. * pullUpDownCtrl:
  717. * Control the internal pull-up/down resistors on a GPIO pin
  718. * The Arduino only has pull-ups and these are enabled by writing 1
  719. * to a port when in input mode - this paradigm doesn't quite apply
  720. * here though.
  721. *********************************************************************************
  722. */
  723. void pullUpDnControlGpio (int pin, int pud)
  724. {
  725. pin &= 63 ;
  726. pud &= 3 ;
  727. *(gpio + GPPUD) = pud ; delayMicroseconds (5) ;
  728. *(gpio + gpioToPUDCLK [pin]) = 1 << (pin & 31) ; delayMicroseconds (5) ;
  729. *(gpio + GPPUD) = 0 ; delayMicroseconds (5) ;
  730. *(gpio + gpioToPUDCLK [pin]) = 0 ; delayMicroseconds (5) ;
  731. }
  732. void pullUpDnControlWPi (int pin, int pud)
  733. {
  734. pullUpDnControlGpio (pinToGpio [pin & 63], pud) ;
  735. }
  736. void pullUpDnControlSys (int pin, int pud)
  737. {
  738. return ;
  739. }
  740. /*
  741. * pinMode:
  742. * Sets the mode of a pin to be input, output or PWM output
  743. *********************************************************************************
  744. */
  745. void pinModeGpio (int pin, int mode)
  746. {
  747. // register int barrier ;
  748. int fSel, shift, alt ;
  749. pin &= 63 ;
  750. fSel = gpioToGPFSEL [pin] ;
  751. shift = gpioToShift [pin] ;
  752. /**/ if (mode == INPUT)
  753. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) ; // Sets bits to zero = input
  754. else if (mode == OUTPUT)
  755. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (1 << shift) ;
  756. else if (mode == PWM_OUTPUT)
  757. {
  758. if ((alt = gpioToPwmALT [pin]) == 0) // Not a PWM pin
  759. return ;
  760. // Set pin to PWM mode
  761. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ;
  762. delayMicroseconds (110) ; // See comments in pwmSetClockWPi
  763. pwmSetModeWPi (PWM_MODE_BAL) ; // Pi default mode
  764. pwmSetRangeWPi (1024) ; // Default range of 1024
  765. pwmSetClockWPi (32) ; // 19.2 / 32 = 600KHz - Also starts the PWM
  766. }
  767. else if (mode == GPIO_CLOCK)
  768. {
  769. if ((alt = gpioToGpClkALT0 [pin]) == 0) // Not a GPIO_CLOCK pin
  770. return ;
  771. // Set pin to GPIO_CLOCK mode and set the clock frequency to 100KHz
  772. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ;
  773. delayMicroseconds (110) ;
  774. gpioClockSetGpio (pin, 100000) ;
  775. }
  776. }
  777. void pinModeWPi (int pin, int mode)
  778. {
  779. pinModeGpio (pinToGpio [pin & 63], mode) ;
  780. }
  781. void pinModeSys (int pin, int mode)
  782. {
  783. return ;
  784. }
  785. /*
  786. * waitForInterrupt:
  787. * Wait for Interrupt on a GPIO pin.
  788. * This is actually done via the /sys/class/gpio interface regardless of
  789. * the wiringPi access mode in-use. Maybe sometime it might get a better
  790. * way for a bit more efficiency.
  791. *********************************************************************************
  792. */
  793. int waitForInterruptSys (int pin, int mS)
  794. {
  795. int fd, x ;
  796. uint8_t c ;
  797. struct pollfd polls ;
  798. if ((fd = sysFds [pin & 63]) == -1)
  799. return -2 ;
  800. // Setup poll structure
  801. polls.fd = fd ;
  802. polls.events = POLLPRI ; // Urgent data!
  803. // Wait for it ...
  804. x = poll (&polls, 1, mS) ;
  805. // Do a dummy read to clear the interrupt
  806. // A one character read appars to be enough.
  807. (void)read (fd, &c, 1) ;
  808. return x ;
  809. }
  810. int waitForInterruptWPi (int pin, int mS)
  811. {
  812. return waitForInterruptSys (pinToGpio [pin & 63], mS) ;
  813. }
  814. int waitForInterruptGpio (int pin, int mS)
  815. {
  816. return waitForInterruptSys (pin, mS) ;
  817. }
  818. /*
  819. * interruptHandler:
  820. * This is a thread and gets started to wait for the interrupt we're
  821. * hoping to catch. It will call the user-function when the interrupt
  822. * fires.
  823. *********************************************************************************
  824. */
  825. static void *interruptHandler (void *arg)
  826. {
  827. int myPin = *(int *)arg ;
  828. (void)piHiPri (55) ; // Only effective if we run as root
  829. for (;;)
  830. if (waitForInterruptSys (myPin, -1) > 0)
  831. isrFunctions [myPin] () ;
  832. return NULL ;
  833. }
  834. /*
  835. * wiringPiISR:
  836. * Take the details and create an interrupt handler that will do a call-
  837. * back to the user supplied function.
  838. *********************************************************************************
  839. */
  840. int wiringPiISR (int pin, int mode, void (*function)(void))
  841. {
  842. pthread_t threadId ;
  843. char fName [64] ;
  844. char *modeS ;
  845. char pinS [8] ;
  846. pid_t pid ;
  847. int count, i ;
  848. uint8_t c ;
  849. pin &= 63 ;
  850. if (wiringPiMode == WPI_MODE_UNINITIALISED)
  851. {
  852. fprintf (stderr, "wiringPiISR: wiringPi has not been initialised. Unable to continue.\n") ;
  853. exit (EXIT_FAILURE) ;
  854. }
  855. else if (wiringPiMode == WPI_MODE_PINS)
  856. pin = pinToGpio [pin] ;
  857. // Now export the pin and set the right edge
  858. // We're going to use the gpio program to do this, so it assumes
  859. // a full installation of wiringPi. It's a bit 'clunky', but it
  860. // is a way that will work when we're running in "Sys" mode, as
  861. // a non-root user. (without sudo)
  862. if (mode != INT_EDGE_SETUP)
  863. {
  864. /**/ if (mode == INT_EDGE_FALLING)
  865. modeS = "falling" ;
  866. else if (mode == INT_EDGE_RISING)
  867. modeS = "rising" ;
  868. else
  869. modeS = "both" ;
  870. sprintf (pinS, "%d", pin) ;
  871. if ((pid = fork ()) < 0) // Fail
  872. return pid ;
  873. if (pid == 0) // Child, exec
  874. {
  875. execl ("/usr/local/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ;
  876. return -1 ; // Failure ...
  877. }
  878. else // Parent, wait
  879. wait (NULL) ;
  880. }
  881. // Now pre-open the /sys/class node - it may already be open if
  882. // we are in Sys mode, but this will do no harm.
  883. sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
  884. if ((sysFds [pin] = open (fName, O_RDWR)) < 0)
  885. return -1 ;
  886. // Clear any initial pending interrupt
  887. ioctl (sysFds [pin], FIONREAD, &count) ;
  888. for (i = 0 ; i < count ; ++i)
  889. read (sysFds [pin], &c, 1) ;
  890. isrFunctions [pin] = function ;
  891. pthread_create (&threadId, NULL, interruptHandler, &pin) ;
  892. delay (1) ;
  893. return 0 ;
  894. }
  895. /*
  896. * initialiseEpoch:
  897. * Initialise our start-of-time variable to be the current unix
  898. * time in milliseconds.
  899. *********************************************************************************
  900. */
  901. static void initialiseEpoch (void)
  902. {
  903. struct timeval tv ;
  904. gettimeofday (&tv, NULL) ;
  905. epochMilli = (uint64_t)tv.tv_sec * (uint64_t)1000 + (uint64_t)(tv.tv_usec / 1000) ;
  906. epochMicro = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)(tv.tv_usec) ;
  907. }
  908. /*
  909. * delay:
  910. * Wait for some number of milli seconds
  911. *********************************************************************************
  912. */
  913. void delay (unsigned int howLong)
  914. {
  915. struct timespec sleeper, dummy ;
  916. sleeper.tv_sec = (time_t)(howLong / 1000) ;
  917. sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ;
  918. nanosleep (&sleeper, &dummy) ;
  919. }
  920. /*
  921. * delayMicroseconds:
  922. * This is somewhat intersting. It seems that on the Pi, a single call
  923. * to nanosleep takes some 80 to 130 microseconds anyway, so while
  924. * obeying the standards (may take longer), it's not always what we
  925. * want!
  926. *
  927. * So what I'll do now is if the delay is less than 100uS we'll do it
  928. * in a hard loop, watching a built-in counter on the ARM chip. This is
  929. * somewhat sub-optimal in that it uses 100% CPU, something not an issue
  930. * in a microcontroller, but under a multi-tasking, multi-user OS, it's
  931. * wastefull, however we've no real choice )-:
  932. *
  933. * Plan B: It seems all might not be well with that plan, so changing it
  934. * to use gettimeofday () and poll on that instead...
  935. *********************************************************************************
  936. */
  937. void delayMicrosecondsHard (unsigned int howLong)
  938. {
  939. struct timeval tNow, tLong, tEnd ;
  940. gettimeofday (&tNow, NULL) ;
  941. tLong.tv_sec = howLong / 1000000 ;
  942. tLong.tv_usec = howLong % 1000000 ;
  943. timeradd (&tNow, &tLong, &tEnd) ;
  944. while (timercmp (&tNow, &tEnd, <))
  945. gettimeofday (&tNow, NULL) ;
  946. }
  947. void delayMicroseconds (unsigned int howLong)
  948. {
  949. struct timespec sleeper ;
  950. /**/ if (howLong == 0)
  951. return ;
  952. else if (howLong < 100)
  953. delayMicrosecondsHard (howLong) ;
  954. else
  955. {
  956. sleeper.tv_sec = 0 ;
  957. sleeper.tv_nsec = (long)(howLong * 1000) ;
  958. nanosleep (&sleeper, NULL) ;
  959. }
  960. }
  961. /*
  962. * millis:
  963. * Return a number of milliseconds as an unsigned int.
  964. *********************************************************************************
  965. */
  966. unsigned int millis (void)
  967. {
  968. struct timeval tv ;
  969. uint64_t now ;
  970. gettimeofday (&tv, NULL) ;
  971. now = (uint64_t)tv.tv_sec * (uint64_t)1000 + (uint64_t)(tv.tv_usec / 1000) ;
  972. return (uint32_t)(now - epochMilli) ;
  973. }
  974. /*
  975. * micros:
  976. * Return a number of microseconds as an unsigned int.
  977. *********************************************************************************
  978. */
  979. unsigned int micros (void)
  980. {
  981. struct timeval tv ;
  982. uint64_t now ;
  983. gettimeofday (&tv, NULL) ;
  984. now = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)tv.tv_usec ;
  985. return (uint32_t)(now - epochMicro) ;
  986. }
  987. /*
  988. * wiringPiSetup:
  989. * Must be called once at the start of your program execution.
  990. *
  991. * Default setup: Initialises the system into wiringPi Pin mode and uses the
  992. * memory mapped hardware directly.
  993. *********************************************************************************
  994. */
  995. int wiringPiSetup (void)
  996. {
  997. int fd ;
  998. int boardRev ;
  999. if (geteuid () != 0)
  1000. {
  1001. fprintf (stderr, "wiringPi:\n Must be root to call wiringPiSetup().\n (Did you forget sudo?)\n") ;
  1002. exit (EXIT_FAILURE) ;
  1003. }
  1004. if (getenv ("WIRINGPI_DEBUG") != NULL)
  1005. {
  1006. printf ("wiringPi: Debug mode enabled\n") ;
  1007. wiringPiDebug = TRUE ;
  1008. }
  1009. if (wiringPiDebug)
  1010. printf ("wiringPi: wiringPiSetup called\n") ;
  1011. pinMode = pinModeWPi ;
  1012. getAlt = getAltWPi ;
  1013. pullUpDnControl = pullUpDnControlWPi ;
  1014. digitalWrite = digitalWriteWPi ;
  1015. digitalWriteByte = digitalWriteByteGpio ; // Same code
  1016. gpioClockSet = gpioClockSetWPi ;
  1017. pwmWrite = pwmWriteWPi ;
  1018. setPadDrive = setPadDriveWPi ;
  1019. digitalRead = digitalReadWPi ;
  1020. waitForInterrupt = waitForInterruptWPi ;
  1021. pwmSetMode = pwmSetModeWPi ;
  1022. pwmSetRange = pwmSetRangeWPi ;
  1023. pwmSetClock = pwmSetClockWPi ;
  1024. boardRev = piBoardRev () ;
  1025. if (boardRev == 1)
  1026. pinToGpio = pinToGpioR1 ;
  1027. else
  1028. pinToGpio = pinToGpioR2 ;
  1029. // Open the master /dev/memory device
  1030. if ((fd = open ("/dev/mem", O_RDWR | O_SYNC) ) < 0)
  1031. {
  1032. if (wiringPiDebug)
  1033. {
  1034. int serr = errno ;
  1035. fprintf (stderr, "wiringPiSetup: Unable to open /dev/mem: %s\n", strerror (errno)) ;
  1036. errno = serr ;
  1037. }
  1038. return -1 ;
  1039. }
  1040. // GPIO:
  1041. gpio = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_BASE) ;
  1042. if ((int32_t)gpio == -1)
  1043. {
  1044. if (wiringPiDebug)
  1045. {
  1046. int serr = errno ;
  1047. fprintf (stderr, "wiringPiSetup: mmap failed: %s\n", strerror (errno)) ;
  1048. errno = serr ;
  1049. }
  1050. return -1 ;
  1051. }
  1052. // PWM
  1053. pwm = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PWM) ;
  1054. if ((int32_t)pwm == -1)
  1055. {
  1056. if (wiringPiDebug)
  1057. {
  1058. int serr = errno ;
  1059. fprintf (stderr, "wiringPiSetup: mmap failed (pwm): %s\n", strerror (errno)) ;
  1060. errno = serr ;
  1061. }
  1062. return -1 ;
  1063. }
  1064. // Clock control (needed for PWM)
  1065. clk = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, CLOCK_BASE) ;
  1066. if ((int32_t)clk == -1)
  1067. {
  1068. if (wiringPiDebug)
  1069. {
  1070. int serr = errno ;
  1071. fprintf (stderr, "wiringPiSetup: mmap failed (clk): %s\n", strerror (errno)) ;
  1072. errno = serr ;
  1073. }
  1074. return -1 ;
  1075. }
  1076. // The drive pads
  1077. pads = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PADS) ;
  1078. if ((int32_t)pads == -1)
  1079. {
  1080. if (wiringPiDebug)
  1081. {
  1082. int serr = errno ;
  1083. fprintf (stderr, "wiringPiSetup: mmap failed (pads): %s\n", strerror (errno)) ;
  1084. errno = serr ;
  1085. }
  1086. return -1 ;
  1087. }
  1088. // The system timer
  1089. timer = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_TIMER) ;
  1090. if ((int32_t)timer == -1)
  1091. {
  1092. if (wiringPiDebug)
  1093. {
  1094. int serr = errno ;
  1095. fprintf (stderr, "wiringPiSetup: mmap failed (timer): %s\n", strerror (errno)) ;
  1096. errno = serr ;
  1097. }
  1098. return -1 ;
  1099. }
  1100. // Set the timer to free-running, 1MHz.
  1101. // 0xF9 is 249, the timer divide is base clock / (divide+1)
  1102. // so base clock is 250MHz / 250 = 1MHz.
  1103. *(timer + TIMER_CONTROL) = 0x0000280 ;
  1104. *(timer + TIMER_PRE_DIV) = 0x00000F9 ;
  1105. timerIrqRaw = timer + TIMER_IRQ_RAW ;
  1106. initialiseEpoch () ;
  1107. wiringPiMode = WPI_MODE_PINS ;
  1108. return 0 ;
  1109. }
  1110. /*
  1111. * wiringPiSetupGpio:
  1112. * Must be called once at the start of your program execution.
  1113. *
  1114. * GPIO setup: Initialises the system into GPIO Pin mode and uses the
  1115. * memory mapped hardware directly.
  1116. *********************************************************************************
  1117. */
  1118. int wiringPiSetupGpio (void)
  1119. {
  1120. int x ;
  1121. if (geteuid () != 0)
  1122. {
  1123. fprintf (stderr, "Must be root to call wiringPiSetupGpio(). (Did you forget sudo?)\n") ;
  1124. exit (EXIT_FAILURE) ;
  1125. }
  1126. if ((x = wiringPiSetup ()) < 0)
  1127. return x ;
  1128. if (wiringPiDebug)
  1129. printf ("wiringPi: wiringPiSetupGpio called\n") ;
  1130. pinMode = pinModeGpio ;
  1131. getAlt = getAltGpio ;
  1132. pullUpDnControl = pullUpDnControlGpio ;
  1133. digitalWrite = digitalWriteGpio ;
  1134. digitalWriteByte = digitalWriteByteGpio ;
  1135. gpioClockSet = gpioClockSetGpio ;
  1136. pwmWrite = pwmWriteGpio ;
  1137. setPadDrive = setPadDriveGpio ;
  1138. digitalRead = digitalReadGpio ;
  1139. waitForInterrupt = waitForInterruptGpio ;
  1140. pwmSetMode = pwmSetModeWPi ;
  1141. pwmSetRange = pwmSetRangeWPi ;
  1142. pwmSetClock = pwmSetClockWPi ;
  1143. wiringPiMode = WPI_MODE_GPIO ;
  1144. return 0 ;
  1145. }
  1146. /*
  1147. * wiringPiSetupSys:
  1148. * Must be called once at the start of your program execution.
  1149. *
  1150. * Initialisation (again), however this time we are using the /sys/class/gpio
  1151. * interface to the GPIO systems - slightly slower, but always usable as
  1152. * a non-root user, assuming the devices are already exported and setup correctly.
  1153. */
  1154. int wiringPiSetupSys (void)
  1155. {
  1156. int boardRev ;
  1157. int pin ;
  1158. char fName [128] ;
  1159. if (getenv ("WIRINGPI_DEBUG") != NULL)
  1160. wiringPiDebug = TRUE ;
  1161. if (wiringPiDebug)
  1162. printf ("wiringPi: wiringPiSetupSys called\n") ;
  1163. pinMode = pinModeSys ;
  1164. getAlt = getAltSys ;
  1165. pullUpDnControl = pullUpDnControlSys ;
  1166. digitalWrite = digitalWriteSys ;
  1167. digitalWriteByte = digitalWriteByteSys ;
  1168. gpioClockSet = gpioClockSetSys ;
  1169. pwmWrite = pwmWriteSys ;
  1170. setPadDrive = setPadDriveSys ;
  1171. digitalRead = digitalReadSys ;
  1172. waitForInterrupt = waitForInterruptSys ;
  1173. pwmSetMode = pwmSetModeSys ;
  1174. pwmSetRange = pwmSetRangeSys ;
  1175. pwmSetClock = pwmSetClockSys ;
  1176. boardRev = piBoardRev () ;
  1177. if (boardRev == 1)
  1178. pinToGpio = pinToGpioR1 ;
  1179. else
  1180. pinToGpio = pinToGpioR2 ;
  1181. // Open and scan the directory, looking for exported GPIOs, and pre-open
  1182. // the 'value' interface to speed things up for later
  1183. for (pin = 0 ; pin < 64 ; ++pin)
  1184. {
  1185. sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
  1186. sysFds [pin] = open (fName, O_RDWR) ;
  1187. }
  1188. initialiseEpoch () ;
  1189. wiringPiMode = WPI_MODE_GPIO_SYS ;
  1190. return 0 ;
  1191. }