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

2772 line
80 KiB

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