Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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