wiringPi.c 69 KB

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