Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

wiringPi.c 71 KiB

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