25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wiringPi.c 26 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  1. /*
  2. * wiringPi:
  3. * Arduino compatable (ish) Wiring library for the Raspberry Pi
  4. * Copyright (c) 2012 Gordon Henderson
  5. *
  6. * Thanks to code samples from Gert Jan van Loo and the
  7. * BCM2835 ARM Peripherals manual, however it's missing
  8. * the clock section /grr/mutter/
  9. ***********************************************************************
  10. * This file is part of wiringPi:
  11. * https://projects.drogon.net/raspberry-pi/wiringpi/
  12. *
  13. * wiringPi is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Lesser General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * wiringPi is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Lesser General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public
  24. * License along with wiringPi.
  25. * If not, see <http://www.gnu.org/licenses/>.
  26. ***********************************************************************
  27. */
  28. // Revisions:
  29. // 19 Jul 2012:
  30. // Moved to the LGPL
  31. // Added an abstraction layer to the main routines to save a tiny
  32. // bit of run-time and make the clode a little cleaner (if a little
  33. // larger)
  34. // Added waitForInterrupt code
  35. // Added piHiPri code
  36. //
  37. // 9 Jul 2012:
  38. // Added in support to use the /sys/class/gpio interface.
  39. // 2 Jul 2012:
  40. // Fixed a few more bugs to do with range-checking when in GPIO mode.
  41. // 11 Jun 2012:
  42. // Fixed some typos.
  43. // Added c++ support for the .h file
  44. // Added a new function to allow for using my "pin" numbers, or native
  45. // GPIO pin numbers.
  46. // Removed my busy-loop delay and replaced it with a call to delayMicroseconds
  47. //
  48. // 02 May 2012:
  49. // Added in the 2 UART pins
  50. // Change maxPins to numPins to more accurately reflect purpose
  51. // Pad drive current fiddling
  52. #undef DEBUG_PADS
  53. #include <stdio.h>
  54. #include <stdint.h>
  55. #include <poll.h>
  56. #include <unistd.h>
  57. #include <errno.h>
  58. #include <string.h>
  59. #include <time.h>
  60. #include <fcntl.h>
  61. #include <sys/time.h>
  62. #include <sys/mman.h>
  63. #include <sys/types.h>
  64. #include <sys/stat.h>
  65. #include "wiringPi.h"
  66. // Function stubs
  67. void (*pinMode) (int pin, int mode) ;
  68. void (*pullUpDnControl) (int pin, int pud) ;
  69. void (*digitalWrite) (int pin, int value) ;
  70. void (*pwmWrite) (int pin, int value) ;
  71. void (*setPadDrive) (int group, int value) ;
  72. int (*digitalRead) (int pin) ;
  73. int (*waitForInterrupt) (int pin, int mS) ;
  74. void (*delayMicroseconds) (unsigned int howLong) ;
  75. #ifndef TRUE
  76. #define TRUE (1==1)
  77. #define FALSE (1==2)
  78. #endif
  79. // BCM Magic
  80. #define BCM_PASSWORD 0x5A000000
  81. // Port function select bits
  82. #define FSEL_INPT 0b000
  83. #define FSEL_OUTP 0b001
  84. #define FSEL_ALT0 0b100
  85. #define FSEL_ALT0 0b100
  86. #define FSEL_ALT1 0b101
  87. #define FSEL_ALT2 0b110
  88. #define FSEL_ALT3 0b111
  89. #define FSEL_ALT4 0b011
  90. #define FSEL_ALT5 0b010
  91. // Access from ARM Running Linux
  92. // Take from Gert/Doms code. Some of this is not in the manual
  93. // that I can find )-:
  94. #define BCM2708_PERI_BASE 0x20000000
  95. #define GPIO_PADS (BCM2708_PERI_BASE + 0x100000)
  96. #define CLOCK_BASE (BCM2708_PERI_BASE + 0x101000)
  97. #define GPIO_BASE (BCM2708_PERI_BASE + 0x200000)
  98. #define GPIO_TIMER (BCM2708_PERI_BASE + 0x00B000)
  99. #define GPIO_PWM (BCM2708_PERI_BASE + 0x20C000)
  100. #define PAGE_SIZE (4*1024)
  101. #define BLOCK_SIZE (4*1024)
  102. // PWM
  103. #define PWM_CONTROL 0
  104. #define PWM_STATUS 1
  105. #define PWM0_RANGE 4
  106. #define PWM0_DATA 5
  107. #define PWM1_RANGE 8
  108. #define PWM1_DATA 9
  109. #define PWMCLK_CNTL 40
  110. #define PWMCLK_DIV 41
  111. #define PWM1_MS_MODE 0x8000 // Run in MS mode
  112. #define PWM1_USEFIFO 0x2000 // Data from FIFO
  113. #define PWM1_REVPOLAR 0x1000 // Reverse polarity
  114. #define PWM1_OFFSTATE 0x0800 // Ouput Off state
  115. #define PWM1_REPEATFF 0x0400 // Repeat last value if FIFO empty
  116. #define PWM1_SERIAL 0x0200 // Run in serial mode
  117. #define PWM1_ENABLE 0x0100 // Channel Enable
  118. #define PWM0_MS_MODE 0x0080 // Run in MS mode
  119. #define PWM0_USEFIFO 0x0020 // Data from FIFO
  120. #define PWM0_REVPOLAR 0x0010 // Reverse polarity
  121. #define PWM0_OFFSTATE 0x0008 // Ouput Off state
  122. #define PWM0_REPEATFF 0x0004 // Repeat last value if FIFO empty
  123. #define PWM0_SERIAL 0x0002 // Run in serial mode
  124. #define PWM0_ENABLE 0x0001 // Channel Enable
  125. // Timer
  126. #define TIMER_LOAD (0x400 >> 2)
  127. #define TIMER_VALUE (0x404 >> 2)
  128. #define TIMER_CONTROL (0x408 >> 2)
  129. #define TIMER_IRQ_CLR (0x40C >> 2)
  130. #define TIMER_IRQ_RAW (0x410 >> 2)
  131. #define TIMER_IRQ_MASK (0x414 >> 2)
  132. #define TIMER_RELOAD (0x418 >> 2)
  133. #define TIMER_PRE_DIV (0x41C >> 2)
  134. #define TIMER_COUNTER (0x420 >> 2)
  135. // Locals to hold pointers to the hardware
  136. static volatile uint32_t *gpio ;
  137. static volatile uint32_t *pwm ;
  138. static volatile uint32_t *clk ;
  139. static volatile uint32_t *pads ;
  140. static volatile uint32_t *timer ;
  141. static volatile uint32_t *timerIrqRaw ;
  142. // The BCM2835 has 54 GPIO pins.
  143. // BCM2835 data sheet, Page 90 onwards.
  144. // There are 6 control registers, each control the functions of a block
  145. // of 10 pins.
  146. // Each control register has 10 sets of 3 bits per GPIO pin:
  147. //
  148. // 000 = GPIO Pin X is an input
  149. // 001 = GPIO Pin X is an output
  150. // 100 = GPIO Pin X takes alternate function 0
  151. // 101 = GPIO Pin X takes alternate function 1
  152. // 110 = GPIO Pin X takes alternate function 2
  153. // 111 = GPIO Pin X takes alternate function 3
  154. // 011 = GPIO Pin X takes alternate function 4
  155. // 010 = GPIO Pin X takes alternate function 5
  156. //
  157. // So the 3 bits for port X are:
  158. // X / 10 + ((X % 10) * 3)
  159. // sysFds:
  160. // Map a file descriptor from the /sys/class/gpio/gpioX/value
  161. static int sysFds [64] ;
  162. // Doing it the Arduino way with lookup tables...
  163. // Yes, it's probably more innefficient than all the bit-twidling, but it
  164. // does tend to make it all a bit clearer. At least to me!
  165. // pinToGpio:
  166. // Take a Wiring pin (0 through X) and re-map it to the BCM_GPIO pin
  167. static int pinToGpio [64] =
  168. {
  169. 17, 18, 21, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7
  170. 0, 1, // I2C - SDA0, SCL0
  171. 8, 7, // SPI - CE1, CE0
  172. 10, 9, 11, // SPI - MOSI, MISO, SCLK
  173. 14, 15, // UART - Tx, Rx
  174. // Padding:
  175. -1, -1, -1,-1,-1,-1,-1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 31
  176. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
  177. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
  178. } ;
  179. // gpioToGPFSEL:
  180. // Map a BCM_GPIO pin to it's control port. (GPFSEL 0-5)
  181. static uint8_t gpioToGPFSEL [] =
  182. {
  183. 0,0,0,0,0,0,0,0,0,0,
  184. 1,1,1,1,1,1,1,1,1,1,
  185. 2,2,2,2,2,2,2,2,2,2,
  186. 3,3,3,3,3,3,3,3,3,3,
  187. 4,4,4,4,4,4,4,4,4,4,
  188. 5,5,5,5,5,5,5,5,5,5,
  189. } ;
  190. // gpioToShift
  191. // Define the shift up for the 3 bits per pin in each GPFSEL port
  192. static uint8_t gpioToShift [] =
  193. {
  194. 0,3,6,9,12,15,18,21,24,27,
  195. 0,3,6,9,12,15,18,21,24,27,
  196. 0,3,6,9,12,15,18,21,24,27,
  197. 0,3,6,9,12,15,18,21,24,27,
  198. 0,3,6,9,12,15,18,21,24,27,
  199. } ;
  200. // gpioToGPSET:
  201. // (Word) offset to the GPIO Set registers for each GPIO pin
  202. static uint8_t gpioToGPSET [] =
  203. {
  204. 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,
  205. 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,
  206. } ;
  207. // gpioToGPCLR:
  208. // (Word) offset to the GPIO Clear registers for each GPIO pin
  209. static uint8_t gpioToGPCLR [] =
  210. {
  211. 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,
  212. 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,
  213. } ;
  214. // gpioToGPLEV:
  215. // (Word) offset to the GPIO Input level registers for each GPIO pin
  216. static uint8_t gpioToGPLEV [] =
  217. {
  218. 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,
  219. 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,
  220. } ;
  221. #ifdef notYetReady
  222. // gpioToEDS
  223. // (Word) offset to the Event Detect Status
  224. static uint8_t gpioToEDS [] =
  225. {
  226. 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,
  227. 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,
  228. } ;
  229. // gpioToREN
  230. // (Word) offset to the Rising edgde ENable register
  231. static uint8_t gpioToREN [] =
  232. {
  233. 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,
  234. 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,
  235. } ;
  236. // gpioToFEN
  237. // (Word) offset to the Falling edgde ENable register
  238. static uint8_t gpioToFEN [] =
  239. {
  240. 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,
  241. 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,
  242. } ;
  243. #endif
  244. // gpioToPUDCLK
  245. // (Word) offset to the Pull Up Down Clock regsiter
  246. #define GPPUD 37
  247. static uint8_t gpioToPUDCLK [] =
  248. {
  249. 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,
  250. 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,
  251. } ;
  252. // gpioToPwmALT
  253. // the ALT value to put a GPIO pin into PWM mode
  254. static uint8_t gpioToPwmALT [] =
  255. {
  256. 0, 0, 0, 0, 0, 0, 0, 0, // 0 -> 7
  257. 0, 0, 0, 0, FSEL_ALT0, FSEL_ALT0, 0, 0, // 8 -> 15
  258. 0, 0, FSEL_ALT5, FSEL_ALT5, 0, 0, 0, 0, // 16 -> 23
  259. 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
  260. 0, 0, 0, 0, 0, 0, 0, 0, // 32 -> 39
  261. FSEL_ALT0, FSEL_ALT0, 0, 0, 0, FSEL_ALT0, 0, 0, // 40 -> 47
  262. 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
  263. 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
  264. } ;
  265. static uint8_t gpioToPwmPort [] =
  266. {
  267. 0, 0, 0, 0, 0, 0, 0, 0, // 0 -> 7
  268. 0, 0, 0, 0, PWM0_DATA, PWM1_DATA, 0, 0, // 8 -> 15
  269. 0, 0, PWM0_DATA, PWM1_DATA, 0, 0, 0, 0, // 16 -> 23
  270. 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
  271. 0, 0, 0, 0, 0, 0, 0, 0, // 32 -> 39
  272. PWM0_DATA, PWM1_DATA, 0, 0, 0, PWM1_DATA, 0, 0, // 40 -> 47
  273. 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
  274. 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
  275. } ;
  276. // Time for easy calculations
  277. static unsigned long long epoch ;
  278. //////////////////////////////////////////////////////////////////////////////////
  279. /*
  280. * pinMode:
  281. * Sets the mode of a pin to be input, output or PWM output
  282. *********************************************************************************
  283. */
  284. void pinModeGpio (int pin, int mode)
  285. {
  286. static int pwmRunning = FALSE ;
  287. int fSel, shift, alt ;
  288. pin &= 63 ;
  289. fSel = gpioToGPFSEL [pin] ;
  290. shift = gpioToShift [pin] ;
  291. /**/ if (mode == INPUT)
  292. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) ; // Sets bits to zero = input
  293. else if (mode == OUTPUT)
  294. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (1 << shift) ;
  295. else if (mode == PWM_OUTPUT)
  296. {
  297. if ((alt = gpioToPwmALT [pin]) == 0) // Not a PWM pin
  298. return ;
  299. // Set pin to PWM mode
  300. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ;
  301. // We didn't initialise the PWM hardware at setup time - because it's possible that
  302. // something else is using the PWM - e.g. the Audio systems! So if we use PWM
  303. // here, then we're assuming that nothing else is, otherwise things are going
  304. // to sound a bit funny...
  305. if (!pwmRunning)
  306. {
  307. // Gert/Doms Values
  308. *(clk + PWMCLK_DIV) = BCM_PASSWORD | (32<<12) ; // set pwm div to 32 (19.2/3 = 600KHz)
  309. *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x11 ; // Source=osc and enable
  310. digitalWrite (pin, LOW) ;
  311. *(pwm + PWM_CONTROL) = 0 ; // Disable PWM
  312. delayMicroseconds (10) ;
  313. *(pwm + PWM0_RANGE) = 0x400 ;
  314. delayMicroseconds (10) ;
  315. *(pwm + PWM1_RANGE) = 0x400 ;
  316. delayMicroseconds (10) ;
  317. // Enable PWMs
  318. *(pwm + PWM0_DATA) = 512 ;
  319. *(pwm + PWM1_DATA) = 512 ;
  320. *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ;
  321. pwmRunning = TRUE ;
  322. }
  323. }
  324. // When we change mode of any pin, we remove the pull up/downs
  325. // Or we used to... Hm. Commented out now because for some wieird reason,
  326. // it seems to block subsequent attempts to set the pull up/downs and I've
  327. // not quite gotten to the bottom of why this happens
  328. // The down-side is that the pull up/downs are rememberd in the SoC between
  329. // power cycles, so it's going to be a good idea to explicitly set them in
  330. // any new code.
  331. //
  332. // pullUpDnControl (pin, PUD_OFF) ;
  333. }
  334. void pinModeWPi (int pin, int mode)
  335. {
  336. pinModeGpio (pinToGpio [pin & 63], mode) ;
  337. }
  338. void pinModeSys (int pin, int mode)
  339. {
  340. return ;
  341. }
  342. #ifdef notYetReady
  343. /*
  344. * pinED01:
  345. * pinED10:
  346. * Enables edge-detect mode on a pin - from a 0 to a 1 or 1 to 0
  347. * Pin must already be in input mode with appropriate pull up/downs set.
  348. *********************************************************************************
  349. */
  350. void pinEnableED01Pi (int pin)
  351. {
  352. pin = pinToGpio [pin & 63] ;
  353. }
  354. #endif
  355. /*
  356. * digitalWrite:
  357. * Set an output bit
  358. *********************************************************************************
  359. */
  360. void digitalWriteWPi (int pin, int value)
  361. {
  362. pin = pinToGpio [pin & 63] ;
  363. if (value == LOW)
  364. *(gpio + gpioToGPCLR [pin]) = 1 << (pin & 31) ;
  365. else
  366. *(gpio + gpioToGPSET [pin]) = 1 << (pin & 31) ;
  367. }
  368. void digitalWriteGpio (int pin, int value)
  369. {
  370. pin &= 63 ;
  371. if (value == LOW)
  372. *(gpio + gpioToGPCLR [pin]) = 1 << (pin & 31) ;
  373. else
  374. *(gpio + gpioToGPSET [pin]) = 1 << (pin & 31) ;
  375. }
  376. void digitalWriteSys (int pin, int value)
  377. {
  378. pin &= 63 ;
  379. if (sysFds [pin] != -1)
  380. {
  381. if (value == LOW)
  382. write (sysFds [pin], "0\n", 2) ;
  383. else
  384. write (sysFds [pin], "1\n", 2) ;
  385. }
  386. }
  387. /*
  388. * pwnWrite:
  389. * Set an output PWM value
  390. *********************************************************************************
  391. */
  392. void pwmWriteGpio (int pin, int value)
  393. {
  394. int port ;
  395. pin = pin & 63 ;
  396. port = gpioToPwmPort [pin] ;
  397. *(pwm + port) = value & 0x3FF ;
  398. }
  399. void pwmWriteWPi (int pin, int value)
  400. {
  401. pwmWriteGpio (pinToGpio [pin & 63], value) ;
  402. }
  403. void pwmWriteSys (int pin, int value)
  404. {
  405. return ;
  406. }
  407. /*
  408. * setPadDrive:
  409. * Set the PAD driver value
  410. *********************************************************************************
  411. */
  412. void setPadDriveWPi (int group, int value)
  413. {
  414. uint32_t wrVal ;
  415. if ((group < 0) || (group > 2))
  416. return ;
  417. wrVal = BCM_PASSWORD | 0x18 | (value & 7) ;
  418. *(pads + group + 11) = wrVal ;
  419. #ifdef DEBUG_PADS
  420. printf ("setPadDrive: Group: %d, value: %d (%08X)\n", group, value, wrVal) ;
  421. printf ("Read : %08X\n", *(pads + group + 11)) ;
  422. #endif
  423. }
  424. void setPadDriveGpio (int group, int value)
  425. {
  426. setPadDriveWPi (group, value) ;
  427. }
  428. void setPadDriveSys (int group, int value)
  429. {
  430. return ;
  431. }
  432. /*
  433. * digitalRead:
  434. * Read the value of a given Pin, returning HIGH or LOW
  435. *********************************************************************************
  436. */
  437. int digitalReadWPi (int pin)
  438. {
  439. pin = pinToGpio [pin & 63] ;
  440. if ((*(gpio + gpioToGPLEV [pin]) & (1 << (pin & 31))) != 0)
  441. return HIGH ;
  442. else
  443. return LOW ;
  444. }
  445. int digitalReadGpio (int pin)
  446. {
  447. pin &= 63 ;
  448. if ((*(gpio + gpioToGPLEV [pin]) & (1 << (pin & 31))) != 0)
  449. return HIGH ;
  450. else
  451. return LOW ;
  452. }
  453. int digitalReadSys (int pin)
  454. {
  455. char c ;
  456. pin &= 63 ;
  457. if (sysFds [pin] == -1)
  458. return 0 ;
  459. lseek (sysFds [pin], 0L, SEEK_SET) ;
  460. read (sysFds [pin], &c, 1) ;
  461. return (c == '0') ? 0 : 1 ;
  462. }
  463. /*
  464. * pullUpDownCtrl:
  465. * Control the internal pull-up/down resistors on a GPIO pin
  466. * The Arduino only has pull-ups and these are enabled by writing 1
  467. * to a port when in input mode - this paradigm doesn't quite apply
  468. * here though.
  469. *********************************************************************************
  470. */
  471. void pullUpDnControlGpio (int pin, int pud)
  472. {
  473. pin &= 63 ;
  474. pud &= 3 ;
  475. *(gpio + GPPUD) = pud ; delayMicroseconds (5) ;
  476. *(gpio + gpioToPUDCLK [pin]) = 1 << (pin & 31) ; delayMicroseconds (5) ;
  477. *(gpio + GPPUD) = 0 ; delayMicroseconds (5) ;
  478. *(gpio + gpioToPUDCLK [pin]) = 0 ; delayMicroseconds (5) ;
  479. }
  480. void pullUpDnControlWPi (int pin, int pud)
  481. {
  482. pullUpDnControlGpio (pinToGpio [pin & 63], pud) ;
  483. }
  484. void pullUpDnControlSys (int pin, int pud)
  485. {
  486. return ;
  487. }
  488. /*
  489. * waitForInterrupt:
  490. * Wait for Interrupt on a GPIO pin.
  491. * This is actually done via the /sys/class/gpio interface regardless of
  492. * the wiringPi access mode in-use. Maybe sometime it might get a better
  493. * way for a bit more efficiency.
  494. *********************************************************************************
  495. */
  496. int waitForInterruptSys (int pin, int mS)
  497. {
  498. int fd, x ;
  499. char buf [8] ;
  500. struct pollfd polls ;
  501. if ((fd = sysFds [pin & 63]) == -1)
  502. return -2 ;
  503. // Do a dummy read
  504. x = read (fd, buf, 6) ;
  505. if (x < 0)
  506. return x ;
  507. // And seek
  508. lseek (fd, 0, SEEK_SET) ;
  509. // Setup poll structure
  510. polls.fd = fd ;
  511. polls.events = POLLPRI ; // Urgent data!
  512. // Wait for it ...
  513. return poll (&polls, 1, mS) ;
  514. }
  515. int waitForInterruptWPi (int pin, int mS)
  516. {
  517. return waitForInterruptSys (pinToGpio [pin & 63], mS) ;
  518. }
  519. int waitForInterruptGpio (int pin, int mS)
  520. {
  521. return waitForInterruptSys (pin, mS) ;
  522. }
  523. /*
  524. * delay:
  525. * Wait for some number of milli seconds
  526. *********************************************************************************
  527. */
  528. void delay (unsigned int howLong)
  529. {
  530. struct timespec sleeper, dummy ;
  531. sleeper.tv_sec = (time_t)(howLong / 1000) ;
  532. sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ;
  533. nanosleep (&sleeper, &dummy) ;
  534. }
  535. /*
  536. * delayMicroseconds:
  537. * This is somewhat intersting. It seems that on the Pi, a single call
  538. * to nanosleep takes some 80 to 130 microseconds anyway, so while
  539. * obeying the standards (may take longer), it's not always what we
  540. * want!
  541. *
  542. * So what I'll do now is if the delay is less than 100uS we'll do it
  543. * in a hard loop, watching a built-in counter on the ARM chip. This is
  544. * somewhat sub-optimal in that it uses 100% CPU, something not an issue
  545. * in a microcontroller, but under a multi-tasking, multi-user OS, it's
  546. * wastefull, however we've no real choice )-:
  547. *********************************************************************************
  548. */
  549. void delayMicrosecondsSys (unsigned int howLong)
  550. {
  551. struct timespec sleeper, dummy ;
  552. sleeper.tv_sec = 0 ;
  553. sleeper.tv_nsec = (long)(howLong * 1000) ;
  554. nanosleep (&sleeper, &dummy) ;
  555. }
  556. void delayMicrosecondsHard (unsigned int howLong)
  557. {
  558. *(timer + TIMER_LOAD) = howLong ;
  559. *(timer + TIMER_IRQ_CLR) = 0 ;
  560. while (*timerIrqRaw == 0)
  561. ;
  562. }
  563. void delayMicrosecondsWPi (unsigned int howLong)
  564. {
  565. struct timespec sleeper, dummy ;
  566. /**/ if (howLong == 0)
  567. return ;
  568. else if (howLong < 100)
  569. delayMicrosecondsHard (howLong) ;
  570. else
  571. {
  572. sleeper.tv_sec = 0 ;
  573. sleeper.tv_nsec = (long)(howLong * 1000) ;
  574. nanosleep (&sleeper, &dummy) ;
  575. }
  576. }
  577. /*
  578. * millis:
  579. * Return a number of milliseconds as an unsigned int.
  580. *********************************************************************************
  581. */
  582. unsigned int millis (void)
  583. {
  584. struct timeval tv ;
  585. unsigned long long t1 ;
  586. gettimeofday (&tv, NULL) ;
  587. t1 = (tv.tv_sec * 1000000 + tv.tv_usec) / 1000 ;
  588. return (uint32_t)(t1 - epoch) ;
  589. }
  590. /*
  591. * wiringPiSetup:
  592. * Must be called once at the start of your program execution.
  593. *
  594. * Default setup: Initialises the system into wiringPi Pin mode and uses the
  595. * memory mapped hardware directly.
  596. *********************************************************************************
  597. */
  598. int wiringPiSetup (void)
  599. {
  600. int fd ;
  601. uint8_t *gpioMem, *pwmMem, *clkMem, *padsMem, *timerMem ;
  602. struct timeval tv ;
  603. pinMode = pinModeWPi ;
  604. pullUpDnControl = pullUpDnControlWPi ;
  605. digitalWrite = digitalWriteWPi ;
  606. pwmWrite = pwmWriteWPi ;
  607. setPadDrive = setPadDriveWPi ;
  608. digitalRead = digitalReadWPi ;
  609. waitForInterrupt = waitForInterruptWPi ;
  610. delayMicroseconds = delayMicrosecondsWPi ;
  611. // Open the master /dev/memory device
  612. if ((fd = open ("/dev/mem", O_RDWR | O_SYNC) ) < 0)
  613. {
  614. fprintf (stderr, "wiringPiSetup: Unable to open /dev/mem: %s\n", strerror (errno)) ;
  615. return -1 ;
  616. }
  617. // GPIO:
  618. // Allocate 2 pages - 1 ...
  619. if ((gpioMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
  620. {
  621. fprintf (stderr, "wiringPiSetup: malloc failed: %s\n", strerror (errno)) ;
  622. return -1 ;
  623. }
  624. // ... presumably to make sure we can round it up to a whole page size
  625. if (((uint32_t)gpioMem % PAGE_SIZE) != 0)
  626. gpioMem += PAGE_SIZE - ((uint32_t)gpioMem % PAGE_SIZE) ;
  627. gpio = (uint32_t *)mmap((caddr_t)gpioMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, GPIO_BASE) ;
  628. if ((int32_t)gpio < 0)
  629. {
  630. fprintf (stderr, "wiringPiSetup: mmap failed: %s\n", strerror (errno)) ;
  631. return -1 ;
  632. }
  633. // PWM
  634. if ((pwmMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
  635. {
  636. fprintf (stderr, "wiringPiSetup: pwmMem malloc failed: %s\n", strerror (errno)) ;
  637. return -1 ;
  638. }
  639. if (((uint32_t)pwmMem % PAGE_SIZE) != 0)
  640. pwmMem += PAGE_SIZE - ((uint32_t)pwmMem % PAGE_SIZE) ;
  641. pwm = (uint32_t *)mmap(pwmMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, GPIO_PWM) ;
  642. if ((int32_t)pwm < 0)
  643. {
  644. fprintf (stderr, "wiringPiSetup: mmap failed (pwm): %s\n", strerror (errno)) ;
  645. return -1 ;
  646. }
  647. // Clock control (needed for PWM)
  648. if ((clkMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
  649. {
  650. fprintf (stderr, "wiringPiSetup: clkMem malloc failed: %s\n", strerror (errno)) ;
  651. return -1 ;
  652. }
  653. if (((uint32_t)clkMem % PAGE_SIZE) != 0)
  654. clkMem += PAGE_SIZE - ((uint32_t)clkMem % PAGE_SIZE) ;
  655. clk = (uint32_t *)mmap(clkMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, CLOCK_BASE) ;
  656. if ((int32_t)clk < 0)
  657. {
  658. fprintf (stderr, "wiringPiSetup: mmap failed (clk): %s\n", strerror (errno)) ;
  659. return -1 ;
  660. }
  661. // The drive pads
  662. if ((padsMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
  663. {
  664. fprintf (stderr, "wiringPiSetup: padsMem malloc failed: %s\n", strerror (errno)) ;
  665. return -1 ;
  666. }
  667. if (((uint32_t)padsMem % PAGE_SIZE) != 0)
  668. padsMem += PAGE_SIZE - ((uint32_t)padsMem % PAGE_SIZE) ;
  669. pads = (uint32_t *)mmap(padsMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, GPIO_PADS) ;
  670. if ((int32_t)pads < 0)
  671. {
  672. fprintf (stderr, "wiringPiSetup: mmap failed (pads): %s\n", strerror (errno)) ;
  673. return -1 ;
  674. }
  675. #ifdef DEBUG_PADS
  676. printf ("Checking pads @ 0x%08X\n", (unsigned int)pads) ;
  677. printf (" -> %08X %08X %08X\n", *(pads + 11), *(pads + 12), *(pads + 13)) ;
  678. #endif
  679. // The system timer
  680. if ((timerMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
  681. {
  682. fprintf (stderr, "wiringPiSetup: timerMem malloc failed: %s\n", strerror (errno)) ;
  683. return -1 ;
  684. }
  685. if (((uint32_t)timerMem % PAGE_SIZE) != 0)
  686. timerMem += PAGE_SIZE - ((uint32_t)timerMem % PAGE_SIZE) ;
  687. timer = (uint32_t *)mmap(timerMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, GPIO_TIMER) ;
  688. if ((int32_t)timer < 0)
  689. {
  690. fprintf (stderr, "wiringPiSetup: mmap failed (timer): %s\n", strerror (errno)) ;
  691. return -1 ;
  692. }
  693. // Set the timer to free-running, 1MHz.
  694. // 0xF9 is 249, the timer divide is base clock / (divide+1)
  695. // so base clock is 250MHz / 250 = 1MHz.
  696. *(timer + TIMER_CONTROL) = 0x0000280 ;
  697. *(timer + TIMER_PRE_DIV) = 0x00000F9 ;
  698. timerIrqRaw = timer + TIMER_IRQ_RAW ;
  699. // Initialise our epoch for millis()
  700. gettimeofday (&tv, NULL) ;
  701. epoch = (tv.tv_sec * 1000000 + tv.tv_usec) / 1000 ;
  702. return 0 ;
  703. }
  704. /*
  705. * wiringPiSetupGpio:
  706. * Must be called once at the start of your program execution.
  707. *
  708. * GPIO setup: Initialises the system into GPIO Pin mode and uses the
  709. * memory mapped hardware directly.
  710. *********************************************************************************
  711. */
  712. int wiringPiSetupGpio (void)
  713. {
  714. int x = wiringPiSetup () ;
  715. if (x != 0)
  716. return x ;
  717. pinMode = pinModeGpio ;
  718. pullUpDnControl = pullUpDnControlGpio ;
  719. digitalWrite = digitalWriteGpio ;
  720. pwmWrite = pwmWriteGpio ;
  721. setPadDrive = setPadDriveGpio ;
  722. digitalRead = digitalReadGpio ;
  723. waitForInterrupt = waitForInterruptGpio ;
  724. delayMicroseconds = delayMicrosecondsWPi ; // Same
  725. return 0 ;
  726. }
  727. /*
  728. * wiringPiSetupSys:
  729. * Must be called once at the start of your program execution.
  730. *
  731. * Initialisation (again), however this time we are using the /sys/class/gpio
  732. * interface to the GPIO systems - slightly slower, but always usable as
  733. * a non-root user, assuming the devices are already exported and setup correctly.
  734. */
  735. int wiringPiSetupSys (void)
  736. {
  737. int pin ;
  738. struct timeval tv ;
  739. char fName [128] ;
  740. pinMode = pinModeSys ;
  741. pullUpDnControl = pullUpDnControlSys ;
  742. digitalWrite = digitalWriteSys ;
  743. pwmWrite = pwmWriteSys ;
  744. setPadDrive = setPadDriveSys ;
  745. digitalRead = digitalReadSys ;
  746. waitForInterrupt = waitForInterruptSys ;
  747. delayMicroseconds = delayMicrosecondsSys ;
  748. // Open and scan the directory, looking for exported GPIOs, and pre-open
  749. // the 'value' interface to speed things up for later
  750. for (pin = 0 ; pin < 64 ; ++pin)
  751. {
  752. sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
  753. sysFds [pin] = open (fName, O_RDWR) ;
  754. }
  755. // Initialise the epoch for mills() ...
  756. gettimeofday (&tv, NULL) ;
  757. epoch = (tv.tv_sec * 1000000 + tv.tv_usec) / 1000 ;
  758. return 0 ;
  759. }