wiringPi.c 70 KB

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