Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

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