Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 

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