Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 

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