You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

2008 line
56 KiB

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