Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373
  1. /*
  2. * wiringPi:
  3. * Arduino look-a-like Wiring library for the Raspberry Pi
  4. * Copyright (c) 2012-2017 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 <asm/ioctl.h>
  70. #include "softPwm.h"
  71. #include "softTone.h"
  72. #include "wiringPi.h"
  73. #include "../version.h"
  74. // Environment Variables
  75. #define ENV_DEBUG "WIRINGPI_DEBUG"
  76. #define ENV_CODES "WIRINGPI_CODES"
  77. #define ENV_GPIOMEM "WIRINGPI_GPIOMEM"
  78. // Extend wiringPi with other pin-based devices and keep track of
  79. // them in this structure
  80. struct wiringPiNodeStruct *wiringPiNodes = NULL ;
  81. // BCM Magic
  82. #define BCM_PASSWORD 0x5A000000
  83. // The BCM2835 has 54 GPIO pins.
  84. // BCM2835 data sheet, Page 90 onwards.
  85. // There are 6 control registers, each control the functions of a block
  86. // of 10 pins.
  87. // Each control register has 10 sets of 3 bits per GPIO pin - the ALT values
  88. //
  89. // 000 = GPIO Pin X is an input
  90. // 001 = GPIO Pin X is an output
  91. // 100 = GPIO Pin X takes alternate function 0
  92. // 101 = GPIO Pin X takes alternate function 1
  93. // 110 = GPIO Pin X takes alternate function 2
  94. // 111 = GPIO Pin X takes alternate function 3
  95. // 011 = GPIO Pin X takes alternate function 4
  96. // 010 = GPIO Pin X takes alternate function 5
  97. //
  98. // So the 3 bits for port X are:
  99. // X / 10 + ((X % 10) * 3)
  100. // Port function select bits
  101. #define FSEL_INPT 0b000
  102. #define FSEL_OUTP 0b001
  103. #define FSEL_ALT0 0b100
  104. #define FSEL_ALT1 0b101
  105. #define FSEL_ALT2 0b110
  106. #define FSEL_ALT3 0b111
  107. #define FSEL_ALT4 0b011
  108. #define FSEL_ALT5 0b010
  109. // Access from ARM Running Linux
  110. // Taken from Gert/Doms code. Some of this is not in the manual
  111. // that I can find )-:
  112. //
  113. // Updates in September 2015 - all now static variables (and apologies for the caps)
  114. // due to the Pi v2, v3, etc. and the new /dev/gpiomem interface
  115. static volatile unsigned int GPIO_PADS ;
  116. static volatile unsigned int GPIO_CLOCK_BASE ;
  117. static volatile unsigned int GPIO_BASE ;
  118. static volatile unsigned int GPIO_TIMER ;
  119. static volatile unsigned int GPIO_PWM ;
  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. // piGpioBase:
  172. // The base address of the GPIO memory mapped hardware IO
  173. #define GPIO_PERI_BASE_OLD 0x20000000
  174. #define GPIO_PERI_BASE_NEW 0x3F000000
  175. static volatile unsigned int piGpioBase = 0 ;
  176. const char *piModelNames [16] =
  177. {
  178. "Model A", // 0
  179. "Model B", // 1
  180. "Model A+", // 2
  181. "Model B+", // 3
  182. "Pi 2", // 4
  183. "Alpha", // 5
  184. "CM", // 6
  185. "Unknown07", // 07
  186. "Pi 3", // 08
  187. "Pi Zero", // 09
  188. "CM3", // 10
  189. "Unknown11", // 11
  190. "Pi Zero-W", // 12
  191. "Unknown13", // 13
  192. "Unknown14", // 14
  193. "Unknown15", // 15
  194. } ;
  195. const char *piRevisionNames [16] =
  196. {
  197. "00",
  198. "01",
  199. "02",
  200. "03",
  201. "04",
  202. "05",
  203. "06",
  204. "07",
  205. "08",
  206. "09",
  207. "10",
  208. "11",
  209. "12",
  210. "13",
  211. "14",
  212. "15",
  213. } ;
  214. const char *piMakerNames [16] =
  215. {
  216. "Sony", // 0
  217. "Egoman", // 1
  218. "Embest", // 2
  219. "Unknown", // 3
  220. "Embest", // 4
  221. "Unknown05", // 5
  222. "Unknown06", // 6
  223. "Unknown07", // 7
  224. "Unknown08", // 8
  225. "Unknown09", // 9
  226. "Unknown10", // 10
  227. "Unknown11", // 11
  228. "Unknown12", // 12
  229. "Unknown13", // 13
  230. "Unknown14", // 14
  231. "Unknown15", // 15
  232. } ;
  233. const int piMemorySize [8] =
  234. {
  235. 256, // 0
  236. 512, // 1
  237. 1024, // 2
  238. 0, // 3
  239. 0, // 4
  240. 0, // 5
  241. 0, // 6
  242. 0, // 7
  243. } ;
  244. // Time for easy calculations
  245. static uint64_t epochMilli, epochMicro ;
  246. // Misc
  247. static int wiringPiMode = WPI_MODE_UNINITIALISED ;
  248. static volatile int pinPass = -1 ;
  249. static pthread_mutex_t pinMutex ;
  250. // Debugging & Return codes
  251. int wiringPiDebug = FALSE ;
  252. int wiringPiReturnCodes = FALSE ;
  253. // Use /dev/gpiomem ?
  254. int wiringPiTryGpioMem = FALSE ;
  255. // sysFds:
  256. // Map a file descriptor from the /sys/class/gpio/gpioX/value
  257. static int sysFds [64] =
  258. {
  259. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  260. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  261. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  262. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  263. } ;
  264. // ISR Data
  265. static void (*isrFunctions [64])(void) ;
  266. // Doing it the Arduino way with lookup tables...
  267. // Yes, it's probably more innefficient than all the bit-twidling, but it
  268. // does tend to make it all a bit clearer. At least to me!
  269. // pinToGpio:
  270. // Take a Wiring pin (0 through X) and re-map it to the BCM_GPIO pin
  271. // Cope for 3 different board revisions here.
  272. static int *pinToGpio ;
  273. // Revision 1, 1.1:
  274. static int pinToGpioR1 [64] =
  275. {
  276. 17, 18, 21, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7: wpi 0 - 7
  277. 0, 1, // I2C - SDA1, SCL1 wpi 8 - 9
  278. 8, 7, // SPI - CE1, CE0 wpi 10 - 11
  279. 10, 9, 11, // SPI - MOSI, MISO, SCLK wpi 12 - 14
  280. 14, 15, // UART - Tx, Rx wpi 15 - 16
  281. // Padding:
  282. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 31
  283. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
  284. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
  285. } ;
  286. // Revision 2:
  287. static int pinToGpioR2 [64] =
  288. {
  289. 17, 18, 27, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7: wpi 0 - 7
  290. 2, 3, // I2C - SDA0, SCL0 wpi 8 - 9
  291. 8, 7, // SPI - CE1, CE0 wpi 10 - 11
  292. 10, 9, 11, // SPI - MOSI, MISO, SCLK wpi 12 - 14
  293. 14, 15, // UART - Tx, Rx wpi 15 - 16
  294. 28, 29, 30, 31, // Rev 2: New GPIOs 8 though 11 wpi 17 - 20
  295. 5, 6, 13, 19, 26, // B+ wpi 21, 22, 23, 24, 25
  296. 12, 16, 20, 21, // B+ wpi 26, 27, 28, 29
  297. 0, 1, // B+ wpi 30, 31
  298. // Padding:
  299. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
  300. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
  301. } ;
  302. // physToGpio:
  303. // Take a physical pin (1 through 26) and re-map it to the BCM_GPIO pin
  304. // Cope for 2 different board revisions here.
  305. // Also add in the P5 connector, so the P5 pins are 3,4,5,6, so 53,54,55,56
  306. static int *physToGpio ;
  307. static int physToGpioR1 [64] =
  308. {
  309. -1, // 0
  310. -1, -1, // 1, 2
  311. 0, -1,
  312. 1, -1,
  313. 4, 14,
  314. -1, 15,
  315. 17, 18,
  316. 21, -1,
  317. 22, 23,
  318. -1, 24,
  319. 10, -1,
  320. 9, 25,
  321. 11, 8,
  322. -1, 7, // 25, 26
  323. -1, -1, -1, -1, -1, // ... 31
  324. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
  325. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
  326. } ;
  327. static int physToGpioR2 [64] =
  328. {
  329. -1, // 0
  330. -1, -1, // 1, 2
  331. 2, -1,
  332. 3, -1,
  333. 4, 14,
  334. -1, 15,
  335. 17, 18,
  336. 27, -1,
  337. 22, 23,
  338. -1, 24,
  339. 10, -1,
  340. 9, 25,
  341. 11, 8,
  342. -1, 7, // 25, 26
  343. // B+
  344. 0, 1,
  345. 5, -1,
  346. 6, 12,
  347. 13, -1,
  348. 19, 16,
  349. 26, 20,
  350. -1, 21,
  351. // the P5 connector on the Rev 2 boards:
  352. -1, -1,
  353. -1, -1,
  354. -1, -1,
  355. -1, -1,
  356. -1, -1,
  357. 28, 29,
  358. 30, 31,
  359. -1, -1,
  360. -1, -1,
  361. -1, -1,
  362. -1, -1,
  363. } ;
  364. // gpioToGPFSEL:
  365. // Map a BCM_GPIO pin to it's Function Selection
  366. // control port. (GPFSEL 0-5)
  367. // Groups of 10 - 3 bits per Function - 30 bits per port
  368. static uint8_t gpioToGPFSEL [] =
  369. {
  370. 0,0,0,0,0,0,0,0,0,0,
  371. 1,1,1,1,1,1,1,1,1,1,
  372. 2,2,2,2,2,2,2,2,2,2,
  373. 3,3,3,3,3,3,3,3,3,3,
  374. 4,4,4,4,4,4,4,4,4,4,
  375. 5,5,5,5,5,5,5,5,5,5,
  376. } ;
  377. // gpioToShift
  378. // Define the shift up for the 3 bits per pin in each GPFSEL port
  379. static uint8_t gpioToShift [] =
  380. {
  381. 0,3,6,9,12,15,18,21,24,27,
  382. 0,3,6,9,12,15,18,21,24,27,
  383. 0,3,6,9,12,15,18,21,24,27,
  384. 0,3,6,9,12,15,18,21,24,27,
  385. 0,3,6,9,12,15,18,21,24,27,
  386. 0,3,6,9,12,15,18,21,24,27,
  387. } ;
  388. // gpioToGPSET:
  389. // (Word) offset to the GPIO Set registers for each GPIO pin
  390. static uint8_t gpioToGPSET [] =
  391. {
  392. 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,
  393. 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,
  394. } ;
  395. // gpioToGPCLR:
  396. // (Word) offset to the GPIO Clear registers for each GPIO pin
  397. static uint8_t gpioToGPCLR [] =
  398. {
  399. 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,
  400. 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,
  401. } ;
  402. // gpioToGPLEV:
  403. // (Word) offset to the GPIO Input level registers for each GPIO pin
  404. static uint8_t gpioToGPLEV [] =
  405. {
  406. 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,
  407. 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,
  408. } ;
  409. #ifdef notYetReady
  410. // gpioToEDS
  411. // (Word) offset to the Event Detect Status
  412. static uint8_t gpioToEDS [] =
  413. {
  414. 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,
  415. 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,
  416. } ;
  417. // gpioToREN
  418. // (Word) offset to the Rising edge ENable register
  419. static uint8_t gpioToREN [] =
  420. {
  421. 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,
  422. 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,
  423. } ;
  424. // gpioToFEN
  425. // (Word) offset to the Falling edgde ENable register
  426. static uint8_t gpioToFEN [] =
  427. {
  428. 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,
  429. 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,
  430. } ;
  431. #endif
  432. // GPPUD:
  433. // GPIO Pin pull up/down register
  434. #define GPPUD 37
  435. // gpioToPUDCLK
  436. // (Word) offset to the Pull Up Down Clock regsiter
  437. static uint8_t gpioToPUDCLK [] =
  438. {
  439. 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,
  440. 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,
  441. } ;
  442. // gpioToPwmALT
  443. // the ALT value to put a GPIO pin into PWM mode
  444. static uint8_t gpioToPwmALT [] =
  445. {
  446. 0, 0, 0, 0, 0, 0, 0, 0, // 0 -> 7
  447. 0, 0, 0, 0, FSEL_ALT0, FSEL_ALT0, 0, 0, // 8 -> 15
  448. 0, 0, FSEL_ALT5, FSEL_ALT5, 0, 0, 0, 0, // 16 -> 23
  449. 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
  450. 0, 0, 0, 0, 0, 0, 0, 0, // 32 -> 39
  451. FSEL_ALT0, FSEL_ALT0, 0, 0, 0, FSEL_ALT0, 0, 0, // 40 -> 47
  452. 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
  453. 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
  454. } ;
  455. // gpioToPwmPort
  456. // The port value to put a GPIO pin into PWM mode
  457. static uint8_t gpioToPwmPort [] =
  458. {
  459. 0, 0, 0, 0, 0, 0, 0, 0, // 0 -> 7
  460. 0, 0, 0, 0, PWM0_DATA, PWM1_DATA, 0, 0, // 8 -> 15
  461. 0, 0, PWM0_DATA, PWM1_DATA, 0, 0, 0, 0, // 16 -> 23
  462. 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
  463. 0, 0, 0, 0, 0, 0, 0, 0, // 32 -> 39
  464. PWM0_DATA, PWM1_DATA, 0, 0, 0, PWM1_DATA, 0, 0, // 40 -> 47
  465. 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
  466. 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
  467. } ;
  468. // gpioToGpClkALT:
  469. // ALT value to put a GPIO pin into GP Clock mode.
  470. // On the Pi we can really only use BCM_GPIO_4 and BCM_GPIO_21
  471. // for clocks 0 and 1 respectively, however I'll include the full
  472. // list for completeness - maybe one day...
  473. #define GPIO_CLOCK_SOURCE 1
  474. // gpioToGpClkALT0:
  475. static uint8_t gpioToGpClkALT0 [] =
  476. {
  477. 0, 0, 0, 0, FSEL_ALT0, FSEL_ALT0, FSEL_ALT0, 0, // 0 -> 7
  478. 0, 0, 0, 0, 0, 0, 0, 0, // 8 -> 15
  479. 0, 0, 0, 0, FSEL_ALT5, FSEL_ALT5, 0, 0, // 16 -> 23
  480. 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
  481. FSEL_ALT0, 0, FSEL_ALT0, 0, 0, 0, 0, 0, // 32 -> 39
  482. 0, 0, FSEL_ALT0, FSEL_ALT0, FSEL_ALT0, 0, 0, 0, // 40 -> 47
  483. 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
  484. 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
  485. } ;
  486. // gpioToClk:
  487. // (word) Offsets to the clock Control and Divisor register
  488. static uint8_t gpioToClkCon [] =
  489. {
  490. -1, -1, -1, -1, 28, 30, 32, -1, // 0 -> 7
  491. -1, -1, -1, -1, -1, -1, -1, -1, // 8 -> 15
  492. -1, -1, -1, -1, 28, 30, -1, -1, // 16 -> 23
  493. -1, -1, -1, -1, -1, -1, -1, -1, // 24 -> 31
  494. 28, -1, 28, -1, -1, -1, -1, -1, // 32 -> 39
  495. -1, -1, 28, 30, 28, -1, -1, -1, // 40 -> 47
  496. -1, -1, -1, -1, -1, -1, -1, -1, // 48 -> 55
  497. -1, -1, -1, -1, -1, -1, -1, -1, // 56 -> 63
  498. } ;
  499. static uint8_t gpioToClkDiv [] =
  500. {
  501. -1, -1, -1, -1, 29, 31, 33, -1, // 0 -> 7
  502. -1, -1, -1, -1, -1, -1, -1, -1, // 8 -> 15
  503. -1, -1, -1, -1, 29, 31, -1, -1, // 16 -> 23
  504. -1, -1, -1, -1, -1, -1, -1, -1, // 24 -> 31
  505. 29, -1, 29, -1, -1, -1, -1, -1, // 32 -> 39
  506. -1, -1, 29, 31, 29, -1, -1, -1, // 40 -> 47
  507. -1, -1, -1, -1, -1, -1, -1, -1, // 48 -> 55
  508. -1, -1, -1, -1, -1, -1, -1, -1, // 56 -> 63
  509. } ;
  510. /*
  511. * Functions
  512. *********************************************************************************
  513. */
  514. /*
  515. * wiringPiFailure:
  516. * Fail. Or not.
  517. *********************************************************************************
  518. */
  519. int wiringPiFailure (int fatal, const char *message, ...)
  520. {
  521. va_list argp ;
  522. char buffer [1024] ;
  523. if (!fatal && wiringPiReturnCodes)
  524. return -1 ;
  525. va_start (argp, message) ;
  526. vsnprintf (buffer, 1023, message, argp) ;
  527. va_end (argp) ;
  528. fprintf (stderr, "%s", buffer) ;
  529. exit (EXIT_FAILURE) ;
  530. return 0 ;
  531. }
  532. /*
  533. * piGpioLayout:
  534. * Return a number representing the hardware revision of the board.
  535. * This is not strictly the board revision but is used to check the
  536. * layout of the GPIO connector - and there are 2 types that we are
  537. * really interested in here. The very earliest Pi's and the
  538. * ones that came after that which switched some pins ....
  539. *
  540. * Revision 1 really means the early Model A and B's.
  541. * Revision 2 is everything else - it covers the B, B+ and CM.
  542. * ... and the Pi 2 - which is a B+ ++ ...
  543. * ... and the Pi 0 - which is an A+ ...
  544. *
  545. * The main difference between the revision 1 and 2 system that I use here
  546. * is the mapping of the GPIO pins. From revision 2, the Pi Foundation changed
  547. * 3 GPIO pins on the (original) 26-way header - BCM_GPIO 22 was dropped and
  548. * replaced with 27, and 0 + 1 - I2C bus 0 was changed to 2 + 3; I2C bus 1.
  549. *
  550. * Additionally, here we set the piModel2 flag too. This is again, nothing to
  551. * do with the actual model, but the major version numbers - the GPIO base
  552. * hardware address changed at model 2 and above (not the Zero though)
  553. *
  554. *********************************************************************************
  555. */
  556. static void piGpioLayoutOops (const char *why)
  557. {
  558. fprintf (stderr, "Oops: Unable to determine board revision from /proc/cpuinfo\n") ;
  559. fprintf (stderr, " -> %s\n", why) ;
  560. fprintf (stderr, " -> You'd best google the error to find out why.\n") ;
  561. //fprintf (stderr, " -> http://www.raspberrypi.org/phpBB3/viewtopic.php?p=184410#p184410\n") ;
  562. exit (EXIT_FAILURE) ;
  563. }
  564. int piGpioLayout (void)
  565. {
  566. FILE *cpuFd ;
  567. char line [120] ;
  568. char *c ;
  569. static int gpioLayout = -1 ;
  570. if (gpioLayout != -1) // No point checking twice
  571. return gpioLayout ;
  572. if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL)
  573. piGpioLayoutOops ("Unable to open /proc/cpuinfo") ;
  574. // Start by looking for the Architecture to make sure we're really running
  575. // on a Pi. I'm getting fed-up with people whinging at me because
  576. // they can't get it to work on weirdFruitPi boards...
  577. while (fgets (line, 120, cpuFd) != NULL)
  578. if (strncmp (line, "Hardware", 8) == 0)
  579. break ;
  580. if (strncmp (line, "Hardware", 8) != 0)
  581. piGpioLayoutOops ("No \"Hardware\" line") ;
  582. if (wiringPiDebug)
  583. printf ("piGpioLayout: Hardware: %s\n", line) ;
  584. // See if it's BCM2708 or BCM2709 or the new BCM2835.
  585. // OK. As of Kernel 4.8, we have BCM2835 only, regardless of model.
  586. // However I still want to check because it will trap the cheapskates and rip-
  587. // off merchants who want to use wiringPi on non-Raspberry Pi platforms - which
  588. // I do not support so don't email me your bleating whinges about anything
  589. // other than a genuine Raspberry Pi.
  590. if (! (strstr (line, "BCM2708") || strstr (line, "BCM2709") || strstr (line, "BCM2835")))
  591. {
  592. fprintf (stderr, "Unable to determine hardware version. I see: %s,\n", line) ;
  593. fprintf (stderr, " - expecting BCM2708, BCM2709 or BCM2835.\n") ;
  594. fprintf (stderr, "If this is a genuine Raspberry Pi then please report this\n") ;
  595. fprintf (stderr, "to projects@drogon.net. If this is not a Raspberry Pi then you\n") ;
  596. fprintf (stderr, "are on your own as wiringPi is designed to support the\n") ;
  597. fprintf (stderr, "Raspberry Pi ONLY.\n") ;
  598. exit (EXIT_FAILURE) ;
  599. }
  600. // Right - we're Probably on a Raspberry Pi. Check the revision field for the real
  601. // hardware type
  602. // In-future, I ought to use the device tree as there are now Pi entries in
  603. // /proc/device-tree/ ...
  604. // but I'll leave that for the next revision.
  605. // Isolate the Revision line
  606. rewind (cpuFd) ;
  607. while (fgets (line, 120, cpuFd) != NULL)
  608. if (strncmp (line, "Revision", 8) == 0)
  609. break ;
  610. fclose (cpuFd) ;
  611. if (strncmp (line, "Revision", 8) != 0)
  612. piGpioLayoutOops ("No \"Revision\" line") ;
  613. // Chomp trailing CR/NL
  614. for (c = &line [strlen (line) - 1] ; (*c == '\n') || (*c == '\r') ; --c)
  615. *c = 0 ;
  616. if (wiringPiDebug)
  617. printf ("piGpioLayout: Revision string: %s\n", line) ;
  618. // Scan to the first character of the revision number
  619. for (c = line ; *c ; ++c)
  620. if (*c == ':')
  621. break ;
  622. if (*c != ':')
  623. piGpioLayoutOops ("Bogus \"Revision\" line (no colon)") ;
  624. // Chomp spaces
  625. ++c ;
  626. while (isspace (*c))
  627. ++c ;
  628. if (!isxdigit (*c))
  629. piGpioLayoutOops ("Bogus \"Revision\" line (no hex digit at start of revision)") ;
  630. // Make sure its long enough
  631. if (strlen (c) < 4)
  632. piGpioLayoutOops ("Bogus revision line (too small)") ;
  633. // Isolate last 4 characters: (in-case of overvolting or new encoding scheme)
  634. c = c + strlen (c) - 4 ;
  635. if (wiringPiDebug)
  636. printf ("piGpioLayout: last4Chars are: \"%s\"\n", c) ;
  637. if ( (strcmp (c, "0002") == 0) || (strcmp (c, "0003") == 0))
  638. gpioLayout = 1 ;
  639. else
  640. gpioLayout = 2 ; // Covers everything else from the B revision 2 to the B+, the Pi v2, v3, zero and CM's.
  641. if (wiringPiDebug)
  642. printf ("piGpioLayoutOops: Returning revision: %d\n", gpioLayout) ;
  643. return gpioLayout ;
  644. }
  645. /*
  646. * piBoardRev:
  647. * Deprecated, but does the same as piGpioLayout
  648. *********************************************************************************
  649. */
  650. int piBoardRev (void)
  651. {
  652. return piGpioLayout () ;
  653. }
  654. /*
  655. * piBoardId:
  656. * Return the real details of the board we have.
  657. *
  658. * This is undocumented and really only intended for the GPIO command.
  659. * Use at your own risk!
  660. *
  661. * Seems there are some boards with 0000 in them (mistake in manufacture)
  662. * So the distinction between boards that I can see is:
  663. *
  664. * 0000 - Error
  665. * 0001 - Not used
  666. *
  667. * Original Pi boards:
  668. * 0002 - Model B, Rev 1, 256MB, Egoman
  669. * 0003 - Model B, Rev 1.1, 256MB, Egoman, Fuses/D14 removed.
  670. *
  671. * Newer Pi's with remapped GPIO:
  672. * 0004 - Model B, Rev 1.2, 256MB, Sony
  673. * 0005 - Model B, Rev 1.2, 256MB, Egoman
  674. * 0006 - Model B, Rev 1.2, 256MB, Egoman
  675. *
  676. * 0007 - Model A, Rev 1.2, 256MB, Egoman
  677. * 0008 - Model A, Rev 1.2, 256MB, Sony
  678. * 0009 - Model A, Rev 1.2, 256MB, Egoman
  679. *
  680. * 000d - Model B, Rev 1.2, 512MB, Egoman (Red Pi, Blue Pi?)
  681. * 000e - Model B, Rev 1.2, 512MB, Sony
  682. * 000f - Model B, Rev 1.2, 512MB, Egoman
  683. *
  684. * 0010 - Model B+, Rev 1.2, 512MB, Sony
  685. * 0013 - Model B+ Rev 1.2, 512MB, Embest
  686. * 0016 - Model B+ Rev 1.2, 512MB, Sony
  687. * 0019 - Model B+ Rev 1.2, 512MB, Egoman
  688. *
  689. * 0011 - Pi CM, Rev 1.1, 512MB, Sony
  690. * 0014 - Pi CM, Rev 1.1, 512MB, Embest
  691. * 0017 - Pi CM, Rev 1.1, 512MB, Sony
  692. * 001a - Pi CM, Rev 1.1, 512MB, Egoman
  693. *
  694. * 0012 - Model A+ Rev 1.1, 256MB, Sony
  695. * 0015 - Model A+ Rev 1.1, 512MB, Embest
  696. * 0018 - Model A+ Rev 1.1, 256MB, Sony
  697. * 001b - Model A+ Rev 1.1, 256MB, Egoman
  698. *
  699. * A small thorn is the olde style overvolting - that will add in
  700. * 1000000
  701. *
  702. * The Pi compute module has an revision of 0011 or 0014 - since we only
  703. * check the last digit, then it's 1, therefore it'll default to not 2 or
  704. * 3 for a Rev 1, so will appear as a Rev 2. This is fine for the most part, but
  705. * we'll properly detect the Compute Module later and adjust accordingly.
  706. *
  707. * And then things changed with the introduction of the v2...
  708. *
  709. * For Pi v2 and subsequent models - e.g. the Zero:
  710. *
  711. * [USER:8] [NEW:1] [MEMSIZE:3] [MANUFACTURER:4] [PROCESSOR:4] [TYPE:8] [REV:4]
  712. * NEW 23: will be 1 for the new scheme, 0 for the old scheme
  713. * MEMSIZE 20: 0=256M 1=512M 2=1G
  714. * MANUFACTURER 16: 0=SONY 1=EGOMAN 2=EMBEST
  715. * PROCESSOR 12: 0=2835 1=2836
  716. * TYPE 04: 0=MODELA 1=MODELB 2=MODELA+ 3=MODELB+ 4=Pi2 MODEL B 5=ALPHA 6=CM
  717. * REV 00: 0=REV0 1=REV1 2=REV2
  718. *********************************************************************************
  719. */
  720. void piBoardId (int *model, int *rev, int *mem, int *maker, int *warranty)
  721. {
  722. FILE *cpuFd ;
  723. char line [120] ;
  724. char *c ;
  725. unsigned int revision ;
  726. int bRev, bType, bProc, bMfg, bMem, bWarranty ;
  727. // Will deal with the properly later on - for now, lets just get it going...
  728. // unsigned int modelNum ;
  729. (void)piGpioLayout () ; // Call this first to make sure all's OK. Don't care about the result.
  730. if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL)
  731. piGpioLayoutOops ("Unable to open /proc/cpuinfo") ;
  732. while (fgets (line, 120, cpuFd) != NULL)
  733. if (strncmp (line, "Revision", 8) == 0)
  734. break ;
  735. fclose (cpuFd) ;
  736. if (strncmp (line, "Revision", 8) != 0)
  737. piGpioLayoutOops ("No \"Revision\" line") ;
  738. // Chomp trailing CR/NL
  739. for (c = &line [strlen (line) - 1] ; (*c == '\n') || (*c == '\r') ; --c)
  740. *c = 0 ;
  741. if (wiringPiDebug)
  742. printf ("piBoardId: Revision string: %s\n", line) ;
  743. // Need to work out if it's using the new or old encoding scheme:
  744. // Scan to the first character of the revision number
  745. for (c = line ; *c ; ++c)
  746. if (*c == ':')
  747. break ;
  748. if (*c != ':')
  749. piGpioLayoutOops ("Bogus \"Revision\" line (no colon)") ;
  750. // Chomp spaces
  751. ++c ;
  752. while (isspace (*c))
  753. ++c ;
  754. if (!isxdigit (*c))
  755. piGpioLayoutOops ("Bogus \"Revision\" line (no hex digit at start of revision)") ;
  756. revision = (unsigned int)strtol (c, NULL, 16) ; // Hex number with no leading 0x
  757. // Check for new way:
  758. if ((revision & (1 << 23)) != 0) // New way
  759. {
  760. if (wiringPiDebug)
  761. printf ("piBoardId: New Way: revision is: 0x%08X\n", revision) ;
  762. bRev = (revision & (0x0F << 0)) >> 0 ;
  763. bType = (revision & (0xFF << 4)) >> 4 ;
  764. bProc = (revision & (0x0F << 12)) >> 12 ; // Not used for now.
  765. bMfg = (revision & (0x0F << 16)) >> 16 ;
  766. bMem = (revision & (0x07 << 20)) >> 20 ;
  767. bWarranty = (revision & (0x03 << 24)) != 0 ;
  768. *model = bType ;
  769. *rev = bRev ;
  770. *mem = bMem ;
  771. *maker = bMfg ;
  772. *warranty = bWarranty ;
  773. if (wiringPiDebug)
  774. printf ("piBoardId: rev: %d, type: %d, proc: %d, mfg: %d, mem: %d, warranty: %d\n",
  775. bRev, bType, bProc, bMfg, bMem, bWarranty) ;
  776. }
  777. else // Old way
  778. {
  779. if (wiringPiDebug)
  780. printf ("piBoardId: Old Way: revision is: %s\n", c) ;
  781. if (!isdigit (*c))
  782. piGpioLayoutOops ("Bogus \"Revision\" line (no digit at start of revision)") ;
  783. // Make sure its long enough
  784. if (strlen (c) < 4)
  785. piGpioLayoutOops ("Bogus \"Revision\" line (not long enough)") ;
  786. // If longer than 4, we'll assume it's been overvolted
  787. *warranty = strlen (c) > 4 ;
  788. // Extract last 4 characters:
  789. c = c + strlen (c) - 4 ;
  790. // Fill out the replys as appropriate
  791. /**/ if (strcmp (c, "0002") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
  792. else if (strcmp (c, "0003") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_1 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
  793. else if (strcmp (c, "0004") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_2 ; *mem = 0 ; *maker = PI_MAKER_SONY ; }
  794. else if (strcmp (c, "0005") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_2 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
  795. else if (strcmp (c, "0006") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_2 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
  796. else if (strcmp (c, "0007") == 0) { *model = PI_MODEL_A ; *rev = PI_VERSION_1_2 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
  797. else if (strcmp (c, "0008") == 0) { *model = PI_MODEL_A ; *rev = PI_VERSION_1_2 ; *mem = 0 ; *maker = PI_MAKER_SONY ; ; }
  798. else if (strcmp (c, "0009") == 0) { *model = PI_MODEL_A ; *rev = PI_VERSION_1_2 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
  799. else if (strcmp (c, "000d") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_EGOMAN ; }
  800. else if (strcmp (c, "000e") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_SONY ; }
  801. else if (strcmp (c, "000f") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_EGOMAN ; }
  802. else if (strcmp (c, "0010") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_SONY ; }
  803. else if (strcmp (c, "0013") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_EMBEST ; }
  804. else if (strcmp (c, "0016") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_SONY ; }
  805. else if (strcmp (c, "0019") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_EGOMAN ; }
  806. else if (strcmp (c, "0011") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_1 ; *mem = 1 ; *maker = PI_MAKER_SONY ; }
  807. else if (strcmp (c, "0014") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_1 ; *mem = 1 ; *maker = PI_MAKER_EMBEST ; }
  808. else if (strcmp (c, "0017") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_1 ; *mem = 1 ; *maker = PI_MAKER_SONY ; }
  809. else if (strcmp (c, "001a") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_1 ; *mem = 1 ; *maker = PI_MAKER_EGOMAN ; }
  810. else if (strcmp (c, "0012") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_1 ; *mem = 0 ; *maker = PI_MAKER_SONY ; }
  811. else if (strcmp (c, "0015") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_1 ; *mem = 1 ; *maker = PI_MAKER_EMBEST ; }
  812. else if (strcmp (c, "0018") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_1 ; *mem = 0 ; *maker = PI_MAKER_SONY ; }
  813. else if (strcmp (c, "001b") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_1 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
  814. else { *model = 0 ; *rev = 0 ; *mem = 0 ; *maker = 0 ; }
  815. }
  816. }
  817. /*
  818. * wpiPinToGpio:
  819. * Translate a wiringPi Pin number to native GPIO pin number.
  820. * Provided for external support.
  821. *********************************************************************************
  822. */
  823. int wpiPinToGpio (int wpiPin)
  824. {
  825. return pinToGpio [wpiPin & 63] ;
  826. }
  827. /*
  828. * physPinToGpio:
  829. * Translate a physical Pin number to native GPIO pin number.
  830. * Provided for external support.
  831. *********************************************************************************
  832. */
  833. int physPinToGpio (int physPin)
  834. {
  835. return physToGpio [physPin & 63] ;
  836. }
  837. /*
  838. * setPadDrive:
  839. * Set the PAD driver value
  840. *********************************************************************************
  841. */
  842. void setPadDrive (int group, int value)
  843. {
  844. uint32_t wrVal ;
  845. if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
  846. {
  847. if ((group < 0) || (group > 2))
  848. return ;
  849. wrVal = BCM_PASSWORD | 0x18 | (value & 7) ;
  850. *(pads + group + 11) = wrVal ;
  851. if (wiringPiDebug)
  852. {
  853. printf ("setPadDrive: Group: %d, value: %d (%08X)\n", group, value, wrVal) ;
  854. printf ("Read : %08X\n", *(pads + group + 11)) ;
  855. }
  856. }
  857. }
  858. /*
  859. * getAlt:
  860. * Returns the ALT bits for a given port. Only really of-use
  861. * for the gpio readall command (I think)
  862. *********************************************************************************
  863. */
  864. int getAlt (int pin)
  865. {
  866. int fSel, shift, alt ;
  867. pin &= 63 ;
  868. /**/ if (wiringPiMode == WPI_MODE_PINS)
  869. pin = pinToGpio [pin] ;
  870. else if (wiringPiMode == WPI_MODE_PHYS)
  871. pin = physToGpio [pin] ;
  872. else if (wiringPiMode != WPI_MODE_GPIO)
  873. return 0 ;
  874. fSel = gpioToGPFSEL [pin] ;
  875. shift = gpioToShift [pin] ;
  876. alt = (*(gpio + fSel) >> shift) & 7 ;
  877. return alt ;
  878. }
  879. /*
  880. * pwmSetMode:
  881. * Select the native "balanced" mode, or standard mark:space mode
  882. *********************************************************************************
  883. */
  884. void pwmSetMode (int mode)
  885. {
  886. if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
  887. {
  888. if (mode == PWM_MODE_MS)
  889. *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE | PWM0_MS_MODE | PWM1_MS_MODE ;
  890. else
  891. *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ;
  892. }
  893. }
  894. /*
  895. * pwmSetRange:
  896. * Set the PWM range register. We set both range registers to the same
  897. * value. If you want different in your own code, then write your own.
  898. *********************************************************************************
  899. */
  900. void pwmSetRange (unsigned int range)
  901. {
  902. if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
  903. {
  904. *(pwm + PWM0_RANGE) = range ; delayMicroseconds (10) ;
  905. *(pwm + PWM1_RANGE) = range ; delayMicroseconds (10) ;
  906. }
  907. }
  908. /*
  909. * pwmSetClock:
  910. * Set/Change the PWM clock. Originally my code, but changed
  911. * (for the better!) by Chris Hall, <chris@kchall.plus.com>
  912. * after further study of the manual and testing with a 'scope
  913. *********************************************************************************
  914. */
  915. void pwmSetClock (int divisor)
  916. {
  917. uint32_t pwm_control ;
  918. divisor &= 4095 ;
  919. if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
  920. {
  921. if (wiringPiDebug)
  922. printf ("Setting to: %d. Current: 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;
  923. pwm_control = *(pwm + PWM_CONTROL) ; // preserve PWM_CONTROL
  924. // We need to stop PWM prior to stopping PWM clock in MS mode otherwise BUSY
  925. // stays high.
  926. *(pwm + PWM_CONTROL) = 0 ; // Stop PWM
  927. // Stop PWM clock before changing divisor. The delay after this does need to
  928. // this big (95uS occasionally fails, 100uS OK), it's almost as though the BUSY
  929. // flag is not working properly in balanced mode. Without the delay when DIV is
  930. // adjusted the clock sometimes switches to very slow, once slow further DIV
  931. // adjustments do nothing and it's difficult to get out of this mode.
  932. *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x01 ; // Stop PWM Clock
  933. delayMicroseconds (110) ; // prevents clock going sloooow
  934. while ((*(clk + PWMCLK_CNTL) & 0x80) != 0) // Wait for clock to be !BUSY
  935. delayMicroseconds (1) ;
  936. *(clk + PWMCLK_DIV) = BCM_PASSWORD | (divisor << 12) ;
  937. *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x11 ; // Start PWM clock
  938. *(pwm + PWM_CONTROL) = pwm_control ; // restore PWM_CONTROL
  939. if (wiringPiDebug)
  940. printf ("Set to: %d. Now : 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;
  941. }
  942. }
  943. /*
  944. * gpioClockSet:
  945. * Set the freuency on a GPIO clock pin
  946. *********************************************************************************
  947. */
  948. void gpioClockSet (int pin, int freq)
  949. {
  950. int divi, divr, divf ;
  951. pin &= 63 ;
  952. /**/ if (wiringPiMode == WPI_MODE_PINS)
  953. pin = pinToGpio [pin] ;
  954. else if (wiringPiMode == WPI_MODE_PHYS)
  955. pin = physToGpio [pin] ;
  956. else if (wiringPiMode != WPI_MODE_GPIO)
  957. return ;
  958. divi = 19200000 / freq ;
  959. divr = 19200000 % freq ;
  960. divf = (int)((double)divr * 4096.0 / 19200000.0) ;
  961. if (divi > 4095)
  962. divi = 4095 ;
  963. *(clk + gpioToClkCon [pin]) = BCM_PASSWORD | GPIO_CLOCK_SOURCE ; // Stop GPIO Clock
  964. while ((*(clk + gpioToClkCon [pin]) & 0x80) != 0) // ... and wait
  965. ;
  966. *(clk + gpioToClkDiv [pin]) = BCM_PASSWORD | (divi << 12) | divf ; // Set dividers
  967. *(clk + gpioToClkCon [pin]) = BCM_PASSWORD | 0x10 | GPIO_CLOCK_SOURCE ; // Start Clock
  968. }
  969. /*
  970. * wiringPiFindNode:
  971. * Locate our device node
  972. *********************************************************************************
  973. */
  974. struct wiringPiNodeStruct *wiringPiFindNode (int pin)
  975. {
  976. struct wiringPiNodeStruct *node = wiringPiNodes ;
  977. while (node != NULL)
  978. if ((pin >= node->pinBase) && (pin <= node->pinMax))
  979. return node ;
  980. else
  981. node = node->next ;
  982. return NULL ;
  983. }
  984. /*
  985. * wiringPiNewNode:
  986. * Create a new GPIO node into the wiringPi handling system
  987. *********************************************************************************
  988. */
  989. static void pinModeDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int mode) { return ; }
  990. static void pullUpDnControlDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int pud) { return ; }
  991. static unsigned int digitalRead8Dummy (UNU struct wiringPiNodeStruct *node, UNU int UNU pin) { return 0 ; }
  992. static void digitalWrite8Dummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int value) { return ; }
  993. static int digitalReadDummy (UNU struct wiringPiNodeStruct *node, UNU int UNU pin) { return LOW ; }
  994. static void digitalWriteDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int value) { return ; }
  995. static void pwmWriteDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int value) { return ; }
  996. static int analogReadDummy (UNU struct wiringPiNodeStruct *node, UNU int pin) { return 0 ; }
  997. static void analogWriteDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int value) { return ; }
  998. struct wiringPiNodeStruct *wiringPiNewNode (int pinBase, int numPins)
  999. {
  1000. int pin ;
  1001. struct wiringPiNodeStruct *node ;
  1002. // Minimum pin base is 64
  1003. if (pinBase < 64)
  1004. (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: pinBase of %d is < 64\n", pinBase) ;
  1005. // Check all pins in-case there is overlap:
  1006. for (pin = pinBase ; pin < (pinBase + numPins) ; ++pin)
  1007. if (wiringPiFindNode (pin) != NULL)
  1008. (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: Pin %d overlaps with existing definition\n", pin) ;
  1009. node = (struct wiringPiNodeStruct *)calloc (sizeof (struct wiringPiNodeStruct), 1) ; // calloc zeros
  1010. if (node == NULL)
  1011. (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: Unable to allocate memory: %s\n", strerror (errno)) ;
  1012. node->pinBase = pinBase ;
  1013. node->pinMax = pinBase + numPins - 1 ;
  1014. node->pinMode = pinModeDummy ;
  1015. node->pullUpDnControl = pullUpDnControlDummy ;
  1016. node->digitalRead = digitalReadDummy ;
  1017. //node->digitalRead8 = digitalRead8Dummy ;
  1018. node->digitalWrite = digitalWriteDummy ;
  1019. //node->digitalWrite8 = digitalWrite8Dummy ;
  1020. node->pwmWrite = pwmWriteDummy ;
  1021. node->analogRead = analogReadDummy ;
  1022. node->analogWrite = analogWriteDummy ;
  1023. node->next = wiringPiNodes ;
  1024. wiringPiNodes = node ;
  1025. return node ;
  1026. }
  1027. #ifdef notYetReady
  1028. /*
  1029. * pinED01:
  1030. * pinED10:
  1031. * Enables edge-detect mode on a pin - from a 0 to a 1 or 1 to 0
  1032. * Pin must already be in input mode with appropriate pull up/downs set.
  1033. *********************************************************************************
  1034. */
  1035. void pinEnableED01Pi (int pin)
  1036. {
  1037. pin = pinToGpio [pin & 63] ;
  1038. }
  1039. #endif
  1040. /*
  1041. *********************************************************************************
  1042. * Core Functions
  1043. *********************************************************************************
  1044. */
  1045. /*
  1046. * pinModeAlt:
  1047. * This is an un-documented special to let you set any pin to any mode
  1048. *********************************************************************************
  1049. */
  1050. void pinModeAlt (int pin, int mode)
  1051. {
  1052. int fSel, shift ;
  1053. if ((pin & PI_GPIO_MASK) == 0) // On-board pin
  1054. {
  1055. /**/ if (wiringPiMode == WPI_MODE_PINS)
  1056. pin = pinToGpio [pin] ;
  1057. else if (wiringPiMode == WPI_MODE_PHYS)
  1058. pin = physToGpio [pin] ;
  1059. else if (wiringPiMode != WPI_MODE_GPIO)
  1060. return ;
  1061. fSel = gpioToGPFSEL [pin] ;
  1062. shift = gpioToShift [pin] ;
  1063. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | ((mode & 0x7) << shift) ;
  1064. }
  1065. }
  1066. /*
  1067. * pinMode:
  1068. * Sets the mode of a pin to be input, output or PWM output
  1069. *********************************************************************************
  1070. */
  1071. void pinMode (int pin, int mode)
  1072. {
  1073. int fSel, shift, alt ;
  1074. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1075. int origPin = pin ;
  1076. if ((pin & PI_GPIO_MASK) == 0) // On-board pin
  1077. {
  1078. /**/ if (wiringPiMode == WPI_MODE_PINS)
  1079. pin = pinToGpio [pin] ;
  1080. else if (wiringPiMode == WPI_MODE_PHYS)
  1081. pin = physToGpio [pin] ;
  1082. else if (wiringPiMode != WPI_MODE_GPIO)
  1083. return ;
  1084. softPwmStop (origPin) ;
  1085. softToneStop (origPin) ;
  1086. fSel = gpioToGPFSEL [pin] ;
  1087. shift = gpioToShift [pin] ;
  1088. /**/ if (mode == INPUT)
  1089. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) ; // Sets bits to zero = input
  1090. else if (mode == OUTPUT)
  1091. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (1 << shift) ;
  1092. else if (mode == SOFT_PWM_OUTPUT)
  1093. softPwmCreate (origPin, 0, 100) ;
  1094. else if (mode == SOFT_TONE_OUTPUT)
  1095. softToneCreate (origPin) ;
  1096. else if (mode == PWM_TONE_OUTPUT)
  1097. {
  1098. pinMode (origPin, PWM_OUTPUT) ; // Call myself to enable PWM mode
  1099. pwmSetMode (PWM_MODE_MS) ;
  1100. }
  1101. else if (mode == PWM_OUTPUT)
  1102. {
  1103. if ((alt = gpioToPwmALT [pin]) == 0) // Not a hardware capable PWM pin
  1104. return ;
  1105. // Set pin to PWM mode
  1106. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ;
  1107. delayMicroseconds (110) ; // See comments in pwmSetClockWPi
  1108. pwmSetMode (PWM_MODE_BAL) ; // Pi default mode
  1109. pwmSetRange (1024) ; // Default range of 1024
  1110. pwmSetClock (32) ; // 19.2 / 32 = 600KHz - Also starts the PWM
  1111. }
  1112. else if (mode == GPIO_CLOCK)
  1113. {
  1114. if ((alt = gpioToGpClkALT0 [pin]) == 0) // Not a GPIO_CLOCK pin
  1115. return ;
  1116. // Set pin to GPIO_CLOCK mode and set the clock frequency to 100KHz
  1117. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ;
  1118. delayMicroseconds (110) ;
  1119. gpioClockSet (pin, 100000) ;
  1120. }
  1121. }
  1122. else
  1123. {
  1124. if ((node = wiringPiFindNode (pin)) != NULL)
  1125. node->pinMode (node, pin, mode) ;
  1126. return ;
  1127. }
  1128. }
  1129. /*
  1130. * pullUpDownCtrl:
  1131. * Control the internal pull-up/down resistors on a GPIO pin
  1132. * The Arduino only has pull-ups and these are enabled by writing 1
  1133. * to a port when in input mode - this paradigm doesn't quite apply
  1134. * here though.
  1135. *********************************************************************************
  1136. */
  1137. void pullUpDnControl (int pin, int pud)
  1138. {
  1139. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1140. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1141. {
  1142. /**/ if (wiringPiMode == WPI_MODE_PINS)
  1143. pin = pinToGpio [pin] ;
  1144. else if (wiringPiMode == WPI_MODE_PHYS)
  1145. pin = physToGpio [pin] ;
  1146. else if (wiringPiMode != WPI_MODE_GPIO)
  1147. return ;
  1148. *(gpio + GPPUD) = pud & 3 ; delayMicroseconds (5) ;
  1149. *(gpio + gpioToPUDCLK [pin]) = 1 << (pin & 31) ; delayMicroseconds (5) ;
  1150. *(gpio + GPPUD) = 0 ; delayMicroseconds (5) ;
  1151. *(gpio + gpioToPUDCLK [pin]) = 0 ; delayMicroseconds (5) ;
  1152. }
  1153. else // Extension module
  1154. {
  1155. if ((node = wiringPiFindNode (pin)) != NULL)
  1156. node->pullUpDnControl (node, pin, pud) ;
  1157. return ;
  1158. }
  1159. }
  1160. /*
  1161. * digitalRead:
  1162. * Read the value of a given Pin, returning HIGH or LOW
  1163. *********************************************************************************
  1164. */
  1165. int digitalRead (int pin)
  1166. {
  1167. char c ;
  1168. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1169. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1170. {
  1171. /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS) // Sys mode
  1172. {
  1173. if (sysFds [pin] == -1)
  1174. return LOW ;
  1175. lseek (sysFds [pin], 0L, SEEK_SET) ;
  1176. read (sysFds [pin], &c, 1) ;
  1177. return (c == '0') ? LOW : HIGH ;
  1178. }
  1179. else if (wiringPiMode == WPI_MODE_PINS)
  1180. pin = pinToGpio [pin] ;
  1181. else if (wiringPiMode == WPI_MODE_PHYS)
  1182. pin = physToGpio [pin] ;
  1183. else if (wiringPiMode != WPI_MODE_GPIO)
  1184. return LOW ;
  1185. if ((*(gpio + gpioToGPLEV [pin]) & (1 << (pin & 31))) != 0)
  1186. return HIGH ;
  1187. else
  1188. return LOW ;
  1189. }
  1190. else
  1191. {
  1192. if ((node = wiringPiFindNode (pin)) == NULL)
  1193. return LOW ;
  1194. return node->digitalRead (node, pin) ;
  1195. }
  1196. }
  1197. /*
  1198. * digitalRead8:
  1199. * Read 8-bits (a byte) from given start pin.
  1200. *********************************************************************************
  1201. unsigned int digitalRead8 (int pin)
  1202. {
  1203. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1204. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1205. return 0 ;
  1206. else
  1207. {
  1208. if ((node = wiringPiFindNode (pin)) == NULL)
  1209. return LOW ;
  1210. return node->digitalRead8 (node, pin) ;
  1211. }
  1212. }
  1213. */
  1214. /*
  1215. * digitalWrite:
  1216. * Set an output bit
  1217. *********************************************************************************
  1218. */
  1219. void digitalWrite (int pin, int value)
  1220. {
  1221. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1222. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1223. {
  1224. /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS) // Sys mode
  1225. {
  1226. if (sysFds [pin] != -1)
  1227. {
  1228. if (value == LOW)
  1229. write (sysFds [pin], "0\n", 2) ;
  1230. else
  1231. write (sysFds [pin], "1\n", 2) ;
  1232. }
  1233. return ;
  1234. }
  1235. else if (wiringPiMode == WPI_MODE_PINS)
  1236. pin = pinToGpio [pin] ;
  1237. else if (wiringPiMode == WPI_MODE_PHYS)
  1238. pin = physToGpio [pin] ;
  1239. else if (wiringPiMode != WPI_MODE_GPIO)
  1240. return ;
  1241. if (value == LOW)
  1242. *(gpio + gpioToGPCLR [pin]) = 1 << (pin & 31) ;
  1243. else
  1244. *(gpio + gpioToGPSET [pin]) = 1 << (pin & 31) ;
  1245. }
  1246. else
  1247. {
  1248. if ((node = wiringPiFindNode (pin)) != NULL)
  1249. node->digitalWrite (node, pin, value) ;
  1250. }
  1251. }
  1252. /*
  1253. * digitalWrite8:
  1254. * Set an output 8-bit byte on the device from the given pin number
  1255. *********************************************************************************
  1256. void digitalWrite8 (int pin, int value)
  1257. {
  1258. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1259. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1260. return ;
  1261. else
  1262. {
  1263. if ((node = wiringPiFindNode (pin)) != NULL)
  1264. node->digitalWrite8 (node, pin, value) ;
  1265. }
  1266. }
  1267. */
  1268. /*
  1269. * pwmWrite:
  1270. * Set an output PWM value
  1271. *********************************************************************************
  1272. */
  1273. void pwmWrite (int pin, int value)
  1274. {
  1275. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1276. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1277. {
  1278. /**/ if (wiringPiMode == WPI_MODE_PINS)
  1279. pin = pinToGpio [pin] ;
  1280. else if (wiringPiMode == WPI_MODE_PHYS)
  1281. pin = physToGpio [pin] ;
  1282. else if (wiringPiMode != WPI_MODE_GPIO)
  1283. return ;
  1284. *(pwm + gpioToPwmPort [pin]) = value ;
  1285. }
  1286. else
  1287. {
  1288. if ((node = wiringPiFindNode (pin)) != NULL)
  1289. node->pwmWrite (node, pin, value) ;
  1290. }
  1291. }
  1292. /*
  1293. * analogRead:
  1294. * Read the analog value of a given Pin.
  1295. * There is no on-board Pi analog hardware,
  1296. * so this needs to go to a new node.
  1297. *********************************************************************************
  1298. */
  1299. int analogRead (int pin)
  1300. {
  1301. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1302. if ((node = wiringPiFindNode (pin)) == NULL)
  1303. return 0 ;
  1304. else
  1305. return node->analogRead (node, pin) ;
  1306. }
  1307. /*
  1308. * analogWrite:
  1309. * Write the analog value to the given Pin.
  1310. * There is no on-board Pi analog hardware,
  1311. * so this needs to go to a new node.
  1312. *********************************************************************************
  1313. */
  1314. void analogWrite (int pin, int value)
  1315. {
  1316. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1317. if ((node = wiringPiFindNode (pin)) == NULL)
  1318. return ;
  1319. node->analogWrite (node, pin, value) ;
  1320. }
  1321. /*
  1322. * pwmToneWrite:
  1323. * Pi Specific.
  1324. * Output the given frequency on the Pi's PWM pin
  1325. *********************************************************************************
  1326. */
  1327. void pwmToneWrite (int pin, int freq)
  1328. {
  1329. int range ;
  1330. if (freq == 0)
  1331. pwmWrite (pin, 0) ; // Off
  1332. else
  1333. {
  1334. range = 600000 / freq ;
  1335. pwmSetRange (range) ;
  1336. pwmWrite (pin, freq / 2) ;
  1337. }
  1338. }
  1339. /*
  1340. * digitalWriteByte:
  1341. * digitalReadByte:
  1342. * Pi Specific
  1343. * Write an 8-bit byte to the first 8 GPIO pins - try to do it as
  1344. * fast as possible.
  1345. * However it still needs 2 operations to set the bits, so any external
  1346. * hardware must not rely on seeing a change as there will be a change
  1347. * to set the outputs bits to zero, then another change to set the 1's
  1348. * Reading is just bit fiddling.
  1349. * These are wiringPi pin numbers 0..7, or BCM_GPIO pin numbers
  1350. * 17, 18, 22, 23, 24, 24, 4 on a Pi v1 rev 0-3
  1351. * 17, 18, 27, 23, 24, 24, 4 on a Pi v1 rev 3 onwards or B+, 2, 3, zero
  1352. *********************************************************************************
  1353. */
  1354. void digitalWriteByte (const int value)
  1355. {
  1356. uint32_t pinSet = 0 ;
  1357. uint32_t pinClr = 0 ;
  1358. int mask = 1 ;
  1359. int pin ;
  1360. /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS)
  1361. {
  1362. for (pin = 0 ; pin < 8 ; ++pin)
  1363. {
  1364. digitalWrite (pinToGpio [pin], value & mask) ;
  1365. mask <<= 1 ;
  1366. }
  1367. return ;
  1368. }
  1369. else
  1370. {
  1371. for (pin = 0 ; pin < 8 ; ++pin)
  1372. {
  1373. if ((value & mask) == 0)
  1374. pinClr |= (1 << pinToGpio [pin]) ;
  1375. else
  1376. pinSet |= (1 << pinToGpio [pin]) ;
  1377. mask <<= 1 ;
  1378. }
  1379. *(gpio + gpioToGPCLR [0]) = pinClr ;
  1380. *(gpio + gpioToGPSET [0]) = pinSet ;
  1381. }
  1382. }
  1383. unsigned int digitalReadByte (void)
  1384. {
  1385. int pin, x ;
  1386. uint32_t raw ;
  1387. uint32_t data = 0 ;
  1388. /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS)
  1389. {
  1390. for (pin = 0 ; pin < 8 ; ++pin)
  1391. {
  1392. x = digitalRead (pinToGpio [pin]) ;
  1393. data = (data << 1) | x ;
  1394. }
  1395. }
  1396. else
  1397. {
  1398. raw = *(gpio + gpioToGPLEV [0]) ; // First bank for these pins
  1399. for (pin = 0 ; pin < 8 ; ++pin)
  1400. {
  1401. x = pinToGpio [pin] ;
  1402. data = (data << 1) | (((raw & (1 << x)) == 0) ? 0 : 1) ;
  1403. }
  1404. }
  1405. return data ;
  1406. }
  1407. /*
  1408. * digitalWriteByte2:
  1409. * digitalReadByte2:
  1410. * Pi Specific
  1411. * Write an 8-bit byte to the second set of 8 GPIO pins. This is marginally
  1412. * faster than the first lot as these are consecutive BCM_GPIO pin numbers.
  1413. * However they overlap with the original read/write bytes.
  1414. *********************************************************************************
  1415. */
  1416. void digitalWriteByte2 (const int value)
  1417. {
  1418. register int mask = 1 ;
  1419. register int pin ;
  1420. /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS)
  1421. {
  1422. for (pin = 20 ; pin < 28 ; ++pin)
  1423. {
  1424. digitalWrite (pin, value & mask) ;
  1425. mask <<= 1 ;
  1426. }
  1427. return ;
  1428. }
  1429. else
  1430. {
  1431. *(gpio + gpioToGPCLR [0]) = (~value & 0xFF) << 20 ; // 0x0FF00000; ILJ > CHANGE: Old causes glitch
  1432. *(gpio + gpioToGPSET [0]) = ( value & 0xFF) << 20 ;
  1433. }
  1434. }
  1435. unsigned int digitalReadByte2 (void)
  1436. {
  1437. int pin, x ;
  1438. uint32_t data = 0 ;
  1439. /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS)
  1440. {
  1441. for (pin = 20 ; pin < 28 ; ++pin)
  1442. {
  1443. x = digitalRead (pin) ;
  1444. data = (data << 1) | x ;
  1445. }
  1446. }
  1447. else
  1448. data = ((*(gpio + gpioToGPLEV [0])) >> 20) & 0xFF ; // First bank for these pins
  1449. return data ;
  1450. }
  1451. /*
  1452. * waitForInterrupt:
  1453. * Pi Specific.
  1454. * Wait for Interrupt on a GPIO pin.
  1455. * This is actually done via the /sys/class/gpio interface regardless of
  1456. * the wiringPi access mode in-use. Maybe sometime it might get a better
  1457. * way for a bit more efficiency.
  1458. *********************************************************************************
  1459. */
  1460. int waitForInterrupt (int pin, int mS)
  1461. {
  1462. int fd, x ;
  1463. uint8_t c ;
  1464. struct pollfd polls ;
  1465. /**/ if (wiringPiMode == WPI_MODE_PINS)
  1466. pin = pinToGpio [pin] ;
  1467. else if (wiringPiMode == WPI_MODE_PHYS)
  1468. pin = physToGpio [pin] ;
  1469. if ((fd = sysFds [pin]) == -1)
  1470. return -2 ;
  1471. // Setup poll structure
  1472. polls.fd = fd ;
  1473. polls.events = POLLPRI | POLLERR ;
  1474. // Wait for it ...
  1475. x = poll (&polls, 1, mS) ;
  1476. // If no error, do a dummy read to clear the interrupt
  1477. // A one character read appars to be enough.
  1478. if (x > 0)
  1479. {
  1480. lseek (fd, 0, SEEK_SET) ; // Rewind
  1481. (void)read (fd, &c, 1) ; // Read & clear
  1482. }
  1483. return x ;
  1484. }
  1485. /*
  1486. * interruptHandler:
  1487. * This is a thread and gets started to wait for the interrupt we're
  1488. * hoping to catch. It will call the user-function when the interrupt
  1489. * fires.
  1490. *********************************************************************************
  1491. */
  1492. static void *interruptHandler (UNU void *arg)
  1493. {
  1494. int myPin ;
  1495. (void)piHiPri (55) ; // Only effective if we run as root
  1496. myPin = pinPass ;
  1497. pinPass = -1 ;
  1498. for (;;)
  1499. if (waitForInterrupt (myPin, -1) > 0)
  1500. isrFunctions [myPin] () ;
  1501. return NULL ;
  1502. }
  1503. /*
  1504. * wiringPiISR:
  1505. * Pi Specific.
  1506. * Take the details and create an interrupt handler that will do a call-
  1507. * back to the user supplied function.
  1508. *********************************************************************************
  1509. */
  1510. int wiringPiISR (int pin, int mode, void (*function)(void))
  1511. {
  1512. pthread_t threadId ;
  1513. const char *modeS ;
  1514. char fName [64] ;
  1515. char pinS [8] ;
  1516. pid_t pid ;
  1517. int count, i ;
  1518. char c ;
  1519. int bcmGpioPin ;
  1520. if ((pin < 0) || (pin > 63))
  1521. return wiringPiFailure (WPI_FATAL, "wiringPiISR: pin must be 0-63 (%d)\n", pin) ;
  1522. /**/ if (wiringPiMode == WPI_MODE_UNINITIALISED)
  1523. return wiringPiFailure (WPI_FATAL, "wiringPiISR: wiringPi has not been initialised. Unable to continue.\n") ;
  1524. else if (wiringPiMode == WPI_MODE_PINS)
  1525. bcmGpioPin = pinToGpio [pin] ;
  1526. else if (wiringPiMode == WPI_MODE_PHYS)
  1527. bcmGpioPin = physToGpio [pin] ;
  1528. else
  1529. bcmGpioPin = pin ;
  1530. // Now export the pin and set the right edge
  1531. // We're going to use the gpio program to do this, so it assumes
  1532. // a full installation of wiringPi. It's a bit 'clunky', but it
  1533. // is a way that will work when we're running in "Sys" mode, as
  1534. // a non-root user. (without sudo)
  1535. if (mode != INT_EDGE_SETUP)
  1536. {
  1537. /**/ if (mode == INT_EDGE_FALLING)
  1538. modeS = "falling" ;
  1539. else if (mode == INT_EDGE_RISING)
  1540. modeS = "rising" ;
  1541. else
  1542. modeS = "both" ;
  1543. sprintf (pinS, "%d", bcmGpioPin) ;
  1544. if ((pid = fork ()) < 0) // Fail
  1545. return wiringPiFailure (WPI_FATAL, "wiringPiISR: fork failed: %s\n", strerror (errno)) ;
  1546. if (pid == 0) // Child, exec
  1547. {
  1548. /**/ if (access ("/usr/local/bin/gpio", X_OK) == 0)
  1549. {
  1550. execl ("/usr/local/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ;
  1551. return wiringPiFailure (WPI_FATAL, "wiringPiISR: execl failed: %s\n", strerror (errno)) ;
  1552. }
  1553. else if (access ("/usr/bin/gpio", X_OK) == 0)
  1554. {
  1555. execl ("/usr/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ;
  1556. return wiringPiFailure (WPI_FATAL, "wiringPiISR: execl failed: %s\n", strerror (errno)) ;
  1557. }
  1558. else
  1559. return wiringPiFailure (WPI_FATAL, "wiringPiISR: Can't find gpio program\n") ;
  1560. }
  1561. else // Parent, wait
  1562. wait (NULL) ;
  1563. }
  1564. // Now pre-open the /sys/class node - but it may already be open if
  1565. // we are in Sys mode...
  1566. if (sysFds [bcmGpioPin] == -1)
  1567. {
  1568. sprintf (fName, "/sys/class/gpio/gpio%d/value", bcmGpioPin) ;
  1569. if ((sysFds [bcmGpioPin] = open (fName, O_RDWR)) < 0)
  1570. return wiringPiFailure (WPI_FATAL, "wiringPiISR: unable to open %s: %s\n", fName, strerror (errno)) ;
  1571. }
  1572. // Clear any initial pending interrupt
  1573. ioctl (sysFds [bcmGpioPin], FIONREAD, &count) ;
  1574. for (i = 0 ; i < count ; ++i)
  1575. read (sysFds [bcmGpioPin], &c, 1) ;
  1576. isrFunctions [pin] = function ;
  1577. pthread_mutex_lock (&pinMutex) ;
  1578. pinPass = pin ;
  1579. pthread_create (&threadId, NULL, interruptHandler, NULL) ;
  1580. while (pinPass != -1)
  1581. delay (1) ;
  1582. pthread_mutex_unlock (&pinMutex) ;
  1583. return 0 ;
  1584. }
  1585. /*
  1586. * initialiseEpoch:
  1587. * Initialise our start-of-time variable to be the current unix
  1588. * time in milliseconds and microseconds.
  1589. *********************************************************************************
  1590. */
  1591. static void initialiseEpoch (void)
  1592. {
  1593. #ifdef OLD_WAY
  1594. struct timeval tv ;
  1595. gettimeofday (&tv, NULL) ;
  1596. epochMilli = (uint64_t)tv.tv_sec * (uint64_t)1000 + (uint64_t)(tv.tv_usec / 1000) ;
  1597. epochMicro = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)(tv.tv_usec) ;
  1598. #else
  1599. struct timespec ts ;
  1600. clock_gettime (CLOCK_MONOTONIC_RAW, &ts) ;
  1601. epochMilli = (uint64_t)ts.tv_sec * (uint64_t)1000 + (uint64_t)(ts.tv_nsec / 1000000L) ;
  1602. epochMicro = (uint64_t)ts.tv_sec * (uint64_t)1000000 + (uint64_t)(ts.tv_nsec / 1000L) ;
  1603. #endif
  1604. }
  1605. /*
  1606. * delay:
  1607. * Wait for some number of milliseconds
  1608. *********************************************************************************
  1609. */
  1610. void delay (unsigned int howLong)
  1611. {
  1612. struct timespec sleeper, dummy ;
  1613. sleeper.tv_sec = (time_t)(howLong / 1000) ;
  1614. sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ;
  1615. nanosleep (&sleeper, &dummy) ;
  1616. }
  1617. /*
  1618. * delayMicroseconds:
  1619. * This is somewhat intersting. It seems that on the Pi, a single call
  1620. * to nanosleep takes some 80 to 130 microseconds anyway, so while
  1621. * obeying the standards (may take longer), it's not always what we
  1622. * want!
  1623. *
  1624. * So what I'll do now is if the delay is less than 100uS we'll do it
  1625. * in a hard loop, watching a built-in counter on the ARM chip. This is
  1626. * somewhat sub-optimal in that it uses 100% CPU, something not an issue
  1627. * in a microcontroller, but under a multi-tasking, multi-user OS, it's
  1628. * wastefull, however we've no real choice )-:
  1629. *
  1630. * Plan B: It seems all might not be well with that plan, so changing it
  1631. * to use gettimeofday () and poll on that instead...
  1632. *********************************************************************************
  1633. */
  1634. void delayMicrosecondsHard (unsigned int howLong)
  1635. {
  1636. struct timeval tNow, tLong, tEnd ;
  1637. gettimeofday (&tNow, NULL) ;
  1638. tLong.tv_sec = howLong / 1000000 ;
  1639. tLong.tv_usec = howLong % 1000000 ;
  1640. timeradd (&tNow, &tLong, &tEnd) ;
  1641. while (timercmp (&tNow, &tEnd, <))
  1642. gettimeofday (&tNow, NULL) ;
  1643. }
  1644. void delayMicroseconds (unsigned int howLong)
  1645. {
  1646. struct timespec sleeper ;
  1647. unsigned int uSecs = howLong % 1000000 ;
  1648. unsigned int wSecs = howLong / 1000000 ;
  1649. /**/ if (howLong == 0)
  1650. return ;
  1651. else if (howLong < 100)
  1652. delayMicrosecondsHard (howLong) ;
  1653. else
  1654. {
  1655. sleeper.tv_sec = wSecs ;
  1656. sleeper.tv_nsec = (long)(uSecs * 1000L) ;
  1657. nanosleep (&sleeper, NULL) ;
  1658. }
  1659. }
  1660. /*
  1661. * millis:
  1662. * Return a number of milliseconds as an unsigned int.
  1663. * Wraps at 49 days.
  1664. *********************************************************************************
  1665. */
  1666. unsigned int millis (void)
  1667. {
  1668. uint64_t now ;
  1669. #ifdef OLD_WAY
  1670. struct timeval tv ;
  1671. gettimeofday (&tv, NULL) ;
  1672. now = (uint64_t)tv.tv_sec * (uint64_t)1000 + (uint64_t)(tv.tv_usec / 1000) ;
  1673. #else
  1674. struct timespec ts ;
  1675. clock_gettime (CLOCK_MONOTONIC_RAW, &ts) ;
  1676. now = (uint64_t)ts.tv_sec * (uint64_t)1000 + (uint64_t)(ts.tv_nsec / 1000000L) ;
  1677. #endif
  1678. return (uint32_t)(now - epochMilli) ;
  1679. }
  1680. /*
  1681. * micros:
  1682. * Return a number of microseconds as an unsigned int.
  1683. * Wraps after 71 minutes.
  1684. *********************************************************************************
  1685. */
  1686. unsigned int micros (void)
  1687. {
  1688. uint64_t now ;
  1689. #ifdef OLD_WAY
  1690. struct timeval tv ;
  1691. gettimeofday (&tv, NULL) ;
  1692. now = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)tv.tv_usec ;
  1693. #else
  1694. struct timespec ts ;
  1695. clock_gettime (CLOCK_MONOTONIC_RAW, &ts) ;
  1696. now = (uint64_t)ts.tv_sec * (uint64_t)1000000 + (uint64_t)(ts.tv_nsec / 1000) ;
  1697. #endif
  1698. return (uint32_t)(now - epochMicro) ;
  1699. }
  1700. /*
  1701. * wiringPiVersion:
  1702. * Return our current version number
  1703. *********************************************************************************
  1704. */
  1705. void wiringPiVersion (int *major, int *minor)
  1706. {
  1707. *major = VERSION_MAJOR ;
  1708. *minor = VERSION_MINOR ;
  1709. }
  1710. /*
  1711. * wiringPiSetup:
  1712. * Must be called once at the start of your program execution.
  1713. *
  1714. * Default setup: Initialises the system into wiringPi Pin mode and uses the
  1715. * memory mapped hardware directly.
  1716. *
  1717. * Changed now to revert to "gpio" mode if we're running on a Compute Module.
  1718. *********************************************************************************
  1719. */
  1720. int wiringPiSetup (void)
  1721. {
  1722. int fd ;
  1723. int model, rev, mem, maker, overVolted ;
  1724. static int alreadyDoneThis = FALSE ;
  1725. // It's actually a fatal error to call any of the wiringPiSetup routines more than once,
  1726. // (you run out of file handles!) but I'm fed-up with the useless twats who email
  1727. // me bleating that there is a bug in my code, so screw-em.
  1728. if (alreadyDoneThis)
  1729. return 0 ;
  1730. alreadyDoneThis = TRUE ;
  1731. if (getenv (ENV_DEBUG) != NULL)
  1732. wiringPiDebug = TRUE ;
  1733. if (getenv (ENV_CODES) != NULL)
  1734. wiringPiReturnCodes = TRUE ;
  1735. if (wiringPiDebug)
  1736. printf ("wiringPi: wiringPiSetup called\n") ;
  1737. // Get the board ID information. We're not really using the information here,
  1738. // but it will give us information like the GPIO layout scheme (2 variants
  1739. // on the older 26-pin Pi's) and the GPIO peripheral base address.
  1740. // and if we're running on a compute module, then wiringPi pin numbers
  1741. // don't really many anything, so force native BCM mode anyway.
  1742. piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
  1743. if ((model == PI_MODEL_CM) || (model == PI_MODEL_CM3))
  1744. wiringPiMode = WPI_MODE_GPIO ;
  1745. else
  1746. wiringPiMode = WPI_MODE_PINS ;
  1747. /**/ if (piGpioLayout () == 1) // A, B, Rev 1, 1.1
  1748. {
  1749. pinToGpio = pinToGpioR1 ;
  1750. physToGpio = physToGpioR1 ;
  1751. }
  1752. else // A2, B2, A+, B+, CM, Pi2, Pi3, Zero
  1753. {
  1754. pinToGpio = pinToGpioR2 ;
  1755. physToGpio = physToGpioR2 ;
  1756. }
  1757. // ...
  1758. switch (model)
  1759. {
  1760. case PI_MODEL_A: case PI_MODEL_B:
  1761. case PI_MODEL_AP: case PI_MODEL_BP:
  1762. case PI_ALPHA: case PI_MODEL_CM:
  1763. case PI_MODEL_ZERO: case PI_MODEL_ZERO_W:
  1764. piGpioBase = GPIO_PERI_BASE_OLD ;
  1765. break ;
  1766. default:
  1767. piGpioBase = GPIO_PERI_BASE_NEW ;
  1768. break ;
  1769. }
  1770. // Open the master /dev/ memory control device
  1771. // Device strategy: December 2016:
  1772. // Try /dev/mem. If that fails, then
  1773. // try /dev/gpiomem. If that fails then game over.
  1774. if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0)
  1775. {
  1776. if ((fd = open ("/dev/gpiomem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0)
  1777. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: Unable to open /dev/mem or /dev/gpiomem: %s.\n Try running with sudo?\n", strerror (errno)) ;
  1778. piGpioBase = 0 ;
  1779. }
  1780. // Set the offsets into the memory interface.
  1781. GPIO_PADS = piGpioBase + 0x00100000 ;
  1782. GPIO_CLOCK_BASE = piGpioBase + 0x00101000 ;
  1783. GPIO_BASE = piGpioBase + 0x00200000 ;
  1784. GPIO_TIMER = piGpioBase + 0x0000B000 ;
  1785. GPIO_PWM = piGpioBase + 0x0020C000 ;
  1786. // Map the individual hardware components
  1787. // GPIO:
  1788. gpio = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_BASE) ;
  1789. if (gpio == MAP_FAILED)
  1790. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (GPIO) failed: %s\n", strerror (errno)) ;
  1791. // PWM
  1792. pwm = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PWM) ;
  1793. if (pwm == MAP_FAILED)
  1794. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PWM) failed: %s\n", strerror (errno)) ;
  1795. // Clock control (needed for PWM)
  1796. clk = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_CLOCK_BASE) ;
  1797. if (clk == MAP_FAILED)
  1798. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (CLOCK) failed: %s\n", strerror (errno)) ;
  1799. // The drive pads
  1800. pads = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PADS) ;
  1801. if (pads == MAP_FAILED)
  1802. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PADS) failed: %s\n", strerror (errno)) ;
  1803. #ifdef USE_TIMER
  1804. // The system timer
  1805. timer = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_TIMER) ;
  1806. if (timer == MAP_FAILED)
  1807. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (TIMER) failed: %s\n", strerror (errno)) ;
  1808. // Set the timer to free-running, 1MHz.
  1809. // 0xF9 is 249, the timer divide is base clock / (divide+1)
  1810. // so base clock is 250MHz / 250 = 1MHz.
  1811. *(timer + TIMER_CONTROL) = 0x0000280 ;
  1812. *(timer + TIMER_PRE_DIV) = 0x00000F9 ;
  1813. timerIrqRaw = timer + TIMER_IRQ_RAW ;
  1814. #endif
  1815. initialiseEpoch () ;
  1816. return 0 ;
  1817. }
  1818. /*
  1819. * wiringPiSetupGpio:
  1820. * Must be called once at the start of your program execution.
  1821. *
  1822. * GPIO setup: Initialises the system into GPIO Pin mode and uses the
  1823. * memory mapped hardware directly.
  1824. *********************************************************************************
  1825. */
  1826. int wiringPiSetupGpio (void)
  1827. {
  1828. (void)wiringPiSetup () ;
  1829. if (wiringPiDebug)
  1830. printf ("wiringPi: wiringPiSetupGpio called\n") ;
  1831. wiringPiMode = WPI_MODE_GPIO ;
  1832. return 0 ;
  1833. }
  1834. /*
  1835. * wiringPiSetupPhys:
  1836. * Must be called once at the start of your program execution.
  1837. *
  1838. * Phys setup: Initialises the system into Physical Pin mode and uses the
  1839. * memory mapped hardware directly.
  1840. *********************************************************************************
  1841. */
  1842. int wiringPiSetupPhys (void)
  1843. {
  1844. (void)wiringPiSetup () ;
  1845. if (wiringPiDebug)
  1846. printf ("wiringPi: wiringPiSetupPhys called\n") ;
  1847. wiringPiMode = WPI_MODE_PHYS ;
  1848. return 0 ;
  1849. }
  1850. /*
  1851. * wiringPiSetupSys:
  1852. * Must be called once at the start of your program execution.
  1853. *
  1854. * Initialisation (again), however this time we are using the /sys/class/gpio
  1855. * interface to the GPIO systems - slightly slower, but always usable as
  1856. * a non-root user, assuming the devices are already exported and setup correctly.
  1857. */
  1858. int wiringPiSetupSys (void)
  1859. {
  1860. int pin ;
  1861. char fName [128] ;
  1862. static int alreadyDoneThis = FALSE ;
  1863. // It's actually a fatal error to call any of the wiringPiSetup routines more than once,
  1864. // (you run out of file handles!) but I'm fed-up with the useless twats who email
  1865. // me bleating that there is a bug in my code, so screw-em.
  1866. if (alreadyDoneThis)
  1867. return 0 ;
  1868. alreadyDoneThis = TRUE ;
  1869. if (getenv (ENV_DEBUG) != NULL)
  1870. wiringPiDebug = TRUE ;
  1871. if (getenv (ENV_CODES) != NULL)
  1872. wiringPiReturnCodes = TRUE ;
  1873. if (wiringPiDebug)
  1874. printf ("wiringPi: wiringPiSetupSys called\n") ;
  1875. if (piGpioLayout () == 1)
  1876. {
  1877. pinToGpio = pinToGpioR1 ;
  1878. physToGpio = physToGpioR1 ;
  1879. }
  1880. else
  1881. {
  1882. pinToGpio = pinToGpioR2 ;
  1883. physToGpio = physToGpioR2 ;
  1884. }
  1885. // Open and scan the directory, looking for exported GPIOs, and pre-open
  1886. // the 'value' interface to speed things up for later
  1887. for (pin = 0 ; pin < 64 ; ++pin)
  1888. {
  1889. sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
  1890. sysFds [pin] = open (fName, O_RDWR) ;
  1891. }
  1892. initialiseEpoch () ;
  1893. wiringPiMode = WPI_MODE_GPIO_SYS ;
  1894. return 0 ;
  1895. }