Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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