Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 

2540 řádky
72 KiB

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