wiringPi.c 56 KB

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