You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

8 月之前
8 月之前
8 月之前
8 月之前
8 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
10 月之前
10 月之前
10 月之前
10 月之前
8 月之前
10 月之前
8 月之前
8 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
10 月之前
9 月之前
10 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
8 月之前
8 月之前
8 月之前
8 月之前
8 月之前
8 月之前
8 月之前
8 月之前
8 月之前
8 月之前
8 月之前
8 月之前
8 月之前
8 月之前
10 月之前
8 月之前
8 月之前
10 月之前
10 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
8 月之前
8 月之前
9 月之前
8 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
8 月之前
9 月之前
8 月之前
8 月之前
9 月之前
8 月之前
9 月之前
8 月之前
8 月之前
8 月之前
8 月之前
8 月之前
8 月之前
8 月之前
8 月之前
8 月之前
8 月之前
8 月之前
8 月之前
8 月之前
8 月之前
8 月之前
8 月之前
8 月之前
8 月之前
8 月之前
8 月之前
9 月之前
9 月之前
8 月之前
8 月之前
8 月之前
8 月之前
8 月之前
8 月之前
8 月之前
8 月之前
8 月之前
8 月之前
10 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
10 月之前
10 月之前
10 月之前
10 月之前
10 月之前
10 月之前
10 月之前
10 月之前
10 月之前
10 月之前
10 月之前
9 月之前
9 月之前
10 月之前
9 月之前
10 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
9 月之前
10 月之前
9 月之前
10 月之前
9 月之前
9 月之前
8 月之前
8 月之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820
  1. /*
  2. * wiringPi:
  3. * Arduino look-a-like Wiring library for the Raspberry Pi
  4. * Copyright (c) 2012-2024 Gordon Henderson and contributors
  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 <byteswap.h>
  71. #include <sys/utsname.h>
  72. #include <linux/gpio.h>
  73. #include "softPwm.h"
  74. #include "softTone.h"
  75. #include "wiringPi.h"
  76. #include "../version.h"
  77. #include "wiringPiLegacy.h"
  78. // Environment Variables
  79. #define ENV_DEBUG "WIRINGPI_DEBUG"
  80. #define ENV_CODES "WIRINGPI_CODES"
  81. #define ENV_GPIOMEM "WIRINGPI_GPIOMEM"
  82. // Extend wiringPi with other pin-based devices and keep track of
  83. // them in this structure
  84. struct wiringPiNodeStruct *wiringPiNodes = NULL ;
  85. // BCM Magic
  86. #define BCM_PASSWORD 0x5A000000
  87. // The BCM2835 has 54 GPIO pins.
  88. // BCM2835 data sheet, Page 90 onwards.
  89. // There are 6 control registers, each control the functions of a block
  90. // of 10 pins.
  91. // Each control register has 10 sets of 3 bits per GPIO pin - the ALT values
  92. //
  93. // 000 = GPIO Pin X is an input
  94. // 001 = GPIO Pin X is an output
  95. // 100 = GPIO Pin X takes alternate function 0
  96. // 101 = GPIO Pin X takes alternate function 1
  97. // 110 = GPIO Pin X takes alternate function 2
  98. // 111 = GPIO Pin X takes alternate function 3
  99. // 011 = GPIO Pin X takes alternate function 4
  100. // 010 = GPIO Pin X takes alternate function 5
  101. //
  102. // So the 3 bits for port X are:
  103. // X / 10 + ((X % 10) * 3)
  104. // Port function select bits
  105. #define FSEL_INPT 0b000
  106. #define FSEL_OUTP 0b001
  107. #define FSEL_ALT0 0b100
  108. #define FSEL_ALT1 0b101
  109. #define FSEL_ALT2 0b110
  110. #define FSEL_ALT3 0b111
  111. #define FSEL_ALT4 0b011
  112. #define FSEL_ALT5 0b010
  113. //RP1 chip (@Pi5) - 3.1.1. Function select
  114. #define RP1_FSEL_ALT0 0x00
  115. #define RP1_FSEL_GPIO 0x05 //SYS_RIO
  116. #define RP1_FSEL_NONE 0x09
  117. #define RP1_FSEL_NONE_HW 0x1f //default, mask
  118. //RP1 chip (@Pi5) RIO address
  119. const unsigned int RP1_RIO_OUT = 0x0000;
  120. const unsigned int RP1_RIO_OE = (0x0004/4);
  121. const unsigned int RP1_RIO_IN = (0x0008/4);
  122. //RP1 chip (@Pi5) RIO offset for set/clear value
  123. const unsigned int RP1_SET_OFFSET = (0x2000/4);
  124. const unsigned int RP1_CLR_OFFSET = (0x3000/4);
  125. //RP1 chip (@Pi5) PDE/PDU pull-up/-down enable
  126. const unsigned int RP1_PUD_UP = (1<<3);
  127. const unsigned int RP1_PUD_DOWN = (1<<2);
  128. const unsigned int RP1_INV_PUD_MASK = ~(RP1_PUD_UP | RP1_PUD_DOWN); //~0x0C
  129. //RP1 chip (@Pi5) pin level, status register
  130. const unsigned int RP1_STATUS_LEVEL_LOW = 0x00400000;
  131. const unsigned int RP1_STATUS_LEVEL_HIGH = 0x00800000;
  132. const unsigned int RP1_STATUS_LEVEL_MASK = 0x00C00000;
  133. const unsigned int RP1_DEBOUNCE_DEFAULT_VALUE = 4;
  134. const unsigned int RP1_DEBOUNCE_MASK = 0x7f;
  135. const unsigned int RP1_DEBOUNCE_DEFAULT = (RP1_DEBOUNCE_DEFAULT_VALUE << 5);
  136. const unsigned int RP1_PAD_DEFAULT_0TO8 = (0x0B | 0x70); //Slewfast, Schmitt, PullUp, | 12mA, Input enable
  137. const unsigned int RP1_PAD_DEFAULT_FROM9 = (0x07 | 0x70); //Slewfast, Schmitt, PullDown, | 12mA, Input enable
  138. const unsigned int RP1_PAD_DRIVE_MASK = 0x00000030;
  139. const unsigned int RP1_INV_PAD_DRIVE_MASK = ~(RP1_PAD_DRIVE_MASK);
  140. //RP1 chip (@Pi5) address
  141. const unsigned long long RP1_64_BASE_Addr = 0x1f000d0000;
  142. const unsigned int RP1_BASE_Addr = 0x40000000;
  143. const unsigned int RP1_PWM0_Addr = 0x40098000; // Adress is not mapped to gpiomem device!
  144. const unsigned int RP1_IO0_Addr = 0x400d0000;
  145. const unsigned int RP1_SYS_RIO0_Addr = 0x400e0000;
  146. const unsigned int RP1_PADS0_Addr = 0x400f0000;
  147. // Access from ARM Running Linux
  148. // Taken from Gert/Doms code. Some of this is not in the manual
  149. // that I can find )-:
  150. //
  151. // Updates in September 2015 - all now static variables (and apologies for the caps)
  152. // due to the Pi v2, v3, etc. and the new /dev/gpiomem interface
  153. const char* gpiomem_global = "/dev/mem";
  154. const char* gpiomem_BCM = "/dev/gpiomem";
  155. const char* gpiomem_RP1 = "/dev/gpiomem0";
  156. const int gpiomem_RP1_Size = 0x00030000;
  157. // PCIe Memory access, static define - maybe needed to detect in future
  158. //dmesg: rp1 0000:01:00.0: bar1 len 0x400000, start 0x1f00000000, end 0x1f003fffff, flags, 0x40200
  159. const char* pciemem_RP1_path = "/sys/bus/pci/devices/0000:01:00.0";
  160. const char* pciemem_RP1 = "/sys/bus/pci/devices/0000:01:00.0/resource1";
  161. const int pciemem_RP1_Size = 0x00400000;
  162. const unsigned short pciemem_RP1_Ventor= 0x1de4;
  163. const unsigned short pciemem_RP1_Device= 0x0001;
  164. static volatile unsigned int GPIO_PADS ;
  165. static volatile unsigned int GPIO_CLOCK_BASE ;
  166. static volatile unsigned int GPIO_BASE ;
  167. static volatile unsigned int GPIO_TIMER ;
  168. static volatile unsigned int GPIO_PWM ;
  169. static volatile unsigned int GPIO_RIO ;
  170. #define PAGE_SIZE (4*1024)
  171. #define BLOCK_SIZE (4*1024)
  172. static unsigned int usingGpioMem = FALSE ;
  173. static int wiringPiSetuped = FALSE ;
  174. // PWM
  175. // Word offsets into the PWM control region
  176. #define PWM_CONTROL 0
  177. #define PWM_STATUS 1
  178. #define PWM0_RANGE 4
  179. #define PWM0_DATA 5
  180. #define PWM1_RANGE 8
  181. #define PWM1_DATA 9
  182. // Clock regsiter offsets
  183. #define PWMCLK_CNTL 40
  184. #define PWMCLK_DIV 41
  185. #define PWM0_MS_MODE 0x0080 // Run in MS mode
  186. #define PWM0_USEFIFO 0x0020 // Data from FIFO
  187. #define PWM0_REVPOLAR 0x0010 // Reverse polarity
  188. #define PWM0_OFFSTATE 0x0008 // Ouput Off state
  189. #define PWM0_REPEATFF 0x0004 // Repeat last value if FIFO empty
  190. #define PWM0_SERIAL 0x0002 // Run in serial mode
  191. #define PWM0_ENABLE 0x0001 // Channel Enable
  192. #define PWM1_MS_MODE 0x8000 // Run in MS mode
  193. #define PWM1_USEFIFO 0x2000 // Data from FIFO
  194. #define PWM1_REVPOLAR 0x1000 // Reverse polarity
  195. #define PWM1_OFFSTATE 0x0800 // Ouput Off state
  196. #define PWM1_REPEATFF 0x0400 // Repeat last value if FIFO empty
  197. #define PWM1_SERIAL 0x0200 // Run in serial mode
  198. #define PWM1_ENABLE 0x0100 // Channel Enable
  199. // Timer
  200. // Word offsets
  201. #define TIMER_LOAD (0x400 >> 2)
  202. #define TIMER_VALUE (0x404 >> 2)
  203. #define TIMER_CONTROL (0x408 >> 2)
  204. #define TIMER_IRQ_CLR (0x40C >> 2)
  205. #define TIMER_IRQ_RAW (0x410 >> 2)
  206. #define TIMER_IRQ_MASK (0x414 >> 2)
  207. #define TIMER_RELOAD (0x418 >> 2)
  208. #define TIMER_PRE_DIV (0x41C >> 2)
  209. #define TIMER_COUNTER (0x420 >> 2)
  210. // Locals to hold pointers to the hardware
  211. static volatile unsigned int *base ;
  212. static volatile unsigned int *gpio ;
  213. static volatile unsigned int *pwm ;
  214. static volatile unsigned int *clk ;
  215. static volatile unsigned int *pads ;
  216. static volatile unsigned int *timer ;
  217. static volatile unsigned int *timerIrqRaw ;
  218. static volatile unsigned int *rio ;
  219. // Export variables for the hardware pointers
  220. volatile unsigned int *_wiringPiBase ;
  221. volatile unsigned int *_wiringPiGpio ;
  222. volatile unsigned int *_wiringPiPwm ;
  223. volatile unsigned int *_wiringPiClk ;
  224. volatile unsigned int *_wiringPiPads ;
  225. volatile unsigned int *_wiringPiTimer ;
  226. volatile unsigned int *_wiringPiTimerIrqRaw ;
  227. volatile unsigned int *_wiringPiRio ;
  228. // Data for use with the boardId functions.
  229. // The order of entries here to correspond with the PI_MODEL_X
  230. // and PI_VERSION_X defines in wiringPi.h
  231. // Only intended for the gpio command - use at your own risk!
  232. // piGpioBase:
  233. // The base address of the GPIO memory mapped hardware IO
  234. #define GPIO_PERI_BASE_OLD 0x20000000
  235. #define GPIO_PERI_BASE_2835 0x3F000000
  236. #define GPIO_PERI_BASE_2711 0xFE000000
  237. #define GPIO_PERI_BASE_2712 0x00 //unknown - 32-bit mapped global mem access not supported for now
  238. static volatile unsigned int piGpioBase = 0 ;
  239. const char *piModelNames [24] =
  240. {
  241. "Model A", // 0
  242. "Model B", // 1
  243. "Model A+", // 2
  244. "Model B+", // 3
  245. "Pi 2", // 4
  246. "Alpha", // 5
  247. "CM", // 6
  248. "Unknown07", // 07
  249. "Pi 3", // 08
  250. "Pi Zero", // 09
  251. "CM3", // 10
  252. "Unknown11", // 11
  253. "Pi Zero-W", // 12
  254. "Pi 3B+", // 13
  255. "Pi 3A+", // 14
  256. "Unknown15", // 15
  257. "CM3+", // 16
  258. "Pi 4B", // 17
  259. "Pi Zero2-W", // 18
  260. "Pi 400", // 19
  261. "CM4", // 20
  262. "CM4S", // 21
  263. "Unknown22", // 22
  264. "Pi 5", // 23
  265. } ;
  266. const char *piProcessor [5] =
  267. {
  268. "BCM2835",
  269. "BCM2836",
  270. "BCM2837",
  271. "BCM2711",
  272. "BCM2712",
  273. } ;
  274. const char *piRevisionNames [16] =
  275. {
  276. "00",
  277. "01",
  278. "02",
  279. "03",
  280. "04",
  281. "05",
  282. "06",
  283. "07",
  284. "08",
  285. "09",
  286. "10",
  287. "11",
  288. "12",
  289. "13",
  290. "14",
  291. "15",
  292. } ;
  293. const char *piMakerNames [16] =
  294. {
  295. "Sony", // 0
  296. "Egoman", // 1
  297. "Embest", // 2
  298. "Unknown",// 3
  299. "Embest", // 4
  300. "Stadium",// 5
  301. "Unknown06", // 6
  302. "Unknown07", // 7
  303. "Unknown08", // 8
  304. "Unknown09", // 9
  305. "Unknown10", // 10
  306. "Unknown11", // 11
  307. "Unknown12", // 12
  308. "Unknown13", // 13
  309. "Unknown14", // 14
  310. "Unknown15", // 15
  311. } ;
  312. const int piMemorySize [8] =
  313. {
  314. 256, // 0
  315. 512, // 1
  316. 1024, // 2
  317. 2048, // 3
  318. 4096, // 4
  319. 8192, // 5
  320. 0, // 6
  321. 0, // 7
  322. } ;
  323. // Time for easy calculations
  324. static uint64_t epochMilli, epochMicro ;
  325. // Misc
  326. static int wiringPiMode = WPI_MODE_UNINITIALISED ;
  327. static volatile int pinPass = -1 ;
  328. static pthread_mutex_t pinMutex ;
  329. static int RaspberryPiModel = -1;
  330. static int RaspberryPiLayout = -1;
  331. // Debugging & Return codes
  332. int wiringPiDebug = FALSE ;
  333. int wiringPiReturnCodes = FALSE ;
  334. // Use /dev/gpiomem ?
  335. int wiringPiTryGpioMem = FALSE ;
  336. // sysFds:
  337. // Map a file descriptor from the /sys/class/gpio/gpioX/value
  338. static int sysFds [64] =
  339. {
  340. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  341. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  342. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  343. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  344. } ;
  345. // ISR Data
  346. static int chipFd = -1;
  347. static void (*isrFunctions [64])(void) ;
  348. static pthread_t isrThreads[64];
  349. static int isrMode[64];
  350. // Doing it the Arduino way with lookup tables...
  351. // Yes, it's probably more innefficient than all the bit-twidling, but it
  352. // does tend to make it all a bit clearer. At least to me!
  353. // pinToGpio:
  354. // Take a Wiring pin (0 through X) and re-map it to the BCM_GPIO pin
  355. // Cope for 3 different board revisions here.
  356. static int *pinToGpio ;
  357. // Revision 1, 1.1:
  358. static int pinToGpioR1 [64] =
  359. {
  360. 17, 18, 21, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7: wpi 0 - 7
  361. 0, 1, // I2C - SDA1, SCL1 wpi 8 - 9
  362. 8, 7, // SPI - CE1, CE0 wpi 10 - 11
  363. 10, 9, 11, // SPI - MOSI, MISO, SCLK wpi 12 - 14
  364. 14, 15, // UART - Tx, Rx wpi 15 - 16
  365. // Padding:
  366. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 31
  367. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
  368. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
  369. } ;
  370. // Revision 2:
  371. static int pinToGpioR2 [64] =
  372. {
  373. 17, 18, 27, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7: wpi 0 - 7
  374. 2, 3, // I2C - SDA0, SCL0 wpi 8 - 9
  375. 8, 7, // SPI - CE1, CE0 wpi 10 - 11
  376. 10, 9, 11, // SPI - MOSI, MISO, SCLK wpi 12 - 14
  377. 14, 15, // UART - Tx, Rx wpi 15 - 16
  378. 28, 29, 30, 31, // Rev 2: New GPIOs 8 though 11 wpi 17 - 20
  379. 5, 6, 13, 19, 26, // B+ wpi 21, 22, 23, 24, 25
  380. 12, 16, 20, 21, // B+ wpi 26, 27, 28, 29
  381. 0, 1, // B+ wpi 30, 31
  382. // Padding:
  383. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
  384. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
  385. } ;
  386. // physToGpio:
  387. // Take a physical pin (1 through 26) and re-map it to the BCM_GPIO pin
  388. // Cope for 2 different board revisions here.
  389. // Also add in the P5 connector, so the P5 pins are 3,4,5,6, so 53,54,55,56
  390. static int *physToGpio ;
  391. static int physToGpioR1 [64] =
  392. {
  393. -1, // 0
  394. -1, -1, // 1, 2
  395. 0, -1,
  396. 1, -1,
  397. 4, 14,
  398. -1, 15,
  399. 17, 18,
  400. 21, -1,
  401. 22, 23,
  402. -1, 24,
  403. 10, -1,
  404. 9, 25,
  405. 11, 8,
  406. -1, 7, // 25, 26
  407. -1, -1, -1, -1, -1, // ... 31
  408. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
  409. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
  410. } ;
  411. static int physToGpioR2 [64] =
  412. {
  413. -1, // 0
  414. -1, -1, // 1, 2
  415. 2, -1,
  416. 3, -1,
  417. 4, 14,
  418. -1, 15,
  419. 17, 18,
  420. 27, -1,
  421. 22, 23,
  422. -1, 24,
  423. 10, -1,
  424. 9, 25,
  425. 11, 8,
  426. -1, 7, // 25, 26
  427. // B+
  428. 0, 1,
  429. 5, -1,
  430. 6, 12,
  431. 13, -1,
  432. 19, 16,
  433. 26, 20,
  434. -1, 21,
  435. // the P5 connector on the Rev 2 boards:
  436. -1, -1,
  437. -1, -1,
  438. -1, -1,
  439. -1, -1,
  440. -1, -1,
  441. 28, 29,
  442. 30, 31,
  443. -1, -1,
  444. -1, -1,
  445. -1, -1,
  446. -1, -1,
  447. } ;
  448. const int _5v=-1;
  449. const int _0v=-1;
  450. const int _3v=-1;
  451. static int physToSysGPIOPi5 [41] =
  452. {
  453. -1, // 0
  454. _3v, _5v, // 1, 2
  455. 401, _5v,
  456. 402, _0v,
  457. 403, 413,
  458. _0v, 414,
  459. 416, 417,
  460. 426, _0v,
  461. 421, 422,
  462. _3v, 423,
  463. 409, _0v,
  464. 408, 424,
  465. 410, 407,
  466. _0v, 406,
  467. 399, 400,
  468. 404, _0v,
  469. 405, 411,
  470. 412, _0v,
  471. 418, 415,
  472. 425, 419,
  473. _0v, 420, //39, 40
  474. } ;
  475. int GPIOToSysFS(const int pin) {
  476. int sysfspin = pin;
  477. if (RaspberryPiModel<0) { //need to detect pi model
  478. int model, rev, mem, maker, overVolted ;
  479. piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
  480. }
  481. if (PI_MODEL_5 == RaspberryPiModel) {
  482. sysfspin = pin + 399;
  483. if (sysfspin<399 || sysfspin>426) { // only 399-426 supported, 40-pin GPIO header
  484. sysfspin = -1;
  485. }
  486. }
  487. if (wiringPiDebug)
  488. printf ("GPIOToSysFS: translate bcm gpio %d to sysfs gpio %d\n", pin, sysfspin) ;
  489. return sysfspin;
  490. }
  491. int GetMaxPin() {
  492. return PI_MODEL_5 == RaspberryPiModel ? 27 : 63;
  493. }
  494. #define RETURN_ON_MODEL5 if (PI_MODEL_5 == RaspberryPiModel) { if (wiringPiDebug) printf("Function not supported on Pi5\n"); return; }
  495. int FailOnModel5() {
  496. if (PI_MODEL_5 == RaspberryPiModel) {
  497. return wiringPiFailure (WPI_ALMOST, "Function not supported on Raspberry Pi 5.\n"
  498. " Unable to continue. Keep an eye of new versions at https://github.com/wiringpi/wiringpi\n") ;
  499. }
  500. return 0;
  501. }
  502. // gpioToGPFSEL:
  503. // Map a BCM_GPIO pin to it's Function Selection
  504. // control port. (GPFSEL 0-5)
  505. // Groups of 10 - 3 bits per Function - 30 bits per port
  506. static uint8_t gpioToGPFSEL [] =
  507. {
  508. 0,0,0,0,0,0,0,0,0,0,
  509. 1,1,1,1,1,1,1,1,1,1,
  510. 2,2,2,2,2,2,2,2,2,2,
  511. 3,3,3,3,3,3,3,3,3,3,
  512. 4,4,4,4,4,4,4,4,4,4,
  513. 5,5,5,5,5,5,5,5,5,5,
  514. } ;
  515. // gpioToShift
  516. // Define the shift up for the 3 bits per pin in each GPFSEL port
  517. static uint8_t gpioToShift [] =
  518. {
  519. 0,3,6,9,12,15,18,21,24,27,
  520. 0,3,6,9,12,15,18,21,24,27,
  521. 0,3,6,9,12,15,18,21,24,27,
  522. 0,3,6,9,12,15,18,21,24,27,
  523. 0,3,6,9,12,15,18,21,24,27,
  524. 0,3,6,9,12,15,18,21,24,27,
  525. } ;
  526. // gpioToGPSET:
  527. // (Word) offset to the GPIO Set registers for each GPIO pin
  528. static uint8_t gpioToGPSET [] =
  529. {
  530. 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,
  531. 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,
  532. } ;
  533. // gpioToGPCLR:
  534. // (Word) offset to the GPIO Clear registers for each GPIO pin
  535. static uint8_t gpioToGPCLR [] =
  536. {
  537. 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,
  538. 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,
  539. } ;
  540. // gpioToGPLEV:
  541. // (Word) offset to the GPIO Input level registers for each GPIO pin
  542. static uint8_t gpioToGPLEV [] =
  543. {
  544. 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,
  545. 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,
  546. } ;
  547. #ifdef notYetReady
  548. // gpioToEDS
  549. // (Word) offset to the Event Detect Status
  550. static uint8_t gpioToEDS [] =
  551. {
  552. 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,
  553. 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,
  554. } ;
  555. // gpioToREN
  556. // (Word) offset to the Rising edge ENable register
  557. static uint8_t gpioToREN [] =
  558. {
  559. 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,
  560. 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,
  561. } ;
  562. // gpioToFEN
  563. // (Word) offset to the Falling edgde ENable register
  564. static uint8_t gpioToFEN [] =
  565. {
  566. 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,
  567. 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,
  568. } ;
  569. #endif
  570. // GPPUD:
  571. // GPIO Pin pull up/down register
  572. #define GPPUD 37
  573. /* 2711 has a different mechanism for pin pull-up/down/enable */
  574. #define GPPUPPDN0 57 /* Pin pull-up/down for pins 15:0 */
  575. #define GPPUPPDN1 58 /* Pin pull-up/down for pins 31:16 */
  576. #define GPPUPPDN2 59 /* Pin pull-up/down for pins 47:32 */
  577. #define GPPUPPDN3 60 /* Pin pull-up/down for pins 57:48 */
  578. static volatile unsigned int piGpioPupOffset = 0 ;
  579. // gpioToPUDCLK
  580. // (Word) offset to the Pull Up Down Clock regsiter
  581. static uint8_t gpioToPUDCLK [] =
  582. {
  583. 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,
  584. 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,
  585. } ;
  586. // gpioToPwmALT
  587. // the ALT value to put a GPIO pin into PWM mode
  588. static uint8_t gpioToPwmALT [] =
  589. {
  590. 0, 0, 0, 0, 0, 0, 0, 0, // 0 -> 7
  591. 0, 0, 0, 0, FSEL_ALT0, FSEL_ALT0, 0, 0, // 8 -> 15
  592. 0, 0, FSEL_ALT5, FSEL_ALT5, 0, 0, 0, 0, // 16 -> 23
  593. 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
  594. 0, 0, 0, 0, 0, 0, 0, 0, // 32 -> 39
  595. FSEL_ALT0, FSEL_ALT0, 0, 0, 0, FSEL_ALT0, 0, 0, // 40 -> 47
  596. 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
  597. 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
  598. } ;
  599. // gpioToPwmPort
  600. // The port value to put a GPIO pin into PWM mode
  601. static uint8_t gpioToPwmPort [] =
  602. {
  603. 0, 0, 0, 0, 0, 0, 0, 0, // 0 -> 7
  604. 0, 0, 0, 0, PWM0_DATA, PWM1_DATA, 0, 0, // 8 -> 15
  605. 0, 0, PWM0_DATA, PWM1_DATA, 0, 0, 0, 0, // 16 -> 23
  606. 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
  607. 0, 0, 0, 0, 0, 0, 0, 0, // 32 -> 39
  608. PWM0_DATA, PWM1_DATA, 0, 0, 0, PWM1_DATA, 0, 0, // 40 -> 47
  609. 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
  610. 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
  611. } ;
  612. // gpioToGpClkALT:
  613. // ALT value to put a GPIO pin into GP Clock mode.
  614. // On the Pi we can really only use BCM_GPIO_4 and BCM_GPIO_21
  615. // for clocks 0 and 1 respectively, however I'll include the full
  616. // list for completeness - maybe one day...
  617. #define GPIO_CLOCK_SOURCE 1
  618. // gpioToGpClkALT0:
  619. static uint8_t gpioToGpClkALT0 [] =
  620. {
  621. 0, 0, 0, 0, FSEL_ALT0, FSEL_ALT0, FSEL_ALT0, 0, // 0 -> 7
  622. 0, 0, 0, 0, 0, 0, 0, 0, // 8 -> 15
  623. 0, 0, 0, 0, FSEL_ALT5, FSEL_ALT5, 0, 0, // 16 -> 23
  624. 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
  625. FSEL_ALT0, 0, FSEL_ALT0, 0, 0, 0, 0, 0, // 32 -> 39
  626. 0, 0, FSEL_ALT0, FSEL_ALT0, FSEL_ALT0, 0, 0, 0, // 40 -> 47
  627. 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
  628. 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
  629. } ;
  630. // gpioToClk:
  631. // (word) Offsets to the clock Control and Divisor register
  632. static uint8_t gpioToClkCon [] =
  633. {
  634. -1, -1, -1, -1, 28, 30, 32, -1, // 0 -> 7
  635. -1, -1, -1, -1, -1, -1, -1, -1, // 8 -> 15
  636. -1, -1, -1, -1, 28, 30, -1, -1, // 16 -> 23
  637. -1, -1, -1, -1, -1, -1, -1, -1, // 24 -> 31
  638. 28, -1, 28, -1, -1, -1, -1, -1, // 32 -> 39
  639. -1, -1, 28, 30, 28, -1, -1, -1, // 40 -> 47
  640. -1, -1, -1, -1, -1, -1, -1, -1, // 48 -> 55
  641. -1, -1, -1, -1, -1, -1, -1, -1, // 56 -> 63
  642. } ;
  643. static uint8_t gpioToClkDiv [] =
  644. {
  645. -1, -1, -1, -1, 29, 31, 33, -1, // 0 -> 7
  646. -1, -1, -1, -1, -1, -1, -1, -1, // 8 -> 15
  647. -1, -1, -1, -1, 29, 31, -1, -1, // 16 -> 23
  648. -1, -1, -1, -1, -1, -1, -1, -1, // 24 -> 31
  649. 29, -1, 29, -1, -1, -1, -1, -1, // 32 -> 39
  650. -1, -1, 29, 31, 29, -1, -1, -1, // 40 -> 47
  651. -1, -1, -1, -1, -1, -1, -1, -1, // 48 -> 55
  652. -1, -1, -1, -1, -1, -1, -1, -1, // 56 -> 63
  653. } ;
  654. /*
  655. * Functions
  656. *********************************************************************************
  657. */
  658. /*
  659. * wiringPiFailure:
  660. * Fail. Or not.
  661. *********************************************************************************
  662. */
  663. int wiringPiFailure (int fatal, const char *message, ...)
  664. {
  665. va_list argp ;
  666. char buffer [1024] ;
  667. if (!fatal && wiringPiReturnCodes)
  668. return -1 ;
  669. va_start (argp, message) ;
  670. vsnprintf (buffer, 1023, message, argp) ;
  671. va_end (argp) ;
  672. fprintf (stderr, "%s", buffer) ;
  673. exit (EXIT_FAILURE) ;
  674. return 0 ;
  675. }
  676. /*
  677. * setupCheck
  678. * Another sanity check because some users forget to call the setup
  679. * function. Mosty because they need feeding C drip by drip )-:
  680. *********************************************************************************
  681. */
  682. static void setupCheck (const char *fName)
  683. {
  684. if (!wiringPiSetuped)
  685. {
  686. fprintf (stderr, "%s: You have not called one of the wiringPiSetup\n"
  687. " functions, so I'm aborting your program before it crashes anyway.\n", fName) ;
  688. exit (EXIT_FAILURE) ;
  689. }
  690. }
  691. /*
  692. * gpioMemCheck:
  693. * See if we're using the /dev/gpiomem interface, if-so then some operations
  694. * can't be done and will crash the Pi.
  695. *********************************************************************************
  696. */
  697. static void usingGpioMemCheck (const char *what)
  698. {
  699. if (usingGpioMem)
  700. {
  701. fprintf (stderr, "%s: Unable to do this when using /dev/gpiomem. Try sudo?\n", what) ;
  702. exit (EXIT_FAILURE) ;
  703. }
  704. }
  705. void PrintSystemStdErr () {
  706. struct utsname sys_info;
  707. if (uname(&sys_info) == 0) {
  708. fprintf (stderr, " wiringpi = %d.%d\n", VERSION_MAJOR, VERSION_MINOR);
  709. fprintf (stderr, " system name = %s\n", sys_info.sysname);
  710. //fprintf (stderr, " node name = %s\n", sys_info.nodename);
  711. fprintf (stderr, " release = %s\n", sys_info.release);
  712. fprintf (stderr, " version = %s\n", sys_info.version);
  713. fprintf (stderr, " machine = %s\n", sys_info.machine);
  714. if (strstr(sys_info.machine, "arm") == NULL && strstr(sys_info.machine, "aarch")==NULL) {
  715. fprintf (stderr, " -> This is not an ARM architecture; it cannot be a Raspberry Pi.\n") ;
  716. }
  717. }
  718. }
  719. void piFunctionOops (const char *function, const char* suggestion, const char* url)
  720. {
  721. fprintf (stderr, "Oops: Function %s is not supported\n", function) ;
  722. PrintSystemStdErr();
  723. if (suggestion) {
  724. fprintf (stderr, " -> Please %s\n", suggestion) ;
  725. }
  726. if (url) {
  727. fprintf (stderr, " -> See info at %s\n", url) ;
  728. }
  729. fprintf (stderr, " -> Check at https://github.com/wiringpi/wiringpi/issues.\n\n") ;
  730. exit (EXIT_FAILURE) ;
  731. }
  732. /*
  733. * piGpioLayout:
  734. * Return a number representing the hardware revision of the board.
  735. * This is not strictly the board revision but is used to check the
  736. * layout of the GPIO connector - and there are 2 types that we are
  737. * really interested in here. The very earliest Pi's and the
  738. * ones that came after that which switched some pins ....
  739. *
  740. * Revision 1 really means the early Model A and B's.
  741. * Revision 2 is everything else - it covers the B, B+ and CM.
  742. * ... and the Pi 2 - which is a B+ ++ ...
  743. * ... and the Pi 0 - which is an A+ ...
  744. *
  745. * The main difference between the revision 1 and 2 system that I use here
  746. * is the mapping of the GPIO pins. From revision 2, the Pi Foundation changed
  747. * 3 GPIO pins on the (original) 26-way header - BCM_GPIO 22 was dropped and
  748. * replaced with 27, and 0 + 1 - I2C bus 0 was changed to 2 + 3; I2C bus 1.
  749. *
  750. * Additionally, here we set the piModel2 flag too. This is again, nothing to
  751. * do with the actual model, but the major version numbers - the GPIO base
  752. * hardware address changed at model 2 and above (not the Zero though)
  753. *
  754. *********************************************************************************
  755. */
  756. const char* revfile = "/proc/device-tree/system/linux,revision";
  757. void piGpioLayoutOops (const char *why)
  758. {
  759. fprintf (stderr, "Oops: Unable to determine Raspberry Pi board revision from %s and from /proc/cpuinfo\n", revfile) ;
  760. PrintSystemStdErr();
  761. fprintf (stderr, " -> %s\n", why) ;
  762. fprintf (stderr, " -> WiringPi is designed for Raspberry Pi and can only be used with a Raspberry Pi.\n\n") ;
  763. fprintf (stderr, " -> Check at https://github.com/wiringpi/wiringpi/issues.\n\n") ;
  764. exit (EXIT_FAILURE) ;
  765. }
  766. int piGpioLayout (void)
  767. {
  768. if (-1==RaspberryPiLayout) {
  769. int model, rev, mem, maker, overVolted ;
  770. piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
  771. }
  772. return RaspberryPiLayout;
  773. }
  774. /*
  775. * piBoardRev:
  776. * Deprecated, but does the same as piGpioLayout
  777. *********************************************************************************
  778. */
  779. int piBoardRev (void)
  780. {
  781. return piGpioLayout () ;
  782. }
  783. const char* GetPiRevision(char* line, int linelength, unsigned int* revision) {
  784. const char* c = NULL;
  785. uint32_t Revision = 0;
  786. _Static_assert(sizeof(Revision)==4, "should be unsigend integer with 4 byte size");
  787. FILE* fp = fopen(revfile,"rb");
  788. if (!fp) {
  789. if (wiringPiDebug)
  790. perror(revfile);
  791. return NULL; // revision file not found or no access
  792. }
  793. int result = fread(&Revision, sizeof(Revision), 1, fp);
  794. fclose(fp);
  795. if (result<1) {
  796. if (wiringPiDebug)
  797. perror(revfile);
  798. return NULL; // read error
  799. }
  800. Revision = bswap_32(Revision);
  801. snprintf(line, linelength, "Revision\t: %04x", Revision);
  802. c = &line[11];
  803. *revision = Revision;
  804. if (wiringPiDebug)
  805. printf("GetPiRevision: Revision string: \"%s\" (%s) - 0x%x\n", line, c, *revision);
  806. return c;
  807. }
  808. /*
  809. * piBoardId:
  810. * Return the real details of the board we have.
  811. *
  812. * This is undocumented and really only intended for the GPIO command.
  813. * Use at your own risk!
  814. *
  815. * Seems there are some boards with 0000 in them (mistake in manufacture)
  816. * So the distinction between boards that I can see is:
  817. *
  818. * 0000 - Error
  819. * 0001 - Not used
  820. *
  821. * Original Pi boards:
  822. * 0002 - Model B, Rev 1, 256MB, Egoman
  823. * 0003 - Model B, Rev 1.1, 256MB, Egoman, Fuses/D14 removed.
  824. *
  825. * Newer Pi's with remapped GPIO:
  826. * 0004 - Model B, Rev 1.2, 256MB, Sony
  827. * 0005 - Model B, Rev 1.2, 256MB, Egoman
  828. * 0006 - Model B, Rev 1.2, 256MB, Egoman
  829. *
  830. * 0007 - Model A, Rev 1.2, 256MB, Egoman
  831. * 0008 - Model A, Rev 1.2, 256MB, Sony
  832. * 0009 - Model A, Rev 1.2, 256MB, Egoman
  833. *
  834. * 000d - Model B, Rev 1.2, 512MB, Egoman (Red Pi, Blue Pi?)
  835. * 000e - Model B, Rev 1.2, 512MB, Sony
  836. * 000f - Model B, Rev 1.2, 512MB, Egoman
  837. *
  838. * 0010 - Model B+, Rev 1.2, 512MB, Sony
  839. * 0013 - Model B+ Rev 1.2, 512MB, Embest
  840. * 0016 - Model B+ Rev 1.2, 512MB, Sony
  841. * 0019 - Model B+ Rev 1.2, 512MB, Egoman
  842. *
  843. * 0011 - Pi CM, Rev 1.1, 512MB, Sony
  844. * 0014 - Pi CM, Rev 1.1, 512MB, Embest
  845. * 0017 - Pi CM, Rev 1.1, 512MB, Sony
  846. * 001a - Pi CM, Rev 1.1, 512MB, Egoman
  847. *
  848. * 0012 - Model A+ Rev 1.1, 256MB, Sony
  849. * 0015 - Model A+ Rev 1.1, 512MB, Embest
  850. * 0018 - Model A+ Rev 1.1, 256MB, Sony
  851. * 001b - Model A+ Rev 1.1, 256MB, Egoman
  852. *
  853. * A small thorn is the olde style overvolting - that will add in
  854. * 1000000
  855. *
  856. * The Pi compute module has an revision of 0011 or 0014 - since we only
  857. * check the last digit, then it's 1, therefore it'll default to not 2 or
  858. * 3 for a Rev 1, so will appear as a Rev 2. This is fine for the most part, but
  859. * we'll properly detect the Compute Module later and adjust accordingly.
  860. *
  861. * And then things changed with the introduction of the v2...
  862. *
  863. * For Pi v2 and subsequent models - e.g. the Zero:
  864. *
  865. * [USER:8] [NEW:1] [MEMSIZE:3] [MANUFACTURER:4] [PROCESSOR:4] [TYPE:8] [REV:4]
  866. * NEW 23: will be 1 for the new scheme, 0 for the old scheme
  867. * MEMSIZE 20: 0=256M 1=512M 2=1G
  868. * MANUFACTURER 16: 0=SONY 1=EGOMAN 2=EMBEST
  869. * PROCESSOR 12: 0=2835 1=2836
  870. * TYPE 04: 0=MODELA 1=MODELB 2=MODELA+ 3=MODELB+ 4=Pi2 MODEL B 5=ALPHA 6=CM
  871. * REV 00: 0=REV0 1=REV1 2=REV2
  872. *********************************************************************************
  873. */
  874. void piBoardId (int *model, int *rev, int *mem, int *maker, int *warranty)
  875. {
  876. const int maxlength = 120;
  877. char line [maxlength+1] ;
  878. const char *c ;
  879. unsigned int revision = 0x00 ;
  880. int bRev, bType, bProc, bMfg, bMem, bWarranty ;
  881. //piGpioLayoutOops ("this is only a test case");
  882. c = GetPiRevision(line, maxlength, &revision); // device tree
  883. if (NULL==c) {
  884. c = GetPiRevisionLegacy(line, maxlength, &revision); // proc/cpuinfo
  885. }
  886. if (NULL==c) {
  887. piGpioLayoutOops ("GetPiRevision failed!") ;
  888. }
  889. if ((revision & (1 << 23)) != 0) // New style, not available for Raspberry Pi 1B/A, CM
  890. {
  891. if (wiringPiDebug)
  892. printf ("piBoardId: New Way: revision is: %08X\n", revision) ;
  893. bRev = (revision & (0x0F << 0)) >> 0 ;
  894. bType = (revision & (0xFF << 4)) >> 4 ;
  895. bProc = (revision & (0x0F << 12)) >> 12 ; // Not used for now.
  896. bMfg = (revision & (0x0F << 16)) >> 16 ;
  897. bMem = (revision & (0x07 << 20)) >> 20 ;
  898. bWarranty = (revision & (0x03 << 24)) != 0 ;
  899. // Ref: https://www.raspberrypi.com/documentation/computers/raspberry-pi.html#raspberry-pi-revision-codes
  900. *model = bType ;
  901. *rev = bRev ;
  902. *mem = bMem ;
  903. *maker = bMfg ;
  904. *warranty = bWarranty ;
  905. RaspberryPiLayout = GPIO_LAYOUT_DEFAULT ; //default
  906. if (wiringPiDebug)
  907. printf ("piBoardId: rev: %d, type: %d, proc: %d, mfg: %d, mem: %d, warranty: %d\n",
  908. bRev, bType, bProc, bMfg, bMem, bWarranty) ;
  909. }
  910. else // Old way
  911. {
  912. if (wiringPiDebug)
  913. printf ("piBoardId: Old Way: revision is: %s\n", c) ;
  914. if (!isdigit (*c))
  915. piGpioLayoutOops ("Bogus \"Revision\" line (no digit at start of revision)") ;
  916. // Make sure its long enough
  917. if (strlen (c) < 4)
  918. piGpioLayoutOops ("Bogus \"Revision\" line (not long enough)") ;
  919. // If longer than 4, we'll assume it's been overvolted
  920. *warranty = strlen (c) > 4 ;
  921. // Extract last 4 characters:
  922. c = c + strlen (c) - 4 ;
  923. // Fill out the replys as appropriate
  924. RaspberryPiLayout = GPIO_LAYOUT_DEFAULT ; //default
  925. /**/ if (strcmp (c, "0002") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; RaspberryPiLayout = GPIO_LAYOUT_PI1_REV1; }
  926. else if (strcmp (c, "0003") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_1 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; RaspberryPiLayout = GPIO_LAYOUT_PI1_REV1; }
  927. else if (strcmp (c, "0004") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_2 ; *mem = 0 ; *maker = PI_MAKER_SONY ; }
  928. else if (strcmp (c, "0005") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_2 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
  929. else if (strcmp (c, "0006") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_2 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
  930. else if (strcmp (c, "0007") == 0) { *model = PI_MODEL_A ; *rev = PI_VERSION_1_2 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
  931. else if (strcmp (c, "0008") == 0) { *model = PI_MODEL_A ; *rev = PI_VERSION_1_2 ; *mem = 0 ; *maker = PI_MAKER_SONY ; ; }
  932. else if (strcmp (c, "0009") == 0) { *model = PI_MODEL_A ; *rev = PI_VERSION_1_2 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
  933. else if (strcmp (c, "000d") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_EGOMAN ; }
  934. else if (strcmp (c, "000e") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_SONY ; }
  935. else if (strcmp (c, "000f") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_EGOMAN ; }
  936. else if (strcmp (c, "0010") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_SONY ; }
  937. else if (strcmp (c, "0013") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_EMBEST ; }
  938. else if (strcmp (c, "0016") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_SONY ; }
  939. else if (strcmp (c, "0019") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_EGOMAN ; }
  940. else if (strcmp (c, "0011") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_1 ; *mem = 1 ; *maker = PI_MAKER_SONY ; }
  941. else if (strcmp (c, "0014") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_1 ; *mem = 1 ; *maker = PI_MAKER_EMBEST ; }
  942. else if (strcmp (c, "0017") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_1 ; *mem = 1 ; *maker = PI_MAKER_SONY ; }
  943. else if (strcmp (c, "001a") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_1 ; *mem = 1 ; *maker = PI_MAKER_EGOMAN ; }
  944. else if (strcmp (c, "0012") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_1 ; *mem = 0 ; *maker = PI_MAKER_SONY ; }
  945. else if (strcmp (c, "0015") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_1 ; *mem = 1 ; *maker = PI_MAKER_EMBEST ; }
  946. else if (strcmp (c, "0018") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_1 ; *mem = 0 ; *maker = PI_MAKER_SONY ; }
  947. else if (strcmp (c, "001b") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_1 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
  948. else { *model = 0 ; *rev = 0 ; *mem = 0 ; *maker = 0 ; }
  949. }
  950. RaspberryPiModel = *model;
  951. }
  952. /*
  953. * wpiPinToGpio:
  954. * Translate a wiringPi Pin number to native GPIO pin number.
  955. * Provided for external support.
  956. *********************************************************************************
  957. */
  958. int wpiPinToGpio (int wpiPin)
  959. {
  960. return pinToGpio [wpiPin & 63] ;
  961. }
  962. /*
  963. * physPinToGpio:
  964. * Translate a physical Pin number to native GPIO pin number.
  965. * Provided for external support.
  966. *********************************************************************************
  967. */
  968. int physPinToGpio (int physPin)
  969. {
  970. return physToGpio [physPin & 63] ;
  971. }
  972. /*
  973. * setPadDrive:
  974. * Set the PAD driver value
  975. *********************************************************************************
  976. */
  977. void setPadDrivePin (int pin, int value) {
  978. if (PI_MODEL_5 != RaspberryPiModel) return;
  979. if (pin < 0 || pin > GetMaxPin()) return ;
  980. uint32_t wrVal;
  981. value = value & 3; // 0-3 supported
  982. wrVal = (value << 4); //Drive strength 0-3
  983. pads[1+pin] = (pads[1+pin] & RP1_INV_PAD_DRIVE_MASK) | wrVal;
  984. if (wiringPiDebug) {
  985. printf ("setPadDrivePin: pin: %d, value: %d (%08X)\n", pin, value, pads[1+pin]) ;
  986. }
  987. }
  988. void setPadDrive (int group, int value)
  989. {
  990. uint32_t wrVal, rdVal;
  991. if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
  992. {
  993. value = value & 7; // 0-7 supported
  994. if (PI_MODEL_5 == RaspberryPiModel) {
  995. if (-1==group) {
  996. printf ("Pad register:\n");
  997. for (int pin=0, maxpin=GetMaxPin(); pin<=maxpin; ++pin) {
  998. unsigned int drive = (pads[1+pin] & RP1_PAD_DRIVE_MASK)>>4;
  999. printf (" Pin %2d: 0x%08X drive: 0x%d = %2dmA\n", pin, pads[1+pin], drive, 0==drive ? 2 : drive*4) ;
  1000. }
  1001. }
  1002. if (group !=0) { // only GPIO range @RP1
  1003. return ;
  1004. }
  1005. switch(value) {
  1006. default:
  1007. /* bcm*/ // RP1
  1008. case 0: /* 2mA*/ value=0; break; // 2mA
  1009. case 1: /* 4mA*/
  1010. case 2: /* 6mA*/ value=1; break; // 4mA
  1011. case 3: /* 8mA*/
  1012. case 4: /*10mA*/ value=2; break; // 8mA
  1013. case 5: /*12mA*/
  1014. case 6: /*14mA*/
  1015. case 7: /*16mA*/ value=3; break; //12mA
  1016. }
  1017. wrVal = (value << 4); //Drive strength 0-3
  1018. //set for all pins even when it's avaiable for each pin separately
  1019. for (int pin=0, maxpin=GetMaxPin(); pin<=maxpin; ++pin) {
  1020. pads[1+pin] = (pads[1+pin] & RP1_INV_PAD_DRIVE_MASK) | wrVal;
  1021. }
  1022. rdVal = pads[1+17]; // only pin 17 readback, for logging
  1023. } else {
  1024. if (-1==group) {
  1025. printf ("Pad register: Group 0: 0x%08X, Group 1: 0x%08X, Group 2: 0x%08X\n", *(pads + 0 + 11), *(pads + 1 + 11), *(pads + 2 + 11)) ;
  1026. }
  1027. if ((group < 0) || (group > 2))
  1028. return ;
  1029. wrVal = BCM_PASSWORD | 0x18 | value; //Drive strength 0-7
  1030. *(pads + group + 11) = wrVal ;
  1031. rdVal = *(pads + group + 11);
  1032. }
  1033. if (wiringPiDebug)
  1034. {
  1035. printf ("setPadDrive: Group: %d, value: %d (%08X)\n", group, value, wrVal) ;
  1036. printf ("Read : %08X\n", rdVal) ;
  1037. }
  1038. }
  1039. }
  1040. /*
  1041. * getAlt:
  1042. * Returns the ALT bits for a given port. Only really of-use
  1043. * for the gpio readall command (I think)
  1044. *********************************************************************************
  1045. */
  1046. int getAlt (int pin)
  1047. {
  1048. int fSel, shift, alt ;
  1049. pin &= 63 ;
  1050. /**/ if (wiringPiMode == WPI_MODE_PINS)
  1051. pin = pinToGpio [pin] ;
  1052. else if (wiringPiMode == WPI_MODE_PHYS)
  1053. pin = physToGpio [pin] ;
  1054. else if (wiringPiMode != WPI_MODE_GPIO)
  1055. return 0 ;
  1056. if (PI_MODEL_5 == RaspberryPiModel) {
  1057. alt = (gpio[2*pin+1] & RP1_FSEL_NONE_HW); //0-4 function
  1058. /*
  1059. BCM:
  1060. 000b = GPIO Pin 9 is an input
  1061. 001b = GPIO Pin 9 is an output
  1062. 100b = GPIO Pin 9 takes alternate function 0
  1063. 101b = GPIO Pin 9 takes alternate function 1
  1064. 110b = GPIO Pin 9 takes alternate function 2
  1065. 111b = GPIO Pin 9 takes alternate function 3
  1066. 011b = GPIO Pin 9 takes alternate function 4
  1067. 010b = GPIO Pin 9 takes alternate function 5
  1068. RP1:
  1069. 8 = alternate function 6
  1070. 9 = alternate function 7
  1071. 10 = alternate function 8
  1072. 11 = alternate function 9
  1073. */
  1074. switch(alt) {
  1075. case 0: return 4;
  1076. case 1: return 5;
  1077. case 2: return 6;
  1078. case 3: return 7;
  1079. case 4: return 3;
  1080. case RP1_FSEL_GPIO: {
  1081. unsigned int outputmask = gpio[2*pin] & 0x3000; //Bit13-OETOPAD + Bit12-OEFROMPERI
  1082. return (outputmask==0x3000) ? 1 : 0; //1=OUT 0=IN
  1083. }
  1084. case 6: return 8;
  1085. case 7: return 9;
  1086. case 8: return 10;
  1087. case 9: return 11;
  1088. default:return alt;
  1089. }
  1090. } else {
  1091. fSel = gpioToGPFSEL [pin] ;
  1092. shift = gpioToShift [pin] ;
  1093. alt = (*(gpio + fSel) >> shift) & 7 ;
  1094. }
  1095. return alt ;
  1096. }
  1097. /*
  1098. * pwmSetMode:
  1099. * Select the native "balanced" mode, or standard mark:space mode
  1100. *********************************************************************************
  1101. */
  1102. void pwmSetMode (int mode)
  1103. {
  1104. if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
  1105. {
  1106. FailOnModel5();
  1107. if (mode == PWM_MODE_MS)
  1108. *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE | PWM0_MS_MODE | PWM1_MS_MODE ;
  1109. else
  1110. *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ;
  1111. }
  1112. }
  1113. /*
  1114. * pwmSetRange:
  1115. * Set the PWM range register. We set both range registers to the same
  1116. * value. If you want different in your own code, then write your own.
  1117. *********************************************************************************
  1118. */
  1119. void pwmSetRange (unsigned int range)
  1120. {
  1121. FailOnModel5();
  1122. if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
  1123. {
  1124. *(pwm + PWM0_RANGE) = range ; delayMicroseconds (10) ;
  1125. *(pwm + PWM1_RANGE) = range ; delayMicroseconds (10) ;
  1126. }
  1127. }
  1128. /*
  1129. * pwmSetClock:
  1130. * Set/Change the PWM clock. Originally my code, but changed
  1131. * (for the better!) by Chris Hall, <chris@kchall.plus.com>
  1132. * after further study of the manual and testing with a 'scope
  1133. *********************************************************************************
  1134. */
  1135. void pwmSetClock (int divisor)
  1136. {
  1137. uint32_t pwm_control ;
  1138. FailOnModel5();
  1139. if (piGpioBase == GPIO_PERI_BASE_2711)
  1140. {
  1141. divisor = 540*divisor/192;
  1142. }
  1143. divisor &= 4095 ;
  1144. if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
  1145. {
  1146. if (wiringPiDebug)
  1147. printf ("Setting to: %d. Current: 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;
  1148. pwm_control = *(pwm + PWM_CONTROL) ; // preserve PWM_CONTROL
  1149. // We need to stop PWM prior to stopping PWM clock in MS mode otherwise BUSY
  1150. // stays high.
  1151. *(pwm + PWM_CONTROL) = 0 ; // Stop PWM
  1152. // Stop PWM clock before changing divisor. The delay after this does need to
  1153. // this big (95uS occasionally fails, 100uS OK), it's almost as though the BUSY
  1154. // flag is not working properly in balanced mode. Without the delay when DIV is
  1155. // adjusted the clock sometimes switches to very slow, once slow further DIV
  1156. // adjustments do nothing and it's difficult to get out of this mode.
  1157. *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x01 ; // Stop PWM Clock
  1158. delayMicroseconds (110) ; // prevents clock going sloooow
  1159. while ((*(clk + PWMCLK_CNTL) & 0x80) != 0) // Wait for clock to be !BUSY
  1160. delayMicroseconds (1) ;
  1161. *(clk + PWMCLK_DIV) = BCM_PASSWORD | (divisor << 12) ;
  1162. *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x11 ; // Start PWM clock
  1163. *(pwm + PWM_CONTROL) = pwm_control ; // restore PWM_CONTROL
  1164. if (wiringPiDebug)
  1165. printf ("Set to: %d. Now : 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;
  1166. }
  1167. }
  1168. /*
  1169. * gpioClockSet:
  1170. * Set the frequency on a GPIO clock pin
  1171. *********************************************************************************
  1172. */
  1173. void gpioClockSet (int pin, int freq)
  1174. {
  1175. int divi, divr, divf ;
  1176. FailOnModel5();
  1177. pin &= 63 ;
  1178. /**/ if (wiringPiMode == WPI_MODE_PINS)
  1179. pin = pinToGpio [pin] ;
  1180. else if (wiringPiMode == WPI_MODE_PHYS)
  1181. pin = physToGpio [pin] ;
  1182. else if (wiringPiMode != WPI_MODE_GPIO)
  1183. return ;
  1184. divi = 19200000 / freq ;
  1185. divr = 19200000 % freq ;
  1186. divf = (int)((double)divr * 4096.0 / 19200000.0) ;
  1187. if (divi > 4095)
  1188. divi = 4095 ;
  1189. *(clk + gpioToClkCon [pin]) = BCM_PASSWORD | GPIO_CLOCK_SOURCE ; // Stop GPIO Clock
  1190. while ((*(clk + gpioToClkCon [pin]) & 0x80) != 0) // ... and wait
  1191. ;
  1192. *(clk + gpioToClkDiv [pin]) = BCM_PASSWORD | (divi << 12) | divf ; // Set dividers
  1193. *(clk + gpioToClkCon [pin]) = BCM_PASSWORD | 0x10 | GPIO_CLOCK_SOURCE ; // Start Clock
  1194. }
  1195. /*
  1196. * wiringPiFindNode:
  1197. * Locate our device node
  1198. *********************************************************************************
  1199. */
  1200. struct wiringPiNodeStruct *wiringPiFindNode (int pin)
  1201. {
  1202. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1203. while (node != NULL)
  1204. if ((pin >= node->pinBase) && (pin <= node->pinMax))
  1205. return node ;
  1206. else
  1207. node = node->next ;
  1208. return NULL ;
  1209. }
  1210. /*
  1211. * wiringPiNewNode:
  1212. * Create a new GPIO node into the wiringPi handling system
  1213. *********************************************************************************
  1214. */
  1215. static void pinModeDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int mode) { return ; }
  1216. static void pullUpDnControlDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int pud) { return ; }
  1217. //static unsigned int digitalRead8Dummy (UNU struct wiringPiNodeStruct *node, UNU int UNU pin) { return 0 ; }
  1218. //static void digitalWrite8Dummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int value) { return ; }
  1219. static int digitalReadDummy (UNU struct wiringPiNodeStruct *node, UNU int UNU pin) { return LOW ; }
  1220. static void digitalWriteDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int value) { return ; }
  1221. static void pwmWriteDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int value) { return ; }
  1222. static int analogReadDummy (UNU struct wiringPiNodeStruct *node, UNU int pin) { return 0 ; }
  1223. static void analogWriteDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int value) { return ; }
  1224. struct wiringPiNodeStruct *wiringPiNewNode (int pinBase, int numPins)
  1225. {
  1226. int pin ;
  1227. struct wiringPiNodeStruct *node ;
  1228. // Minimum pin base is 64
  1229. if (pinBase < 64)
  1230. (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: pinBase of %d is < 64\n", pinBase) ;
  1231. // Check all pins in-case there is overlap:
  1232. for (pin = pinBase ; pin < (pinBase + numPins) ; ++pin)
  1233. if (wiringPiFindNode (pin) != NULL)
  1234. (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: Pin %d overlaps with existing definition\n", pin) ;
  1235. node = (struct wiringPiNodeStruct *)calloc (sizeof (struct wiringPiNodeStruct), 1) ; // calloc zeros
  1236. if (node == NULL)
  1237. (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: Unable to allocate memory: %s\n", strerror (errno)) ;
  1238. node->pinBase = pinBase ;
  1239. node->pinMax = pinBase + numPins - 1 ;
  1240. node->pinMode = pinModeDummy ;
  1241. node->pullUpDnControl = pullUpDnControlDummy ;
  1242. node->digitalRead = digitalReadDummy ;
  1243. //node->digitalRead8 = digitalRead8Dummy ;
  1244. node->digitalWrite = digitalWriteDummy ;
  1245. //node->digitalWrite8 = digitalWrite8Dummy ;
  1246. node->pwmWrite = pwmWriteDummy ;
  1247. node->analogRead = analogReadDummy ;
  1248. node->analogWrite = analogWriteDummy ;
  1249. node->next = wiringPiNodes ;
  1250. wiringPiNodes = node ;
  1251. return node ;
  1252. }
  1253. #ifdef notYetReady
  1254. /*
  1255. * pinED01:
  1256. * pinED10:
  1257. * Enables edge-detect mode on a pin - from a 0 to a 1 or 1 to 0
  1258. * Pin must already be in input mode with appropriate pull up/downs set.
  1259. *********************************************************************************
  1260. */
  1261. void pinEnableED01Pi (int pin)
  1262. {
  1263. pin = pinToGpio [pin & 63] ;
  1264. }
  1265. #endif
  1266. /*
  1267. *********************************************************************************
  1268. * Core Functions
  1269. *********************************************************************************
  1270. */
  1271. /*
  1272. * pinModeAlt:
  1273. * This is an un-documented special to let you set any pin to any mode
  1274. *********************************************************************************
  1275. */
  1276. void pinModeAlt (int pin, int mode)
  1277. {
  1278. int fSel, shift ;
  1279. RETURN_ON_MODEL5
  1280. setupCheck ("pinModeAlt") ;
  1281. if ((pin & PI_GPIO_MASK) == 0) // On-board pin
  1282. {
  1283. /**/ if (wiringPiMode == WPI_MODE_PINS)
  1284. pin = pinToGpio [pin] ;
  1285. else if (wiringPiMode == WPI_MODE_PHYS)
  1286. pin = physToGpio [pin] ;
  1287. else if (wiringPiMode != WPI_MODE_GPIO)
  1288. return ;
  1289. if (PI_MODEL_5 == RaspberryPiModel) {
  1290. //confusion! diffrent to to BCM! this is taking directly the value for the register
  1291. /*
  1292. "alt0" 0b100
  1293. "alt1" 0b101
  1294. "alt2" 0b110
  1295. "alt3" 0b111
  1296. "alt4" 0b011
  1297. "alt5" 0b010
  1298. */
  1299. gpio[2*pin+1] = (mode & RP1_FSEL_NONE_HW) | RP1_DEBOUNCE_DEFAULT; //0-4 function, 5-11 debounce time
  1300. } else {
  1301. fSel = gpioToGPFSEL [pin] ;
  1302. shift = gpioToShift [pin] ;
  1303. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | ((mode & 0x7) << shift) ;
  1304. }
  1305. }
  1306. }
  1307. /*
  1308. * pinMode:
  1309. * Sets the mode of a pin to be input, output or PWM output
  1310. *********************************************************************************
  1311. */
  1312. //Default: rp1_set_pad(pin, 0, 1, 0, 1, 1, 1, 0);
  1313. void rp1_set_pad(int pin, int slewfast, int schmitt, int pulldown, int pullup, int drive, int inputenable, int outputdisable) {
  1314. pads[1+pin] = (slewfast != 0) | ((schmitt != 0) << 1) | ((pulldown != 0) << 2) | ((pullup != 0) << 3) | ((drive & 0x3) << 4) | ((inputenable != 0) << 6) | ((outputdisable != 0) << 7);
  1315. }
  1316. void pinMode (int pin, int mode)
  1317. {
  1318. int fSel, shift, alt ;
  1319. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1320. int origPin = pin ;
  1321. if (wiringPiDebug)
  1322. printf ("pinMode: pin:%d mode:%d\n", pin, mode) ;
  1323. setupCheck ("pinMode") ;
  1324. if ((pin & PI_GPIO_MASK) == 0) // On-board pin
  1325. {
  1326. /**/ if (wiringPiMode == WPI_MODE_PINS)
  1327. pin = pinToGpio [pin] ;
  1328. else if (wiringPiMode == WPI_MODE_PHYS)
  1329. pin = physToGpio [pin] ;
  1330. else if (wiringPiMode != WPI_MODE_GPIO)
  1331. return ;
  1332. if (wiringPiDebug)
  1333. printf ("pinMode: bcm pin:%d mode:%d\n", pin, mode) ;
  1334. softPwmStop (origPin) ;
  1335. softToneStop (origPin) ;
  1336. fSel = gpioToGPFSEL [pin] ;
  1337. shift = gpioToShift [pin] ;
  1338. if (mode == INPUT) {
  1339. if (PI_MODEL_5 == RaspberryPiModel) {
  1340. pads[1+pin] = (pin<=8) ? RP1_PAD_DEFAULT_0TO8 : RP1_PAD_DEFAULT_FROM9;
  1341. gpio[2*pin+1] = RP1_FSEL_GPIO | RP1_DEBOUNCE_DEFAULT; // GPIO
  1342. rio[RP1_RIO_OE + RP1_CLR_OFFSET] = 1<<pin; // Input
  1343. } else {
  1344. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) ; // Sets bits to zero = input
  1345. }
  1346. } else if (mode == OUTPUT) {
  1347. if (PI_MODEL_5 == RaspberryPiModel) {
  1348. pads[1+pin] = (pin<=8) ? RP1_PAD_DEFAULT_0TO8 : RP1_PAD_DEFAULT_FROM9;
  1349. gpio[2*pin+1] = RP1_FSEL_GPIO | RP1_DEBOUNCE_DEFAULT; // GPIO
  1350. rio[RP1_RIO_OE + RP1_SET_OFFSET] = 1<<pin; // Output
  1351. } else {
  1352. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (1 << shift) ;
  1353. }
  1354. } else if (mode == SOFT_PWM_OUTPUT) {
  1355. softPwmCreate (origPin, 0, 100) ;
  1356. } else if (mode == SOFT_TONE_OUTPUT) {
  1357. softToneCreate (origPin) ;
  1358. } else if (mode == PWM_TONE_OUTPUT)
  1359. {
  1360. pinMode (origPin, PWM_OUTPUT) ; // Call myself to enable PWM mode
  1361. pwmSetMode (PWM_MODE_MS) ;
  1362. }
  1363. else if (mode == PWM_OUTPUT)
  1364. {
  1365. RETURN_ON_MODEL5
  1366. if ((alt = gpioToPwmALT [pin]) == 0) // Not a hardware capable PWM pin
  1367. return ;
  1368. usingGpioMemCheck ("pinMode PWM") ;
  1369. // Set pin to PWM mode
  1370. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ;
  1371. delayMicroseconds (110) ; // See comments in pwmSetClockWPi
  1372. pwmSetMode (PWM_MODE_BAL) ; // Pi default mode
  1373. pwmSetRange (1024) ; // Default range of 1024
  1374. pwmSetClock (32) ; // 19.2 / 32 = 600KHz - Also starts the PWM
  1375. }
  1376. else if (mode == GPIO_CLOCK)
  1377. {
  1378. RETURN_ON_MODEL5
  1379. if ((alt = gpioToGpClkALT0 [pin]) == 0) // Not a GPIO_CLOCK pin
  1380. return ;
  1381. usingGpioMemCheck ("pinMode CLOCK") ;
  1382. // Set pin to GPIO_CLOCK mode and set the clock frequency to 100KHz
  1383. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ;
  1384. delayMicroseconds (110) ;
  1385. gpioClockSet (pin, 100000) ;
  1386. }
  1387. }
  1388. else
  1389. {
  1390. if ((node = wiringPiFindNode (pin)) != NULL)
  1391. node->pinMode (node, pin, mode) ;
  1392. return ;
  1393. }
  1394. }
  1395. /*
  1396. * pullUpDownCtrl:
  1397. * Control the internal pull-up/down resistors on a GPIO pin.
  1398. *********************************************************************************
  1399. */
  1400. void pullUpDnControl (int pin, int pud)
  1401. {
  1402. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1403. setupCheck ("pullUpDnControl") ;
  1404. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1405. {
  1406. /**/ if (wiringPiMode == WPI_MODE_PINS)
  1407. pin = pinToGpio [pin] ;
  1408. else if (wiringPiMode == WPI_MODE_PHYS)
  1409. pin = physToGpio [pin] ;
  1410. else if (wiringPiMode != WPI_MODE_GPIO)
  1411. return ;
  1412. if (PI_MODEL_5 == RaspberryPiModel) {
  1413. unsigned int pullbits = pads[1+pin] & RP1_INV_PUD_MASK; // remove bits
  1414. switch (pud){
  1415. case PUD_OFF: pads[1+pin] = pullbits; break;
  1416. case PUD_UP: pads[1+pin] = pullbits | RP1_PUD_UP; break;
  1417. case PUD_DOWN: pads[1+pin] = pullbits | RP1_PUD_DOWN; break;
  1418. default: return ; /* An illegal value */
  1419. }
  1420. } else {
  1421. if (piGpioPupOffset == GPPUPPDN0)
  1422. {
  1423. // Pi 4B pull up/down method
  1424. int pullreg = GPPUPPDN0 + (pin>>4);
  1425. int pullshift = (pin & 0xf) << 1;
  1426. unsigned int pullbits;
  1427. unsigned int pull;
  1428. switch (pud)
  1429. {
  1430. case PUD_OFF: pull = 0; break;
  1431. case PUD_UP: pull = 1; break;
  1432. case PUD_DOWN: pull = 2; break;
  1433. default: return ; /* An illegal value */
  1434. }
  1435. pullbits = *(gpio + pullreg);
  1436. pullbits &= ~(3 << pullshift);
  1437. pullbits |= (pull << pullshift);
  1438. *(gpio + pullreg) = pullbits;
  1439. }
  1440. else
  1441. {
  1442. // legacy pull up/down method
  1443. *(gpio + GPPUD) = pud & 3 ; delayMicroseconds (5) ;
  1444. *(gpio + gpioToPUDCLK [pin]) = 1 << (pin & 31) ; delayMicroseconds (5) ;
  1445. *(gpio + GPPUD) = 0 ; delayMicroseconds (5) ;
  1446. *(gpio + gpioToPUDCLK [pin]) = 0 ; delayMicroseconds (5) ;
  1447. }
  1448. }
  1449. }
  1450. else // Extension module
  1451. {
  1452. if ((node = wiringPiFindNode (pin)) != NULL)
  1453. node->pullUpDnControl (node, pin, pud) ;
  1454. return ;
  1455. }
  1456. }
  1457. /*
  1458. * digitalRead:
  1459. * Read the value of a given Pin, returning HIGH or LOW
  1460. *********************************************************************************
  1461. */
  1462. int digitalRead (int pin)
  1463. {
  1464. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1465. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1466. {
  1467. if (wiringPiMode == WPI_MODE_GPIO_SYS)
  1468. return LOW ;
  1469. else if (wiringPiMode == WPI_MODE_PINS)
  1470. pin = pinToGpio [pin] ;
  1471. else if (wiringPiMode == WPI_MODE_PHYS)
  1472. pin = physToGpio [pin] ;
  1473. else if (wiringPiMode != WPI_MODE_GPIO)
  1474. return LOW ;
  1475. if (PI_MODEL_5 == RaspberryPiModel) {
  1476. switch(gpio[2*pin] & RP1_STATUS_LEVEL_MASK) {
  1477. default: // 11 or 00 not allowed, give LOW!
  1478. case RP1_STATUS_LEVEL_LOW: return LOW ;
  1479. case RP1_STATUS_LEVEL_HIGH: return HIGH ;
  1480. }
  1481. } else {
  1482. if ((*(gpio + gpioToGPLEV [pin]) & (1 << (pin & 31))) != 0)
  1483. return HIGH ;
  1484. else
  1485. return LOW ;
  1486. }
  1487. }
  1488. else
  1489. {
  1490. if ((node = wiringPiFindNode (pin)) == NULL)
  1491. return LOW ;
  1492. return node->digitalRead (node, pin) ;
  1493. }
  1494. }
  1495. /*
  1496. * digitalRead8:
  1497. * Read 8-bits (a byte) from given start pin.
  1498. *********************************************************************************
  1499. unsigned int digitalRead8 (int pin)
  1500. {
  1501. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1502. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1503. return 0 ;
  1504. else
  1505. {
  1506. if ((node = wiringPiFindNode (pin)) == NULL)
  1507. return LOW ;
  1508. return node->digitalRead8 (node, pin) ;
  1509. }
  1510. }
  1511. */
  1512. /*
  1513. * digitalWrite:
  1514. * Set an output bit
  1515. *********************************************************************************
  1516. */
  1517. void digitalWrite (int pin, int value)
  1518. {
  1519. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1520. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1521. {
  1522. if (wiringPiMode == WPI_MODE_GPIO_SYS) // Sys mode
  1523. return ;
  1524. else if (wiringPiMode == WPI_MODE_PINS)
  1525. pin = pinToGpio [pin] ;
  1526. else if (wiringPiMode == WPI_MODE_PHYS)
  1527. pin = physToGpio [pin] ;
  1528. else if (wiringPiMode != WPI_MODE_GPIO)
  1529. return ;
  1530. if (PI_MODEL_5 == RaspberryPiModel) {
  1531. if (value == LOW) {
  1532. //printf("Set pin %d >>0x%08x<< to low\n", pin, 1<<pin);
  1533. rio[RP1_RIO_OUT + RP1_CLR_OFFSET] = 1<<pin;
  1534. } else {
  1535. //printf("Set pin %d >>0x%08x<< to high\n", pin, 1<<pin);
  1536. rio[RP1_RIO_OUT + RP1_SET_OFFSET] = 1<<pin;
  1537. }
  1538. } else {
  1539. if (value == LOW)
  1540. *(gpio + gpioToGPCLR [pin]) = 1 << (pin & 31) ;
  1541. else
  1542. *(gpio + gpioToGPSET [pin]) = 1 << (pin & 31) ;
  1543. }
  1544. }
  1545. else
  1546. {
  1547. if ((node = wiringPiFindNode (pin)) != NULL)
  1548. node->digitalWrite (node, pin, value) ;
  1549. }
  1550. }
  1551. /*
  1552. * digitalWrite8:
  1553. * Set an output 8-bit byte on the device from the given pin number
  1554. *********************************************************************************
  1555. void digitalWrite8 (int pin, int value)
  1556. {
  1557. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1558. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1559. return ;
  1560. else
  1561. {
  1562. if ((node = wiringPiFindNode (pin)) != NULL)
  1563. node->digitalWrite8 (node, pin, value) ;
  1564. }
  1565. }
  1566. */
  1567. /*
  1568. * pwmWrite:
  1569. * Set an output PWM value
  1570. *********************************************************************************
  1571. */
  1572. void pwmWrite (int pin, int value)
  1573. {
  1574. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1575. FailOnModel5();
  1576. setupCheck ("pwmWrite") ;
  1577. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1578. {
  1579. /**/ if (wiringPiMode == WPI_MODE_PINS)
  1580. pin = pinToGpio [pin] ;
  1581. else if (wiringPiMode == WPI_MODE_PHYS)
  1582. pin = physToGpio [pin] ;
  1583. else if (wiringPiMode != WPI_MODE_GPIO)
  1584. return ;
  1585. usingGpioMemCheck ("pwmWrite") ;
  1586. *(pwm + gpioToPwmPort [pin]) = value ;
  1587. }
  1588. else
  1589. {
  1590. if ((node = wiringPiFindNode (pin)) != NULL)
  1591. node->pwmWrite (node, pin, value) ;
  1592. }
  1593. }
  1594. /*
  1595. * analogRead:
  1596. * Read the analog value of a given Pin.
  1597. * There is no on-board Pi analog hardware,
  1598. * so this needs to go to a new node.
  1599. *********************************************************************************
  1600. */
  1601. int analogRead (int pin)
  1602. {
  1603. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1604. if ((node = wiringPiFindNode (pin)) == NULL)
  1605. return 0 ;
  1606. else
  1607. return node->analogRead (node, pin) ;
  1608. }
  1609. /*
  1610. * analogWrite:
  1611. * Write the analog value to the given Pin.
  1612. * There is no on-board Pi analog hardware,
  1613. * so this needs to go to a new node.
  1614. *********************************************************************************
  1615. */
  1616. void analogWrite (int pin, int value)
  1617. {
  1618. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1619. if ((node = wiringPiFindNode (pin)) == NULL)
  1620. return ;
  1621. node->analogWrite (node, pin, value) ;
  1622. }
  1623. /*
  1624. * pwmToneWrite:
  1625. * Pi Specific.
  1626. * Output the given frequency on the Pi's PWM pin
  1627. *********************************************************************************
  1628. */
  1629. void pwmToneWrite (int pin, int freq)
  1630. {
  1631. FailOnModel5();
  1632. setupCheck ("pwmToneWrite") ;
  1633. if (freq == 0)
  1634. pwmWrite (pin, 0) ; // Off
  1635. else
  1636. {
  1637. int range = 600000 / freq ;
  1638. pwmSetRange (range) ;
  1639. pwmWrite (pin, freq / 2) ;
  1640. }
  1641. }
  1642. /*
  1643. * digitalWriteByte:
  1644. * digitalReadByte:
  1645. * Pi Specific
  1646. * Write an 8-bit byte to the first 8 GPIO pins - try to do it as
  1647. * fast as possible.
  1648. * However it still needs 2 operations to set the bits, so any external
  1649. * hardware must not rely on seeing a change as there will be a change
  1650. * to set the outputs bits to zero, then another change to set the 1's
  1651. * Reading is just bit fiddling.
  1652. * These are wiringPi pin numbers 0..7, or BCM_GPIO pin numbers
  1653. * 17, 18, 22, 23, 24, 24, 4 on a Pi v1 rev 0-3
  1654. * 17, 18, 27, 23, 24, 24, 4 on a Pi v1 rev 3 onwards or B+, 2, 3, zero
  1655. *********************************************************************************
  1656. */
  1657. void digitalWriteByte (const int value)
  1658. {
  1659. uint32_t pinSet = 0 ;
  1660. uint32_t pinClr = 0 ;
  1661. int mask = 1 ;
  1662. int pin ;
  1663. FailOnModel5();
  1664. if (wiringPiMode == WPI_MODE_GPIO_SYS)
  1665. {
  1666. return ;
  1667. }
  1668. else
  1669. {
  1670. for (pin = 0 ; pin < 8 ; ++pin)
  1671. {
  1672. if ((value & mask) == 0)
  1673. pinClr |= (1 << pinToGpio [pin]) ;
  1674. else
  1675. pinSet |= (1 << pinToGpio [pin]) ;
  1676. mask <<= 1 ;
  1677. }
  1678. *(gpio + gpioToGPCLR [0]) = pinClr ;
  1679. *(gpio + gpioToGPSET [0]) = pinSet ;
  1680. }
  1681. }
  1682. unsigned int digitalReadByte (void)
  1683. {
  1684. int pin, x ;
  1685. uint32_t raw ;
  1686. uint32_t data = 0 ;
  1687. FailOnModel5();
  1688. if (wiringPiMode == WPI_MODE_GPIO_SYS)
  1689. {
  1690. return 0;
  1691. }
  1692. else
  1693. {
  1694. raw = *(gpio + gpioToGPLEV [0]) ; // First bank for these pins
  1695. for (pin = 0 ; pin < 8 ; ++pin)
  1696. {
  1697. x = pinToGpio [pin] ;
  1698. data = (data << 1) | (((raw & (1 << x)) == 0) ? 0 : 1) ;
  1699. }
  1700. }
  1701. return data ;
  1702. }
  1703. /*
  1704. * digitalWriteByte2:
  1705. * digitalReadByte2:
  1706. * Pi Specific
  1707. * Write an 8-bit byte to the second set of 8 GPIO pins. This is marginally
  1708. * faster than the first lot as these are consecutive BCM_GPIO pin numbers.
  1709. * However they overlap with the original read/write bytes.
  1710. *********************************************************************************
  1711. */
  1712. void digitalWriteByte2 (const int value)
  1713. {
  1714. FailOnModel5();
  1715. if (wiringPiMode == WPI_MODE_GPIO_SYS)
  1716. {
  1717. }
  1718. else
  1719. {
  1720. *(gpio + gpioToGPCLR [0]) = (~value & 0xFF) << 20 ; // 0x0FF00000; ILJ > CHANGE: Old causes glitch
  1721. *(gpio + gpioToGPSET [0]) = ( value & 0xFF) << 20 ;
  1722. }
  1723. }
  1724. unsigned int digitalReadByte2 (void)
  1725. {
  1726. uint32_t data = 0 ;
  1727. FailOnModel5();
  1728. if (wiringPiMode == WPI_MODE_GPIO_SYS)
  1729. {
  1730. }
  1731. else
  1732. data = ((*(gpio + gpioToGPLEV [0])) >> 20) & 0xFF ; // First bank for these pins
  1733. return data ;
  1734. }
  1735. /*
  1736. * waitForInterrupt:
  1737. * Pi Specific.
  1738. * Wait for Interrupt on a GPIO pin.
  1739. * This is actually done via the /dev/gpiochip interface regardless of
  1740. * the wiringPi access mode in-use. Maybe sometime it might get a better
  1741. * way for a bit more efficiency.
  1742. *********************************************************************************
  1743. */
  1744. int waitForInterrupt (int pin, int mS)
  1745. {
  1746. int fd, ret;
  1747. struct pollfd polls ;
  1748. struct gpioevent_data evdata;
  1749. //struct gpio_v2_line_request req2;
  1750. if (wiringPiMode == WPI_MODE_PINS)
  1751. pin = pinToGpio [pin] ;
  1752. else if (wiringPiMode == WPI_MODE_PHYS)
  1753. pin = physToGpio [pin] ;
  1754. if ((fd = sysFds [pin]) == -1)
  1755. return -2 ;
  1756. // Setup poll structure
  1757. polls.fd = fd;
  1758. polls.events = POLLIN | POLLERR ;
  1759. polls.revents = 0;
  1760. // Wait for it ...
  1761. ret = poll(&polls, 1, mS);
  1762. if (ret <= 0) {
  1763. fprintf(stderr, "wiringPi: ERROR: poll returned=%d\n", ret);
  1764. } else {
  1765. //if (polls.revents & POLLIN)
  1766. if (wiringPiDebug) {
  1767. printf ("wiringPi: IRQ line %d received %d, fd=%d\n", pin, ret, sysFds [pin]) ;
  1768. }
  1769. /* read event data */
  1770. int readret = read(sysFds [pin], &evdata, sizeof(evdata));
  1771. if (readret == sizeof(evdata)) {
  1772. if (wiringPiDebug) {
  1773. printf ("wiringPi: IRQ data id: %d, timestamp: %lld\n", evdata.id, evdata.timestamp) ;
  1774. }
  1775. ret = evdata.id;
  1776. } else {
  1777. ret = 0;
  1778. }
  1779. }
  1780. return ret;
  1781. }
  1782. const char DEV_GPIO_PI[] ="/dev/gpiochip0";
  1783. const char DEV_GPIO_PI5[]="/dev/gpiochip4";
  1784. int waitForInterruptInit (int pin, int mode)
  1785. {
  1786. const char* strmode = "";
  1787. if (wiringPiMode == WPI_MODE_PINS) {
  1788. pin = pinToGpio [pin] ;
  1789. } else if (wiringPiMode == WPI_MODE_PHYS) {
  1790. pin = physToGpio [pin] ;
  1791. }
  1792. /* open gpio */
  1793. sleep(1);
  1794. const char* gpiochip = PI_MODEL_5 == RaspberryPiModel ? DEV_GPIO_PI5 : DEV_GPIO_PI;
  1795. if (chipFd < 0) {
  1796. chipFd = open(gpiochip, O_RDWR);
  1797. if (chipFd < 0) {
  1798. fprintf(stderr, "wiringPi: ERROR: %s open ret=%d\n", gpiochip, chipFd);
  1799. return -1;
  1800. }
  1801. }
  1802. if (wiringPiDebug) {
  1803. printf ("wiringPi: Open chip %s succeded, fd=%d\n", gpiochip, chipFd) ;
  1804. }
  1805. struct gpioevent_request req;
  1806. req.lineoffset = pin;
  1807. req.handleflags = GPIOHANDLE_REQUEST_INPUT;
  1808. switch(mode) {
  1809. default:
  1810. case INT_EDGE_SETUP:
  1811. if (wiringPiDebug) {
  1812. printf ("wiringPi: waitForInterruptMode mode INT_EDGE_SETUP - exiting\n") ;
  1813. }
  1814. return -1;
  1815. case INT_EDGE_FALLING:
  1816. req.eventflags = GPIOEVENT_REQUEST_FALLING_EDGE;
  1817. strmode = "falling";
  1818. break;
  1819. case INT_EDGE_RISING:
  1820. req.eventflags = GPIOEVENT_REQUEST_RISING_EDGE;
  1821. strmode = "rising";
  1822. break;
  1823. case INT_EDGE_BOTH:
  1824. req.eventflags = GPIOEVENT_REQUEST_BOTH_EDGES;
  1825. strmode = "both";
  1826. break;
  1827. }
  1828. strncpy(req.consumer_label, "wiringpi_gpio_irq", sizeof(req.consumer_label) - 1);
  1829. //later implement GPIO_V2_GET_LINE_IOCTL req2
  1830. int ret = ioctl(chipFd, GPIO_GET_LINEEVENT_IOCTL, &req);
  1831. if (ret) {
  1832. fprintf(stderr, "wiringPi: ERROR: %s ioctl get line %d %s returned %d\n", gpiochip, pin, strmode, ret);
  1833. return -1;
  1834. }
  1835. if (wiringPiDebug) {
  1836. printf ("wiringPi: GPIO get line %d , mode %s succeded, fd=%d\n", pin, strmode, req.fd) ;
  1837. }
  1838. /* set event fd nonbloack read */
  1839. int fd_line = req.fd;
  1840. sysFds [pin] = fd_line;
  1841. int flags = fcntl(fd_line, F_GETFL);
  1842. flags |= O_NONBLOCK;
  1843. ret = fcntl(fd_line, F_SETFL, flags);
  1844. if (ret) {
  1845. fprintf(stderr, "wiringPi: ERROR: %s fcntl set nonblock read=%d\n", gpiochip, chipFd);
  1846. return -1;
  1847. }
  1848. return 0;
  1849. }
  1850. int waitForInterruptClose (int pin) {
  1851. if (sysFds[pin]>0) {
  1852. if (wiringPiDebug) {
  1853. printf ("wiringPi: waitForInterruptClose close thread 0x%lX\n", (unsigned long)isrThreads[pin]) ;
  1854. }
  1855. if (pthread_cancel(isrThreads[pin]) == 0) {
  1856. if (wiringPiDebug) {
  1857. printf ("wiringPi: waitForInterruptClose thread canceled successfuly\n") ;
  1858. }
  1859. } else {
  1860. if (wiringPiDebug) {
  1861. fprintf (stderr, "wiringPi: waitForInterruptClose could not cancel thread\n");
  1862. }
  1863. }
  1864. close(sysFds [pin]);
  1865. }
  1866. sysFds [pin] = -1;
  1867. isrFunctions [pin] = NULL;
  1868. /* -not closing so far - other isr may be using it - only close if no other is using - will code later
  1869. if (chipFd>0) {
  1870. close(chipFd);
  1871. }
  1872. chipFd = -1;
  1873. */
  1874. if (wiringPiDebug) {
  1875. printf ("wiringPi: waitForInterruptClose finished\n") ;
  1876. }
  1877. return 0;
  1878. }
  1879. int wiringPiISRStop (int pin) {
  1880. return waitForInterruptClose (pin);
  1881. }
  1882. /*
  1883. * interruptHandler:
  1884. * This is a thread and gets started to wait for the interrupt we're
  1885. * hoping to catch. It will call the user-function when the interrupt
  1886. * fires.
  1887. *********************************************************************************
  1888. */
  1889. static void *interruptHandler (UNU void *arg)
  1890. {
  1891. int pin ;
  1892. (void)piHiPri (55) ; // Only effective if we run as root
  1893. pin = pinPass ;
  1894. pinPass = -1 ;
  1895. for (;;) {
  1896. int ret = waitForInterrupt(pin, -1);
  1897. if ( ret> 0) {
  1898. if (wiringPiDebug) {
  1899. printf ("wiringPi: call function\n") ;
  1900. }
  1901. if(isrFunctions [pin]) {
  1902. isrFunctions [pin] () ;
  1903. }
  1904. // wait again - in the past forever - now can be stopped by waitForInterruptClose
  1905. } else if( ret< 0) {
  1906. break; // stop thread!
  1907. }
  1908. }
  1909. waitForInterruptClose (pin);
  1910. if (wiringPiDebug) {
  1911. printf ("wiringPi: interruptHandler finished\n") ;
  1912. }
  1913. return NULL ;
  1914. }
  1915. /*
  1916. * wiringPiISR:
  1917. * Pi Specific.
  1918. * Take the details and create an interrupt handler that will do a call-
  1919. * back to the user supplied function.
  1920. *********************************************************************************
  1921. */
  1922. int wiringPiISR (int pin, int mode, void (*function)(void))
  1923. {
  1924. const int maxpin = GetMaxPin();
  1925. if (pin < 0 || pin > maxpin)
  1926. return wiringPiFailure (WPI_FATAL, "wiringPiISR: pin must be 0-%d (%d)\n", maxpin, pin) ;
  1927. if (wiringPiMode == WPI_MODE_UNINITIALISED)
  1928. return wiringPiFailure (WPI_FATAL, "wiringPiISR: wiringPi has not been initialised. Unable to continue.\n") ;
  1929. if (wiringPiDebug) {
  1930. printf ("wiringPi: wiringPiISR pin %d, mode %d\n", pin, mode) ;
  1931. }
  1932. if (isrFunctions [pin]) {
  1933. printf ("wiringPi: ISR function alread active, ignoring \n") ;
  1934. }
  1935. isrFunctions [pin] = function ;
  1936. isrMode[pin] = mode;
  1937. if(waitForInterruptInit (pin, mode)<0) {
  1938. if (wiringPiDebug) {
  1939. fprintf (stderr, "wiringPi: waitForInterruptInit failed\n") ;
  1940. }
  1941. };
  1942. if (wiringPiDebug) {
  1943. printf ("wiringPi: mutex in\n") ;
  1944. }
  1945. pthread_mutex_lock (&pinMutex) ;
  1946. pinPass = pin ;
  1947. if (wiringPiDebug) {
  1948. printf("wiringPi: pthread_create before 0x%lX\n", (unsigned long)isrThreads[pin]);
  1949. }
  1950. if (pthread_create (&isrThreads[pin], NULL, interruptHandler, NULL)==0) {
  1951. if (wiringPiDebug) {
  1952. printf("wiringPi: pthread_create successed, 0x%lX\n", (unsigned long)isrThreads[pin]);
  1953. }
  1954. while (pinPass != -1)
  1955. delay (1) ;
  1956. } else {
  1957. if (wiringPiDebug) {
  1958. printf("wiringPi: pthread_create failed\n");
  1959. }
  1960. }
  1961. if (wiringPiDebug) {
  1962. printf ("wiringPi: mutex out\n") ;
  1963. }
  1964. pthread_mutex_unlock (&pinMutex) ;
  1965. if (wiringPiDebug) {
  1966. printf ("wiringPi: wiringPiISR finished\n") ;
  1967. }
  1968. return 0 ;
  1969. }
  1970. /*
  1971. * initialiseEpoch:
  1972. * Initialise our start-of-time variable to be the current unix
  1973. * time in milliseconds and microseconds.
  1974. *********************************************************************************
  1975. */
  1976. static void initialiseEpoch (void)
  1977. {
  1978. #ifdef OLD_WAY
  1979. struct timeval tv ;
  1980. gettimeofday (&tv, NULL) ;
  1981. epochMilli = (uint64_t)tv.tv_sec * (uint64_t)1000 + (uint64_t)(tv.tv_usec / 1000) ;
  1982. epochMicro = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)(tv.tv_usec) ;
  1983. #else
  1984. struct timespec ts ;
  1985. clock_gettime (CLOCK_MONOTONIC_RAW, &ts) ;
  1986. epochMilli = (uint64_t)ts.tv_sec * (uint64_t)1000 + (uint64_t)(ts.tv_nsec / 1000000L) ;
  1987. epochMicro = (uint64_t)ts.tv_sec * (uint64_t)1000000 + (uint64_t)(ts.tv_nsec / 1000L) ;
  1988. #endif
  1989. }
  1990. /*
  1991. * delay:
  1992. * Wait for some number of milliseconds
  1993. *********************************************************************************
  1994. */
  1995. void delay (unsigned int howLong)
  1996. {
  1997. struct timespec sleeper, dummy ;
  1998. sleeper.tv_sec = (time_t)(howLong / 1000) ;
  1999. sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ;
  2000. nanosleep (&sleeper, &dummy) ;
  2001. }
  2002. /*
  2003. * delayMicroseconds:
  2004. * This is somewhat intersting. It seems that on the Pi, a single call
  2005. * to nanosleep takes some 80 to 130 microseconds anyway, so while
  2006. * obeying the standards (may take longer), it's not always what we
  2007. * want!
  2008. *
  2009. * So what I'll do now is if the delay is less than 100uS we'll do it
  2010. * in a hard loop, watching a built-in counter on the ARM chip. This is
  2011. * somewhat sub-optimal in that it uses 100% CPU, something not an issue
  2012. * in a microcontroller, but under a multi-tasking, multi-user OS, it's
  2013. * wastefull, however we've no real choice )-:
  2014. *
  2015. * Plan B: It seems all might not be well with that plan, so changing it
  2016. * to use gettimeofday () and poll on that instead...
  2017. *********************************************************************************
  2018. */
  2019. void delayMicrosecondsHard (unsigned int howLong)
  2020. {
  2021. struct timeval tNow, tLong, tEnd ;
  2022. gettimeofday (&tNow, NULL) ;
  2023. tLong.tv_sec = howLong / 1000000 ;
  2024. tLong.tv_usec = howLong % 1000000 ;
  2025. timeradd (&tNow, &tLong, &tEnd) ;
  2026. while (timercmp (&tNow, &tEnd, <))
  2027. gettimeofday (&tNow, NULL) ;
  2028. }
  2029. void delayMicroseconds (unsigned int howLong)
  2030. {
  2031. struct timespec sleeper ;
  2032. unsigned int uSecs = howLong % 1000000 ;
  2033. unsigned int wSecs = howLong / 1000000 ;
  2034. /**/ if (howLong == 0)
  2035. return ;
  2036. else if (howLong < 100)
  2037. delayMicrosecondsHard (howLong) ;
  2038. else
  2039. {
  2040. sleeper.tv_sec = wSecs ;
  2041. sleeper.tv_nsec = (long)(uSecs * 1000L) ;
  2042. nanosleep (&sleeper, NULL) ;
  2043. }
  2044. }
  2045. /*
  2046. * millis:
  2047. * Return a number of milliseconds as an unsigned int.
  2048. * Wraps at 49 days.
  2049. *********************************************************************************
  2050. */
  2051. unsigned int millis (void)
  2052. {
  2053. uint64_t now ;
  2054. #ifdef OLD_WAY
  2055. struct timeval tv ;
  2056. gettimeofday (&tv, NULL) ;
  2057. now = (uint64_t)tv.tv_sec * (uint64_t)1000 + (uint64_t)(tv.tv_usec / 1000) ;
  2058. #else
  2059. struct timespec ts ;
  2060. clock_gettime (CLOCK_MONOTONIC_RAW, &ts) ;
  2061. now = (uint64_t)ts.tv_sec * (uint64_t)1000 + (uint64_t)(ts.tv_nsec / 1000000L) ;
  2062. #endif
  2063. return (uint32_t)(now - epochMilli) ;
  2064. }
  2065. /*
  2066. * micros:
  2067. * Return a number of microseconds as an unsigned int.
  2068. * Wraps after 71 minutes.
  2069. *********************************************************************************
  2070. */
  2071. unsigned int micros (void)
  2072. {
  2073. uint64_t now ;
  2074. #ifdef OLD_WAY
  2075. struct timeval tv ;
  2076. gettimeofday (&tv, NULL) ;
  2077. now = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)tv.tv_usec ;
  2078. #else
  2079. struct timespec ts ;
  2080. clock_gettime (CLOCK_MONOTONIC_RAW, &ts) ;
  2081. now = (uint64_t)ts.tv_sec * (uint64_t)1000000 + (uint64_t)(ts.tv_nsec / 1000) ;
  2082. #endif
  2083. return (uint32_t)(now - epochMicro) ;
  2084. }
  2085. /*
  2086. * wiringPiVersion:
  2087. * Return our current version number
  2088. *********************************************************************************
  2089. */
  2090. void wiringPiVersion (int *major, int *minor)
  2091. {
  2092. *major = VERSION_MAJOR ;
  2093. *minor = VERSION_MINOR ;
  2094. }
  2095. int wiringPiUserLevelAccess(void)
  2096. {
  2097. struct stat statBuf ;
  2098. const char* gpiomemModule = gpiomem_BCM;
  2099. if (PI_MODEL_5 == RaspberryPiModel) {
  2100. gpiomemModule = gpiomem_RP1;
  2101. }
  2102. return stat(gpiomemModule, &statBuf) == 0 ? 1 : 0;
  2103. }
  2104. /*
  2105. * wiringPiSetup:
  2106. * Must be called once at the start of your program execution.
  2107. *
  2108. * Default setup: Initialises the system into wiringPi Pin mode and uses the
  2109. * memory mapped hardware directly.
  2110. *
  2111. * Changed now to revert to "gpio" mode if we're running on a Compute Module.
  2112. *********************************************************************************
  2113. */
  2114. int wiringPiSetup (void)
  2115. {
  2116. int fd ;
  2117. int model, rev, mem, maker, overVolted ;
  2118. if (wiringPiSetuped)
  2119. return 0 ;
  2120. wiringPiSetuped = TRUE ;
  2121. if (getenv (ENV_DEBUG) != NULL)
  2122. wiringPiDebug = TRUE ;
  2123. if (getenv (ENV_CODES) != NULL)
  2124. wiringPiReturnCodes = TRUE ;
  2125. if (wiringPiDebug)
  2126. printf ("wiringPi: wiringPiSetup called\n") ;
  2127. // Get the board ID information. We're not really using the information here,
  2128. // but it will give us information like the GPIO layout scheme (2 variants
  2129. // on the older 26-pin Pi's) and the GPIO peripheral base address.
  2130. // and if we're running on a compute module, then wiringPi pin numbers
  2131. // don't really mean anything, so force native BCM mode anyway.
  2132. piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
  2133. if ((model == PI_MODEL_CM) ||
  2134. (model == PI_MODEL_CM3) ||
  2135. (model == PI_MODEL_CM3P))
  2136. wiringPiMode = WPI_MODE_GPIO ;
  2137. else
  2138. wiringPiMode = WPI_MODE_PINS ;
  2139. /**/ if (piGpioLayout () == GPIO_LAYOUT_PI1_REV1) // A, B, Rev 1, 1.1
  2140. {
  2141. pinToGpio = pinToGpioR1 ;
  2142. physToGpio = physToGpioR1 ;
  2143. }
  2144. else // A2, B2, A+, B+, CM, Pi2, Pi3, Zero, Zero W, Zero 2 W
  2145. {
  2146. pinToGpio = pinToGpioR2 ;
  2147. physToGpio = physToGpioR2 ;
  2148. }
  2149. // ...
  2150. switch (model)
  2151. {
  2152. case PI_MODEL_A:
  2153. case PI_MODEL_B:
  2154. case PI_MODEL_AP:
  2155. case PI_MODEL_BP:
  2156. case PI_ALPHA:
  2157. case PI_MODEL_CM:
  2158. case PI_MODEL_ZERO:
  2159. case PI_MODEL_ZERO_W:
  2160. piGpioBase = GPIO_PERI_BASE_OLD ;
  2161. piGpioPupOffset = GPPUD ;
  2162. break ;
  2163. case PI_MODEL_4B:
  2164. case PI_MODEL_400:
  2165. case PI_MODEL_CM4:
  2166. piGpioBase = GPIO_PERI_BASE_2711 ;
  2167. piGpioPupOffset = GPPUPPDN0 ;
  2168. break ;
  2169. case PI_MODEL_5:
  2170. piGpioBase = GPIO_PERI_BASE_2712 ;
  2171. piGpioPupOffset = 0 ;
  2172. break ;
  2173. default:
  2174. piGpioBase = GPIO_PERI_BASE_2835 ;
  2175. piGpioPupOffset = GPPUD ;
  2176. break ;
  2177. }
  2178. // Open the master /dev/ memory control device
  2179. // Device strategy: December 2016:
  2180. // Try /dev/mem. If that fails, then
  2181. // try /dev/gpiomem. If that fails then game over.
  2182. const char* gpiomemGlobal = gpiomem_global;
  2183. const char* gpiomemModule = gpiomem_BCM;
  2184. if (PI_MODEL_5 == model) {
  2185. gpiomemGlobal = pciemem_RP1;
  2186. gpiomemModule = gpiomem_RP1;
  2187. }
  2188. if (gpiomemGlobal==NULL || (fd = open (gpiomemGlobal, O_RDWR | O_SYNC | O_CLOEXEC)) < 0)
  2189. {
  2190. if (gpiomemModule && (fd = open (gpiomemModule, O_RDWR | O_SYNC | O_CLOEXEC) ) >= 0) // We're using gpiomem
  2191. {
  2192. piGpioBase = 0 ;
  2193. usingGpioMem = TRUE ;
  2194. }
  2195. else
  2196. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: Unable to open %s or %s: %s.\n"
  2197. " Aborting your program because if it can not access the GPIO\n"
  2198. " hardware then it most certianly won't work\n"
  2199. " Try running with sudo?\n", gpiomemGlobal, gpiomemModule, strerror (errno)) ;
  2200. }
  2201. if (wiringPiDebug) {
  2202. printf ("wiringPi: access to %s succeded\n", usingGpioMem ? gpiomemModule : gpiomemGlobal) ;
  2203. }
  2204. // GPIO:
  2205. if (PI_MODEL_5 != model) {
  2206. //Set the offsets into the memory interface.
  2207. GPIO_PADS = piGpioBase + 0x00100000 ;
  2208. GPIO_CLOCK_BASE = piGpioBase + 0x00101000 ;
  2209. GPIO_BASE = piGpioBase + 0x00200000 ;
  2210. GPIO_TIMER = piGpioBase + 0x0000B000 ;
  2211. GPIO_PWM = piGpioBase + 0x0020C000 ;
  2212. GPIO_RIO = 0x00 ;
  2213. // Map the individual hardware components
  2214. // GPIO:
  2215. base = NULL;
  2216. gpio = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_BASE) ;
  2217. if (gpio == MAP_FAILED)
  2218. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (GPIO) failed: %s\n", strerror (errno)) ;
  2219. // PWM
  2220. pwm = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PWM) ;
  2221. if (pwm == MAP_FAILED)
  2222. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PWM) failed: %s\n", strerror (errno)) ;
  2223. // Clock control (needed for PWM)
  2224. clk = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_CLOCK_BASE) ;
  2225. if (clk == MAP_FAILED)
  2226. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (CLOCK) failed: %s\n", strerror (errno)) ;
  2227. // The drive pads
  2228. pads = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PADS) ;
  2229. if (pads == MAP_FAILED)
  2230. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PADS) failed: %s\n", strerror (errno)) ;
  2231. // The system timer
  2232. timer = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_TIMER) ;
  2233. if (timer == MAP_FAILED)
  2234. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (TIMER) failed: %s\n", strerror (errno)) ;
  2235. // Set the timer to free-running, 1MHz.
  2236. // 0xF9 is 249, the timer divide is base clock / (divide+1)
  2237. // so base clock is 250MHz / 250 = 1MHz.
  2238. *(timer + TIMER_CONTROL) = 0x0000280 ;
  2239. *(timer + TIMER_PRE_DIV) = 0x00000F9 ;
  2240. timerIrqRaw = timer + TIMER_IRQ_RAW ;
  2241. // Export the base addresses for any external software that might need them
  2242. _wiringPiBase = base ;
  2243. _wiringPiGpio = gpio ;
  2244. _wiringPiPwm = pwm ;
  2245. _wiringPiClk = clk ;
  2246. _wiringPiPads = pads ;
  2247. _wiringPiTimer = timer ;
  2248. _wiringPiRio = NULL ;
  2249. } else {
  2250. unsigned int MMAP_size = (usingGpioMem) ? gpiomem_RP1_Size : pciemem_RP1_Size;
  2251. GPIO_PADS = (RP1_PADS0_Addr-RP1_IO0_Addr) ;
  2252. GPIO_CLOCK_BASE = 0x00;
  2253. GPIO_BASE = (RP1_IO0_Addr-RP1_BASE_Addr) ;
  2254. GPIO_TIMER = 0x00;
  2255. GPIO_PWM = RP1_PWM0_Addr-RP1_BASE_Addr;
  2256. GPIO_RIO = (RP1_SYS_RIO0_Addr-RP1_IO0_Addr) ;
  2257. //map hole RP1 memory block from beginning,
  2258. base = (unsigned int *)mmap(0, MMAP_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x00000000) ;
  2259. if (base == MAP_FAILED)
  2260. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap failed: %s\n", strerror (errno)) ;
  2261. if (usingGpioMem) {
  2262. gpio = base; // RP1 start adress of map memory for gpio (same as module memory)
  2263. } else {
  2264. gpio = &base[GPIO_BASE/4];// RP1 start adress of map memory for gpio
  2265. }
  2266. pads = &gpio[GPIO_PADS/4]; // RP1 start adress of map memory for pads
  2267. rio = &gpio[GPIO_RIO/4]; // RP1 start adress of map memory for rio
  2268. GPIO_PADS += GPIO_BASE;
  2269. GPIO_RIO += GPIO_BASE;
  2270. // Export the base addresses for any external software that might need them
  2271. _wiringPiBase = base ;
  2272. _wiringPiGpio = gpio ;
  2273. _wiringPiPwm = NULL ;
  2274. _wiringPiClk = NULL ;
  2275. _wiringPiPads = pads ;
  2276. _wiringPiTimer = NULL ;
  2277. _wiringPiRio = rio ;
  2278. }
  2279. if (wiringPiDebug) {
  2280. printf ("wiringPi: memory map gpio 0x%x %s\n", GPIO_BASE , _wiringPiGpio ? "valid" : "invalid");
  2281. printf ("wiringPi: memory map pads 0x%x %s\n", GPIO_PADS , _wiringPiPads ? "valid" : "invalid");
  2282. printf ("wiringPi: memory map rio 0x%x %s\n", GPIO_RIO , _wiringPiRio ? "valid" : "invalid");
  2283. printf ("wiringPi: memory map pwm 0x%x %s\n", GPIO_PWM , _wiringPiPwm ? "valid" : "invalid");
  2284. printf ("wiringPi: memory map clk 0x%x %s\n", GPIO_CLOCK_BASE, _wiringPiClk ? "valid" : "invalid");
  2285. printf ("wiringPi: memory map timer 0x%x %s\n", GPIO_TIMER,_wiringPiTimer ? "valid" : "invalid");
  2286. }
  2287. initialiseEpoch () ;
  2288. return 0 ;
  2289. }
  2290. /*
  2291. * wiringPiSetupGpio:
  2292. * Must be called once at the start of your program execution.
  2293. *
  2294. * GPIO setup: Initialises the system into GPIO Pin mode and uses the
  2295. * memory mapped hardware directly.
  2296. *********************************************************************************
  2297. */
  2298. int wiringPiSetupGpio (void)
  2299. {
  2300. (void)wiringPiSetup () ;
  2301. if (wiringPiDebug)
  2302. printf ("wiringPi: wiringPiSetupGpio called\n") ;
  2303. wiringPiMode = WPI_MODE_GPIO ;
  2304. return 0 ;
  2305. }
  2306. /*
  2307. * wiringPiSetupPhys:
  2308. * Must be called once at the start of your program execution.
  2309. *
  2310. * Phys setup: Initialises the system into Physical Pin mode and uses the
  2311. * memory mapped hardware directly.
  2312. *********************************************************************************
  2313. */
  2314. int wiringPiSetupPhys (void)
  2315. {
  2316. (void)wiringPiSetup () ;
  2317. if (wiringPiDebug)
  2318. printf ("wiringPi: wiringPiSetupPhys called\n") ;
  2319. wiringPiMode = WPI_MODE_PHYS ;
  2320. return 0 ;
  2321. }
  2322. /*
  2323. * wiringPiSetupSys:
  2324. * GPIO Sysfs Interface for Userspace is deprecated
  2325. * https://www.kernel.org/doc/html/v5.5/admin-guide/gpio/sysfs.html
  2326. * The last Raspberry Pi Kernel with Sysfs was 6.1.
  2327. * If needed, please use WiringPi 3.1.
  2328. *
  2329. */
  2330. int wiringPiSetupSys (void)
  2331. {
  2332. piFunctionOops("wiringPiSetupSys",
  2333. "use wringpi 3.1 (last version with GPIO Sysfs interface)",
  2334. "https://www.kernel.org/doc/html/v5.5/admin-guide/gpio/sysfs.html");
  2335. return 0 ;
  2336. }