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

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