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.

wiringPi.c 93 KiB

12 jaren geleden
8 maanden geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
8 maanden geleden
8 maanden geleden
8 maanden geleden
12 jaren geleden
12 jaren geleden
8 maanden geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
12 jaren geleden
6 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
4 maanden geleden
12 jaren geleden
12 jaren geleden
9 maanden geleden
6 jaren geleden
9 maanden geleden
6 jaren geleden
9 maanden geleden
6 jaren geleden
9 maanden geleden
12 jaren geleden
9 maanden geleden
10 maanden geleden
10 maanden geleden
10 maanden geleden
10 maanden geleden
8 maanden geleden
10 maanden geleden
7 maanden geleden
6 maanden geleden
12 jaren geleden
8 maanden geleden
8 maanden geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
7 maanden geleden
9 maanden geleden
7 maanden geleden
9 maanden geleden
7 maanden geleden
5 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
6 jaren geleden
9 maanden geleden
6 jaren geleden
8 maanden geleden
7 maanden geleden
7 maanden geleden
8 maanden geleden
7 maanden geleden
6 jaren geleden
8 maanden geleden
8 maanden geleden
8 maanden geleden
8 maanden geleden
7 maanden geleden
8 maanden geleden
8 maanden geleden
8 maanden geleden
8 maanden geleden
12 jaren geleden
8 maanden geleden
8 maanden geleden
8 maanden geleden
8 maanden geleden
6 jaren geleden
10 maanden geleden
8 maanden geleden
8 maanden geleden
10 maanden geleden
10 maanden geleden
6 maanden geleden
4 maanden geleden
6 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
12 jaren geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
4 maanden geleden
4 maanden geleden
4 maanden geleden
9 maanden geleden
4 maanden geleden
4 maanden geleden
9 maanden geleden
4 maanden geleden
4 maanden geleden
4 maanden geleden
12 jaren geleden
6 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
9 maanden geleden
12 jaren geleden
12 jaren geleden
4 maanden geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
7 maanden geleden
6 maanden geleden
7 maanden geleden
6 maanden geleden
12 jaren geleden
9 maanden geleden
6 jaren geleden
9 maanden geleden
9 maanden geleden
12 jaren geleden
9 maanden geleden
7 maanden geleden
7 maanden geleden
7 maanden geleden
7 maanden geleden
9 maanden geleden
7 maanden geleden
9 maanden geleden
6 jaren geleden
7 maanden geleden
6 maanden geleden
6 maanden geleden
7 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
6 jaren geleden
9 maanden geleden
6 jaren geleden
6 jaren geleden
12 jaren geleden
7 maanden geleden
7 maanden geleden
7 maanden geleden
7 maanden geleden
7 maanden geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
6 jaren geleden
7 maanden geleden
6 maanden geleden
7 maanden geleden
6 maanden geleden
7 maanden geleden
12 jaren geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
7 maanden geleden
9 maanden geleden
9 maanden geleden
12 jaren geleden
12 jaren geleden
7 maanden geleden
7 maanden geleden
7 maanden geleden
7 maanden geleden
12 jaren geleden
8 maanden geleden
7 maanden geleden
6 maanden geleden
7 maanden geleden
6 maanden geleden
7 maanden geleden
9 maanden geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
7 maanden geleden
7 maanden geleden
7 maanden geleden
7 maanden geleden
12 jaren geleden
12 jaren geleden
7 maanden geleden
6 maanden geleden
7 maanden geleden
6 maanden geleden
7 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
9 maanden geleden
6 jaren geleden
12 jaren geleden
4 maanden geleden
6 jaren geleden
4 maanden geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
9 maanden geleden
6 jaren geleden
9 maanden geleden
9 maanden geleden
8 maanden geleden
9 maanden geleden
8 maanden geleden
8 maanden geleden
9 maanden geleden
8 maanden geleden
9 maanden geleden
8 maanden geleden
12 jaren geleden
12 jaren geleden
8 maanden geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
8 maanden geleden
12 jaren geleden
8 maanden geleden
6 maanden geleden
12 jaren geleden
8 maanden geleden
12 jaren geleden
8 maanden geleden
8 maanden geleden
6 maanden geleden
8 maanden geleden
6 maanden geleden
8 maanden geleden
12 jaren geleden
8 maanden geleden
8 maanden geleden
8 maanden geleden
6 maanden geleden
7 maanden geleden
8 maanden geleden
8 maanden geleden
8 maanden geleden
7 maanden geleden
8 maanden geleden
6 maanden geleden
8 maanden geleden
7 maanden geleden
8 maanden geleden
6 maanden geleden
8 maanden geleden
8 maanden geleden
6 maanden geleden
8 maanden geleden
6 maanden geleden
8 maanden geleden
12 jaren geleden
8 maanden geleden
12 jaren geleden
8 maanden geleden
8 maanden geleden
8 maanden geleden
8 maanden geleden
8 maanden geleden
9 maanden geleden
9 maanden geleden
8 maanden geleden
8 maanden geleden
8 maanden geleden
8 maanden geleden
8 maanden geleden
8 maanden geleden
8 maanden geleden
8 maanden geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
4 maanden geleden
12 jaren geleden
8 maanden geleden
7 maanden geleden
8 maanden geleden
7 maanden geleden
7 maanden geleden
7 maanden geleden
7 maanden geleden
7 maanden geleden
6 maanden geleden
7 maanden geleden
6 maanden geleden
7 maanden geleden
12 jaren geleden
12 jaren geleden
6 jaren geleden
6 jaren geleden
12 jaren geleden
8 maanden geleden
8 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
7 maanden geleden
9 maanden geleden
7 maanden geleden
9 maanden geleden
6 jaren geleden
9 maanden geleden
6 jaren geleden
9 maanden geleden
7 maanden geleden
9 maanden geleden
12 jaren geleden
9 maanden geleden
9 maanden geleden
12 jaren geleden
9 maanden geleden
9 maanden geleden
10 maanden geleden
12 jaren geleden
10 maanden geleden
12 jaren geleden
10 maanden geleden
12 jaren geleden
10 maanden geleden
10 maanden geleden
12 jaren geleden
10 maanden geleden
10 maanden geleden
12 jaren geleden
10 maanden geleden
12 jaren geleden
10 maanden geleden
12 jaren geleden
10 maanden geleden
12 jaren geleden
10 maanden geleden
12 jaren geleden
9 maanden geleden
9 maanden geleden
10 maanden geleden
9 maanden geleden
6 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
9 maanden geleden
10 maanden geleden
9 maanden geleden
10 maanden geleden
9 maanden geleden
9 maanden geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
6 maanden geleden
6 maanden geleden
7 maanden geleden
6 maanden geleden
7 maanden geleden
6 maanden geleden
7 maanden geleden
6 maanden geleden
7 maanden geleden
12 jaren geleden
8 maanden geleden
7 maanden geleden
12 jaren geleden
7 maanden geleden
6 maanden geleden
12 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177
  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_IRQRESET = 0x10000000; //CTRL Bit 28
  137. const unsigned int RP1_PAD_DEFAULT_0TO8 = (0x0B | 0x70); //Slewfast, Schmitt, PullUp, | 12mA, Input enable
  138. const unsigned int RP1_PAD_DEFAULT_FROM9 = (0x07 | 0x70); //Slewfast, Schmitt, PullDown, | 12mA, Input enable
  139. const unsigned int RP1_PAD_IC_DEFAULT_0TO8 = 0x9A; //pull-up, Schmitt
  140. const unsigned int RP1_PAD_IC_DEFAULT_FROM9 = 0x96; //pull-down, Schmitt
  141. const unsigned int RP1_PAD_DRIVE_MASK = 0x00000030;
  142. const unsigned int RP1_INV_PAD_DRIVE_MASK = ~(RP1_PAD_DRIVE_MASK);
  143. //RP1 chip (@Pi5) address
  144. const unsigned long long RP1_64_BASE_Addr = 0x1f000d0000;
  145. const unsigned int RP1_BASE_Addr = 0x40000000;
  146. const unsigned int RP1_PWM0_Addr = 0x40098000; // Adress is not mapped to gpiomem device!
  147. const unsigned int RP1_IO0_Addr = 0x400d0000;
  148. const unsigned int RP1_SYS_RIO0_Addr = 0x400e0000;
  149. const unsigned int RP1_PADS0_Addr = 0x400f0000;
  150. // Access from ARM Running Linux
  151. // Taken from Gert/Doms code. Some of this is not in the manual
  152. // that I can find )-:
  153. //
  154. // Updates in September 2015 - all now static variables (and apologies for the caps)
  155. // due to the Pi v2, v3, etc. and the new /dev/gpiomem interface
  156. const char* gpiomem_global = "/dev/mem";
  157. const char* gpiomem_BCM = "/dev/gpiomem";
  158. const char* gpiomem_RP1 = "/dev/gpiomem0";
  159. const int gpiomem_RP1_Size = 0x00030000;
  160. // PCIe Memory access, static define - maybe needed to detect in future
  161. //dmesg: rp1 0000:01:00.0: bar1 len 0x400000, start 0x1f00000000, end 0x1f003fffff, flags, 0x40200
  162. const char* pciemem_RP1_path = "/sys/bus/pci/devices/0000:01:00.0";
  163. const char* pciemem_RP1 = "/sys/bus/pci/devices/0000:01:00.0/resource1";
  164. const int pciemem_RP1_Size = 0x00400000;
  165. const unsigned short pciemem_RP1_Ventor= 0x1de4;
  166. const unsigned short pciemem_RP1_Device= 0x0001;
  167. static volatile unsigned int GPIO_PADS ;
  168. static volatile unsigned int GPIO_CLOCK_BASE ;
  169. static volatile unsigned int GPIO_BASE ;
  170. static volatile unsigned int GPIO_TIMER ;
  171. static volatile unsigned int GPIO_PWM ;
  172. static volatile unsigned int GPIO_RIO ;
  173. #define PAGE_SIZE (4*1024)
  174. #define BLOCK_SIZE (4*1024)
  175. static unsigned int usingGpioMem = FALSE ;
  176. static int wiringPiSetuped = FALSE ;
  177. // PWM
  178. // Word offsets into the PWM control region
  179. #define PWM_CONTROL 0
  180. #define PWM_STATUS 1
  181. #define PWM0_RANGE 4
  182. #define PWM0_DATA 5
  183. #define PWM1_RANGE 8
  184. #define PWM1_DATA 9
  185. // Clock regsiter offsets
  186. #define PWMCLK_CNTL 40
  187. #define PWMCLK_DIV 41
  188. #define PWM0_MS_MODE 0x0080 // Run in MS mode
  189. #define PWM0_USEFIFO 0x0020 // Data from FIFO
  190. #define PWM0_REVPOLAR 0x0010 // Reverse polarity
  191. #define PWM0_OFFSTATE 0x0008 // Ouput Off state
  192. #define PWM0_REPEATFF 0x0004 // Repeat last value if FIFO empty
  193. #define PWM0_SERIAL 0x0002 // Run in serial mode
  194. #define PWM0_ENABLE 0x0001 // Channel Enable
  195. #define PWM1_MS_MODE 0x8000 // Run in MS mode
  196. #define PWM1_USEFIFO 0x2000 // Data from FIFO
  197. #define PWM1_REVPOLAR 0x1000 // Reverse polarity
  198. #define PWM1_OFFSTATE 0x0800 // Ouput Off state
  199. #define PWM1_REPEATFF 0x0400 // Repeat last value if FIFO empty
  200. #define PWM1_SERIAL 0x0200 // Run in serial mode
  201. #define PWM1_ENABLE 0x0100 // Channel Enable
  202. const int PWMCLK_DIVI_MAX = 0xFFF; // 3 Byte max size for Clock devider
  203. const int OSC_FREQ_DEFAULT = 192; // x100kHz OSC
  204. const int OSC_FREQ_BCM2711 = 540; // x100kHz OSC
  205. // Timer
  206. // Word offsets
  207. #define TIMER_LOAD (0x400 >> 2)
  208. #define TIMER_VALUE (0x404 >> 2)
  209. #define TIMER_CONTROL (0x408 >> 2)
  210. #define TIMER_IRQ_CLR (0x40C >> 2)
  211. #define TIMER_IRQ_RAW (0x410 >> 2)
  212. #define TIMER_IRQ_MASK (0x414 >> 2)
  213. #define TIMER_RELOAD (0x418 >> 2)
  214. #define TIMER_PRE_DIV (0x41C >> 2)
  215. #define TIMER_COUNTER (0x420 >> 2)
  216. // Locals to hold pointers to the hardware
  217. static volatile unsigned int *base ;
  218. static volatile unsigned int *gpio ;
  219. static volatile unsigned int *pwm ;
  220. static volatile unsigned int *clk ;
  221. static volatile unsigned int *pads ;
  222. static volatile unsigned int *timer ;
  223. static volatile unsigned int *timerIrqRaw ;
  224. static volatile unsigned int *rio ;
  225. // Export variables for the hardware pointers
  226. volatile unsigned int *_wiringPiBase ;
  227. volatile unsigned int *_wiringPiGpio ;
  228. volatile unsigned int *_wiringPiPwm ;
  229. volatile unsigned int *_wiringPiClk ;
  230. volatile unsigned int *_wiringPiPads ;
  231. volatile unsigned int *_wiringPiTimer ;
  232. volatile unsigned int *_wiringPiTimerIrqRaw ;
  233. volatile unsigned int *_wiringPiRio ;
  234. // Data for use with the boardId functions.
  235. // The order of entries here to correspond with the PI_MODEL_X
  236. // and PI_VERSION_X defines in wiringPi.h
  237. // Only intended for the gpio command - use at your own risk!
  238. // piGpioBase:
  239. // The base address of the GPIO memory mapped hardware IO
  240. #define GPIO_PERI_BASE_OLD 0x20000000
  241. #define GPIO_PERI_BASE_2835 0x3F000000
  242. #define GPIO_PERI_BASE_2711 0xFE000000
  243. #define GPIO_PERI_BASE_2712 0x00 //unknown - 32-bit mapped global mem access not supported for now
  244. static volatile unsigned int piGpioBase = 0 ;
  245. const char *piModelNames [24] =
  246. {
  247. "Model A", // 0
  248. "Model B", // 1
  249. "Model A+", // 2
  250. "Model B+", // 3
  251. "Pi 2", // 4
  252. "Alpha", // 5
  253. "CM", // 6
  254. "Unknown07", // 07
  255. "Pi 3", // 08
  256. "Pi Zero", // 09
  257. "CM3", // 10
  258. "Unknown11", // 11
  259. "Pi Zero-W", // 12
  260. "Pi 3B+", // 13
  261. "Pi 3A+", // 14
  262. "Unknown15", // 15
  263. "CM3+", // 16
  264. "Pi 4B", // 17
  265. "Pi Zero2-W", // 18
  266. "Pi 400", // 19
  267. "CM4", // 20
  268. "CM4S", // 21
  269. "Unknown22", // 22
  270. "Pi 5", // 23
  271. } ;
  272. const char *piProcessor [5] =
  273. {
  274. "BCM2835",
  275. "BCM2836",
  276. "BCM2837",
  277. "BCM2711",
  278. "BCM2712",
  279. } ;
  280. const char *piRevisionNames [16] =
  281. {
  282. "00",
  283. "01",
  284. "02",
  285. "03",
  286. "04",
  287. "05",
  288. "06",
  289. "07",
  290. "08",
  291. "09",
  292. "10",
  293. "11",
  294. "12",
  295. "13",
  296. "14",
  297. "15",
  298. } ;
  299. const char *piMakerNames [16] =
  300. {
  301. "Sony", // 0
  302. "Egoman", // 1
  303. "Embest", // 2
  304. "Unknown",// 3
  305. "Embest", // 4
  306. "Stadium",// 5
  307. "Unknown06", // 6
  308. "Unknown07", // 7
  309. "Unknown08", // 8
  310. "Unknown09", // 9
  311. "Unknown10", // 10
  312. "Unknown11", // 11
  313. "Unknown12", // 12
  314. "Unknown13", // 13
  315. "Unknown14", // 14
  316. "Unknown15", // 15
  317. } ;
  318. const int piMemorySize [8] =
  319. {
  320. 256, // 0
  321. 512, // 1
  322. 1024, // 2
  323. 2048, // 3
  324. 4096, // 4
  325. 8192, // 5
  326. 0, // 6
  327. 0, // 7
  328. } ;
  329. // Time for easy calculations
  330. static uint64_t epochMilli, epochMicro ;
  331. // Misc
  332. static int wiringPiMode = WPI_MODE_UNINITIALISED ;
  333. static volatile int pinPass = -1 ;
  334. static pthread_mutex_t pinMutex ;
  335. static int RaspberryPiModel = -1;
  336. static int RaspberryPiLayout = -1;
  337. // Debugging & Return codes
  338. int wiringPiDebug = FALSE ;
  339. int wiringPiReturnCodes = FALSE ;
  340. // Use /dev/gpiomem ?
  341. int wiringPiTryGpioMem = FALSE ;
  342. static unsigned int lineFlags [64] =
  343. {
  344. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  345. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  346. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  347. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  348. } ;
  349. static int lineFds [64] =
  350. {
  351. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  352. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  353. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  354. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  355. } ;
  356. static int isrFds [64] =
  357. {
  358. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  359. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  360. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  361. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  362. } ;
  363. // ISR Data
  364. static int chipFd = -1;
  365. static void (*isrFunctions [64])(void) ;
  366. static pthread_t isrThreads[64];
  367. static int isrMode[64];
  368. // Doing it the Arduino way with lookup tables...
  369. // Yes, it's probably more innefficient than all the bit-twidling, but it
  370. // does tend to make it all a bit clearer. At least to me!
  371. // pinToGpio:
  372. // Take a Wiring pin (0 through X) and re-map it to the BCM_GPIO pin
  373. // Cope for 3 different board revisions here.
  374. static int *pinToGpio ;
  375. // Revision 1, 1.1:
  376. static int pinToGpioR1 [64] =
  377. {
  378. 17, 18, 21, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7: wpi 0 - 7
  379. 0, 1, // I2C - SDA1, SCL1 wpi 8 - 9
  380. 8, 7, // SPI - CE1, CE0 wpi 10 - 11
  381. 10, 9, 11, // SPI - MOSI, MISO, SCLK wpi 12 - 14
  382. 14, 15, // UART - Tx, Rx wpi 15 - 16
  383. // Padding:
  384. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 31
  385. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
  386. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
  387. } ;
  388. // Revision 2:
  389. static int pinToGpioR2 [64] =
  390. {
  391. 17, 18, 27, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7: wpi 0 - 7
  392. 2, 3, // I2C - SDA0, SCL0 wpi 8 - 9
  393. 8, 7, // SPI - CE1, CE0 wpi 10 - 11
  394. 10, 9, 11, // SPI - MOSI, MISO, SCLK wpi 12 - 14
  395. 14, 15, // UART - Tx, Rx wpi 15 - 16
  396. 28, 29, 30, 31, // Rev 2: New GPIOs 8 though 11 wpi 17 - 20
  397. 5, 6, 13, 19, 26, // B+ wpi 21, 22, 23, 24, 25
  398. 12, 16, 20, 21, // B+ wpi 26, 27, 28, 29
  399. 0, 1, // B+ wpi 30, 31
  400. // Padding:
  401. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
  402. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
  403. } ;
  404. // physToGpio:
  405. // Take a physical pin (1 through 26) and re-map it to the BCM_GPIO pin
  406. // Cope for 2 different board revisions here.
  407. // Also add in the P5 connector, so the P5 pins are 3,4,5,6, so 53,54,55,56
  408. static int *physToGpio ;
  409. static int physToGpioR1 [64] =
  410. {
  411. -1, // 0
  412. -1, -1, // 1, 2
  413. 0, -1,
  414. 1, -1,
  415. 4, 14,
  416. -1, 15,
  417. 17, 18,
  418. 21, -1,
  419. 22, 23,
  420. -1, 24,
  421. 10, -1,
  422. 9, 25,
  423. 11, 8,
  424. -1, 7, // 25, 26
  425. -1, -1, -1, -1, -1, // ... 31
  426. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
  427. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
  428. } ;
  429. static int physToGpioR2 [64] =
  430. {
  431. -1, // 0
  432. -1, -1, // 1, 2
  433. 2, -1,
  434. 3, -1,
  435. 4, 14,
  436. -1, 15,
  437. 17, 18,
  438. 27, -1,
  439. 22, 23,
  440. -1, 24,
  441. 10, -1,
  442. 9, 25,
  443. 11, 8,
  444. -1, 7, // 25, 26
  445. // B+
  446. 0, 1,
  447. 5, -1,
  448. 6, 12,
  449. 13, -1,
  450. 19, 16,
  451. 26, 20,
  452. -1, 21,
  453. // the P5 connector on the Rev 2 boards:
  454. -1, -1,
  455. -1, -1,
  456. -1, -1,
  457. -1, -1,
  458. -1, -1,
  459. 28, 29,
  460. 30, 31,
  461. -1, -1,
  462. -1, -1,
  463. -1, -1,
  464. -1, -1,
  465. } ;
  466. int piBoard() {
  467. if (RaspberryPiModel<0) { //need to detect pi model
  468. int model, rev, mem, maker, overVolted;
  469. piBoardId (&model, &rev, &mem, &maker, &overVolted);
  470. }
  471. return RaspberryPiModel<0 ? 0 : 1;
  472. }
  473. int piBoard40Pin() {
  474. if (!piBoard()){
  475. // Board not detected
  476. return -1;
  477. }
  478. switch(RaspberryPiModel){
  479. case PI_MODEL_A:
  480. case PI_MODEL_B:
  481. return 0;
  482. // PI_MODEL_CM
  483. // PI_MODEL_CM3
  484. // PI_MODEL_CM4
  485. // PI_MODEL_CM4S
  486. // ? guess yes
  487. default:
  488. return 1;
  489. }
  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. void ReportDeviceError(const char *function, int pin, const char *mode, int ret) {
  733. fprintf(stderr, "wiringPi: ERROR: ioctl %s of %d (%s) returned error '%s' (%d)\n", function, pin, mode, strerror(errno), ret);
  734. }
  735. /*
  736. * piGpioLayout:
  737. * Return a number representing the hardware revision of the board.
  738. * This is not strictly the board revision but is used to check the
  739. * layout of the GPIO connector - and there are 2 types that we are
  740. * really interested in here. The very earliest Pi's and the
  741. * ones that came after that which switched some pins ....
  742. *
  743. * Revision 1 really means the early Model A and B's.
  744. * Revision 2 is everything else - it covers the B, B+ and CM.
  745. * ... and the Pi 2 - which is a B+ ++ ...
  746. * ... and the Pi 0 - which is an A+ ...
  747. *
  748. * The main difference between the revision 1 and 2 system that I use here
  749. * is the mapping of the GPIO pins. From revision 2, the Pi Foundation changed
  750. * 3 GPIO pins on the (original) 26-way header - BCM_GPIO 22 was dropped and
  751. * replaced with 27, and 0 + 1 - I2C bus 0 was changed to 2 + 3; I2C bus 1.
  752. *
  753. * Additionally, here we set the piModel2 flag too. This is again, nothing to
  754. * do with the actual model, but the major version numbers - the GPIO base
  755. * hardware address changed at model 2 and above (not the Zero though)
  756. *
  757. *********************************************************************************
  758. */
  759. const char* revfile = "/proc/device-tree/system/linux,revision";
  760. void piGpioLayoutOops (const char *why)
  761. {
  762. fprintf (stderr, "Oops: Unable to determine Raspberry Pi board revision from %s and from /proc/cpuinfo\n", revfile) ;
  763. PrintSystemStdErr();
  764. fprintf (stderr, " -> %s\n", why) ;
  765. fprintf (stderr, " -> WiringPi is designed for Raspberry Pi and can only be used with a Raspberry Pi.\n\n") ;
  766. fprintf (stderr, " -> Check at https://github.com/wiringpi/wiringpi/issues.\n\n") ;
  767. exit (EXIT_FAILURE) ;
  768. }
  769. int piGpioLayout (void)
  770. {
  771. piBoard();
  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. switch (RaspberryPiModel){
  952. case PI_MODEL_A:
  953. case PI_MODEL_B:
  954. case PI_MODEL_AP:
  955. case PI_MODEL_BP:
  956. case PI_ALPHA:
  957. case PI_MODEL_CM:
  958. case PI_MODEL_ZERO:
  959. case PI_MODEL_ZERO_W:
  960. piGpioBase = GPIO_PERI_BASE_OLD ;
  961. piGpioPupOffset = GPPUD ;
  962. break ;
  963. case PI_MODEL_4B:
  964. case PI_MODEL_400:
  965. case PI_MODEL_CM4:
  966. case PI_MODEL_CM4S:
  967. piGpioBase = GPIO_PERI_BASE_2711 ;
  968. piGpioPupOffset = GPPUPPDN0 ;
  969. break ;
  970. case PI_MODEL_5:
  971. piGpioBase = GPIO_PERI_BASE_2712 ;
  972. piGpioPupOffset = 0 ;
  973. break ;
  974. default:
  975. piGpioBase = GPIO_PERI_BASE_2835 ;
  976. piGpioPupOffset = GPPUD ;
  977. break ;
  978. }
  979. }
  980. /*
  981. * wpiPinToGpio:
  982. * Translate a wiringPi Pin number to native GPIO pin number.
  983. * Provided for external support.
  984. *********************************************************************************
  985. */
  986. int wpiPinToGpio (int wpiPin)
  987. {
  988. return pinToGpio [wpiPin & 63] ;
  989. }
  990. /*
  991. * physPinToGpio:
  992. * Translate a physical Pin number to native GPIO pin number.
  993. * Provided for external support.
  994. *********************************************************************************
  995. */
  996. int physPinToGpio (int physPin)
  997. {
  998. return physToGpio [physPin & 63] ;
  999. }
  1000. /*
  1001. * setPadDrive:
  1002. * Set the PAD driver value
  1003. *********************************************************************************
  1004. */
  1005. void setPadDrivePin (int pin, int value) {
  1006. if (PI_MODEL_5 != RaspberryPiModel) return;
  1007. if (pin < 0 || pin > GetMaxPin()) return ;
  1008. uint32_t wrVal;
  1009. value = value & 3; // 0-3 supported
  1010. wrVal = (value << 4); //Drive strength 0-3
  1011. pads[1+pin] = (pads[1+pin] & RP1_INV_PAD_DRIVE_MASK) | wrVal;
  1012. if (wiringPiDebug) {
  1013. printf ("setPadDrivePin: pin: %d, value: %d (%08X)\n", pin, value, pads[1+pin]) ;
  1014. }
  1015. }
  1016. void setPadDrive (int group, int value)
  1017. {
  1018. uint32_t wrVal, rdVal;
  1019. if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
  1020. {
  1021. value = value & 7; // 0-7 supported
  1022. if (PI_MODEL_5 == RaspberryPiModel) {
  1023. if (-1==group) {
  1024. printf ("Pad register:\n");
  1025. for (int pin=0, maxpin=GetMaxPin(); pin<=maxpin; ++pin) {
  1026. unsigned int drive = (pads[1+pin] & RP1_PAD_DRIVE_MASK)>>4;
  1027. printf (" Pin %2d: 0x%08X drive: 0x%d = %2dmA\n", pin, pads[1+pin], drive, 0==drive ? 2 : drive*4) ;
  1028. }
  1029. }
  1030. if (group !=0) { // only GPIO range @RP1
  1031. return ;
  1032. }
  1033. switch(value) {
  1034. default:
  1035. /* bcm*/ // RP1
  1036. case 0: /* 2mA*/ value=0; break; // 2mA
  1037. case 1: /* 4mA*/
  1038. case 2: /* 6mA*/ value=1; break; // 4mA
  1039. case 3: /* 8mA*/
  1040. case 4: /*10mA*/ value=2; break; // 8mA
  1041. case 5: /*12mA*/
  1042. case 6: /*14mA*/
  1043. case 7: /*16mA*/ value=3; break; //12mA
  1044. }
  1045. wrVal = (value << 4); //Drive strength 0-3
  1046. //set for all pins even when it's avaiable for each pin separately
  1047. for (int pin=0, maxpin=GetMaxPin(); pin<=maxpin; ++pin) {
  1048. pads[1+pin] = (pads[1+pin] & RP1_INV_PAD_DRIVE_MASK) | wrVal;
  1049. }
  1050. rdVal = pads[1+17]; // only pin 17 readback, for logging
  1051. } else {
  1052. if (-1==group) {
  1053. 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)) ;
  1054. }
  1055. if ((group < 0) || (group > 2))
  1056. return ;
  1057. wrVal = BCM_PASSWORD | 0x18 | value; //Drive strength 0-7
  1058. *(pads + group + 11) = wrVal ;
  1059. rdVal = *(pads + group + 11);
  1060. }
  1061. if (wiringPiDebug)
  1062. {
  1063. printf ("setPadDrive: Group: %d, value: %d (%08X)\n", group, value, wrVal) ;
  1064. printf ("Read : %08X\n", rdVal) ;
  1065. }
  1066. }
  1067. }
  1068. /*
  1069. * getAlt:
  1070. * Returns the ALT bits for a given port. Only really of-use
  1071. * for the gpio readall command (I think)
  1072. *********************************************************************************
  1073. */
  1074. int getAlt (int pin)
  1075. {
  1076. int fSel, shift, alt ;
  1077. pin &= 63 ;
  1078. /**/ if (wiringPiMode == WPI_MODE_PINS)
  1079. pin = pinToGpio [pin] ;
  1080. else if (wiringPiMode == WPI_MODE_PHYS)
  1081. pin = physToGpio [pin] ;
  1082. else if (wiringPiMode != WPI_MODE_GPIO)
  1083. return 0 ;
  1084. if (PI_MODEL_5 == RaspberryPiModel) {
  1085. alt = (gpio[2*pin+1] & RP1_FSEL_NONE_HW); //0-4 function
  1086. /*
  1087. BCM:
  1088. 000b = GPIO Pin 9 is an input
  1089. 001b = GPIO Pin 9 is an output
  1090. 100b = GPIO Pin 9 takes alternate function 0
  1091. 101b = GPIO Pin 9 takes alternate function 1
  1092. 110b = GPIO Pin 9 takes alternate function 2
  1093. 111b = GPIO Pin 9 takes alternate function 3
  1094. 011b = GPIO Pin 9 takes alternate function 4
  1095. 010b = GPIO Pin 9 takes alternate function 5
  1096. RP1:
  1097. 8 = alternate function 6
  1098. 9 = alternate function 7
  1099. 10 = alternate function 8
  1100. 11 = alternate function 9
  1101. */
  1102. switch(alt) {
  1103. case 0: return 4;
  1104. case 1: return 5;
  1105. case 2: return 6;
  1106. case 3: return 7;
  1107. case 4: return 3;
  1108. case RP1_FSEL_GPIO: {
  1109. unsigned int outputmask = gpio[2*pin] & 0x3000; //Bit13-OETOPAD + Bit12-OEFROMPERI
  1110. return (outputmask==0x3000) ? 1 : 0; //1=OUT 0=IN
  1111. }
  1112. case 6: return 8;
  1113. case 7: return 9;
  1114. case 8: return 10;
  1115. case 9: return 11;
  1116. default:return alt;
  1117. }
  1118. } else {
  1119. fSel = gpioToGPFSEL [pin] ;
  1120. shift = gpioToShift [pin] ;
  1121. alt = (*(gpio + fSel) >> shift) & 7 ;
  1122. }
  1123. return alt ;
  1124. }
  1125. enum WPIPinAlt getPinModeAlt(int pin) {
  1126. return (enum WPIPinAlt) getAlt(pin);
  1127. }
  1128. /*
  1129. * pwmSetMode:
  1130. * Select the native "balanced" mode, or standard mark:space mode
  1131. *********************************************************************************
  1132. */
  1133. void pwmSetMode (int mode)
  1134. {
  1135. if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
  1136. {
  1137. FailOnModel5();
  1138. if (mode == PWM_MODE_MS) {
  1139. *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE | PWM0_MS_MODE | PWM1_MS_MODE ;
  1140. } else {
  1141. *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ;
  1142. }
  1143. if (wiringPiDebug) {
  1144. printf ("Enable PWM mode: %s. Current register: 0x%08X\n", mode == PWM_MODE_MS ? "mark:space (freq. stable)" : "balanced (freq. change)", *(pwm + PWM_CONTROL));
  1145. }
  1146. }
  1147. }
  1148. /*
  1149. * pwmSetRange:
  1150. * Set the PWM range register. We set both range registers to the same
  1151. * value. If you want different in your own code, then write your own.
  1152. *********************************************************************************
  1153. */
  1154. void pwmSetRange (unsigned int range)
  1155. {
  1156. FailOnModel5();
  1157. if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
  1158. {
  1159. /* would be possible on ms mode but not on bal, deactivated, use pwmc modify instead
  1160. if (piGpioBase == GPIO_PERI_BASE_2711) {
  1161. range = (OSC_FREQ_BCM2711*range)/OSC_FREQ_DEFAULT;
  1162. }
  1163. */
  1164. *(pwm + PWM0_RANGE) = range ; delayMicroseconds (10) ;
  1165. *(pwm + PWM1_RANGE) = range ; delayMicroseconds (10) ;
  1166. if (wiringPiDebug) {
  1167. printf ("PWM range: %u. Current register: 0x%08X\n", range, *(pwm + PWM0_RANGE));
  1168. }
  1169. }
  1170. }
  1171. /*
  1172. * pwmSetClock:
  1173. * Set/Change the PWM clock. Originally my code, but changed
  1174. * (for the better!) by Chris Hall, <chris@kchall.plus.com>
  1175. * after further study of the manual and testing with a 'scope
  1176. *********************************************************************************
  1177. */
  1178. void pwmSetClock (int divisor)
  1179. {
  1180. uint32_t pwm_control ;
  1181. FailOnModel5();
  1182. if (piGpioBase == GPIO_PERI_BASE_2711) {
  1183. //calculate value for OSC 54MHz -> 19.2MHz
  1184. // Pi 4 max divisor is 1456, Pi0-3 is 4095 (0xFFF)
  1185. divisor = (OSC_FREQ_BCM2711*divisor)/OSC_FREQ_DEFAULT;
  1186. }
  1187. if (divisor > PWMCLK_DIVI_MAX) {
  1188. divisor = PWMCLK_DIVI_MAX;
  1189. }
  1190. if (divisor < 1) {
  1191. divisor = 1;
  1192. }
  1193. if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
  1194. {
  1195. if (wiringPiDebug) {
  1196. printf ("PWM clock divisor: Old register: 0x%08X\n", *(clk + PWMCLK_DIV)) ;
  1197. }
  1198. pwm_control = *(pwm + PWM_CONTROL) ; // preserve PWM_CONTROL
  1199. // We need to stop PWM prior to stopping PWM clock in MS mode otherwise BUSY
  1200. // stays high.
  1201. *(pwm + PWM_CONTROL) = 0 ; // Stop PWM
  1202. // Stop PWM clock before changing divisor. The delay after this does need to
  1203. // this big (95uS occasionally fails, 100uS OK), it's almost as though the BUSY
  1204. // flag is not working properly in balanced mode. Without the delay when DIV is
  1205. // adjusted the clock sometimes switches to very slow, once slow further DIV
  1206. // adjustments do nothing and it's difficult to get out of this mode.
  1207. *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x01 ; // Stop PWM Clock
  1208. delayMicroseconds (110) ; // prevents clock going sloooow
  1209. while ((*(clk + PWMCLK_CNTL) & 0x80) != 0) // Wait for clock to be !BUSY
  1210. delayMicroseconds (1) ;
  1211. *(clk + PWMCLK_DIV) = BCM_PASSWORD | (divisor << 12) ;
  1212. *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x11 ; // Start PWM clock
  1213. *(pwm + PWM_CONTROL) = pwm_control ; // restore PWM_CONTROL
  1214. if (wiringPiDebug) {
  1215. printf ("PWM clock divisor %d. Current register: 0x%08X\n", divisor, *(clk + PWMCLK_DIV));
  1216. }
  1217. }
  1218. }
  1219. /*
  1220. * gpioClockSet:
  1221. * Set the frequency on a GPIO clock pin
  1222. *********************************************************************************
  1223. */
  1224. void gpioClockSet (int pin, int freq)
  1225. {
  1226. int divi, divr, divf ;
  1227. FailOnModel5();
  1228. pin &= 63 ;
  1229. /**/ if (wiringPiMode == WPI_MODE_PINS)
  1230. pin = pinToGpio [pin] ;
  1231. else if (wiringPiMode == WPI_MODE_PHYS)
  1232. pin = physToGpio [pin] ;
  1233. else if (wiringPiMode != WPI_MODE_GPIO)
  1234. return ;
  1235. divi = 19200000 / freq ;
  1236. divr = 19200000 % freq ;
  1237. divf = (int)((double)divr * 4096.0 / 19200000.0) ;
  1238. if (divi > PWMCLK_DIVI_MAX) {
  1239. divi = PWMCLK_DIVI_MAX;
  1240. }
  1241. *(clk + gpioToClkCon [pin]) = BCM_PASSWORD | GPIO_CLOCK_SOURCE ; // Stop GPIO Clock
  1242. while ((*(clk + gpioToClkCon [pin]) & 0x80) != 0) // ... and wait
  1243. ;
  1244. *(clk + gpioToClkDiv [pin]) = BCM_PASSWORD | (divi << 12) | divf ; // Set dividers
  1245. *(clk + gpioToClkCon [pin]) = BCM_PASSWORD | 0x10 | GPIO_CLOCK_SOURCE ; // Start Clock
  1246. }
  1247. /*
  1248. * wiringPiFindNode:
  1249. * Locate our device node
  1250. *********************************************************************************
  1251. */
  1252. struct wiringPiNodeStruct *wiringPiFindNode (int pin)
  1253. {
  1254. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1255. while (node != NULL)
  1256. if ((pin >= node->pinBase) && (pin <= node->pinMax))
  1257. return node ;
  1258. else
  1259. node = node->next ;
  1260. return NULL ;
  1261. }
  1262. /*
  1263. * wiringPiNewNode:
  1264. * Create a new GPIO node into the wiringPi handling system
  1265. *********************************************************************************
  1266. */
  1267. static void pinModeDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int mode) { return ; }
  1268. static void pullUpDnControlDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int pud) { return ; }
  1269. //static unsigned int digitalRead8Dummy (UNU struct wiringPiNodeStruct *node, UNU int UNU pin) { return 0 ; }
  1270. //static void digitalWrite8Dummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int value) { return ; }
  1271. static int digitalReadDummy (UNU struct wiringPiNodeStruct *node, UNU int UNU pin) { return LOW ; }
  1272. static void digitalWriteDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int value) { return ; }
  1273. static void pwmWriteDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int value) { return ; }
  1274. static int analogReadDummy (UNU struct wiringPiNodeStruct *node, UNU int pin) { return 0 ; }
  1275. static void analogWriteDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int value) { return ; }
  1276. struct wiringPiNodeStruct *wiringPiNewNode (int pinBase, int numPins)
  1277. {
  1278. int pin ;
  1279. struct wiringPiNodeStruct *node ;
  1280. // Minimum pin base is 64
  1281. if (pinBase < 64)
  1282. (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: pinBase of %d is < 64\n", pinBase) ;
  1283. // Check all pins in-case there is overlap:
  1284. for (pin = pinBase ; pin < (pinBase + numPins) ; ++pin)
  1285. if (wiringPiFindNode (pin) != NULL)
  1286. (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: Pin %d overlaps with existing definition\n", pin) ;
  1287. node = (struct wiringPiNodeStruct *)calloc (sizeof (struct wiringPiNodeStruct), 1) ; // calloc zeros
  1288. if (node == NULL)
  1289. (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: Unable to allocate memory: %s\n", strerror (errno)) ;
  1290. node->pinBase = pinBase ;
  1291. node->pinMax = pinBase + numPins - 1 ;
  1292. node->pinMode = pinModeDummy ;
  1293. node->pullUpDnControl = pullUpDnControlDummy ;
  1294. node->digitalRead = digitalReadDummy ;
  1295. //node->digitalRead8 = digitalRead8Dummy ;
  1296. node->digitalWrite = digitalWriteDummy ;
  1297. //node->digitalWrite8 = digitalWrite8Dummy ;
  1298. node->pwmWrite = pwmWriteDummy ;
  1299. node->analogRead = analogReadDummy ;
  1300. node->analogWrite = analogWriteDummy ;
  1301. node->next = wiringPiNodes ;
  1302. wiringPiNodes = node ;
  1303. return node ;
  1304. }
  1305. #ifdef notYetReady
  1306. /*
  1307. * pinED01:
  1308. * pinED10:
  1309. * Enables edge-detect mode on a pin - from a 0 to a 1 or 1 to 0
  1310. * Pin must already be in input mode with appropriate pull up/downs set.
  1311. *********************************************************************************
  1312. */
  1313. void pinEnableED01Pi (int pin)
  1314. {
  1315. pin = pinToGpio [pin & 63] ;
  1316. }
  1317. #endif
  1318. const char DEV_GPIO_PI[] ="/dev/gpiochip0";
  1319. const char DEV_GPIO_PI5[]="/dev/gpiochip4";
  1320. int wiringPiGpioDeviceGetFd() {
  1321. if (chipFd<0) {
  1322. piBoard();
  1323. const char* gpiochip = PI_MODEL_5 == RaspberryPiModel ? DEV_GPIO_PI5 : DEV_GPIO_PI;
  1324. chipFd = open(gpiochip, O_RDWR);
  1325. if (chipFd < 0) {
  1326. fprintf(stderr, "wiringPi: ERROR: %s open ret=%d\n", gpiochip, chipFd);
  1327. } else if (wiringPiDebug) {
  1328. printf ("wiringPi: Open chip %s succeded, fd=%d\n", gpiochip, chipFd) ;
  1329. }
  1330. }
  1331. return chipFd;
  1332. }
  1333. void releaseLine(int pin) {
  1334. if (wiringPiDebug)
  1335. printf ("releaseLine: pin:%d\n", pin) ;
  1336. lineFlags[pin] = 0;
  1337. close(lineFds[pin]);
  1338. lineFds[pin] = -1;
  1339. }
  1340. int requestLine(int pin, unsigned int lineRequestFlags) {
  1341. struct gpiohandle_request rq;
  1342. if (lineFds[pin]>=0) {
  1343. if (lineRequestFlags == lineFlags[pin]) {
  1344. //already requested
  1345. return lineFds[pin];
  1346. } else {
  1347. //different request -> rerequest
  1348. releaseLine(pin);
  1349. }
  1350. }
  1351. //requested line
  1352. if (wiringPiGpioDeviceGetFd()<0) {
  1353. return -1; // error
  1354. }
  1355. rq.lineoffsets[0] = pin;
  1356. rq.lines = 1;
  1357. rq.flags = lineRequestFlags;
  1358. int ret = ioctl(chipFd, GPIO_GET_LINEHANDLE_IOCTL, &rq);
  1359. if (ret || rq.fd<0) {
  1360. ReportDeviceError("get line handle", pin, "RequestLine", ret);
  1361. return -1; // error
  1362. }
  1363. lineFlags[pin] = lineRequestFlags;
  1364. lineFds[pin] = rq.fd;
  1365. if (wiringPiDebug)
  1366. printf ("requestLine succeeded: pin:%d, flags: %u, fd :%d\n", pin, lineRequestFlags, lineFds[pin]) ;
  1367. return lineFds[pin];
  1368. }
  1369. /*
  1370. *********************************************************************************
  1371. * Core Functions
  1372. *********************************************************************************
  1373. */
  1374. /*
  1375. * pinModeAlt:
  1376. * This is an un-documented special to let you set any pin to any mode
  1377. *********************************************************************************
  1378. */
  1379. void pinModeAlt (int pin, int mode)
  1380. {
  1381. int fSel, shift ;
  1382. RETURN_ON_MODEL5
  1383. setupCheck ("pinModeAlt") ;
  1384. if ((pin & PI_GPIO_MASK) == 0) // On-board pin
  1385. {
  1386. /**/ if (wiringPiMode == WPI_MODE_PINS)
  1387. pin = pinToGpio [pin] ;
  1388. else if (wiringPiMode == WPI_MODE_PHYS)
  1389. pin = physToGpio [pin] ;
  1390. else if (wiringPiMode != WPI_MODE_GPIO)
  1391. return ;
  1392. if (PI_MODEL_5 == RaspberryPiModel) {
  1393. //confusion! diffrent to to BCM! this is taking directly the value for the register
  1394. /*
  1395. "alt0" 0b100
  1396. "alt1" 0b101
  1397. "alt2" 0b110
  1398. "alt3" 0b111
  1399. "alt4" 0b011
  1400. "alt5" 0b010
  1401. */
  1402. gpio[2*pin+1] = (mode & RP1_FSEL_NONE_HW) | RP1_DEBOUNCE_DEFAULT; //0-4 function, 5-11 debounce time
  1403. } else {
  1404. fSel = gpioToGPFSEL [pin] ;
  1405. shift = gpioToShift [pin] ;
  1406. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | ((mode & 0x7) << shift) ;
  1407. }
  1408. }
  1409. }
  1410. /*
  1411. * pinMode:
  1412. * Sets the mode of a pin to be input, output or PWM output
  1413. *********************************************************************************
  1414. */
  1415. //Default: rp1_set_pad(pin, 0, 1, 0, 1, 1, 1, 0);
  1416. void rp1_set_pad(int pin, int slewfast, int schmitt, int pulldown, int pullup, int drive, int inputenable, int outputdisable) {
  1417. pads[1+pin] = (slewfast != 0) | ((schmitt != 0) << 1) | ((pulldown != 0) << 2) | ((pullup != 0) << 3) | ((drive & 0x3) << 4) | ((inputenable != 0) << 6) | ((outputdisable != 0) << 7);
  1418. }
  1419. void pinModeFlagsDevice (int pin, int mode, unsigned int flags) {
  1420. unsigned int lflag = flags;
  1421. if (wiringPiDebug)
  1422. printf ("pinModeFlagsDevice: pin:%d mode:%d, flags: %u\n", pin, mode, flags) ;
  1423. lflag &= ~(GPIOHANDLE_REQUEST_INPUT | GPIOHANDLE_REQUEST_OUTPUT);
  1424. switch(mode) {
  1425. default:
  1426. fprintf(stderr, "pinMode: invalid mode request (only input und output supported)\n");
  1427. return;
  1428. case INPUT:
  1429. lflag |= GPIOHANDLE_REQUEST_INPUT;
  1430. break;
  1431. case OUTPUT:
  1432. lflag |= GPIOHANDLE_REQUEST_OUTPUT;
  1433. break;
  1434. case PM_OFF:
  1435. pinModeFlagsDevice(pin, INPUT, 0);
  1436. releaseLine(pin);
  1437. return;
  1438. }
  1439. requestLine(pin, lflag);
  1440. }
  1441. void pinModeDevice (int pin, int mode) {
  1442. pinModeFlagsDevice(pin, mode, lineFlags[pin]);
  1443. }
  1444. void pinMode (int pin, int mode)
  1445. {
  1446. int fSel, shift, alt ;
  1447. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1448. int origPin = pin ;
  1449. if (wiringPiDebug)
  1450. printf ("pinMode: pin:%d mode:%d\n", pin, mode) ;
  1451. setupCheck ("pinMode") ;
  1452. if ((pin & PI_GPIO_MASK) == 0) // On-board pin
  1453. {
  1454. switch(wiringPiMode) {
  1455. default: //WPI_MODE_GPIO_SYS
  1456. fprintf(stderr, "pinMode: invalid mode\n");
  1457. return;
  1458. case WPI_MODE_PINS:
  1459. pin = pinToGpio [pin];
  1460. break;
  1461. case WPI_MODE_PHYS:
  1462. pin = physToGpio [pin];
  1463. break;
  1464. case WPI_MODE_GPIO_DEVICE_BCM:
  1465. pinModeDevice(pin, mode);
  1466. return;
  1467. case WPI_MODE_GPIO_DEVICE_WPI:
  1468. pinModeDevice(pinToGpio[pin], mode);
  1469. return;
  1470. case WPI_MODE_GPIO_DEVICE_PHYS:
  1471. pinModeDevice(physToGpio[pin], mode);
  1472. return;
  1473. case WPI_MODE_GPIO:
  1474. break;
  1475. }
  1476. if (wiringPiDebug)
  1477. printf ("pinMode: bcm pin:%d mode:%d\n", pin, mode) ;
  1478. softPwmStop (origPin) ;
  1479. softToneStop (origPin) ;
  1480. fSel = gpioToGPFSEL [pin] ;
  1481. shift = gpioToShift [pin] ;
  1482. if (INPUT==mode || PM_OFF==mode) {
  1483. if (PI_MODEL_5 == RaspberryPiModel) {
  1484. if (INPUT==mode) {
  1485. pads[1+pin] = (pin<=8) ? RP1_PAD_DEFAULT_0TO8 : RP1_PAD_DEFAULT_FROM9;
  1486. gpio[2*pin+1] = RP1_FSEL_GPIO | RP1_DEBOUNCE_DEFAULT; // GPIO
  1487. rio[RP1_RIO_OE + RP1_CLR_OFFSET] = 1<<pin; // Input
  1488. } else { //PM_OFF
  1489. pads[1+pin] = (pin<=8) ? RP1_PAD_IC_DEFAULT_0TO8 : RP1_PAD_IC_DEFAULT_FROM9;
  1490. gpio[2*pin+1] = RP1_IRQRESET | RP1_FSEL_NONE_HW | RP1_DEBOUNCE_DEFAULT; // default but with irq reset
  1491. }
  1492. } else {
  1493. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) ; // Sets bits to zero = input
  1494. }
  1495. } else if (mode == OUTPUT) {
  1496. if (PI_MODEL_5 == RaspberryPiModel) {
  1497. pads[1+pin] = (pin<=8) ? RP1_PAD_DEFAULT_0TO8 : RP1_PAD_DEFAULT_FROM9;
  1498. gpio[2*pin+1] = RP1_FSEL_GPIO | RP1_DEBOUNCE_DEFAULT; // GPIO
  1499. rio[RP1_RIO_OE + RP1_SET_OFFSET] = 1<<pin; // Output
  1500. } else {
  1501. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (1 << shift) ;
  1502. }
  1503. } else if (mode == SOFT_PWM_OUTPUT) {
  1504. softPwmCreate (origPin, 0, 100) ;
  1505. } else if (mode == SOFT_TONE_OUTPUT) {
  1506. softToneCreate (origPin) ;
  1507. } else if (mode == PWM_TONE_OUTPUT)
  1508. {
  1509. pinMode (origPin, PWM_OUTPUT) ; // Call myself to enable PWM mode
  1510. pwmSetMode (PWM_MODE_MS) ;
  1511. }
  1512. else if (mode == PWM_OUTPUT)
  1513. {
  1514. RETURN_ON_MODEL5
  1515. if ((alt = gpioToPwmALT [pin]) == 0) // Not a hardware capable PWM pin
  1516. return ;
  1517. usingGpioMemCheck ("pinMode PWM") ;
  1518. // Set pin to PWM mode
  1519. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ;
  1520. delayMicroseconds (110) ; // See comments in pwmSetClockWPi
  1521. pwmSetMode (PWM_MODE_BAL) ; // Pi default mode
  1522. pwmSetRange (1024) ; // Default range of 1024
  1523. pwmSetClock (32) ; // 19.2 / 32 = 600KHz - Also starts the PWM
  1524. }
  1525. else if (mode == GPIO_CLOCK)
  1526. {
  1527. RETURN_ON_MODEL5
  1528. if ((alt = gpioToGpClkALT0 [pin]) == 0) // Not a GPIO_CLOCK pin
  1529. return ;
  1530. usingGpioMemCheck ("pinMode CLOCK") ;
  1531. // Set pin to GPIO_CLOCK mode and set the clock frequency to 100KHz
  1532. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ;
  1533. delayMicroseconds (110) ;
  1534. gpioClockSet (pin, 100000) ;
  1535. }
  1536. }
  1537. else
  1538. {
  1539. if ((node = wiringPiFindNode (pin)) != NULL)
  1540. node->pinMode (node, pin, mode) ;
  1541. return ;
  1542. }
  1543. }
  1544. /*
  1545. * pullUpDownCtrl:
  1546. * Control the internal pull-up/down resistors on a GPIO pin.
  1547. *********************************************************************************
  1548. */
  1549. void pullUpDnControlDevice (int pin, int pud) {
  1550. unsigned int flag = lineFlags[pin];
  1551. unsigned int biasflags = GPIOHANDLE_REQUEST_BIAS_DISABLE | GPIOHANDLE_REQUEST_BIAS_PULL_UP | GPIOHANDLE_REQUEST_BIAS_PULL_DOWN;
  1552. flag &= ~biasflags;
  1553. switch (pud){
  1554. case PUD_OFF: flag |= GPIOHANDLE_REQUEST_BIAS_DISABLE; break;
  1555. case PUD_UP: flag |= GPIOHANDLE_REQUEST_BIAS_PULL_UP; break;
  1556. case PUD_DOWN: flag |= GPIOHANDLE_REQUEST_BIAS_PULL_DOWN; break;
  1557. default: return ; /* An illegal value */
  1558. }
  1559. // reset input/output
  1560. if (lineFlags[pin] & GPIOHANDLE_REQUEST_OUTPUT) {
  1561. pinModeFlagsDevice (pin, OUTPUT, flag);
  1562. } else if(lineFlags[pin] & GPIOHANDLE_REQUEST_INPUT) {
  1563. pinModeFlagsDevice (pin, INPUT, flag);
  1564. } else {
  1565. lineFlags[pin] = flag; // only store for later
  1566. }
  1567. }
  1568. void pullUpDnControl (int pin, int pud)
  1569. {
  1570. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1571. setupCheck ("pullUpDnControl") ;
  1572. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1573. {
  1574. switch(wiringPiMode) {
  1575. default: //WPI_MODE_GPIO_SYS
  1576. fprintf(stderr, "pinMode: invalid mode\n");
  1577. return;
  1578. case WPI_MODE_PINS:
  1579. pin = pinToGpio [pin];
  1580. break;
  1581. case WPI_MODE_PHYS:
  1582. pin = physToGpio [pin];
  1583. break;
  1584. case WPI_MODE_GPIO_DEVICE_BCM:
  1585. return pullUpDnControlDevice(pin, pud);
  1586. case WPI_MODE_GPIO_DEVICE_WPI:
  1587. return pullUpDnControlDevice(pinToGpio[pin], pud);
  1588. case WPI_MODE_GPIO_DEVICE_PHYS:
  1589. return pullUpDnControlDevice(physToGpio[pin], pud);
  1590. case WPI_MODE_GPIO:
  1591. break;
  1592. }
  1593. if (PI_MODEL_5 == RaspberryPiModel) {
  1594. unsigned int pullbits = pads[1+pin] & RP1_INV_PUD_MASK; // remove bits
  1595. switch (pud){
  1596. case PUD_OFF: pads[1+pin] = pullbits; break;
  1597. case PUD_UP: pads[1+pin] = pullbits | RP1_PUD_UP; break;
  1598. case PUD_DOWN: pads[1+pin] = pullbits | RP1_PUD_DOWN; break;
  1599. default: return ; /* An illegal value */
  1600. }
  1601. } else {
  1602. if (piGpioPupOffset == GPPUPPDN0)
  1603. {
  1604. // Pi 4B pull up/down method
  1605. int pullreg = GPPUPPDN0 + (pin>>4);
  1606. int pullshift = (pin & 0xf) << 1;
  1607. unsigned int pullbits;
  1608. unsigned int pull;
  1609. switch (pud) {
  1610. case PUD_OFF: pull = 0; break;
  1611. case PUD_UP: pull = 1; break;
  1612. case PUD_DOWN: pull = 2; break;
  1613. default: return ; /* An illegal value */
  1614. }
  1615. pullbits = *(gpio + pullreg);
  1616. pullbits &= ~(3 << pullshift);
  1617. pullbits |= (pull << pullshift);
  1618. *(gpio + pullreg) = pullbits;
  1619. }
  1620. else
  1621. {
  1622. // legacy pull up/down method
  1623. *(gpio + GPPUD) = pud & 3 ; delayMicroseconds (5) ;
  1624. *(gpio + gpioToPUDCLK [pin]) = 1 << (pin & 31) ; delayMicroseconds (5) ;
  1625. *(gpio + GPPUD) = 0 ; delayMicroseconds (5) ;
  1626. *(gpio + gpioToPUDCLK [pin]) = 0 ; delayMicroseconds (5) ;
  1627. }
  1628. }
  1629. }
  1630. else // Extension module
  1631. {
  1632. if ((node = wiringPiFindNode (pin)) != NULL)
  1633. node->pullUpDnControl (node, pin, pud) ;
  1634. return ;
  1635. }
  1636. }
  1637. /*
  1638. * digitalRead:
  1639. * Read the value of a given Pin, returning HIGH or LOW
  1640. *********************************************************************************
  1641. */
  1642. int digitalReadDevice (int pin) { // INPUT and OUTPUT should work
  1643. if (lineFds[pin]<0) {
  1644. // line not requested - auto request on first read as input
  1645. pinModeDevice(pin, INPUT);
  1646. }
  1647. if (lineFds[pin]>=0) {
  1648. struct gpiohandle_data data;
  1649. int ret = ioctl(lineFds[pin], GPIOHANDLE_GET_LINE_VALUES_IOCTL, &data);
  1650. if (ret) {
  1651. ReportDeviceError("get line values", pin, "digitalRead", ret);
  1652. return LOW; // error
  1653. }
  1654. return data.values[0];
  1655. }
  1656. return LOW; // error , need to request line before
  1657. }
  1658. int digitalRead (int pin)
  1659. {
  1660. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1661. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1662. {
  1663. switch(wiringPiMode) {
  1664. default: //WPI_MODE_GPIO_SYS
  1665. fprintf(stderr, "digitalRead: invalid mode\n");
  1666. return LOW;
  1667. case WPI_MODE_PINS:
  1668. pin = pinToGpio [pin];
  1669. break;
  1670. case WPI_MODE_PHYS:
  1671. pin = physToGpio [pin];
  1672. break;
  1673. case WPI_MODE_GPIO_DEVICE_BCM:
  1674. return digitalReadDevice(pin);
  1675. case WPI_MODE_GPIO_DEVICE_WPI:
  1676. return digitalReadDevice(pinToGpio[pin]);
  1677. case WPI_MODE_GPIO_DEVICE_PHYS:
  1678. return digitalReadDevice(physToGpio[pin]);
  1679. case WPI_MODE_GPIO:
  1680. break;
  1681. }
  1682. if (PI_MODEL_5 == RaspberryPiModel) {
  1683. switch(gpio[2*pin] & RP1_STATUS_LEVEL_MASK) {
  1684. default: // 11 or 00 not allowed, give LOW!
  1685. case RP1_STATUS_LEVEL_LOW: return LOW ;
  1686. case RP1_STATUS_LEVEL_HIGH: return HIGH ;
  1687. }
  1688. } else {
  1689. if ((*(gpio + gpioToGPLEV [pin]) & (1 << (pin & 31))) != 0)
  1690. return HIGH ;
  1691. else
  1692. return LOW ;
  1693. }
  1694. }
  1695. else
  1696. {
  1697. if ((node = wiringPiFindNode (pin)) == NULL)
  1698. return LOW ;
  1699. return node->digitalRead (node, pin) ;
  1700. }
  1701. }
  1702. /*
  1703. * digitalRead8:
  1704. * Read 8-bits (a byte) from given start pin.
  1705. *********************************************************************************
  1706. unsigned int digitalRead8 (int pin)
  1707. {
  1708. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1709. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1710. return 0 ;
  1711. else
  1712. {
  1713. if ((node = wiringPiFindNode (pin)) == NULL)
  1714. return LOW ;
  1715. return node->digitalRead8 (node, pin) ;
  1716. }
  1717. }
  1718. */
  1719. /*
  1720. * digitalWrite:
  1721. * Set an output bit
  1722. *********************************************************************************
  1723. */
  1724. void digitalWriteDevice (int pin, int value) {
  1725. if (wiringPiDebug)
  1726. printf ("digitalWriteDevice: ioctl pin:%d value: %d\n", pin, value) ;
  1727. if (lineFds[pin]<0) {
  1728. // line not requested - auto request on first write as output
  1729. pinModeDevice(pin, OUTPUT);
  1730. }
  1731. if (lineFds[pin]>=0 && (lineFlags[pin] & GPIOHANDLE_REQUEST_OUTPUT)>0) {
  1732. struct gpiohandle_data data;
  1733. data.values[0] = value;
  1734. if (wiringPiDebug)
  1735. printf ("digitalWriteDevice: ioctl pin:%d cmd: GPIOHANDLE_SET_LINE_VALUES_IOCTL, value: %d\n", pin, value) ;
  1736. int ret = ioctl(lineFds[pin], GPIOHANDLE_SET_LINE_VALUES_IOCTL, &data);
  1737. if (ret) {
  1738. ReportDeviceError("set line values", pin, "digitalWrite", ret);
  1739. return; // error
  1740. }
  1741. } else {
  1742. fprintf(stderr, "digitalWrite: no output (%d)\n", lineFlags[pin]);
  1743. }
  1744. return; // error
  1745. }
  1746. void digitalWrite (int pin, int value)
  1747. {
  1748. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1749. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1750. {
  1751. switch(wiringPiMode) {
  1752. default: //WPI_MODE_GPIO_SYS
  1753. fprintf(stderr, "digitalWrite: invalid mode\n");
  1754. return;
  1755. case WPI_MODE_PINS:
  1756. pin = pinToGpio [pin];
  1757. break;
  1758. case WPI_MODE_PHYS:
  1759. pin = physToGpio [pin];
  1760. break;
  1761. case WPI_MODE_GPIO_DEVICE_BCM:
  1762. digitalWriteDevice(pin, value);
  1763. return;
  1764. case WPI_MODE_GPIO_DEVICE_WPI:
  1765. digitalWriteDevice(pinToGpio[pin], value);
  1766. return;
  1767. case WPI_MODE_GPIO_DEVICE_PHYS:
  1768. digitalWriteDevice(physToGpio[pin], value);
  1769. return;
  1770. case WPI_MODE_GPIO:
  1771. break;
  1772. }
  1773. if (PI_MODEL_5 == RaspberryPiModel) {
  1774. if (value == LOW) {
  1775. //printf("Set pin %d >>0x%08x<< to low\n", pin, 1<<pin);
  1776. rio[RP1_RIO_OUT + RP1_CLR_OFFSET] = 1<<pin;
  1777. } else {
  1778. //printf("Set pin %d >>0x%08x<< to high\n", pin, 1<<pin);
  1779. rio[RP1_RIO_OUT + RP1_SET_OFFSET] = 1<<pin;
  1780. }
  1781. } else {
  1782. if (value == LOW)
  1783. *(gpio + gpioToGPCLR [pin]) = 1 << (pin & 31) ;
  1784. else
  1785. *(gpio + gpioToGPSET [pin]) = 1 << (pin & 31) ;
  1786. }
  1787. }
  1788. else
  1789. {
  1790. if ((node = wiringPiFindNode (pin)) != NULL)
  1791. node->digitalWrite (node, pin, value) ;
  1792. }
  1793. }
  1794. /*
  1795. * digitalWrite8:
  1796. * Set an output 8-bit byte on the device from the given pin number
  1797. *********************************************************************************
  1798. void digitalWrite8 (int pin, int value)
  1799. {
  1800. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1801. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1802. return ;
  1803. else
  1804. {
  1805. if ((node = wiringPiFindNode (pin)) != NULL)
  1806. node->digitalWrite8 (node, pin, value) ;
  1807. }
  1808. }
  1809. */
  1810. /*
  1811. * pwmWrite:
  1812. * Set an output PWM value
  1813. *********************************************************************************
  1814. */
  1815. void pwmWrite (int pin, int value)
  1816. {
  1817. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1818. FailOnModel5();
  1819. setupCheck ("pwmWrite") ;
  1820. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1821. {
  1822. /**/ if (wiringPiMode == WPI_MODE_PINS)
  1823. pin = pinToGpio [pin] ;
  1824. else if (wiringPiMode == WPI_MODE_PHYS)
  1825. pin = physToGpio [pin] ;
  1826. else if (wiringPiMode != WPI_MODE_GPIO)
  1827. return ;
  1828. /* would be possible on ms mode but not on bal, deactivated, use pwmc modify instead
  1829. if (piGpioBase == GPIO_PERI_BASE_2711) {
  1830. value = (OSC_FREQ_BCM2711*value)/OSC_FREQ_DEFAULT;
  1831. }
  1832. */
  1833. usingGpioMemCheck ("pwmWrite") ;
  1834. *(pwm + gpioToPwmPort [pin]) = value ;
  1835. if (wiringPiDebug) {
  1836. printf ("PWM value: %u. Current register: 0x%08X\n", value, *(pwm + gpioToPwmPort[pin]));
  1837. }
  1838. }
  1839. else
  1840. {
  1841. if ((node = wiringPiFindNode (pin)) != NULL)
  1842. node->pwmWrite (node, pin, value) ;
  1843. }
  1844. }
  1845. /*
  1846. * analogRead:
  1847. * Read the analog value of a given Pin.
  1848. * There is no on-board Pi analog hardware,
  1849. * so this needs to go to a new node.
  1850. *********************************************************************************
  1851. */
  1852. int analogRead (int pin)
  1853. {
  1854. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1855. if ((node = wiringPiFindNode (pin)) == NULL)
  1856. return 0 ;
  1857. else
  1858. return node->analogRead (node, pin) ;
  1859. }
  1860. /*
  1861. * analogWrite:
  1862. * Write the analog value to the given Pin.
  1863. * There is no on-board Pi analog hardware,
  1864. * so this needs to go to a new node.
  1865. *********************************************************************************
  1866. */
  1867. void analogWrite (int pin, int value)
  1868. {
  1869. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1870. if ((node = wiringPiFindNode (pin)) == NULL)
  1871. return ;
  1872. node->analogWrite (node, pin, value) ;
  1873. }
  1874. /*
  1875. * pwmToneWrite:
  1876. * Pi Specific.
  1877. * Output the given frequency on the Pi's PWM pin
  1878. *********************************************************************************
  1879. */
  1880. void pwmToneWrite (int pin, int freq)
  1881. {
  1882. FailOnModel5();
  1883. setupCheck ("pwmToneWrite") ;
  1884. if (freq == 0)
  1885. pwmWrite (pin, 0) ; // Off
  1886. else
  1887. {
  1888. int range = 600000 / freq ;
  1889. pwmSetRange (range) ;
  1890. pwmWrite (pin, freq / 2) ;
  1891. }
  1892. }
  1893. /*
  1894. * digitalWriteByte:
  1895. * digitalReadByte:
  1896. * Pi Specific
  1897. * Write an 8-bit byte to the first 8 GPIO pins - try to do it as
  1898. * fast as possible.
  1899. * However it still needs 2 operations to set the bits, so any external
  1900. * hardware must not rely on seeing a change as there will be a change
  1901. * to set the outputs bits to zero, then another change to set the 1's
  1902. * Reading is just bit fiddling.
  1903. * These are wiringPi pin numbers 0..7, or BCM_GPIO pin numbers
  1904. * 17, 18, 22, 23, 24, 24, 4 on a Pi v1 rev 0-3
  1905. * 17, 18, 27, 23, 24, 24, 4 on a Pi v1 rev 3 onwards or B+, 2, 3, zero
  1906. *********************************************************************************
  1907. */
  1908. void digitalWriteByte (const int value)
  1909. {
  1910. uint32_t pinSet = 0 ;
  1911. uint32_t pinClr = 0 ;
  1912. int mask = 1 ;
  1913. int pin ;
  1914. FailOnModel5();
  1915. if (wiringPiMode == WPI_MODE_GPIO_SYS)
  1916. {
  1917. return ;
  1918. }
  1919. else
  1920. {
  1921. for (pin = 0 ; pin < 8 ; ++pin)
  1922. {
  1923. if ((value & mask) == 0)
  1924. pinClr |= (1 << pinToGpio [pin]) ;
  1925. else
  1926. pinSet |= (1 << pinToGpio [pin]) ;
  1927. mask <<= 1 ;
  1928. }
  1929. *(gpio + gpioToGPCLR [0]) = pinClr ;
  1930. *(gpio + gpioToGPSET [0]) = pinSet ;
  1931. }
  1932. }
  1933. unsigned int digitalReadByte (void)
  1934. {
  1935. int pin, x ;
  1936. uint32_t raw ;
  1937. uint32_t data = 0 ;
  1938. FailOnModel5();
  1939. if (wiringPiMode == WPI_MODE_GPIO_SYS)
  1940. {
  1941. return 0;
  1942. }
  1943. else
  1944. {
  1945. raw = *(gpio + gpioToGPLEV [0]) ; // First bank for these pins
  1946. for (pin = 0 ; pin < 8 ; ++pin)
  1947. {
  1948. x = pinToGpio [pin] ;
  1949. data = (data << 1) | (((raw & (1 << x)) == 0) ? 0 : 1) ;
  1950. }
  1951. }
  1952. return data ;
  1953. }
  1954. /*
  1955. * digitalWriteByte2:
  1956. * digitalReadByte2:
  1957. * Pi Specific
  1958. * Write an 8-bit byte to the second set of 8 GPIO pins. This is marginally
  1959. * faster than the first lot as these are consecutive BCM_GPIO pin numbers.
  1960. * However they overlap with the original read/write bytes.
  1961. *********************************************************************************
  1962. */
  1963. void digitalWriteByte2 (const int value)
  1964. {
  1965. FailOnModel5();
  1966. if (wiringPiMode == WPI_MODE_GPIO_SYS)
  1967. {
  1968. }
  1969. else
  1970. {
  1971. *(gpio + gpioToGPCLR [0]) = (~value & 0xFF) << 20 ; // 0x0FF00000; ILJ > CHANGE: Old causes glitch
  1972. *(gpio + gpioToGPSET [0]) = ( value & 0xFF) << 20 ;
  1973. }
  1974. }
  1975. unsigned int digitalReadByte2 (void)
  1976. {
  1977. uint32_t data = 0 ;
  1978. FailOnModel5();
  1979. if (wiringPiMode == WPI_MODE_GPIO_SYS)
  1980. {
  1981. }
  1982. else
  1983. data = ((*(gpio + gpioToGPLEV [0])) >> 20) & 0xFF ; // First bank for these pins
  1984. return data ;
  1985. }
  1986. /*
  1987. * waitForInterrupt:
  1988. * Pi Specific.
  1989. * Wait for Interrupt on a GPIO pin.
  1990. * This is actually done via the /dev/gpiochip interface regardless of
  1991. * the wiringPi access mode in-use. Maybe sometime it might get a better
  1992. * way for a bit more efficiency.
  1993. *********************************************************************************
  1994. */
  1995. int waitForInterrupt (int pin, int mS)
  1996. {
  1997. int fd, ret;
  1998. struct pollfd polls ;
  1999. struct gpioevent_data evdata;
  2000. //struct gpio_v2_line_request req2;
  2001. if (wiringPiMode == WPI_MODE_PINS)
  2002. pin = pinToGpio [pin] ;
  2003. else if (wiringPiMode == WPI_MODE_PHYS)
  2004. pin = physToGpio [pin] ;
  2005. if ((fd = isrFds [pin]) == -1)
  2006. return -2 ;
  2007. // Setup poll structure
  2008. polls.fd = fd;
  2009. polls.events = POLLIN | POLLERR ;
  2010. polls.revents = 0;
  2011. // Wait for it ...
  2012. ret = poll(&polls, 1, mS);
  2013. if (ret <= 0) {
  2014. fprintf(stderr, "wiringPi: ERROR: poll returned=%d\n", ret);
  2015. } else {
  2016. //if (polls.revents & POLLIN)
  2017. if (wiringPiDebug) {
  2018. printf ("wiringPi: IRQ line %d received %d, fd=%d\n", pin, ret, isrFds[pin]) ;
  2019. }
  2020. /* read event data */
  2021. int readret = read(isrFds [pin], &evdata, sizeof(evdata));
  2022. if (readret == sizeof(evdata)) {
  2023. if (wiringPiDebug) {
  2024. printf ("wiringPi: IRQ data id: %d, timestamp: %lld\n", evdata.id, evdata.timestamp) ;
  2025. }
  2026. ret = evdata.id;
  2027. } else {
  2028. ret = 0;
  2029. }
  2030. }
  2031. return ret;
  2032. }
  2033. int waitForInterruptInit (int pin, int mode)
  2034. {
  2035. const char* strmode = "";
  2036. if (wiringPiMode == WPI_MODE_PINS) {
  2037. pin = pinToGpio [pin] ;
  2038. } else if (wiringPiMode == WPI_MODE_PHYS) {
  2039. pin = physToGpio [pin] ;
  2040. }
  2041. /* open gpio */
  2042. sleep(1);
  2043. if (wiringPiGpioDeviceGetFd()<0) {
  2044. return -1;
  2045. }
  2046. struct gpioevent_request req;
  2047. req.lineoffset = pin;
  2048. req.handleflags = GPIOHANDLE_REQUEST_INPUT;
  2049. switch(mode) {
  2050. default:
  2051. case INT_EDGE_SETUP:
  2052. if (wiringPiDebug) {
  2053. printf ("wiringPi: waitForInterruptMode mode INT_EDGE_SETUP - exiting\n") ;
  2054. }
  2055. return -1;
  2056. case INT_EDGE_FALLING:
  2057. req.eventflags = GPIOEVENT_REQUEST_FALLING_EDGE;
  2058. strmode = "falling";
  2059. break;
  2060. case INT_EDGE_RISING:
  2061. req.eventflags = GPIOEVENT_REQUEST_RISING_EDGE;
  2062. strmode = "rising";
  2063. break;
  2064. case INT_EDGE_BOTH:
  2065. req.eventflags = GPIOEVENT_REQUEST_BOTH_EDGES;
  2066. strmode = "both";
  2067. break;
  2068. }
  2069. strncpy(req.consumer_label, "wiringpi_gpio_irq", sizeof(req.consumer_label) - 1);
  2070. //later implement GPIO_V2_GET_LINE_IOCTL req2
  2071. int ret = ioctl(chipFd, GPIO_GET_LINEEVENT_IOCTL, &req);
  2072. if (ret) {
  2073. ReportDeviceError("get line event", pin , strmode, ret);
  2074. return -1;
  2075. }
  2076. if (wiringPiDebug) {
  2077. printf ("wiringPi: GPIO get line %d , mode %s succeded, fd=%d\n", pin, strmode, req.fd) ;
  2078. }
  2079. /* set event fd nonbloack read */
  2080. int fd_line = req.fd;
  2081. isrFds [pin] = fd_line;
  2082. int flags = fcntl(fd_line, F_GETFL);
  2083. flags |= O_NONBLOCK;
  2084. ret = fcntl(fd_line, F_SETFL, flags);
  2085. if (ret) {
  2086. fprintf(stderr, "wiringPi: ERROR: fcntl set nonblock return=%d\n", ret);
  2087. return -1;
  2088. }
  2089. return 0;
  2090. }
  2091. int waitForInterruptClose (int pin) {
  2092. if (isrFds[pin]>0) {
  2093. if (wiringPiDebug) {
  2094. printf ("wiringPi: waitForInterruptClose close thread 0x%lX\n", (unsigned long)isrThreads[pin]) ;
  2095. }
  2096. if (pthread_cancel(isrThreads[pin]) == 0) {
  2097. if (wiringPiDebug) {
  2098. printf ("wiringPi: waitForInterruptClose thread canceled successfuly\n") ;
  2099. }
  2100. } else {
  2101. if (wiringPiDebug) {
  2102. fprintf (stderr, "wiringPi: waitForInterruptClose could not cancel thread\n");
  2103. }
  2104. }
  2105. close(isrFds [pin]);
  2106. }
  2107. isrFds [pin] = -1;
  2108. isrFunctions [pin] = NULL;
  2109. /* -not closing so far - other isr may be using it - only close if no other is using - will code later
  2110. if (chipFd>0) {
  2111. close(chipFd);
  2112. }
  2113. chipFd = -1;
  2114. */
  2115. if (wiringPiDebug) {
  2116. printf ("wiringPi: waitForInterruptClose finished\n") ;
  2117. }
  2118. return 0;
  2119. }
  2120. int wiringPiISRStop (int pin) {
  2121. return waitForInterruptClose (pin);
  2122. }
  2123. /*
  2124. * interruptHandler:
  2125. * This is a thread and gets started to wait for the interrupt we're
  2126. * hoping to catch. It will call the user-function when the interrupt
  2127. * fires.
  2128. *********************************************************************************
  2129. */
  2130. static void *interruptHandler (UNU void *arg)
  2131. {
  2132. int pin ;
  2133. (void)piHiPri (55) ; // Only effective if we run as root
  2134. pin = pinPass ;
  2135. pinPass = -1 ;
  2136. for (;;) {
  2137. int ret = waitForInterrupt(pin, -1);
  2138. if ( ret> 0) {
  2139. if (wiringPiDebug) {
  2140. printf ("wiringPi: call function\n") ;
  2141. }
  2142. if(isrFunctions [pin]) {
  2143. isrFunctions [pin] () ;
  2144. }
  2145. // wait again - in the past forever - now can be stopped by waitForInterruptClose
  2146. } else if( ret< 0) {
  2147. break; // stop thread!
  2148. }
  2149. }
  2150. waitForInterruptClose (pin);
  2151. if (wiringPiDebug) {
  2152. printf ("wiringPi: interruptHandler finished\n") ;
  2153. }
  2154. return NULL ;
  2155. }
  2156. /*
  2157. * wiringPiISR:
  2158. * Pi Specific.
  2159. * Take the details and create an interrupt handler that will do a call-
  2160. * back to the user supplied function.
  2161. *********************************************************************************
  2162. */
  2163. int wiringPiISR (int pin, int mode, void (*function)(void))
  2164. {
  2165. const int maxpin = GetMaxPin();
  2166. if (pin < 0 || pin > maxpin)
  2167. return wiringPiFailure (WPI_FATAL, "wiringPiISR: pin must be 0-%d (%d)\n", maxpin, pin) ;
  2168. if (wiringPiMode == WPI_MODE_UNINITIALISED)
  2169. return wiringPiFailure (WPI_FATAL, "wiringPiISR: wiringPi has not been initialised. Unable to continue.\n") ;
  2170. if (wiringPiDebug) {
  2171. printf ("wiringPi: wiringPiISR pin %d, mode %d\n", pin, mode) ;
  2172. }
  2173. if (isrFunctions [pin]) {
  2174. printf ("wiringPi: ISR function alread active, ignoring \n") ;
  2175. }
  2176. isrFunctions [pin] = function ;
  2177. isrMode[pin] = mode;
  2178. if(waitForInterruptInit (pin, mode)<0) {
  2179. if (wiringPiDebug) {
  2180. fprintf (stderr, "wiringPi: waitForInterruptInit failed\n") ;
  2181. }
  2182. };
  2183. if (wiringPiDebug) {
  2184. printf ("wiringPi: mutex in\n") ;
  2185. }
  2186. pthread_mutex_lock (&pinMutex) ;
  2187. pinPass = pin ;
  2188. if (wiringPiDebug) {
  2189. printf("wiringPi: pthread_create before 0x%lX\n", (unsigned long)isrThreads[pin]);
  2190. }
  2191. if (pthread_create (&isrThreads[pin], NULL, interruptHandler, NULL)==0) {
  2192. if (wiringPiDebug) {
  2193. printf("wiringPi: pthread_create successed, 0x%lX\n", (unsigned long)isrThreads[pin]);
  2194. }
  2195. while (pinPass != -1)
  2196. delay (1) ;
  2197. } else {
  2198. if (wiringPiDebug) {
  2199. printf("wiringPi: pthread_create failed\n");
  2200. }
  2201. }
  2202. if (wiringPiDebug) {
  2203. printf ("wiringPi: mutex out\n") ;
  2204. }
  2205. pthread_mutex_unlock (&pinMutex) ;
  2206. if (wiringPiDebug) {
  2207. printf ("wiringPi: wiringPiISR finished\n") ;
  2208. }
  2209. return 0 ;
  2210. }
  2211. /*
  2212. * initialiseEpoch:
  2213. * Initialise our start-of-time variable to be the current unix
  2214. * time in milliseconds and microseconds.
  2215. *********************************************************************************
  2216. */
  2217. static void initialiseEpoch (void)
  2218. {
  2219. #ifdef OLD_WAY
  2220. struct timeval tv ;
  2221. gettimeofday (&tv, NULL) ;
  2222. epochMilli = (uint64_t)tv.tv_sec * (uint64_t)1000 + (uint64_t)(tv.tv_usec / 1000) ;
  2223. epochMicro = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)(tv.tv_usec) ;
  2224. #else
  2225. struct timespec ts ;
  2226. clock_gettime (CLOCK_MONOTONIC_RAW, &ts) ;
  2227. epochMilli = (uint64_t)ts.tv_sec * (uint64_t)1000 + (uint64_t)(ts.tv_nsec / 1000000L) ;
  2228. epochMicro = (uint64_t)ts.tv_sec * (uint64_t)1000000 + (uint64_t)(ts.tv_nsec / 1000L) ;
  2229. #endif
  2230. }
  2231. /*
  2232. * delay:
  2233. * Wait for some number of milliseconds
  2234. *********************************************************************************
  2235. */
  2236. void delay (unsigned int howLong)
  2237. {
  2238. struct timespec sleeper, dummy ;
  2239. sleeper.tv_sec = (time_t)(howLong / 1000) ;
  2240. sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ;
  2241. nanosleep (&sleeper, &dummy) ;
  2242. }
  2243. /*
  2244. * delayMicroseconds:
  2245. * This is somewhat intersting. It seems that on the Pi, a single call
  2246. * to nanosleep takes some 80 to 130 microseconds anyway, so while
  2247. * obeying the standards (may take longer), it's not always what we
  2248. * want!
  2249. *
  2250. * So what I'll do now is if the delay is less than 100uS we'll do it
  2251. * in a hard loop, watching a built-in counter on the ARM chip. This is
  2252. * somewhat sub-optimal in that it uses 100% CPU, something not an issue
  2253. * in a microcontroller, but under a multi-tasking, multi-user OS, it's
  2254. * wastefull, however we've no real choice )-:
  2255. *
  2256. * Plan B: It seems all might not be well with that plan, so changing it
  2257. * to use gettimeofday () and poll on that instead...
  2258. *********************************************************************************
  2259. */
  2260. void delayMicrosecondsHard (unsigned int howLong)
  2261. {
  2262. struct timeval tNow, tLong, tEnd ;
  2263. gettimeofday (&tNow, NULL) ;
  2264. tLong.tv_sec = howLong / 1000000 ;
  2265. tLong.tv_usec = howLong % 1000000 ;
  2266. timeradd (&tNow, &tLong, &tEnd) ;
  2267. while (timercmp (&tNow, &tEnd, <))
  2268. gettimeofday (&tNow, NULL) ;
  2269. }
  2270. void delayMicroseconds (unsigned int howLong)
  2271. {
  2272. struct timespec sleeper ;
  2273. unsigned int uSecs = howLong % 1000000 ;
  2274. unsigned int wSecs = howLong / 1000000 ;
  2275. /**/ if (howLong == 0)
  2276. return ;
  2277. else if (howLong < 100)
  2278. delayMicrosecondsHard (howLong) ;
  2279. else
  2280. {
  2281. sleeper.tv_sec = wSecs ;
  2282. sleeper.tv_nsec = (long)(uSecs * 1000L) ;
  2283. nanosleep (&sleeper, NULL) ;
  2284. }
  2285. }
  2286. /*
  2287. * millis:
  2288. * Return a number of milliseconds as an unsigned int.
  2289. * Wraps at 49 days.
  2290. *********************************************************************************
  2291. */
  2292. unsigned int millis (void)
  2293. {
  2294. uint64_t now ;
  2295. #ifdef OLD_WAY
  2296. struct timeval tv ;
  2297. gettimeofday (&tv, NULL) ;
  2298. now = (uint64_t)tv.tv_sec * (uint64_t)1000 + (uint64_t)(tv.tv_usec / 1000) ;
  2299. #else
  2300. struct timespec ts ;
  2301. clock_gettime (CLOCK_MONOTONIC_RAW, &ts) ;
  2302. now = (uint64_t)ts.tv_sec * (uint64_t)1000 + (uint64_t)(ts.tv_nsec / 1000000L) ;
  2303. #endif
  2304. return (uint32_t)(now - epochMilli) ;
  2305. }
  2306. /*
  2307. * micros:
  2308. * Return a number of microseconds as an unsigned int.
  2309. * Wraps after 71 minutes.
  2310. *********************************************************************************
  2311. */
  2312. unsigned int micros (void)
  2313. {
  2314. uint64_t now ;
  2315. #ifdef OLD_WAY
  2316. struct timeval tv ;
  2317. gettimeofday (&tv, NULL) ;
  2318. now = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)tv.tv_usec ;
  2319. #else
  2320. struct timespec ts ;
  2321. clock_gettime (CLOCK_MONOTONIC_RAW, &ts) ;
  2322. now = (uint64_t)ts.tv_sec * (uint64_t)1000000 + (uint64_t)(ts.tv_nsec / 1000) ;
  2323. #endif
  2324. return (uint32_t)(now - epochMicro) ;
  2325. }
  2326. unsigned long long piMicros64(void) {
  2327. struct timespec ts;
  2328. clock_gettime (CLOCK_MONOTONIC_RAW, &ts) ;
  2329. uint64_t now = (uint64_t)ts.tv_sec * (uint64_t)1000000 + (uint64_t)(ts.tv_nsec / 1000) ;
  2330. return (now - epochMicro) ;
  2331. }
  2332. /*
  2333. * wiringPiVersion:
  2334. * Return our current version number
  2335. *********************************************************************************
  2336. */
  2337. void wiringPiVersion (int *major, int *minor)
  2338. {
  2339. *major = VERSION_MAJOR ;
  2340. *minor = VERSION_MINOR ;
  2341. }
  2342. int wiringPiUserLevelAccess(void)
  2343. {
  2344. struct stat statBuf ;
  2345. const char* gpiomemModule = gpiomem_BCM;
  2346. piBoard();
  2347. if (PI_MODEL_5 == RaspberryPiModel) {
  2348. gpiomemModule = gpiomem_RP1;
  2349. }
  2350. return stat(gpiomemModule, &statBuf) == 0 ? 1 : 0;
  2351. }
  2352. int wiringPiGlobalMemoryAccess(void)
  2353. {
  2354. const char* gpiomemGlobal;
  2355. int fd=-1;
  2356. unsigned int MMAP_size;
  2357. unsigned int BaseAddr, PWMAddr;
  2358. piBoard();
  2359. if (PI_MODEL_5 == RaspberryPiModel) {
  2360. gpiomemGlobal = pciemem_RP1;
  2361. MMAP_size = pciemem_RP1_Size;
  2362. BaseAddr = 0x00000000;
  2363. PWMAddr = 0x00000000; //not supported so far
  2364. } else {
  2365. gpiomemGlobal = gpiomem_global;
  2366. MMAP_size = BLOCK_SIZE;
  2367. BaseAddr = piGpioBase + 0x00200000 ;
  2368. PWMAddr = piGpioBase + 0x0020C000 ;
  2369. }
  2370. if ((fd = open (gpiomemGlobal, O_RDWR | O_SYNC | O_CLOEXEC)) >0) {
  2371. int returnvalue = 1; // OK
  2372. uint32_t * lgpio = (uint32_t *)mmap(0, MMAP_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, BaseAddr) ;
  2373. if (lgpio == MAP_FAILED) {
  2374. returnvalue = 0;
  2375. if (wiringPiDebug)
  2376. fprintf(stderr,"wiringPiGlobalMemoryAccess: mmap (GPIO 0x%X,0x%X) failed: %s\n", BaseAddr, MMAP_size, strerror (errno)) ;
  2377. } else {
  2378. munmap(lgpio, MMAP_size);
  2379. if (PI_MODEL_5 == RaspberryPiModel) {
  2380. returnvalue = 2; // GPIO & PWM accessible (same area, nothing to mmap)
  2381. } else {
  2382. //check PWM area
  2383. uint32_t* lpwm = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, PWMAddr) ;
  2384. if (lpwm == MAP_FAILED) {
  2385. returnvalue = 1; // only GPIO accessible
  2386. if (wiringPiDebug)
  2387. fprintf(stderr,"wiringPiGlobalMemoryAccess: mmap (PWM 0x%X,0x%X) failed: %s\n", PWMAddr, MMAP_size, strerror (errno)) ;
  2388. } else {
  2389. returnvalue = 2; // GPIO & PWM accessible
  2390. munmap(lpwm, BLOCK_SIZE);
  2391. }
  2392. }
  2393. }
  2394. close(fd);
  2395. return returnvalue;
  2396. }
  2397. return 0; // Failed!
  2398. }
  2399. /*
  2400. * wiringPiSetup:
  2401. * Must be called once at the start of your program execution.
  2402. *
  2403. * Default setup: Initialises the system into wiringPi Pin mode and uses the
  2404. * memory mapped hardware directly.
  2405. *
  2406. * Changed now to revert to "gpio" mode if we're running on a Compute Module.
  2407. *********************************************************************************
  2408. */
  2409. int wiringPiSetup (void)
  2410. {
  2411. int fd ;
  2412. int model, rev, mem, maker, overVolted ;
  2413. if (wiringPiSetuped)
  2414. return 0 ;
  2415. wiringPiSetuped = TRUE ;
  2416. if (getenv (ENV_DEBUG) != NULL)
  2417. wiringPiDebug = TRUE ;
  2418. if (getenv (ENV_CODES) != NULL)
  2419. wiringPiReturnCodes = TRUE ;
  2420. if (wiringPiDebug)
  2421. printf ("wiringPi: wiringPiSetup called\n") ;
  2422. // Get the board ID information. We're not really using the information here,
  2423. // but it will give us information like the GPIO layout scheme (2 variants
  2424. // on the older 26-pin Pi's) and the GPIO peripheral base address.
  2425. // and if we're running on a compute module, then wiringPi pin numbers
  2426. // don't really mean anything, so force native BCM mode anyway.
  2427. piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
  2428. if ((model == PI_MODEL_CM) ||
  2429. (model == PI_MODEL_CM3) ||
  2430. (model == PI_MODEL_CM3P))
  2431. wiringPiMode = WPI_MODE_GPIO ;
  2432. else
  2433. wiringPiMode = WPI_MODE_PINS ;
  2434. /**/ if (piGpioLayout () == GPIO_LAYOUT_PI1_REV1) // A, B, Rev 1, 1.1
  2435. {
  2436. pinToGpio = pinToGpioR1 ;
  2437. physToGpio = physToGpioR1 ;
  2438. }
  2439. else // A2, B2, A+, B+, CM, Pi2, Pi3, Zero, Zero W, Zero 2 W
  2440. {
  2441. pinToGpio = pinToGpioR2 ;
  2442. physToGpio = physToGpioR2 ;
  2443. }
  2444. // Open the master /dev/ memory control device
  2445. // Device strategy: December 2016:
  2446. // Try /dev/mem. If that fails, then
  2447. // try /dev/gpiomem. If that fails then game over.
  2448. const char* gpiomemGlobal = gpiomem_global;
  2449. const char* gpiomemModule = gpiomem_BCM;
  2450. if (PI_MODEL_5 == model) {
  2451. gpiomemGlobal = pciemem_RP1;
  2452. gpiomemModule = gpiomem_RP1;
  2453. }
  2454. usingGpioMem = FALSE;
  2455. if (gpiomemGlobal==NULL || (fd = open (gpiomemGlobal, O_RDWR | O_SYNC | O_CLOEXEC)) < 0)
  2456. {
  2457. if (wiringPiDebug) {
  2458. printf ("wiringPi: no access to %s try %s\n", gpiomemGlobal, gpiomemModule) ;
  2459. }
  2460. if (gpiomemModule && (fd = open (gpiomemModule, O_RDWR | O_SYNC | O_CLOEXEC) ) >= 0) // We're using gpiomem
  2461. {
  2462. piGpioBase = 0 ;
  2463. usingGpioMem = TRUE ;
  2464. }
  2465. else
  2466. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: Unable to open %s or %s: %s.\n"
  2467. " Aborting your program because if it can not access the GPIO\n"
  2468. " hardware then it most certianly won't work\n"
  2469. " Try running with sudo?\n", gpiomemGlobal, gpiomemModule, strerror (errno)) ;
  2470. }
  2471. if (wiringPiDebug) {
  2472. printf ("wiringPi: access to %s succeded %d\n", usingGpioMem ? gpiomemModule : gpiomemGlobal, fd) ;
  2473. }
  2474. // GPIO:
  2475. if (PI_MODEL_5 != model) {
  2476. //Set the offsets into the memory interface.
  2477. GPIO_PADS = piGpioBase + 0x00100000 ;
  2478. GPIO_CLOCK_BASE = piGpioBase + 0x00101000 ;
  2479. GPIO_BASE = piGpioBase + 0x00200000 ;
  2480. GPIO_TIMER = piGpioBase + 0x0000B000 ;
  2481. GPIO_PWM = piGpioBase + 0x0020C000 ;
  2482. GPIO_RIO = 0x00 ;
  2483. // Map the individual hardware components
  2484. // GPIO:
  2485. base = NULL;
  2486. gpio = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_BASE) ;
  2487. if (gpio == MAP_FAILED)
  2488. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (GPIO) failed: %s\n", strerror (errno)) ;
  2489. // PWM
  2490. pwm = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PWM) ;
  2491. if (pwm == MAP_FAILED)
  2492. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PWM) failed: %s\n", strerror (errno)) ;
  2493. // Clock control (needed for PWM)
  2494. clk = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_CLOCK_BASE) ;
  2495. if (clk == MAP_FAILED)
  2496. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (CLOCK) failed: %s\n", strerror (errno)) ;
  2497. // The drive pads
  2498. pads = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PADS) ;
  2499. if (pads == MAP_FAILED)
  2500. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PADS) failed: %s\n", strerror (errno)) ;
  2501. // The system timer
  2502. timer = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_TIMER) ;
  2503. if (timer == MAP_FAILED)
  2504. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (TIMER) failed: %s\n", strerror (errno)) ;
  2505. // Set the timer to free-running, 1MHz.
  2506. // 0xF9 is 249, the timer divide is base clock / (divide+1)
  2507. // so base clock is 250MHz / 250 = 1MHz.
  2508. *(timer + TIMER_CONTROL) = 0x0000280 ;
  2509. *(timer + TIMER_PRE_DIV) = 0x00000F9 ;
  2510. timerIrqRaw = timer + TIMER_IRQ_RAW ;
  2511. // Export the base addresses for any external software that might need them
  2512. _wiringPiBase = base ;
  2513. _wiringPiGpio = gpio ;
  2514. _wiringPiPwm = pwm ;
  2515. _wiringPiClk = clk ;
  2516. _wiringPiPads = pads ;
  2517. _wiringPiTimer = timer ;
  2518. _wiringPiRio = NULL ;
  2519. } else {
  2520. unsigned int MMAP_size = (usingGpioMem) ? gpiomem_RP1_Size : pciemem_RP1_Size;
  2521. GPIO_PADS = (RP1_PADS0_Addr-RP1_IO0_Addr) ;
  2522. GPIO_CLOCK_BASE = 0x00;
  2523. GPIO_BASE = (RP1_IO0_Addr-RP1_BASE_Addr) ;
  2524. GPIO_TIMER = 0x00;
  2525. GPIO_PWM = RP1_PWM0_Addr-RP1_BASE_Addr;
  2526. GPIO_RIO = (RP1_SYS_RIO0_Addr-RP1_IO0_Addr) ;
  2527. //map hole RP1 memory block from beginning,
  2528. base = (unsigned int *)mmap(0, MMAP_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x00000000) ;
  2529. if (base == MAP_FAILED)
  2530. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap failed: %s\n", strerror (errno)) ;
  2531. if (usingGpioMem) {
  2532. gpio = base; // RP1 start adress of map memory for gpio (same as module memory)
  2533. } else {
  2534. gpio = &base[GPIO_BASE/4];// RP1 start adress of map memory for gpio
  2535. }
  2536. pads = &gpio[GPIO_PADS/4]; // RP1 start adress of map memory for pads
  2537. rio = &gpio[GPIO_RIO/4]; // RP1 start adress of map memory for rio
  2538. GPIO_PADS += GPIO_BASE;
  2539. GPIO_RIO += GPIO_BASE;
  2540. // Export the base addresses for any external software that might need them
  2541. _wiringPiBase = base ;
  2542. _wiringPiGpio = gpio ;
  2543. _wiringPiPwm = NULL ;
  2544. _wiringPiClk = NULL ;
  2545. _wiringPiPads = pads ;
  2546. _wiringPiTimer = NULL ;
  2547. _wiringPiRio = rio ;
  2548. }
  2549. if (wiringPiDebug) {
  2550. printf ("wiringPi: memory map gpio 0x%x %s\n", GPIO_BASE , _wiringPiGpio ? "valid" : "invalid");
  2551. printf ("wiringPi: memory map pads 0x%x %s\n", GPIO_PADS , _wiringPiPads ? "valid" : "invalid");
  2552. printf ("wiringPi: memory map rio 0x%x %s\n", GPIO_RIO , _wiringPiRio ? "valid" : "invalid");
  2553. printf ("wiringPi: memory map pwm 0x%x %s\n", GPIO_PWM , _wiringPiPwm ? "valid" : "invalid");
  2554. printf ("wiringPi: memory map clk 0x%x %s\n", GPIO_CLOCK_BASE, _wiringPiClk ? "valid" : "invalid");
  2555. printf ("wiringPi: memory map timer 0x%x %s\n", GPIO_TIMER,_wiringPiTimer ? "valid" : "invalid");
  2556. }
  2557. initialiseEpoch () ;
  2558. return 0 ;
  2559. }
  2560. /*
  2561. * wiringPiSetupGpio:
  2562. * Must be called once at the start of your program execution.
  2563. *
  2564. * GPIO setup: Initialises the system into GPIO Pin mode and uses the
  2565. * memory mapped hardware directly.
  2566. *********************************************************************************
  2567. */
  2568. int wiringPiSetupGpio (void)
  2569. {
  2570. (void)wiringPiSetup () ;
  2571. if (wiringPiDebug)
  2572. printf ("wiringPi: wiringPiSetupGpio called\n") ;
  2573. wiringPiMode = WPI_MODE_GPIO ;
  2574. return 0 ;
  2575. }
  2576. /*
  2577. * wiringPiSetupPhys:
  2578. * Must be called once at the start of your program execution.
  2579. *
  2580. * Phys setup: Initialises the system into Physical Pin mode and uses the
  2581. * memory mapped hardware directly.
  2582. *********************************************************************************
  2583. */
  2584. int wiringPiSetupPhys (void)
  2585. {
  2586. (void)wiringPiSetup () ;
  2587. if (wiringPiDebug)
  2588. printf ("wiringPi: wiringPiSetupPhys called\n") ;
  2589. wiringPiMode = WPI_MODE_PHYS ;
  2590. return 0 ;
  2591. }
  2592. int wiringPiSetupPinType (enum WPIPinType pinType) {
  2593. if (wiringPiDebug)
  2594. printf ("wiringPi: wiringPiSetupPinType(%d) called\n", (int) pinType) ;
  2595. switch (pinType) {
  2596. case WPI_PIN_BCM: return wiringPiSetupGpio();
  2597. case WPI_PIN_WPI: return wiringPiSetup();
  2598. case WPI_PIN_PHYS: return wiringPiSetupPhys();
  2599. default: return -1;
  2600. }
  2601. }
  2602. int wiringPiSetupGpioDevice (enum WPIPinType pinType) {
  2603. if (wiringPiSetuped)
  2604. return 0 ;
  2605. if (wiringPiDebug) {
  2606. printf ("wiringPi: wiringPiSetupGpioDevice(%d) called\n", (int)pinType) ;
  2607. }
  2608. if (getenv (ENV_DEBUG) != NULL)
  2609. wiringPiDebug = TRUE ;
  2610. if (getenv (ENV_CODES) != NULL)
  2611. wiringPiReturnCodes = TRUE ;
  2612. if (wiringPiGpioDeviceGetFd()<0) {
  2613. return -1;
  2614. }
  2615. wiringPiSetuped = TRUE ;
  2616. if (piGpioLayout () == GPIO_LAYOUT_PI1_REV1){
  2617. pinToGpio = pinToGpioR1 ;
  2618. physToGpio = physToGpioR1 ;
  2619. } else {
  2620. pinToGpio = pinToGpioR2 ;
  2621. physToGpio = physToGpioR2 ;
  2622. }
  2623. initialiseEpoch () ;
  2624. switch (pinType) {
  2625. case WPI_PIN_BCM:
  2626. wiringPiMode = WPI_MODE_GPIO_DEVICE_BCM;
  2627. break;
  2628. case WPI_PIN_WPI:
  2629. wiringPiMode = WPI_MODE_GPIO_DEVICE_WPI;
  2630. break;
  2631. case WPI_PIN_PHYS:
  2632. wiringPiMode = WPI_MODE_GPIO_DEVICE_PHYS;
  2633. break;
  2634. default:
  2635. wiringPiSetuped = FALSE;
  2636. return -1;
  2637. }
  2638. return 0 ;
  2639. }
  2640. /*
  2641. * wiringPiSetupSys:
  2642. * GPIO Sysfs Interface for Userspace is deprecated
  2643. * https://www.kernel.org/doc/html/v5.5/admin-guide/gpio/sysfs.html
  2644. *
  2645. * Switched to new GPIO driver Interface in version 3.3
  2646. */
  2647. int wiringPiSetupSys (void)
  2648. {
  2649. if (wiringPiSetuped)
  2650. return 0 ;
  2651. if (wiringPiDebug)
  2652. printf ("wiringPi: wiringPiSetupSys called\n") ;
  2653. return wiringPiSetupGpioDevice(WPI_PIN_BCM);
  2654. }