wiringPi.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372
  1. /*
  2. * wiringPi:
  3. * Arduino look-a-like Wiring library for the Raspberry Pi
  4. * Copyright (c) 2012-2017 Gordon Henderson
  5. * Additional code for pwmSetClock by Chris Hall <chris@kchall.plus.com>
  6. *
  7. * Thanks to code samples from Gert Jan van Loo and the
  8. * BCM2835 ARM Peripherals manual, however it's missing
  9. * the clock section /grr/mutter/
  10. ***********************************************************************
  11. * This file is part of wiringPi:
  12. * https://projects.drogon.net/raspberry-pi/wiringpi/
  13. *
  14. * wiringPi is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Lesser General Public License as
  16. * published by the Free Software Foundation, either version 3 of the
  17. * License, or (at your option) any later version.
  18. *
  19. * wiringPi is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Lesser General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Lesser General Public
  25. * License along with wiringPi.
  26. * If not, see <http://www.gnu.org/licenses/>.
  27. ***********************************************************************
  28. */
  29. // Revisions:
  30. // 19 Jul 2012:
  31. // Moved to the LGPL
  32. // Added an abstraction layer to the main routines to save a tiny
  33. // bit of run-time and make the clode a little cleaner (if a little
  34. // larger)
  35. // Added waitForInterrupt code
  36. // Added piHiPri code
  37. //
  38. // 9 Jul 2012:
  39. // Added in support to use the /sys/class/gpio interface.
  40. // 2 Jul 2012:
  41. // Fixed a few more bugs to do with range-checking when in GPIO mode.
  42. // 11 Jun 2012:
  43. // Fixed some typos.
  44. // Added c++ support for the .h file
  45. // Added a new function to allow for using my "pin" numbers, or native
  46. // GPIO pin numbers.
  47. // Removed my busy-loop delay and replaced it with a call to delayMicroseconds
  48. //
  49. // 02 May 2012:
  50. // Added in the 2 UART pins
  51. // Change maxPins to numPins to more accurately reflect purpose
  52. #include <stdio.h>
  53. #include <stdarg.h>
  54. #include <stdint.h>
  55. #include <stdlib.h>
  56. #include <ctype.h>
  57. #include <poll.h>
  58. #include <unistd.h>
  59. #include <errno.h>
  60. #include <string.h>
  61. #include <time.h>
  62. #include <fcntl.h>
  63. #include <pthread.h>
  64. #include <sys/time.h>
  65. #include <sys/mman.h>
  66. #include <sys/stat.h>
  67. #include <sys/wait.h>
  68. #include <sys/ioctl.h>
  69. #include <asm/ioctl.h>
  70. #include "softPwm.h"
  71. #include "softTone.h"
  72. #include "wiringPi.h"
  73. #include "../version.h"
  74. // Environment Variables
  75. #define ENV_DEBUG "WIRINGPI_DEBUG"
  76. #define ENV_CODES "WIRINGPI_CODES"
  77. #define ENV_GPIOMEM "WIRINGPI_GPIOMEM"
  78. // Extend wiringPi with other pin-based devices and keep track of
  79. // them in this structure
  80. struct wiringPiNodeStruct *wiringPiNodes = NULL ;
  81. // BCM Magic
  82. #define BCM_PASSWORD 0x5A000000
  83. // The BCM2835 has 54 GPIO pins.
  84. // BCM2835 data sheet, Page 90 onwards.
  85. // There are 6 control registers, each control the functions of a block
  86. // of 10 pins.
  87. // Each control register has 10 sets of 3 bits per GPIO pin - the ALT values
  88. //
  89. // 000 = GPIO Pin X is an input
  90. // 001 = GPIO Pin X is an output
  91. // 100 = GPIO Pin X takes alternate function 0
  92. // 101 = GPIO Pin X takes alternate function 1
  93. // 110 = GPIO Pin X takes alternate function 2
  94. // 111 = GPIO Pin X takes alternate function 3
  95. // 011 = GPIO Pin X takes alternate function 4
  96. // 010 = GPIO Pin X takes alternate function 5
  97. //
  98. // So the 3 bits for port X are:
  99. // X / 10 + ((X % 10) * 3)
  100. // Port function select bits
  101. #define FSEL_INPT 0b000
  102. #define FSEL_OUTP 0b001
  103. #define FSEL_ALT0 0b100
  104. #define FSEL_ALT1 0b101
  105. #define FSEL_ALT2 0b110
  106. #define FSEL_ALT3 0b111
  107. #define FSEL_ALT4 0b011
  108. #define FSEL_ALT5 0b010
  109. // Access from ARM Running Linux
  110. // Taken from Gert/Doms code. Some of this is not in the manual
  111. // that I can find )-:
  112. //
  113. // Updates in September 2015 - all now static variables (and apologies for the caps)
  114. // due to the Pi v2, v3, etc. and the new /dev/gpiomem interface
  115. static volatile unsigned int GPIO_PADS ;
  116. static volatile unsigned int GPIO_CLOCK_BASE ;
  117. static volatile unsigned int GPIO_BASE ;
  118. static volatile unsigned int GPIO_TIMER ;
  119. static volatile unsigned int GPIO_PWM ;
  120. #define PAGE_SIZE (4*1024)
  121. #define BLOCK_SIZE (4*1024)
  122. // PWM
  123. // Word offsets into the PWM control region
  124. #define PWM_CONTROL 0
  125. #define PWM_STATUS 1
  126. #define PWM0_RANGE 4
  127. #define PWM0_DATA 5
  128. #define PWM1_RANGE 8
  129. #define PWM1_DATA 9
  130. // Clock regsiter offsets
  131. #define PWMCLK_CNTL 40
  132. #define PWMCLK_DIV 41
  133. #define PWM0_MS_MODE 0x0080 // Run in MS mode
  134. #define PWM0_USEFIFO 0x0020 // Data from FIFO
  135. #define PWM0_REVPOLAR 0x0010 // Reverse polarity
  136. #define PWM0_OFFSTATE 0x0008 // Ouput Off state
  137. #define PWM0_REPEATFF 0x0004 // Repeat last value if FIFO empty
  138. #define PWM0_SERIAL 0x0002 // Run in serial mode
  139. #define PWM0_ENABLE 0x0001 // Channel Enable
  140. #define PWM1_MS_MODE 0x8000 // Run in MS mode
  141. #define PWM1_USEFIFO 0x2000 // Data from FIFO
  142. #define PWM1_REVPOLAR 0x1000 // Reverse polarity
  143. #define PWM1_OFFSTATE 0x0800 // Ouput Off state
  144. #define PWM1_REPEATFF 0x0400 // Repeat last value if FIFO empty
  145. #define PWM1_SERIAL 0x0200 // Run in serial mode
  146. #define PWM1_ENABLE 0x0100 // Channel Enable
  147. // Timer
  148. // Word offsets
  149. #define TIMER_LOAD (0x400 >> 2)
  150. #define TIMER_VALUE (0x404 >> 2)
  151. #define TIMER_CONTROL (0x408 >> 2)
  152. #define TIMER_IRQ_CLR (0x40C >> 2)
  153. #define TIMER_IRQ_RAW (0x410 >> 2)
  154. #define TIMER_IRQ_MASK (0x414 >> 2)
  155. #define TIMER_RELOAD (0x418 >> 2)
  156. #define TIMER_PRE_DIV (0x41C >> 2)
  157. #define TIMER_COUNTER (0x420 >> 2)
  158. // Locals to hold pointers to the hardware
  159. static volatile uint32_t *gpio ;
  160. static volatile uint32_t *pwm ;
  161. static volatile uint32_t *clk ;
  162. static volatile uint32_t *pads ;
  163. #ifdef USE_TIMER
  164. static volatile uint32_t *timer ;
  165. static volatile uint32_t *timerIrqRaw ;
  166. #endif
  167. // Data for use with the boardId functions.
  168. // The order of entries here to correspond with the PI_MODEL_X
  169. // and PI_VERSION_X defines in wiringPi.h
  170. // Only intended for the gpio command - use at your own risk!
  171. // piGpioBase:
  172. // The base address of the GPIO memory mapped hardware IO
  173. #define GPIO_PERI_BASE_OLD 0x20000000
  174. #define GPIO_PERI_BASE_NEW 0x3F000000
  175. static volatile unsigned int piGpioBase = 0 ;
  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. "Pi 3", // 08
  187. "Pi Zero", // 09
  188. "CM3", // 10
  189. "Unknown11", // 11
  190. "Pi Zero-W", // 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. * piGpioLayout:
  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. * Additionally, here we set the piModel2 flag too. This is again, nothing to
  551. * do with the actual model, but the major version numbers - the GPIO base
  552. * hardware address changed at model 2 and above (not the Zero though)
  553. *
  554. *********************************************************************************
  555. */
  556. static void piGpioLayoutOops (const char *why)
  557. {
  558. fprintf (stderr, "Oops: Unable to determine board revision from /proc/cpuinfo\n") ;
  559. fprintf (stderr, " -> %s\n", why) ;
  560. fprintf (stderr, " -> You'd best google the error to find out why.\n") ;
  561. //fprintf (stderr, " -> http://www.raspberrypi.org/phpBB3/viewtopic.php?p=184410#p184410\n") ;
  562. exit (EXIT_FAILURE) ;
  563. }
  564. int piGpioLayout (void)
  565. {
  566. FILE *cpuFd ;
  567. char line [120] ;
  568. char *c ;
  569. static int gpioLayout = -1 ;
  570. if (gpioLayout != -1) // No point checking twice
  571. return gpioLayout ;
  572. if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL)
  573. piGpioLayoutOops ("Unable to open /proc/cpuinfo") ;
  574. // Start by looking for the Architecture to make sure we're really running
  575. // on a Pi. I'm getting fed-up with people whinging at me because
  576. // they can't get it to work on weirdFruitPi boards...
  577. while (fgets (line, 120, cpuFd) != NULL)
  578. if (strncmp (line, "Hardware", 8) == 0)
  579. break ;
  580. if (strncmp (line, "Hardware", 8) != 0)
  581. piGpioLayoutOops ("No \"Hardware\" line") ;
  582. if (wiringPiDebug)
  583. printf ("piGpioLayout: Hardware: %s\n", line) ;
  584. // See if it's BCM2708 or BCM2709 or the new BCM2835.
  585. // OK. As of Kernel 4.8, we have BCM2835 only, regardless of model.
  586. // However I still want to check because it will trap the cheapskates and rip-
  587. // off merchants who want to use wiringPi on non-Raspberry Pi platforms - which
  588. // I do not support so don't email me your bleating whinges about anything
  589. // other than a genuine Raspberry Pi.
  590. if (! (strstr (line, "BCM2708") || strstr (line, "BCM2709") || strstr (line, "BCM2835")))
  591. {
  592. fprintf (stderr, "Unable to determine hardware version. I see: %s,\n", line) ;
  593. fprintf (stderr, " - expecting BCM2708, BCM2709 or BCM2835.\n") ;
  594. fprintf (stderr, "If this is a genuine Raspberry Pi then please report this\n") ;
  595. fprintf (stderr, "to projects@drogon.net. If this is not a Raspberry Pi then you\n") ;
  596. fprintf (stderr, "are on your own as wiringPi is designed to support the\n") ;
  597. fprintf (stderr, "Raspberry Pi ONLY.\n") ;
  598. exit (EXIT_FAILURE) ;
  599. }
  600. // Right - we're Probably on a Raspberry Pi. Check the revision field for the real
  601. // hardware type
  602. // In-future, I ought to use the device tree as there are now Pi entries in
  603. // /proc/device-tree/ ...
  604. // but I'll leave that for the next revision.
  605. // Isolate the Revision line
  606. rewind (cpuFd) ;
  607. while (fgets (line, 120, cpuFd) != NULL)
  608. if (strncmp (line, "Revision", 8) == 0)
  609. break ;
  610. fclose (cpuFd) ;
  611. if (strncmp (line, "Revision", 8) != 0)
  612. piGpioLayoutOops ("No \"Revision\" line") ;
  613. // Chomp trailing CR/NL
  614. for (c = &line [strlen (line) - 1] ; (*c == '\n') || (*c == '\r') ; --c)
  615. *c = 0 ;
  616. if (wiringPiDebug)
  617. printf ("piGpioLayout: Revision string: %s\n", line) ;
  618. // Scan to the first character of the revision number
  619. for (c = line ; *c ; ++c)
  620. if (*c == ':')
  621. break ;
  622. if (*c != ':')
  623. piGpioLayoutOops ("Bogus \"Revision\" line (no colon)") ;
  624. // Chomp spaces
  625. ++c ;
  626. while (isspace (*c))
  627. ++c ;
  628. if (!isxdigit (*c))
  629. piGpioLayoutOops ("Bogus \"Revision\" line (no hex digit at start of revision)") ;
  630. // Make sure its long enough
  631. if (strlen (c) < 4)
  632. piGpioLayoutOops ("Bogus revision line (too small)") ;
  633. // Isolate last 4 characters: (in-case of overvolting or new encoding scheme)
  634. c = c + strlen (c) - 4 ;
  635. if (wiringPiDebug)
  636. printf ("piGpioLayout: last4Chars are: \"%s\"\n", c) ;
  637. if ( (strcmp (c, "0002") == 0) || (strcmp (c, "0003") == 0))
  638. gpioLayout = 1 ;
  639. else
  640. gpioLayout = 2 ; // Covers everything else from the B revision 2 to the B+, the Pi v2, v3, zero and CM's.
  641. if (wiringPiDebug)
  642. printf ("piGpioLayoutOops: Returning revision: %d\n", gpioLayout) ;
  643. return gpioLayout ;
  644. }
  645. /*
  646. * piBoardRev:
  647. * Deprecated, but does the same as piGpioLayout
  648. *********************************************************************************
  649. */
  650. int piBoardRev (void)
  651. {
  652. return piGpioLayout () ;
  653. }
  654. /*
  655. * piBoardId:
  656. * Return the real details of the board we have.
  657. *
  658. * This is undocumented and really only intended for the GPIO command.
  659. * Use at your own risk!
  660. *
  661. * Seems there are some boards with 0000 in them (mistake in manufacture)
  662. * So the distinction between boards that I can see is:
  663. *
  664. * 0000 - Error
  665. * 0001 - Not used
  666. *
  667. * Original Pi boards:
  668. * 0002 - Model B, Rev 1, 256MB, Egoman
  669. * 0003 - Model B, Rev 1.1, 256MB, Egoman, Fuses/D14 removed.
  670. *
  671. * Newer Pi's with remapped GPIO:
  672. * 0004 - Model B, Rev 1.2, 256MB, Sony
  673. * 0005 - Model B, Rev 1.2, 256MB, Egoman
  674. * 0006 - Model B, Rev 1.2, 256MB, Egoman
  675. *
  676. * 0007 - Model A, Rev 1.2, 256MB, Egoman
  677. * 0008 - Model A, Rev 1.2, 256MB, Sony
  678. * 0009 - Model A, Rev 1.2, 256MB, Egoman
  679. *
  680. * 000d - Model B, Rev 1.2, 512MB, Egoman (Red Pi, Blue Pi?)
  681. * 000e - Model B, Rev 1.2, 512MB, Sony
  682. * 000f - Model B, Rev 1.2, 512MB, Egoman
  683. *
  684. * 0010 - Model B+, Rev 1.2, 512MB, Sony
  685. * 0013 - Model B+ Rev 1.2, 512MB, Embest
  686. * 0016 - Model B+ Rev 1.2, 512MB, Sony
  687. * 0019 - Model B+ Rev 1.2, 512MB, Egoman
  688. *
  689. * 0011 - Pi CM, Rev 1.1, 512MB, Sony
  690. * 0014 - Pi CM, Rev 1.1, 512MB, Embest
  691. * 0017 - Pi CM, Rev 1.1, 512MB, Sony
  692. * 001a - Pi CM, Rev 1.1, 512MB, Egoman
  693. *
  694. * 0012 - Model A+ Rev 1.1, 256MB, Sony
  695. * 0015 - Model A+ Rev 1.1, 512MB, Embest
  696. * 0018 - Model A+ Rev 1.1, 256MB, Sony
  697. * 001b - Model A+ Rev 1.1, 256MB, Egoman
  698. *
  699. * A small thorn is the olde style overvolting - that will add in
  700. * 1000000
  701. *
  702. * The Pi compute module has an revision of 0011 or 0014 - since we only
  703. * check the last digit, then it's 1, therefore it'll default to not 2 or
  704. * 3 for a Rev 1, so will appear as a Rev 2. This is fine for the most part, but
  705. * we'll properly detect the Compute Module later and adjust accordingly.
  706. *
  707. * And then things changed with the introduction of the v2...
  708. *
  709. * For Pi v2 and subsequent models - e.g. the Zero:
  710. *
  711. * [USER:8] [NEW:1] [MEMSIZE:3] [MANUFACTURER:4] [PROCESSOR:4] [TYPE:8] [REV:4]
  712. * NEW 23: will be 1 for the new scheme, 0 for the old scheme
  713. * MEMSIZE 20: 0=256M 1=512M 2=1G
  714. * MANUFACTURER 16: 0=SONY 1=EGOMAN 2=EMBEST
  715. * PROCESSOR 12: 0=2835 1=2836
  716. * TYPE 04: 0=MODELA 1=MODELB 2=MODELA+ 3=MODELB+ 4=Pi2 MODEL B 5=ALPHA 6=CM
  717. * REV 00: 0=REV0 1=REV1 2=REV2
  718. *********************************************************************************
  719. */
  720. void piBoardId (int *model, int *rev, int *mem, int *maker, int *warranty)
  721. {
  722. FILE *cpuFd ;
  723. char line [120] ;
  724. char *c ;
  725. unsigned int revision ;
  726. int bRev, bType, bProc, bMfg, bMem, bWarranty ;
  727. // Will deal with the properly later on - for now, lets just get it going...
  728. // unsigned int modelNum ;
  729. (void)piGpioLayout () ; // Call this first to make sure all's OK. Don't care about the result.
  730. if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL)
  731. piGpioLayoutOops ("Unable to open /proc/cpuinfo") ;
  732. while (fgets (line, 120, cpuFd) != NULL)
  733. if (strncmp (line, "Revision", 8) == 0)
  734. break ;
  735. fclose (cpuFd) ;
  736. if (strncmp (line, "Revision", 8) != 0)
  737. piGpioLayoutOops ("No \"Revision\" line") ;
  738. // Chomp trailing CR/NL
  739. for (c = &line [strlen (line) - 1] ; (*c == '\n') || (*c == '\r') ; --c)
  740. *c = 0 ;
  741. if (wiringPiDebug)
  742. printf ("piBoardId: Revision string: %s\n", line) ;
  743. // Need to work out if it's using the new or old encoding scheme:
  744. // Scan to the first character of the revision number
  745. for (c = line ; *c ; ++c)
  746. if (*c == ':')
  747. break ;
  748. if (*c != ':')
  749. piGpioLayoutOops ("Bogus \"Revision\" line (no colon)") ;
  750. // Chomp spaces
  751. ++c ;
  752. while (isspace (*c))
  753. ++c ;
  754. if (!isxdigit (*c))
  755. piGpioLayoutOops ("Bogus \"Revision\" line (no hex digit at start of revision)") ;
  756. revision = (unsigned int)strtol (c, NULL, 16) ; // Hex number with no leading 0x
  757. // Check for new way:
  758. if ((revision & (1 << 23)) != 0) // New way
  759. {
  760. if (wiringPiDebug)
  761. printf ("piBoardId: New Way: revision is: 0x%08X\n", revision) ;
  762. bRev = (revision & (0x0F << 0)) >> 0 ;
  763. bType = (revision & (0xFF << 4)) >> 4 ;
  764. bProc = (revision & (0x0F << 12)) >> 12 ; // Not used for now.
  765. bMfg = (revision & (0x0F << 16)) >> 16 ;
  766. bMem = (revision & (0x07 << 20)) >> 20 ;
  767. bWarranty = (revision & (0x03 << 24)) != 0 ;
  768. *model = bType ;
  769. *rev = bRev ;
  770. *mem = bMem ;
  771. *maker = bMfg ;
  772. *warranty = bWarranty ;
  773. if (wiringPiDebug)
  774. printf ("piBoardId: rev: %d, type: %d, proc: %d, mfg: %d, mem: %d, warranty: %d\n",
  775. bRev, bType, bProc, bMfg, bMem, bWarranty) ;
  776. }
  777. else // Old way
  778. {
  779. if (wiringPiDebug)
  780. printf ("piBoardId: Old Way: revision is: %s\n", c) ;
  781. if (!isdigit (*c))
  782. piGpioLayoutOops ("Bogus \"Revision\" line (no digit at start of revision)") ;
  783. // Make sure its long enough
  784. if (strlen (c) < 4)
  785. piGpioLayoutOops ("Bogus \"Revision\" line (not long enough)") ;
  786. // If longer than 4, we'll assume it's been overvolted
  787. *warranty = strlen (c) > 4 ;
  788. // Extract last 4 characters:
  789. c = c + strlen (c) - 4 ;
  790. // Fill out the replys as appropriate
  791. /**/ if (strcmp (c, "0002") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
  792. else if (strcmp (c, "0003") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_1 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
  793. else if (strcmp (c, "0004") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_2 ; *mem = 0 ; *maker = PI_MAKER_SONY ; }
  794. else if (strcmp (c, "0005") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_2 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
  795. else if (strcmp (c, "0006") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_2 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
  796. else if (strcmp (c, "0007") == 0) { *model = PI_MODEL_A ; *rev = PI_VERSION_1_2 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
  797. else if (strcmp (c, "0008") == 0) { *model = PI_MODEL_A ; *rev = PI_VERSION_1_2 ; *mem = 0 ; *maker = PI_MAKER_SONY ; ; }
  798. else if (strcmp (c, "0009") == 0) { *model = PI_MODEL_A ; *rev = PI_VERSION_1_2 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
  799. else if (strcmp (c, "000d") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_EGOMAN ; }
  800. else if (strcmp (c, "000e") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_SONY ; }
  801. else if (strcmp (c, "000f") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_EGOMAN ; }
  802. else if (strcmp (c, "0010") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_SONY ; }
  803. else if (strcmp (c, "0013") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_EMBEST ; }
  804. else if (strcmp (c, "0016") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_SONY ; }
  805. else if (strcmp (c, "0019") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_EGOMAN ; }
  806. else if (strcmp (c, "0011") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_1 ; *mem = 1 ; *maker = PI_MAKER_SONY ; }
  807. else if (strcmp (c, "0014") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_1 ; *mem = 1 ; *maker = PI_MAKER_EMBEST ; }
  808. else if (strcmp (c, "0017") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_1 ; *mem = 1 ; *maker = PI_MAKER_SONY ; }
  809. else if (strcmp (c, "001a") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_1 ; *mem = 1 ; *maker = PI_MAKER_EGOMAN ; }
  810. else if (strcmp (c, "0012") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_1 ; *mem = 0 ; *maker = PI_MAKER_SONY ; }
  811. else if (strcmp (c, "0015") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_1 ; *mem = 1 ; *maker = PI_MAKER_EMBEST ; }
  812. else if (strcmp (c, "0018") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_1 ; *mem = 0 ; *maker = PI_MAKER_SONY ; }
  813. else if (strcmp (c, "001b") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_1 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
  814. else { *model = 0 ; *rev = 0 ; *mem = 0 ; *maker = 0 ; }
  815. }
  816. }
  817. /*
  818. * wpiPinToGpio:
  819. * Translate a wiringPi Pin number to native GPIO pin number.
  820. * Provided for external support.
  821. *********************************************************************************
  822. */
  823. int wpiPinToGpio (int wpiPin)
  824. {
  825. return pinToGpio [wpiPin & 63] ;
  826. }
  827. /*
  828. * physPinToGpio:
  829. * Translate a physical Pin number to native GPIO pin number.
  830. * Provided for external support.
  831. *********************************************************************************
  832. */
  833. int physPinToGpio (int physPin)
  834. {
  835. return physToGpio [physPin & 63] ;
  836. }
  837. /*
  838. * setPadDrive:
  839. * Set the PAD driver value
  840. *********************************************************************************
  841. */
  842. void setPadDrive (int group, int value)
  843. {
  844. uint32_t wrVal ;
  845. if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
  846. {
  847. if ((group < 0) || (group > 2))
  848. return ;
  849. wrVal = BCM_PASSWORD | 0x18 | (value & 7) ;
  850. *(pads + group + 11) = wrVal ;
  851. if (wiringPiDebug)
  852. {
  853. printf ("setPadDrive: Group: %d, value: %d (%08X)\n", group, value, wrVal) ;
  854. printf ("Read : %08X\n", *(pads + group + 11)) ;
  855. }
  856. }
  857. }
  858. /*
  859. * getAlt:
  860. * Returns the ALT bits for a given port. Only really of-use
  861. * for the gpio readall command (I think)
  862. *********************************************************************************
  863. */
  864. int getAlt (int pin)
  865. {
  866. int fSel, shift, alt ;
  867. pin &= 63 ;
  868. /**/ if (wiringPiMode == WPI_MODE_PINS)
  869. pin = pinToGpio [pin] ;
  870. else if (wiringPiMode == WPI_MODE_PHYS)
  871. pin = physToGpio [pin] ;
  872. else if (wiringPiMode != WPI_MODE_GPIO)
  873. return 0 ;
  874. fSel = gpioToGPFSEL [pin] ;
  875. shift = gpioToShift [pin] ;
  876. alt = (*(gpio + fSel) >> shift) & 7 ;
  877. return alt ;
  878. }
  879. /*
  880. * pwmSetMode:
  881. * Select the native "balanced" mode, or standard mark:space mode
  882. *********************************************************************************
  883. */
  884. void pwmSetMode (int mode)
  885. {
  886. if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
  887. {
  888. if (mode == PWM_MODE_MS)
  889. *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE | PWM0_MS_MODE | PWM1_MS_MODE ;
  890. else
  891. *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ;
  892. }
  893. }
  894. /*
  895. * pwmSetRange:
  896. * Set the PWM range register. We set both range registers to the same
  897. * value. If you want different in your own code, then write your own.
  898. *********************************************************************************
  899. */
  900. void pwmSetRange (unsigned int range)
  901. {
  902. if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
  903. {
  904. *(pwm + PWM0_RANGE) = range ; delayMicroseconds (10) ;
  905. *(pwm + PWM1_RANGE) = range ; delayMicroseconds (10) ;
  906. }
  907. }
  908. /*
  909. * pwmSetClock:
  910. * Set/Change the PWM clock. Originally my code, but changed
  911. * (for the better!) by Chris Hall, <chris@kchall.plus.com>
  912. * after further study of the manual and testing with a 'scope
  913. *********************************************************************************
  914. */
  915. void pwmSetClock (int divisor)
  916. {
  917. uint32_t pwm_control ;
  918. divisor &= 4095 ;
  919. if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
  920. {
  921. if (wiringPiDebug)
  922. printf ("Setting to: %d. Current: 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;
  923. pwm_control = *(pwm + PWM_CONTROL) ; // preserve PWM_CONTROL
  924. // We need to stop PWM prior to stopping PWM clock in MS mode otherwise BUSY
  925. // stays high.
  926. *(pwm + PWM_CONTROL) = 0 ; // Stop PWM
  927. // Stop PWM clock before changing divisor. The delay after this does need to
  928. // this big (95uS occasionally fails, 100uS OK), it's almost as though the BUSY
  929. // flag is not working properly in balanced mode. Without the delay when DIV is
  930. // adjusted the clock sometimes switches to very slow, once slow further DIV
  931. // adjustments do nothing and it's difficult to get out of this mode.
  932. *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x01 ; // Stop PWM Clock
  933. delayMicroseconds (110) ; // prevents clock going sloooow
  934. while ((*(clk + PWMCLK_CNTL) & 0x80) != 0) // Wait for clock to be !BUSY
  935. delayMicroseconds (1) ;
  936. *(clk + PWMCLK_DIV) = BCM_PASSWORD | (divisor << 12) ;
  937. *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x11 ; // Start PWM clock
  938. *(pwm + PWM_CONTROL) = pwm_control ; // restore PWM_CONTROL
  939. if (wiringPiDebug)
  940. printf ("Set to: %d. Now : 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;
  941. }
  942. }
  943. /*
  944. * gpioClockSet:
  945. * Set the freuency on a GPIO clock pin
  946. *********************************************************************************
  947. */
  948. void gpioClockSet (int pin, int freq)
  949. {
  950. int divi, divr, divf ;
  951. pin &= 63 ;
  952. /**/ if (wiringPiMode == WPI_MODE_PINS)
  953. pin = pinToGpio [pin] ;
  954. else if (wiringPiMode == WPI_MODE_PHYS)
  955. pin = physToGpio [pin] ;
  956. else if (wiringPiMode != WPI_MODE_GPIO)
  957. return ;
  958. divi = 19200000 / freq ;
  959. divr = 19200000 % freq ;
  960. divf = (int)((double)divr * 4096.0 / 19200000.0) ;
  961. if (divi > 4095)
  962. divi = 4095 ;
  963. *(clk + gpioToClkCon [pin]) = BCM_PASSWORD | GPIO_CLOCK_SOURCE ; // Stop GPIO Clock
  964. while ((*(clk + gpioToClkCon [pin]) & 0x80) != 0) // ... and wait
  965. ;
  966. *(clk + gpioToClkDiv [pin]) = BCM_PASSWORD | (divi << 12) | divf ; // Set dividers
  967. *(clk + gpioToClkCon [pin]) = BCM_PASSWORD | 0x10 | GPIO_CLOCK_SOURCE ; // Start Clock
  968. }
  969. /*
  970. * wiringPiFindNode:
  971. * Locate our device node
  972. *********************************************************************************
  973. */
  974. struct wiringPiNodeStruct *wiringPiFindNode (int pin)
  975. {
  976. struct wiringPiNodeStruct *node = wiringPiNodes ;
  977. while (node != NULL)
  978. if ((pin >= node->pinBase) && (pin <= node->pinMax))
  979. return node ;
  980. else
  981. node = node->next ;
  982. return NULL ;
  983. }
  984. /*
  985. * wiringPiNewNode:
  986. * Create a new GPIO node into the wiringPi handling system
  987. *********************************************************************************
  988. */
  989. static void pinModeDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int mode) { return ; }
  990. static void pullUpDnControlDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int pud) { return ; }
  991. static unsigned int digitalRead8Dummy (UNU struct wiringPiNodeStruct *node, UNU int UNU pin) { return 0 ; }
  992. static void digitalWrite8Dummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int value) { return ; }
  993. static int digitalReadDummy (UNU struct wiringPiNodeStruct *node, UNU int UNU pin) { return LOW ; }
  994. static void digitalWriteDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int value) { return ; }
  995. static void pwmWriteDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int value) { return ; }
  996. static int analogReadDummy (UNU struct wiringPiNodeStruct *node, UNU int pin) { return 0 ; }
  997. static void analogWriteDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int value) { return ; }
  998. struct wiringPiNodeStruct *wiringPiNewNode (int pinBase, int numPins)
  999. {
  1000. int pin ;
  1001. struct wiringPiNodeStruct *node ;
  1002. // Minimum pin base is 64
  1003. if (pinBase < 64)
  1004. (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: pinBase of %d is < 64\n", pinBase) ;
  1005. // Check all pins in-case there is overlap:
  1006. for (pin = pinBase ; pin < (pinBase + numPins) ; ++pin)
  1007. if (wiringPiFindNode (pin) != NULL)
  1008. (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: Pin %d overlaps with existing definition\n", pin) ;
  1009. node = (struct wiringPiNodeStruct *)calloc (sizeof (struct wiringPiNodeStruct), 1) ; // calloc zeros
  1010. if (node == NULL)
  1011. (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: Unable to allocate memory: %s\n", strerror (errno)) ;
  1012. node->pinBase = pinBase ;
  1013. node->pinMax = pinBase + numPins - 1 ;
  1014. node->pinMode = pinModeDummy ;
  1015. node->pullUpDnControl = pullUpDnControlDummy ;
  1016. node->digitalRead = digitalReadDummy ;
  1017. //node->digitalRead8 = digitalRead8Dummy ;
  1018. node->digitalWrite = digitalWriteDummy ;
  1019. //node->digitalWrite8 = digitalWrite8Dummy ;
  1020. node->pwmWrite = pwmWriteDummy ;
  1021. node->analogRead = analogReadDummy ;
  1022. node->analogWrite = analogWriteDummy ;
  1023. node->next = wiringPiNodes ;
  1024. wiringPiNodes = node ;
  1025. return node ;
  1026. }
  1027. #ifdef notYetReady
  1028. /*
  1029. * pinED01:
  1030. * pinED10:
  1031. * Enables edge-detect mode on a pin - from a 0 to a 1 or 1 to 0
  1032. * Pin must already be in input mode with appropriate pull up/downs set.
  1033. *********************************************************************************
  1034. */
  1035. void pinEnableED01Pi (int pin)
  1036. {
  1037. pin = pinToGpio [pin & 63] ;
  1038. }
  1039. #endif
  1040. /*
  1041. *********************************************************************************
  1042. * Core Functions
  1043. *********************************************************************************
  1044. */
  1045. /*
  1046. * pinModeAlt:
  1047. * This is an un-documented special to let you set any pin to any mode
  1048. *********************************************************************************
  1049. */
  1050. void pinModeAlt (int pin, int mode)
  1051. {
  1052. int fSel, shift ;
  1053. if ((pin & PI_GPIO_MASK) == 0) // On-board pin
  1054. {
  1055. /**/ if (wiringPiMode == WPI_MODE_PINS)
  1056. pin = pinToGpio [pin] ;
  1057. else if (wiringPiMode == WPI_MODE_PHYS)
  1058. pin = physToGpio [pin] ;
  1059. else if (wiringPiMode != WPI_MODE_GPIO)
  1060. return ;
  1061. fSel = gpioToGPFSEL [pin] ;
  1062. shift = gpioToShift [pin] ;
  1063. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | ((mode & 0x7) << shift) ;
  1064. }
  1065. }
  1066. /*
  1067. * pinMode:
  1068. * Sets the mode of a pin to be input, output or PWM output
  1069. *********************************************************************************
  1070. */
  1071. void pinMode (int pin, int mode)
  1072. {
  1073. int fSel, shift, alt ;
  1074. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1075. int origPin = pin ;
  1076. if ((pin & PI_GPIO_MASK) == 0) // On-board pin
  1077. {
  1078. /**/ if (wiringPiMode == WPI_MODE_PINS)
  1079. pin = pinToGpio [pin] ;
  1080. else if (wiringPiMode == WPI_MODE_PHYS)
  1081. pin = physToGpio [pin] ;
  1082. else if (wiringPiMode != WPI_MODE_GPIO)
  1083. return ;
  1084. softPwmStop (origPin) ;
  1085. softToneStop (origPin) ;
  1086. fSel = gpioToGPFSEL [pin] ;
  1087. shift = gpioToShift [pin] ;
  1088. /**/ if (mode == INPUT)
  1089. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) ; // Sets bits to zero = input
  1090. else if (mode == OUTPUT)
  1091. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (1 << shift) ;
  1092. else if (mode == SOFT_PWM_OUTPUT)
  1093. softPwmCreate (origPin, 0, 100) ;
  1094. else if (mode == SOFT_TONE_OUTPUT)
  1095. softToneCreate (origPin) ;
  1096. else if (mode == PWM_TONE_OUTPUT)
  1097. {
  1098. pinMode (origPin, PWM_OUTPUT) ; // Call myself to enable PWM mode
  1099. pwmSetMode (PWM_MODE_MS) ;
  1100. }
  1101. else if (mode == PWM_OUTPUT)
  1102. {
  1103. if ((alt = gpioToPwmALT [pin]) == 0) // Not a hardware capable PWM pin
  1104. return ;
  1105. // Set pin to PWM mode
  1106. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ;
  1107. delayMicroseconds (110) ; // See comments in pwmSetClockWPi
  1108. pwmSetMode (PWM_MODE_BAL) ; // Pi default mode
  1109. pwmSetRange (1024) ; // Default range of 1024
  1110. pwmSetClock (32) ; // 19.2 / 32 = 600KHz - Also starts the PWM
  1111. }
  1112. else if (mode == GPIO_CLOCK)
  1113. {
  1114. if ((alt = gpioToGpClkALT0 [pin]) == 0) // Not a GPIO_CLOCK pin
  1115. return ;
  1116. // Set pin to GPIO_CLOCK mode and set the clock frequency to 100KHz
  1117. *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ;
  1118. delayMicroseconds (110) ;
  1119. gpioClockSet (pin, 100000) ;
  1120. }
  1121. }
  1122. else
  1123. {
  1124. if ((node = wiringPiFindNode (pin)) != NULL)
  1125. node->pinMode (node, pin, mode) ;
  1126. return ;
  1127. }
  1128. }
  1129. /*
  1130. * pullUpDownCtrl:
  1131. * Control the internal pull-up/down resistors on a GPIO pin
  1132. * The Arduino only has pull-ups and these are enabled by writing 1
  1133. * to a port when in input mode - this paradigm doesn't quite apply
  1134. * here though.
  1135. *********************************************************************************
  1136. */
  1137. void pullUpDnControl (int pin, int pud)
  1138. {
  1139. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1140. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1141. {
  1142. /**/ if (wiringPiMode == WPI_MODE_PINS)
  1143. pin = pinToGpio [pin] ;
  1144. else if (wiringPiMode == WPI_MODE_PHYS)
  1145. pin = physToGpio [pin] ;
  1146. else if (wiringPiMode != WPI_MODE_GPIO)
  1147. return ;
  1148. *(gpio + GPPUD) = pud & 3 ; delayMicroseconds (5) ;
  1149. *(gpio + gpioToPUDCLK [pin]) = 1 << (pin & 31) ; delayMicroseconds (5) ;
  1150. *(gpio + GPPUD) = 0 ; delayMicroseconds (5) ;
  1151. *(gpio + gpioToPUDCLK [pin]) = 0 ; delayMicroseconds (5) ;
  1152. }
  1153. else // Extension module
  1154. {
  1155. if ((node = wiringPiFindNode (pin)) != NULL)
  1156. node->pullUpDnControl (node, pin, pud) ;
  1157. return ;
  1158. }
  1159. }
  1160. /*
  1161. * digitalRead:
  1162. * Read the value of a given Pin, returning HIGH or LOW
  1163. *********************************************************************************
  1164. */
  1165. int digitalRead (int pin)
  1166. {
  1167. char c ;
  1168. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1169. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1170. {
  1171. /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS) // Sys mode
  1172. {
  1173. if (sysFds [pin] == -1)
  1174. return LOW ;
  1175. lseek (sysFds [pin], 0L, SEEK_SET) ;
  1176. read (sysFds [pin], &c, 1) ;
  1177. return (c == '0') ? LOW : HIGH ;
  1178. }
  1179. else if (wiringPiMode == WPI_MODE_PINS)
  1180. pin = pinToGpio [pin] ;
  1181. else if (wiringPiMode == WPI_MODE_PHYS)
  1182. pin = physToGpio [pin] ;
  1183. else if (wiringPiMode != WPI_MODE_GPIO)
  1184. return LOW ;
  1185. if ((*(gpio + gpioToGPLEV [pin]) & (1 << (pin & 31))) != 0)
  1186. return HIGH ;
  1187. else
  1188. return LOW ;
  1189. }
  1190. else
  1191. {
  1192. if ((node = wiringPiFindNode (pin)) == NULL)
  1193. return LOW ;
  1194. return node->digitalRead (node, pin) ;
  1195. }
  1196. }
  1197. /*
  1198. * digitalRead8:
  1199. * Read 8-bits (a byte) from given start pin.
  1200. *********************************************************************************
  1201. unsigned int digitalRead8 (int pin)
  1202. {
  1203. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1204. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1205. return 0 ;
  1206. else
  1207. {
  1208. if ((node = wiringPiFindNode (pin)) == NULL)
  1209. return LOW ;
  1210. return node->digitalRead8 (node, pin) ;
  1211. }
  1212. }
  1213. */
  1214. /*
  1215. * digitalWrite:
  1216. * Set an output bit
  1217. *********************************************************************************
  1218. */
  1219. void digitalWrite (int pin, int value)
  1220. {
  1221. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1222. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1223. {
  1224. /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS) // Sys mode
  1225. {
  1226. if (sysFds [pin] != -1)
  1227. {
  1228. if (value == LOW)
  1229. write (sysFds [pin], "0\n", 2) ;
  1230. else
  1231. write (sysFds [pin], "1\n", 2) ;
  1232. }
  1233. return ;
  1234. }
  1235. else 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. if (value == LOW)
  1242. *(gpio + gpioToGPCLR [pin]) = 1 << (pin & 31) ;
  1243. else
  1244. *(gpio + gpioToGPSET [pin]) = 1 << (pin & 31) ;
  1245. }
  1246. else
  1247. {
  1248. if ((node = wiringPiFindNode (pin)) != NULL)
  1249. node->digitalWrite (node, pin, value) ;
  1250. }
  1251. }
  1252. /*
  1253. * digitalWrite8:
  1254. * Set an output 8-bit byte on the device from the given pin number
  1255. *********************************************************************************
  1256. void digitalWrite8 (int pin, int value)
  1257. {
  1258. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1259. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1260. return ;
  1261. else
  1262. {
  1263. if ((node = wiringPiFindNode (pin)) != NULL)
  1264. node->digitalWrite8 (node, pin, value) ;
  1265. }
  1266. }
  1267. */
  1268. /*
  1269. * pwmWrite:
  1270. * Set an output PWM value
  1271. *********************************************************************************
  1272. */
  1273. void pwmWrite (int pin, int value)
  1274. {
  1275. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1276. if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
  1277. {
  1278. /**/ if (wiringPiMode == WPI_MODE_PINS)
  1279. pin = pinToGpio [pin] ;
  1280. else if (wiringPiMode == WPI_MODE_PHYS)
  1281. pin = physToGpio [pin] ;
  1282. else if (wiringPiMode != WPI_MODE_GPIO)
  1283. return ;
  1284. *(pwm + gpioToPwmPort [pin]) = value ;
  1285. }
  1286. else
  1287. {
  1288. if ((node = wiringPiFindNode (pin)) != NULL)
  1289. node->pwmWrite (node, pin, value) ;
  1290. }
  1291. }
  1292. /*
  1293. * analogRead:
  1294. * Read the analog value of a given Pin.
  1295. * There is no on-board Pi analog hardware,
  1296. * so this needs to go to a new node.
  1297. *********************************************************************************
  1298. */
  1299. int analogRead (int pin)
  1300. {
  1301. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1302. if ((node = wiringPiFindNode (pin)) == NULL)
  1303. return 0 ;
  1304. else
  1305. return node->analogRead (node, pin) ;
  1306. }
  1307. /*
  1308. * analogWrite:
  1309. * Write the analog value to the given Pin.
  1310. * There is no on-board Pi analog hardware,
  1311. * so this needs to go to a new node.
  1312. *********************************************************************************
  1313. */
  1314. void analogWrite (int pin, int value)
  1315. {
  1316. struct wiringPiNodeStruct *node = wiringPiNodes ;
  1317. if ((node = wiringPiFindNode (pin)) == NULL)
  1318. return ;
  1319. node->analogWrite (node, pin, value) ;
  1320. }
  1321. /*
  1322. * pwmToneWrite:
  1323. * Pi Specific.
  1324. * Output the given frequency on the Pi's PWM pin
  1325. *********************************************************************************
  1326. */
  1327. void pwmToneWrite (int pin, int freq)
  1328. {
  1329. int range ;
  1330. if (freq == 0)
  1331. pwmWrite (pin, 0) ; // Off
  1332. else
  1333. {
  1334. range = 600000 / freq ;
  1335. pwmSetRange (range) ;
  1336. pwmWrite (pin, freq / 2) ;
  1337. }
  1338. }
  1339. /*
  1340. * digitalWriteByte:
  1341. * digitalReadByte:
  1342. * Pi Specific
  1343. * Write an 8-bit byte to the first 8 GPIO pins - try to do it as
  1344. * fast as possible.
  1345. * However it still needs 2 operations to set the bits, so any external
  1346. * hardware must not rely on seeing a change as there will be a change
  1347. * to set the outputs bits to zero, then another change to set the 1's
  1348. * Reading is just bit fiddling.
  1349. * These are wiringPi pin numbers 0..7, or BCM_GPIO pin numbers
  1350. * 17, 18, 22, 23, 24, 24, 4 on a Pi v1 rev 0-3
  1351. * 17, 18, 27, 23, 24, 24, 4 on a Pi v1 rev 3 onwards or B+, 2, 3, zero
  1352. *********************************************************************************
  1353. */
  1354. void digitalWriteByte (const int value)
  1355. {
  1356. uint32_t pinSet = 0 ;
  1357. uint32_t pinClr = 0 ;
  1358. int mask = 1 ;
  1359. int pin ;
  1360. /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS)
  1361. {
  1362. for (pin = 0 ; pin < 8 ; ++pin)
  1363. {
  1364. digitalWrite (pinToGpio [pin], value & mask) ;
  1365. mask <<= 1 ;
  1366. }
  1367. return ;
  1368. }
  1369. else
  1370. {
  1371. for (pin = 0 ; pin < 8 ; ++pin)
  1372. {
  1373. if ((value & mask) == 0)
  1374. pinClr |= (1 << pinToGpio [pin]) ;
  1375. else
  1376. pinSet |= (1 << pinToGpio [pin]) ;
  1377. mask <<= 1 ;
  1378. }
  1379. *(gpio + gpioToGPCLR [0]) = pinClr ;
  1380. *(gpio + gpioToGPSET [0]) = pinSet ;
  1381. }
  1382. }
  1383. unsigned int digitalReadByte (void)
  1384. {
  1385. int pin, x ;
  1386. uint32_t raw ;
  1387. uint32_t data = 0 ;
  1388. /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS)
  1389. {
  1390. for (pin = 0 ; pin < 8 ; ++pin)
  1391. {
  1392. x = digitalRead (pinToGpio [pin]) ;
  1393. data = (data << 1) | x ;
  1394. }
  1395. }
  1396. else
  1397. {
  1398. raw = *(gpio + gpioToGPLEV [0]) ; // First bank for these pins
  1399. for (pin = 0 ; pin < 8 ; ++pin)
  1400. {
  1401. x = pinToGpio [pin] ;
  1402. data = (data << 1) | (((raw & (1 << x)) == 0) ? 0 : 1) ;
  1403. }
  1404. }
  1405. return data ;
  1406. }
  1407. /*
  1408. * digitalWriteByte2:
  1409. * digitalReadByte2:
  1410. * Pi Specific
  1411. * Write an 8-bit byte to the second set of 8 GPIO pins. This is marginally
  1412. * faster than the first lot as these are consecutive BCM_GPIO pin numbers.
  1413. * However they overlap with the original read/write bytes.
  1414. *********************************************************************************
  1415. */
  1416. void digitalWriteByte2 (const int value)
  1417. {
  1418. register int mask = 1 ;
  1419. register int pin ;
  1420. /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS)
  1421. {
  1422. for (pin = 20 ; pin < 28 ; ++pin)
  1423. {
  1424. digitalWrite (pin, value & mask) ;
  1425. mask <<= 1 ;
  1426. }
  1427. return ;
  1428. }
  1429. else
  1430. {
  1431. *(gpio + gpioToGPCLR [0]) = (~value & 0xFF) << 20 ; // 0x0FF00000; ILJ > CHANGE: Old causes glitch
  1432. *(gpio + gpioToGPSET [0]) = ( value & 0xFF) << 20 ;
  1433. }
  1434. }
  1435. unsigned int digitalReadByte2 (void)
  1436. {
  1437. int pin, x ;
  1438. uint32_t data = 0 ;
  1439. /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS)
  1440. {
  1441. for (pin = 20 ; pin < 28 ; ++pin)
  1442. {
  1443. x = digitalRead (pin) ;
  1444. data = (data << 1) | x ;
  1445. }
  1446. }
  1447. else
  1448. data = ((*(gpio + gpioToGPLEV [0])) >> 20) & 0xFF ; // First bank for these pins
  1449. return data ;
  1450. }
  1451. /*
  1452. * waitForInterrupt:
  1453. * Pi Specific.
  1454. * Wait for Interrupt on a GPIO pin.
  1455. * This is actually done via the /sys/class/gpio interface regardless of
  1456. * the wiringPi access mode in-use. Maybe sometime it might get a better
  1457. * way for a bit more efficiency.
  1458. *********************************************************************************
  1459. */
  1460. int waitForInterrupt (int pin, int mS)
  1461. {
  1462. int fd, x ;
  1463. uint8_t c ;
  1464. struct pollfd polls ;
  1465. /**/ if (wiringPiMode == WPI_MODE_PINS)
  1466. pin = pinToGpio [pin] ;
  1467. else if (wiringPiMode == WPI_MODE_PHYS)
  1468. pin = physToGpio [pin] ;
  1469. if ((fd = sysFds [pin]) == -1)
  1470. return -2 ;
  1471. // Setup poll structure
  1472. polls.fd = fd ;
  1473. polls.events = POLLPRI | POLLERR ;
  1474. // Wait for it ...
  1475. x = poll (&polls, 1, mS) ;
  1476. // If no error, do a dummy read to clear the interrupt
  1477. // A one character read appars to be enough.
  1478. if (x > 0)
  1479. {
  1480. lseek (fd, 0, SEEK_SET) ; // Rewind
  1481. (void)read (fd, &c, 1) ; // Read & clear
  1482. }
  1483. return x ;
  1484. }
  1485. /*
  1486. * interruptHandler:
  1487. * This is a thread and gets started to wait for the interrupt we're
  1488. * hoping to catch. It will call the user-function when the interrupt
  1489. * fires.
  1490. *********************************************************************************
  1491. */
  1492. static void *interruptHandler (UNU void *arg)
  1493. {
  1494. int myPin ;
  1495. (void)piHiPri (55) ; // Only effective if we run as root
  1496. myPin = pinPass ;
  1497. pinPass = -1 ;
  1498. for (;;)
  1499. if (waitForInterrupt (myPin, -1) > 0)
  1500. isrFunctions [myPin] () ;
  1501. return NULL ;
  1502. }
  1503. /*
  1504. * wiringPiISR:
  1505. * Pi Specific.
  1506. * Take the details and create an interrupt handler that will do a call-
  1507. * back to the user supplied function.
  1508. *********************************************************************************
  1509. */
  1510. int wiringPiISR (int pin, int mode, void (*function)(void))
  1511. {
  1512. pthread_t threadId ;
  1513. const char *modeS ;
  1514. char fName [64] ;
  1515. char pinS [8] ;
  1516. pid_t pid ;
  1517. int count, i ;
  1518. char c ;
  1519. int bcmGpioPin ;
  1520. if ((pin < 0) || (pin > 63))
  1521. return wiringPiFailure (WPI_FATAL, "wiringPiISR: pin must be 0-63 (%d)\n", pin) ;
  1522. /**/ if (wiringPiMode == WPI_MODE_UNINITIALISED)
  1523. return wiringPiFailure (WPI_FATAL, "wiringPiISR: wiringPi has not been initialised. Unable to continue.\n") ;
  1524. else if (wiringPiMode == WPI_MODE_PINS)
  1525. bcmGpioPin = pinToGpio [pin] ;
  1526. else if (wiringPiMode == WPI_MODE_PHYS)
  1527. bcmGpioPin = physToGpio [pin] ;
  1528. else
  1529. bcmGpioPin = pin ;
  1530. // Now export the pin and set the right edge
  1531. // We're going to use the gpio program to do this, so it assumes
  1532. // a full installation of wiringPi. It's a bit 'clunky', but it
  1533. // is a way that will work when we're running in "Sys" mode, as
  1534. // a non-root user. (without sudo)
  1535. if (mode != INT_EDGE_SETUP)
  1536. {
  1537. /**/ if (mode == INT_EDGE_FALLING)
  1538. modeS = "falling" ;
  1539. else if (mode == INT_EDGE_RISING)
  1540. modeS = "rising" ;
  1541. else
  1542. modeS = "both" ;
  1543. sprintf (pinS, "%d", bcmGpioPin) ;
  1544. if ((pid = fork ()) < 0) // Fail
  1545. return wiringPiFailure (WPI_FATAL, "wiringPiISR: fork failed: %s\n", strerror (errno)) ;
  1546. if (pid == 0) // Child, exec
  1547. {
  1548. /**/ if (access ("/usr/local/bin/gpio", X_OK) == 0)
  1549. {
  1550. execl ("/usr/local/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ;
  1551. return wiringPiFailure (WPI_FATAL, "wiringPiISR: execl failed: %s\n", strerror (errno)) ;
  1552. }
  1553. else if (access ("/usr/bin/gpio", X_OK) == 0)
  1554. {
  1555. execl ("/usr/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ;
  1556. return wiringPiFailure (WPI_FATAL, "wiringPiISR: execl failed: %s\n", strerror (errno)) ;
  1557. }
  1558. else
  1559. return wiringPiFailure (WPI_FATAL, "wiringPiISR: Can't find gpio program\n") ;
  1560. }
  1561. else // Parent, wait
  1562. wait (NULL) ;
  1563. }
  1564. // Now pre-open the /sys/class node - but it may already be open if
  1565. // we are in Sys mode...
  1566. if (sysFds [bcmGpioPin] == -1)
  1567. {
  1568. sprintf (fName, "/sys/class/gpio/gpio%d/value", bcmGpioPin) ;
  1569. if ((sysFds [bcmGpioPin] = open (fName, O_RDWR)) < 0)
  1570. return wiringPiFailure (WPI_FATAL, "wiringPiISR: unable to open %s: %s\n", fName, strerror (errno)) ;
  1571. }
  1572. // Clear any initial pending interrupt
  1573. ioctl (sysFds [bcmGpioPin], FIONREAD, &count) ;
  1574. for (i = 0 ; i < count ; ++i)
  1575. read (sysFds [bcmGpioPin], &c, 1) ;
  1576. isrFunctions [pin] = function ;
  1577. pthread_mutex_lock (&pinMutex) ;
  1578. pinPass = pin ;
  1579. pthread_create (&threadId, NULL, interruptHandler, NULL) ;
  1580. while (pinPass != -1)
  1581. delay (1) ;
  1582. pthread_mutex_unlock (&pinMutex) ;
  1583. return 0 ;
  1584. }
  1585. /*
  1586. * initialiseEpoch:
  1587. * Initialise our start-of-time variable to be the current unix
  1588. * time in milliseconds and microseconds.
  1589. *********************************************************************************
  1590. */
  1591. static void initialiseEpoch (void)
  1592. {
  1593. #ifdef OLD_WAY
  1594. struct timeval tv ;
  1595. gettimeofday (&tv, NULL) ;
  1596. epochMilli = (uint64_t)tv.tv_sec * (uint64_t)1000 + (uint64_t)(tv.tv_usec / 1000) ;
  1597. epochMicro = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)(tv.tv_usec) ;
  1598. #else
  1599. struct timespec ts ;
  1600. clock_gettime (CLOCK_MONOTONIC_RAW, &ts) ;
  1601. epochMilli = (uint64_t)ts.tv_sec * (uint64_t)1000 + (uint64_t)(ts.tv_nsec / 1000000L) ;
  1602. epochMicro = (uint64_t)ts.tv_sec * (uint64_t)1000000 + (uint64_t)(ts.tv_nsec / 1000L) ;
  1603. #endif
  1604. }
  1605. /*
  1606. * delay:
  1607. * Wait for some number of milliseconds
  1608. *********************************************************************************
  1609. */
  1610. void delay (unsigned int howLong)
  1611. {
  1612. struct timespec sleeper, dummy ;
  1613. sleeper.tv_sec = (time_t)(howLong / 1000) ;
  1614. sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ;
  1615. nanosleep (&sleeper, &dummy) ;
  1616. }
  1617. /*
  1618. * delayMicroseconds:
  1619. * This is somewhat intersting. It seems that on the Pi, a single call
  1620. * to nanosleep takes some 80 to 130 microseconds anyway, so while
  1621. * obeying the standards (may take longer), it's not always what we
  1622. * want!
  1623. *
  1624. * So what I'll do now is if the delay is less than 100uS we'll do it
  1625. * in a hard loop, watching a built-in counter on the ARM chip. This is
  1626. * somewhat sub-optimal in that it uses 100% CPU, something not an issue
  1627. * in a microcontroller, but under a multi-tasking, multi-user OS, it's
  1628. * wastefull, however we've no real choice )-:
  1629. *
  1630. * Plan B: It seems all might not be well with that plan, so changing it
  1631. * to use gettimeofday () and poll on that instead...
  1632. *********************************************************************************
  1633. */
  1634. void delayMicrosecondsHard (unsigned int howLong)
  1635. {
  1636. struct timeval tNow, tLong, tEnd ;
  1637. gettimeofday (&tNow, NULL) ;
  1638. tLong.tv_sec = howLong / 1000000 ;
  1639. tLong.tv_usec = howLong % 1000000 ;
  1640. timeradd (&tNow, &tLong, &tEnd) ;
  1641. while (timercmp (&tNow, &tEnd, <))
  1642. gettimeofday (&tNow, NULL) ;
  1643. }
  1644. void delayMicroseconds (unsigned int howLong)
  1645. {
  1646. struct timespec sleeper ;
  1647. unsigned int uSecs = howLong % 1000000 ;
  1648. unsigned int wSecs = howLong / 1000000 ;
  1649. /**/ if (howLong == 0)
  1650. return ;
  1651. else if (howLong < 100)
  1652. delayMicrosecondsHard (howLong) ;
  1653. else
  1654. {
  1655. sleeper.tv_sec = wSecs ;
  1656. sleeper.tv_nsec = (long)(uSecs * 1000L) ;
  1657. nanosleep (&sleeper, NULL) ;
  1658. }
  1659. }
  1660. /*
  1661. * millis:
  1662. * Return a number of milliseconds as an unsigned int.
  1663. * Wraps at 49 days.
  1664. *********************************************************************************
  1665. */
  1666. unsigned int millis (void)
  1667. {
  1668. uint64_t now ;
  1669. #ifdef OLD_WAY
  1670. struct timeval tv ;
  1671. gettimeofday (&tv, NULL) ;
  1672. now = (uint64_t)tv.tv_sec * (uint64_t)1000 + (uint64_t)(tv.tv_usec / 1000) ;
  1673. #else
  1674. struct timespec ts ;
  1675. clock_gettime (CLOCK_MONOTONIC_RAW, &ts) ;
  1676. now = (uint64_t)ts.tv_sec * (uint64_t)1000 + (uint64_t)(ts.tv_nsec / 1000000L) ;
  1677. #endif
  1678. return (uint32_t)(now - epochMilli) ;
  1679. }
  1680. /*
  1681. * micros:
  1682. * Return a number of microseconds as an unsigned int.
  1683. * Wraps after 71 minutes.
  1684. *********************************************************************************
  1685. */
  1686. unsigned int micros (void)
  1687. {
  1688. uint64_t now ;
  1689. #ifdef OLD_WAY
  1690. struct timeval tv ;
  1691. gettimeofday (&tv, NULL) ;
  1692. now = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)tv.tv_usec ;
  1693. #else
  1694. struct timespec ts ;
  1695. clock_gettime (CLOCK_MONOTONIC_RAW, &ts) ;
  1696. now = (uint64_t)ts.tv_sec * (uint64_t)1000000 + (uint64_t)(ts.tv_nsec / 1000) ;
  1697. #endif
  1698. return (uint32_t)(now - epochMicro) ;
  1699. }
  1700. /*
  1701. * wiringPiVersion:
  1702. * Return our current version number
  1703. *********************************************************************************
  1704. */
  1705. void wiringPiVersion (int *major, int *minor)
  1706. {
  1707. *major = VERSION_MAJOR ;
  1708. *minor = VERSION_MINOR ;
  1709. }
  1710. /*
  1711. * wiringPiSetup:
  1712. * Must be called once at the start of your program execution.
  1713. *
  1714. * Default setup: Initialises the system into wiringPi Pin mode and uses the
  1715. * memory mapped hardware directly.
  1716. *
  1717. * Changed now to revert to "gpio" mode if we're running on a Compute Module.
  1718. *********************************************************************************
  1719. */
  1720. int wiringPiSetup (void)
  1721. {
  1722. int fd ;
  1723. int model, rev, mem, maker, overVolted ;
  1724. static int alreadyDoneThis = FALSE ;
  1725. // It's actually a fatal error to call any of the wiringPiSetup routines more than once,
  1726. // (you run out of file handles!) but I'm fed-up with the useless twats who email
  1727. // me bleating that there is a bug in my code, so screw-em.
  1728. if (alreadyDoneThis)
  1729. return 0 ;
  1730. alreadyDoneThis = TRUE ;
  1731. if (getenv (ENV_DEBUG) != NULL)
  1732. wiringPiDebug = TRUE ;
  1733. if (getenv (ENV_CODES) != NULL)
  1734. wiringPiReturnCodes = TRUE ;
  1735. if (wiringPiDebug)
  1736. printf ("wiringPi: wiringPiSetup called\n") ;
  1737. // Get the board ID information. We're not really using the information here,
  1738. // but it will give us information like the GPIO layout scheme (2 variants
  1739. // on the older 26-pin Pi's) and the GPIO peripheral base address.
  1740. // and if we're running on a compute module, then wiringPi pin numbers
  1741. // don't really many anything, so force native BCM mode anyway.
  1742. piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
  1743. if ((model == PI_MODEL_CM) || (model == PI_MODEL_CM3))
  1744. wiringPiMode = WPI_MODE_GPIO ;
  1745. else
  1746. wiringPiMode = WPI_MODE_PINS ;
  1747. /**/ if (piGpioLayout () == 1) // A, B, Rev 1, 1.1
  1748. {
  1749. pinToGpio = pinToGpioR1 ;
  1750. physToGpio = physToGpioR1 ;
  1751. }
  1752. else // A2, B2, A+, B+, CM, Pi2, Pi3, Zero
  1753. {
  1754. pinToGpio = pinToGpioR2 ;
  1755. physToGpio = physToGpioR2 ;
  1756. }
  1757. // ...
  1758. switch (model)
  1759. {
  1760. case PI_MODEL_A: case PI_MODEL_B:
  1761. case PI_MODEL_AP: case PI_MODEL_BP:
  1762. case PI_ALPHA: case PI_MODEL_CM: case PI_MODEL_ZERO:
  1763. piGpioBase = GPIO_PERI_BASE_OLD ;
  1764. break ;
  1765. default:
  1766. piGpioBase = GPIO_PERI_BASE_NEW ;
  1767. break ;
  1768. }
  1769. // Open the master /dev/ memory control device
  1770. // Device strategy: December 2016:
  1771. // Try /dev/mem. If that fails, then
  1772. // try /dev/gpiomem. If that fails then game over.
  1773. if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0)
  1774. {
  1775. if ((fd = open ("/dev/gpiomem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0)
  1776. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: Unable to open /dev/mem or /dev/gpiomem: %s.\n Try running with sudo?\n", strerror (errno)) ;
  1777. piGpioBase = 0 ;
  1778. }
  1779. // Set the offsets into the memory interface.
  1780. GPIO_PADS = piGpioBase + 0x00100000 ;
  1781. GPIO_CLOCK_BASE = piGpioBase + 0x00101000 ;
  1782. GPIO_BASE = piGpioBase + 0x00200000 ;
  1783. GPIO_TIMER = piGpioBase + 0x0000B000 ;
  1784. GPIO_PWM = piGpioBase + 0x0020C000 ;
  1785. // Map the individual hardware components
  1786. // GPIO:
  1787. gpio = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_BASE) ;
  1788. if (gpio == MAP_FAILED)
  1789. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (GPIO) failed: %s\n", strerror (errno)) ;
  1790. // PWM
  1791. pwm = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PWM) ;
  1792. if (pwm == MAP_FAILED)
  1793. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PWM) failed: %s\n", strerror (errno)) ;
  1794. // Clock control (needed for PWM)
  1795. clk = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_CLOCK_BASE) ;
  1796. if (clk == MAP_FAILED)
  1797. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (CLOCK) failed: %s\n", strerror (errno)) ;
  1798. // The drive pads
  1799. pads = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PADS) ;
  1800. if (pads == MAP_FAILED)
  1801. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PADS) failed: %s\n", strerror (errno)) ;
  1802. #ifdef USE_TIMER
  1803. // The system timer
  1804. timer = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_TIMER) ;
  1805. if (timer == MAP_FAILED)
  1806. return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (TIMER) failed: %s\n", strerror (errno)) ;
  1807. // Set the timer to free-running, 1MHz.
  1808. // 0xF9 is 249, the timer divide is base clock / (divide+1)
  1809. // so base clock is 250MHz / 250 = 1MHz.
  1810. *(timer + TIMER_CONTROL) = 0x0000280 ;
  1811. *(timer + TIMER_PRE_DIV) = 0x00000F9 ;
  1812. timerIrqRaw = timer + TIMER_IRQ_RAW ;
  1813. #endif
  1814. initialiseEpoch () ;
  1815. return 0 ;
  1816. }
  1817. /*
  1818. * wiringPiSetupGpio:
  1819. * Must be called once at the start of your program execution.
  1820. *
  1821. * GPIO setup: Initialises the system into GPIO Pin mode and uses the
  1822. * memory mapped hardware directly.
  1823. *********************************************************************************
  1824. */
  1825. int wiringPiSetupGpio (void)
  1826. {
  1827. (void)wiringPiSetup () ;
  1828. if (wiringPiDebug)
  1829. printf ("wiringPi: wiringPiSetupGpio called\n") ;
  1830. wiringPiMode = WPI_MODE_GPIO ;
  1831. return 0 ;
  1832. }
  1833. /*
  1834. * wiringPiSetupPhys:
  1835. * Must be called once at the start of your program execution.
  1836. *
  1837. * Phys setup: Initialises the system into Physical Pin mode and uses the
  1838. * memory mapped hardware directly.
  1839. *********************************************************************************
  1840. */
  1841. int wiringPiSetupPhys (void)
  1842. {
  1843. (void)wiringPiSetup () ;
  1844. if (wiringPiDebug)
  1845. printf ("wiringPi: wiringPiSetupPhys called\n") ;
  1846. wiringPiMode = WPI_MODE_PHYS ;
  1847. return 0 ;
  1848. }
  1849. /*
  1850. * wiringPiSetupSys:
  1851. * Must be called once at the start of your program execution.
  1852. *
  1853. * Initialisation (again), however this time we are using the /sys/class/gpio
  1854. * interface to the GPIO systems - slightly slower, but always usable as
  1855. * a non-root user, assuming the devices are already exported and setup correctly.
  1856. */
  1857. int wiringPiSetupSys (void)
  1858. {
  1859. int pin ;
  1860. char fName [128] ;
  1861. static int alreadyDoneThis = FALSE ;
  1862. // It's actually a fatal error to call any of the wiringPiSetup routines more than once,
  1863. // (you run out of file handles!) but I'm fed-up with the useless twats who email
  1864. // me bleating that there is a bug in my code, so screw-em.
  1865. if (alreadyDoneThis)
  1866. return 0 ;
  1867. alreadyDoneThis = TRUE ;
  1868. if (getenv (ENV_DEBUG) != NULL)
  1869. wiringPiDebug = TRUE ;
  1870. if (getenv (ENV_CODES) != NULL)
  1871. wiringPiReturnCodes = TRUE ;
  1872. if (wiringPiDebug)
  1873. printf ("wiringPi: wiringPiSetupSys called\n") ;
  1874. if (piGpioLayout () == 1)
  1875. {
  1876. pinToGpio = pinToGpioR1 ;
  1877. physToGpio = physToGpioR1 ;
  1878. }
  1879. else
  1880. {
  1881. pinToGpio = pinToGpioR2 ;
  1882. physToGpio = physToGpioR2 ;
  1883. }
  1884. // Open and scan the directory, looking for exported GPIOs, and pre-open
  1885. // the 'value' interface to speed things up for later
  1886. for (pin = 0 ; pin < 64 ; ++pin)
  1887. {
  1888. sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
  1889. sysFds [pin] = open (fName, O_RDWR) ;
  1890. }
  1891. initialiseEpoch () ;
  1892. wiringPiMode = WPI_MODE_GPIO_SYS ;
  1893. return 0 ;
  1894. }