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

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