wiringPi.c 64 KB

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