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"
87 #define ENV_GPIOMEM "WIRINGPI_GPIOMEM"
90 // Mask for the bottom 64 pins which belong to the Raspberry Pi
91 // The others are available for the other devices
93 #define PI_GPIO_MASK (0xFFFFFFC0)
95 struct wiringPiNodeStruct *wiringPiNodes = NULL ;
99 #define BCM_PASSWORD 0x5A000000
102 // The BCM2835 has 54 GPIO pins.
103 // BCM2835 data sheet, Page 90 onwards.
104 // There are 6 control registers, each control the functions of a block
106 // Each control register has 10 sets of 3 bits per GPIO pin - the ALT values
108 // 000 = GPIO Pin X is an input
109 // 001 = GPIO Pin X is an output
110 // 100 = GPIO Pin X takes alternate function 0
111 // 101 = GPIO Pin X takes alternate function 1
112 // 110 = GPIO Pin X takes alternate function 2
113 // 111 = GPIO Pin X takes alternate function 3
114 // 011 = GPIO Pin X takes alternate function 4
115 // 010 = GPIO Pin X takes alternate function 5
117 // So the 3 bits for port X are:
118 // X / 10 + ((X % 10) * 3)
120 // Port function select bits
122 #define FSEL_INPT 0b000
123 #define FSEL_OUTP 0b001
124 #define FSEL_ALT0 0b100
125 #define FSEL_ALT1 0b101
126 #define FSEL_ALT2 0b110
127 #define FSEL_ALT3 0b111
128 #define FSEL_ALT4 0b011
129 #define FSEL_ALT5 0b010
131 // Access from ARM Running Linux
132 // Taken from Gert/Doms code. Some of this is not in the manual
133 // that I can find )-:
135 // Updates in September 2015 - all now static variables (and apologies for the caps)
136 // due to the Pi v2 and the new /dev/gpiomem interface
138 static volatile unsigned int RASPBERRY_PI_PERI_BASE ;
139 static volatile unsigned int GPIO_PADS ;
140 static volatile unsigned int GPIO_CLOCK_BASE ;
141 static volatile unsigned int GPIO_BASE ;
142 static volatile unsigned int GPIO_TIMER ;
143 static volatile unsigned int GPIO_PWM ;
145 #define PAGE_SIZE (4*1024)
146 #define BLOCK_SIZE (4*1024)
149 // Word offsets into the PWM control region
151 #define PWM_CONTROL 0
158 // Clock regsiter offsets
160 #define PWMCLK_CNTL 40
161 #define PWMCLK_DIV 41
163 #define PWM0_MS_MODE 0x0080 // Run in MS mode
164 #define PWM0_USEFIFO 0x0020 // Data from FIFO
165 #define PWM0_REVPOLAR 0x0010 // Reverse polarity
166 #define PWM0_OFFSTATE 0x0008 // Ouput Off state
167 #define PWM0_REPEATFF 0x0004 // Repeat last value if FIFO empty
168 #define PWM0_SERIAL 0x0002 // Run in serial mode
169 #define PWM0_ENABLE 0x0001 // Channel Enable
171 #define PWM1_MS_MODE 0x8000 // Run in MS mode
172 #define PWM1_USEFIFO 0x2000 // Data from FIFO
173 #define PWM1_REVPOLAR 0x1000 // Reverse polarity
174 #define PWM1_OFFSTATE 0x0800 // Ouput Off state
175 #define PWM1_REPEATFF 0x0400 // Repeat last value if FIFO empty
176 #define PWM1_SERIAL 0x0200 // Run in serial mode
177 #define PWM1_ENABLE 0x0100 // Channel Enable
182 #define TIMER_LOAD (0x400 >> 2)
183 #define TIMER_VALUE (0x404 >> 2)
184 #define TIMER_CONTROL (0x408 >> 2)
185 #define TIMER_IRQ_CLR (0x40C >> 2)
186 #define TIMER_IRQ_RAW (0x410 >> 2)
187 #define TIMER_IRQ_MASK (0x414 >> 2)
188 #define TIMER_RELOAD (0x418 >> 2)
189 #define TIMER_PRE_DIV (0x41C >> 2)
190 #define TIMER_COUNTER (0x420 >> 2)
192 // Locals to hold pointers to the hardware
194 static volatile uint32_t *gpio ;
195 static volatile uint32_t *pwm ;
196 static volatile uint32_t *clk ;
197 static volatile uint32_t *pads ;
200 static volatile uint32_t *timer ;
201 static volatile uint32_t *timerIrqRaw ;
205 // Data for use with the boardId functions.
206 // The order of entries here to correspond with the PI_MODEL_X
207 // and PI_VERSION_X defines in wiringPi.h
208 // Only intended for the gpio command - use at your own risk!
210 static int piModel2 = FALSE ;
212 const char *piModelNames [7] =
220 "Model 2", // Quad Core
223 const char *piRevisionNames [5] =
232 const char *piMakerNames [5] =
242 // Time for easy calculations
244 static uint64_t epochMilli, epochMicro ;
248 static int wiringPiMode = WPI_MODE_UNINITIALISED ;
249 static volatile int pinPass = -1 ;
250 static pthread_mutex_t pinMutex ;
252 // Debugging & Return codes
254 int wiringPiDebug = FALSE ;
255 int wiringPiReturnCodes = FALSE ;
257 // Use /dev/gpiomem ?
259 int wiringPiTryGpioMem = FALSE ;
262 // Map a file descriptor from the /sys/class/gpio/gpioX/value
264 static int sysFds [64] =
266 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
267 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
268 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
269 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
274 static void (*isrFunctions [64])(void) ;
277 // Doing it the Arduino way with lookup tables...
278 // Yes, it's probably more innefficient than all the bit-twidling, but it
279 // does tend to make it all a bit clearer. At least to me!
282 // Take a Wiring pin (0 through X) and re-map it to the BCM_GPIO pin
283 // Cope for 3 different board revisions here.
285 static int *pinToGpio ;
289 static int pinToGpioR1 [64] =
291 17, 18, 21, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7: wpi 0 - 7
292 0, 1, // I2C - SDA1, SCL1 wpi 8 - 9
293 8, 7, // SPI - CE1, CE0 wpi 10 - 11
294 10, 9, 11, // SPI - MOSI, MISO, SCLK wpi 12 - 14
295 14, 15, // UART - Tx, Rx wpi 15 - 16
299 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 31
300 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
301 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
306 static int pinToGpioR2 [64] =
308 17, 18, 27, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7: wpi 0 - 7
309 2, 3, // I2C - SDA0, SCL0 wpi 8 - 9
310 8, 7, // SPI - CE1, CE0 wpi 10 - 11
311 10, 9, 11, // SPI - MOSI, MISO, SCLK wpi 12 - 14
312 14, 15, // UART - Tx, Rx wpi 15 - 16
313 28, 29, 30, 31, // Rev 2: New GPIOs 8 though 11 wpi 17 - 20
314 5, 6, 13, 19, 26, // B+ wpi 21, 22, 23, 24, 25
315 12, 16, 20, 21, // B+ wpi 26, 27, 28, 29
316 0, 1, // B+ wpi 30, 31
320 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
321 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
326 // Take a physical pin (1 through 26) and re-map it to the BCM_GPIO pin
327 // Cope for 2 different board revisions here.
328 // Also add in the P5 connector, so the P5 pins are 3,4,5,6, so 53,54,55,56
330 static int *physToGpio ;
332 static int physToGpioR1 [64] =
349 -1, -1, -1, -1, -1, // ... 31
350 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
351 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
354 static int physToGpioR2 [64] =
381 // the P5 connector on the Rev 2 boards:
397 // Map a BCM_GPIO pin to it's Function Selection
398 // control port. (GPFSEL 0-5)
399 // Groups of 10 - 3 bits per Function - 30 bits per port
401 static uint8_t gpioToGPFSEL [] =
403 0,0,0,0,0,0,0,0,0,0,
404 1,1,1,1,1,1,1,1,1,1,
405 2,2,2,2,2,2,2,2,2,2,
406 3,3,3,3,3,3,3,3,3,3,
407 4,4,4,4,4,4,4,4,4,4,
408 5,5,5,5,5,5,5,5,5,5,
413 // Define the shift up for the 3 bits per pin in each GPFSEL port
415 static uint8_t gpioToShift [] =
417 0,3,6,9,12,15,18,21,24,27,
418 0,3,6,9,12,15,18,21,24,27,
419 0,3,6,9,12,15,18,21,24,27,
420 0,3,6,9,12,15,18,21,24,27,
421 0,3,6,9,12,15,18,21,24,27,
426 // (Word) offset to the GPIO Set registers for each GPIO pin
428 static uint8_t gpioToGPSET [] =
430 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,
431 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,
435 // (Word) offset to the GPIO Clear registers for each GPIO pin
437 static uint8_t gpioToGPCLR [] =
439 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,
440 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,
445 // (Word) offset to the GPIO Input level registers for each GPIO pin
447 static uint8_t gpioToGPLEV [] =
449 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,
450 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,
456 // (Word) offset to the Event Detect Status
458 static uint8_t gpioToEDS [] =
460 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,
461 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,
465 // (Word) offset to the Rising edge ENable register
467 static uint8_t gpioToREN [] =
469 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,
470 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,
474 // (Word) offset to the Falling edgde ENable register
476 static uint8_t gpioToFEN [] =
478 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,
479 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,
485 // GPIO Pin pull up/down register
490 // (Word) offset to the Pull Up Down Clock regsiter
492 static uint8_t gpioToPUDCLK [] =
494 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,
495 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,
500 // the ALT value to put a GPIO pin into PWM mode
502 static uint8_t gpioToPwmALT [] =
504 0, 0, 0, 0, 0, 0, 0, 0, // 0 -> 7
505 0, 0, 0, 0, FSEL_ALT0, FSEL_ALT0, 0, 0, // 8 -> 15
506 0, 0, FSEL_ALT5, FSEL_ALT5, 0, 0, 0, 0, // 16 -> 23
507 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
508 0, 0, 0, 0, 0, 0, 0, 0, // 32 -> 39
509 FSEL_ALT0, FSEL_ALT0, 0, 0, 0, FSEL_ALT0, 0, 0, // 40 -> 47
510 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
511 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
516 // The port value to put a GPIO pin into PWM mode
518 static uint8_t gpioToPwmPort [] =
520 0, 0, 0, 0, 0, 0, 0, 0, // 0 -> 7
521 0, 0, 0, 0, PWM0_DATA, PWM1_DATA, 0, 0, // 8 -> 15
522 0, 0, PWM0_DATA, PWM1_DATA, 0, 0, 0, 0, // 16 -> 23
523 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
524 0, 0, 0, 0, 0, 0, 0, 0, // 32 -> 39
525 PWM0_DATA, PWM1_DATA, 0, 0, 0, PWM1_DATA, 0, 0, // 40 -> 47
526 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
527 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
532 // ALT value to put a GPIO pin into GP Clock mode.
533 // On the Pi we can really only use BCM_GPIO_4 and BCM_GPIO_21
534 // for clocks 0 and 1 respectively, however I'll include the full
535 // list for completeness - maybe one day...
537 #define GPIO_CLOCK_SOURCE 1
541 static uint8_t gpioToGpClkALT0 [] =
543 0, 0, 0, 0, FSEL_ALT0, FSEL_ALT0, FSEL_ALT0, 0, // 0 -> 7
544 0, 0, 0, 0, 0, 0, 0, 0, // 8 -> 15
545 0, 0, 0, 0, FSEL_ALT5, FSEL_ALT5, 0, 0, // 16 -> 23
546 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
547 FSEL_ALT0, 0, FSEL_ALT0, 0, 0, 0, 0, 0, // 32 -> 39
548 0, 0, FSEL_ALT0, FSEL_ALT0, FSEL_ALT0, 0, 0, 0, // 40 -> 47
549 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
550 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
554 // (word) Offsets to the clock Control and Divisor register
556 static uint8_t gpioToClkCon [] =
558 -1, -1, -1, -1, 28, 30, 32, -1, // 0 -> 7
559 -1, -1, -1, -1, -1, -1, -1, -1, // 8 -> 15
560 -1, -1, -1, -1, 28, 30, -1, -1, // 16 -> 23
561 -1, -1, -1, -1, -1, -1, -1, -1, // 24 -> 31
562 28, -1, 28, -1, -1, -1, -1, -1, // 32 -> 39
563 -1, -1, 28, 30, 28, -1, -1, -1, // 40 -> 47
564 -1, -1, -1, -1, -1, -1, -1, -1, // 48 -> 55
565 -1, -1, -1, -1, -1, -1, -1, -1, // 56 -> 63
568 static uint8_t gpioToClkDiv [] =
570 -1, -1, -1, -1, 29, 31, 33, -1, // 0 -> 7
571 -1, -1, -1, -1, -1, -1, -1, -1, // 8 -> 15
572 -1, -1, -1, -1, 29, 31, -1, -1, // 16 -> 23
573 -1, -1, -1, -1, -1, -1, -1, -1, // 24 -> 31
574 29, -1, 29, -1, -1, -1, -1, -1, // 32 -> 39
575 -1, -1, 29, 31, 29, -1, -1, -1, // 40 -> 47
576 -1, -1, -1, -1, -1, -1, -1, -1, // 48 -> 55
577 -1, -1, -1, -1, -1, -1, -1, -1, // 56 -> 63
583 *********************************************************************************
590 *********************************************************************************
593 int wiringPiFailure (int fatal, const char *message, ...)
598 if (!fatal && wiringPiReturnCodes)
601 va_start (argp, message) ;
602 vsnprintf (buffer, 1023, message, argp) ;
605 fprintf (stderr, "%s", buffer) ;
606 exit (EXIT_FAILURE) ;
614 * Return a number representing the hardware revision of the board.
616 * Revision 1 really means the early Model B's.
617 * Revision 2 is everything else - it covers the B, B+ and CM.
618 * ... and the Pi 2 - which is a B+ ++ ...
620 * Seems there are some boards with 0000 in them (mistake in manufacture)
621 * So the distinction between boards that I can see is:
624 * 0002 - Model B, Rev 1, 256MB, Egoman
625 * 0003 - Model B, Rev 1.1, 256MB, Egoman, Fuses/D14 removed.
626 * 0004 - Model B, Rev 2, 256MB, Sony
627 * 0005 - Model B, Rev 2, 256MB, Qisda
628 * 0006 - Model B, Rev 2, 256MB, Egoman
629 * 0007 - Model A, Rev 2, 256MB, Egoman
630 * 0008 - Model A, Rev 2, 256MB, Sony
631 * 0009 - Model A, Rev 2, 256MB, Qisda
632 * 000d - Model B, Rev 2, 512MB, Egoman
633 * 000e - Model B, Rev 2, 512MB, Sony
634 * 000f - Model B, Rev 2, 512MB, Qisda
635 * 0010 - Model B+, Rev 1.2, 512MB, Sony
636 * 0011 - Pi CM, Rev 1.2, 512MB, Sony
637 * 0012 - Model A+ Rev 1.2, 256MB, Sony
638 * 0014 - Pi CM, Rev 1.1, 512MB, Sony (Actual Revision might be different)
639 * 0015 - Model A+ Rev 1.1, 256MB, Sony
642 * 0010 - Model 2, Rev 1.1, Quad Core, 1GB, Sony
644 * A small thorn is the olde style overvolting - that will add in
647 * The Pi compute module has an revision of 0011 - since we only check the
648 * last digit, then it's 1, therefore it'll default to not 2 or 3 for a
649 * Rev 1, so will appear as a Rev 2. This is fine for the most part, but
650 * we'll properly detect the Compute Module later and adjust accordingly.
651 * And the next rev of the CN is 0014 ...
653 *********************************************************************************
656 static void piBoardRevOops (const char *why)
658 fprintf (stderr, "piBoardRev: Unable to determine board revision from /proc/cpuinfo\n") ;
659 fprintf (stderr, " -> %s\n", why) ;
660 fprintf (stderr, " -> You may want to check:\n") ;
661 fprintf (stderr, " -> http://www.raspberrypi.org/phpBB3/viewtopic.php?p=184410#p184410\n") ;
662 exit (EXIT_FAILURE) ;
665 int piBoardRev (void)
670 static int boardRev = -1 ;
672 if (boardRev != -1) // No point checking twice
675 if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL)
676 piBoardRevOops ("Unable to open /proc/cpuinfo") ;
678 // Start by looking for the Architecture, then we can look for a B2 revision....
680 while (fgets (line, 120, cpuFd) != NULL)
681 if (strncmp (line, "Hardware", 8) == 0)
684 if (strncmp (line, "Hardware", 8) != 0)
685 piBoardRevOops ("No \"Hardware\" line") ;
688 printf ("piboardRev: Hardware: %s\n", line) ;
690 // See if it's BCM2708 or BCM2709
692 if (strstr (line, "BCM2709") != NULL)
694 else if (strstr (line, "BCM2708") == NULL)
696 fprintf (stderr, "Unable to determine hardware version. I see: %s,\n", line) ;
697 fprintf (stderr, " - expecting BCM2708 or BCM2709.\n") ;
698 fprintf (stderr, "If this is a genuine Raspberry Pi then please report this\n") ;
699 fprintf (stderr, "to projects@drogon.net. If this is not a Raspberry Pi then you\n") ;
700 fprintf (stderr, "are on your own as wiringPi is designed to support the\n") ;
701 fprintf (stderr, "Raspberry Pi ONLY.\n") ;
702 exit (EXIT_FAILURE) ;
705 // Now do the rest of it as before
709 while (fgets (line, 120, cpuFd) != NULL)
710 if (strncmp (line, "Revision", 8) == 0)
715 if (strncmp (line, "Revision", 8) != 0)
716 piBoardRevOops ("No \"Revision\" line") ;
718 // Chomp trailing CR/NL
720 for (c = &line [strlen (line) - 1] ; (*c == '\n') || (*c == '\r') ; --c)
724 printf ("piboardRev: Revision string: %s\n", line) ;
726 // Scan to first digit
728 for (c = line ; *c ; ++c)
733 piBoardRevOops ("No numeric revision string") ;
735 // Make sure its long enough
738 piBoardRevOops ("Bogus \"Revision\" line (too small)") ;
740 // If you have overvolted the Pi, then it appears that the revision
741 // has 100000 added to it!
742 // The actual condition for it being set is:
743 // (force_turbo || current_limit_override || temp_limit>85) && over_voltage>0
747 printf ("piboardRev: This Pi has/is (force_turbo || current_limit_override || temp_limit>85) && over_voltage>0\n") ;
749 // Isolate last 4 characters:
751 c = c + strlen (c) - 4 ;
754 printf ("piboardRev: last4Chars are: \"%s\"\n", c) ;
756 if ( (strcmp (c, "0002") == 0) || (strcmp (c, "0003") == 0))
759 boardRev = 2 ; // Covers everything else from the B revision 2 to the B+, the Pi v2 and CM's.
762 printf ("piBoardRev: Returning revision: %d\n", boardRev) ;
770 * Do more digging into the board revision string as above, but return
771 * as much details as we can.
772 * This is undocumented and really only intended for the GPIO command.
773 * Use at your own risk!
776 * [USER:8] [NEW:1] [MEMSIZE:3] [MANUFACTURER:4] [PROCESSOR:4] [TYPE:8] [REV:4]
777 * NEW 23: will be 1 for the new scheme, 0 for the old scheme
778 * MEMSIZE 20: 0=256M 1=512M 2=1G
779 * MANUFACTURER 16: 0=SONY 1=EGOMAN 2=EMBEST
780 * PROCESSOR 12: 0=2835 1=2836
781 * TYPE 04: 0=MODELA 1=MODELB 2=MODELA+ 3=MODELB+ 4=Pi2 MODEL B 5=ALPHA 6=CM
782 * REV 00: 0=REV0 1=REV1 2=REV2
783 *********************************************************************************
786 void piBoardId (int *model, int *rev, int *mem, int *maker, int *overVolted)
792 // Will deal with the properly later on - for now, lets just get it going...
793 // unsigned int modelNum ;
795 (void)piBoardRev () ; // Call this first to make sure all's OK. Don't care about the result.
797 if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL)
798 piBoardRevOops ("Unable to open /proc/cpuinfo") ;
800 while (fgets (line, 120, cpuFd) != NULL)
801 if (strncmp (line, "Revision", 8) == 0)
806 if (strncmp (line, "Revision", 8) != 0)
807 piBoardRevOops ("No \"Revision\" line") ;
809 // Chomp trailing CR/NL
811 for (c = &line [strlen (line) - 1] ; (*c == '\n') || (*c == '\r') ; --c)
815 printf ("piboardId: Revision string: %s\n", line) ;
822 for (c = line ; *c ; ++c)
827 piBoardRevOops ("Bogus \"Revision\" line (no colon)") ;
829 // modelNum = (unsigned int)strtol (++c, NULL, 16) ; // Hex number with no leading 0x
831 *model = PI_MODEL_2 ;
832 *rev = PI_VERSION_1_1 ;
834 *maker = PI_MAKER_SONY ;
839 // Scan to first digit
841 for (c = line ; *c ; ++c)
845 // Make sure its long enough
848 piBoardRevOops ("Bogus \"Revision\" line (not long enough)") ;
850 // If longer than 4, we'll assume it's been overvolted
852 *overVolted = strlen (c) > 4 ;
854 // Extract last 4 characters:
856 c = c + strlen (c) - 4 ;
858 // Fill out the replys as appropriate
860 /**/ if (strcmp (c, "0002") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1 ; *mem = 256 ; *maker = PI_MAKER_EGOMAN ; }
861 else if (strcmp (c, "0003") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_1 ; *mem = 256 ; *maker = PI_MAKER_EGOMAN ; }
862 else if (strcmp (c, "0004") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_SONY ; }
863 else if (strcmp (c, "0005") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_QISDA ; }
864 else if (strcmp (c, "0006") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_EGOMAN ; }
865 else if (strcmp (c, "0007") == 0) { *model = PI_MODEL_A ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_EGOMAN ; }
866 else if (strcmp (c, "0008") == 0) { *model = PI_MODEL_A ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_SONY ; ; }
867 else if (strcmp (c, "0009") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_QISDA ; }
868 else if (strcmp (c, "000d") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 512 ; *maker = PI_MAKER_EGOMAN ; }
869 else if (strcmp (c, "000e") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 512 ; *maker = PI_MAKER_SONY ; }
870 else if (strcmp (c, "000f") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 512 ; *maker = PI_MAKER_EGOMAN ; }
871 else if (strcmp (c, "0010") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 512 ; *maker = PI_MAKER_SONY ; }
872 else if (strcmp (c, "0011") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_2 ; *mem = 512 ; *maker = PI_MAKER_SONY ; }
873 else if (strcmp (c, "0012") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_2 ; *mem = 256 ; *maker = PI_MAKER_SONY ; }
874 else if (strcmp (c, "0013") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 512 ; *maker = PI_MAKER_MBEST ; }
875 else if (strcmp (c, "0014") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_2 ; *mem = 512 ; *maker = PI_MAKER_SONY ; }
876 else if (strcmp (c, "0015") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_1 ; *mem = 256 ; *maker = PI_MAKER_SONY ; }
877 else { *model = 0 ; *rev = 0 ; *mem = 0 ; *maker = 0 ; }
885 * Translate a wiringPi Pin number to native GPIO pin number.
886 * Provided for external support.
887 *********************************************************************************
890 int wpiPinToGpio (int wpiPin)
892 return pinToGpio [wpiPin & 63] ;
898 * Translate a physical Pin number to native GPIO pin number.
899 * Provided for external support.
900 *********************************************************************************
903 int physPinToGpio (int physPin)
905 return physToGpio [physPin & 63] ;
911 * Set the PAD driver value
912 *********************************************************************************
915 void setPadDrive (int group, int value)
919 if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
921 if (RASPBERRY_PI_PERI_BASE == 0) // Ignore for now
924 if ((group < 0) || (group > 2))
927 wrVal = BCM_PASSWORD | 0x18 | (value & 7) ;
928 *(pads + group + 11) = wrVal ;
932 printf ("setPadDrive: Group: %d, value: %d (%08X)\n", group, value, wrVal) ;
933 printf ("Read : %08X\n", *(pads + group + 11)) ;
941 * Returns the ALT bits for a given port. Only really of-use
942 * for the gpio readall command (I think)
943 *********************************************************************************
948 int fSel, shift, alt ;
952 /**/ if (wiringPiMode == WPI_MODE_PINS)
953 pin = pinToGpio [pin] ;
954 else if (wiringPiMode == WPI_MODE_PHYS)
955 pin = physToGpio [pin] ;
956 else if (wiringPiMode != WPI_MODE_GPIO)
959 fSel = gpioToGPFSEL [pin] ;
960 shift = gpioToShift [pin] ;
962 alt = (*(gpio + fSel) >> shift) & 7 ;
970 * Select the native "balanced" mode, or standard mark:space mode
971 *********************************************************************************
974 void pwmSetMode (int mode)
976 if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
978 if (mode == PWM_MODE_MS)
979 *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE | PWM0_MS_MODE | PWM1_MS_MODE ;
981 *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ;
988 * Set the PWM range register. We set both range registers to the same
989 * value. If you want different in your own code, then write your own.
990 *********************************************************************************
993 void pwmSetRange (unsigned int range)
995 if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
997 if (RASPBERRY_PI_PERI_BASE == 0) // Ignore for now
1000 *(pwm + PWM0_RANGE) = range ; delayMicroseconds (10) ;
1001 *(pwm + PWM1_RANGE) = range ; delayMicroseconds (10) ;
1008 * Set/Change the PWM clock. Originally my code, but changed
1009 * (for the better!) by Chris Hall, <chris@kchall.plus.com>
1010 * after further study of the manual and testing with a 'scope
1011 *********************************************************************************
1014 void pwmSetClock (int divisor)
1016 uint32_t pwm_control ;
1019 if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
1021 if (RASPBERRY_PI_PERI_BASE == 0) // Ignore for now
1025 printf ("Setting to: %d. Current: 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;
1027 pwm_control = *(pwm + PWM_CONTROL) ; // preserve PWM_CONTROL
1029 // We need to stop PWM prior to stopping PWM clock in MS mode otherwise BUSY
1032 *(pwm + PWM_CONTROL) = 0 ; // Stop PWM
1034 // Stop PWM clock before changing divisor. The delay after this does need to
1035 // this big (95uS occasionally fails, 100uS OK), it's almost as though the BUSY
1036 // flag is not working properly in balanced mode. Without the delay when DIV is
1037 // adjusted the clock sometimes switches to very slow, once slow further DIV
1038 // adjustments do nothing and it's difficult to get out of this mode.
1040 *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x01 ; // Stop PWM Clock
1041 delayMicroseconds (110) ; // prevents clock going sloooow
1043 while ((*(clk + PWMCLK_CNTL) & 0x80) != 0) // Wait for clock to be !BUSY
1044 delayMicroseconds (1) ;
1046 *(clk + PWMCLK_DIV) = BCM_PASSWORD | (divisor << 12) ;
1048 *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x11 ; // Start PWM clock
1049 *(pwm + PWM_CONTROL) = pwm_control ; // restore PWM_CONTROL
1052 printf ("Set to: %d. Now : 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;
1059 * Set the freuency on a GPIO clock pin
1060 *********************************************************************************
1063 void gpioClockSet (int pin, int freq)
1065 int divi, divr, divf ;
1069 /**/ if (wiringPiMode == WPI_MODE_PINS)
1070 pin = pinToGpio [pin] ;
1071 else if (wiringPiMode == WPI_MODE_PHYS)
1072 pin = physToGpio [pin] ;
1073 else if (wiringPiMode != WPI_MODE_GPIO)
1076 if (RASPBERRY_PI_PERI_BASE == 0) // Ignore for now
1079 divi = 19200000 / freq ;
1080 divr = 19200000 % freq ;
1081 divf = (int)((double)divr * 4096.0 / 19200000.0) ;
1086 *(clk + gpioToClkCon [pin]) = BCM_PASSWORD | GPIO_CLOCK_SOURCE ; // Stop GPIO Clock
1087 while ((*(clk + gpioToClkCon [pin]) & 0x80) != 0) // ... and wait
1090 *(clk + gpioToClkDiv [pin]) = BCM_PASSWORD | (divi << 12) | divf ; // Set dividers
1091 *(clk + gpioToClkCon [pin]) = BCM_PASSWORD | 0x10 | GPIO_CLOCK_SOURCE ; // Start Clock
1097 * Locate our device node
1098 *********************************************************************************
1101 struct wiringPiNodeStruct *wiringPiFindNode (int pin)
1103 struct wiringPiNodeStruct *node = wiringPiNodes ;
1105 while (node != NULL)
1106 if ((pin >= node->pinBase) && (pin <= node->pinMax))
1117 * Create a new GPIO node into the wiringPi handling system
1118 *********************************************************************************
1121 static void pinModeDummy (struct wiringPiNodeStruct *node, int pin, int mode) { return ; }
1122 static void pullUpDnControlDummy (struct wiringPiNodeStruct *node, int pin, int pud) { return ; }
1123 static int digitalReadDummy (struct wiringPiNodeStruct *node, int pin) { return LOW ; }
1124 static void digitalWriteDummy (struct wiringPiNodeStruct *node, int pin, int value) { return ; }
1125 static void pwmWriteDummy (struct wiringPiNodeStruct *node, int pin, int value) { return ; }
1126 static int analogReadDummy (struct wiringPiNodeStruct *node, int pin) { return 0 ; }
1127 static void analogWriteDummy (struct wiringPiNodeStruct *node, int pin, int value) { return ; }
1129 struct wiringPiNodeStruct *wiringPiNewNode (int pinBase, int numPins)
1132 struct wiringPiNodeStruct *node ;
1134 // Minimum pin base is 64
1137 (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: pinBase of %d is < 64\n", pinBase) ;
1139 // Check all pins in-case there is overlap:
1141 for (pin = pinBase ; pin < (pinBase + numPins) ; ++pin)
1142 if (wiringPiFindNode (pin) != NULL)
1143 (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: Pin %d overlaps with existing definition\n", pin) ;
1145 node = (struct wiringPiNodeStruct *)calloc (sizeof (struct wiringPiNodeStruct), 1) ; // calloc zeros
1147 (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: Unable to allocate memory: %s\n", strerror (errno)) ;
1149 node->pinBase = pinBase ;
1150 node->pinMax = pinBase + numPins - 1 ;
1151 node->pinMode = pinModeDummy ;
1152 node->pullUpDnControl = pullUpDnControlDummy ;
1153 node->digitalRead = digitalReadDummy ;
1154 node->digitalWrite = digitalWriteDummy ;
1155 node->pwmWrite = pwmWriteDummy ;
1156 node->analogRead = analogReadDummy ;
1157 node->analogWrite = analogWriteDummy ;
1158 node->next = wiringPiNodes ;
1159 wiringPiNodes = node ;
1169 * Enables edge-detect mode on a pin - from a 0 to a 1 or 1 to 0
1170 * Pin must already be in input mode with appropriate pull up/downs set.
1171 *********************************************************************************
1174 void pinEnableED01Pi (int pin)
1176 pin = pinToGpio [pin & 63] ;
1182 *********************************************************************************
1184 *********************************************************************************
1189 * This is an un-documented special to let you set any pin to any mode
1190 *********************************************************************************
1193 void pinModeAlt (int pin, int mode)
1197 if ((pin & PI_GPIO_MASK) == 0) // On-board pin
1199 /**/ if (wiringPiMode == WPI_MODE_PINS)
1200 pin = pinToGpio [pin] ;
1201 else if (wiringPiMode == WPI_MODE_PHYS)
1202 pin = physToGpio [pin] ;
1203 else if (wiringPiMode != WPI_MODE_GPIO)
1206 fSel = gpioToGPFSEL [pin] ;
1207 shift = gpioToShift [pin] ;
1209 *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | ((mode & 0x7) << shift) ;
1216 * Sets the mode of a pin to be input, output or PWM output
1217 *********************************************************************************
1220 void pinMode (int pin, int mode)
1222 int fSel, shift, alt ;
1223 struct wiringPiNodeStruct *node = wiringPiNodes ;
1226 if ((pin & PI_GPIO_MASK) == 0) // On-board pin
1228 /**/ if (wiringPiMode == WPI_MODE_PINS)
1229 pin = pinToGpio [pin] ;
1230 else if (wiringPiMode == WPI_MODE_PHYS)
1231 pin = physToGpio [pin] ;
1232 else if (wiringPiMode != WPI_MODE_GPIO)
1235 softPwmStop (origPin) ;
1236 softToneStop (origPin) ;
1238 fSel = gpioToGPFSEL [pin] ;
1239 shift = gpioToShift [pin] ;
1241 /**/ if (mode == INPUT)
1242 *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) ; // Sets bits to zero = input
1243 else if (mode == OUTPUT)
1244 *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (1 << shift) ;
1245 else if (mode == SOFT_PWM_OUTPUT)
1246 softPwmCreate (origPin, 0, 100) ;
1247 else if (mode == SOFT_TONE_OUTPUT)
1248 softToneCreate (origPin) ;
1249 else if (mode == PWM_TONE_OUTPUT)
1251 if (RASPBERRY_PI_PERI_BASE == 0) // Ignore for now
1254 pinMode (origPin, PWM_OUTPUT) ; // Call myself to enable PWM mode
1255 pwmSetMode (PWM_MODE_MS) ;
1257 else if (mode == PWM_OUTPUT)
1259 if (RASPBERRY_PI_PERI_BASE == 0) // Ignore for now
1262 if ((alt = gpioToPwmALT [pin]) == 0) // Not a hardware capable PWM pin
1265 // Set pin to PWM mode
1267 *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ;
1268 delayMicroseconds (110) ; // See comments in pwmSetClockWPi
1270 pwmSetMode (PWM_MODE_BAL) ; // Pi default mode
1271 pwmSetRange (1024) ; // Default range of 1024
1272 pwmSetClock (32) ; // 19.2 / 32 = 600KHz - Also starts the PWM
1274 else if (mode == GPIO_CLOCK)
1276 if (RASPBERRY_PI_PERI_BASE == 0) // Ignore for now
1279 if ((alt = gpioToGpClkALT0 [pin]) == 0) // Not a GPIO_CLOCK pin
1282 // Set pin to GPIO_CLOCK mode and set the clock frequency to 100KHz
1284 *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ;
1285 delayMicroseconds (110) ;
1286 gpioClockSet (pin, 100000) ;
1291 if ((node = wiringPiFindNode (pin)) != NULL)
1292 node->pinMode (node, pin, mode) ;
1300 * Control the internal pull-up/down resistors on a GPIO pin
1301 * The Arduino only has pull-ups and these are enabled by writing 1
1302 * to a port when in input mode - this paradigm doesn't quite apply
1304 *********************************************************************************
1307 void pullUpDnControl (int pin, int pud)
1309 struct wiringPiNodeStruct *node = wiringPiNodes ;
1311 if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
1313 /**/ if (wiringPiMode == WPI_MODE_PINS)
1314 pin = pinToGpio [pin] ;
1315 else if (wiringPiMode == WPI_MODE_PHYS)
1316 pin = physToGpio [pin] ;
1317 else if (wiringPiMode != WPI_MODE_GPIO)
1320 *(gpio + GPPUD) = pud & 3 ; delayMicroseconds (5) ;
1321 *(gpio + gpioToPUDCLK [pin]) = 1 << (pin & 31) ; delayMicroseconds (5) ;
1323 *(gpio + GPPUD) = 0 ; delayMicroseconds (5) ;
1324 *(gpio + gpioToPUDCLK [pin]) = 0 ; delayMicroseconds (5) ;
1326 else // Extension module
1328 if ((node = wiringPiFindNode (pin)) != NULL)
1329 node->pullUpDnControl (node, pin, pud) ;
1337 * Read the value of a given Pin, returning HIGH or LOW
1338 *********************************************************************************
1341 int digitalRead (int pin)
1344 struct wiringPiNodeStruct *node = wiringPiNodes ;
1346 if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
1348 /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS) // Sys mode
1350 if (sysFds [pin] == -1)
1353 lseek (sysFds [pin], 0L, SEEK_SET) ;
1354 read (sysFds [pin], &c, 1) ;
1355 return (c == '0') ? LOW : HIGH ;
1357 else if (wiringPiMode == WPI_MODE_PINS)
1358 pin = pinToGpio [pin] ;
1359 else if (wiringPiMode == WPI_MODE_PHYS)
1360 pin = physToGpio [pin] ;
1361 else if (wiringPiMode != WPI_MODE_GPIO)
1364 if ((*(gpio + gpioToGPLEV [pin]) & (1 << (pin & 31))) != 0)
1371 if ((node = wiringPiFindNode (pin)) == NULL)
1373 return node->digitalRead (node, pin) ;
1381 *********************************************************************************
1384 void digitalWrite (int pin, int value)
1386 struct wiringPiNodeStruct *node = wiringPiNodes ;
1388 if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
1390 /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS) // Sys mode
1392 if (sysFds [pin] != -1)
1395 write (sysFds [pin], "0\n", 2) ;
1397 write (sysFds [pin], "1\n", 2) ;
1401 else if (wiringPiMode == WPI_MODE_PINS)
1402 pin = pinToGpio [pin] ;
1403 else if (wiringPiMode == WPI_MODE_PHYS)
1404 pin = physToGpio [pin] ;
1405 else if (wiringPiMode != WPI_MODE_GPIO)
1409 *(gpio + gpioToGPCLR [pin]) = 1 << (pin & 31) ;
1411 *(gpio + gpioToGPSET [pin]) = 1 << (pin & 31) ;
1415 if ((node = wiringPiFindNode (pin)) != NULL)
1416 node->digitalWrite (node, pin, value) ;
1423 * Set an output PWM value
1424 *********************************************************************************
1427 void pwmWrite (int pin, int value)
1429 struct wiringPiNodeStruct *node = wiringPiNodes ;
1431 if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
1433 if (RASPBERRY_PI_PERI_BASE == 0) // Ignore for now
1436 /**/ if (wiringPiMode == WPI_MODE_PINS)
1437 pin = pinToGpio [pin] ;
1438 else if (wiringPiMode == WPI_MODE_PHYS)
1439 pin = physToGpio [pin] ;
1440 else if (wiringPiMode != WPI_MODE_GPIO)
1443 *(pwm + gpioToPwmPort [pin]) = value ;
1447 if ((node = wiringPiFindNode (pin)) != NULL)
1448 node->pwmWrite (node, pin, value) ;
1455 * Read the analog value of a given Pin.
1456 * There is no on-board Pi analog hardware,
1457 * so this needs to go to a new node.
1458 *********************************************************************************
1461 int analogRead (int pin)
1463 struct wiringPiNodeStruct *node = wiringPiNodes ;
1465 if ((node = wiringPiFindNode (pin)) == NULL)
1468 return node->analogRead (node, pin) ;
1474 * Write the analog value to the given Pin.
1475 * There is no on-board Pi analog hardware,
1476 * so this needs to go to a new node.
1477 *********************************************************************************
1480 void analogWrite (int pin, int value)
1482 struct wiringPiNodeStruct *node = wiringPiNodes ;
1484 if ((node = wiringPiFindNode (pin)) == NULL)
1487 node->analogWrite (node, pin, value) ;
1494 * Output the given frequency on the Pi's PWM pin
1495 *********************************************************************************
1498 void pwmToneWrite (int pin, int freq)
1502 if (RASPBERRY_PI_PERI_BASE == 0) // Ignore for now
1506 pwmWrite (pin, 0) ; // Off
1509 range = 600000 / freq ;
1510 pwmSetRange (range) ;
1511 pwmWrite (pin, freq / 2) ;
1520 * Write an 8-bit byte to the first 8 GPIO pins - try to do it as
1522 * However it still needs 2 operations to set the bits, so any external
1523 * hardware must not rely on seeing a change as there will be a change
1524 * to set the outputs bits to zero, then another change to set the 1's
1525 *********************************************************************************
1528 void digitalWriteByte (int value)
1530 uint32_t pinSet = 0 ;
1531 uint32_t pinClr = 0 ;
1535 /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS)
1537 for (pin = 0 ; pin < 8 ; ++pin)
1539 digitalWrite (pin, value & mask) ;
1546 for (pin = 0 ; pin < 8 ; ++pin)
1548 if ((value & mask) == 0)
1549 pinClr |= (1 << pinToGpio [pin]) ;
1551 pinSet |= (1 << pinToGpio [pin]) ;
1556 *(gpio + gpioToGPCLR [0]) = pinClr ;
1557 *(gpio + gpioToGPSET [0]) = pinSet ;
1565 * Wait for Interrupt on a GPIO pin.
1566 * This is actually done via the /sys/class/gpio interface regardless of
1567 * the wiringPi access mode in-use. Maybe sometime it might get a better
1568 * way for a bit more efficiency.
1569 *********************************************************************************
1572 int waitForInterrupt (int pin, int mS)
1576 struct pollfd polls ;
1578 /**/ if (wiringPiMode == WPI_MODE_PINS)
1579 pin = pinToGpio [pin] ;
1580 else if (wiringPiMode == WPI_MODE_PHYS)
1581 pin = physToGpio [pin] ;
1583 if ((fd = sysFds [pin]) == -1)
1586 // Setup poll structure
1589 polls.events = POLLPRI ; // Urgent data!
1593 x = poll (&polls, 1, mS) ;
1595 // Do a dummy read to clear the interrupt
1596 // A one character read appars to be enough.
1597 // Followed by a seek to reset it.
1599 (void)read (fd, &c, 1) ;
1600 lseek (fd, 0, SEEK_SET) ;
1608 * This is a thread and gets started to wait for the interrupt we're
1609 * hoping to catch. It will call the user-function when the interrupt
1611 *********************************************************************************
1614 static void *interruptHandler (void *arg)
1618 (void)piHiPri (55) ; // Only effective if we run as root
1624 if (waitForInterrupt (myPin, -1) > 0)
1625 isrFunctions [myPin] () ;
1634 * Take the details and create an interrupt handler that will do a call-
1635 * back to the user supplied function.
1636 *********************************************************************************
1639 int wiringPiISR (int pin, int mode, void (*function)(void))
1641 pthread_t threadId ;
1650 if ((pin < 0) || (pin > 63))
1651 return wiringPiFailure (WPI_FATAL, "wiringPiISR: pin must be 0-63 (%d)\n", pin) ;
1653 /**/ if (wiringPiMode == WPI_MODE_UNINITIALISED)
1654 return wiringPiFailure (WPI_FATAL, "wiringPiISR: wiringPi has not been initialised. Unable to continue.\n") ;
1655 else if (wiringPiMode == WPI_MODE_PINS)
1656 bcmGpioPin = pinToGpio [pin] ;
1657 else if (wiringPiMode == WPI_MODE_PHYS)
1658 bcmGpioPin = physToGpio [pin] ;
1662 // Now export the pin and set the right edge
1663 // We're going to use the gpio program to do this, so it assumes
1664 // a full installation of wiringPi. It's a bit 'clunky', but it
1665 // is a way that will work when we're running in "Sys" mode, as
1666 // a non-root user. (without sudo)
1668 if (mode != INT_EDGE_SETUP)
1670 /**/ if (mode == INT_EDGE_FALLING)
1672 else if (mode == INT_EDGE_RISING)
1677 sprintf (pinS, "%d", bcmGpioPin) ;
1679 if ((pid = fork ()) < 0) // Fail
1680 return wiringPiFailure (WPI_FATAL, "wiringPiISR: fork failed: %s\n", strerror (errno)) ;
1682 if (pid == 0) // Child, exec
1684 /**/ if (access ("/usr/local/bin/gpio", X_OK) == 0)
1686 execl ("/usr/local/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ;
1687 return wiringPiFailure (WPI_FATAL, "wiringPiISR: execl failed: %s\n", strerror (errno)) ;
1689 else if (access ("/usr/bin/gpio", X_OK) == 0)
1691 execl ("/usr/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ;
1692 return wiringPiFailure (WPI_FATAL, "wiringPiISR: execl failed: %s\n", strerror (errno)) ;
1695 return wiringPiFailure (WPI_FATAL, "wiringPiISR: Can't find gpio program\n") ;
1697 else // Parent, wait
1701 // Now pre-open the /sys/class node - but it may already be open if
1702 // we are in Sys mode...
1704 if (sysFds [bcmGpioPin] == -1)
1706 sprintf (fName, "/sys/class/gpio/gpio%d/value", bcmGpioPin) ;
1707 if ((sysFds [bcmGpioPin] = open (fName, O_RDWR)) < 0)
1708 return wiringPiFailure (WPI_FATAL, "wiringPiISR: unable to open %s: %s\n", fName, strerror (errno)) ;
1711 // Clear any initial pending interrupt
1713 ioctl (sysFds [bcmGpioPin], FIONREAD, &count) ;
1714 for (i = 0 ; i < count ; ++i)
1715 read (sysFds [bcmGpioPin], &c, 1) ;
1717 isrFunctions [pin] = function ;
1719 pthread_mutex_lock (&pinMutex) ;
1721 pthread_create (&threadId, NULL, interruptHandler, NULL) ;
1722 while (pinPass != -1)
1724 pthread_mutex_unlock (&pinMutex) ;
1732 * Initialise our start-of-time variable to be the current unix
1733 * time in milliseconds and microseconds.
1734 *********************************************************************************
1737 static void initialiseEpoch (void)
1741 gettimeofday (&tv, NULL) ;
1742 epochMilli = (uint64_t)tv.tv_sec * (uint64_t)1000 + (uint64_t)(tv.tv_usec / 1000) ;
1743 epochMicro = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)(tv.tv_usec) ;
1749 * Wait for some number of milliseconds
1750 *********************************************************************************
1753 void delay (unsigned int howLong)
1755 struct timespec sleeper, dummy ;
1757 sleeper.tv_sec = (time_t)(howLong / 1000) ;
1758 sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ;
1760 nanosleep (&sleeper, &dummy) ;
1765 * delayMicroseconds:
1766 * This is somewhat intersting. It seems that on the Pi, a single call
1767 * to nanosleep takes some 80 to 130 microseconds anyway, so while
1768 * obeying the standards (may take longer), it's not always what we
1771 * So what I'll do now is if the delay is less than 100uS we'll do it
1772 * in a hard loop, watching a built-in counter on the ARM chip. This is
1773 * somewhat sub-optimal in that it uses 100% CPU, something not an issue
1774 * in a microcontroller, but under a multi-tasking, multi-user OS, it's
1775 * wastefull, however we've no real choice )-:
1777 * Plan B: It seems all might not be well with that plan, so changing it
1778 * to use gettimeofday () and poll on that instead...
1779 *********************************************************************************
1782 void delayMicrosecondsHard (unsigned int howLong)
1784 struct timeval tNow, tLong, tEnd ;
1786 gettimeofday (&tNow, NULL) ;
1787 tLong.tv_sec = howLong / 1000000 ;
1788 tLong.tv_usec = howLong % 1000000 ;
1789 timeradd (&tNow, &tLong, &tEnd) ;
1791 while (timercmp (&tNow, &tEnd, <))
1792 gettimeofday (&tNow, NULL) ;
1795 void delayMicroseconds (unsigned int howLong)
1797 struct timespec sleeper ;
1798 unsigned int uSecs = howLong % 1000000 ;
1799 unsigned int wSecs = howLong / 1000000 ;
1801 /**/ if (howLong == 0)
1803 else if (howLong < 100)
1804 delayMicrosecondsHard (howLong) ;
1807 sleeper.tv_sec = wSecs ;
1808 sleeper.tv_nsec = (long)(uSecs * 1000L) ;
1809 nanosleep (&sleeper, NULL) ;
1816 * Return a number of milliseconds as an unsigned int.
1817 *********************************************************************************
1820 unsigned int millis (void)
1825 gettimeofday (&tv, NULL) ;
1826 now = (uint64_t)tv.tv_sec * (uint64_t)1000 + (uint64_t)(tv.tv_usec / 1000) ;
1828 return (uint32_t)(now - epochMilli) ;
1834 * Return a number of microseconds as an unsigned int.
1835 *********************************************************************************
1838 unsigned int micros (void)
1843 gettimeofday (&tv, NULL) ;
1844 now = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)tv.tv_usec ;
1846 return (uint32_t)(now - epochMicro) ;
1852 * Must be called once at the start of your program execution.
1854 * Default setup: Initialises the system into wiringPi Pin mode and uses the
1855 * memory mapped hardware directly.
1857 * Changed now to revert to "gpio" mode if we're running on a Compute Module.
1858 *********************************************************************************
1861 int wiringPiSetup (void)
1865 int model, rev, mem, maker, overVolted ;
1867 if (getenv (ENV_DEBUG) != NULL)
1868 wiringPiDebug = TRUE ;
1870 if (getenv (ENV_CODES) != NULL)
1871 wiringPiReturnCodes = TRUE ;
1873 if (getenv (ENV_GPIOMEM) != NULL)
1874 wiringPiTryGpioMem = TRUE ;
1878 printf ("wiringPi: wiringPiSetup called\n") ;
1879 if (wiringPiTryGpioMem)
1880 printf ("wiringPi: Using /dev/gpiomem\n") ;
1883 boardRev = piBoardRev () ;
1885 /**/ if (boardRev == 1) // A, B, Rev 1, 1.1
1887 pinToGpio = pinToGpioR1 ;
1888 physToGpio = physToGpioR1 ;
1890 else // A, B, Rev 2, B+, CM, Pi2
1892 pinToGpio = pinToGpioR2 ;
1893 physToGpio = physToGpioR2 ;
1897 RASPBERRY_PI_PERI_BASE = 0x3F000000 ;
1899 RASPBERRY_PI_PERI_BASE = 0x20000000 ;
1901 // Open the master /dev/ memory control device
1903 // See if /dev/gpiomem exists and we can open it...
1905 if (wiringPiTryGpioMem)
1907 if ((fd = open ("/dev/gpiomem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0)
1908 return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: Unable to open /dev/gpiomem: %s\n", strerror (errno)) ;
1909 RASPBERRY_PI_PERI_BASE = 0 ;
1912 // ... otherwise fall back to the original /dev/mem which requires root level access
1917 // This check is here because people are too stupid to check for themselves or read
1920 if (geteuid () != 0)
1921 (void)wiringPiFailure (WPI_FATAL, "wiringPiSetup: Must be root. (Did you forget sudo?)\n") ;
1923 if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0)
1924 return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: Unable to open /dev/mem: %s\n", strerror (errno)) ;
1927 // Set the offsets into the memory interface.
1929 GPIO_PADS = RASPBERRY_PI_PERI_BASE + 0x00100000 ;
1930 GPIO_CLOCK_BASE = RASPBERRY_PI_PERI_BASE + 0x00101000 ;
1931 GPIO_BASE = RASPBERRY_PI_PERI_BASE + 0x00200000 ;
1932 GPIO_TIMER = RASPBERRY_PI_PERI_BASE + 0x0000B000 ;
1933 GPIO_PWM = RASPBERRY_PI_PERI_BASE + 0x0020C000 ;
1935 // Map the individual hardware components
1939 gpio = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_BASE) ;
1940 if ((int32_t)gpio == -1)
1941 return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (GPIO) failed: %s\n", strerror (errno)) ;
1945 pwm = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PWM) ;
1946 if ((int32_t)pwm == -1)
1947 return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PWM) failed: %s\n", strerror (errno)) ;
1949 // Clock control (needed for PWM)
1951 clk = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_CLOCK_BASE) ;
1952 if ((int32_t)clk == -1)
1953 return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (CLOCK) failed: %s\n", strerror (errno)) ;
1957 pads = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PADS) ;
1958 if ((int32_t)pads == -1)
1959 return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PADS) failed: %s\n", strerror (errno)) ;
1964 timer = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_TIMER) ;
1965 if ((int32_t)timer == -1)
1966 return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (TIMER) failed: %s\n", strerror (errno)) ;
1968 // Set the timer to free-running, 1MHz.
1969 // 0xF9 is 249, the timer divide is base clock / (divide+1)
1970 // so base clock is 250MHz / 250 = 1MHz.
1972 *(timer + TIMER_CONTROL) = 0x0000280 ;
1973 *(timer + TIMER_PRE_DIV) = 0x00000F9 ;
1974 timerIrqRaw = timer + TIMER_IRQ_RAW ;
1977 initialiseEpoch () ;
1979 // If we're running on a compute module, then wiringPi pin numbers don't really many anything...
1981 piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
1982 if (model == PI_MODEL_CM)
1983 wiringPiMode = WPI_MODE_GPIO ;
1985 wiringPiMode = WPI_MODE_PINS ;
1992 * wiringPiSetupGpio:
1993 * Must be called once at the start of your program execution.
1995 * GPIO setup: Initialises the system into GPIO Pin mode and uses the
1996 * memory mapped hardware directly.
1997 *********************************************************************************
2000 int wiringPiSetupGpio (void)
2002 (void)wiringPiSetup () ;
2005 printf ("wiringPi: wiringPiSetupGpio called\n") ;
2007 wiringPiMode = WPI_MODE_GPIO ;
2014 * wiringPiSetupPhys:
2015 * Must be called once at the start of your program execution.
2017 * Phys setup: Initialises the system into Physical Pin mode and uses the
2018 * memory mapped hardware directly.
2019 *********************************************************************************
2022 int wiringPiSetupPhys (void)
2024 (void)wiringPiSetup () ;
2027 printf ("wiringPi: wiringPiSetupPhys called\n") ;
2029 wiringPiMode = WPI_MODE_PHYS ;
2037 * Must be called once at the start of your program execution.
2039 * Initialisation (again), however this time we are using the /sys/class/gpio
2040 * interface to the GPIO systems - slightly slower, but always usable as
2041 * a non-root user, assuming the devices are already exported and setup correctly.
2044 int wiringPiSetupSys (void)
2050 if (getenv (ENV_DEBUG) != NULL)
2051 wiringPiDebug = TRUE ;
2053 if (getenv (ENV_CODES) != NULL)
2054 wiringPiReturnCodes = TRUE ;
2057 printf ("wiringPi: wiringPiSetupSys called\n") ;
2059 boardRev = piBoardRev () ;
2063 pinToGpio = pinToGpioR1 ;
2064 physToGpio = physToGpioR1 ;
2068 pinToGpio = pinToGpioR2 ;
2069 physToGpio = physToGpioR2 ;
2072 // Open and scan the directory, looking for exported GPIOs, and pre-open
2073 // the 'value' interface to speed things up for later
2075 for (pin = 0 ; pin < 64 ; ++pin)
2077 sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
2078 sysFds [pin] = open (fName, O_RDWR) ;
2081 initialiseEpoch () ;
2083 wiringPiMode = WPI_MODE_GPIO_SYS ;