Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 

1203 righe
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, "WARNING: wiringPi: Unable to determine board revision from \"%d\"\n", r) ;
  363. fprintf (stderr, " -> You may want to check:\n") ;
  364. fprintf (stderr, " -> http://www.raspberrypi.org/phpBB3/viewtopic.php?p=184410#p184410\n") ;
  365. fprintf (stderr, " -> Assuming a Rev 1 board\n") ;
  366. boardRev = 1 ;
  367. }
  368. if (wiringPiDebug)
  369. printf ("piboardRev: Revision: %d, board revision: %d\n", r, boardRev) ;
  370. return boardRev ;
  371. }
  372. /*
  373. * pinMode:
  374. * Sets the mode of a pin to be input, output or PWM output
  375. *********************************************************************************
  376. */
  377. void pinModeGpio (int pin, int mode)
  378. {
  379. int fSel, shift, alt ;
  380. pin &= 63 ;
  381. fSel = gpioToGPFSEL [pin] ;
  382. shift = gpioToShift [pin] ;
  383. /**/ if (mode == INPUT)
  384. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) ; // Sets bits to zero = input
  385. else if (mode == OUTPUT)
  386. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (1 << shift) ;
  387. else if (mode == PWM_OUTPUT)
  388. {
  389. if ((alt = gpioToPwmALT [pin]) == 0) // Not a PWM pin
  390. return ;
  391. // Set pin to PWM mode
  392. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ;
  393. // Page 107 of the BCM Peripherals manual talks about the GPIO clocks,
  394. // but I'm assuming (hoping!) that this applies to other clocks too.
  395. *(pwm + PWM_CONTROL) = 0 ; // Stop PWM
  396. *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x01 ; // Stop PWM Clock
  397. delayMicroseconds (110) ; // See comments in pwmSetClockWPi
  398. (void)*(pwm + PWM_CONTROL) ;
  399. while ((*(pwm + PWM_CONTROL) & 0x80) != 0) // Wait for clock to be !BUSY
  400. delayMicroseconds (1) ;
  401. *(clk + PWMCLK_DIV) = BCM_PASSWORD | (32 << 12) ; // set pwm div to 32 (19.2/32 = 600KHz)
  402. *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x11 ; // enable clk
  403. // Default range regsiter of 1024
  404. *(pwm + PWM0_DATA) = 0 ; *(pwm + PWM0_RANGE) = 1024 ;
  405. *(pwm + PWM1_DATA) = 0 ; *(pwm + PWM1_RANGE) = 1024 ;
  406. // Enable PWMs in balanced mode (default)
  407. *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ;
  408. }
  409. // When we change mode of any pin, we remove the pull up/downs
  410. // Or we used to... Hm. Commented out now because for some wieird reason,
  411. // it seems to block subsequent attempts to set the pull up/downs and I've
  412. // not quite gotten to the bottom of why this happens
  413. // The down-side is that the pull up/downs are rememberd in the SoC between
  414. // power cycles, so it's going to be a good idea to explicitly set them in
  415. // any new code.
  416. //
  417. // pullUpDnControl (pin, PUD_OFF) ;
  418. }
  419. void pinModeWPi (int pin, int mode)
  420. {
  421. pinModeGpio (pinToGpio [pin & 63], mode) ;
  422. }
  423. void pinModeSys (int pin, int mode)
  424. {
  425. return ;
  426. }
  427. /*
  428. * pwmControl:
  429. * Allow the user to control some of the PWM functions
  430. *********************************************************************************
  431. */
  432. void pwmSetModeWPi (int mode)
  433. {
  434. if (mode == PWM_MODE_MS)
  435. *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE | PWM0_MS_MODE | PWM1_MS_MODE ;
  436. else
  437. *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ;
  438. }
  439. void pwmSetModeSys (int mode)
  440. {
  441. return ;
  442. }
  443. void pwmSetRangeWPi (unsigned int range)
  444. {
  445. *(pwm + PWM0_RANGE) = range ; delayMicroseconds (10) ;
  446. *(pwm + PWM1_RANGE) = range ; delayMicroseconds (10) ;
  447. }
  448. void pwmSetRangeSys (unsigned int range)
  449. {
  450. return ;
  451. }
  452. /*
  453. * pwmSetClockWPi:
  454. * Set/Change the PWM clock. Originally my code, but changed
  455. * (for the better!) by Chris Hall, <chris@kchall.plus.com>
  456. * after further study of the manual and testing with a 'scope
  457. *********************************************************************************
  458. */
  459. void pwmSetClockWPi (int divisor)
  460. {
  461. unsigned int pwm_control ;
  462. divisor &= 4095 ;
  463. if (wiringPiDebug)
  464. printf ("Setting to: %d. Current: 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;
  465. pwm_control = *(pwm + PWM_CONTROL) ; // preserve PWM_CONTROL
  466. // We need to stop PWM prior to stopping PWM clock in MS mode otherwise BUSY
  467. // stays high.
  468. *(pwm + PWM_CONTROL) = 0 ; // Stop PWM
  469. // Stop PWM clock before changing divisor. The delay after this does need to
  470. // this big (95uS occasionally fails, 100uS OK), it's almost as though the BUSY
  471. // flag is not working properly in balanced mode. Without the delay when DIV is
  472. // adjusted the clock sometimes switches to very slow, once slow further DIV
  473. // adjustments do nothing and it's difficult to get out of this mode.
  474. *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x01 ; // Stop PWM Clock
  475. delayMicroseconds (110) ; // prevents clock going sloooow
  476. while ((*(pwm + PWM_CONTROL) & 0x80) != 0) // Wait for clock to be !BUSY
  477. delayMicroseconds (1) ;
  478. *(clk + PWMCLK_DIV) = BCM_PASSWORD | (divisor << 12) ;
  479. *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x11 ; // Start PWM clock
  480. *(pwm + PWM_CONTROL) = pwm_control ; // restore PWM_CONTROL
  481. if (wiringPiDebug)
  482. printf ("Set to: %d. Now : 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;
  483. }
  484. void pwmSetClockSys (int divisor)
  485. {
  486. return ;
  487. }
  488. #ifdef notYetReady
  489. /*
  490. * pinED01:
  491. * pinED10:
  492. * Enables edge-detect mode on a pin - from a 0 to a 1 or 1 to 0
  493. * Pin must already be in input mode with appropriate pull up/downs set.
  494. *********************************************************************************
  495. */
  496. void pinEnableED01Pi (int pin)
  497. {
  498. pin = pinToGpio [pin & 63] ;
  499. }
  500. #endif
  501. /*
  502. * digitalWrite:
  503. * Set an output bit
  504. *********************************************************************************
  505. */
  506. void digitalWriteWPi (int pin, int value)
  507. {
  508. pin = pinToGpio [pin & 63] ;
  509. if (value == LOW)
  510. *(gpio + gpioToGPCLR [pin]) = 1 << (pin & 31) ;
  511. else
  512. *(gpio + gpioToGPSET [pin]) = 1 << (pin & 31) ;
  513. }
  514. void digitalWriteGpio (int pin, int value)
  515. {
  516. pin &= 63 ;
  517. if (value == LOW)
  518. *(gpio + gpioToGPCLR [pin]) = 1 << (pin & 31) ;
  519. else
  520. *(gpio + gpioToGPSET [pin]) = 1 << (pin & 31) ;
  521. }
  522. void digitalWriteSys (int pin, int value)
  523. {
  524. pin &= 63 ;
  525. if (sysFds [pin] != -1)
  526. {
  527. if (value == LOW)
  528. write (sysFds [pin], "0\n", 2) ;
  529. else
  530. write (sysFds [pin], "1\n", 2) ;
  531. }
  532. }
  533. /*
  534. * pwmWrite:
  535. * Set an output PWM value
  536. *********************************************************************************
  537. */
  538. void pwmWriteGpio (int pin, int value)
  539. {
  540. int port ;
  541. pin = pin & 63 ;
  542. port = gpioToPwmPort [pin] ;
  543. *(pwm + port) = value ;
  544. }
  545. void pwmWriteWPi (int pin, int value)
  546. {
  547. pwmWriteGpio (pinToGpio [pin & 63], value) ;
  548. }
  549. void pwmWriteSys (int pin, int value)
  550. {
  551. return ;
  552. }
  553. /*
  554. * setPadDrive:
  555. * Set the PAD driver value
  556. *********************************************************************************
  557. */
  558. void setPadDriveWPi (int group, int value)
  559. {
  560. uint32_t wrVal ;
  561. if ((group < 0) || (group > 2))
  562. return ;
  563. wrVal = BCM_PASSWORD | 0x18 | (value & 7) ;
  564. *(pads + group + 11) = wrVal ;
  565. #ifdef DEBUG_PADS
  566. printf ("setPadDrive: Group: %d, value: %d (%08X)\n", group, value, wrVal) ;
  567. printf ("Read : %08X\n", *(pads + group + 11)) ;
  568. #endif
  569. }
  570. void setPadDriveGpio (int group, int value)
  571. {
  572. setPadDriveWPi (group, value) ;
  573. }
  574. void setPadDriveSys (int group, int value)
  575. {
  576. return ;
  577. }
  578. /*
  579. * digitalRead:
  580. * Read the value of a given Pin, returning HIGH or LOW
  581. *********************************************************************************
  582. */
  583. int digitalReadWPi (int pin)
  584. {
  585. pin = pinToGpio [pin & 63] ;
  586. if ((*(gpio + gpioToGPLEV [pin]) & (1 << (pin & 31))) != 0)
  587. return HIGH ;
  588. else
  589. return LOW ;
  590. }
  591. int digitalReadGpio (int pin)
  592. {
  593. pin &= 63 ;
  594. if ((*(gpio + gpioToGPLEV [pin]) & (1 << (pin & 31))) != 0)
  595. return HIGH ;
  596. else
  597. return LOW ;
  598. }
  599. int digitalReadSys (int pin)
  600. {
  601. char c ;
  602. pin &= 63 ;
  603. if (sysFds [pin] == -1)
  604. return 0 ;
  605. lseek (sysFds [pin], 0L, SEEK_SET) ;
  606. read (sysFds [pin], &c, 1) ;
  607. return (c == '0') ? 0 : 1 ;
  608. }
  609. /*
  610. * pullUpDownCtrl:
  611. * Control the internal pull-up/down resistors on a GPIO pin
  612. * The Arduino only has pull-ups and these are enabled by writing 1
  613. * to a port when in input mode - this paradigm doesn't quite apply
  614. * here though.
  615. *********************************************************************************
  616. */
  617. void pullUpDnControlGpio (int pin, int pud)
  618. {
  619. pin &= 63 ;
  620. pud &= 3 ;
  621. *(gpio + GPPUD) = pud ; delayMicroseconds (5) ;
  622. *(gpio + gpioToPUDCLK [pin]) = 1 << (pin & 31) ; delayMicroseconds (5) ;
  623. *(gpio + GPPUD) = 0 ; delayMicroseconds (5) ;
  624. *(gpio + gpioToPUDCLK [pin]) = 0 ; delayMicroseconds (5) ;
  625. }
  626. void pullUpDnControlWPi (int pin, int pud)
  627. {
  628. pullUpDnControlGpio (pinToGpio [pin & 63], pud) ;
  629. }
  630. void pullUpDnControlSys (int pin, int pud)
  631. {
  632. return ;
  633. }
  634. /*
  635. * waitForInterrupt:
  636. * Wait for Interrupt on a GPIO pin.
  637. * This is actually done via the /sys/class/gpio interface regardless of
  638. * the wiringPi access mode in-use. Maybe sometime it might get a better
  639. * way for a bit more efficiency.
  640. *********************************************************************************
  641. */
  642. int waitForInterruptSys (int pin, int mS)
  643. {
  644. int fd, x ;
  645. char buf [8] ;
  646. struct pollfd polls ;
  647. if ((fd = sysFds [pin & 63]) == -1)
  648. return -2 ;
  649. // Do a dummy read
  650. x = read (fd, buf, 6) ;
  651. if (x < 0)
  652. return x ;
  653. // And seek
  654. lseek (fd, 0, SEEK_SET) ;
  655. // Setup poll structure
  656. polls.fd = fd ;
  657. polls.events = POLLPRI ; // Urgent data!
  658. // Wait for it ...
  659. return poll (&polls, 1, mS) ;
  660. }
  661. int waitForInterruptWPi (int pin, int mS)
  662. {
  663. return waitForInterruptSys (pinToGpio [pin & 63], mS) ;
  664. }
  665. int waitForInterruptGpio (int pin, int mS)
  666. {
  667. return waitForInterruptSys (pin, mS) ;
  668. }
  669. /*
  670. * delay:
  671. * Wait for some number of milli seconds
  672. *********************************************************************************
  673. */
  674. void delay (unsigned int howLong)
  675. {
  676. struct timespec sleeper, dummy ;
  677. sleeper.tv_sec = (time_t)(howLong / 1000) ;
  678. sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ;
  679. nanosleep (&sleeper, &dummy) ;
  680. }
  681. /*
  682. * delayMicroseconds:
  683. * This is somewhat intersting. It seems that on the Pi, a single call
  684. * to nanosleep takes some 80 to 130 microseconds anyway, so while
  685. * obeying the standards (may take longer), it's not always what we
  686. * want!
  687. *
  688. * So what I'll do now is if the delay is less than 100uS we'll do it
  689. * in a hard loop, watching a built-in counter on the ARM chip. This is
  690. * somewhat sub-optimal in that it uses 100% CPU, something not an issue
  691. * in a microcontroller, but under a multi-tasking, multi-user OS, it's
  692. * wastefull, however we've no real choice )-:
  693. *********************************************************************************
  694. */
  695. void delayMicrosecondsSys (unsigned int howLong)
  696. {
  697. struct timespec sleeper, dummy ;
  698. sleeper.tv_sec = 0 ;
  699. sleeper.tv_nsec = (long)(howLong * 1000) ;
  700. nanosleep (&sleeper, &dummy) ;
  701. }
  702. void delayMicrosecondsHard (unsigned int howLong)
  703. {
  704. *(timer + TIMER_LOAD) = howLong ;
  705. *(timer + TIMER_IRQ_CLR) = 0 ;
  706. while (*timerIrqRaw == 0)
  707. ;
  708. }
  709. void delayMicrosecondsWPi (unsigned int howLong)
  710. {
  711. struct timespec sleeper, dummy ;
  712. /**/ if (howLong == 0)
  713. return ;
  714. else if (howLong < 100)
  715. delayMicrosecondsHard (howLong) ;
  716. else
  717. {
  718. sleeper.tv_sec = 0 ;
  719. sleeper.tv_nsec = (long)(howLong * 1000) ;
  720. nanosleep (&sleeper, &dummy) ;
  721. }
  722. }
  723. /*
  724. * millis:
  725. * Return a number of milliseconds as an unsigned int.
  726. *********************************************************************************
  727. */
  728. unsigned int millis (void)
  729. {
  730. struct timeval tv ;
  731. unsigned long long t1 ;
  732. gettimeofday (&tv, NULL) ;
  733. t1 = (tv.tv_sec * 1000000 + tv.tv_usec) / 1000 ;
  734. return (uint32_t)(t1 - epoch) ;
  735. }
  736. /*
  737. * wiringPiSetup:
  738. * Must be called once at the start of your program execution.
  739. *
  740. * Default setup: Initialises the system into wiringPi Pin mode and uses the
  741. * memory mapped hardware directly.
  742. *********************************************************************************
  743. */
  744. int wiringPiSetup (void)
  745. {
  746. int fd ;
  747. int boardRev ;
  748. uint8_t *gpioMem, *pwmMem, *clkMem, *padsMem, *timerMem ;
  749. struct timeval tv ;
  750. if (getenv ("WIRINGPI_DEBUG") != NULL)
  751. wiringPiDebug = TRUE ;
  752. if (wiringPiDebug)
  753. printf ("wiringPi: wiringPiSetup called\n") ;
  754. pinMode = pinModeWPi ;
  755. pullUpDnControl = pullUpDnControlWPi ;
  756. digitalWrite = digitalWriteWPi ;
  757. pwmWrite = pwmWriteWPi ;
  758. setPadDrive = setPadDriveWPi ;
  759. digitalRead = digitalReadWPi ;
  760. waitForInterrupt = waitForInterruptWPi ;
  761. delayMicroseconds = delayMicrosecondsWPi ;
  762. pwmSetMode = pwmSetModeWPi ;
  763. pwmSetRange = pwmSetRangeWPi ;
  764. pwmSetClock = pwmSetClockWPi ;
  765. if ((boardRev = piBoardRev ()) < 0)
  766. return -1 ;
  767. if (boardRev == 1)
  768. pinToGpio = pinToGpioR1 ;
  769. else
  770. pinToGpio = pinToGpioR2 ;
  771. // Open the master /dev/memory device
  772. if ((fd = open ("/dev/mem", O_RDWR | O_SYNC) ) < 0)
  773. {
  774. fprintf (stderr, "wiringPiSetup: Unable to open /dev/mem: %s\n", strerror (errno)) ;
  775. return -1 ;
  776. }
  777. // GPIO:
  778. // Allocate 2 pages - 1 ...
  779. if ((gpioMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
  780. {
  781. fprintf (stderr, "wiringPiSetup: malloc failed: %s\n", strerror (errno)) ;
  782. return -1 ;
  783. }
  784. // ... presumably to make sure we can round it up to a whole page size
  785. if (((uint32_t)gpioMem % PAGE_SIZE) != 0)
  786. gpioMem += PAGE_SIZE - ((uint32_t)gpioMem % PAGE_SIZE) ;
  787. gpio = (uint32_t *)mmap((caddr_t)gpioMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, GPIO_BASE) ;
  788. if ((int32_t)gpio < 0)
  789. {
  790. fprintf (stderr, "wiringPiSetup: mmap failed: %s\n", strerror (errno)) ;
  791. return -1 ;
  792. }
  793. // PWM
  794. if ((pwmMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
  795. {
  796. fprintf (stderr, "wiringPiSetup: pwmMem malloc failed: %s\n", strerror (errno)) ;
  797. return -1 ;
  798. }
  799. if (((uint32_t)pwmMem % PAGE_SIZE) != 0)
  800. pwmMem += PAGE_SIZE - ((uint32_t)pwmMem % PAGE_SIZE) ;
  801. pwm = (uint32_t *)mmap(pwmMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, GPIO_PWM) ;
  802. if ((int32_t)pwm < 0)
  803. {
  804. fprintf (stderr, "wiringPiSetup: mmap failed (pwm): %s\n", strerror (errno)) ;
  805. return -1 ;
  806. }
  807. // Clock control (needed for PWM)
  808. if ((clkMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
  809. {
  810. fprintf (stderr, "wiringPiSetup: clkMem malloc failed: %s\n", strerror (errno)) ;
  811. return -1 ;
  812. }
  813. if (((uint32_t)clkMem % PAGE_SIZE) != 0)
  814. clkMem += PAGE_SIZE - ((uint32_t)clkMem % PAGE_SIZE) ;
  815. clk = (uint32_t *)mmap(clkMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, CLOCK_BASE) ;
  816. if ((int32_t)clk < 0)
  817. {
  818. fprintf (stderr, "wiringPiSetup: mmap failed (clk): %s\n", strerror (errno)) ;
  819. return -1 ;
  820. }
  821. // The drive pads
  822. if ((padsMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
  823. {
  824. fprintf (stderr, "wiringPiSetup: padsMem malloc failed: %s\n", strerror (errno)) ;
  825. return -1 ;
  826. }
  827. if (((uint32_t)padsMem % PAGE_SIZE) != 0)
  828. padsMem += PAGE_SIZE - ((uint32_t)padsMem % PAGE_SIZE) ;
  829. pads = (uint32_t *)mmap(padsMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, GPIO_PADS) ;
  830. if ((int32_t)pads < 0)
  831. {
  832. fprintf (stderr, "wiringPiSetup: mmap failed (pads): %s\n", strerror (errno)) ;
  833. return -1 ;
  834. }
  835. #ifdef DEBUG_PADS
  836. printf ("Checking pads @ 0x%08X\n", (unsigned int)pads) ;
  837. printf (" -> %08X %08X %08X\n", *(pads + 11), *(pads + 12), *(pads + 13)) ;
  838. #endif
  839. // The system timer
  840. if ((timerMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
  841. {
  842. fprintf (stderr, "wiringPiSetup: timerMem malloc failed: %s\n", strerror (errno)) ;
  843. return -1 ;
  844. }
  845. if (((uint32_t)timerMem % PAGE_SIZE) != 0)
  846. timerMem += PAGE_SIZE - ((uint32_t)timerMem % PAGE_SIZE) ;
  847. timer = (uint32_t *)mmap(timerMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, GPIO_TIMER) ;
  848. if ((int32_t)timer < 0)
  849. {
  850. fprintf (stderr, "wiringPiSetup: mmap failed (timer): %s\n", strerror (errno)) ;
  851. return -1 ;
  852. }
  853. // Set the timer to free-running, 1MHz.
  854. // 0xF9 is 249, the timer divide is base clock / (divide+1)
  855. // so base clock is 250MHz / 250 = 1MHz.
  856. *(timer + TIMER_CONTROL) = 0x0000280 ;
  857. *(timer + TIMER_PRE_DIV) = 0x00000F9 ;
  858. timerIrqRaw = timer + TIMER_IRQ_RAW ;
  859. // Initialise our epoch for millis()
  860. gettimeofday (&tv, NULL) ;
  861. epoch = (tv.tv_sec * 1000000 + tv.tv_usec) / 1000 ;
  862. return 0 ;
  863. }
  864. /*
  865. * wiringPiSetupGpio:
  866. * Must be called once at the start of your program execution.
  867. *
  868. * GPIO setup: Initialises the system into GPIO Pin mode and uses the
  869. * memory mapped hardware directly.
  870. *********************************************************************************
  871. */
  872. int wiringPiSetupGpio (void)
  873. {
  874. int x ;
  875. if (wiringPiDebug)
  876. printf ("wiringPi: wiringPiSetupGpio called\n") ;
  877. if ((x = wiringPiSetup ()) < 0)
  878. return x ;
  879. pinMode = pinModeGpio ;
  880. pullUpDnControl = pullUpDnControlGpio ;
  881. digitalWrite = digitalWriteGpio ;
  882. pwmWrite = pwmWriteGpio ;
  883. setPadDrive = setPadDriveGpio ;
  884. digitalRead = digitalReadGpio ;
  885. waitForInterrupt = waitForInterruptGpio ;
  886. delayMicroseconds = delayMicrosecondsWPi ; // Same
  887. pwmSetMode = pwmSetModeWPi ;
  888. pwmSetRange = pwmSetRangeWPi ;
  889. pwmSetClock = pwmSetClockWPi ;
  890. return 0 ;
  891. }
  892. /*
  893. * wiringPiSetupSys:
  894. * Must be called once at the start of your program execution.
  895. *
  896. * Initialisation (again), however this time we are using the /sys/class/gpio
  897. * interface to the GPIO systems - slightly slower, but always usable as
  898. * a non-root user, assuming the devices are already exported and setup correctly.
  899. */
  900. int wiringPiSetupSys (void)
  901. {
  902. int pin ;
  903. struct timeval tv ;
  904. char fName [128] ;
  905. if (wiringPiDebug)
  906. printf ("wiringPi: wiringPiSetupSys called\n") ;
  907. pinMode = pinModeSys ;
  908. pullUpDnControl = pullUpDnControlSys ;
  909. digitalWrite = digitalWriteSys ;
  910. pwmWrite = pwmWriteSys ;
  911. setPadDrive = setPadDriveSys ;
  912. digitalRead = digitalReadSys ;
  913. waitForInterrupt = waitForInterruptSys ;
  914. delayMicroseconds = delayMicrosecondsSys ;
  915. pwmSetMode = pwmSetModeSys ;
  916. pwmSetRange = pwmSetRangeSys ;
  917. pwmSetClock = pwmSetClockSys ;
  918. // Open and scan the directory, looking for exported GPIOs, and pre-open
  919. // the 'value' interface to speed things up for later
  920. for (pin = 0 ; pin < 64 ; ++pin)
  921. {
  922. sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
  923. sysFds [pin] = open (fName, O_RDWR) ;
  924. }
  925. // Initialise the epoch for mills() ...
  926. gettimeofday (&tv, NULL) ;
  927. epoch = (tv.tv_sec * 1000000 + tv.tv_usec) / 1000 ;
  928. return 0 ;
  929. }