Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

wiringPi.c 31 KiB

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