Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 

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