Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

wiringPi.c 38 KiB

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