Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 

1691 Zeilen
47 KiB

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