wiringPi.c 62 KB

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