wiringPi.c 66 KB

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