wiringPi.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503
  1. /*
  2. * wiringPi:
  3. * Arduino look-a-like Wiring library for the Raspberry Pi
  4. * Copyright (c) 2012-2017 Gordon Henderson
  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://projects.drogon.net/raspberry-pi/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 "softPwm.h"
  71. #include "softTone.h"
  72. #include "wiringPi.h"
  73. #include "../version.h"
  74. // Environment Variables
  75. #define ENV_DEBUG "WIRINGPI_DEBUG"
  76. #define ENV_CODES "WIRINGPI_CODES"
  77. #define ENV_GPIOMEM "WIRINGPI_GPIOMEM"
  78. // Extend wiringPi with other pin-based devices and keep track of
  79. // them in this structure
  80. struct wiringPiNodeStruct *wiringPiNodes = NULL ;
  81. // BCM Magic
  82. #define BCM_PASSWORD 0x5A000000
  83. // The BCM2835 has 54 GPIO pins.
  84. // BCM2835 data sheet, Page 90 onwards.
  85. // There are 6 control registers, each control the functions of a block
  86. // of 10 pins.
  87. // Each control register has 10 sets of 3 bits per GPIO pin - the ALT values
  88. //
  89. // 000 = GPIO Pin X is an input
  90. // 001 = GPIO Pin X is an output
  91. // 100 = GPIO Pin X takes alternate function 0
  92. // 101 = GPIO Pin X takes alternate function 1
  93. // 110 = GPIO Pin X takes alternate function 2
  94. // 111 = GPIO Pin X takes alternate function 3
  95. // 011 = GPIO Pin X takes alternate function 4
  96. // 010 = GPIO Pin X takes alternate function 5
  97. //
  98. // So the 3 bits for port X are:
  99. // X / 10 + ((X % 10) * 3)
  100. // Port function select bits
  101. #define FSEL_INPT 0b000
  102. #define FSEL_OUTP 0b001
  103. #define FSEL_ALT0 0b100
  104. #define FSEL_ALT1 0b101
  105. #define FSEL_ALT2 0b110
  106. #define FSEL_ALT3 0b111
  107. #define FSEL_ALT4 0b011
  108. #define FSEL_ALT5 0b010
  109. // Access from ARM Running Linux
  110. // Taken from Gert/Doms code. Some of this is not in the manual
  111. // that I can find )-:
  112. //
  113. // Updates in September 2015 - all now static variables (and apologies for the caps)
  114. // due to the Pi v2, v3, etc. and the new /dev/gpiomem interface
  115. static volatile unsigned int GPIO_PADS ;
  116. static volatile unsigned int GPIO_CLOCK_BASE ;
  117. static volatile unsigned int GPIO_BASE ;
  118. static volatile unsigned int GPIO_TIMER ;
  119. static volatile unsigned int GPIO_PWM ;
  120. #define PAGE_SIZE (4*1024)
  121. #define BLOCK_SIZE (4*1024)
  122. static unsigned int usingGpioMem = FALSE ;
  123. static int wiringPiSetuped = FALSE ;
  124. // PWM
  125. // Word offsets into the PWM control region
  126. #define PWM_CONTROL 0
  127. #define PWM_STATUS 1
  128. #define PWM0_RANGE 4
  129. #define PWM0_DATA 5
  130. #define PWM1_RANGE 8
  131. #define PWM1_DATA 9
  132. // Clock regsiter offsets
  133. #define PWMCLK_CNTL 40
  134. #define PWMCLK_DIV 41
  135. #define PWM0_MS_MODE 0x0080 // Run in MS mode
  136. #define PWM0_USEFIFO 0x0020 // Data from FIFO
  137. #define PWM0_REVPOLAR 0x0010 // Reverse polarity
  138. #define PWM0_OFFSTATE 0x0008 // Ouput Off state
  139. #define PWM0_REPEATFF 0x0004 // Repeat last value if FIFO empty
  140. #define PWM0_SERIAL 0x0002 // Run in serial mode
  141. #define PWM0_ENABLE 0x0001 // Channel Enable
  142. #define PWM1_MS_MODE 0x8000 // Run in MS mode
  143. #define PWM1_USEFIFO 0x2000 // Data from FIFO
  144. #define PWM1_REVPOLAR 0x1000 // Reverse polarity
  145. #define PWM1_OFFSTATE 0x0800 // Ouput Off state
  146. #define PWM1_REPEATFF 0x0400 // Repeat last value if FIFO empty
  147. #define PWM1_SERIAL 0x0200 // Run in serial mode
  148. #define PWM1_ENABLE 0x0100 // Channel Enable
  149. // Timer
  150. // Word offsets
  151. #define TIMER_LOAD (0x400 >> 2)
  152. #define TIMER_VALUE (0x404 >> 2)
  153. #define TIMER_CONTROL (0x408 >> 2)
  154. #define TIMER_IRQ_CLR (0x40C >> 2)
  155. #define TIMER_IRQ_RAW (0x410 >> 2)
  156. #define TIMER_IRQ_MASK (0x414 >> 2)
  157. #define TIMER_RELOAD (0x418 >> 2)
  158. #define TIMER_PRE_DIV (0x41C >> 2)
  159. #define TIMER_COUNTER (0x420 >> 2)
  160. // Locals to hold pointers to the hardware
  161. static volatile unsigned int *gpio ;
  162. static volatile unsigned int *pwm ;
  163. static volatile unsigned int *clk ;
  164. static volatile unsigned int *pads ;
  165. static volatile unsigned int *timer ;
  166. static volatile unsigned int *timerIrqRaw ;
  167. // Export variables for the hardware pointers
  168. volatile unsigned int *_wiringPiGpio ;
  169. volatile unsigned int *_wiringPiPwm ;
  170. volatile unsigned int *_wiringPiClk ;
  171. volatile unsigned int *_wiringPiPads ;
  172. volatile unsigned int *_wiringPiTimer ;
  173. volatile unsigned int *_wiringPiTimerIrqRaw ;
  174. // Data for use with the boardId functions.
  175. // The order of entries here to correspond with the PI_MODEL_X
  176. // and PI_VERSION_X defines in wiringPi.h
  177. // Only intended for the gpio command - use at your own risk!
  178. // piGpioBase:
  179. // The base address of the GPIO memory mapped hardware IO
  180. #define GPIO_PERI_BASE_OLD 0x20000000
  181. #define GPIO_PERI_BASE_2835 0x3F000000
  182. #define GPIO_PERI_BASE_2711 0xFE000000
  183. static volatile unsigned int piGpioBase = 0 ;
  184. const char *piModelNames [20] =
  185. {
  186. "Model A", // 0
  187. "Model B", // 1
  188. "Model A+", // 2
  189. "Model B+", // 3
  190. "Pi 2", // 4
  191. "Alpha", // 5
  192. "CM", // 6
  193. "Unknown07", // 07
  194. "Pi 3", // 08
  195. "Pi Zero", // 09
  196. "CM3", // 10
  197. "Unknown11", // 11
  198. "Pi Zero-W", // 12
  199. "Pi 3B+", // 13
  200. "Pi 3A+", // 14
  201. "Unknown15", // 15
  202. "CM3+", // 16
  203. "Pi 4B", // 17
  204. "Unknown18", // 18
  205. "Unknown19", // 19
  206. } ;
  207. const char *piRevisionNames [16] =
  208. {
  209. "00",
  210. "01",
  211. "02",
  212. "03",
  213. "04",
  214. "05",
  215. "06",
  216. "07",
  217. "08",
  218. "09",
  219. "10",
  220. "11",
  221. "12",
  222. "13",
  223. "14",
  224. "15",
  225. } ;
  226. const char *piMakerNames [16] =
  227. {
  228. "Sony", // 0
  229. "Egoman", // 1
  230. "Embest", // 2
  231. "Unknown", // 3
  232. "Embest", // 4
  233. "Unknown05", // 5
  234. "Unknown06", // 6
  235. "Unknown07", // 7
  236. "Unknown08", // 8
  237. "Unknown09", // 9
  238. "Unknown10", // 10
  239. "Unknown11", // 11
  240. "Unknown12", // 12
  241. "Unknown13", // 13
  242. "Unknown14", // 14
  243. "Unknown15", // 15
  244. } ;
  245. const int piMemorySize [8] =
  246. {
  247. 256, // 0
  248. 512, // 1
  249. 1024, // 2
  250. 2048, // 3
  251. 4096, // 4
  252. 0, // 5
  253. 0, // 6
  254. 0, // 7
  255. } ;
  256. // Time for easy calculations
  257. static uint64_t epochMilli, epochMicro ;
  258. // Misc
  259. static int wiringPiMode = WPI_MODE_UNINITIALISED ;
  260. static volatile int pinPass = -1 ;
  261. static pthread_mutex_t pinMutex ;
  262. // Debugging & Return codes
  263. int wiringPiDebug = FALSE ;
  264. int wiringPiReturnCodes = FALSE ;
  265. // Use /dev/gpiomem ?
  266. int wiringPiTryGpioMem = FALSE ;
  267. // sysFds:
  268. // Map a file descriptor from the /sys/class/gpio/gpioX/value
  269. static int sysFds [64] =
  270. {
  271. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  272. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  273. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  274. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  275. } ;
  276. // ISR Data
  277. static void (*isrFunctions [64])(void) ;
  278. // Doing it the Arduino way with lookup tables...
  279. // Yes, it's probably more innefficient than all the bit-twidling, but it
  280. // does tend to make it all a bit clearer. At least to me!
  281. // pinToGpio:
  282. // Take a Wiring pin (0 through X) and re-map it to the BCM_GPIO pin
  283. // Cope for 3 different board revisions here.
  284. static int *pinToGpio ;
  285. // Revision 1, 1.1:
  286. static int pinToGpioR1 [64] =
  287. {
  288. 17, 18, 21, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7: wpi 0 - 7
  289. 0, 1, // I2C - SDA1, SCL1 wpi 8 - 9
  290. 8, 7, // SPI - CE1, CE0 wpi 10 - 11
  291. 10, 9, 11, // SPI - MOSI, MISO, SCLK wpi 12 - 14
  292. 14, 15, // UART - Tx, Rx wpi 15 - 16
  293. // Padding:
  294. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 31
  295. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
  296. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
  297. } ;
  298. // Revision 2:
  299. static int pinToGpioR2 [64] =
  300. {
  301. 17, 18, 27, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7: wpi 0 - 7
  302. 2, 3, // I2C - SDA0, SCL0 wpi 8 - 9
  303. 8, 7, // SPI - CE1, CE0 wpi 10 - 11
  304. 10, 9, 11, // SPI - MOSI, MISO, SCLK wpi 12 - 14
  305. 14, 15, // UART - Tx, Rx wpi 15 - 16
  306. 28, 29, 30, 31, // Rev 2: New GPIOs 8 though 11 wpi 17 - 20
  307. 5, 6, 13, 19, 26, // B+ wpi 21, 22, 23, 24, 25
  308. 12, 16, 20, 21, // B+ wpi 26, 27, 28, 29
  309. 0, 1, // B+ wpi 30, 31
  310. // Padding:
  311. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
  312. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
  313. } ;
  314. // physToGpio:
  315. // Take a physical pin (1 through 26) and re-map it to the BCM_GPIO pin
  316. // Cope for 2 different board revisions here.
  317. // Also add in the P5 connector, so the P5 pins are 3,4,5,6, so 53,54,55,56
  318. static int *physToGpio ;
  319. static int physToGpioR1 [64] =
  320. {
  321. -1, // 0
  322. -1, -1, // 1, 2
  323. 0, -1,
  324. 1, -1,
  325. 4, 14,
  326. -1, 15,
  327. 17, 18,
  328. 21, -1,
  329. 22, 23,
  330. -1, 24,
  331. 10, -1,
  332. 9, 25,
  333. 11, 8,
  334. -1, 7, // 25, 26
  335. -1, -1, -1, -1, -1, // ... 31
  336. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
  337. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
  338. } ;
  339. static int physToGpioR2 [64] =
  340. {
  341. -1, // 0
  342. -1, -1, // 1, 2
  343. 2, -1,
  344. 3, -1,
  345. 4, 14,
  346. -1, 15,
  347. 17, 18,
  348. 27, -1,
  349. 22, 23,
  350. -1, 24,
  351. 10, -1,
  352. 9, 25,
  353. 11, 8,
  354. -1, 7, // 25, 26
  355. // B+
  356. 0, 1,
  357. 5, -1,
  358. 6, 12,
  359. 13, -1,
  360. 19, 16,
  361. 26, 20,
  362. -1, 21,
  363. // the P5 connector on the Rev 2 boards:
  364. -1, -1,
  365. -1, -1,
  366. -1, -1,
  367. -1, -1,
  368. -1, -1,
  369. 28, 29,
  370. 30, 31,
  371. -1, -1,
  372. -1, -1,
  373. -1, -1,
  374. -1, -1,
  375. } ;
  376. // gpioToGPFSEL:
  377. // Map a BCM_GPIO pin to it's Function Selection
  378. // control port. (GPFSEL 0-5)
  379. // Groups of 10 - 3 bits per Function - 30 bits per port
  380. static uint8_t gpioToGPFSEL [] =
  381. {
  382. 0,0,0,0,0,0,0,0,0,0,
  383. 1,1,1,1,1,1,1,1,1,1,
  384. 2,2,2,2,2,2,2,2,2,2,
  385. 3,3,3,3,3,3,3,3,3,3,
  386. 4,4,4,4,4,4,4,4,4,4,
  387. 5,5,5,5,5,5,5,5,5,5,
  388. } ;
  389. // gpioToShift
  390. // Define the shift up for the 3 bits per pin in each GPFSEL port
  391. static uint8_t gpioToShift [] =
  392. {
  393. 0,3,6,9,12,15,18,21,24,27,
  394. 0,3,6,9,12,15,18,21,24,27,
  395. 0,3,6,9,12,15,18,21,24,27,
  396. 0,3,6,9,12,15,18,21,24,27,
  397. 0,3,6,9,12,15,18,21,24,27,
  398. 0,3,6,9,12,15,18,21,24,27,
  399. } ;
  400. // gpioToGPSET:
  401. // (Word) offset to the GPIO Set registers for each GPIO pin
  402. static uint8_t gpioToGPSET [] =
  403. {
  404. 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,
  405. 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,
  406. } ;
  407. // gpioToGPCLR:
  408. // (Word) offset to the GPIO Clear registers for each GPIO pin
  409. static uint8_t gpioToGPCLR [] =
  410. {
  411. 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,
  412. 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,
  413. } ;
  414. // gpioToGPLEV:
  415. // (Word) offset to the GPIO Input level registers for each GPIO pin
  416. static uint8_t gpioToGPLEV [] =
  417. {
  418. 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,
  419. 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,
  420. } ;
  421. #ifdef notYetReady
  422. // gpioToEDS
  423. // (Word) offset to the Event Detect Status
  424. static uint8_t gpioToEDS [] =
  425. {
  426. 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,
  427. 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,
  428. } ;
  429. // gpioToREN
  430. // (Word) offset to the Rising edge ENable register
  431. static uint8_t gpioToREN [] =
  432. {
  433. 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,
  434. 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,
  435. } ;
  436. // gpioToFEN
  437. // (Word) offset to the Falling edgde ENable register
  438. static uint8_t gpioToFEN [] =
  439. {
  440. 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,
  441. 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,
  442. } ;
  443. #endif
  444. // GPPUD:
  445. // GPIO Pin pull up/down register
  446. #define GPPUD 37
  447. /* 2711 has a different mechanism for pin pull-up/down/enable */
  448. #define GPPUPPDN0 57 /* Pin pull-up/down for pins 15:0 */
  449. #define GPPUPPDN1 58 /* Pin pull-up/down for pins 31:16 */
  450. #define GPPUPPDN2 59 /* Pin pull-up/down for pins 47:32 */
  451. #define GPPUPPDN3 60 /* Pin pull-up/down for pins 57:48 */
  452. static volatile unsigned int piGpioPupOffset = 0 ;
  453. // gpioToPUDCLK
  454. // (Word) offset to the Pull Up Down Clock regsiter
  455. static uint8_t gpioToPUDCLK [] =
  456. {
  457. 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,
  458. 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,
  459. } ;
  460. // gpioToPwmALT
  461. // the ALT value to put a GPIO pin into PWM mode
  462. static uint8_t gpioToPwmALT [] =
  463. {
  464. 0, 0, 0, 0, 0, 0, 0, 0, // 0 -> 7
  465. 0, 0, 0, 0, FSEL_ALT0, FSEL_ALT0, 0, 0, // 8 -> 15
  466. 0, 0, FSEL_ALT5, FSEL_ALT5, 0, 0, 0, 0, // 16 -> 23
  467. 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
  468. 0, 0, 0, 0, 0, 0, 0, 0, // 32 -> 39
  469. FSEL_ALT0, FSEL_ALT0, 0, 0, 0, FSEL_ALT0, 0, 0, // 40 -> 47
  470. 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
  471. 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
  472. } ;
  473. // gpioToPwmPort
  474. // The port value to put a GPIO pin into PWM mode
  475. static uint8_t gpioToPwmPort [] =
  476. {
  477. 0, 0, 0, 0, 0, 0, 0, 0, // 0 -> 7
  478. 0, 0, 0, 0, PWM0_DATA, PWM1_DATA, 0, 0, // 8 -> 15
  479. 0, 0, PWM0_DATA, PWM1_DATA, 0, 0, 0, 0, // 16 -> 23
  480. 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
  481. 0, 0, 0, 0, 0, 0, 0, 0, // 32 -> 39
  482. PWM0_DATA, PWM1_DATA, 0, 0, 0, PWM1_DATA, 0, 0, // 40 -> 47
  483. 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
  484. 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
  485. } ;
  486. // gpioToGpClkALT:
  487. // ALT value to put a GPIO pin into GP Clock mode.
  488. // On the Pi we can really only use BCM_GPIO_4 and BCM_GPIO_21
  489. // for clocks 0 and 1 respectively, however I'll include the full
  490. // list for completeness - maybe one day...
  491. #define GPIO_CLOCK_SOURCE 1
  492. // gpioToGpClkALT0:
  493. static uint8_t gpioToGpClkALT0 [] =
  494. {
  495. 0, 0, 0, 0, FSEL_ALT0, FSEL_ALT0, FSEL_ALT0, 0, // 0 -> 7
  496. 0, 0, 0, 0, 0, 0, 0, 0, // 8 -> 15
  497. 0, 0, 0, 0, FSEL_ALT5, FSEL_ALT5, 0, 0, // 16 -> 23
  498. 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
  499. FSEL_ALT0, 0, FSEL_ALT0, 0, 0, 0, 0, 0, // 32 -> 39
  500. 0, 0, FSEL_ALT0, FSEL_ALT0, FSEL_ALT0, 0, 0, 0, // 40 -> 47
  501. 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
  502. 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
  503. } ;
  504. // gpioToClk:
  505. // (word) Offsets to the clock Control and Divisor register
  506. static uint8_t gpioToClkCon [] =
  507. {
  508. -1, -1, -1, -1, 28, 30, 32, -1, // 0 -> 7
  509. -1, -1, -1, -1, -1, -1, -1, -1, // 8 -> 15
  510. -1, -1, -1, -1, 28, 30, -1, -1, // 16 -> 23
  511. -1, -1, -1, -1, -1, -1, -1, -1, // 24 -> 31
  512. 28, -1, 28, -1, -1, -1, -1, -1, // 32 -> 39
  513. -1, -1, 28, 30, 28, -1, -1, -1, // 40 -> 47
  514. -1, -1, -1, -1, -1, -1, -1, -1, // 48 -> 55
  515. -1, -1, -1, -1, -1, -1, -1, -1, // 56 -> 63
  516. } ;
  517. static uint8_t gpioToClkDiv [] =
  518. {
  519. -1, -1, -1, -1, 29, 31, 33, -1, // 0 -> 7
  520. -1, -1, -1, -1, -1, -1, -1, -1, // 8 -> 15
  521. -1, -1, -1, -1, 29, 31, -1, -1, // 16 -> 23
  522. -1, -1, -1, -1, -1, -1, -1, -1, // 24 -> 31
  523. 29, -1, 29, -1, -1, -1, -1, -1, // 32 -> 39
  524. -1, -1, 29, 31, 29, -1, -1, -1, // 40 -> 47
  525. -1, -1, -1, -1, -1, -1, -1, -1, // 48 -> 55
  526. -1, -1, -1, -1, -1, -1, -1, -1, // 56 -> 63
  527. } ;
  528. /*
  529. * Functions
  530. *********************************************************************************
  531. */
  532. /*
  533. * wiringPiFailure:
  534. * Fail. Or not.
  535. *********************************************************************************
  536. */
  537. int wiringPiFailure (int fatal, const char *message, ...)
  538. {
  539. va_list argp ;
  540. char buffer [1024] ;
  541. if (!fatal && wiringPiReturnCodes)
  542. return -1 ;
  543. va_start (argp, message) ;
  544. vsnprintf (buffer, 1023, message, argp) ;
  545. va_end (argp) ;
  546. fprintf (stderr, "%s", buffer) ;
  547. exit (EXIT_FAILURE) ;
  548. return 0 ;
  549. }
  550. /*
  551. * setupCheck
  552. * Another sanity check because some users forget to call the setup
  553. * function. Mosty because they need feeding C drip by drip )-:
  554. *********************************************************************************
  555. */
  556. static void setupCheck (const char *fName)
  557. {
  558. if (!wiringPiSetuped)
  559. {
  560. fprintf (stderr, "%s: You have not called one of the wiringPiSetup\n"
  561. " functions, so I'm aborting your program before it crashes anyway.\n", fName) ;
  562. exit (EXIT_FAILURE) ;
  563. }
  564. }
  565. /*
  566. * gpioMemCheck:
  567. * See if we're using the /dev/gpiomem interface, if-so then some operations
  568. * can't be done and will crash the Pi.
  569. *********************************************************************************
  570. */
  571. static void usingGpioMemCheck (const char *what)
  572. {
  573. if (usingGpioMem)
  574. {
  575. fprintf (stderr, "%s: Unable to do this when using /dev/gpiomem. Try sudo?\n", what) ;
  576. exit (EXIT_FAILURE) ;
  577. }
  578. }
  579. /*
  580. * piGpioLayout:
  581. * Return a number representing the hardware revision of the board.
  582. * This is not strictly the board revision but is used to check the
  583. * layout of the GPIO connector - and there are 2 types that we are
  584. * really interested in here. The very earliest Pi's and the
  585. * ones that came after that which switched some pins ....
  586. *
  587. * Revision 1 really means the early Model A and B's.
  588. * Revision 2 is everything else - it covers the B, B+ and CM.
  589. * ... and the Pi 2 - which is a B+ ++ ...
  590. * ... and the Pi 0 - which is an A+ ...
  591. *
  592. * The main difference between the revision 1 and 2 system that I use here
  593. * is the mapping of the GPIO pins. From revision 2, the Pi Foundation changed
  594. * 3 GPIO pins on the (original) 26-way header - BCM_GPIO 22 was dropped and
  595. * replaced with 27, and 0 + 1 - I2C bus 0 was changed to 2 + 3; I2C bus 1.
  596. *
  597. * Additionally, here we set the piModel2 flag too. This is again, nothing to
  598. * do with the actual model, but the major version numbers - the GPIO base
  599. * hardware address changed at model 2 and above (not the Zero though)
  600. *
  601. *********************************************************************************
  602. */
  603. static void piGpioLayoutOops (const char *why)
  604. {
  605. fprintf (stderr, "Oops: Unable to determine board revision from /proc/cpuinfo\n") ;
  606. fprintf (stderr, " -> %s\n", why) ;
  607. fprintf (stderr, " -> You'd best google the error to find out why.\n") ;
  608. //fprintf (stderr, " -> http://www.raspberrypi.org/phpBB3/viewtopic.php?p=184410#p184410\n") ;
  609. exit (EXIT_FAILURE) ;
  610. }
  611. int piGpioLayout (void)
  612. {
  613. FILE *cpuFd ;
  614. char line [120] ;
  615. char *c ;
  616. static int gpioLayout = -1 ;
  617. if (gpioLayout != -1) // No point checking twice
  618. return gpioLayout ;
  619. if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL)
  620. piGpioLayoutOops ("Unable to open /proc/cpuinfo") ;
  621. // Start by looking for the Architecture to make sure we're really running
  622. // on a Pi. I'm getting fed-up with people whinging at me because
  623. // they can't get it to work on weirdFruitPi boards...
  624. while (fgets (line, 120, cpuFd) != NULL)
  625. if (strncmp (line, "Hardware", 8) == 0)
  626. break ;
  627. if (strncmp (line, "Hardware", 8) != 0)
  628. piGpioLayoutOops ("No \"Hardware\" line") ;
  629. if (wiringPiDebug)
  630. printf ("piGpioLayout: Hardware: %s\n", line) ;
  631. // See if it's BCM2708 or BCM2709 or the new BCM2835.
  632. // OK. As of Kernel 4.8, we have BCM2835 only, regardless of model.
  633. // However I still want to check because it will trap the cheapskates and rip-
  634. // off merchants who want to use wiringPi on non-Raspberry Pi platforms - which
  635. // I do not support so don't email me your bleating whinges about anything
  636. // other than a genuine Raspberry Pi.
  637. #ifdef DONT_CARE_ANYMORE
  638. if (! (strstr (line, "BCM2708") || strstr (line, "BCM2709") || strstr (line, "BCM2835")))
  639. {
  640. fprintf (stderr, "Unable to determine hardware version. I see: %s,\n", line) ;
  641. fprintf (stderr, " - expecting BCM2708, BCM2709 or BCM2835.\n") ;
  642. fprintf (stderr, "If this is a genuine Raspberry Pi then please report this\n") ;
  643. fprintf (stderr, "to projects@drogon.net. If this is not a Raspberry Pi then you\n") ;
  644. fprintf (stderr, "are on your own as wiringPi is designed to support the\n") ;
  645. fprintf (stderr, "Raspberry Pi ONLY.\n") ;
  646. exit (EXIT_FAILURE) ;
  647. }
  648. #endif
  649. // Actually... That has caused me more than 10,000 emails so-far. Mosty by
  650. // people who think they know better by creating a statically linked
  651. // version that will not run with a new 4.9 kernel. I utterly hate and
  652. // despise those people.
  653. //
  654. // I also get bleats from people running other than Raspbian with another
  655. // distros compiled kernel rather than a foundation compiled kernel, so
  656. // this might actually help them. It might not - I only have the capacity
  657. // to support Raspbian.
  658. //
  659. // However, I've decided to leave this check out and rely purely on the
  660. // Revision: line for now. It will not work on a non-pi hardware or weird
  661. // kernels that don't give you a suitable revision line.
  662. // So - we're Probably on a Raspberry Pi. Check the revision field for the real
  663. // hardware type
  664. // In-future, I ought to use the device tree as there are now Pi entries in
  665. // /proc/device-tree/ ...
  666. // but I'll leave that for the next revision. Or the next.
  667. // Isolate the Revision line
  668. rewind (cpuFd) ;
  669. while (fgets (line, 120, cpuFd) != NULL)
  670. if (strncmp (line, "Revision", 8) == 0)
  671. break ;
  672. fclose (cpuFd) ;
  673. if (strncmp (line, "Revision", 8) != 0)
  674. piGpioLayoutOops ("No \"Revision\" line") ;
  675. // Chomp trailing CR/NL
  676. for (c = &line [strlen (line) - 1] ; (*c == '\n') || (*c == '\r') ; --c)
  677. *c = 0 ;
  678. if (wiringPiDebug)
  679. printf ("piGpioLayout: Revision string: %s\n", line) ;
  680. // Scan to the first character of the revision number
  681. for (c = line ; *c ; ++c)
  682. if (*c == ':')
  683. break ;
  684. if (*c != ':')
  685. piGpioLayoutOops ("Bogus \"Revision\" line (no colon)") ;
  686. // Chomp spaces
  687. ++c ;
  688. while (isspace (*c))
  689. ++c ;
  690. if (!isxdigit (*c))
  691. piGpioLayoutOops ("Bogus \"Revision\" line (no hex digit at start of revision)") ;
  692. // Make sure its long enough
  693. if (strlen (c) < 4)
  694. piGpioLayoutOops ("Bogus revision line (too small)") ;
  695. // Isolate last 4 characters: (in-case of overvolting or new encoding scheme)
  696. c = c + strlen (c) - 4 ;
  697. if (wiringPiDebug)
  698. printf ("piGpioLayout: last4Chars are: \"%s\"\n", c) ;
  699. if ( (strcmp (c, "0002") == 0) || (strcmp (c, "0003") == 0))
  700. gpioLayout = 1 ;
  701. else
  702. gpioLayout = 2 ; // Covers everything else from the B revision 2 to the B+, the Pi v2, v3, zero and CM's.
  703. if (wiringPiDebug)
  704. printf ("piGpioLayoutOops: Returning revision: %d\n", gpioLayout) ;
  705. return gpioLayout ;
  706. }
  707. /*
  708. * piBoardRev:
  709. * Deprecated, but does the same as piGpioLayout
  710. *********************************************************************************
  711. */
  712. int piBoardRev (void)
  713. {
  714. return piGpioLayout () ;
  715. }
  716. /*
  717. * piBoardId:
  718. * Return the real details of the board we have.
  719. *
  720. * This is undocumented and really only intended for the GPIO command.
  721. * Use at your own risk!
  722. *
  723. * Seems there are some boards with 0000 in them (mistake in manufacture)
  724. * So the distinction between boards that I can see is:
  725. *
  726. * 0000 - Error
  727. * 0001 - Not used
  728. *
  729. * Original Pi boards:
  730. * 0002 - Model B, Rev 1, 256MB, Egoman
  731. * 0003 - Model B, Rev 1.1, 256MB, Egoman, Fuses/D14 removed.
  732. *
  733. * Newer Pi's with remapped GPIO:
  734. * 0004 - Model B, Rev 1.2, 256MB, Sony
  735. * 0005 - Model B, Rev 1.2, 256MB, Egoman
  736. * 0006 - Model B, Rev 1.2, 256MB, Egoman
  737. *
  738. * 0007 - Model A, Rev 1.2, 256MB, Egoman
  739. * 0008 - Model A, Rev 1.2, 256MB, Sony
  740. * 0009 - Model A, Rev 1.2, 256MB, Egoman
  741. *
  742. * 000d - Model B, Rev 1.2, 512MB, Egoman (Red Pi, Blue Pi?)
  743. * 000e - Model B, Rev 1.2, 512MB, Sony
  744. * 000f - Model B, Rev 1.2, 512MB, Egoman
  745. *
  746. * 0010 - Model B+, Rev 1.2, 512MB, Sony
  747. * 0013 - Model B+ Rev 1.2, 512MB, Embest
  748. * 0016 - Model B+ Rev 1.2, 512MB, Sony
  749. * 0019 - Model B+ Rev 1.2, 512MB, Egoman
  750. *
  751. * 0011 - Pi CM, Rev 1.1, 512MB, Sony
  752. * 0014 - Pi CM, Rev 1.1, 512MB, Embest
  753. * 0017 - Pi CM, Rev 1.1, 512MB, Sony
  754. * 001a - Pi CM, Rev 1.1, 512MB, Egoman
  755. *
  756. * 0012 - Model A+ Rev 1.1, 256MB, Sony
  757. * 0015 - Model A+ Rev 1.1, 512MB, Embest
  758. * 0018 - Model A+ Rev 1.1, 256MB, Sony
  759. * 001b - Model A+ Rev 1.1, 256MB, Egoman
  760. *
  761. * A small thorn is the olde style overvolting - that will add in
  762. * 1000000
  763. *
  764. * The Pi compute module has an revision of 0011 or 0014 - since we only
  765. * check the last digit, then it's 1, therefore it'll default to not 2 or
  766. * 3 for a Rev 1, so will appear as a Rev 2. This is fine for the most part, but
  767. * we'll properly detect the Compute Module later and adjust accordingly.
  768. *
  769. * And then things changed with the introduction of the v2...
  770. *
  771. * For Pi v2 and subsequent models - e.g. the Zero:
  772. *
  773. * [USER:8] [NEW:1] [MEMSIZE:3] [MANUFACTURER:4] [PROCESSOR:4] [TYPE:8] [REV:4]
  774. * NEW 23: will be 1 for the new scheme, 0 for the old scheme
  775. * MEMSIZE 20: 0=256M 1=512M 2=1G
  776. * MANUFACTURER 16: 0=SONY 1=EGOMAN 2=EMBEST
  777. * PROCESSOR 12: 0=2835 1=2836
  778. * TYPE 04: 0=MODELA 1=MODELB 2=MODELA+ 3=MODELB+ 4=Pi2 MODEL B 5=ALPHA 6=CM
  779. * REV 00: 0=REV0 1=REV1 2=REV2
  780. *********************************************************************************
  781. */
  782. void piBoardId (int *model, int *rev, int *mem, int *maker, int *warranty)
  783. {
  784. FILE *cpuFd ;
  785. char line [120] ;
  786. char *c ;
  787. unsigned int revision ;
  788. int bRev, bType, bProc, bMfg, bMem, bWarranty ;
  789. // Will deal with the properly later on - for now, lets just get it going...
  790. // unsigned int modelNum ;
  791. (void)piGpioLayout () ; // Call this first to make sure all's OK. Don't care about the result.
  792. if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL)
  793. piGpioLayoutOops ("Unable to open /proc/cpuinfo") ;
  794. while (fgets (line, 120, cpuFd) != NULL)
  795. if (strncmp (line, "Revision", 8) == 0)
  796. break ;
  797. fclose (cpuFd) ;
  798. if (strncmp (line, "Revision", 8) != 0)
  799. piGpioLayoutOops ("No \"Revision\" line") ;
  800. // Chomp trailing CR/NL
  801. for (c = &line [strlen (line) - 1] ; (*c == '\n') || (*c == '\r') ; --c)
  802. *c = 0 ;
  803. if (wiringPiDebug)
  804. printf ("piBoardId: Revision string: %s\n", line) ;
  805. // Need to work out if it's using the new or old encoding scheme:
  806. // Scan to the first character of the revision number
  807. for (c = line ; *c ; ++c)
  808. if (*c == ':')
  809. break ;
  810. if (*c != ':')
  811. piGpioLayoutOops ("Bogus \"Revision\" line (no colon)") ;
  812. // Chomp spaces
  813. ++c ;
  814. while (isspace (*c))
  815. ++c ;
  816. if (!isxdigit (*c))
  817. piGpioLayoutOops ("Bogus \"Revision\" line (no hex digit at start of revision)") ;
  818. revision = (unsigned int)strtol (c, NULL, 16) ; // Hex number with no leading 0x
  819. // Check for new way:
  820. if ((revision & (1 << 23)) != 0) // New way
  821. {
  822. if (wiringPiDebug)
  823. printf ("piBoardId: New Way: revision is: %08X\n", revision) ;
  824. bRev = (revision & (0x0F << 0)) >> 0 ;
  825. bType = (revision & (0xFF << 4)) >> 4 ;
  826. bProc = (revision & (0x0F << 12)) >> 12 ; // Not used for now.
  827. bMfg = (revision & (0x0F << 16)) >> 16 ;
  828. bMem = (revision & (0x07 << 20)) >> 20 ;
  829. bWarranty = (revision & (0x03 << 24)) != 0 ;
  830. *model = bType ;
  831. *rev = bRev ;
  832. *mem = bMem ;
  833. *maker = bMfg ;
  834. *warranty = bWarranty ;
  835. if (wiringPiDebug)
  836. printf ("piBoardId: rev: %d, type: %d, proc: %d, mfg: %d, mem: %d, warranty: %d\n",
  837. bRev, bType, bProc, bMfg, bMem, bWarranty) ;
  838. }
  839. else // Old way
  840. {
  841. if (wiringPiDebug)
  842. printf ("piBoardId: Old Way: revision is: %s\n", c) ;
  843. if (!isdigit (*c))
  844. piGpioLayoutOops ("Bogus \"Revision\" line (no digit at start of revision)") ;
  845. // Make sure its long enough
  846. if (strlen (c) < 4)
  847. piGpioLayoutOops ("Bogus \"Revision\" line (not long enough)") ;
  848. // If longer than 4, we'll assume it's been overvolted
  849. *warranty = strlen (c) > 4 ;
  850. // Extract last 4 characters:
  851. c = c + strlen (c) - 4 ;
  852. // Fill out the replys as appropriate
  853. /**/ if (strcmp (c, "0002") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
  854. else if (strcmp (c, "0003") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_1 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
  855. else if (strcmp (c, "0004") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_2 ; *mem = 0 ; *maker = PI_MAKER_SONY ; }
  856. else if (strcmp (c, "0005") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_2 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
  857. else if (strcmp (c, "0006") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_2 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
  858. else if (strcmp (c, "0007") == 0) { *model = PI_MODEL_A ; *rev = PI_VERSION_1_2 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
  859. else if (strcmp (c, "0008") == 0) { *model = PI_MODEL_A ; *rev = PI_VERSION_1_2 ; *mem = 0 ; *maker = PI_MAKER_SONY ; ; }
  860. else if (strcmp (c, "0009") == 0) { *model = PI_MODEL_A ; *rev = PI_VERSION_1_2 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
  861. else if (strcmp (c, "000d") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_EGOMAN ; }
  862. else if (strcmp (c, "000e") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_SONY ; }
  863. else if (strcmp (c, "000f") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_EGOMAN ; }
  864. else if (strcmp (c, "0010") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_SONY ; }
  865. else if (strcmp (c, "0013") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_EMBEST ; }
  866. else if (strcmp (c, "0016") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_SONY ; }
  867. else if (strcmp (c, "0019") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_EGOMAN ; }
  868. else if (strcmp (c, "0011") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_1 ; *mem = 1 ; *maker = PI_MAKER_SONY ; }
  869. else if (strcmp (c, "0014") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_1 ; *mem = 1 ; *maker = PI_MAKER_EMBEST ; }
  870. else if (strcmp (c, "0017") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_1 ; *mem = 1 ; *maker = PI_MAKER_SONY ; }
  871. else if (strcmp (c, "001a") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_1 ; *mem = 1 ; *maker = PI_MAKER_EGOMAN ; }
  872. else if (strcmp (c, "0012") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_1 ; *mem = 0 ; *maker = PI_MAKER_SONY ; }
  873. else if (strcmp (c, "0015") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_1 ; *mem = 1 ; *maker = PI_MAKER_EMBEST ; }
  874. else if (strcmp (c, "0018") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_1 ; *mem = 0 ; *maker = PI_MAKER_SONY ; }
  875. else if (strcmp (c, "001b") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_1 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
  876. else { *model = 0 ; *rev = 0 ; *mem = 0 ; *maker = 0 ; }
  877. }
  878. }
  879. /*
  880. * wpiPinToGpio:
  881. * Translate a wiringPi Pin number to native GPIO pin number.
  882. * Provided for external support.
  883. *********************************************************************************
  884. */
  885. int wpiPinToGpio (int wpiPin)
  886. {
  887. return pinToGpio [wpiPin & 63] ;
  888. }
  889. /*
  890. * physPinToGpio:
  891. * Translate a physical Pin number to native GPIO pin number.
  892. * Provided for external support.
  893. *********************************************************************************
  894. */
  895. int physPinToGpio (int physPin)
  896. {
  897. return physToGpio [physPin & 63] ;
  898. }
  899. /*
  900. * setPadDrive:
  901. * Set the PAD driver value
  902. *********************************************************************************
  903. */
  904. void setPadDrive (int group, int value)
  905. {
  906. uint32_t wrVal ;
  907. if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
  908. {
  909. if ((group < 0) || (group > 2))
  910. return ;
  911. wrVal = BCM_PASSWORD | 0x18 | (value & 7) ;
  912. *(pads + group + 11) = wrVal ;
  913. if (wiringPiDebug)
  914. {
  915. printf ("setPadDrive: Group: %d, value: %d (%08X)\n", group, value, wrVal) ;
  916. printf ("Read : %08X\n", *(pads + group + 11)) ;
  917. }
  918. }
  919. }
  920. /*
  921. * getAlt:
  922. * Returns the ALT bits for a given port. Only really of-use
  923. * for the gpio readall command (I think)
  924. *********************************************************************************
  925. */
  926. int getAlt (int pin)
  927. {
  928. int fSel, shift, alt ;
  929. pin &= 63 ;
  930. /**/ if (wiringPiMode == WPI_MODE_PINS)
  931. pin = pinToGpio [pin] ;
  932. else if (wiringPiMode == WPI_MODE_PHYS)
  933. pin = physToGpio [pin] ;
  934. else if (wiringPiMode != WPI_MODE_GPIO)
  935. return 0 ;
  936. fSel = gpioToGPFSEL [pin] ;
  937. shift = gpioToShift [pin] ;
  938. alt = (*(gpio + fSel) >> shift) & 7 ;
  939. return alt ;
  940. }
  941. /*
  942. * pwmSetMode:
  943. * Select the native "balanced" mode, or standard mark:space mode
  944. *********************************************************************************
  945. */
  946. void pwmSetMode (int mode)
  947. {
  948. if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
  949. {
  950. if (mode == PWM_MODE_MS)
  951. *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE | PWM0_MS_MODE | PWM1_MS_MODE ;
  952. else
  953. *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ;
  954. }
  955. }
  956. /*
  957. * pwmSetRange:
  958. * Set the PWM range register. We set both range registers to the same
  959. * value. If you want different in your own code, then write your own.
  960. *********************************************************************************
  961. */
  962. void pwmSetRange (unsigned int range)
  963. {
  964. if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
  965. {
  966. *(pwm + PWM0_RANGE) = range ; delayMicroseconds (10) ;
  967. *(pwm + PWM1_RANGE) = range ; delayMicroseconds (10) ;
  968. }
  969. }
  970. /*
  971. * pwmSetClock:
  972. * Set/Change the PWM clock. Originally my code, but changed
  973. * (for the better!) by Chris Hall, <chris@kchall.plus.com>
  974. * after further study of the manual and testing with a 'scope
  975. *********************************************************************************
  976. */
  977. void pwmSetClock (int divisor)
  978. {
  979. uint32_t pwm_control ;
  980. if (piGpioBase == GPIO_PERI_BASE_2711)
  981. {
  982. divisor = 540*divisor/192;
  983. }
  984. divisor &= 4095 ;
  985. if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
  986. {
  987. if (wiringPiDebug)
  988. printf ("Setting to: %d. Current: 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;
  989. pwm_control = *(pwm + PWM_CONTROL) ; // preserve PWM_CONTROL
  990. // We need to stop PWM prior to stopping PWM clock in MS mode otherwise BUSY
  991. // stays high.
  992. *(pwm + PWM_CONTROL) = 0 ; // Stop PWM
  993. // Stop PWM clock before changing divisor. The delay after this does need to
  994. // this big (95uS occasionally fails, 100uS OK), it's almost as though the BUSY
  995. // flag is not working properly in balanced mode. Without the delay when DIV is
  996. // adjusted the clock sometimes switches to very slow, once slow further DIV
  997. // adjustments do nothing and it's difficult to get out of this mode.
  998. *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x01 ; // Stop PWM Clock
  999. delayMicroseconds (110) ; // prevents clock going sloooow
  1000. while ((*(clk + PWMCLK_CNTL) & 0x80) != 0) // Wait for clock to be !BUSY
  1001. delayMicroseconds (1) ;
  1002. *(clk + PWMCLK_DIV) = BCM_PASSWORD | (divisor << 12) ;
  1003. *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x11 ; // Start PWM clock
  1004. *(pwm + PWM_CONTROL) = pwm_control ; // restore PWM_CONTROL
  1005. if (wiringPiDebug)
  1006. printf ("Set to: %d. Now : 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;
  1007. }
  1008. }
  1009. /*
  1010. * gpioClockSet:
  1011. * Set the frequency on a GPIO clock pin
  1012. *********************************************************************************
  1013. */
  1014. void gpioClockSet (int pin, int freq)
  1015. {
  1016. int divi, divr, divf ;
  1017. pin &= 63 ;
  1018. /**/ if (wiringPiMode == WPI_MODE_PINS)
  1019. pin = pinToGpio [pin] ;
  1020. else if (wiringPiMode == WPI_MODE_PHYS)
  1021. pin = physToGpio [pin] ;
  1022. else if (wiringPiMode != WPI_MODE_GPIO)
  1023. return ;
  1024. divi = 19200000 / freq ;
  1025. divr = 19200000 % freq ;
  1026. divf = (int)((double)divr * 4096.0 / 19200000.0) ;
  1027. if (divi > 4095)
  1028. divi = 4095 ;
  1029. *(clk + gpioToClkCon [pin]) = BCM_PASSWORD | GPIO_CLOCK_SOURCE ; // Stop GPIO Clock
  1030. while ((*(clk + gpioToClkCon [pin]) & 0x80) != 0) // ... and wait
  1031. ;
  1032. *(clk + gpioToClkDiv [pin]) = BCM_PASSWORD | (divi << 12) | divf ; // Set dividers
  1033. *(clk + gpioToClkCon [pin]) = BCM_PASSWORD | 0x10 | GPIO_CLOCK_SOURCE ; // Start Clock
  1034. }
  1035. /*
  1036. * wiringPiFindNode:
  1037. * Locate our device node
  1038. *********************************************************************************
  1039. */
  1040. struct wiringPiNodeStruct *wiringPiFindNode (int pin)
  1041. {
  1042. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1043. while (node != NULL)
  1044. if ((pin >= node->pinBase) && (pin <= node->pinMax))
  1045. return node ;
  1046. else
  1047. node = node->next ;
  1048. return NULL ;
  1049. }
  1050. /*
  1051. * wiringPiNewNode:
  1052. * Create a new GPIO node into the wiringPi handling system
  1053. *********************************************************************************
  1054. */
  1055. static void pinModeDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int mode) { return ; }
  1056. static void pullUpDnControlDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int pud) { return ; }
  1057. static unsigned int digitalRead8Dummy (UNU struct wiringPiNodeStruct *node, UNU int UNU pin) { return 0 ; }
  1058. static void digitalWrite8Dummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int value) { return ; }
  1059. static int digitalReadDummy (UNU struct wiringPiNodeStruct *node, UNU int UNU pin) { return LOW ; }
  1060. static void digitalWriteDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int value) { return ; }
  1061. static void pwmWriteDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int value) { return ; }
  1062. static int analogReadDummy (UNU struct wiringPiNodeStruct *node, UNU int pin) { return 0 ; }
  1063. static void analogWriteDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int value) { return ; }
  1064. struct wiringPiNodeStruct *wiringPiNewNode (int pinBase, int numPins)
  1065. {
  1066. int pin ;
  1067. struct wiringPiNodeStruct *node ;
  1068. // Minimum pin base is 64
  1069. if (pinBase < 64)
  1070. (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: pinBase of %d is < 64\n", pinBase) ;
  1071. // Check all pins in-case there is overlap:
  1072. for (pin = pinBase ; pin < (pinBase + numPins) ; ++pin)
  1073. if (wiringPiFindNode (pin) != NULL)
  1074. (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: Pin %d overlaps with existing definition\n", pin) ;
  1075. node = (struct wiringPiNodeStruct *)calloc (sizeof (struct wiringPiNodeStruct), 1) ; // calloc zeros
  1076. if (node == NULL)
  1077. (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: Unable to allocate memory: %s\n", strerror (errno)) ;
  1078. node->pinBase = pinBase ;
  1079. node->pinMax = pinBase + numPins - 1 ;
  1080. node->pinMode = pinModeDummy ;
  1081. node->pullUpDnControl = pullUpDnControlDummy ;
  1082. node->digitalRead = digitalReadDummy ;
  1083. //node->digitalRead8 = digitalRead8Dummy ;
  1084. node->digitalWrite = digitalWriteDummy ;
  1085. //node->digitalWrite8 = digitalWrite8Dummy ;
  1086. node->pwmWrite = pwmWriteDummy ;
  1087. node->analogRead = analogReadDummy ;
  1088. node->analogWrite = analogWriteDummy ;
  1089. node->next = wiringPiNodes ;
  1090. wiringPiNodes = node ;
  1091. return node ;
  1092. }
  1093. #ifdef notYetReady
  1094. /*
  1095. * pinED01:
  1096. * pinED10:
  1097. * Enables edge-detect mode on a pin - from a 0 to a 1 or 1 to 0
  1098. * Pin must already be in input mode with appropriate pull up/downs set.
  1099. *********************************************************************************
  1100. */
  1101. void pinEnableED01Pi (int pin)
  1102. {
  1103. pin = pinToGpio [pin & 63] ;
  1104. }
  1105. #endif
  1106. /*
  1107. *********************************************************************************
  1108. * Core Functions
  1109. *********************************************************************************
  1110. */
  1111. /*
  1112. * pinModeAlt:
  1113. * This is an un-documented special to let you set any pin to any mode
  1114. *********************************************************************************
  1115. */
  1116. void pinModeAlt (int pin, int mode)
  1117. {
  1118. int fSel, shift ;
  1119. setupCheck ("pinModeAlt") ;
  1120. if ((pin & PI_GPIO_MASK) == 0) // On-board pin
  1121. {
  1122. /**/ if (wiringPiMode == WPI_MODE_PINS)
  1123. pin = pinToGpio [pin] ;
  1124. else if (wiringPiMode == WPI_MODE_PHYS)
  1125. pin = physToGpio [pin] ;
  1126. else if (wiringPiMode != WPI_MODE_GPIO)
  1127. return ;
  1128. fSel = gpioToGPFSEL [pin] ;
  1129. shift = gpioToShift [pin] ;
  1130. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | ((mode & 0x7) << shift) ;
  1131. }
  1132. }
  1133. /*
  1134. * pinMode:
  1135. * Sets the mode of a pin to be input, output or PWM output
  1136. *********************************************************************************
  1137. */
  1138. void pinMode (int pin, int mode)
  1139. {
  1140. int fSel, shift, alt ;
  1141. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1142. int origPin = pin ;
  1143. setupCheck ("pinMode") ;
  1144. if ((pin & PI_GPIO_MASK) == 0) // On-board pin
  1145. {
  1146. /**/ if (wiringPiMode == WPI_MODE_PINS)
  1147. pin = pinToGpio [pin] ;
  1148. else if (wiringPiMode == WPI_MODE_PHYS)
  1149. pin = physToGpio [pin] ;
  1150. else if (wiringPiMode != WPI_MODE_GPIO)
  1151. return ;
  1152. softPwmStop (origPin) ;
  1153. softToneStop (origPin) ;
  1154. fSel = gpioToGPFSEL [pin] ;
  1155. shift = gpioToShift [pin] ;
  1156. /**/ if (mode == INPUT)
  1157. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) ; // Sets bits to zero = input
  1158. else if (mode == OUTPUT)
  1159. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (1 << shift) ;
  1160. else if (mode == SOFT_PWM_OUTPUT)
  1161. softPwmCreate (origPin, 0, 100) ;
  1162. else if (mode == SOFT_TONE_OUTPUT)
  1163. softToneCreate (origPin) ;
  1164. else if (mode == PWM_TONE_OUTPUT)
  1165. {
  1166. pinMode (origPin, PWM_OUTPUT) ; // Call myself to enable PWM mode
  1167. pwmSetMode (PWM_MODE_MS) ;
  1168. }
  1169. else if (mode == PWM_OUTPUT)
  1170. {
  1171. if ((alt = gpioToPwmALT [pin]) == 0) // Not a hardware capable PWM pin
  1172. return ;
  1173. usingGpioMemCheck ("pinMode PWM") ;
  1174. // Set pin to PWM mode
  1175. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ;
  1176. delayMicroseconds (110) ; // See comments in pwmSetClockWPi
  1177. pwmSetMode (PWM_MODE_BAL) ; // Pi default mode
  1178. pwmSetRange (1024) ; // Default range of 1024
  1179. pwmSetClock (32) ; // 19.2 / 32 = 600KHz - Also starts the PWM
  1180. }
  1181. else if (mode == GPIO_CLOCK)
  1182. {
  1183. if ((alt = gpioToGpClkALT0 [pin]) == 0) // Not a GPIO_CLOCK pin
  1184. return ;
  1185. usingGpioMemCheck ("pinMode CLOCK") ;
  1186. // Set pin to GPIO_CLOCK mode and set the clock frequency to 100KHz
  1187. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ;
  1188. delayMicroseconds (110) ;
  1189. gpioClockSet (pin, 100000) ;
  1190. }
  1191. }
  1192. else
  1193. {
  1194. if ((node = wiringPiFindNode (pin)) != NULL)
  1195. node->pinMode (node, pin, mode) ;
  1196. return ;
  1197. }
  1198. }
  1199. /*
  1200. * pullUpDownCtrl:
  1201. * Control the internal pull-up/down resistors on a GPIO pin.
  1202. *********************************************************************************
  1203. */
  1204. void pullUpDnControl (int pin, int pud)
  1205. {
  1206. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1207. setupCheck ("pullUpDnControl") ;
  1208. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1209. {
  1210. /**/ if (wiringPiMode == WPI_MODE_PINS)
  1211. pin = pinToGpio [pin] ;
  1212. else if (wiringPiMode == WPI_MODE_PHYS)
  1213. pin = physToGpio [pin] ;
  1214. else if (wiringPiMode != WPI_MODE_GPIO)
  1215. return ;
  1216. if (piGpioPupOffset == GPPUPPDN0)
  1217. {
  1218. // Pi 4B pull up/down method
  1219. int pullreg = GPPUPPDN0 + (pin>>4);
  1220. int pullshift = (pin & 0xf) << 1;
  1221. unsigned int pullbits;
  1222. unsigned int pull;
  1223. switch (pud)
  1224. {
  1225. case PUD_OFF: pull = 0; break;
  1226. case PUD_UP: pull = 1; break;
  1227. case PUD_DOWN: pull = 2; break;
  1228. default: return ; /* An illegal value */
  1229. }
  1230. pullbits = *(gpio + pullreg);
  1231. pullbits &= ~(3 << pullshift);
  1232. pullbits |= (pull << pullshift);
  1233. *(gpio + pullreg) = pullbits;
  1234. }
  1235. else
  1236. {
  1237. // legacy pull up/down method
  1238. *(gpio + GPPUD) = pud & 3 ; delayMicroseconds (5) ;
  1239. *(gpio + gpioToPUDCLK [pin]) = 1 << (pin & 31) ; delayMicroseconds (5) ;
  1240. *(gpio + GPPUD) = 0 ; delayMicroseconds (5) ;
  1241. *(gpio + gpioToPUDCLK [pin]) = 0 ; delayMicroseconds (5) ;
  1242. }
  1243. }
  1244. else // Extension module
  1245. {
  1246. if ((node = wiringPiFindNode (pin)) != NULL)
  1247. node->pullUpDnControl (node, pin, pud) ;
  1248. return ;
  1249. }
  1250. }
  1251. /*
  1252. * digitalRead:
  1253. * Read the value of a given Pin, returning HIGH or LOW
  1254. *********************************************************************************
  1255. */
  1256. int digitalRead (int pin)
  1257. {
  1258. char c ;
  1259. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1260. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1261. {
  1262. /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS) // Sys mode
  1263. {
  1264. if (sysFds [pin] == -1)
  1265. return LOW ;
  1266. lseek (sysFds [pin], 0L, SEEK_SET) ;
  1267. read (sysFds [pin], &c, 1) ;
  1268. return (c == '0') ? LOW : HIGH ;
  1269. }
  1270. else if (wiringPiMode == WPI_MODE_PINS)
  1271. pin = pinToGpio [pin] ;
  1272. else if (wiringPiMode == WPI_MODE_PHYS)
  1273. pin = physToGpio [pin] ;
  1274. else if (wiringPiMode != WPI_MODE_GPIO)
  1275. return LOW ;
  1276. if ((*(gpio + gpioToGPLEV [pin]) & (1 << (pin & 31))) != 0)
  1277. return HIGH ;
  1278. else
  1279. return LOW ;
  1280. }
  1281. else
  1282. {
  1283. if ((node = wiringPiFindNode (pin)) == NULL)
  1284. return LOW ;
  1285. return node->digitalRead (node, pin) ;
  1286. }
  1287. }
  1288. /*
  1289. * digitalRead8:
  1290. * Read 8-bits (a byte) from given start pin.
  1291. *********************************************************************************
  1292. unsigned int digitalRead8 (int pin)
  1293. {
  1294. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1295. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1296. return 0 ;
  1297. else
  1298. {
  1299. if ((node = wiringPiFindNode (pin)) == NULL)
  1300. return LOW ;
  1301. return node->digitalRead8 (node, pin) ;
  1302. }
  1303. }
  1304. */
  1305. /*
  1306. * digitalWrite:
  1307. * Set an output bit
  1308. *********************************************************************************
  1309. */
  1310. void digitalWrite (int pin, int value)
  1311. {
  1312. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1313. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1314. {
  1315. /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS) // Sys mode
  1316. {
  1317. if (sysFds [pin] != -1)
  1318. {
  1319. if (value == LOW)
  1320. write (sysFds [pin], "0\n", 2) ;
  1321. else
  1322. write (sysFds [pin], "1\n", 2) ;
  1323. }
  1324. return ;
  1325. }
  1326. else if (wiringPiMode == WPI_MODE_PINS)
  1327. pin = pinToGpio [pin] ;
  1328. else if (wiringPiMode == WPI_MODE_PHYS)
  1329. pin = physToGpio [pin] ;
  1330. else if (wiringPiMode != WPI_MODE_GPIO)
  1331. return ;
  1332. if (value == LOW)
  1333. *(gpio + gpioToGPCLR [pin]) = 1 << (pin & 31) ;
  1334. else
  1335. *(gpio + gpioToGPSET [pin]) = 1 << (pin & 31) ;
  1336. }
  1337. else
  1338. {
  1339. if ((node = wiringPiFindNode (pin)) != NULL)
  1340. node->digitalWrite (node, pin, value) ;
  1341. }
  1342. }
  1343. /*
  1344. * digitalWrite8:
  1345. * Set an output 8-bit byte on the device from the given pin number
  1346. *********************************************************************************
  1347. void digitalWrite8 (int pin, int value)
  1348. {
  1349. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1350. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1351. return ;
  1352. else
  1353. {
  1354. if ((node = wiringPiFindNode (pin)) != NULL)
  1355. node->digitalWrite8 (node, pin, value) ;
  1356. }
  1357. }
  1358. */
  1359. /*
  1360. * pwmWrite:
  1361. * Set an output PWM value
  1362. *********************************************************************************
  1363. */
  1364. void pwmWrite (int pin, int value)
  1365. {
  1366. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1367. setupCheck ("pwmWrite") ;
  1368. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1369. {
  1370. /**/ if (wiringPiMode == WPI_MODE_PINS)
  1371. pin = pinToGpio [pin] ;
  1372. else if (wiringPiMode == WPI_MODE_PHYS)
  1373. pin = physToGpio [pin] ;
  1374. else if (wiringPiMode != WPI_MODE_GPIO)
  1375. return ;
  1376. usingGpioMemCheck ("pwmWrite") ;
  1377. *(pwm + gpioToPwmPort [pin]) = value ;
  1378. }
  1379. else
  1380. {
  1381. if ((node = wiringPiFindNode (pin)) != NULL)
  1382. node->pwmWrite (node, pin, value) ;
  1383. }
  1384. }
  1385. /*
  1386. * analogRead:
  1387. * Read the analog value of a given Pin.
  1388. * There is no on-board Pi analog hardware,
  1389. * so this needs to go to a new node.
  1390. *********************************************************************************
  1391. */
  1392. int analogRead (int pin)
  1393. {
  1394. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1395. if ((node = wiringPiFindNode (pin)) == NULL)
  1396. return 0 ;
  1397. else
  1398. return node->analogRead (node, pin) ;
  1399. }
  1400. /*
  1401. * analogWrite:
  1402. * Write the analog value to the given Pin.
  1403. * There is no on-board Pi analog hardware,
  1404. * so this needs to go to a new node.
  1405. *********************************************************************************
  1406. */
  1407. void analogWrite (int pin, int value)
  1408. {
  1409. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1410. if ((node = wiringPiFindNode (pin)) == NULL)
  1411. return ;
  1412. node->analogWrite (node, pin, value) ;
  1413. }
  1414. /*
  1415. * pwmToneWrite:
  1416. * Pi Specific.
  1417. * Output the given frequency on the Pi's PWM pin
  1418. *********************************************************************************
  1419. */
  1420. void pwmToneWrite (int pin, int freq)
  1421. {
  1422. int range ;
  1423. setupCheck ("pwmToneWrite") ;
  1424. if (freq == 0)
  1425. pwmWrite (pin, 0) ; // Off
  1426. else
  1427. {
  1428. range = 600000 / freq ;
  1429. pwmSetRange (range) ;
  1430. pwmWrite (pin, freq / 2) ;
  1431. }
  1432. }
  1433. /*
  1434. * digitalWriteByte:
  1435. * digitalReadByte:
  1436. * Pi Specific
  1437. * Write an 8-bit byte to the first 8 GPIO pins - try to do it as
  1438. * fast as possible.
  1439. * However it still needs 2 operations to set the bits, so any external
  1440. * hardware must not rely on seeing a change as there will be a change
  1441. * to set the outputs bits to zero, then another change to set the 1's
  1442. * Reading is just bit fiddling.
  1443. * These are wiringPi pin numbers 0..7, or BCM_GPIO pin numbers
  1444. * 17, 18, 22, 23, 24, 24, 4 on a Pi v1 rev 0-3
  1445. * 17, 18, 27, 23, 24, 24, 4 on a Pi v1 rev 3 onwards or B+, 2, 3, zero
  1446. *********************************************************************************
  1447. */
  1448. void digitalWriteByte (const int value)
  1449. {
  1450. uint32_t pinSet = 0 ;
  1451. uint32_t pinClr = 0 ;
  1452. int mask = 1 ;
  1453. int pin ;
  1454. /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS)
  1455. {
  1456. for (pin = 0 ; pin < 8 ; ++pin)
  1457. {
  1458. digitalWrite (pinToGpio [pin], value & mask) ;
  1459. mask <<= 1 ;
  1460. }
  1461. return ;
  1462. }
  1463. else
  1464. {
  1465. for (pin = 0 ; pin < 8 ; ++pin)
  1466. {
  1467. if ((value & mask) == 0)
  1468. pinClr |= (1 << pinToGpio [pin]) ;
  1469. else
  1470. pinSet |= (1 << pinToGpio [pin]) ;
  1471. mask <<= 1 ;
  1472. }
  1473. *(gpio + gpioToGPCLR [0]) = pinClr ;
  1474. *(gpio + gpioToGPSET [0]) = pinSet ;
  1475. }
  1476. }
  1477. unsigned int digitalReadByte (void)
  1478. {
  1479. int pin, x ;
  1480. uint32_t raw ;
  1481. uint32_t data = 0 ;
  1482. /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS)
  1483. {
  1484. for (pin = 0 ; pin < 8 ; ++pin)
  1485. {
  1486. x = digitalRead (pinToGpio [pin]) ;
  1487. data = (data << 1) | x ;
  1488. }
  1489. }
  1490. else
  1491. {
  1492. raw = *(gpio + gpioToGPLEV [0]) ; // First bank for these pins
  1493. for (pin = 0 ; pin < 8 ; ++pin)
  1494. {
  1495. x = pinToGpio [pin] ;
  1496. data = (data << 1) | (((raw & (1 << x)) == 0) ? 0 : 1) ;
  1497. }
  1498. }
  1499. return data ;
  1500. }
  1501. /*
  1502. * digitalWriteByte2:
  1503. * digitalReadByte2:
  1504. * Pi Specific
  1505. * Write an 8-bit byte to the second set of 8 GPIO pins. This is marginally
  1506. * faster than the first lot as these are consecutive BCM_GPIO pin numbers.
  1507. * However they overlap with the original read/write bytes.
  1508. *********************************************************************************
  1509. */
  1510. void digitalWriteByte2 (const int value)
  1511. {
  1512. register int mask = 1 ;
  1513. register int pin ;
  1514. /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS)
  1515. {
  1516. for (pin = 20 ; pin < 28 ; ++pin)
  1517. {
  1518. digitalWrite (pin, value & mask) ;
  1519. mask <<= 1 ;
  1520. }
  1521. return ;
  1522. }
  1523. else
  1524. {
  1525. *(gpio + gpioToGPCLR [0]) = (~value & 0xFF) << 20 ; // 0x0FF00000; ILJ > CHANGE: Old causes glitch
  1526. *(gpio + gpioToGPSET [0]) = ( value & 0xFF) << 20 ;
  1527. }
  1528. }
  1529. unsigned int digitalReadByte2 (void)
  1530. {
  1531. int pin, x ;
  1532. uint32_t data = 0 ;
  1533. /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS)
  1534. {
  1535. for (pin = 20 ; pin < 28 ; ++pin)
  1536. {
  1537. x = digitalRead (pin) ;
  1538. data = (data << 1) | x ;
  1539. }
  1540. }
  1541. else
  1542. data = ((*(gpio + gpioToGPLEV [0])) >> 20) & 0xFF ; // First bank for these pins
  1543. return data ;
  1544. }
  1545. /*
  1546. * waitForInterrupt:
  1547. * Pi Specific.
  1548. * Wait for Interrupt on a GPIO pin.
  1549. * This is actually done via the /sys/class/gpio interface regardless of
  1550. * the wiringPi access mode in-use. Maybe sometime it might get a better
  1551. * way for a bit more efficiency.
  1552. *********************************************************************************
  1553. */
  1554. int waitForInterrupt (int pin, int mS)
  1555. {
  1556. int fd, x ;
  1557. uint8_t c ;
  1558. struct pollfd polls ;
  1559. /**/ if (wiringPiMode == WPI_MODE_PINS)
  1560. pin = pinToGpio [pin] ;
  1561. else if (wiringPiMode == WPI_MODE_PHYS)
  1562. pin = physToGpio [pin] ;
  1563. if ((fd = sysFds [pin]) == -1)
  1564. return -2 ;
  1565. // Setup poll structure
  1566. polls.fd = fd ;
  1567. polls.events = POLLPRI | POLLERR ;
  1568. // Wait for it ...
  1569. x = poll (&polls, 1, mS) ;
  1570. // If no error, do a dummy read to clear the interrupt
  1571. // A one character read appars to be enough.
  1572. if (x > 0)
  1573. {
  1574. lseek (fd, 0, SEEK_SET) ; // Rewind
  1575. (void)read (fd, &c, 1) ; // Read & clear
  1576. }
  1577. return x ;
  1578. }
  1579. /*
  1580. * interruptHandler:
  1581. * This is a thread and gets started to wait for the interrupt we're
  1582. * hoping to catch. It will call the user-function when the interrupt
  1583. * fires.
  1584. *********************************************************************************
  1585. */
  1586. static void *interruptHandler (UNU void *arg)
  1587. {
  1588. int myPin ;
  1589. (void)piHiPri (55) ; // Only effective if we run as root
  1590. myPin = pinPass ;
  1591. pinPass = -1 ;
  1592. for (;;)
  1593. if (waitForInterrupt (myPin, -1) > 0)
  1594. isrFunctions [myPin] () ;
  1595. return NULL ;
  1596. }
  1597. /*
  1598. * wiringPiISR:
  1599. * Pi Specific.
  1600. * Take the details and create an interrupt handler that will do a call-
  1601. * back to the user supplied function.
  1602. *********************************************************************************
  1603. */
  1604. int wiringPiISR (int pin, int mode, void (*function)(void))
  1605. {
  1606. pthread_t threadId ;
  1607. const char *modeS ;
  1608. char fName [64] ;
  1609. char pinS [8] ;
  1610. pid_t pid ;
  1611. int count, i ;
  1612. char c ;
  1613. int bcmGpioPin ;
  1614. if ((pin < 0) || (pin > 63))
  1615. return wiringPiFailure (WPI_FATAL, "wiringPiISR: pin must be 0-63 (%d)\n", pin) ;
  1616. /**/ if (wiringPiMode == WPI_MODE_UNINITIALISED)
  1617. return wiringPiFailure (WPI_FATAL, "wiringPiISR: wiringPi has not been initialised. Unable to continue.\n") ;
  1618. else if (wiringPiMode == WPI_MODE_PINS)
  1619. bcmGpioPin = pinToGpio [pin] ;
  1620. else if (wiringPiMode == WPI_MODE_PHYS)
  1621. bcmGpioPin = physToGpio [pin] ;
  1622. else
  1623. bcmGpioPin = pin ;
  1624. // Now export the pin and set the right edge
  1625. // We're going to use the gpio program to do this, so it assumes
  1626. // a full installation of wiringPi. It's a bit 'clunky', but it
  1627. // is a way that will work when we're running in "Sys" mode, as
  1628. // a non-root user. (without sudo)
  1629. if (mode != INT_EDGE_SETUP)
  1630. {
  1631. /**/ if (mode == INT_EDGE_FALLING)
  1632. modeS = "falling" ;
  1633. else if (mode == INT_EDGE_RISING)
  1634. modeS = "rising" ;
  1635. else
  1636. modeS = "both" ;
  1637. sprintf (pinS, "%d", bcmGpioPin) ;
  1638. if ((pid = fork ()) < 0) // Fail
  1639. return wiringPiFailure (WPI_FATAL, "wiringPiISR: fork failed: %s\n", strerror (errno)) ;
  1640. if (pid == 0) // Child, exec
  1641. {
  1642. /**/ if (access ("/usr/local/bin/gpio", X_OK) == 0)
  1643. {
  1644. execl ("/usr/local/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ;
  1645. return wiringPiFailure (WPI_FATAL, "wiringPiISR: execl failed: %s\n", strerror (errno)) ;
  1646. }
  1647. else if (access ("/usr/bin/gpio", X_OK) == 0)
  1648. {
  1649. execl ("/usr/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ;
  1650. return wiringPiFailure (WPI_FATAL, "wiringPiISR: execl failed: %s\n", strerror (errno)) ;
  1651. }
  1652. else
  1653. return wiringPiFailure (WPI_FATAL, "wiringPiISR: Can't find gpio program\n") ;
  1654. }
  1655. else // Parent, wait
  1656. wait (NULL) ;
  1657. }
  1658. // Now pre-open the /sys/class node - but it may already be open if
  1659. // we are in Sys mode...
  1660. if (sysFds [bcmGpioPin] == -1)
  1661. {
  1662. sprintf (fName, "/sys/class/gpio/gpio%d/value", bcmGpioPin) ;
  1663. if ((sysFds [bcmGpioPin] = open (fName, O_RDWR)) < 0)
  1664. return wiringPiFailure (WPI_FATAL, "wiringPiISR: unable to open %s: %s\n", fName, strerror (errno)) ;
  1665. }
  1666. // Clear any initial pending interrupt
  1667. ioctl (sysFds [bcmGpioPin], FIONREAD, &count) ;
  1668. for (i = 0 ; i < count ; ++i)
  1669. read (sysFds [bcmGpioPin], &c, 1) ;
  1670. isrFunctions [pin] = function ;
  1671. pthread_mutex_lock (&pinMutex) ;
  1672. pinPass = pin ;
  1673. pthread_create (&threadId, NULL, interruptHandler, NULL) ;
  1674. while (pinPass != -1)
  1675. delay (1) ;
  1676. pthread_mutex_unlock (&pinMutex) ;
  1677. return 0 ;
  1678. }
  1679. /*
  1680. * initialiseEpoch:
  1681. * Initialise our start-of-time variable to be the current unix
  1682. * time in milliseconds and microseconds.
  1683. *********************************************************************************
  1684. */
  1685. static void initialiseEpoch (void)
  1686. {
  1687. #ifdef OLD_WAY
  1688. struct timeval tv ;
  1689. gettimeofday (&tv, NULL) ;
  1690. epochMilli = (uint64_t)tv.tv_sec * (uint64_t)1000 + (uint64_t)(tv.tv_usec / 1000) ;
  1691. epochMicro = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)(tv.tv_usec) ;
  1692. #else
  1693. struct timespec ts ;
  1694. clock_gettime (CLOCK_MONOTONIC_RAW, &ts) ;
  1695. epochMilli = (uint64_t)ts.tv_sec * (uint64_t)1000 + (uint64_t)(ts.tv_nsec / 1000000L) ;
  1696. epochMicro = (uint64_t)ts.tv_sec * (uint64_t)1000000 + (uint64_t)(ts.tv_nsec / 1000L) ;
  1697. #endif
  1698. }
  1699. /*
  1700. * delay:
  1701. * Wait for some number of milliseconds
  1702. *********************************************************************************
  1703. */
  1704. void delay (unsigned int howLong)
  1705. {
  1706. struct timespec sleeper, dummy ;
  1707. sleeper.tv_sec = (time_t)(howLong / 1000) ;
  1708. sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ;
  1709. nanosleep (&sleeper, &dummy) ;
  1710. }
  1711. /*
  1712. * delayMicroseconds:
  1713. * This is somewhat intersting. It seems that on the Pi, a single call
  1714. * to nanosleep takes some 80 to 130 microseconds anyway, so while
  1715. * obeying the standards (may take longer), it's not always what we
  1716. * want!
  1717. *
  1718. * So what I'll do now is if the delay is less than 100uS we'll do it
  1719. * in a hard loop, watching a built-in counter on the ARM chip. This is
  1720. * somewhat sub-optimal in that it uses 100% CPU, something not an issue
  1721. * in a microcontroller, but under a multi-tasking, multi-user OS, it's
  1722. * wastefull, however we've no real choice )-:
  1723. *
  1724. * Plan B: It seems all might not be well with that plan, so changing it
  1725. * to use gettimeofday () and poll on that instead...
  1726. *********************************************************************************
  1727. */
  1728. void delayMicrosecondsHard (unsigned int howLong)
  1729. {
  1730. struct timeval tNow, tLong, tEnd ;
  1731. gettimeofday (&tNow, NULL) ;
  1732. tLong.tv_sec = howLong / 1000000 ;
  1733. tLong.tv_usec = howLong % 1000000 ;
  1734. timeradd (&tNow, &tLong, &tEnd) ;
  1735. while (timercmp (&tNow, &tEnd, <))
  1736. gettimeofday (&tNow, NULL) ;
  1737. }
  1738. void delayMicroseconds (unsigned int howLong)
  1739. {
  1740. struct timespec sleeper ;
  1741. unsigned int uSecs = howLong % 1000000 ;
  1742. unsigned int wSecs = howLong / 1000000 ;
  1743. /**/ if (howLong == 0)
  1744. return ;
  1745. else if (howLong < 100)
  1746. delayMicrosecondsHard (howLong) ;
  1747. else
  1748. {
  1749. sleeper.tv_sec = wSecs ;
  1750. sleeper.tv_nsec = (long)(uSecs * 1000L) ;
  1751. nanosleep (&sleeper, NULL) ;
  1752. }
  1753. }
  1754. /*
  1755. * millis:
  1756. * Return a number of milliseconds as an unsigned int.
  1757. * Wraps at 49 days.
  1758. *********************************************************************************
  1759. */
  1760. unsigned int millis (void)
  1761. {
  1762. uint64_t now ;
  1763. #ifdef OLD_WAY
  1764. struct timeval tv ;
  1765. gettimeofday (&tv, NULL) ;
  1766. now = (uint64_t)tv.tv_sec * (uint64_t)1000 + (uint64_t)(tv.tv_usec / 1000) ;
  1767. #else
  1768. struct timespec ts ;
  1769. clock_gettime (CLOCK_MONOTONIC_RAW, &ts) ;
  1770. now = (uint64_t)ts.tv_sec * (uint64_t)1000 + (uint64_t)(ts.tv_nsec / 1000000L) ;
  1771. #endif
  1772. return (uint32_t)(now - epochMilli) ;
  1773. }
  1774. /*
  1775. * micros:
  1776. * Return a number of microseconds as an unsigned int.
  1777. * Wraps after 71 minutes.
  1778. *********************************************************************************
  1779. */
  1780. unsigned int micros (void)
  1781. {
  1782. uint64_t now ;
  1783. #ifdef OLD_WAY
  1784. struct timeval tv ;
  1785. gettimeofday (&tv, NULL) ;
  1786. now = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)tv.tv_usec ;
  1787. #else
  1788. struct timespec ts ;
  1789. clock_gettime (CLOCK_MONOTONIC_RAW, &ts) ;
  1790. now = (uint64_t)ts.tv_sec * (uint64_t)1000000 + (uint64_t)(ts.tv_nsec / 1000) ;
  1791. #endif
  1792. return (uint32_t)(now - epochMicro) ;
  1793. }
  1794. /*
  1795. * wiringPiVersion:
  1796. * Return our current version number
  1797. *********************************************************************************
  1798. */
  1799. void wiringPiVersion (int *major, int *minor)
  1800. {
  1801. *major = VERSION_MAJOR ;
  1802. *minor = VERSION_MINOR ;
  1803. }
  1804. /*
  1805. * wiringPiSetup:
  1806. * Must be called once at the start of your program execution.
  1807. *
  1808. * Default setup: Initialises the system into wiringPi Pin mode and uses the
  1809. * memory mapped hardware directly.
  1810. *
  1811. * Changed now to revert to "gpio" mode if we're running on a Compute Module.
  1812. *********************************************************************************
  1813. */
  1814. int wiringPiSetup (void)
  1815. {
  1816. int fd ;
  1817. int model, rev, mem, maker, overVolted ;
  1818. // It's actually a fatal error to call any of the wiringPiSetup routines more than once,
  1819. // (you run out of file handles!) but I'm fed-up with the useless twats who email
  1820. // me bleating that there is a bug in my code, so screw-em.
  1821. if (wiringPiSetuped)
  1822. return 0 ;
  1823. wiringPiSetuped = TRUE ;
  1824. if (getenv (ENV_DEBUG) != NULL)
  1825. wiringPiDebug = TRUE ;
  1826. if (getenv (ENV_CODES) != NULL)
  1827. wiringPiReturnCodes = TRUE ;
  1828. if (wiringPiDebug)
  1829. printf ("wiringPi: wiringPiSetup called\n") ;
  1830. // Get the board ID information. We're not really using the information here,
  1831. // but it will give us information like the GPIO layout scheme (2 variants
  1832. // on the older 26-pin Pi's) and the GPIO peripheral base address.
  1833. // and if we're running on a compute module, then wiringPi pin numbers
  1834. // don't really many anything, so force native BCM mode anyway.
  1835. piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
  1836. if ((model == PI_MODEL_CM) ||
  1837. (model == PI_MODEL_CM3) ||
  1838. (model == PI_MODEL_CM3P))
  1839. wiringPiMode = WPI_MODE_GPIO ;
  1840. else
  1841. wiringPiMode = WPI_MODE_PINS ;
  1842. /**/ if (piGpioLayout () == 1) // A, B, Rev 1, 1.1
  1843. {
  1844. pinToGpio = pinToGpioR1 ;
  1845. physToGpio = physToGpioR1 ;
  1846. }
  1847. else // A2, B2, A+, B+, CM, Pi2, Pi3, Zero
  1848. {
  1849. pinToGpio = pinToGpioR2 ;
  1850. physToGpio = physToGpioR2 ;
  1851. }
  1852. // ...
  1853. switch (model)
  1854. {
  1855. case PI_MODEL_A: case PI_MODEL_B:
  1856. case PI_MODEL_AP: case PI_MODEL_BP:
  1857. case PI_ALPHA: case PI_MODEL_CM:
  1858. case PI_MODEL_ZERO: case PI_MODEL_ZERO_W:
  1859. piGpioBase = GPIO_PERI_BASE_OLD ;
  1860. piGpioPupOffset = GPPUD ;
  1861. break ;
  1862. case PI_MODEL_4B:
  1863. piGpioBase = GPIO_PERI_BASE_2711 ;
  1864. piGpioPupOffset = GPPUPPDN0 ;
  1865. break ;
  1866. default:
  1867. piGpioBase = GPIO_PERI_BASE_2835 ;
  1868. piGpioPupOffset = GPPUD ;
  1869. break ;
  1870. }
  1871. // Open the master /dev/ memory control device
  1872. // Device strategy: December 2016:
  1873. // Try /dev/mem. If that fails, then
  1874. // try /dev/gpiomem. If that fails then game over.
  1875. if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC)) < 0)
  1876. {
  1877. if ((fd = open ("/dev/gpiomem", O_RDWR | O_SYNC | O_CLOEXEC) ) >= 0) // We're using gpiomem
  1878. {
  1879. piGpioBase = 0 ;
  1880. usingGpioMem = TRUE ;
  1881. }
  1882. else
  1883. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: Unable to open /dev/mem or /dev/gpiomem: %s.\n"
  1884. " Aborting your program because if it can not access the GPIO\n"
  1885. " hardware then it most certianly won't work\n"
  1886. " Try running with sudo?\n", strerror (errno)) ;
  1887. }
  1888. // Set the offsets into the memory interface.
  1889. GPIO_PADS = piGpioBase + 0x00100000 ;
  1890. GPIO_CLOCK_BASE = piGpioBase + 0x00101000 ;
  1891. GPIO_BASE = piGpioBase + 0x00200000 ;
  1892. GPIO_TIMER = piGpioBase + 0x0000B000 ;
  1893. GPIO_PWM = piGpioBase + 0x0020C000 ;
  1894. // Map the individual hardware components
  1895. // GPIO:
  1896. gpio = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_BASE) ;
  1897. if (gpio == MAP_FAILED)
  1898. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (GPIO) failed: %s\n", strerror (errno)) ;
  1899. // PWM
  1900. pwm = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PWM) ;
  1901. if (pwm == MAP_FAILED)
  1902. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PWM) failed: %s\n", strerror (errno)) ;
  1903. // Clock control (needed for PWM)
  1904. clk = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_CLOCK_BASE) ;
  1905. if (clk == MAP_FAILED)
  1906. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (CLOCK) failed: %s\n", strerror (errno)) ;
  1907. // The drive pads
  1908. pads = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PADS) ;
  1909. if (pads == MAP_FAILED)
  1910. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PADS) failed: %s\n", strerror (errno)) ;
  1911. // The system timer
  1912. timer = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_TIMER) ;
  1913. if (timer == MAP_FAILED)
  1914. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (TIMER) failed: %s\n", strerror (errno)) ;
  1915. // Set the timer to free-running, 1MHz.
  1916. // 0xF9 is 249, the timer divide is base clock / (divide+1)
  1917. // so base clock is 250MHz / 250 = 1MHz.
  1918. *(timer + TIMER_CONTROL) = 0x0000280 ;
  1919. *(timer + TIMER_PRE_DIV) = 0x00000F9 ;
  1920. timerIrqRaw = timer + TIMER_IRQ_RAW ;
  1921. // Export the base addresses for any external software that might need them
  1922. _wiringPiGpio = gpio ;
  1923. _wiringPiPwm = pwm ;
  1924. _wiringPiClk = clk ;
  1925. _wiringPiPads = pads ;
  1926. _wiringPiTimer = timer ;
  1927. initialiseEpoch () ;
  1928. return 0 ;
  1929. }
  1930. /*
  1931. * wiringPiSetupGpio:
  1932. * Must be called once at the start of your program execution.
  1933. *
  1934. * GPIO setup: Initialises the system into GPIO Pin mode and uses the
  1935. * memory mapped hardware directly.
  1936. *********************************************************************************
  1937. */
  1938. int wiringPiSetupGpio (void)
  1939. {
  1940. (void)wiringPiSetup () ;
  1941. if (wiringPiDebug)
  1942. printf ("wiringPi: wiringPiSetupGpio called\n") ;
  1943. wiringPiMode = WPI_MODE_GPIO ;
  1944. return 0 ;
  1945. }
  1946. /*
  1947. * wiringPiSetupPhys:
  1948. * Must be called once at the start of your program execution.
  1949. *
  1950. * Phys setup: Initialises the system into Physical Pin mode and uses the
  1951. * memory mapped hardware directly.
  1952. *********************************************************************************
  1953. */
  1954. int wiringPiSetupPhys (void)
  1955. {
  1956. (void)wiringPiSetup () ;
  1957. if (wiringPiDebug)
  1958. printf ("wiringPi: wiringPiSetupPhys called\n") ;
  1959. wiringPiMode = WPI_MODE_PHYS ;
  1960. return 0 ;
  1961. }
  1962. /*
  1963. * wiringPiSetupSys:
  1964. * Must be called once at the start of your program execution.
  1965. *
  1966. * Initialisation (again), however this time we are using the /sys/class/gpio
  1967. * interface to the GPIO systems - slightly slower, but always usable as
  1968. * a non-root user, assuming the devices are already exported and setup correctly.
  1969. */
  1970. int wiringPiSetupSys (void)
  1971. {
  1972. int pin ;
  1973. char fName [128] ;
  1974. if (wiringPiSetuped)
  1975. return 0 ;
  1976. wiringPiSetuped = TRUE ;
  1977. if (getenv (ENV_DEBUG) != NULL)
  1978. wiringPiDebug = TRUE ;
  1979. if (getenv (ENV_CODES) != NULL)
  1980. wiringPiReturnCodes = TRUE ;
  1981. if (wiringPiDebug)
  1982. printf ("wiringPi: wiringPiSetupSys called\n") ;
  1983. if (piGpioLayout () == 1)
  1984. {
  1985. pinToGpio = pinToGpioR1 ;
  1986. physToGpio = physToGpioR1 ;
  1987. }
  1988. else
  1989. {
  1990. pinToGpio = pinToGpioR2 ;
  1991. physToGpio = physToGpioR2 ;
  1992. }
  1993. // Open and scan the directory, looking for exported GPIOs, and pre-open
  1994. // the 'value' interface to speed things up for later
  1995. for (pin = 0 ; pin < 64 ; ++pin)
  1996. {
  1997. sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
  1998. sysFds [pin] = open (fName, O_RDWR) ;
  1999. }
  2000. initialiseEpoch () ;
  2001. wiringPiMode = WPI_MODE_GPIO_SYS ;
  2002. return 0 ;
  2003. }