3 * Arduino look-a-like Wiring library for the Raspberry Pi
4 * Copyright (c) 2012-2015 Gordon Henderson
5 * Additional code for pwmSetClock by Chris Hall <chris@kchall.plus.com>
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://projects.drogon.net/raspberry-pi/wiringpi/
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.
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.
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 ***********************************************************************
33 // Added an abstraction layer to the main routines to save a tiny
34 // bit of run-time and make the clode a little cleaner (if a little
36 // Added waitForInterrupt code
40 // Added in support to use the /sys/class/gpio interface.
42 // Fixed a few more bugs to do with range-checking when in GPIO mode.
45 // Added c++ support for the .h file
46 // Added a new function to allow for using my "pin" numbers, or native
48 // Removed my busy-loop delay and replaced it with a call to delayMicroseconds
51 // Added in the 2 UART pins
52 // Change maxPins to numPins to more accurately reflect purpose
71 #include <sys/ioctl.h>
83 // Environment Variables
85 #define ENV_DEBUG "WIRINGPI_DEBUG"
86 #define ENV_CODES "WIRINGPI_CODES"
89 // Mask for the bottom 64 pins which belong to the Raspberry Pi
90 // The others are available for the other devices
92 #define PI_GPIO_MASK (0xFFFFFFC0)
94 struct wiringPiNodeStruct *wiringPiNodes = NULL ;
98 #define BCM_PASSWORD 0x5A000000
101 // The BCM2835 has 54 GPIO pins.
102 // BCM2835 data sheet, Page 90 onwards.
103 // There are 6 control registers, each control the functions of a block
105 // Each control register has 10 sets of 3 bits per GPIO pin - the ALT values
107 // 000 = GPIO Pin X is an input
108 // 001 = GPIO Pin X is an output
109 // 100 = GPIO Pin X takes alternate function 0
110 // 101 = GPIO Pin X takes alternate function 1
111 // 110 = GPIO Pin X takes alternate function 2
112 // 111 = GPIO Pin X takes alternate function 3
113 // 011 = GPIO Pin X takes alternate function 4
114 // 010 = GPIO Pin X takes alternate function 5
116 // So the 3 bits for port X are:
117 // X / 10 + ((X % 10) * 3)
119 // Port function select bits
121 #define FSEL_INPT 0b000
122 #define FSEL_OUTP 0b001
123 #define FSEL_ALT0 0b100
124 #define FSEL_ALT1 0b101
125 #define FSEL_ALT2 0b110
126 #define FSEL_ALT3 0b111
127 #define FSEL_ALT4 0b011
128 #define FSEL_ALT5 0b010
130 // Access from ARM Running Linux
131 // Taken from Gert/Doms code. Some of this is not in the manual
132 // that I can find )-:
134 // Updates in September 2015 - all now static variables (and apologies for the caps)
135 // due to the Pi v2 and the new /dev/gpiomem interface
137 static volatile unsigned int RASPBERRY_PI_PERI_BASE ;
138 static volatile unsigned int GPIO_PADS ;
139 static volatile unsigned int GPIO_CLOCK_BASE ;
140 static volatile unsigned int GPIO_BASE ;
141 static volatile unsigned int GPIO_TIMER ;
142 static volatile unsigned int GPIO_PWM ;
144 #define PAGE_SIZE (4*1024)
145 #define BLOCK_SIZE (4*1024)
148 // Word offsets into the PWM control region
150 #define PWM_CONTROL 0
157 // Clock regsiter offsets
159 #define PWMCLK_CNTL 40
160 #define PWMCLK_DIV 41
162 #define PWM0_MS_MODE 0x0080 // Run in MS mode
163 #define PWM0_USEFIFO 0x0020 // Data from FIFO
164 #define PWM0_REVPOLAR 0x0010 // Reverse polarity
165 #define PWM0_OFFSTATE 0x0008 // Ouput Off state
166 #define PWM0_REPEATFF 0x0004 // Repeat last value if FIFO empty
167 #define PWM0_SERIAL 0x0002 // Run in serial mode
168 #define PWM0_ENABLE 0x0001 // Channel Enable
170 #define PWM1_MS_MODE 0x8000 // Run in MS mode
171 #define PWM1_USEFIFO 0x2000 // Data from FIFO
172 #define PWM1_REVPOLAR 0x1000 // Reverse polarity
173 #define PWM1_OFFSTATE 0x0800 // Ouput Off state
174 #define PWM1_REPEATFF 0x0400 // Repeat last value if FIFO empty
175 #define PWM1_SERIAL 0x0200 // Run in serial mode
176 #define PWM1_ENABLE 0x0100 // Channel Enable
181 #define TIMER_LOAD (0x400 >> 2)
182 #define TIMER_VALUE (0x404 >> 2)
183 #define TIMER_CONTROL (0x408 >> 2)
184 #define TIMER_IRQ_CLR (0x40C >> 2)
185 #define TIMER_IRQ_RAW (0x410 >> 2)
186 #define TIMER_IRQ_MASK (0x414 >> 2)
187 #define TIMER_RELOAD (0x418 >> 2)
188 #define TIMER_PRE_DIV (0x41C >> 2)
189 #define TIMER_COUNTER (0x420 >> 2)
191 // Locals to hold pointers to the hardware
193 static volatile uint32_t *gpio ;
194 static volatile uint32_t *pwm ;
195 static volatile uint32_t *clk ;
196 static volatile uint32_t *pads ;
199 static volatile uint32_t *timer ;
200 static volatile uint32_t *timerIrqRaw ;
204 // Data for use with the boardId functions.
205 // The order of entries here to correspond with the PI_MODEL_X
206 // and PI_VERSION_X defines in wiringPi.h
207 // Only intended for the gpio command - use at your own risk!
209 static int piModel2 = FALSE ;
211 const char *piModelNames [7] =
219 "Model 2", // Quad Core
222 const char *piRevisionNames [5] =
231 const char *piMakerNames [5] =
241 // Time for easy calculations
243 static uint64_t epochMilli, epochMicro ;
247 static int wiringPiMode = WPI_MODE_UNINITIALISED ;
248 static volatile int pinPass = -1 ;
249 static pthread_mutex_t pinMutex ;
251 // Debugging & Return codes
253 int wiringPiDebug = FALSE ;
254 int wiringPiReturnCodes = FALSE ;
257 // Map a file descriptor from the /sys/class/gpio/gpioX/value
259 static int sysFds [64] =
261 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
262 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
263 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
264 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
269 static void (*isrFunctions [64])(void) ;
272 // Doing it the Arduino way with lookup tables...
273 // Yes, it's probably more innefficient than all the bit-twidling, but it
274 // does tend to make it all a bit clearer. At least to me!
277 // Take a Wiring pin (0 through X) and re-map it to the BCM_GPIO pin
278 // Cope for 3 different board revisions here.
280 static int *pinToGpio ;
284 static int pinToGpioR1 [64] =
286 17, 18, 21, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7: wpi 0 - 7
287 0, 1, // I2C - SDA1, SCL1 wpi 8 - 9
288 8, 7, // SPI - CE1, CE0 wpi 10 - 11
289 10, 9, 11, // SPI - MOSI, MISO, SCLK wpi 12 - 14
290 14, 15, // UART - Tx, Rx wpi 15 - 16
294 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 31
295 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
296 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
301 static int pinToGpioR2 [64] =
303 17, 18, 27, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7: wpi 0 - 7
304 2, 3, // I2C - SDA0, SCL0 wpi 8 - 9
305 8, 7, // SPI - CE1, CE0 wpi 10 - 11
306 10, 9, 11, // SPI - MOSI, MISO, SCLK wpi 12 - 14
307 14, 15, // UART - Tx, Rx wpi 15 - 16
308 28, 29, 30, 31, // Rev 2: New GPIOs 8 though 11 wpi 17 - 20
309 5, 6, 13, 19, 26, // B+ wpi 21, 22, 23, 24, 25
310 12, 16, 20, 21, // B+ wpi 26, 27, 28, 29
311 0, 1, // B+ wpi 30, 31
315 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
316 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
321 // Take a physical pin (1 through 26) and re-map it to the BCM_GPIO pin
322 // Cope for 2 different board revisions here.
323 // Also add in the P5 connector, so the P5 pins are 3,4,5,6, so 53,54,55,56
325 static int *physToGpio ;
327 static int physToGpioR1 [64] =
344 -1, -1, -1, -1, -1, // ... 31
345 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
346 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
349 static int physToGpioR2 [64] =
376 // the P5 connector on the Rev 2 boards:
392 // Map a BCM_GPIO pin to it's Function Selection
393 // control port. (GPFSEL 0-5)
394 // Groups of 10 - 3 bits per Function - 30 bits per port
396 static uint8_t gpioToGPFSEL [] =
398 0,0,0,0,0,0,0,0,0,0,
399 1,1,1,1,1,1,1,1,1,1,
400 2,2,2,2,2,2,2,2,2,2,
401 3,3,3,3,3,3,3,3,3,3,
402 4,4,4,4,4,4,4,4,4,4,
403 5,5,5,5,5,5,5,5,5,5,
408 // Define the shift up for the 3 bits per pin in each GPFSEL port
410 static uint8_t gpioToShift [] =
412 0,3,6,9,12,15,18,21,24,27,
413 0,3,6,9,12,15,18,21,24,27,
414 0,3,6,9,12,15,18,21,24,27,
415 0,3,6,9,12,15,18,21,24,27,
416 0,3,6,9,12,15,18,21,24,27,
421 // (Word) offset to the GPIO Set registers for each GPIO pin
423 static uint8_t gpioToGPSET [] =
425 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,
426 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,
430 // (Word) offset to the GPIO Clear registers for each GPIO pin
432 static uint8_t gpioToGPCLR [] =
434 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,
435 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,
440 // (Word) offset to the GPIO Input level registers for each GPIO pin
442 static uint8_t gpioToGPLEV [] =
444 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,
445 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,
451 // (Word) offset to the Event Detect Status
453 static uint8_t gpioToEDS [] =
455 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,
456 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,
460 // (Word) offset to the Rising edge ENable register
462 static uint8_t gpioToREN [] =
464 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,
465 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,
469 // (Word) offset to the Falling edgde ENable register
471 static uint8_t gpioToFEN [] =
473 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,
474 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,
480 // GPIO Pin pull up/down register
485 // (Word) offset to the Pull Up Down Clock regsiter
487 static uint8_t gpioToPUDCLK [] =
489 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,
490 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,
495 // the ALT value to put a GPIO pin into PWM mode
497 static uint8_t gpioToPwmALT [] =
499 0, 0, 0, 0, 0, 0, 0, 0, // 0 -> 7
500 0, 0, 0, 0, FSEL_ALT0, FSEL_ALT0, 0, 0, // 8 -> 15
501 0, 0, FSEL_ALT5, FSEL_ALT5, 0, 0, 0, 0, // 16 -> 23
502 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
503 0, 0, 0, 0, 0, 0, 0, 0, // 32 -> 39
504 FSEL_ALT0, FSEL_ALT0, 0, 0, 0, FSEL_ALT0, 0, 0, // 40 -> 47
505 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
506 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
511 // The port value to put a GPIO pin into PWM mode
513 static uint8_t gpioToPwmPort [] =
515 0, 0, 0, 0, 0, 0, 0, 0, // 0 -> 7
516 0, 0, 0, 0, PWM0_DATA, PWM1_DATA, 0, 0, // 8 -> 15
517 0, 0, PWM0_DATA, PWM1_DATA, 0, 0, 0, 0, // 16 -> 23
518 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
519 0, 0, 0, 0, 0, 0, 0, 0, // 32 -> 39
520 PWM0_DATA, PWM1_DATA, 0, 0, 0, PWM1_DATA, 0, 0, // 40 -> 47
521 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
522 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
527 // ALT value to put a GPIO pin into GP Clock mode.
528 // On the Pi we can really only use BCM_GPIO_4 and BCM_GPIO_21
529 // for clocks 0 and 1 respectively, however I'll include the full
530 // list for completeness - maybe one day...
532 #define GPIO_CLOCK_SOURCE 1
536 static uint8_t gpioToGpClkALT0 [] =
538 0, 0, 0, 0, FSEL_ALT0, FSEL_ALT0, FSEL_ALT0, 0, // 0 -> 7
539 0, 0, 0, 0, 0, 0, 0, 0, // 8 -> 15
540 0, 0, 0, 0, FSEL_ALT5, FSEL_ALT5, 0, 0, // 16 -> 23
541 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
542 FSEL_ALT0, 0, FSEL_ALT0, 0, 0, 0, 0, 0, // 32 -> 39
543 0, 0, FSEL_ALT0, FSEL_ALT0, FSEL_ALT0, 0, 0, 0, // 40 -> 47
544 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
545 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
549 // (word) Offsets to the clock Control and Divisor register
551 static uint8_t gpioToClkCon [] =
553 -1, -1, -1, -1, 28, 30, 32, -1, // 0 -> 7
554 -1, -1, -1, -1, -1, -1, -1, -1, // 8 -> 15
555 -1, -1, -1, -1, 28, 30, -1, -1, // 16 -> 23
556 -1, -1, -1, -1, -1, -1, -1, -1, // 24 -> 31
557 28, -1, 28, -1, -1, -1, -1, -1, // 32 -> 39
558 -1, -1, 28, 30, 28, -1, -1, -1, // 40 -> 47
559 -1, -1, -1, -1, -1, -1, -1, -1, // 48 -> 55
560 -1, -1, -1, -1, -1, -1, -1, -1, // 56 -> 63
563 static uint8_t gpioToClkDiv [] =
565 -1, -1, -1, -1, 29, 31, 33, -1, // 0 -> 7
566 -1, -1, -1, -1, -1, -1, -1, -1, // 8 -> 15
567 -1, -1, -1, -1, 29, 31, -1, -1, // 16 -> 23
568 -1, -1, -1, -1, -1, -1, -1, -1, // 24 -> 31
569 29, -1, 29, -1, -1, -1, -1, -1, // 32 -> 39
570 -1, -1, 29, 31, 29, -1, -1, -1, // 40 -> 47
571 -1, -1, -1, -1, -1, -1, -1, -1, // 48 -> 55
572 -1, -1, -1, -1, -1, -1, -1, -1, // 56 -> 63
578 *********************************************************************************
585 *********************************************************************************
588 int wiringPiFailure (int fatal, const char *message, ...)
593 if (!fatal && wiringPiReturnCodes)
596 va_start (argp, message) ;
597 vsnprintf (buffer, 1023, message, argp) ;
600 fprintf (stderr, "%s", buffer) ;
601 exit (EXIT_FAILURE) ;
609 * Return a number representing the hardware revision of the board.
611 * Revision 1 really means the early Model B's.
612 * Revision 2 is everything else - it covers the B, B+ and CM.
613 * ... and the Pi 2 - which is a B+ ++ ...
615 * Seems there are some boards with 0000 in them (mistake in manufacture)
616 * So the distinction between boards that I can see is:
619 * 0002 - Model B, Rev 1, 256MB, Egoman
620 * 0003 - Model B, Rev 1.1, 256MB, Egoman, Fuses/D14 removed.
621 * 0004 - Model B, Rev 2, 256MB, Sony
622 * 0005 - Model B, Rev 2, 256MB, Qisda
623 * 0006 - Model B, Rev 2, 256MB, Egoman
624 * 0007 - Model A, Rev 2, 256MB, Egoman
625 * 0008 - Model A, Rev 2, 256MB, Sony
626 * 0009 - Model A, Rev 2, 256MB, Qisda
627 * 000d - Model B, Rev 2, 512MB, Egoman
628 * 000e - Model B, Rev 2, 512MB, Sony
629 * 000f - Model B, Rev 2, 512MB, Qisda
630 * 0010 - Model B+, Rev 1.2, 512MB, Sony
631 * 0011 - Pi CM, Rev 1.2, 512MB, Sony
632 * 0012 - Model A+ Rev 1.2, 256MB, Sony
633 * 0014 - Pi CM, Rev 1.1, 512MB, Sony (Actual Revision might be different)
634 * 0015 - Model A+ Rev 1.1, 256MB, Sony
637 * 0010 - Model 2, Rev 1.1, Quad Core, 1GB, Sony
639 * A small thorn is the olde style overvolting - that will add in
642 * The Pi compute module has an revision of 0011 - since we only check the
643 * last digit, then it's 1, therefore it'll default to not 2 or 3 for a
644 * Rev 1, so will appear as a Rev 2. This is fine for the most part, but
645 * we'll properly detect the Compute Module later and adjust accordingly.
646 * And the next rev of the CN is 0014 ...
648 *********************************************************************************
651 static void piBoardRevOops (const char *why)
653 fprintf (stderr, "piBoardRev: Unable to determine board revision from /proc/cpuinfo\n") ;
654 fprintf (stderr, " -> %s\n", why) ;
655 fprintf (stderr, " -> You may want to check:\n") ;
656 fprintf (stderr, " -> http://www.raspberrypi.org/phpBB3/viewtopic.php?p=184410#p184410\n") ;
657 exit (EXIT_FAILURE) ;
660 int piBoardRev (void)
665 static int boardRev = -1 ;
667 if (boardRev != -1) // No point checking twice
670 if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL)
671 piBoardRevOops ("Unable to open /proc/cpuinfo") ;
673 // Start by looking for the Architecture, then we can look for a B2 revision....
675 while (fgets (line, 120, cpuFd) != NULL)
676 if (strncmp (line, "Hardware", 8) == 0)
679 if (strncmp (line, "Hardware", 8) != 0)
680 piBoardRevOops ("No \"Hardware\" line") ;
683 printf ("piboardRev: Hardware: %s\n", line) ;
685 // See if it's BCM2708 or BCM2709
687 if (strstr (line, "BCM2709") != NULL)
689 else if (strstr (line, "BCM2708") == NULL)
691 fprintf (stderr, "Unable to determine hardware version. I see: %s,\n", line) ;
692 fprintf (stderr, " - expecting BCM2708 or BCM2709.\n") ;
693 fprintf (stderr, "If this is a genuine Raspberry Pi then please report this\n") ;
694 fprintf (stderr, "to projects@drogon.net. If this is not a Raspberry Pi then you\n") ;
695 fprintf (stderr, "are on your own as wiringPi is designed to support the\n") ;
696 fprintf (stderr, "Raspberry Pi ONLY.\n") ;
697 exit (EXIT_FAILURE) ;
700 // Now do the rest of it as before
704 while (fgets (line, 120, cpuFd) != NULL)
705 if (strncmp (line, "Revision", 8) == 0)
710 if (strncmp (line, "Revision", 8) != 0)
711 piBoardRevOops ("No \"Revision\" line") ;
713 // Chomp trailing CR/NL
715 for (c = &line [strlen (line) - 1] ; (*c == '\n') || (*c == '\r') ; --c)
719 printf ("piboardRev: Revision string: %s\n", line) ;
721 // Scan to first digit
723 for (c = line ; *c ; ++c)
728 piBoardRevOops ("No numeric revision string") ;
730 // Make sure its long enough
733 piBoardRevOops ("Bogus \"Revision\" line (too small)") ;
735 // If you have overvolted the Pi, then it appears that the revision
736 // has 100000 added to it!
737 // The actual condition for it being set is:
738 // (force_turbo || current_limit_override || temp_limit>85) && over_voltage>0
742 printf ("piboardRev: This Pi has/is (force_turbo || current_limit_override || temp_limit>85) && over_voltage>0\n") ;
744 // Isolate last 4 characters:
746 c = c + strlen (c) - 4 ;
749 printf ("piboardRev: last4Chars are: \"%s\"\n", c) ;
751 if ( (strcmp (c, "0002") == 0) || (strcmp (c, "0003") == 0))
754 boardRev = 2 ; // Covers everything else from the B revision 2 to the B+, the Pi v2 and CM's.
757 printf ("piBoardRev: Returning revision: %d\n", boardRev) ;
765 * Do more digging into the board revision string as above, but return
766 * as much details as we can.
767 * This is undocumented and really only intended for the GPIO command.
768 * Use at your own risk!
771 * [USER:8] [NEW:1] [MEMSIZE:3] [MANUFACTURER:4] [PROCESSOR:4] [TYPE:8] [REV:4]
772 * NEW 23: will be 1 for the new scheme, 0 for the old scheme
773 * MEMSIZE 20: 0=256M 1=512M 2=1G
774 * MANUFACTURER 16: 0=SONY 1=EGOMAN 2=EMBEST
775 * PROCESSOR 12: 0=2835 1=2836
776 * TYPE 04: 0=MODELA 1=MODELB 2=MODELA+ 3=MODELB+ 4=Pi2 MODEL B 5=ALPHA 6=CM
777 * REV 00: 0=REV0 1=REV1 2=REV2
778 *********************************************************************************
781 void piBoardId (int *model, int *rev, int *mem, int *maker, int *overVolted)
787 // Will deal with the properly later on - for now, lets just get it going...
788 // unsigned int modelNum ;
790 (void)piBoardRev () ; // Call this first to make sure all's OK. Don't care about the result.
792 if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL)
793 piBoardRevOops ("Unable to open /proc/cpuinfo") ;
795 while (fgets (line, 120, cpuFd) != NULL)
796 if (strncmp (line, "Revision", 8) == 0)
801 if (strncmp (line, "Revision", 8) != 0)
802 piBoardRevOops ("No \"Revision\" line") ;
804 // Chomp trailing CR/NL
806 for (c = &line [strlen (line) - 1] ; (*c == '\n') || (*c == '\r') ; --c)
810 printf ("piboardId: Revision string: %s\n", line) ;
817 for (c = line ; *c ; ++c)
822 piBoardRevOops ("Bogus \"Revision\" line (no colon)") ;
824 // modelNum = (unsigned int)strtol (++c, NULL, 16) ; // Hex number with no leading 0x
826 *model = PI_MODEL_2 ;
827 *rev = PI_VERSION_1_1 ;
829 *maker = PI_MAKER_SONY ;
834 // Scan to first digit
836 for (c = line ; *c ; ++c)
840 // Make sure its long enough
843 piBoardRevOops ("Bogus \"Revision\" line (not long enough)") ;
845 // If longer than 4, we'll assume it's been overvolted
847 *overVolted = strlen (c) > 4 ;
849 // Extract last 4 characters:
851 c = c + strlen (c) - 4 ;
853 // Fill out the replys as appropriate
855 /**/ if (strcmp (c, "0002") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1 ; *mem = 256 ; *maker = PI_MAKER_EGOMAN ; }
856 else if (strcmp (c, "0003") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_1 ; *mem = 256 ; *maker = PI_MAKER_EGOMAN ; }
857 else if (strcmp (c, "0004") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_SONY ; }
858 else if (strcmp (c, "0005") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_QISDA ; }
859 else if (strcmp (c, "0006") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_EGOMAN ; }
860 else if (strcmp (c, "0007") == 0) { *model = PI_MODEL_A ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_EGOMAN ; }
861 else if (strcmp (c, "0008") == 0) { *model = PI_MODEL_A ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_SONY ; ; }
862 else if (strcmp (c, "0009") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_QISDA ; }
863 else if (strcmp (c, "000d") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 512 ; *maker = PI_MAKER_EGOMAN ; }
864 else if (strcmp (c, "000e") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 512 ; *maker = PI_MAKER_SONY ; }
865 else if (strcmp (c, "000f") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 512 ; *maker = PI_MAKER_EGOMAN ; }
866 else if (strcmp (c, "0010") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 512 ; *maker = PI_MAKER_SONY ; }
867 else if (strcmp (c, "0011") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_2 ; *mem = 512 ; *maker = PI_MAKER_SONY ; }
868 else if (strcmp (c, "0012") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_2 ; *mem = 256 ; *maker = PI_MAKER_SONY ; }
869 else if (strcmp (c, "0013") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 512 ; *maker = PI_MAKER_MBEST ; }
870 else if (strcmp (c, "0014") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_2 ; *mem = 512 ; *maker = PI_MAKER_SONY ; }
871 else if (strcmp (c, "0015") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_1 ; *mem = 256 ; *maker = PI_MAKER_SONY ; }
872 else { *model = 0 ; *rev = 0 ; *mem = 0 ; *maker = 0 ; }
880 * Translate a wiringPi Pin number to native GPIO pin number.
881 * Provided for external support.
882 *********************************************************************************
885 int wpiPinToGpio (int wpiPin)
887 return pinToGpio [wpiPin & 63] ;
893 * Translate a physical Pin number to native GPIO pin number.
894 * Provided for external support.
895 *********************************************************************************
898 int physPinToGpio (int physPin)
900 return physToGpio [physPin & 63] ;
906 * Set the PAD driver value
907 *********************************************************************************
910 void setPadDrive (int group, int value)
914 if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
916 if ((group < 0) || (group > 2))
919 wrVal = BCM_PASSWORD | 0x18 | (value & 7) ;
920 *(pads + group + 11) = wrVal ;
924 printf ("setPadDrive: Group: %d, value: %d (%08X)\n", group, value, wrVal) ;
925 printf ("Read : %08X\n", *(pads + group + 11)) ;
933 * Returns the ALT bits for a given port. Only really of-use
934 * for the gpio readall command (I think)
935 *********************************************************************************
940 int fSel, shift, alt ;
944 /**/ if (wiringPiMode == WPI_MODE_PINS)
945 pin = pinToGpio [pin] ;
946 else if (wiringPiMode == WPI_MODE_PHYS)
947 pin = physToGpio [pin] ;
948 else if (wiringPiMode != WPI_MODE_GPIO)
951 fSel = gpioToGPFSEL [pin] ;
952 shift = gpioToShift [pin] ;
954 alt = (*(gpio + fSel) >> shift) & 7 ;
962 * Select the native "balanced" mode, or standard mark:space mode
963 *********************************************************************************
966 void pwmSetMode (int mode)
968 if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
970 if (mode == PWM_MODE_MS)
971 *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE | PWM0_MS_MODE | PWM1_MS_MODE ;
973 *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ;
980 * Set the PWM range register. We set both range registers to the same
981 * value. If you want different in your own code, then write your own.
982 *********************************************************************************
985 void pwmSetRange (unsigned int range)
987 if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
989 *(pwm + PWM0_RANGE) = range ; delayMicroseconds (10) ;
990 *(pwm + PWM1_RANGE) = range ; delayMicroseconds (10) ;
997 * Set/Change the PWM clock. Originally my code, but changed
998 * (for the better!) by Chris Hall, <chris@kchall.plus.com>
999 * after further study of the manual and testing with a 'scope
1000 *********************************************************************************
1003 void pwmSetClock (int divisor)
1005 uint32_t pwm_control ;
1008 if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
1011 printf ("Setting to: %d. Current: 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;
1013 pwm_control = *(pwm + PWM_CONTROL) ; // preserve PWM_CONTROL
1015 // We need to stop PWM prior to stopping PWM clock in MS mode otherwise BUSY
1018 *(pwm + PWM_CONTROL) = 0 ; // Stop PWM
1020 // Stop PWM clock before changing divisor. The delay after this does need to
1021 // this big (95uS occasionally fails, 100uS OK), it's almost as though the BUSY
1022 // flag is not working properly in balanced mode. Without the delay when DIV is
1023 // adjusted the clock sometimes switches to very slow, once slow further DIV
1024 // adjustments do nothing and it's difficult to get out of this mode.
1026 *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x01 ; // Stop PWM Clock
1027 delayMicroseconds (110) ; // prevents clock going sloooow
1029 while ((*(clk + PWMCLK_CNTL) & 0x80) != 0) // Wait for clock to be !BUSY
1030 delayMicroseconds (1) ;
1032 *(clk + PWMCLK_DIV) = BCM_PASSWORD | (divisor << 12) ;
1034 *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x11 ; // Start PWM clock
1035 *(pwm + PWM_CONTROL) = pwm_control ; // restore PWM_CONTROL
1038 printf ("Set to: %d. Now : 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;
1045 * Set the freuency on a GPIO clock pin
1046 *********************************************************************************
1049 void gpioClockSet (int pin, int freq)
1051 int divi, divr, divf ;
1055 /**/ if (wiringPiMode == WPI_MODE_PINS)
1056 pin = pinToGpio [pin] ;
1057 else if (wiringPiMode == WPI_MODE_PHYS)
1058 pin = physToGpio [pin] ;
1059 else if (wiringPiMode != WPI_MODE_GPIO)
1062 divi = 19200000 / freq ;
1063 divr = 19200000 % freq ;
1064 divf = (int)((double)divr * 4096.0 / 19200000.0) ;
1069 *(clk + gpioToClkCon [pin]) = BCM_PASSWORD | GPIO_CLOCK_SOURCE ; // Stop GPIO Clock
1070 while ((*(clk + gpioToClkCon [pin]) & 0x80) != 0) // ... and wait
1073 *(clk + gpioToClkDiv [pin]) = BCM_PASSWORD | (divi << 12) | divf ; // Set dividers
1074 *(clk + gpioToClkCon [pin]) = BCM_PASSWORD | 0x10 | GPIO_CLOCK_SOURCE ; // Start Clock
1080 * Locate our device node
1081 *********************************************************************************
1084 struct wiringPiNodeStruct *wiringPiFindNode (int pin)
1086 struct wiringPiNodeStruct *node = wiringPiNodes ;
1088 while (node != NULL)
1089 if ((pin >= node->pinBase) && (pin <= node->pinMax))
1100 * Create a new GPIO node into the wiringPi handling system
1101 *********************************************************************************
1104 static void pinModeDummy (struct wiringPiNodeStruct *node, int pin, int mode) { return ; }
1105 static void pullUpDnControlDummy (struct wiringPiNodeStruct *node, int pin, int pud) { return ; }
1106 static int digitalReadDummy (struct wiringPiNodeStruct *node, int pin) { return LOW ; }
1107 static void digitalWriteDummy (struct wiringPiNodeStruct *node, int pin, int value) { return ; }
1108 static void pwmWriteDummy (struct wiringPiNodeStruct *node, int pin, int value) { return ; }
1109 static int analogReadDummy (struct wiringPiNodeStruct *node, int pin) { return 0 ; }
1110 static void analogWriteDummy (struct wiringPiNodeStruct *node, int pin, int value) { return ; }
1112 struct wiringPiNodeStruct *wiringPiNewNode (int pinBase, int numPins)
1115 struct wiringPiNodeStruct *node ;
1117 // Minimum pin base is 64
1120 (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: pinBase of %d is < 64\n", pinBase) ;
1122 // Check all pins in-case there is overlap:
1124 for (pin = pinBase ; pin < (pinBase + numPins) ; ++pin)
1125 if (wiringPiFindNode (pin) != NULL)
1126 (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: Pin %d overlaps with existing definition\n", pin) ;
1128 node = (struct wiringPiNodeStruct *)calloc (sizeof (struct wiringPiNodeStruct), 1) ; // calloc zeros
1130 (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: Unable to allocate memory: %s\n", strerror (errno)) ;
1132 node->pinBase = pinBase ;
1133 node->pinMax = pinBase + numPins - 1 ;
1134 node->pinMode = pinModeDummy ;
1135 node->pullUpDnControl = pullUpDnControlDummy ;
1136 node->digitalRead = digitalReadDummy ;
1137 node->digitalWrite = digitalWriteDummy ;
1138 node->pwmWrite = pwmWriteDummy ;
1139 node->analogRead = analogReadDummy ;
1140 node->analogWrite = analogWriteDummy ;
1141 node->next = wiringPiNodes ;
1142 wiringPiNodes = node ;
1152 * Enables edge-detect mode on a pin - from a 0 to a 1 or 1 to 0
1153 * Pin must already be in input mode with appropriate pull up/downs set.
1154 *********************************************************************************
1157 void pinEnableED01Pi (int pin)
1159 pin = pinToGpio [pin & 63] ;
1165 *********************************************************************************
1167 *********************************************************************************
1172 * This is an un-documented special to let you set any pin to any mode
1173 *********************************************************************************
1176 void pinModeAlt (int pin, int mode)
1180 if ((pin & PI_GPIO_MASK) == 0) // On-board pin
1182 /**/ if (wiringPiMode == WPI_MODE_PINS)
1183 pin = pinToGpio [pin] ;
1184 else if (wiringPiMode == WPI_MODE_PHYS)
1185 pin = physToGpio [pin] ;
1186 else if (wiringPiMode != WPI_MODE_GPIO)
1189 fSel = gpioToGPFSEL [pin] ;
1190 shift = gpioToShift [pin] ;
1192 *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | ((mode & 0x7) << shift) ;
1199 * Sets the mode of a pin to be input, output or PWM output
1200 *********************************************************************************
1203 void pinMode (int pin, int mode)
1205 int fSel, shift, alt ;
1206 struct wiringPiNodeStruct *node = wiringPiNodes ;
1209 if ((pin & PI_GPIO_MASK) == 0) // On-board pin
1211 /**/ if (wiringPiMode == WPI_MODE_PINS)
1212 pin = pinToGpio [pin] ;
1213 else if (wiringPiMode == WPI_MODE_PHYS)
1214 pin = physToGpio [pin] ;
1215 else if (wiringPiMode != WPI_MODE_GPIO)
1218 softPwmStop (origPin) ;
1219 softToneStop (origPin) ;
1221 fSel = gpioToGPFSEL [pin] ;
1222 shift = gpioToShift [pin] ;
1224 /**/ if (mode == INPUT)
1225 *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) ; // Sets bits to zero = input
1226 else if (mode == OUTPUT)
1227 *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (1 << shift) ;
1228 else if (mode == SOFT_PWM_OUTPUT)
1229 softPwmCreate (origPin, 0, 100) ;
1230 else if (mode == SOFT_TONE_OUTPUT)
1231 softToneCreate (origPin) ;
1232 else if (mode == PWM_TONE_OUTPUT)
1234 pinMode (origPin, PWM_OUTPUT) ; // Call myself to enable PWM mode
1235 pwmSetMode (PWM_MODE_MS) ;
1237 else if (mode == PWM_OUTPUT)
1239 if ((alt = gpioToPwmALT [pin]) == 0) // Not a hardware capable PWM pin
1242 // Set pin to PWM mode
1244 *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ;
1245 delayMicroseconds (110) ; // See comments in pwmSetClockWPi
1247 pwmSetMode (PWM_MODE_BAL) ; // Pi default mode
1248 pwmSetRange (1024) ; // Default range of 1024
1249 pwmSetClock (32) ; // 19.2 / 32 = 600KHz - Also starts the PWM
1251 else if (mode == GPIO_CLOCK)
1253 if ((alt = gpioToGpClkALT0 [pin]) == 0) // Not a GPIO_CLOCK pin
1256 // Set pin to GPIO_CLOCK mode and set the clock frequency to 100KHz
1258 *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ;
1259 delayMicroseconds (110) ;
1260 gpioClockSet (pin, 100000) ;
1265 if ((node = wiringPiFindNode (pin)) != NULL)
1266 node->pinMode (node, pin, mode) ;
1274 * Control the internal pull-up/down resistors on a GPIO pin
1275 * The Arduino only has pull-ups and these are enabled by writing 1
1276 * to a port when in input mode - this paradigm doesn't quite apply
1278 *********************************************************************************
1281 void pullUpDnControl (int pin, int pud)
1283 struct wiringPiNodeStruct *node = wiringPiNodes ;
1285 if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
1287 /**/ if (wiringPiMode == WPI_MODE_PINS)
1288 pin = pinToGpio [pin] ;
1289 else if (wiringPiMode == WPI_MODE_PHYS)
1290 pin = physToGpio [pin] ;
1291 else if (wiringPiMode != WPI_MODE_GPIO)
1294 *(gpio + GPPUD) = pud & 3 ; delayMicroseconds (5) ;
1295 *(gpio + gpioToPUDCLK [pin]) = 1 << (pin & 31) ; delayMicroseconds (5) ;
1297 *(gpio + GPPUD) = 0 ; delayMicroseconds (5) ;
1298 *(gpio + gpioToPUDCLK [pin]) = 0 ; delayMicroseconds (5) ;
1300 else // Extension module
1302 if ((node = wiringPiFindNode (pin)) != NULL)
1303 node->pullUpDnControl (node, pin, pud) ;
1311 * Read the value of a given Pin, returning HIGH or LOW
1312 *********************************************************************************
1315 int digitalRead (int pin)
1318 struct wiringPiNodeStruct *node = wiringPiNodes ;
1320 if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
1322 /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS) // Sys mode
1324 if (sysFds [pin] == -1)
1327 lseek (sysFds [pin], 0L, SEEK_SET) ;
1328 read (sysFds [pin], &c, 1) ;
1329 return (c == '0') ? LOW : HIGH ;
1331 else if (wiringPiMode == WPI_MODE_PINS)
1332 pin = pinToGpio [pin] ;
1333 else if (wiringPiMode == WPI_MODE_PHYS)
1334 pin = physToGpio [pin] ;
1335 else if (wiringPiMode != WPI_MODE_GPIO)
1338 if ((*(gpio + gpioToGPLEV [pin]) & (1 << (pin & 31))) != 0)
1345 if ((node = wiringPiFindNode (pin)) == NULL)
1347 return node->digitalRead (node, pin) ;
1355 *********************************************************************************
1358 void digitalWrite (int pin, int value)
1360 struct wiringPiNodeStruct *node = wiringPiNodes ;
1362 if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
1364 /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS) // Sys mode
1366 if (sysFds [pin] != -1)
1369 write (sysFds [pin], "0\n", 2) ;
1371 write (sysFds [pin], "1\n", 2) ;
1375 else if (wiringPiMode == WPI_MODE_PINS)
1376 pin = pinToGpio [pin] ;
1377 else if (wiringPiMode == WPI_MODE_PHYS)
1378 pin = physToGpio [pin] ;
1379 else if (wiringPiMode != WPI_MODE_GPIO)
1383 *(gpio + gpioToGPCLR [pin]) = 1 << (pin & 31) ;
1385 *(gpio + gpioToGPSET [pin]) = 1 << (pin & 31) ;
1389 if ((node = wiringPiFindNode (pin)) != NULL)
1390 node->digitalWrite (node, pin, value) ;
1397 * Set an output PWM value
1398 *********************************************************************************
1401 void pwmWrite (int pin, int value)
1403 struct wiringPiNodeStruct *node = wiringPiNodes ;
1405 if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
1407 /**/ if (wiringPiMode == WPI_MODE_PINS)
1408 pin = pinToGpio [pin] ;
1409 else if (wiringPiMode == WPI_MODE_PHYS)
1410 pin = physToGpio [pin] ;
1411 else if (wiringPiMode != WPI_MODE_GPIO)
1414 *(pwm + gpioToPwmPort [pin]) = value ;
1418 if ((node = wiringPiFindNode (pin)) != NULL)
1419 node->pwmWrite (node, pin, value) ;
1426 * Read the analog value of a given Pin.
1427 * There is no on-board Pi analog hardware,
1428 * so this needs to go to a new node.
1429 *********************************************************************************
1432 int analogRead (int pin)
1434 struct wiringPiNodeStruct *node = wiringPiNodes ;
1436 if ((node = wiringPiFindNode (pin)) == NULL)
1439 return node->analogRead (node, pin) ;
1445 * Write the analog value to the given Pin.
1446 * There is no on-board Pi analog hardware,
1447 * so this needs to go to a new node.
1448 *********************************************************************************
1451 void analogWrite (int pin, int value)
1453 struct wiringPiNodeStruct *node = wiringPiNodes ;
1455 if ((node = wiringPiFindNode (pin)) == NULL)
1458 node->analogWrite (node, pin, value) ;
1465 * Output the given frequency on the Pi's PWM pin
1466 *********************************************************************************
1469 void pwmToneWrite (int pin, int freq)
1474 pwmWrite (pin, 0) ; // Off
1477 range = 600000 / freq ;
1478 pwmSetRange (range) ;
1479 pwmWrite (pin, freq / 2) ;
1488 * Write an 8-bit byte to the first 8 GPIO pins - try to do it as
1490 * However it still needs 2 operations to set the bits, so any external
1491 * hardware must not rely on seeing a change as there will be a change
1492 * to set the outputs bits to zero, then another change to set the 1's
1493 *********************************************************************************
1496 void digitalWriteByte (int value)
1498 uint32_t pinSet = 0 ;
1499 uint32_t pinClr = 0 ;
1503 /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS)
1505 for (pin = 0 ; pin < 8 ; ++pin)
1507 digitalWrite (pin, value & mask) ;
1514 for (pin = 0 ; pin < 8 ; ++pin)
1516 if ((value & mask) == 0)
1517 pinClr |= (1 << pinToGpio [pin]) ;
1519 pinSet |= (1 << pinToGpio [pin]) ;
1524 *(gpio + gpioToGPCLR [0]) = pinClr ;
1525 *(gpio + gpioToGPSET [0]) = pinSet ;
1533 * Wait for Interrupt on a GPIO pin.
1534 * This is actually done via the /sys/class/gpio interface regardless of
1535 * the wiringPi access mode in-use. Maybe sometime it might get a better
1536 * way for a bit more efficiency.
1537 *********************************************************************************
1540 int waitForInterrupt (int pin, int mS)
1544 struct pollfd polls ;
1546 /**/ if (wiringPiMode == WPI_MODE_PINS)
1547 pin = pinToGpio [pin] ;
1548 else if (wiringPiMode == WPI_MODE_PHYS)
1549 pin = physToGpio [pin] ;
1551 if ((fd = sysFds [pin]) == -1)
1554 // Setup poll structure
1557 polls.events = POLLPRI ; // Urgent data!
1561 x = poll (&polls, 1, mS) ;
1563 // Do a dummy read to clear the interrupt
1564 // A one character read appars to be enough.
1565 // Followed by a seek to reset it.
1567 (void)read (fd, &c, 1) ;
1568 lseek (fd, 0, SEEK_SET) ;
1576 * This is a thread and gets started to wait for the interrupt we're
1577 * hoping to catch. It will call the user-function when the interrupt
1579 *********************************************************************************
1582 static void *interruptHandler (void *arg)
1586 (void)piHiPri (55) ; // Only effective if we run as root
1592 if (waitForInterrupt (myPin, -1) > 0)
1593 isrFunctions [myPin] () ;
1602 * Take the details and create an interrupt handler that will do a call-
1603 * back to the user supplied function.
1604 *********************************************************************************
1607 int wiringPiISR (int pin, int mode, void (*function)(void))
1609 pthread_t threadId ;
1618 if ((pin < 0) || (pin > 63))
1619 return wiringPiFailure (WPI_FATAL, "wiringPiISR: pin must be 0-63 (%d)\n", pin) ;
1621 /**/ if (wiringPiMode == WPI_MODE_UNINITIALISED)
1622 return wiringPiFailure (WPI_FATAL, "wiringPiISR: wiringPi has not been initialised. Unable to continue.\n") ;
1623 else if (wiringPiMode == WPI_MODE_PINS)
1624 bcmGpioPin = pinToGpio [pin] ;
1625 else if (wiringPiMode == WPI_MODE_PHYS)
1626 bcmGpioPin = physToGpio [pin] ;
1630 // Now export the pin and set the right edge
1631 // We're going to use the gpio program to do this, so it assumes
1632 // a full installation of wiringPi. It's a bit 'clunky', but it
1633 // is a way that will work when we're running in "Sys" mode, as
1634 // a non-root user. (without sudo)
1636 if (mode != INT_EDGE_SETUP)
1638 /**/ if (mode == INT_EDGE_FALLING)
1640 else if (mode == INT_EDGE_RISING)
1645 sprintf (pinS, "%d", bcmGpioPin) ;
1647 if ((pid = fork ()) < 0) // Fail
1648 return wiringPiFailure (WPI_FATAL, "wiringPiISR: fork failed: %s\n", strerror (errno)) ;
1650 if (pid == 0) // Child, exec
1652 /**/ if (access ("/usr/local/bin/gpio", X_OK) == 0)
1654 execl ("/usr/local/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ;
1655 return wiringPiFailure (WPI_FATAL, "wiringPiISR: execl failed: %s\n", strerror (errno)) ;
1657 else if (access ("/usr/bin/gpio", X_OK) == 0)
1659 execl ("/usr/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ;
1660 return wiringPiFailure (WPI_FATAL, "wiringPiISR: execl failed: %s\n", strerror (errno)) ;
1663 return wiringPiFailure (WPI_FATAL, "wiringPiISR: Can't find gpio program\n") ;
1665 else // Parent, wait
1669 // Now pre-open the /sys/class node - but it may already be open if
1670 // we are in Sys mode...
1672 if (sysFds [bcmGpioPin] == -1)
1674 sprintf (fName, "/sys/class/gpio/gpio%d/value", bcmGpioPin) ;
1675 if ((sysFds [bcmGpioPin] = open (fName, O_RDWR)) < 0)
1676 return wiringPiFailure (WPI_FATAL, "wiringPiISR: unable to open %s: %s\n", fName, strerror (errno)) ;
1679 // Clear any initial pending interrupt
1681 ioctl (sysFds [bcmGpioPin], FIONREAD, &count) ;
1682 for (i = 0 ; i < count ; ++i)
1683 read (sysFds [bcmGpioPin], &c, 1) ;
1685 isrFunctions [pin] = function ;
1687 pthread_mutex_lock (&pinMutex) ;
1689 pthread_create (&threadId, NULL, interruptHandler, NULL) ;
1690 while (pinPass != -1)
1692 pthread_mutex_unlock (&pinMutex) ;
1700 * Initialise our start-of-time variable to be the current unix
1701 * time in milliseconds and microseconds.
1702 *********************************************************************************
1705 static void initialiseEpoch (void)
1709 gettimeofday (&tv, NULL) ;
1710 epochMilli = (uint64_t)tv.tv_sec * (uint64_t)1000 + (uint64_t)(tv.tv_usec / 1000) ;
1711 epochMicro = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)(tv.tv_usec) ;
1717 * Wait for some number of milliseconds
1718 *********************************************************************************
1721 void delay (unsigned int howLong)
1723 struct timespec sleeper, dummy ;
1725 sleeper.tv_sec = (time_t)(howLong / 1000) ;
1726 sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ;
1728 nanosleep (&sleeper, &dummy) ;
1733 * delayMicroseconds:
1734 * This is somewhat intersting. It seems that on the Pi, a single call
1735 * to nanosleep takes some 80 to 130 microseconds anyway, so while
1736 * obeying the standards (may take longer), it's not always what we
1739 * So what I'll do now is if the delay is less than 100uS we'll do it
1740 * in a hard loop, watching a built-in counter on the ARM chip. This is
1741 * somewhat sub-optimal in that it uses 100% CPU, something not an issue
1742 * in a microcontroller, but under a multi-tasking, multi-user OS, it's
1743 * wastefull, however we've no real choice )-:
1745 * Plan B: It seems all might not be well with that plan, so changing it
1746 * to use gettimeofday () and poll on that instead...
1747 *********************************************************************************
1750 void delayMicrosecondsHard (unsigned int howLong)
1752 struct timeval tNow, tLong, tEnd ;
1754 gettimeofday (&tNow, NULL) ;
1755 tLong.tv_sec = howLong / 1000000 ;
1756 tLong.tv_usec = howLong % 1000000 ;
1757 timeradd (&tNow, &tLong, &tEnd) ;
1759 while (timercmp (&tNow, &tEnd, <))
1760 gettimeofday (&tNow, NULL) ;
1763 void delayMicroseconds (unsigned int howLong)
1765 struct timespec sleeper ;
1766 unsigned int uSecs = howLong % 1000000 ;
1767 unsigned int wSecs = howLong / 1000000 ;
1769 /**/ if (howLong == 0)
1771 else if (howLong < 100)
1772 delayMicrosecondsHard (howLong) ;
1775 sleeper.tv_sec = wSecs ;
1776 sleeper.tv_nsec = (long)(uSecs * 1000L) ;
1777 nanosleep (&sleeper, NULL) ;
1784 * Return a number of milliseconds as an unsigned int.
1785 *********************************************************************************
1788 unsigned int millis (void)
1793 gettimeofday (&tv, NULL) ;
1794 now = (uint64_t)tv.tv_sec * (uint64_t)1000 + (uint64_t)(tv.tv_usec / 1000) ;
1796 return (uint32_t)(now - epochMilli) ;
1802 * Return a number of microseconds as an unsigned int.
1803 *********************************************************************************
1806 unsigned int micros (void)
1811 gettimeofday (&tv, NULL) ;
1812 now = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)tv.tv_usec ;
1814 return (uint32_t)(now - epochMicro) ;
1820 * Must be called once at the start of your program execution.
1822 * Default setup: Initialises the system into wiringPi Pin mode and uses the
1823 * memory mapped hardware directly.
1825 * Changed now to revert to "gpio" mode if we're running on a Compute Module.
1826 *********************************************************************************
1829 int wiringPiSetup (void)
1833 int model, rev, mem, maker, overVolted ;
1835 if (getenv (ENV_DEBUG) != NULL)
1836 wiringPiDebug = TRUE ;
1838 if (getenv (ENV_CODES) != NULL)
1839 wiringPiReturnCodes = TRUE ;
1842 printf ("wiringPi: wiringPiSetup called\n") ;
1844 boardRev = piBoardRev () ;
1846 /**/ if (boardRev == 1) // A, B, Rev 1, 1.1
1848 pinToGpio = pinToGpioR1 ;
1849 physToGpio = physToGpioR1 ;
1851 else // A, B, Rev 2, B+, CM, Pi2
1853 pinToGpio = pinToGpioR2 ;
1854 physToGpio = physToGpioR2 ;
1858 RASPBERRY_PI_PERI_BASE = 0x3F000000 ;
1860 RASPBERRY_PI_PERI_BASE = 0x20000000 ;
1862 // Open the master /dev/ memory control device
1864 // See if /dev/gpiomem exists and we can open it...
1866 if ((fd = open ("/dev/gpiomem", O_RDWR | O_SYNC | O_CLOEXEC) ) >= 0)
1867 RASPBERRY_PI_PERI_BASE = 0 ;
1869 // ... otherwise fall back to the original /dev/mem which requires root level access
1874 // This check is here because people are too stupid to check for themselves or read
1877 if (geteuid () != 0)
1878 (void)wiringPiFailure (WPI_FATAL, "wiringPiSetup: Must be root. (Did you forget sudo?)\n") ;
1880 if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0)
1881 return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: Unable to open /dev/mem: %s\n", strerror (errno)) ;
1884 // Set the offsets into the memory interface.
1886 GPIO_PADS = RASPBERRY_PI_PERI_BASE + 0x00100000 ;
1887 GPIO_CLOCK_BASE = RASPBERRY_PI_PERI_BASE + 0x00101000 ;
1888 GPIO_BASE = RASPBERRY_PI_PERI_BASE + 0x00200000 ;
1889 GPIO_TIMER = RASPBERRY_PI_PERI_BASE + 0x0000B000 ;
1890 GPIO_PWM = RASPBERRY_PI_PERI_BASE + 0x0020C000 ;
1892 // Map the individual hardware components
1896 gpio = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_BASE) ;
1897 if ((int32_t)gpio == -1)
1898 return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (GPIO) failed: %s\n", strerror (errno)) ;
1902 pwm = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PWM) ;
1903 if ((int32_t)pwm == -1)
1904 return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PWM) failed: %s\n", strerror (errno)) ;
1906 // Clock control (needed for PWM)
1908 clk = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_CLOCK_BASE) ;
1909 if ((int32_t)clk == -1)
1910 return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (CLOCK) failed: %s\n", strerror (errno)) ;
1914 pads = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PADS) ;
1915 if ((int32_t)pads == -1)
1916 return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PADS) failed: %s\n", strerror (errno)) ;
1921 timer = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_TIMER) ;
1922 if ((int32_t)timer == -1)
1923 return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (TIMER) failed: %s\n", strerror (errno)) ;
1925 // Set the timer to free-running, 1MHz.
1926 // 0xF9 is 249, the timer divide is base clock / (divide+1)
1927 // so base clock is 250MHz / 250 = 1MHz.
1929 *(timer + TIMER_CONTROL) = 0x0000280 ;
1930 *(timer + TIMER_PRE_DIV) = 0x00000F9 ;
1931 timerIrqRaw = timer + TIMER_IRQ_RAW ;
1934 initialiseEpoch () ;
1936 // If we're running on a compute module, then wiringPi pin numbers don't really many anything...
1938 piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
1939 if (model == PI_MODEL_CM)
1940 wiringPiMode = WPI_MODE_GPIO ;
1942 wiringPiMode = WPI_MODE_PINS ;
1949 * wiringPiSetupGpio:
1950 * Must be called once at the start of your program execution.
1952 * GPIO setup: Initialises the system into GPIO Pin mode and uses the
1953 * memory mapped hardware directly.
1954 *********************************************************************************
1957 int wiringPiSetupGpio (void)
1959 (void)wiringPiSetup () ;
1962 printf ("wiringPi: wiringPiSetupGpio called\n") ;
1964 wiringPiMode = WPI_MODE_GPIO ;
1971 * wiringPiSetupPhys:
1972 * Must be called once at the start of your program execution.
1974 * Phys setup: Initialises the system into Physical Pin mode and uses the
1975 * memory mapped hardware directly.
1976 *********************************************************************************
1979 int wiringPiSetupPhys (void)
1981 (void)wiringPiSetup () ;
1984 printf ("wiringPi: wiringPiSetupPhys called\n") ;
1986 wiringPiMode = WPI_MODE_PHYS ;
1994 * Must be called once at the start of your program execution.
1996 * Initialisation (again), however this time we are using the /sys/class/gpio
1997 * interface to the GPIO systems - slightly slower, but always usable as
1998 * a non-root user, assuming the devices are already exported and setup correctly.
2001 int wiringPiSetupSys (void)
2007 if (getenv (ENV_DEBUG) != NULL)
2008 wiringPiDebug = TRUE ;
2010 if (getenv (ENV_CODES) != NULL)
2011 wiringPiReturnCodes = TRUE ;
2014 printf ("wiringPi: wiringPiSetupSys called\n") ;
2016 boardRev = piBoardRev () ;
2020 pinToGpio = pinToGpioR1 ;
2021 physToGpio = physToGpioR1 ;
2025 pinToGpio = pinToGpioR2 ;
2026 physToGpio = physToGpioR2 ;
2029 // Open and scan the directory, looking for exported GPIOs, and pre-open
2030 // the 'value' interface to speed things up for later
2032 for (pin = 0 ; pin < 64 ; ++pin)
2034 sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
2035 sysFds [pin] = open (fName, O_RDWR) ;
2038 initialiseEpoch () ;
2040 wiringPiMode = WPI_MODE_GPIO_SYS ;