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

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