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 static volatile unsigned int BCM2708_PERI_BASE = 0x20000000 ; // Variable for Pi2
135 #define GPIO_PADS (BCM2708_PERI_BASE + 0x00100000)
136 #define CLOCK_BASE (BCM2708_PERI_BASE + 0x00101000)
137 #define GPIO_BASE (BCM2708_PERI_BASE + 0x00200000)
138 #define GPIO_TIMER (BCM2708_PERI_BASE + 0x0000B000)
139 #define GPIO_PWM (BCM2708_PERI_BASE + 0x0020C000)
141 #define PAGE_SIZE (4*1024)
142 #define BLOCK_SIZE (4*1024)
145 // Word offsets into the PWM control region
147 #define PWM_CONTROL 0
154 // Clock regsiter offsets
156 #define PWMCLK_CNTL 40
157 #define PWMCLK_DIV 41
159 #define PWM0_MS_MODE 0x0080 // Run in MS mode
160 #define PWM0_USEFIFO 0x0020 // Data from FIFO
161 #define PWM0_REVPOLAR 0x0010 // Reverse polarity
162 #define PWM0_OFFSTATE 0x0008 // Ouput Off state
163 #define PWM0_REPEATFF 0x0004 // Repeat last value if FIFO empty
164 #define PWM0_SERIAL 0x0002 // Run in serial mode
165 #define PWM0_ENABLE 0x0001 // Channel Enable
167 #define PWM1_MS_MODE 0x8000 // Run in MS mode
168 #define PWM1_USEFIFO 0x2000 // Data from FIFO
169 #define PWM1_REVPOLAR 0x1000 // Reverse polarity
170 #define PWM1_OFFSTATE 0x0800 // Ouput Off state
171 #define PWM1_REPEATFF 0x0400 // Repeat last value if FIFO empty
172 #define PWM1_SERIAL 0x0200 // Run in serial mode
173 #define PWM1_ENABLE 0x0100 // Channel Enable
178 #define TIMER_LOAD (0x400 >> 2)
179 #define TIMER_VALUE (0x404 >> 2)
180 #define TIMER_CONTROL (0x408 >> 2)
181 #define TIMER_IRQ_CLR (0x40C >> 2)
182 #define TIMER_IRQ_RAW (0x410 >> 2)
183 #define TIMER_IRQ_MASK (0x414 >> 2)
184 #define TIMER_RELOAD (0x418 >> 2)
185 #define TIMER_PRE_DIV (0x41C >> 2)
186 #define TIMER_COUNTER (0x420 >> 2)
188 // Locals to hold pointers to the hardware
190 static volatile uint32_t *gpio ;
191 static volatile uint32_t *pwm ;
192 static volatile uint32_t *clk ;
193 static volatile uint32_t *pads ;
196 static volatile uint32_t *timer ;
197 static volatile uint32_t *timerIrqRaw ;
201 // Data for use with the boardId functions.
202 // The order of entries here to correspond with the PI_MODEL_X
203 // and PI_VERSION_X defines in wiringPi.h
204 // Only intended for the gpio command - use at your own risk!
206 static int piModel2 = FALSE ;
208 const char *piModelNames [7] =
216 "Model 2", // Quad Core
219 const char *piRevisionNames [5] =
228 const char *piMakerNames [5] =
238 // Time for easy calculations
240 static uint64_t epochMilli, epochMicro ;
244 static int wiringPiMode = WPI_MODE_UNINITIALISED ;
245 static volatile int pinPass = -1 ;
246 static pthread_mutex_t pinMutex ;
248 // Debugging & Return codes
250 int wiringPiDebug = FALSE ;
251 int wiringPiReturnCodes = FALSE ;
254 // Map a file descriptor from the /sys/class/gpio/gpioX/value
256 static int sysFds [64] =
258 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
259 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
260 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
261 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
266 static void (*isrFunctions [64])(void) ;
269 // Doing it the Arduino way with lookup tables...
270 // Yes, it's probably more innefficient than all the bit-twidling, but it
271 // does tend to make it all a bit clearer. At least to me!
274 // Take a Wiring pin (0 through X) and re-map it to the BCM_GPIO pin
275 // Cope for 3 different board revisions here.
277 static int *pinToGpio ;
281 static int pinToGpioR1 [64] =
283 17, 18, 21, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7: wpi 0 - 7
284 0, 1, // I2C - SDA1, SCL1 wpi 8 - 9
285 8, 7, // SPI - CE1, CE0 wpi 10 - 11
286 10, 9, 11, // SPI - MOSI, MISO, SCLK wpi 12 - 14
287 14, 15, // UART - Tx, Rx wpi 15 - 16
291 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 31
292 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
293 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
298 static int pinToGpioR2 [64] =
300 17, 18, 27, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7: wpi 0 - 7
301 2, 3, // I2C - SDA0, SCL0 wpi 8 - 9
302 8, 7, // SPI - CE1, CE0 wpi 10 - 11
303 10, 9, 11, // SPI - MOSI, MISO, SCLK wpi 12 - 14
304 14, 15, // UART - Tx, Rx wpi 15 - 16
305 28, 29, 30, 31, // Rev 2: New GPIOs 8 though 11 wpi 17 - 20
306 5, 6, 13, 19, 26, // B+ wpi 21, 22, 23, 24, 25
307 12, 16, 20, 21, // B+ wpi 26, 27, 28, 29
308 0, 1, // B+ wpi 30, 31
312 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
313 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
318 // Take a physical pin (1 through 26) and re-map it to the BCM_GPIO pin
319 // Cope for 2 different board revisions here.
320 // Also add in the P5 connector, so the P5 pins are 3,4,5,6, so 53,54,55,56
322 static int *physToGpio ;
324 static int physToGpioR1 [64] =
341 -1, -1, -1, -1, -1, // ... 31
342 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
343 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
346 static int physToGpioR2 [64] =
373 // the P5 connector on the Rev 2 boards:
389 // Map a BCM_GPIO pin to it's Function Selection
390 // control port. (GPFSEL 0-5)
391 // Groups of 10 - 3 bits per Function - 30 bits per port
393 static uint8_t gpioToGPFSEL [] =
395 0,0,0,0,0,0,0,0,0,0,
396 1,1,1,1,1,1,1,1,1,1,
397 2,2,2,2,2,2,2,2,2,2,
398 3,3,3,3,3,3,3,3,3,3,
399 4,4,4,4,4,4,4,4,4,4,
400 5,5,5,5,5,5,5,5,5,5,
405 // Define the shift up for the 3 bits per pin in each GPFSEL port
407 static uint8_t gpioToShift [] =
409 0,3,6,9,12,15,18,21,24,27,
410 0,3,6,9,12,15,18,21,24,27,
411 0,3,6,9,12,15,18,21,24,27,
412 0,3,6,9,12,15,18,21,24,27,
413 0,3,6,9,12,15,18,21,24,27,
418 // (Word) offset to the GPIO Set registers for each GPIO pin
420 static uint8_t gpioToGPSET [] =
422 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,
423 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,
427 // (Word) offset to the GPIO Clear registers for each GPIO pin
429 static uint8_t gpioToGPCLR [] =
431 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,
432 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,
437 // (Word) offset to the GPIO Input level registers for each GPIO pin
439 static uint8_t gpioToGPLEV [] =
441 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,
442 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,
448 // (Word) offset to the Event Detect Status
450 static uint8_t gpioToEDS [] =
452 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,
453 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,
457 // (Word) offset to the Rising edge ENable register
459 static uint8_t gpioToREN [] =
461 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,
462 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,
466 // (Word) offset to the Falling edgde ENable register
468 static uint8_t gpioToFEN [] =
470 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,
471 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,
477 // GPIO Pin pull up/down register
482 // (Word) offset to the Pull Up Down Clock regsiter
484 static uint8_t gpioToPUDCLK [] =
486 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,
487 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,
492 // the ALT value to put a GPIO pin into PWM mode
494 static uint8_t gpioToPwmALT [] =
496 0, 0, 0, 0, 0, 0, 0, 0, // 0 -> 7
497 0, 0, 0, 0, FSEL_ALT0, FSEL_ALT0, 0, 0, // 8 -> 15
498 0, 0, FSEL_ALT5, FSEL_ALT5, 0, 0, 0, 0, // 16 -> 23
499 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
500 0, 0, 0, 0, 0, 0, 0, 0, // 32 -> 39
501 FSEL_ALT0, FSEL_ALT0, 0, 0, 0, FSEL_ALT0, 0, 0, // 40 -> 47
502 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
503 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
508 // The port value to put a GPIO pin into PWM mode
510 static uint8_t gpioToPwmPort [] =
512 0, 0, 0, 0, 0, 0, 0, 0, // 0 -> 7
513 0, 0, 0, 0, PWM0_DATA, PWM1_DATA, 0, 0, // 8 -> 15
514 0, 0, PWM0_DATA, PWM1_DATA, 0, 0, 0, 0, // 16 -> 23
515 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
516 0, 0, 0, 0, 0, 0, 0, 0, // 32 -> 39
517 PWM0_DATA, PWM1_DATA, 0, 0, 0, PWM1_DATA, 0, 0, // 40 -> 47
518 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
519 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
524 // ALT value to put a GPIO pin into GP Clock mode.
525 // On the Pi we can really only use BCM_GPIO_4 and BCM_GPIO_21
526 // for clocks 0 and 1 respectively, however I'll include the full
527 // list for completeness - maybe one day...
529 #define GPIO_CLOCK_SOURCE 1
533 static uint8_t gpioToGpClkALT0 [] =
535 0, 0, 0, 0, FSEL_ALT0, FSEL_ALT0, FSEL_ALT0, 0, // 0 -> 7
536 0, 0, 0, 0, 0, 0, 0, 0, // 8 -> 15
537 0, 0, 0, 0, FSEL_ALT5, FSEL_ALT5, 0, 0, // 16 -> 23
538 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
539 FSEL_ALT0, 0, FSEL_ALT0, 0, 0, 0, 0, 0, // 32 -> 39
540 0, 0, FSEL_ALT0, FSEL_ALT0, FSEL_ALT0, 0, 0, 0, // 40 -> 47
541 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
542 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
546 // (word) Offsets to the clock Control and Divisor register
548 static uint8_t gpioToClkCon [] =
550 -1, -1, -1, -1, 28, 30, 32, -1, // 0 -> 7
551 -1, -1, -1, -1, -1, -1, -1, -1, // 8 -> 15
552 -1, -1, -1, -1, 28, 30, -1, -1, // 16 -> 23
553 -1, -1, -1, -1, -1, -1, -1, -1, // 24 -> 31
554 28, -1, 28, -1, -1, -1, -1, -1, // 32 -> 39
555 -1, -1, 28, 30, 28, -1, -1, -1, // 40 -> 47
556 -1, -1, -1, -1, -1, -1, -1, -1, // 48 -> 55
557 -1, -1, -1, -1, -1, -1, -1, -1, // 56 -> 63
560 static uint8_t gpioToClkDiv [] =
562 -1, -1, -1, -1, 29, 31, 33, -1, // 0 -> 7
563 -1, -1, -1, -1, -1, -1, -1, -1, // 8 -> 15
564 -1, -1, -1, -1, 29, 31, -1, -1, // 16 -> 23
565 -1, -1, -1, -1, -1, -1, -1, -1, // 24 -> 31
566 29, -1, 29, -1, -1, -1, -1, -1, // 32 -> 39
567 -1, -1, 29, 31, 29, -1, -1, -1, // 40 -> 47
568 -1, -1, -1, -1, -1, -1, -1, -1, // 48 -> 55
569 -1, -1, -1, -1, -1, -1, -1, -1, // 56 -> 63
575 *********************************************************************************
582 *********************************************************************************
585 int wiringPiFailure (int fatal, const char *message, ...)
590 if (!fatal && wiringPiReturnCodes)
593 va_start (argp, message) ;
594 vsnprintf (buffer, 1023, message, argp) ;
597 fprintf (stderr, "%s", buffer) ;
598 exit (EXIT_FAILURE) ;
606 * Return a number representing the hardware revision of the board.
608 * Revision 1 really means the early Model B's.
609 * Revision 2 is everything else - it covers the B, B+ and CM.
610 * ... and the Pi 2 - which is a B+ ++ ...
612 * Seems there are some boards with 0000 in them (mistake in manufacture)
613 * So the distinction between boards that I can see is:
616 * 0002 - Model B, Rev 1, 256MB, Egoman
617 * 0003 - Model B, Rev 1.1, 256MB, Egoman, Fuses/D14 removed.
618 * 0004 - Model B, Rev 2, 256MB, Sony
619 * 0005 - Model B, Rev 2, 256MB, Qisda
620 * 0006 - Model B, Rev 2, 256MB, Egoman
621 * 0007 - Model A, Rev 2, 256MB, Egoman
622 * 0008 - Model A, Rev 2, 256MB, Sony
623 * 0009 - Model A, Rev 2, 256MB, Qisda
624 * 000d - Model B, Rev 2, 512MB, Egoman
625 * 000e - Model B, Rev 2, 512MB, Sony
626 * 000f - Model B, Rev 2, 512MB, Qisda
627 * 0010 - Model B+, Rev 1.2, 512MB, Sony
628 * 0011 - Pi CM, Rev 1.2, 512MB, Sony
629 * 0012 - Model A+ Rev 1.2, 256MB, Sony
632 * 0010 - Model 2, Rev 1.1, Quad Core, 1GB, Sony
634 * A small thorn is the olde style overvolting - that will add in
637 * The Pi compute module has an revision of 0011 - since we only check the
638 * last digit, then it's 1, therefore it'll default to not 2 or 3 for a
639 * Rev 1, so will appear as a Rev 2. This is fine for the most part, but
640 * we'll properly detect the Compute Module later and adjust accordingly.
642 *********************************************************************************
645 static void piBoardRevOops (const char *why)
647 fprintf (stderr, "piBoardRev: Unable to determine board revision from /proc/cpuinfo\n") ;
648 fprintf (stderr, " -> %s\n", why) ;
649 fprintf (stderr, " -> You may want to check:\n") ;
650 fprintf (stderr, " -> http://www.raspberrypi.org/phpBB3/viewtopic.php?p=184410#p184410\n") ;
651 exit (EXIT_FAILURE) ;
654 int piBoardRev (void)
659 static int boardRev = -1 ;
661 if (boardRev != -1) // No point checking twice
664 if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL)
665 piBoardRevOops ("Unable to open /proc/cpuinfo") ;
667 // Start by looking for the Architecture, then we can look for a B2 revision....
669 while (fgets (line, 120, cpuFd) != NULL)
670 if (strncmp (line, "Hardware", 8) == 0)
673 if (strncmp (line, "Hardware", 8) != 0)
674 piBoardRevOops ("No \"Hardware\" line") ;
677 printf ("piboardRev: Hardware: %s\n", line) ;
679 // See if it's BCM2708 or BCM2709
681 if (strstr (line, "BCM2709") != NULL)
683 else if (strstr (line, "BCM2708") == NULL)
685 fprintf (stderr, "Unable to determine hardware version. I see: %s,\n", line) ;
686 fprintf (stderr, " - expecting BCM2708 or BCM2709. Please report this to projects@drogon.net\n") ;
687 exit (EXIT_FAILURE) ;
690 // Now do the rest of it as before
694 while (fgets (line, 120, cpuFd) != NULL)
695 if (strncmp (line, "Revision", 8) == 0)
700 if (strncmp (line, "Revision", 8) != 0)
701 piBoardRevOops ("No \"Revision\" line") ;
703 // Chomp trailing CR/NL
705 for (c = &line [strlen (line) - 1] ; (*c == '\n') || (*c == '\r') ; --c)
709 printf ("piboardRev: Revision string: %s\n", line) ;
711 // Scan to first digit
713 for (c = line ; *c ; ++c)
718 piBoardRevOops ("No numeric revision string") ;
720 // Make sure its long enough
723 piBoardRevOops ("Bogus \"Revision\" line (too small)") ;
725 // If you have overvolted the Pi, then it appears that the revision
726 // has 100000 added to it!
727 // The actual condition for it being set is:
728 // (force_turbo || current_limit_override || temp_limit>85) && over_voltage>0
732 printf ("piboardRev: This Pi has/is (force_turbo || current_limit_override || temp_limit>85) && over_voltage>0\n") ;
734 // Isolate last 4 characters:
736 c = c + strlen (c) - 4 ;
739 printf ("piboardRev: last4Chars are: \"%s\"\n", c) ;
741 if ( (strcmp (c, "0002") == 0) || (strcmp (c, "0003") == 0))
747 printf ("piBoardRev: Returning revision: %d\n", boardRev) ;
755 * Do more digging into the board revision string as above, but return
756 * as much details as we can.
757 * This is undocumented and really only intended for the GPIO command.
758 * Use at your own risk!
761 * [USER:8] [NEW:1] [MEMSIZE:3] [MANUFACTURER:4] [PROCESSOR:4] [TYPE:8] [REV:4]
762 * NEW 23: will be 1 for the new scheme, 0 for the old scheme
763 * MEMSIZE 20: 0=256M 1=512M 2=1G
764 * MANUFACTURER 16: 0=SONY 1=EGOMAN 2=EMBEST
765 * PROCESSOR 12: 0=2835 1=2836
766 * TYPE 04: 0=MODELA 1=MODELB 2=MODELA+ 3=MODELB+ 4=Pi2 MODEL B 5=ALPHA 6=CM
767 * REV 00: 0=REV0 1=REV1 2=REV2
768 *********************************************************************************
771 void piBoardId (int *model, int *rev, int *mem, int *maker, int *overVolted)
777 // Will deal with the properly later on - for now, lets just get it going...
778 // unsigned int modelNum ;
780 (void)piBoardRev () ; // Call this first to make sure all's OK. Don't care about the result.
782 if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL)
783 piBoardRevOops ("Unable to open /proc/cpuinfo") ;
785 while (fgets (line, 120, cpuFd) != NULL)
786 if (strncmp (line, "Revision", 8) == 0)
791 if (strncmp (line, "Revision", 8) != 0)
792 piBoardRevOops ("No \"Revision\" line") ;
794 // Chomp trailing CR/NL
796 for (c = &line [strlen (line) - 1] ; (*c == '\n') || (*c == '\r') ; --c)
800 printf ("piboardId: Revision string: %s\n", line) ;
807 for (c = line ; *c ; ++c)
812 piBoardRevOops ("Bogus \"Revision\" line") ;
814 // modelNum = (unsigned int)strtol (++c, NULL, 16) ; // Hex number with no leading 0x
816 *model = PI_MODEL_2 ;
817 *rev = PI_VERSION_1_1 ;
819 *maker = PI_MAKER_SONY ;
824 // Scan to first digit
826 for (c = line ; *c ; ++c)
830 // Make sure its long enough
833 piBoardRevOops ("Bogus \"Revision\" line") ;
835 // If longer than 4, we'll assume it's been overvolted
837 *overVolted = strlen (c) > 4 ;
839 // Extract last 4 characters:
841 c = c + strlen (c) - 4 ;
843 // Fill out the replys as appropriate
845 /**/ if (strcmp (c, "0002") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1 ; *mem = 256 ; *maker = PI_MAKER_EGOMAN ; }
846 else if (strcmp (c, "0003") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_1 ; *mem = 256 ; *maker = PI_MAKER_EGOMAN ; }
847 else if (strcmp (c, "0004") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_SONY ; }
848 else if (strcmp (c, "0005") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_QISDA ; }
849 else if (strcmp (c, "0006") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_EGOMAN ; }
850 else if (strcmp (c, "0007") == 0) { *model = PI_MODEL_A ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_EGOMAN ; }
851 else if (strcmp (c, "0008") == 0) { *model = PI_MODEL_A ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_SONY ; ; }
852 else if (strcmp (c, "0009") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 256 ; *maker = PI_MAKER_QISDA ; }
853 else if (strcmp (c, "000d") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 512 ; *maker = PI_MAKER_EGOMAN ; }
854 else if (strcmp (c, "000e") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 512 ; *maker = PI_MAKER_SONY ; }
855 else if (strcmp (c, "000f") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_2 ; *mem = 512 ; *maker = PI_MAKER_EGOMAN ; }
856 else if (strcmp (c, "0010") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 512 ; *maker = PI_MAKER_SONY ; }
857 else if (strcmp (c, "0011") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_2 ; *mem = 512 ; *maker = PI_MAKER_SONY ; }
858 else if (strcmp (c, "0012") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_2 ; *mem = 256 ; *maker = PI_MAKER_SONY ; }
859 else if (strcmp (c, "0013") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 512 ; *maker = PI_MAKER_MBEST ; }
860 else { *model = 0 ; *rev = 0 ; *mem = 0 ; *maker = 0 ; }
868 * Translate a wiringPi Pin number to native GPIO pin number.
869 * Provided for external support.
870 *********************************************************************************
873 int wpiPinToGpio (int wpiPin)
875 return pinToGpio [wpiPin & 63] ;
881 * Translate a physical Pin number to native GPIO pin number.
882 * Provided for external support.
883 *********************************************************************************
886 int physPinToGpio (int physPin)
888 return physToGpio [physPin & 63] ;
894 * Set the PAD driver value
895 *********************************************************************************
898 void setPadDrive (int group, int value)
902 if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
904 if ((group < 0) || (group > 2))
907 wrVal = BCM_PASSWORD | 0x18 | (value & 7) ;
908 *(pads + group + 11) = wrVal ;
912 printf ("setPadDrive: Group: %d, value: %d (%08X)\n", group, value, wrVal) ;
913 printf ("Read : %08X\n", *(pads + group + 11)) ;
921 * Returns the ALT bits for a given port. Only really of-use
922 * for the gpio readall command (I think)
923 *********************************************************************************
928 int fSel, shift, alt ;
932 /**/ if (wiringPiMode == WPI_MODE_PINS)
933 pin = pinToGpio [pin] ;
934 else if (wiringPiMode == WPI_MODE_PHYS)
935 pin = physToGpio [pin] ;
936 else if (wiringPiMode != WPI_MODE_GPIO)
939 fSel = gpioToGPFSEL [pin] ;
940 shift = gpioToShift [pin] ;
942 alt = (*(gpio + fSel) >> shift) & 7 ;
950 * Select the native "balanced" mode, or standard mark:space mode
951 *********************************************************************************
954 void pwmSetMode (int mode)
956 if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
958 if (mode == PWM_MODE_MS)
959 *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE | PWM0_MS_MODE | PWM1_MS_MODE ;
961 *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ;
968 * Set the PWM range register. We set both range registers to the same
969 * value. If you want different in your own code, then write your own.
970 *********************************************************************************
973 void pwmSetRange (unsigned int range)
975 if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
977 *(pwm + PWM0_RANGE) = range ; delayMicroseconds (10) ;
978 *(pwm + PWM1_RANGE) = range ; delayMicroseconds (10) ;
985 * Set/Change the PWM clock. Originally my code, but changed
986 * (for the better!) by Chris Hall, <chris@kchall.plus.com>
987 * after further study of the manual and testing with a 'scope
988 *********************************************************************************
991 void pwmSetClock (int divisor)
993 uint32_t pwm_control ;
996 if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
999 printf ("Setting to: %d. Current: 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;
1001 pwm_control = *(pwm + PWM_CONTROL) ; // preserve PWM_CONTROL
1003 // We need to stop PWM prior to stopping PWM clock in MS mode otherwise BUSY
1006 *(pwm + PWM_CONTROL) = 0 ; // Stop PWM
1008 // Stop PWM clock before changing divisor. The delay after this does need to
1009 // this big (95uS occasionally fails, 100uS OK), it's almost as though the BUSY
1010 // flag is not working properly in balanced mode. Without the delay when DIV is
1011 // adjusted the clock sometimes switches to very slow, once slow further DIV
1012 // adjustments do nothing and it's difficult to get out of this mode.
1014 *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x01 ; // Stop PWM Clock
1015 delayMicroseconds (110) ; // prevents clock going sloooow
1017 while ((*(clk + PWMCLK_CNTL) & 0x80) != 0) // Wait for clock to be !BUSY
1018 delayMicroseconds (1) ;
1020 *(clk + PWMCLK_DIV) = BCM_PASSWORD | (divisor << 12) ;
1022 *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x11 ; // Start PWM clock
1023 *(pwm + PWM_CONTROL) = pwm_control ; // restore PWM_CONTROL
1026 printf ("Set to: %d. Now : 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;
1033 * Set the freuency on a GPIO clock pin
1034 *********************************************************************************
1037 void gpioClockSet (int pin, int freq)
1039 int divi, divr, divf ;
1043 /**/ if (wiringPiMode == WPI_MODE_PINS)
1044 pin = pinToGpio [pin] ;
1045 else if (wiringPiMode == WPI_MODE_PHYS)
1046 pin = physToGpio [pin] ;
1047 else if (wiringPiMode != WPI_MODE_GPIO)
1050 divi = 19200000 / freq ;
1051 divr = 19200000 % freq ;
1052 divf = (int)((double)divr * 4096.0 / 19200000.0) ;
1057 *(clk + gpioToClkCon [pin]) = BCM_PASSWORD | GPIO_CLOCK_SOURCE ; // Stop GPIO Clock
1058 while ((*(clk + gpioToClkCon [pin]) & 0x80) != 0) // ... and wait
1061 *(clk + gpioToClkDiv [pin]) = BCM_PASSWORD | (divi << 12) | divf ; // Set dividers
1062 *(clk + gpioToClkCon [pin]) = BCM_PASSWORD | 0x10 | GPIO_CLOCK_SOURCE ; // Start Clock
1068 * Locate our device node
1069 *********************************************************************************
1072 struct wiringPiNodeStruct *wiringPiFindNode (int pin)
1074 struct wiringPiNodeStruct *node = wiringPiNodes ;
1076 while (node != NULL)
1077 if ((pin >= node->pinBase) && (pin <= node->pinMax))
1088 * Create a new GPIO node into the wiringPi handling system
1089 *********************************************************************************
1092 static void pinModeDummy (struct wiringPiNodeStruct *node, int pin, int mode) { return ; }
1093 static void pullUpDnControlDummy (struct wiringPiNodeStruct *node, int pin, int pud) { return ; }
1094 static int digitalReadDummy (struct wiringPiNodeStruct *node, int pin) { return LOW ; }
1095 static void digitalWriteDummy (struct wiringPiNodeStruct *node, int pin, int value) { return ; }
1096 static void pwmWriteDummy (struct wiringPiNodeStruct *node, int pin, int value) { return ; }
1097 static int analogReadDummy (struct wiringPiNodeStruct *node, int pin) { return 0 ; }
1098 static void analogWriteDummy (struct wiringPiNodeStruct *node, int pin, int value) { return ; }
1100 struct wiringPiNodeStruct *wiringPiNewNode (int pinBase, int numPins)
1103 struct wiringPiNodeStruct *node ;
1105 // Minimum pin base is 64
1108 (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: pinBase of %d is < 64\n", pinBase) ;
1110 // Check all pins in-case there is overlap:
1112 for (pin = pinBase ; pin < (pinBase + numPins) ; ++pin)
1113 if (wiringPiFindNode (pin) != NULL)
1114 (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: Pin %d overlaps with existing definition\n", pin) ;
1116 node = (struct wiringPiNodeStruct *)calloc (sizeof (struct wiringPiNodeStruct), 1) ; // calloc zeros
1118 (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: Unable to allocate memory: %s\n", strerror (errno)) ;
1120 node->pinBase = pinBase ;
1121 node->pinMax = pinBase + numPins - 1 ;
1122 node->pinMode = pinModeDummy ;
1123 node->pullUpDnControl = pullUpDnControlDummy ;
1124 node->digitalRead = digitalReadDummy ;
1125 node->digitalWrite = digitalWriteDummy ;
1126 node->pwmWrite = pwmWriteDummy ;
1127 node->analogRead = analogReadDummy ;
1128 node->analogWrite = analogWriteDummy ;
1129 node->next = wiringPiNodes ;
1130 wiringPiNodes = node ;
1140 * Enables edge-detect mode on a pin - from a 0 to a 1 or 1 to 0
1141 * Pin must already be in input mode with appropriate pull up/downs set.
1142 *********************************************************************************
1145 void pinEnableED01Pi (int pin)
1147 pin = pinToGpio [pin & 63] ;
1153 *********************************************************************************
1155 *********************************************************************************
1160 * This is an un-documented special to let you set any pin to any mode
1161 *********************************************************************************
1164 void pinModeAlt (int pin, int mode)
1168 if ((pin & PI_GPIO_MASK) == 0) // On-board pin
1170 /**/ if (wiringPiMode == WPI_MODE_PINS)
1171 pin = pinToGpio [pin] ;
1172 else if (wiringPiMode == WPI_MODE_PHYS)
1173 pin = physToGpio [pin] ;
1174 else if (wiringPiMode != WPI_MODE_GPIO)
1177 fSel = gpioToGPFSEL [pin] ;
1178 shift = gpioToShift [pin] ;
1180 *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | ((mode & 0x7) << shift) ;
1187 * Sets the mode of a pin to be input, output or PWM output
1188 *********************************************************************************
1191 void pinMode (int pin, int mode)
1193 int fSel, shift, alt ;
1194 struct wiringPiNodeStruct *node = wiringPiNodes ;
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 softPwmStop (origPin) ;
1207 softToneStop (origPin) ;
1209 fSel = gpioToGPFSEL [pin] ;
1210 shift = gpioToShift [pin] ;
1212 /**/ if (mode == INPUT)
1213 *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) ; // Sets bits to zero = input
1214 else if (mode == OUTPUT)
1215 *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (1 << shift) ;
1216 else if (mode == SOFT_PWM_OUTPUT)
1217 softPwmCreate (origPin, 0, 100) ;
1218 else if (mode == SOFT_TONE_OUTPUT)
1219 softToneCreate (origPin) ;
1220 else if (mode == PWM_TONE_OUTPUT)
1222 pinMode (origPin, PWM_OUTPUT) ; // Call myself to enable PWM mode
1223 pwmSetMode (PWM_MODE_MS) ;
1225 else if (mode == PWM_OUTPUT)
1227 if ((alt = gpioToPwmALT [pin]) == 0) // Not a hardware capable PWM pin
1230 // Set pin to PWM mode
1232 *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ;
1233 delayMicroseconds (110) ; // See comments in pwmSetClockWPi
1235 pwmSetMode (PWM_MODE_BAL) ; // Pi default mode
1236 pwmSetRange (1024) ; // Default range of 1024
1237 pwmSetClock (32) ; // 19.2 / 32 = 600KHz - Also starts the PWM
1239 else if (mode == GPIO_CLOCK)
1241 if ((alt = gpioToGpClkALT0 [pin]) == 0) // Not a GPIO_CLOCK pin
1244 // Set pin to GPIO_CLOCK mode and set the clock frequency to 100KHz
1246 *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ;
1247 delayMicroseconds (110) ;
1248 gpioClockSet (pin, 100000) ;
1253 if ((node = wiringPiFindNode (pin)) != NULL)
1254 node->pinMode (node, pin, mode) ;
1262 * Control the internal pull-up/down resistors on a GPIO pin
1263 * The Arduino only has pull-ups and these are enabled by writing 1
1264 * to a port when in input mode - this paradigm doesn't quite apply
1266 *********************************************************************************
1269 void pullUpDnControl (int pin, int pud)
1271 struct wiringPiNodeStruct *node = wiringPiNodes ;
1273 if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
1275 /**/ if (wiringPiMode == WPI_MODE_PINS)
1276 pin = pinToGpio [pin] ;
1277 else if (wiringPiMode == WPI_MODE_PHYS)
1278 pin = physToGpio [pin] ;
1279 else if (wiringPiMode != WPI_MODE_GPIO)
1282 *(gpio + GPPUD) = pud & 3 ; delayMicroseconds (5) ;
1283 *(gpio + gpioToPUDCLK [pin]) = 1 << (pin & 31) ; delayMicroseconds (5) ;
1285 *(gpio + GPPUD) = 0 ; delayMicroseconds (5) ;
1286 *(gpio + gpioToPUDCLK [pin]) = 0 ; delayMicroseconds (5) ;
1288 else // Extension module
1290 if ((node = wiringPiFindNode (pin)) != NULL)
1291 node->pullUpDnControl (node, pin, pud) ;
1299 * Read the value of a given Pin, returning HIGH or LOW
1300 *********************************************************************************
1303 int digitalRead (int pin)
1306 struct wiringPiNodeStruct *node = wiringPiNodes ;
1308 if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
1310 /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS) // Sys mode
1312 if (sysFds [pin] == -1)
1315 lseek (sysFds [pin], 0L, SEEK_SET) ;
1316 read (sysFds [pin], &c, 1) ;
1317 return (c == '0') ? LOW : HIGH ;
1319 else if (wiringPiMode == WPI_MODE_PINS)
1320 pin = pinToGpio [pin] ;
1321 else if (wiringPiMode == WPI_MODE_PHYS)
1322 pin = physToGpio [pin] ;
1323 else if (wiringPiMode != WPI_MODE_GPIO)
1326 if ((*(gpio + gpioToGPLEV [pin]) & (1 << (pin & 31))) != 0)
1333 if ((node = wiringPiFindNode (pin)) == NULL)
1335 return node->digitalRead (node, pin) ;
1343 *********************************************************************************
1346 void digitalWrite (int pin, int value)
1348 struct wiringPiNodeStruct *node = wiringPiNodes ;
1350 if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
1352 /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS) // Sys mode
1354 if (sysFds [pin] != -1)
1357 write (sysFds [pin], "0\n", 2) ;
1359 write (sysFds [pin], "1\n", 2) ;
1363 else if (wiringPiMode == WPI_MODE_PINS)
1364 pin = pinToGpio [pin] ;
1365 else if (wiringPiMode == WPI_MODE_PHYS)
1366 pin = physToGpio [pin] ;
1367 else if (wiringPiMode != WPI_MODE_GPIO)
1371 *(gpio + gpioToGPCLR [pin]) = 1 << (pin & 31) ;
1373 *(gpio + gpioToGPSET [pin]) = 1 << (pin & 31) ;
1377 if ((node = wiringPiFindNode (pin)) != NULL)
1378 node->digitalWrite (node, pin, value) ;
1385 * Set an output PWM value
1386 *********************************************************************************
1389 void pwmWrite (int pin, int value)
1391 struct wiringPiNodeStruct *node = wiringPiNodes ;
1393 if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
1395 /**/ if (wiringPiMode == WPI_MODE_PINS)
1396 pin = pinToGpio [pin] ;
1397 else if (wiringPiMode == WPI_MODE_PHYS)
1398 pin = physToGpio [pin] ;
1399 else if (wiringPiMode != WPI_MODE_GPIO)
1402 *(pwm + gpioToPwmPort [pin]) = value ;
1406 if ((node = wiringPiFindNode (pin)) != NULL)
1407 node->pwmWrite (node, pin, value) ;
1414 * Read the analog value of a given Pin.
1415 * There is no on-board Pi analog hardware,
1416 * so this needs to go to a new node.
1417 *********************************************************************************
1420 int analogRead (int pin)
1422 struct wiringPiNodeStruct *node = wiringPiNodes ;
1424 if ((node = wiringPiFindNode (pin)) == NULL)
1427 return node->analogRead (node, pin) ;
1433 * Write the analog value to the given Pin.
1434 * There is no on-board Pi analog hardware,
1435 * so this needs to go to a new node.
1436 *********************************************************************************
1439 void analogWrite (int pin, int value)
1441 struct wiringPiNodeStruct *node = wiringPiNodes ;
1443 if ((node = wiringPiFindNode (pin)) == NULL)
1446 node->analogWrite (node, pin, value) ;
1453 * Output the given frequency on the Pi's PWM pin
1454 *********************************************************************************
1457 void pwmToneWrite (int pin, int freq)
1462 pwmWrite (pin, 0) ; // Off
1465 range = 600000 / freq ;
1466 pwmSetRange (range) ;
1467 pwmWrite (pin, freq / 2) ;
1476 * Write an 8-bit byte to the first 8 GPIO pins - try to do it as
1478 * However it still needs 2 operations to set the bits, so any external
1479 * hardware must not rely on seeing a change as there will be a change
1480 * to set the outputs bits to zero, then another change to set the 1's
1481 *********************************************************************************
1484 void digitalWriteByte (int value)
1486 uint32_t pinSet = 0 ;
1487 uint32_t pinClr = 0 ;
1491 /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS)
1493 for (pin = 0 ; pin < 8 ; ++pin)
1495 digitalWrite (pin, value & mask) ;
1502 for (pin = 0 ; pin < 8 ; ++pin)
1504 if ((value & mask) == 0)
1505 pinClr |= (1 << pinToGpio [pin]) ;
1507 pinSet |= (1 << pinToGpio [pin]) ;
1512 *(gpio + gpioToGPCLR [0]) = pinClr ;
1513 *(gpio + gpioToGPSET [0]) = pinSet ;
1521 * Wait for Interrupt on a GPIO pin.
1522 * This is actually done via the /sys/class/gpio interface regardless of
1523 * the wiringPi access mode in-use. Maybe sometime it might get a better
1524 * way for a bit more efficiency.
1525 *********************************************************************************
1528 int waitForInterrupt (int pin, int mS)
1532 struct pollfd polls ;
1534 /**/ if (wiringPiMode == WPI_MODE_PINS)
1535 pin = pinToGpio [pin] ;
1536 else if (wiringPiMode == WPI_MODE_PHYS)
1537 pin = physToGpio [pin] ;
1539 if ((fd = sysFds [pin]) == -1)
1542 // Setup poll structure
1545 polls.events = POLLPRI ; // Urgent data!
1549 x = poll (&polls, 1, mS) ;
1551 // Do a dummy read to clear the interrupt
1552 // A one character read appars to be enough.
1553 // Followed by a seek to reset it.
1555 (void)read (fd, &c, 1) ;
1556 lseek (fd, 0, SEEK_SET) ;
1564 * This is a thread and gets started to wait for the interrupt we're
1565 * hoping to catch. It will call the user-function when the interrupt
1567 *********************************************************************************
1570 static void *interruptHandler (void *arg)
1574 (void)piHiPri (55) ; // Only effective if we run as root
1580 if (waitForInterrupt (myPin, -1) > 0)
1581 isrFunctions [myPin] () ;
1590 * Take the details and create an interrupt handler that will do a call-
1591 * back to the user supplied function.
1592 *********************************************************************************
1595 int wiringPiISR (int pin, int mode, void (*function)(void))
1597 pthread_t threadId ;
1606 if ((pin < 0) || (pin > 63))
1607 return wiringPiFailure (WPI_FATAL, "wiringPiISR: pin must be 0-63 (%d)\n", pin) ;
1609 /**/ if (wiringPiMode == WPI_MODE_UNINITIALISED)
1610 return wiringPiFailure (WPI_FATAL, "wiringPiISR: wiringPi has not been initialised. Unable to continue.\n") ;
1611 else if (wiringPiMode == WPI_MODE_PINS)
1612 bcmGpioPin = pinToGpio [pin] ;
1613 else if (wiringPiMode == WPI_MODE_PHYS)
1614 bcmGpioPin = physToGpio [pin] ;
1618 // Now export the pin and set the right edge
1619 // We're going to use the gpio program to do this, so it assumes
1620 // a full installation of wiringPi. It's a bit 'clunky', but it
1621 // is a way that will work when we're running in "Sys" mode, as
1622 // a non-root user. (without sudo)
1624 if (mode != INT_EDGE_SETUP)
1626 /**/ if (mode == INT_EDGE_FALLING)
1628 else if (mode == INT_EDGE_RISING)
1633 sprintf (pinS, "%d", bcmGpioPin) ;
1635 if ((pid = fork ()) < 0) // Fail
1636 return wiringPiFailure (WPI_FATAL, "wiringPiISR: fork failed: %s\n", strerror (errno)) ;
1638 if (pid == 0) // Child, exec
1640 /**/ if (access ("/usr/local/bin/gpio", X_OK) == 0)
1642 execl ("/usr/local/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ;
1643 return wiringPiFailure (WPI_FATAL, "wiringPiISR: execl failed: %s\n", strerror (errno)) ;
1645 else if (access ("/usr/bin/gpio", X_OK) == 0)
1647 execl ("/usr/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ;
1648 return wiringPiFailure (WPI_FATAL, "wiringPiISR: execl failed: %s\n", strerror (errno)) ;
1651 return wiringPiFailure (WPI_FATAL, "wiringPiISR: Can't find gpio program\n") ;
1653 else // Parent, wait
1657 // Now pre-open the /sys/class node - but it may already be open if
1658 // we are in Sys mode...
1660 if (sysFds [bcmGpioPin] == -1)
1662 sprintf (fName, "/sys/class/gpio/gpio%d/value", bcmGpioPin) ;
1663 if ((sysFds [bcmGpioPin] = open (fName, O_RDWR)) < 0)
1664 return wiringPiFailure (WPI_FATAL, "wiringPiISR: unable to open %s: %s\n", fName, strerror (errno)) ;
1667 // Clear any initial pending interrupt
1669 ioctl (sysFds [bcmGpioPin], FIONREAD, &count) ;
1670 for (i = 0 ; i < count ; ++i)
1671 read (sysFds [bcmGpioPin], &c, 1) ;
1673 isrFunctions [pin] = function ;
1675 pthread_mutex_lock (&pinMutex) ;
1677 pthread_create (&threadId, NULL, interruptHandler, NULL) ;
1678 while (pinPass != -1)
1680 pthread_mutex_unlock (&pinMutex) ;
1688 * Initialise our start-of-time variable to be the current unix
1689 * time in milliseconds and microseconds.
1690 *********************************************************************************
1693 static void initialiseEpoch (void)
1697 gettimeofday (&tv, NULL) ;
1698 epochMilli = (uint64_t)tv.tv_sec * (uint64_t)1000 + (uint64_t)(tv.tv_usec / 1000) ;
1699 epochMicro = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)(tv.tv_usec) ;
1705 * Wait for some number of milliseconds
1706 *********************************************************************************
1709 void delay (unsigned int howLong)
1711 struct timespec sleeper, dummy ;
1713 sleeper.tv_sec = (time_t)(howLong / 1000) ;
1714 sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ;
1716 nanosleep (&sleeper, &dummy) ;
1721 * delayMicroseconds:
1722 * This is somewhat intersting. It seems that on the Pi, a single call
1723 * to nanosleep takes some 80 to 130 microseconds anyway, so while
1724 * obeying the standards (may take longer), it's not always what we
1727 * So what I'll do now is if the delay is less than 100uS we'll do it
1728 * in a hard loop, watching a built-in counter on the ARM chip. This is
1729 * somewhat sub-optimal in that it uses 100% CPU, something not an issue
1730 * in a microcontroller, but under a multi-tasking, multi-user OS, it's
1731 * wastefull, however we've no real choice )-:
1733 * Plan B: It seems all might not be well with that plan, so changing it
1734 * to use gettimeofday () and poll on that instead...
1735 *********************************************************************************
1738 void delayMicrosecondsHard (unsigned int howLong)
1740 struct timeval tNow, tLong, tEnd ;
1742 gettimeofday (&tNow, NULL) ;
1743 tLong.tv_sec = howLong / 1000000 ;
1744 tLong.tv_usec = howLong % 1000000 ;
1745 timeradd (&tNow, &tLong, &tEnd) ;
1747 while (timercmp (&tNow, &tEnd, <))
1748 gettimeofday (&tNow, NULL) ;
1751 void delayMicroseconds (unsigned int howLong)
1753 struct timespec sleeper ;
1754 unsigned int uSecs = howLong % 1000000 ;
1755 unsigned int wSecs = howLong / 1000000 ;
1757 /**/ if (howLong == 0)
1759 else if (howLong < 100)
1760 delayMicrosecondsHard (howLong) ;
1763 sleeper.tv_sec = wSecs ;
1764 sleeper.tv_nsec = (long)(uSecs * 1000L) ;
1765 nanosleep (&sleeper, NULL) ;
1772 * Return a number of milliseconds as an unsigned int.
1773 *********************************************************************************
1776 unsigned int millis (void)
1781 gettimeofday (&tv, NULL) ;
1782 now = (uint64_t)tv.tv_sec * (uint64_t)1000 + (uint64_t)(tv.tv_usec / 1000) ;
1784 return (uint32_t)(now - epochMilli) ;
1790 * Return a number of microseconds as an unsigned int.
1791 *********************************************************************************
1794 unsigned int micros (void)
1799 gettimeofday (&tv, NULL) ;
1800 now = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)tv.tv_usec ;
1802 return (uint32_t)(now - epochMicro) ;
1808 * Must be called once at the start of your program execution.
1810 * Default setup: Initialises the system into wiringPi Pin mode and uses the
1811 * memory mapped hardware directly.
1813 * Changed now to revert to "gpio" mode if we're running on a Compute Module.
1814 *********************************************************************************
1817 int wiringPiSetup (void)
1821 int model, rev, mem, maker, overVolted ;
1823 if (getenv (ENV_DEBUG) != NULL)
1824 wiringPiDebug = TRUE ;
1826 if (getenv (ENV_CODES) != NULL)
1827 wiringPiReturnCodes = TRUE ;
1829 if (geteuid () != 0)
1830 (void)wiringPiFailure (WPI_FATAL, "wiringPiSetup: Must be root. (Did you forget sudo?)\n") ;
1833 printf ("wiringPi: wiringPiSetup called\n") ;
1835 boardRev = piBoardRev () ;
1837 /**/ if (boardRev == 1) // A, B, Rev 1, 1.1
1839 pinToGpio = pinToGpioR1 ;
1840 physToGpio = physToGpioR1 ;
1842 else // A, B, Rev 2, B+, CM, Pi2
1845 BCM2708_PERI_BASE = 0x3F000000 ;
1846 pinToGpio = pinToGpioR2 ;
1847 physToGpio = physToGpioR2 ;
1850 // Open the master /dev/memory device
1852 if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0)
1853 return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: Unable to open /dev/mem: %s\n", strerror (errno)) ;
1857 gpio = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_BASE) ;
1858 if ((int32_t)gpio == -1)
1859 return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (GPIO) failed: %s\n", strerror (errno)) ;
1863 pwm = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PWM) ;
1864 if ((int32_t)pwm == -1)
1865 return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PWM) failed: %s\n", strerror (errno)) ;
1867 // Clock control (needed for PWM)
1869 clk = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, CLOCK_BASE) ;
1870 if ((int32_t)clk == -1)
1871 return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (CLOCK) failed: %s\n", strerror (errno)) ;
1875 pads = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PADS) ;
1876 if ((int32_t)pads == -1)
1877 return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PADS) failed: %s\n", strerror (errno)) ;
1882 timer = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_TIMER) ;
1883 if ((int32_t)timer == -1)
1884 return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (TIMER) failed: %s\n", strerror (errno)) ;
1886 // Set the timer to free-running, 1MHz.
1887 // 0xF9 is 249, the timer divide is base clock / (divide+1)
1888 // so base clock is 250MHz / 250 = 1MHz.
1890 *(timer + TIMER_CONTROL) = 0x0000280 ;
1891 *(timer + TIMER_PRE_DIV) = 0x00000F9 ;
1892 timerIrqRaw = timer + TIMER_IRQ_RAW ;
1895 initialiseEpoch () ;
1897 // If we're running on a compute module, then wiringPi pin numbers don't really many anything...
1899 piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
1900 if (model == PI_MODEL_CM)
1901 wiringPiMode = WPI_MODE_GPIO ;
1903 wiringPiMode = WPI_MODE_PINS ;
1910 * wiringPiSetupGpio:
1911 * Must be called once at the start of your program execution.
1913 * GPIO setup: Initialises the system into GPIO Pin mode and uses the
1914 * memory mapped hardware directly.
1915 *********************************************************************************
1918 int wiringPiSetupGpio (void)
1920 (void)wiringPiSetup () ;
1923 printf ("wiringPi: wiringPiSetupGpio called\n") ;
1925 wiringPiMode = WPI_MODE_GPIO ;
1932 * wiringPiSetupPhys:
1933 * Must be called once at the start of your program execution.
1935 * Phys setup: Initialises the system into Physical Pin mode and uses the
1936 * memory mapped hardware directly.
1937 *********************************************************************************
1940 int wiringPiSetupPhys (void)
1942 (void)wiringPiSetup () ;
1945 printf ("wiringPi: wiringPiSetupPhys called\n") ;
1947 wiringPiMode = WPI_MODE_PHYS ;
1955 * Must be called once at the start of your program execution.
1957 * Initialisation (again), however this time we are using the /sys/class/gpio
1958 * interface to the GPIO systems - slightly slower, but always usable as
1959 * a non-root user, assuming the devices are already exported and setup correctly.
1962 int wiringPiSetupSys (void)
1968 if (getenv (ENV_DEBUG) != NULL)
1969 wiringPiDebug = TRUE ;
1971 if (getenv (ENV_CODES) != NULL)
1972 wiringPiReturnCodes = TRUE ;
1975 printf ("wiringPi: wiringPiSetupSys called\n") ;
1977 boardRev = piBoardRev () ;
1981 pinToGpio = pinToGpioR1 ;
1982 physToGpio = physToGpioR1 ;
1986 pinToGpio = pinToGpioR2 ;
1987 physToGpio = physToGpioR2 ;
1990 // Open and scan the directory, looking for exported GPIOs, and pre-open
1991 // the 'value' interface to speed things up for later
1993 for (pin = 0 ; pin < 64 ; ++pin)
1995 sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
1996 sysFds [pin] = open (fName, O_RDWR) ;
1999 initialiseEpoch () ;
2001 wiringPiMode = WPI_MODE_GPIO_SYS ;