wiringPi.c 35 KB

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