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.

wiringPi.c 91 KiB

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