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.
 
 
 
 
 

3137 lines
91 KiB

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