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.
 
 
 
 
 

1200 regels
32 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. // Pad drive current fiddling
  53. #undef DEBUG_PADS
  54. #include <stdio.h>
  55. #include <stdint.h>
  56. #include <stdlib.h>
  57. #include <ctype.h>
  58. #include <poll.h>
  59. #include <unistd.h>
  60. #include <errno.h>
  61. #include <string.h>
  62. #include <time.h>
  63. #include <fcntl.h>
  64. #include <sys/time.h>
  65. #include <sys/mman.h>
  66. #include <sys/types.h>
  67. #include <sys/stat.h>
  68. #include "wiringPi.h"
  69. // Function stubs
  70. void (*pinMode) (int pin, int mode) ;
  71. void (*pullUpDnControl) (int pin, int pud) ;
  72. void (*digitalWrite) (int pin, int value) ;
  73. void (*pwmWrite) (int pin, int value) ;
  74. void (*setPadDrive) (int group, int value) ;
  75. int (*digitalRead) (int pin) ;
  76. int (*waitForInterrupt) (int pin, int mS) ;
  77. void (*delayMicroseconds) (unsigned int howLong) ;
  78. void (*pwmSetMode) (int mode) ;
  79. void (*pwmSetRange) (unsigned int range) ;
  80. void (*pwmSetClock) (int divisor) ;
  81. #ifndef TRUE
  82. #define TRUE (1==1)
  83. #define FALSE (1==2)
  84. #endif
  85. // BCM Magic
  86. #define BCM_PASSWORD 0x5A000000
  87. // Port function select bits
  88. #define FSEL_INPT 0b000
  89. #define FSEL_OUTP 0b001
  90. #define FSEL_ALT0 0b100
  91. #define FSEL_ALT0 0b100
  92. #define FSEL_ALT1 0b101
  93. #define FSEL_ALT2 0b110
  94. #define FSEL_ALT3 0b111
  95. #define FSEL_ALT4 0b011
  96. #define FSEL_ALT5 0b010
  97. // Access from ARM Running Linux
  98. // Take from Gert/Doms code. Some of this is not in the manual
  99. // that I can find )-:
  100. #define BCM2708_PERI_BASE 0x20000000
  101. #define GPIO_PADS (BCM2708_PERI_BASE + 0x100000)
  102. #define CLOCK_BASE (BCM2708_PERI_BASE + 0x101000)
  103. #define GPIO_BASE (BCM2708_PERI_BASE + 0x200000)
  104. #define GPIO_TIMER (BCM2708_PERI_BASE + 0x00B000)
  105. #define GPIO_PWM (BCM2708_PERI_BASE + 0x20C000)
  106. #define PAGE_SIZE (4*1024)
  107. #define BLOCK_SIZE (4*1024)
  108. // PWM
  109. #define PWM_CONTROL 0
  110. #define PWM_STATUS 1
  111. #define PWM0_RANGE 4
  112. #define PWM0_DATA 5
  113. #define PWM1_RANGE 8
  114. #define PWM1_DATA 9
  115. #define PWMCLK_CNTL 40
  116. #define PWMCLK_DIV 41
  117. #define PWM1_MS_MODE 0x8000 // Run in MS mode
  118. #define PWM1_USEFIFO 0x2000 // Data from FIFO
  119. #define PWM1_REVPOLAR 0x1000 // Reverse polarity
  120. #define PWM1_OFFSTATE 0x0800 // Ouput Off state
  121. #define PWM1_REPEATFF 0x0400 // Repeat last value if FIFO empty
  122. #define PWM1_SERIAL 0x0200 // Run in serial mode
  123. #define PWM1_ENABLE 0x0100 // Channel Enable
  124. #define PWM0_MS_MODE 0x0080 // Run in MS mode
  125. #define PWM0_USEFIFO 0x0020 // Data from FIFO
  126. #define PWM0_REVPOLAR 0x0010 // Reverse polarity
  127. #define PWM0_OFFSTATE 0x0008 // Ouput Off state
  128. #define PWM0_REPEATFF 0x0004 // Repeat last value if FIFO empty
  129. #define PWM0_SERIAL 0x0002 // Run in serial mode
  130. #define PWM0_ENABLE 0x0001 // Channel Enable
  131. // Timer
  132. #define TIMER_LOAD (0x400 >> 2)
  133. #define TIMER_VALUE (0x404 >> 2)
  134. #define TIMER_CONTROL (0x408 >> 2)
  135. #define TIMER_IRQ_CLR (0x40C >> 2)
  136. #define TIMER_IRQ_RAW (0x410 >> 2)
  137. #define TIMER_IRQ_MASK (0x414 >> 2)
  138. #define TIMER_RELOAD (0x418 >> 2)
  139. #define TIMER_PRE_DIV (0x41C >> 2)
  140. #define TIMER_COUNTER (0x420 >> 2)
  141. // Locals to hold pointers to the hardware
  142. static volatile uint32_t *gpio ;
  143. static volatile uint32_t *pwm ;
  144. static volatile uint32_t *clk ;
  145. static volatile uint32_t *pads ;
  146. static volatile uint32_t *timer ;
  147. static volatile uint32_t *timerIrqRaw ;
  148. // Debugging
  149. static int wiringPiDebug = FALSE ;
  150. // The BCM2835 has 54 GPIO pins.
  151. // BCM2835 data sheet, Page 90 onwards.
  152. // There are 6 control registers, each control the functions of a block
  153. // of 10 pins.
  154. // Each control register has 10 sets of 3 bits per GPIO pin:
  155. //
  156. // 000 = GPIO Pin X is an input
  157. // 001 = GPIO Pin X is an output
  158. // 100 = GPIO Pin X takes alternate function 0
  159. // 101 = GPIO Pin X takes alternate function 1
  160. // 110 = GPIO Pin X takes alternate function 2
  161. // 111 = GPIO Pin X takes alternate function 3
  162. // 011 = GPIO Pin X takes alternate function 4
  163. // 010 = GPIO Pin X takes alternate function 5
  164. //
  165. // So the 3 bits for port X are:
  166. // X / 10 + ((X % 10) * 3)
  167. // sysFds:
  168. // Map a file descriptor from the /sys/class/gpio/gpioX/value
  169. static int sysFds [64] ;
  170. // Doing it the Arduino way with lookup tables...
  171. // Yes, it's probably more innefficient than all the bit-twidling, but it
  172. // does tend to make it all a bit clearer. At least to me!
  173. // pinToGpio:
  174. // Take a Wiring pin (0 through X) and re-map it to the BCM_GPIO pin
  175. // Cope for 2 different board revieions here
  176. static int *pinToGpio ;
  177. static int pinToGpioR1 [64] =
  178. {
  179. 17, 18, 21, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7
  180. 0, 1, // I2C - SDA0, SCL0
  181. 8, 7, // SPI - CE1, CE0
  182. 10, 9, 11, // SPI - MOSI, MISO, SCLK
  183. 14, 15, // UART - Tx, Rx
  184. // Padding:
  185. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 31
  186. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
  187. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
  188. } ;
  189. static int pinToGpioR2 [64] =
  190. {
  191. 17, 18, 27, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7: wpi 0 - 7
  192. 2, 3, // I2C - SDA0, SCL0 wpi 8 - 9
  193. 8, 7, // SPI - CE1, CE0 wpi 10 - 11
  194. 10, 9, 11, // SPI - MOSI, MISO, SCLK wpi 12 - 14
  195. 14, 15, // UART - Tx, Rx wpi 15 - 16
  196. 28, 29, 30, 31, // New GPIOs 8 though 11 wpi 17 - 20
  197. // Padding:
  198. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 31
  199. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
  200. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
  201. } ;
  202. // gpioToGPFSEL:
  203. // Map a BCM_GPIO pin to it's control port. (GPFSEL 0-5)
  204. static uint8_t gpioToGPFSEL [] =
  205. {
  206. 0,0,0,0,0,0,0,0,0,0,
  207. 1,1,1,1,1,1,1,1,1,1,
  208. 2,2,2,2,2,2,2,2,2,2,
  209. 3,3,3,3,3,3,3,3,3,3,
  210. 4,4,4,4,4,4,4,4,4,4,
  211. 5,5,5,5,5,5,5,5,5,5,
  212. } ;
  213. // gpioToShift
  214. // Define the shift up for the 3 bits per pin in each GPFSEL port
  215. static uint8_t gpioToShift [] =
  216. {
  217. 0,3,6,9,12,15,18,21,24,27,
  218. 0,3,6,9,12,15,18,21,24,27,
  219. 0,3,6,9,12,15,18,21,24,27,
  220. 0,3,6,9,12,15,18,21,24,27,
  221. 0,3,6,9,12,15,18,21,24,27,
  222. } ;
  223. // gpioToGPSET:
  224. // (Word) offset to the GPIO Set registers for each GPIO pin
  225. static uint8_t gpioToGPSET [] =
  226. {
  227. 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,
  228. 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,
  229. } ;
  230. // gpioToGPCLR:
  231. // (Word) offset to the GPIO Clear registers for each GPIO pin
  232. static uint8_t gpioToGPCLR [] =
  233. {
  234. 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,
  235. 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,
  236. } ;
  237. // gpioToGPLEV:
  238. // (Word) offset to the GPIO Input level registers for each GPIO pin
  239. static uint8_t gpioToGPLEV [] =
  240. {
  241. 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,
  242. 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,
  243. } ;
  244. #ifdef notYetReady
  245. // gpioToEDS
  246. // (Word) offset to the Event Detect Status
  247. static uint8_t gpioToEDS [] =
  248. {
  249. 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,
  250. 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,
  251. } ;
  252. // gpioToREN
  253. // (Word) offset to the Rising edgde ENable register
  254. static uint8_t gpioToREN [] =
  255. {
  256. 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,
  257. 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,
  258. } ;
  259. // gpioToFEN
  260. // (Word) offset to the Falling edgde ENable register
  261. static uint8_t gpioToFEN [] =
  262. {
  263. 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,
  264. 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,
  265. } ;
  266. #endif
  267. // gpioToPUDCLK
  268. // (Word) offset to the Pull Up Down Clock regsiter
  269. #define GPPUD 37
  270. static uint8_t gpioToPUDCLK [] =
  271. {
  272. 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,
  273. 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,
  274. } ;
  275. // gpioToPwmALT
  276. // the ALT value to put a GPIO pin into PWM mode
  277. static uint8_t gpioToPwmALT [] =
  278. {
  279. 0, 0, 0, 0, 0, 0, 0, 0, // 0 -> 7
  280. 0, 0, 0, 0, FSEL_ALT0, FSEL_ALT0, 0, 0, // 8 -> 15
  281. 0, 0, FSEL_ALT5, FSEL_ALT5, 0, 0, 0, 0, // 16 -> 23
  282. 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
  283. 0, 0, 0, 0, 0, 0, 0, 0, // 32 -> 39
  284. FSEL_ALT0, FSEL_ALT0, 0, 0, 0, FSEL_ALT0, 0, 0, // 40 -> 47
  285. 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
  286. 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
  287. } ;
  288. static uint8_t gpioToPwmPort [] =
  289. {
  290. 0, 0, 0, 0, 0, 0, 0, 0, // 0 -> 7
  291. 0, 0, 0, 0, PWM0_DATA, PWM1_DATA, 0, 0, // 8 -> 15
  292. 0, 0, PWM0_DATA, PWM1_DATA, 0, 0, 0, 0, // 16 -> 23
  293. 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
  294. 0, 0, 0, 0, 0, 0, 0, 0, // 32 -> 39
  295. PWM0_DATA, PWM1_DATA, 0, 0, 0, PWM1_DATA, 0, 0, // 40 -> 47
  296. 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
  297. 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
  298. } ;
  299. // Time for easy calculations
  300. static unsigned long long epoch ;
  301. /*
  302. * Functions
  303. *********************************************************************************
  304. */
  305. /*
  306. * wpiPinToGpio:
  307. * Translate a wiringPi Pin number to native GPIO pin number.
  308. * (We don't use this here, prefering to just do the lookup directly,
  309. * but it's been requested!)
  310. *********************************************************************************
  311. */
  312. int wpiPinToGpio (int wpiPin)
  313. {
  314. return pinToGpio [wpiPin & 63] ;
  315. }
  316. /*
  317. * piBoardRev:
  318. * Return a number representing the hardware revision of the board.
  319. * Revision is currently 1 or 2. -1 is returned on error.
  320. *********************************************************************************
  321. */
  322. int piBoardRev (void)
  323. {
  324. FILE *cpuFd ;
  325. char line [80] ;
  326. char *c ;
  327. int r = -1 ;
  328. static int boardRev = -1 ;
  329. // No point checking twice...
  330. if (boardRev != -1)
  331. return boardRev ;
  332. if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL)
  333. return -1 ;
  334. while (fgets (line, 80, cpuFd) != NULL)
  335. if (strncmp (line, "Revision", 8) == 0)
  336. for (c = line ; *c ; ++c)
  337. {
  338. if (!isdigit (*c))
  339. continue ;
  340. r = atoi (c) ;
  341. break ;
  342. }
  343. fclose (cpuFd) ;
  344. if (r == -1)
  345. {
  346. fprintf (stderr, "piBoardRev: Unable to determine board revision from /proc/cpuinfo\n") ;
  347. errno = 0 ;
  348. return -1 ;
  349. }
  350. // If you have overvolted the Pi, then it appears that the revision
  351. // has 100000 added to it!
  352. if (wiringPiDebug)
  353. if (r > 1000)
  354. printf ("piboardRev: This Pi has/is overvolted!\n") ;
  355. r %= 100 ;
  356. /**/ if ((r == 2) || (r == 3))
  357. boardRev = 1 ;
  358. else if ((r == 4) || (r == 5) || (r == 6))
  359. boardRev = 2 ;
  360. else
  361. {
  362. fprintf (stderr, "piBoardRev: Unable to determine board revision from %d\n", r) ;
  363. errno = 0 ;
  364. return -1 ;
  365. }
  366. if (wiringPiDebug)
  367. printf ("piboardRev: Revision: %d, board revision: %d\n", r, boardRev) ;
  368. return boardRev ;
  369. }
  370. /*
  371. * pinMode:
  372. * Sets the mode of a pin to be input, output or PWM output
  373. *********************************************************************************
  374. */
  375. void pinModeGpio (int pin, int mode)
  376. {
  377. int fSel, shift, alt ;
  378. pin &= 63 ;
  379. fSel = gpioToGPFSEL [pin] ;
  380. shift = gpioToShift [pin] ;
  381. /**/ if (mode == INPUT)
  382. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) ; // Sets bits to zero = input
  383. else if (mode == OUTPUT)
  384. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (1 << shift) ;
  385. else if (mode == PWM_OUTPUT)
  386. {
  387. if ((alt = gpioToPwmALT [pin]) == 0) // Not a PWM pin
  388. return ;
  389. // Set pin to PWM mode
  390. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ;
  391. // Page 107 of the BCM Peripherals manual talks about the GPIO clocks,
  392. // but I'm assuming (hoping!) that this applies to other clocks too.
  393. *(pwm + PWM_CONTROL) = 0 ; // Stop PWM
  394. *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x01 ; // Stop PWM Clock
  395. delayMicroseconds (110) ; // See comments in pwmSetClockWPi
  396. (void)*(pwm + PWM_CONTROL) ;
  397. while ((*(pwm + PWM_CONTROL) & 0x80) != 0) // Wait for clock to be !BUSY
  398. delayMicroseconds (1) ;
  399. *(clk + PWMCLK_DIV) = BCM_PASSWORD | (32 << 12) ; // set pwm div to 32 (19.2/32 = 600KHz)
  400. *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x11 ; // enable clk
  401. // Default range regsiter of 1024
  402. *(pwm + PWM0_DATA) = 0 ; *(pwm + PWM0_RANGE) = 1024 ;
  403. *(pwm + PWM1_DATA) = 0 ; *(pwm + PWM1_RANGE) = 1024 ;
  404. // Enable PWMs in balanced mode (default)
  405. *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ;
  406. }
  407. // When we change mode of any pin, we remove the pull up/downs
  408. // Or we used to... Hm. Commented out now because for some wieird reason,
  409. // it seems to block subsequent attempts to set the pull up/downs and I've
  410. // not quite gotten to the bottom of why this happens
  411. // The down-side is that the pull up/downs are rememberd in the SoC between
  412. // power cycles, so it's going to be a good idea to explicitly set them in
  413. // any new code.
  414. //
  415. // pullUpDnControl (pin, PUD_OFF) ;
  416. }
  417. void pinModeWPi (int pin, int mode)
  418. {
  419. pinModeGpio (pinToGpio [pin & 63], mode) ;
  420. }
  421. void pinModeSys (int pin, int mode)
  422. {
  423. return ;
  424. }
  425. /*
  426. * pwmControl:
  427. * Allow the user to control some of the PWM functions
  428. *********************************************************************************
  429. */
  430. void pwmSetModeWPi (int mode)
  431. {
  432. if (mode == PWM_MODE_MS)
  433. *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE | PWM0_MS_MODE | PWM1_MS_MODE ;
  434. else
  435. *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ;
  436. }
  437. void pwmSetModeSys (int mode)
  438. {
  439. return ;
  440. }
  441. void pwmSetRangeWPi (unsigned int range)
  442. {
  443. *(pwm + PWM0_RANGE) = range ; delayMicroseconds (10) ;
  444. *(pwm + PWM1_RANGE) = range ; delayMicroseconds (10) ;
  445. }
  446. void pwmSetRangeSys (unsigned int range)
  447. {
  448. return ;
  449. }
  450. /*
  451. * pwmSetClockWPi:
  452. * Set/Change the PWM clock. Originally my code, but changed
  453. * (for the better!) by Chris Hall, <chris@kchall.plus.com>
  454. * after further study of the manual and testing with a 'scope
  455. *********************************************************************************
  456. */
  457. void pwmSetClockWPi (int divisor)
  458. {
  459. unsigned int pwm_control ;
  460. divisor &= 4095 ;
  461. if (wiringPiDebug)
  462. printf ("Setting to: %d. Current: 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;
  463. pwm_control = *(pwm + PWM_CONTROL) ; // preserve PWM_CONTROL
  464. // We need to stop PWM prior to stopping PWM clock in MS mode otherwise BUSY
  465. // stays high.
  466. *(pwm + PWM_CONTROL) = 0 ; // Stop PWM
  467. // Stop PWM clock before changing divisor. The delay after this does need to
  468. // this big (95uS occasionally fails, 100uS OK), it's almost as though the BUSY
  469. // flag is not working properly in balanced mode. Without the delay when DIV is
  470. // adjusted the clock sometimes switches to very slow, once slow further DIV
  471. // adjustments do nothing and it's difficult to get out of this mode.
  472. *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x01 ; // Stop PWM Clock
  473. delayMicroseconds (110) ; // prevents clock going sloooow
  474. while ((*(pwm + PWM_CONTROL) & 0x80) != 0) // Wait for clock to be !BUSY
  475. delayMicroseconds (1) ;
  476. *(clk + PWMCLK_DIV) = BCM_PASSWORD | (divisor << 12) ;
  477. *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x11 ; // Start PWM clock
  478. *(pwm + PWM_CONTROL) = pwm_control ; // restore PWM_CONTROL
  479. if (wiringPiDebug)
  480. printf ("Set to: %d. Now : 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;
  481. }
  482. void pwmSetClockSys (int divisor)
  483. {
  484. return ;
  485. }
  486. #ifdef notYetReady
  487. /*
  488. * pinED01:
  489. * pinED10:
  490. * Enables edge-detect mode on a pin - from a 0 to a 1 or 1 to 0
  491. * Pin must already be in input mode with appropriate pull up/downs set.
  492. *********************************************************************************
  493. */
  494. void pinEnableED01Pi (int pin)
  495. {
  496. pin = pinToGpio [pin & 63] ;
  497. }
  498. #endif
  499. /*
  500. * digitalWrite:
  501. * Set an output bit
  502. *********************************************************************************
  503. */
  504. void digitalWriteWPi (int pin, int value)
  505. {
  506. pin = pinToGpio [pin & 63] ;
  507. if (value == LOW)
  508. *(gpio + gpioToGPCLR [pin]) = 1 << (pin & 31) ;
  509. else
  510. *(gpio + gpioToGPSET [pin]) = 1 << (pin & 31) ;
  511. }
  512. void digitalWriteGpio (int pin, int value)
  513. {
  514. pin &= 63 ;
  515. if (value == LOW)
  516. *(gpio + gpioToGPCLR [pin]) = 1 << (pin & 31) ;
  517. else
  518. *(gpio + gpioToGPSET [pin]) = 1 << (pin & 31) ;
  519. }
  520. void digitalWriteSys (int pin, int value)
  521. {
  522. pin &= 63 ;
  523. if (sysFds [pin] != -1)
  524. {
  525. if (value == LOW)
  526. write (sysFds [pin], "0\n", 2) ;
  527. else
  528. write (sysFds [pin], "1\n", 2) ;
  529. }
  530. }
  531. /*
  532. * pwmWrite:
  533. * Set an output PWM value
  534. *********************************************************************************
  535. */
  536. void pwmWriteGpio (int pin, int value)
  537. {
  538. int port ;
  539. pin = pin & 63 ;
  540. port = gpioToPwmPort [pin] ;
  541. *(pwm + port) = value ;
  542. }
  543. void pwmWriteWPi (int pin, int value)
  544. {
  545. pwmWriteGpio (pinToGpio [pin & 63], value) ;
  546. }
  547. void pwmWriteSys (int pin, int value)
  548. {
  549. return ;
  550. }
  551. /*
  552. * setPadDrive:
  553. * Set the PAD driver value
  554. *********************************************************************************
  555. */
  556. void setPadDriveWPi (int group, int value)
  557. {
  558. uint32_t wrVal ;
  559. if ((group < 0) || (group > 2))
  560. return ;
  561. wrVal = BCM_PASSWORD | 0x18 | (value & 7) ;
  562. *(pads + group + 11) = wrVal ;
  563. #ifdef DEBUG_PADS
  564. printf ("setPadDrive: Group: %d, value: %d (%08X)\n", group, value, wrVal) ;
  565. printf ("Read : %08X\n", *(pads + group + 11)) ;
  566. #endif
  567. }
  568. void setPadDriveGpio (int group, int value)
  569. {
  570. setPadDriveWPi (group, value) ;
  571. }
  572. void setPadDriveSys (int group, int value)
  573. {
  574. return ;
  575. }
  576. /*
  577. * digitalRead:
  578. * Read the value of a given Pin, returning HIGH or LOW
  579. *********************************************************************************
  580. */
  581. int digitalReadWPi (int pin)
  582. {
  583. pin = pinToGpio [pin & 63] ;
  584. if ((*(gpio + gpioToGPLEV [pin]) & (1 << (pin & 31))) != 0)
  585. return HIGH ;
  586. else
  587. return LOW ;
  588. }
  589. int digitalReadGpio (int pin)
  590. {
  591. pin &= 63 ;
  592. if ((*(gpio + gpioToGPLEV [pin]) & (1 << (pin & 31))) != 0)
  593. return HIGH ;
  594. else
  595. return LOW ;
  596. }
  597. int digitalReadSys (int pin)
  598. {
  599. char c ;
  600. pin &= 63 ;
  601. if (sysFds [pin] == -1)
  602. return 0 ;
  603. lseek (sysFds [pin], 0L, SEEK_SET) ;
  604. read (sysFds [pin], &c, 1) ;
  605. return (c == '0') ? 0 : 1 ;
  606. }
  607. /*
  608. * pullUpDownCtrl:
  609. * Control the internal pull-up/down resistors on a GPIO pin
  610. * The Arduino only has pull-ups and these are enabled by writing 1
  611. * to a port when in input mode - this paradigm doesn't quite apply
  612. * here though.
  613. *********************************************************************************
  614. */
  615. void pullUpDnControlGpio (int pin, int pud)
  616. {
  617. pin &= 63 ;
  618. pud &= 3 ;
  619. *(gpio + GPPUD) = pud ; delayMicroseconds (5) ;
  620. *(gpio + gpioToPUDCLK [pin]) = 1 << (pin & 31) ; delayMicroseconds (5) ;
  621. *(gpio + GPPUD) = 0 ; delayMicroseconds (5) ;
  622. *(gpio + gpioToPUDCLK [pin]) = 0 ; delayMicroseconds (5) ;
  623. }
  624. void pullUpDnControlWPi (int pin, int pud)
  625. {
  626. pullUpDnControlGpio (pinToGpio [pin & 63], pud) ;
  627. }
  628. void pullUpDnControlSys (int pin, int pud)
  629. {
  630. return ;
  631. }
  632. /*
  633. * waitForInterrupt:
  634. * Wait for Interrupt on a GPIO pin.
  635. * This is actually done via the /sys/class/gpio interface regardless of
  636. * the wiringPi access mode in-use. Maybe sometime it might get a better
  637. * way for a bit more efficiency.
  638. *********************************************************************************
  639. */
  640. int waitForInterruptSys (int pin, int mS)
  641. {
  642. int fd, x ;
  643. char buf [8] ;
  644. struct pollfd polls ;
  645. if ((fd = sysFds [pin & 63]) == -1)
  646. return -2 ;
  647. // Do a dummy read
  648. x = read (fd, buf, 6) ;
  649. if (x < 0)
  650. return x ;
  651. // And seek
  652. lseek (fd, 0, SEEK_SET) ;
  653. // Setup poll structure
  654. polls.fd = fd ;
  655. polls.events = POLLPRI ; // Urgent data!
  656. // Wait for it ...
  657. return poll (&polls, 1, mS) ;
  658. }
  659. int waitForInterruptWPi (int pin, int mS)
  660. {
  661. return waitForInterruptSys (pinToGpio [pin & 63], mS) ;
  662. }
  663. int waitForInterruptGpio (int pin, int mS)
  664. {
  665. return waitForInterruptSys (pin, mS) ;
  666. }
  667. /*
  668. * delay:
  669. * Wait for some number of milli seconds
  670. *********************************************************************************
  671. */
  672. void delay (unsigned int howLong)
  673. {
  674. struct timespec sleeper, dummy ;
  675. sleeper.tv_sec = (time_t)(howLong / 1000) ;
  676. sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ;
  677. nanosleep (&sleeper, &dummy) ;
  678. }
  679. /*
  680. * delayMicroseconds:
  681. * This is somewhat intersting. It seems that on the Pi, a single call
  682. * to nanosleep takes some 80 to 130 microseconds anyway, so while
  683. * obeying the standards (may take longer), it's not always what we
  684. * want!
  685. *
  686. * So what I'll do now is if the delay is less than 100uS we'll do it
  687. * in a hard loop, watching a built-in counter on the ARM chip. This is
  688. * somewhat sub-optimal in that it uses 100% CPU, something not an issue
  689. * in a microcontroller, but under a multi-tasking, multi-user OS, it's
  690. * wastefull, however we've no real choice )-:
  691. *********************************************************************************
  692. */
  693. void delayMicrosecondsSys (unsigned int howLong)
  694. {
  695. struct timespec sleeper, dummy ;
  696. sleeper.tv_sec = 0 ;
  697. sleeper.tv_nsec = (long)(howLong * 1000) ;
  698. nanosleep (&sleeper, &dummy) ;
  699. }
  700. void delayMicrosecondsHard (unsigned int howLong)
  701. {
  702. *(timer + TIMER_LOAD) = howLong ;
  703. *(timer + TIMER_IRQ_CLR) = 0 ;
  704. while (*timerIrqRaw == 0)
  705. ;
  706. }
  707. void delayMicrosecondsWPi (unsigned int howLong)
  708. {
  709. struct timespec sleeper, dummy ;
  710. /**/ if (howLong == 0)
  711. return ;
  712. else if (howLong < 100)
  713. delayMicrosecondsHard (howLong) ;
  714. else
  715. {
  716. sleeper.tv_sec = 0 ;
  717. sleeper.tv_nsec = (long)(howLong * 1000) ;
  718. nanosleep (&sleeper, &dummy) ;
  719. }
  720. }
  721. /*
  722. * millis:
  723. * Return a number of milliseconds as an unsigned int.
  724. *********************************************************************************
  725. */
  726. unsigned int millis (void)
  727. {
  728. struct timeval tv ;
  729. unsigned long long t1 ;
  730. gettimeofday (&tv, NULL) ;
  731. t1 = (tv.tv_sec * 1000000 + tv.tv_usec) / 1000 ;
  732. return (uint32_t)(t1 - epoch) ;
  733. }
  734. /*
  735. * wiringPiSetup:
  736. * Must be called once at the start of your program execution.
  737. *
  738. * Default setup: Initialises the system into wiringPi Pin mode and uses the
  739. * memory mapped hardware directly.
  740. *********************************************************************************
  741. */
  742. int wiringPiSetup (void)
  743. {
  744. int fd ;
  745. int boardRev ;
  746. uint8_t *gpioMem, *pwmMem, *clkMem, *padsMem, *timerMem ;
  747. struct timeval tv ;
  748. if (getenv ("WIRINGPI_DEBUG") != NULL)
  749. wiringPiDebug = TRUE ;
  750. if (wiringPiDebug)
  751. printf ("wiringPi: wiringPiSetup called\n") ;
  752. pinMode = pinModeWPi ;
  753. pullUpDnControl = pullUpDnControlWPi ;
  754. digitalWrite = digitalWriteWPi ;
  755. pwmWrite = pwmWriteWPi ;
  756. setPadDrive = setPadDriveWPi ;
  757. digitalRead = digitalReadWPi ;
  758. waitForInterrupt = waitForInterruptWPi ;
  759. delayMicroseconds = delayMicrosecondsWPi ;
  760. pwmSetMode = pwmSetModeWPi ;
  761. pwmSetRange = pwmSetRangeWPi ;
  762. pwmSetClock = pwmSetClockWPi ;
  763. if ((boardRev = piBoardRev ()) < 0)
  764. return -1 ;
  765. if (boardRev == 1)
  766. pinToGpio = pinToGpioR1 ;
  767. else
  768. pinToGpio = pinToGpioR2 ;
  769. // Open the master /dev/memory device
  770. if ((fd = open ("/dev/mem", O_RDWR | O_SYNC) ) < 0)
  771. {
  772. fprintf (stderr, "wiringPiSetup: Unable to open /dev/mem: %s\n", strerror (errno)) ;
  773. return -1 ;
  774. }
  775. // GPIO:
  776. // Allocate 2 pages - 1 ...
  777. if ((gpioMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
  778. {
  779. fprintf (stderr, "wiringPiSetup: malloc failed: %s\n", strerror (errno)) ;
  780. return -1 ;
  781. }
  782. // ... presumably to make sure we can round it up to a whole page size
  783. if (((uint32_t)gpioMem % PAGE_SIZE) != 0)
  784. gpioMem += PAGE_SIZE - ((uint32_t)gpioMem % PAGE_SIZE) ;
  785. gpio = (uint32_t *)mmap((caddr_t)gpioMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, GPIO_BASE) ;
  786. if ((int32_t)gpio < 0)
  787. {
  788. fprintf (stderr, "wiringPiSetup: mmap failed: %s\n", strerror (errno)) ;
  789. return -1 ;
  790. }
  791. // PWM
  792. if ((pwmMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
  793. {
  794. fprintf (stderr, "wiringPiSetup: pwmMem malloc failed: %s\n", strerror (errno)) ;
  795. return -1 ;
  796. }
  797. if (((uint32_t)pwmMem % PAGE_SIZE) != 0)
  798. pwmMem += PAGE_SIZE - ((uint32_t)pwmMem % PAGE_SIZE) ;
  799. pwm = (uint32_t *)mmap(pwmMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, GPIO_PWM) ;
  800. if ((int32_t)pwm < 0)
  801. {
  802. fprintf (stderr, "wiringPiSetup: mmap failed (pwm): %s\n", strerror (errno)) ;
  803. return -1 ;
  804. }
  805. // Clock control (needed for PWM)
  806. if ((clkMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
  807. {
  808. fprintf (stderr, "wiringPiSetup: clkMem malloc failed: %s\n", strerror (errno)) ;
  809. return -1 ;
  810. }
  811. if (((uint32_t)clkMem % PAGE_SIZE) != 0)
  812. clkMem += PAGE_SIZE - ((uint32_t)clkMem % PAGE_SIZE) ;
  813. clk = (uint32_t *)mmap(clkMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, CLOCK_BASE) ;
  814. if ((int32_t)clk < 0)
  815. {
  816. fprintf (stderr, "wiringPiSetup: mmap failed (clk): %s\n", strerror (errno)) ;
  817. return -1 ;
  818. }
  819. // The drive pads
  820. if ((padsMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
  821. {
  822. fprintf (stderr, "wiringPiSetup: padsMem malloc failed: %s\n", strerror (errno)) ;
  823. return -1 ;
  824. }
  825. if (((uint32_t)padsMem % PAGE_SIZE) != 0)
  826. padsMem += PAGE_SIZE - ((uint32_t)padsMem % PAGE_SIZE) ;
  827. pads = (uint32_t *)mmap(padsMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, GPIO_PADS) ;
  828. if ((int32_t)pads < 0)
  829. {
  830. fprintf (stderr, "wiringPiSetup: mmap failed (pads): %s\n", strerror (errno)) ;
  831. return -1 ;
  832. }
  833. #ifdef DEBUG_PADS
  834. printf ("Checking pads @ 0x%08X\n", (unsigned int)pads) ;
  835. printf (" -> %08X %08X %08X\n", *(pads + 11), *(pads + 12), *(pads + 13)) ;
  836. #endif
  837. // The system timer
  838. if ((timerMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
  839. {
  840. fprintf (stderr, "wiringPiSetup: timerMem malloc failed: %s\n", strerror (errno)) ;
  841. return -1 ;
  842. }
  843. if (((uint32_t)timerMem % PAGE_SIZE) != 0)
  844. timerMem += PAGE_SIZE - ((uint32_t)timerMem % PAGE_SIZE) ;
  845. timer = (uint32_t *)mmap(timerMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, GPIO_TIMER) ;
  846. if ((int32_t)timer < 0)
  847. {
  848. fprintf (stderr, "wiringPiSetup: mmap failed (timer): %s\n", strerror (errno)) ;
  849. return -1 ;
  850. }
  851. // Set the timer to free-running, 1MHz.
  852. // 0xF9 is 249, the timer divide is base clock / (divide+1)
  853. // so base clock is 250MHz / 250 = 1MHz.
  854. *(timer + TIMER_CONTROL) = 0x0000280 ;
  855. *(timer + TIMER_PRE_DIV) = 0x00000F9 ;
  856. timerIrqRaw = timer + TIMER_IRQ_RAW ;
  857. // Initialise our epoch for millis()
  858. gettimeofday (&tv, NULL) ;
  859. epoch = (tv.tv_sec * 1000000 + tv.tv_usec) / 1000 ;
  860. return 0 ;
  861. }
  862. /*
  863. * wiringPiSetupGpio:
  864. * Must be called once at the start of your program execution.
  865. *
  866. * GPIO setup: Initialises the system into GPIO Pin mode and uses the
  867. * memory mapped hardware directly.
  868. *********************************************************************************
  869. */
  870. int wiringPiSetupGpio (void)
  871. {
  872. int x ;
  873. if (wiringPiDebug)
  874. printf ("wiringPi: wiringPiSetupGpio called\n") ;
  875. if ((x = wiringPiSetup ()) < 0)
  876. return x ;
  877. pinMode = pinModeGpio ;
  878. pullUpDnControl = pullUpDnControlGpio ;
  879. digitalWrite = digitalWriteGpio ;
  880. pwmWrite = pwmWriteGpio ;
  881. setPadDrive = setPadDriveGpio ;
  882. digitalRead = digitalReadGpio ;
  883. waitForInterrupt = waitForInterruptGpio ;
  884. delayMicroseconds = delayMicrosecondsWPi ; // Same
  885. pwmSetMode = pwmSetModeWPi ;
  886. pwmSetRange = pwmSetRangeWPi ;
  887. pwmSetClock = pwmSetClockWPi ;
  888. return 0 ;
  889. }
  890. /*
  891. * wiringPiSetupSys:
  892. * Must be called once at the start of your program execution.
  893. *
  894. * Initialisation (again), however this time we are using the /sys/class/gpio
  895. * interface to the GPIO systems - slightly slower, but always usable as
  896. * a non-root user, assuming the devices are already exported and setup correctly.
  897. */
  898. int wiringPiSetupSys (void)
  899. {
  900. int pin ;
  901. struct timeval tv ;
  902. char fName [128] ;
  903. if (wiringPiDebug)
  904. printf ("wiringPi: wiringPiSetupSys called\n") ;
  905. pinMode = pinModeSys ;
  906. pullUpDnControl = pullUpDnControlSys ;
  907. digitalWrite = digitalWriteSys ;
  908. pwmWrite = pwmWriteSys ;
  909. setPadDrive = setPadDriveSys ;
  910. digitalRead = digitalReadSys ;
  911. waitForInterrupt = waitForInterruptSys ;
  912. delayMicroseconds = delayMicrosecondsSys ;
  913. pwmSetMode = pwmSetModeSys ;
  914. pwmSetRange = pwmSetRangeSys ;
  915. pwmSetClock = pwmSetClockSys ;
  916. // Open and scan the directory, looking for exported GPIOs, and pre-open
  917. // the 'value' interface to speed things up for later
  918. for (pin = 0 ; pin < 64 ; ++pin)
  919. {
  920. sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
  921. sysFds [pin] = open (fName, O_RDWR) ;
  922. }
  923. // Initialise the epoch for mills() ...
  924. gettimeofday (&tv, NULL) ;
  925. epoch = (tv.tv_sec * 1000000 + tv.tv_usec) / 1000 ;
  926. return 0 ;
  927. }