wiringPi.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971
  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. #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 ;
  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 quad core 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, "model name", 10) == 0)
  544. break ;
  545. if (strncmp (line, "model name", 10) != 0)
  546. piBoardRevOops ("No \"model name\" line") ;
  547. if (wiringPiDebug)
  548. printf ("piboardRev: Model name: %s\n", line) ;
  549. // See if it's v7
  550. if (strstr (line, "ARMv7") != NULL)
  551. piModel2 = TRUE ;
  552. // Now do the rest of it as before
  553. rewind (cpuFd) ;
  554. while (fgets (line, 120, cpuFd) != NULL)
  555. if (strncmp (line, "Revision", 8) == 0)
  556. break ;
  557. fclose (cpuFd) ;
  558. if (strncmp (line, "Revision", 8) != 0)
  559. piBoardRevOops ("No \"Revision\" line") ;
  560. // Chomp trailing CR/NL
  561. for (c = &line [strlen (line) - 1] ; (*c == '\n') || (*c == '\r') ; --c)
  562. *c = 0 ;
  563. if (wiringPiDebug)
  564. printf ("piboardRev: Revision string: %s\n", line) ;
  565. // Scan to first digit
  566. for (c = line ; *c ; ++c)
  567. if (isdigit (*c))
  568. break ;
  569. if (!isdigit (*c))
  570. piBoardRevOops ("No numeric revision string") ;
  571. // Make sure its long enough
  572. if (strlen (c) < 4)
  573. piBoardRevOops ("Bogus \"Revision\" line (too small)") ;
  574. // If you have overvolted the Pi, then it appears that the revision
  575. // has 100000 added to it!
  576. // The actual condition for it being set is:
  577. // (force_turbo || current_limit_override || temp_limit>85) && over_voltage>0
  578. if (wiringPiDebug)
  579. if (strlen (c) != 4)
  580. printf ("piboardRev: This Pi has/is (force_turbo || current_limit_override || temp_limit>85) && over_voltage>0\n") ;
  581. // Isolate last 4 characters:
  582. c = c + strlen (c) - 4 ;
  583. if (wiringPiDebug)
  584. printf ("piboardRev: last4Chars are: \"%s\"\n", c) ;
  585. if ( (strcmp (c, "0002") == 0) || (strcmp (c, "0003") == 0))
  586. boardRev = 1 ;
  587. else
  588. boardRev = 2 ;
  589. if (wiringPiDebug)
  590. printf ("piBoardRev: Returning revision: %d\n", boardRev) ;
  591. return boardRev ;
  592. }
  593. /*
  594. * piBoardId:
  595. * Do more digging into the board revision string as above, but return
  596. * as much details as we can.
  597. * This is undocumented and really only intended for the GPIO command.
  598. * Use at your own risk!
  599. *********************************************************************************
  600. */
  601. void piBoardId (int *model, int *rev, int *mem, int *maker, int *overVolted)
  602. {
  603. FILE *cpuFd ;
  604. char line [120] ;
  605. char *c ;
  606. (void)piBoardRev () ; // Call this first to make sure all's OK. Don't care about the result.
  607. if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL)
  608. piBoardRevOops ("Unable to open /proc/cpuinfo") ;
  609. while (fgets (line, 120, cpuFd) != NULL)
  610. if (strncmp (line, "Revision", 8) == 0)
  611. break ;
  612. fclose (cpuFd) ;
  613. if (strncmp (line, "Revision", 8) != 0)
  614. piBoardRevOops ("No \"Revision\" line") ;
  615. // Chomp trailing CR/NL
  616. for (c = &line [strlen (line) - 1] ; (*c == '\n') || (*c == '\r') ; --c)
  617. *c = 0 ;
  618. if (wiringPiDebug)
  619. printf ("piboardId: Revision string: %s\n", line) ;
  620. // Scan to first digit
  621. for (c = line ; *c ; ++c)
  622. if (isdigit (*c))
  623. break ;
  624. // Make sure its long enough
  625. if (strlen (c) < 4)
  626. piBoardRevOops ("Bogus \"Revision\" line") ;
  627. // If longer than 4, we'll assume it's been overvolted
  628. *overVolted = strlen (c) > 4 ;
  629. // Extract last 4 characters:
  630. c = c + strlen (c) - 4 ;
  631. // Fill out the replys as appropriate
  632. if (piModel2)
  633. {
  634. /**/ if (strcmp (c, "0010") == 0) { *model = PI_MODEL_2 ; *rev = PI_VERSION_1_1 ; *mem = 1024 ; *maker = PI_MAKER_SONY ; }
  635. else { *model = 0 ; *rev = 0 ; *mem = 0 ; *maker = 0 ; }
  636. }
  637. else
  638. {
  639. /**/ if (strcmp (c, "0002") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1 ; *mem = 256 ; *maker = PI_MAKER_EGOMAN ; }
  640. else if (strcmp (c, "0003") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_1 ; *mem = 256 ; *maker = PI_MAKER_EGOMAN ; }
  641. else if (strcmp (c, "0004") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_SONY ; }
  642. else if (strcmp (c, "0005") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_QISDA ; }
  643. else if (strcmp (c, "0006") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_EGOMAN ; }
  644. else if (strcmp (c, "0007") == 0) { *model = PI_MODEL_A ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_EGOMAN ; }
  645. else if (strcmp (c, "0008") == 0) { *model = PI_MODEL_A ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_SONY ; ; }
  646. else if (strcmp (c, "0009") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_QISDA ; }
  647. else if (strcmp (c, "000d") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 512 ; *maker = PI_MAKER_EGOMAN ; }
  648. else if (strcmp (c, "000e") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 512 ; *maker = PI_MAKER_SONY ; }
  649. else if (strcmp (c, "000f") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 512 ; *maker = PI_MAKER_EGOMAN ; }
  650. else if (strcmp (c, "0010") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 512 ; *maker = PI_MAKER_SONY ; }
  651. else if (strcmp (c, "0011") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_2 ; *mem = 512 ; *maker = PI_MAKER_SONY ; }
  652. else if (strcmp (c, "0012") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_2 ; *mem = 256 ; *maker = PI_MAKER_SONY ; }
  653. else if (strcmp (c, "0013") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 512 ; *maker = PI_MAKER_MBEST ; }
  654. else { *model = 0 ; *rev = 0 ; *mem = 0 ; *maker = 0 ; }
  655. }
  656. }
  657. /*
  658. * wpiPinToGpio:
  659. * Translate a wiringPi Pin number to native GPIO pin number.
  660. * Provided for external support.
  661. *********************************************************************************
  662. */
  663. int wpiPinToGpio (int wpiPin)
  664. {
  665. return pinToGpio [wpiPin & 63] ;
  666. }
  667. /*
  668. * physPinToGpio:
  669. * Translate a physical Pin number to native GPIO pin number.
  670. * Provided for external support.
  671. *********************************************************************************
  672. */
  673. int physPinToGpio (int physPin)
  674. {
  675. return physToGpio [physPin & 63] ;
  676. }
  677. /*
  678. * setPadDrive:
  679. * Set the PAD driver value
  680. *********************************************************************************
  681. */
  682. void setPadDrive (int group, int value)
  683. {
  684. uint32_t wrVal ;
  685. if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
  686. {
  687. if ((group < 0) || (group > 2))
  688. return ;
  689. wrVal = BCM_PASSWORD | 0x18 | (value & 7) ;
  690. *(pads + group + 11) = wrVal ;
  691. if (wiringPiDebug)
  692. {
  693. printf ("setPadDrive: Group: %d, value: %d (%08X)\n", group, value, wrVal) ;
  694. printf ("Read : %08X\n", *(pads + group + 11)) ;
  695. }
  696. }
  697. }
  698. /*
  699. * getAlt:
  700. * Returns the ALT bits for a given port. Only really of-use
  701. * for the gpio readall command (I think)
  702. *********************************************************************************
  703. */
  704. int getAlt (int pin)
  705. {
  706. int fSel, shift, alt ;
  707. pin &= 63 ;
  708. /**/ if (wiringPiMode == WPI_MODE_PINS)
  709. pin = pinToGpio [pin] ;
  710. else if (wiringPiMode == WPI_MODE_PHYS)
  711. pin = physToGpio [pin] ;
  712. else if (wiringPiMode != WPI_MODE_GPIO)
  713. return 0 ;
  714. fSel = gpioToGPFSEL [pin] ;
  715. shift = gpioToShift [pin] ;
  716. alt = (*(gpio + fSel) >> shift) & 7 ;
  717. return alt ;
  718. }
  719. /*
  720. * pwmSetMode:
  721. * Select the native "balanced" mode, or standard mark:space mode
  722. *********************************************************************************
  723. */
  724. void pwmSetMode (int mode)
  725. {
  726. if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
  727. {
  728. if (mode == PWM_MODE_MS)
  729. *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE | PWM0_MS_MODE | PWM1_MS_MODE ;
  730. else
  731. *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ;
  732. }
  733. }
  734. /*
  735. * pwmSetRange:
  736. * Set the PWM range register. We set both range registers to the same
  737. * value. If you want different in your own code, then write your own.
  738. *********************************************************************************
  739. */
  740. void pwmSetRange (unsigned int range)
  741. {
  742. if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
  743. {
  744. *(pwm + PWM0_RANGE) = range ; delayMicroseconds (10) ;
  745. *(pwm + PWM1_RANGE) = range ; delayMicroseconds (10) ;
  746. }
  747. }
  748. /*
  749. * pwmSetClock:
  750. * Set/Change the PWM clock. Originally my code, but changed
  751. * (for the better!) by Chris Hall, <chris@kchall.plus.com>
  752. * after further study of the manual and testing with a 'scope
  753. *********************************************************************************
  754. */
  755. void pwmSetClock (int divisor)
  756. {
  757. uint32_t pwm_control ;
  758. divisor &= 4095 ;
  759. if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
  760. {
  761. if (wiringPiDebug)
  762. printf ("Setting to: %d. Current: 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;
  763. pwm_control = *(pwm + PWM_CONTROL) ; // preserve PWM_CONTROL
  764. // We need to stop PWM prior to stopping PWM clock in MS mode otherwise BUSY
  765. // stays high.
  766. *(pwm + PWM_CONTROL) = 0 ; // Stop PWM
  767. // Stop PWM clock before changing divisor. The delay after this does need to
  768. // this big (95uS occasionally fails, 100uS OK), it's almost as though the BUSY
  769. // flag is not working properly in balanced mode. Without the delay when DIV is
  770. // adjusted the clock sometimes switches to very slow, once slow further DIV
  771. // adjustments do nothing and it's difficult to get out of this mode.
  772. *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x01 ; // Stop PWM Clock
  773. delayMicroseconds (110) ; // prevents clock going sloooow
  774. while ((*(clk + PWMCLK_CNTL) & 0x80) != 0) // Wait for clock to be !BUSY
  775. delayMicroseconds (1) ;
  776. *(clk + PWMCLK_DIV) = BCM_PASSWORD | (divisor << 12) ;
  777. *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x11 ; // Start PWM clock
  778. *(pwm + PWM_CONTROL) = pwm_control ; // restore PWM_CONTROL
  779. if (wiringPiDebug)
  780. printf ("Set to: %d. Now : 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;
  781. }
  782. }
  783. /*
  784. * gpioClockSet:
  785. * Set the freuency on a GPIO clock pin
  786. *********************************************************************************
  787. */
  788. void gpioClockSet (int pin, int freq)
  789. {
  790. int divi, divr, divf ;
  791. pin &= 63 ;
  792. /**/ if (wiringPiMode == WPI_MODE_PINS)
  793. pin = pinToGpio [pin] ;
  794. else if (wiringPiMode == WPI_MODE_PHYS)
  795. pin = physToGpio [pin] ;
  796. else if (wiringPiMode != WPI_MODE_GPIO)
  797. return ;
  798. divi = 19200000 / freq ;
  799. divr = 19200000 % freq ;
  800. divf = (int)((double)divr * 4096.0 / 19200000.0) ;
  801. if (divi > 4095)
  802. divi = 4095 ;
  803. *(clk + gpioToClkCon [pin]) = BCM_PASSWORD | GPIO_CLOCK_SOURCE ; // Stop GPIO Clock
  804. while ((*(clk + gpioToClkCon [pin]) & 0x80) != 0) // ... and wait
  805. ;
  806. *(clk + gpioToClkDiv [pin]) = BCM_PASSWORD | (divi << 12) | divf ; // Set dividers
  807. *(clk + gpioToClkCon [pin]) = BCM_PASSWORD | 0x10 | GPIO_CLOCK_SOURCE ; // Start Clock
  808. }
  809. /*
  810. * wiringPiFindNode:
  811. * Locate our device node
  812. *********************************************************************************
  813. */
  814. struct wiringPiNodeStruct *wiringPiFindNode (int pin)
  815. {
  816. struct wiringPiNodeStruct *node = wiringPiNodes ;
  817. while (node != NULL)
  818. if ((pin >= node->pinBase) && (pin <= node->pinMax))
  819. return node ;
  820. else
  821. node = node->next ;
  822. return NULL ;
  823. }
  824. /*
  825. * wiringPiNewNode:
  826. * Create a new GPIO node into the wiringPi handling system
  827. *********************************************************************************
  828. */
  829. static void pinModeDummy (struct wiringPiNodeStruct *node, int pin, int mode) { return ; }
  830. static void pullUpDnControlDummy (struct wiringPiNodeStruct *node, int pin, int pud) { return ; }
  831. static int digitalReadDummy (struct wiringPiNodeStruct *node, int pin) { return LOW ; }
  832. static void digitalWriteDummy (struct wiringPiNodeStruct *node, int pin, int value) { return ; }
  833. static void pwmWriteDummy (struct wiringPiNodeStruct *node, int pin, int value) { return ; }
  834. static int analogReadDummy (struct wiringPiNodeStruct *node, int pin) { return 0 ; }
  835. static void analogWriteDummy (struct wiringPiNodeStruct *node, int pin, int value) { return ; }
  836. struct wiringPiNodeStruct *wiringPiNewNode (int pinBase, int numPins)
  837. {
  838. int pin ;
  839. struct wiringPiNodeStruct *node ;
  840. // Minimum pin base is 64
  841. if (pinBase < 64)
  842. (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: pinBase of %d is < 64\n", pinBase) ;
  843. // Check all pins in-case there is overlap:
  844. for (pin = pinBase ; pin < (pinBase + numPins) ; ++pin)
  845. if (wiringPiFindNode (pin) != NULL)
  846. (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: Pin %d overlaps with existing definition\n", pin) ;
  847. node = (struct wiringPiNodeStruct *)calloc (sizeof (struct wiringPiNodeStruct), 1) ; // calloc zeros
  848. if (node == NULL)
  849. (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: Unable to allocate memory: %s\n", strerror (errno)) ;
  850. node->pinBase = pinBase ;
  851. node->pinMax = pinBase + numPins - 1 ;
  852. node->pinMode = pinModeDummy ;
  853. node->pullUpDnControl = pullUpDnControlDummy ;
  854. node->digitalRead = digitalReadDummy ;
  855. node->digitalWrite = digitalWriteDummy ;
  856. node->pwmWrite = pwmWriteDummy ;
  857. node->analogRead = analogReadDummy ;
  858. node->analogWrite = analogWriteDummy ;
  859. node->next = wiringPiNodes ;
  860. wiringPiNodes = node ;
  861. return node ;
  862. }
  863. #ifdef notYetReady
  864. /*
  865. * pinED01:
  866. * pinED10:
  867. * Enables edge-detect mode on a pin - from a 0 to a 1 or 1 to 0
  868. * Pin must already be in input mode with appropriate pull up/downs set.
  869. *********************************************************************************
  870. */
  871. void pinEnableED01Pi (int pin)
  872. {
  873. pin = pinToGpio [pin & 63] ;
  874. }
  875. #endif
  876. /*
  877. *********************************************************************************
  878. * Core Functions
  879. *********************************************************************************
  880. */
  881. /*
  882. * pinModeAlt:
  883. * This is an un-documented special to let you set any pin to any mode
  884. *********************************************************************************
  885. */
  886. void pinModeAlt (int pin, int mode)
  887. {
  888. int fSel, shift ;
  889. if ((pin & PI_GPIO_MASK) == 0) // On-board pin
  890. {
  891. /**/ if (wiringPiMode == WPI_MODE_PINS)
  892. pin = pinToGpio [pin] ;
  893. else if (wiringPiMode == WPI_MODE_PHYS)
  894. pin = physToGpio [pin] ;
  895. else if (wiringPiMode != WPI_MODE_GPIO)
  896. return ;
  897. fSel = gpioToGPFSEL [pin] ;
  898. shift = gpioToShift [pin] ;
  899. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | ((mode & 0x7) << shift) ;
  900. }
  901. }
  902. /*
  903. * pinMode:
  904. * Sets the mode of a pin to be input, output or PWM output
  905. *********************************************************************************
  906. */
  907. void pinMode (int pin, int mode)
  908. {
  909. int fSel, shift, alt ;
  910. struct wiringPiNodeStruct *node = wiringPiNodes ;
  911. int origPin = pin ;
  912. if ((pin & PI_GPIO_MASK) == 0) // On-board pin
  913. {
  914. /**/ if (wiringPiMode == WPI_MODE_PINS)
  915. pin = pinToGpio [pin] ;
  916. else if (wiringPiMode == WPI_MODE_PHYS)
  917. pin = physToGpio [pin] ;
  918. else if (wiringPiMode != WPI_MODE_GPIO)
  919. return ;
  920. softPwmStop (origPin) ;
  921. softToneStop (origPin) ;
  922. fSel = gpioToGPFSEL [pin] ;
  923. shift = gpioToShift [pin] ;
  924. /**/ if (mode == INPUT)
  925. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) ; // Sets bits to zero = input
  926. else if (mode == OUTPUT)
  927. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (1 << shift) ;
  928. else if (mode == SOFT_PWM_OUTPUT)
  929. softPwmCreate (origPin, 0, 100) ;
  930. else if (mode == SOFT_TONE_OUTPUT)
  931. softToneCreate (origPin) ;
  932. else if (mode == PWM_TONE_OUTPUT)
  933. {
  934. pinMode (origPin, PWM_OUTPUT) ; // Call myself to enable PWM mode
  935. pwmSetMode (PWM_MODE_MS) ;
  936. }
  937. else if (mode == PWM_OUTPUT)
  938. {
  939. if ((alt = gpioToPwmALT [pin]) == 0) // Not a hardware capable PWM pin
  940. return ;
  941. // Set pin to PWM mode
  942. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ;
  943. delayMicroseconds (110) ; // See comments in pwmSetClockWPi
  944. pwmSetMode (PWM_MODE_BAL) ; // Pi default mode
  945. pwmSetRange (1024) ; // Default range of 1024
  946. pwmSetClock (32) ; // 19.2 / 32 = 600KHz - Also starts the PWM
  947. }
  948. else if (mode == GPIO_CLOCK)
  949. {
  950. if ((alt = gpioToGpClkALT0 [pin]) == 0) // Not a GPIO_CLOCK pin
  951. return ;
  952. // Set pin to GPIO_CLOCK mode and set the clock frequency to 100KHz
  953. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ;
  954. delayMicroseconds (110) ;
  955. gpioClockSet (pin, 100000) ;
  956. }
  957. }
  958. else
  959. {
  960. if ((node = wiringPiFindNode (pin)) != NULL)
  961. node->pinMode (node, pin, mode) ;
  962. return ;
  963. }
  964. }
  965. /*
  966. * pullUpDownCtrl:
  967. * Control the internal pull-up/down resistors on a GPIO pin
  968. * The Arduino only has pull-ups and these are enabled by writing 1
  969. * to a port when in input mode - this paradigm doesn't quite apply
  970. * here though.
  971. *********************************************************************************
  972. */
  973. void pullUpDnControl (int pin, int pud)
  974. {
  975. struct wiringPiNodeStruct *node = wiringPiNodes ;
  976. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  977. {
  978. /**/ if (wiringPiMode == WPI_MODE_PINS)
  979. pin = pinToGpio [pin] ;
  980. else if (wiringPiMode == WPI_MODE_PHYS)
  981. pin = physToGpio [pin] ;
  982. else if (wiringPiMode != WPI_MODE_GPIO)
  983. return ;
  984. *(gpio + GPPUD) = pud & 3 ; delayMicroseconds (5) ;
  985. *(gpio + gpioToPUDCLK [pin]) = 1 << (pin & 31) ; delayMicroseconds (5) ;
  986. *(gpio + GPPUD) = 0 ; delayMicroseconds (5) ;
  987. *(gpio + gpioToPUDCLK [pin]) = 0 ; delayMicroseconds (5) ;
  988. }
  989. else // Extension module
  990. {
  991. if ((node = wiringPiFindNode (pin)) != NULL)
  992. node->pullUpDnControl (node, pin, pud) ;
  993. return ;
  994. }
  995. }
  996. /*
  997. * digitalRead:
  998. * Read the value of a given Pin, returning HIGH or LOW
  999. *********************************************************************************
  1000. */
  1001. int digitalRead (int pin)
  1002. {
  1003. char c ;
  1004. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1005. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1006. {
  1007. /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS) // Sys mode
  1008. {
  1009. if (sysFds [pin] == -1)
  1010. return LOW ;
  1011. lseek (sysFds [pin], 0L, SEEK_SET) ;
  1012. read (sysFds [pin], &c, 1) ;
  1013. return (c == '0') ? LOW : HIGH ;
  1014. }
  1015. else if (wiringPiMode == WPI_MODE_PINS)
  1016. pin = pinToGpio [pin] ;
  1017. else if (wiringPiMode == WPI_MODE_PHYS)
  1018. pin = physToGpio [pin] ;
  1019. else if (wiringPiMode != WPI_MODE_GPIO)
  1020. return LOW ;
  1021. if ((*(gpio + gpioToGPLEV [pin]) & (1 << (pin & 31))) != 0)
  1022. return HIGH ;
  1023. else
  1024. return LOW ;
  1025. }
  1026. else
  1027. {
  1028. if ((node = wiringPiFindNode (pin)) == NULL)
  1029. return LOW ;
  1030. return node->digitalRead (node, pin) ;
  1031. }
  1032. }
  1033. /*
  1034. * digitalWrite:
  1035. * Set an output bit
  1036. *********************************************************************************
  1037. */
  1038. void digitalWrite (int pin, int value)
  1039. {
  1040. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1041. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1042. {
  1043. /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS) // Sys mode
  1044. {
  1045. if (sysFds [pin] != -1)
  1046. {
  1047. if (value == LOW)
  1048. write (sysFds [pin], "0\n", 2) ;
  1049. else
  1050. write (sysFds [pin], "1\n", 2) ;
  1051. }
  1052. return ;
  1053. }
  1054. else if (wiringPiMode == WPI_MODE_PINS)
  1055. pin = pinToGpio [pin] ;
  1056. else if (wiringPiMode == WPI_MODE_PHYS)
  1057. pin = physToGpio [pin] ;
  1058. else if (wiringPiMode != WPI_MODE_GPIO)
  1059. return ;
  1060. if (value == LOW)
  1061. *(gpio + gpioToGPCLR [pin]) = 1 << (pin & 31) ;
  1062. else
  1063. *(gpio + gpioToGPSET [pin]) = 1 << (pin & 31) ;
  1064. }
  1065. else
  1066. {
  1067. if ((node = wiringPiFindNode (pin)) != NULL)
  1068. node->digitalWrite (node, pin, value) ;
  1069. }
  1070. }
  1071. /*
  1072. * pwmWrite:
  1073. * Set an output PWM value
  1074. *********************************************************************************
  1075. */
  1076. void pwmWrite (int pin, int value)
  1077. {
  1078. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1079. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1080. {
  1081. /**/ if (wiringPiMode == WPI_MODE_PINS)
  1082. pin = pinToGpio [pin] ;
  1083. else if (wiringPiMode == WPI_MODE_PHYS)
  1084. pin = physToGpio [pin] ;
  1085. else if (wiringPiMode != WPI_MODE_GPIO)
  1086. return ;
  1087. *(pwm + gpioToPwmPort [pin]) = value ;
  1088. }
  1089. else
  1090. {
  1091. if ((node = wiringPiFindNode (pin)) != NULL)
  1092. node->pwmWrite (node, pin, value) ;
  1093. }
  1094. }
  1095. /*
  1096. * analogRead:
  1097. * Read the analog value of a given Pin.
  1098. * There is no on-board Pi analog hardware,
  1099. * so this needs to go to a new node.
  1100. *********************************************************************************
  1101. */
  1102. int analogRead (int pin)
  1103. {
  1104. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1105. if ((node = wiringPiFindNode (pin)) == NULL)
  1106. return 0 ;
  1107. else
  1108. return node->analogRead (node, pin) ;
  1109. }
  1110. /*
  1111. * analogWrite:
  1112. * Write the analog value to the given Pin.
  1113. * There is no on-board Pi analog hardware,
  1114. * so this needs to go to a new node.
  1115. *********************************************************************************
  1116. */
  1117. void analogWrite (int pin, int value)
  1118. {
  1119. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1120. if ((node = wiringPiFindNode (pin)) == NULL)
  1121. return ;
  1122. node->analogWrite (node, pin, value) ;
  1123. }
  1124. /*
  1125. * pwmToneWrite:
  1126. * Pi Specific.
  1127. * Output the given frequency on the Pi's PWM pin
  1128. *********************************************************************************
  1129. */
  1130. void pwmToneWrite (int pin, int freq)
  1131. {
  1132. int range ;
  1133. if (freq == 0)
  1134. pwmWrite (pin, 0) ; // Off
  1135. else
  1136. {
  1137. range = 600000 / freq ;
  1138. pwmSetRange (range) ;
  1139. pwmWrite (pin, freq / 2) ;
  1140. }
  1141. }
  1142. /*
  1143. * digitalWriteByte:
  1144. * Pi Specific
  1145. * Write an 8-bit byte to the first 8 GPIO pins - try to do it as
  1146. * fast as possible.
  1147. * However it still needs 2 operations to set the bits, so any external
  1148. * hardware must not rely on seeing a change as there will be a change
  1149. * to set the outputs bits to zero, then another change to set the 1's
  1150. *********************************************************************************
  1151. */
  1152. void digitalWriteByte (int value)
  1153. {
  1154. uint32_t pinSet = 0 ;
  1155. uint32_t pinClr = 0 ;
  1156. int mask = 1 ;
  1157. int pin ;
  1158. /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS)
  1159. {
  1160. for (pin = 0 ; pin < 8 ; ++pin)
  1161. {
  1162. digitalWrite (pin, value & mask) ;
  1163. mask <<= 1 ;
  1164. }
  1165. return ;
  1166. }
  1167. else
  1168. {
  1169. for (pin = 0 ; pin < 8 ; ++pin)
  1170. {
  1171. if ((value & mask) == 0)
  1172. pinClr |= (1 << pinToGpio [pin]) ;
  1173. else
  1174. pinSet |= (1 << pinToGpio [pin]) ;
  1175. mask <<= 1 ;
  1176. }
  1177. *(gpio + gpioToGPCLR [0]) = pinClr ;
  1178. *(gpio + gpioToGPSET [0]) = pinSet ;
  1179. }
  1180. }
  1181. /*
  1182. * waitForInterrupt:
  1183. * Pi Specific.
  1184. * Wait for Interrupt on a GPIO pin.
  1185. * This is actually done via the /sys/class/gpio interface regardless of
  1186. * the wiringPi access mode in-use. Maybe sometime it might get a better
  1187. * way for a bit more efficiency.
  1188. *********************************************************************************
  1189. */
  1190. int waitForInterrupt (int pin, int mS)
  1191. {
  1192. int fd, x ;
  1193. uint8_t c ;
  1194. struct pollfd polls ;
  1195. /**/ if (wiringPiMode == WPI_MODE_PINS)
  1196. pin = pinToGpio [pin] ;
  1197. else if (wiringPiMode == WPI_MODE_PHYS)
  1198. pin = physToGpio [pin] ;
  1199. if ((fd = sysFds [pin]) == -1)
  1200. return -2 ;
  1201. // Setup poll structure
  1202. polls.fd = fd ;
  1203. polls.events = POLLPRI ; // Urgent data!
  1204. // Wait for it ...
  1205. x = poll (&polls, 1, mS) ;
  1206. // Do a dummy read to clear the interrupt
  1207. // A one character read appars to be enough.
  1208. // Followed by a seek to reset it.
  1209. (void)read (fd, &c, 1) ;
  1210. lseek (fd, 0, SEEK_SET) ;
  1211. return x ;
  1212. }
  1213. /*
  1214. * interruptHandler:
  1215. * This is a thread and gets started to wait for the interrupt we're
  1216. * hoping to catch. It will call the user-function when the interrupt
  1217. * fires.
  1218. *********************************************************************************
  1219. */
  1220. static void *interruptHandler (void *arg)
  1221. {
  1222. int myPin ;
  1223. (void)piHiPri (55) ; // Only effective if we run as root
  1224. myPin = pinPass ;
  1225. pinPass = -1 ;
  1226. for (;;)
  1227. if (waitForInterrupt (myPin, -1) > 0)
  1228. isrFunctions [myPin] () ;
  1229. return NULL ;
  1230. }
  1231. /*
  1232. * wiringPiISR:
  1233. * Pi Specific.
  1234. * Take the details and create an interrupt handler that will do a call-
  1235. * back to the user supplied function.
  1236. *********************************************************************************
  1237. */
  1238. int wiringPiISR (int pin, int mode, void (*function)(void))
  1239. {
  1240. pthread_t threadId ;
  1241. const char *modeS ;
  1242. char fName [64] ;
  1243. char pinS [8] ;
  1244. pid_t pid ;
  1245. int count, i ;
  1246. char c ;
  1247. int bcmGpioPin ;
  1248. if ((pin < 0) || (pin > 63))
  1249. return wiringPiFailure (WPI_FATAL, "wiringPiISR: pin must be 0-63 (%d)\n", pin) ;
  1250. /**/ if (wiringPiMode == WPI_MODE_UNINITIALISED)
  1251. return wiringPiFailure (WPI_FATAL, "wiringPiISR: wiringPi has not been initialised. Unable to continue.\n") ;
  1252. else if (wiringPiMode == WPI_MODE_PINS)
  1253. bcmGpioPin = pinToGpio [pin] ;
  1254. else if (wiringPiMode == WPI_MODE_PHYS)
  1255. bcmGpioPin = physToGpio [pin] ;
  1256. else
  1257. bcmGpioPin = pin ;
  1258. // Now export the pin and set the right edge
  1259. // We're going to use the gpio program to do this, so it assumes
  1260. // a full installation of wiringPi. It's a bit 'clunky', but it
  1261. // is a way that will work when we're running in "Sys" mode, as
  1262. // a non-root user. (without sudo)
  1263. if (mode != INT_EDGE_SETUP)
  1264. {
  1265. /**/ if (mode == INT_EDGE_FALLING)
  1266. modeS = "falling" ;
  1267. else if (mode == INT_EDGE_RISING)
  1268. modeS = "rising" ;
  1269. else
  1270. modeS = "both" ;
  1271. sprintf (pinS, "%d", bcmGpioPin) ;
  1272. if ((pid = fork ()) < 0) // Fail
  1273. return wiringPiFailure (WPI_FATAL, "wiringPiISR: fork failed: %s\n", strerror (errno)) ;
  1274. if (pid == 0) // Child, exec
  1275. {
  1276. /**/ if (access ("/usr/local/bin/gpio", X_OK) == 0)
  1277. {
  1278. execl ("/usr/local/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ;
  1279. return wiringPiFailure (WPI_FATAL, "wiringPiISR: execl failed: %s\n", strerror (errno)) ;
  1280. }
  1281. else if (access ("/usr/bin/gpio", X_OK) == 0)
  1282. {
  1283. execl ("/usr/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ;
  1284. return wiringPiFailure (WPI_FATAL, "wiringPiISR: execl failed: %s\n", strerror (errno)) ;
  1285. }
  1286. else
  1287. return wiringPiFailure (WPI_FATAL, "wiringPiISR: Can't find gpio program\n") ;
  1288. }
  1289. else // Parent, wait
  1290. wait (NULL) ;
  1291. }
  1292. // Now pre-open the /sys/class node - but it may already be open if
  1293. // we are in Sys mode...
  1294. if (sysFds [bcmGpioPin] == -1)
  1295. {
  1296. sprintf (fName, "/sys/class/gpio/gpio%d/value", bcmGpioPin) ;
  1297. if ((sysFds [bcmGpioPin] = open (fName, O_RDWR)) < 0)
  1298. return wiringPiFailure (WPI_FATAL, "wiringPiISR: unable to open %s: %s\n", fName, strerror (errno)) ;
  1299. }
  1300. // Clear any initial pending interrupt
  1301. ioctl (sysFds [bcmGpioPin], FIONREAD, &count) ;
  1302. for (i = 0 ; i < count ; ++i)
  1303. read (sysFds [bcmGpioPin], &c, 1) ;
  1304. isrFunctions [pin] = function ;
  1305. pthread_mutex_lock (&pinMutex) ;
  1306. pinPass = pin ;
  1307. pthread_create (&threadId, NULL, interruptHandler, NULL) ;
  1308. while (pinPass != -1)
  1309. delay (1) ;
  1310. pthread_mutex_unlock (&pinMutex) ;
  1311. return 0 ;
  1312. }
  1313. /*
  1314. * initialiseEpoch:
  1315. * Initialise our start-of-time variable to be the current unix
  1316. * time in milliseconds and microseconds.
  1317. *********************************************************************************
  1318. */
  1319. static void initialiseEpoch (void)
  1320. {
  1321. struct timeval tv ;
  1322. gettimeofday (&tv, NULL) ;
  1323. epochMilli = (uint64_t)tv.tv_sec * (uint64_t)1000 + (uint64_t)(tv.tv_usec / 1000) ;
  1324. epochMicro = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)(tv.tv_usec) ;
  1325. }
  1326. /*
  1327. * delay:
  1328. * Wait for some number of milliseconds
  1329. *********************************************************************************
  1330. */
  1331. void delay (unsigned int howLong)
  1332. {
  1333. struct timespec sleeper, dummy ;
  1334. sleeper.tv_sec = (time_t)(howLong / 1000) ;
  1335. sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ;
  1336. nanosleep (&sleeper, &dummy) ;
  1337. }
  1338. /*
  1339. * delayMicroseconds:
  1340. * This is somewhat intersting. It seems that on the Pi, a single call
  1341. * to nanosleep takes some 80 to 130 microseconds anyway, so while
  1342. * obeying the standards (may take longer), it's not always what we
  1343. * want!
  1344. *
  1345. * So what I'll do now is if the delay is less than 100uS we'll do it
  1346. * in a hard loop, watching a built-in counter on the ARM chip. This is
  1347. * somewhat sub-optimal in that it uses 100% CPU, something not an issue
  1348. * in a microcontroller, but under a multi-tasking, multi-user OS, it's
  1349. * wastefull, however we've no real choice )-:
  1350. *
  1351. * Plan B: It seems all might not be well with that plan, so changing it
  1352. * to use gettimeofday () and poll on that instead...
  1353. *********************************************************************************
  1354. */
  1355. void delayMicrosecondsHard (unsigned int howLong)
  1356. {
  1357. struct timeval tNow, tLong, tEnd ;
  1358. gettimeofday (&tNow, NULL) ;
  1359. tLong.tv_sec = howLong / 1000000 ;
  1360. tLong.tv_usec = howLong % 1000000 ;
  1361. timeradd (&tNow, &tLong, &tEnd) ;
  1362. while (timercmp (&tNow, &tEnd, <))
  1363. gettimeofday (&tNow, NULL) ;
  1364. }
  1365. void delayMicroseconds (unsigned int howLong)
  1366. {
  1367. struct timespec sleeper ;
  1368. unsigned int uSecs = howLong % 1000000 ;
  1369. unsigned int wSecs = howLong / 1000000 ;
  1370. /**/ if (howLong == 0)
  1371. return ;
  1372. else if (howLong < 100)
  1373. delayMicrosecondsHard (howLong) ;
  1374. else
  1375. {
  1376. sleeper.tv_sec = wSecs ;
  1377. sleeper.tv_nsec = (long)(uSecs * 1000L) ;
  1378. nanosleep (&sleeper, NULL) ;
  1379. }
  1380. }
  1381. /*
  1382. * millis:
  1383. * Return a number of milliseconds as an unsigned int.
  1384. *********************************************************************************
  1385. */
  1386. unsigned int millis (void)
  1387. {
  1388. struct timeval tv ;
  1389. uint64_t now ;
  1390. gettimeofday (&tv, NULL) ;
  1391. now = (uint64_t)tv.tv_sec * (uint64_t)1000 + (uint64_t)(tv.tv_usec / 1000) ;
  1392. return (uint32_t)(now - epochMilli) ;
  1393. }
  1394. /*
  1395. * micros:
  1396. * Return a number of microseconds as an unsigned int.
  1397. *********************************************************************************
  1398. */
  1399. unsigned int micros (void)
  1400. {
  1401. struct timeval tv ;
  1402. uint64_t now ;
  1403. gettimeofday (&tv, NULL) ;
  1404. now = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)tv.tv_usec ;
  1405. return (uint32_t)(now - epochMicro) ;
  1406. }
  1407. /*
  1408. * wiringPiSetup:
  1409. * Must be called once at the start of your program execution.
  1410. *
  1411. * Default setup: Initialises the system into wiringPi Pin mode and uses the
  1412. * memory mapped hardware directly.
  1413. *
  1414. * Changed now to revert to "gpio" mode if we're running on a Compute Module.
  1415. *********************************************************************************
  1416. */
  1417. int wiringPiSetup (void)
  1418. {
  1419. int fd ;
  1420. int boardRev ;
  1421. int model, rev, mem, maker, overVolted ;
  1422. if (getenv (ENV_DEBUG) != NULL)
  1423. wiringPiDebug = TRUE ;
  1424. if (getenv (ENV_CODES) != NULL)
  1425. wiringPiReturnCodes = TRUE ;
  1426. if (geteuid () != 0)
  1427. (void)wiringPiFailure (WPI_FATAL, "wiringPiSetup: Must be root. (Did you forget sudo?)\n") ;
  1428. if (wiringPiDebug)
  1429. printf ("wiringPi: wiringPiSetup called\n") ;
  1430. boardRev = piBoardRev () ;
  1431. /**/ if (boardRev == 1) // A, B, Rev 1, 1.1
  1432. {
  1433. pinToGpio = pinToGpioR1 ;
  1434. physToGpio = physToGpioR1 ;
  1435. }
  1436. else // A, B, Rev 2, B+, CM, Pi2
  1437. {
  1438. if (piModel2)
  1439. BCM2708_PERI_BASE = 0x3F000000 ;
  1440. pinToGpio = pinToGpioR2 ;
  1441. physToGpio = physToGpioR2 ;
  1442. }
  1443. // Open the master /dev/memory device
  1444. if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0)
  1445. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: Unable to open /dev/mem: %s\n", strerror (errno)) ;
  1446. // GPIO:
  1447. gpio = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_BASE) ;
  1448. if ((int32_t)gpio == -1)
  1449. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (GPIO) failed: %s\n", strerror (errno)) ;
  1450. // PWM
  1451. pwm = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PWM) ;
  1452. if ((int32_t)pwm == -1)
  1453. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PWM) failed: %s\n", strerror (errno)) ;
  1454. // Clock control (needed for PWM)
  1455. clk = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, CLOCK_BASE) ;
  1456. if ((int32_t)clk == -1)
  1457. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (CLOCK) failed: %s\n", strerror (errno)) ;
  1458. // The drive pads
  1459. pads = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PADS) ;
  1460. if ((int32_t)pads == -1)
  1461. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PADS) failed: %s\n", strerror (errno)) ;
  1462. #ifdef USE_TIMER
  1463. // The system timer
  1464. timer = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_TIMER) ;
  1465. if ((int32_t)timer == -1)
  1466. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (TIMER) failed: %s\n", strerror (errno)) ;
  1467. // Set the timer to free-running, 1MHz.
  1468. // 0xF9 is 249, the timer divide is base clock / (divide+1)
  1469. // so base clock is 250MHz / 250 = 1MHz.
  1470. *(timer + TIMER_CONTROL) = 0x0000280 ;
  1471. *(timer + TIMER_PRE_DIV) = 0x00000F9 ;
  1472. timerIrqRaw = timer + TIMER_IRQ_RAW ;
  1473. #endif
  1474. initialiseEpoch () ;
  1475. // If we're running on a compute module, then wiringPi pin numbers don't really many anything...
  1476. piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
  1477. if (model == PI_MODEL_CM)
  1478. wiringPiMode = WPI_MODE_GPIO ;
  1479. else
  1480. wiringPiMode = WPI_MODE_PINS ;
  1481. return 0 ;
  1482. }
  1483. /*
  1484. * wiringPiSetupGpio:
  1485. * Must be called once at the start of your program execution.
  1486. *
  1487. * GPIO setup: Initialises the system into GPIO Pin mode and uses the
  1488. * memory mapped hardware directly.
  1489. *********************************************************************************
  1490. */
  1491. int wiringPiSetupGpio (void)
  1492. {
  1493. (void)wiringPiSetup () ;
  1494. if (wiringPiDebug)
  1495. printf ("wiringPi: wiringPiSetupGpio called\n") ;
  1496. wiringPiMode = WPI_MODE_GPIO ;
  1497. return 0 ;
  1498. }
  1499. /*
  1500. * wiringPiSetupPhys:
  1501. * Must be called once at the start of your program execution.
  1502. *
  1503. * Phys setup: Initialises the system into Physical Pin mode and uses the
  1504. * memory mapped hardware directly.
  1505. *********************************************************************************
  1506. */
  1507. int wiringPiSetupPhys (void)
  1508. {
  1509. (void)wiringPiSetup () ;
  1510. if (wiringPiDebug)
  1511. printf ("wiringPi: wiringPiSetupPhys called\n") ;
  1512. wiringPiMode = WPI_MODE_PHYS ;
  1513. return 0 ;
  1514. }
  1515. /*
  1516. * wiringPiSetupSys:
  1517. * Must be called once at the start of your program execution.
  1518. *
  1519. * Initialisation (again), however this time we are using the /sys/class/gpio
  1520. * interface to the GPIO systems - slightly slower, but always usable as
  1521. * a non-root user, assuming the devices are already exported and setup correctly.
  1522. */
  1523. int wiringPiSetupSys (void)
  1524. {
  1525. int boardRev ;
  1526. int pin ;
  1527. char fName [128] ;
  1528. if (getenv (ENV_DEBUG) != NULL)
  1529. wiringPiDebug = TRUE ;
  1530. if (getenv (ENV_CODES) != NULL)
  1531. wiringPiReturnCodes = TRUE ;
  1532. if (wiringPiDebug)
  1533. printf ("wiringPi: wiringPiSetupSys called\n") ;
  1534. boardRev = piBoardRev () ;
  1535. if (boardRev == 1)
  1536. {
  1537. pinToGpio = pinToGpioR1 ;
  1538. physToGpio = physToGpioR1 ;
  1539. }
  1540. else
  1541. {
  1542. pinToGpio = pinToGpioR2 ;
  1543. physToGpio = physToGpioR2 ;
  1544. }
  1545. // Open and scan the directory, looking for exported GPIOs, and pre-open
  1546. // the 'value' interface to speed things up for later
  1547. for (pin = 0 ; pin < 64 ; ++pin)
  1548. {
  1549. sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
  1550. sysFds [pin] = open (fName, O_RDWR) ;
  1551. }
  1552. initialiseEpoch () ;
  1553. wiringPiMode = WPI_MODE_GPIO_SYS ;
  1554. return 0 ;
  1555. }