25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

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