3 * Arduino compatable (ish) Wiring library for the Raspberry Pi
4 * Copyright (c) 2012 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 #define BCM2708_PERI_BASE 0x20000000
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 ;
200 // Time for easy calculations
202 static uint64_t epochMilli, epochMicro ;
206 static int wiringPiMode = WPI_MODE_UNINITIALISED ;
207 static volatile int pinPass = -1 ;
208 static pthread_mutex_t pinMutex ;
210 // Debugging & Return codes
212 int wiringPiDebug = FALSE ;
213 int wiringPiReturnCodes = FALSE ;
216 // Map a file descriptor from the /sys/class/gpio/gpioX/value
218 static int sysFds [64] =
220 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
221 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
222 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
223 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
228 static void (*isrFunctions [64])(void) ;
231 // Doing it the Arduino way with lookup tables...
232 // Yes, it's probably more innefficient than all the bit-twidling, but it
233 // does tend to make it all a bit clearer. At least to me!
236 // Take a Wiring pin (0 through X) and re-map it to the BCM_GPIO pin
237 // Cope for 2 different board revisions here.
239 static int *pinToGpio ;
241 static int pinToGpioR1 [64] =
243 17, 18, 21, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7: wpi 0 - 7
244 0, 1, // I2C - SDA0, SCL0 wpi 8 - 9
245 8, 7, // SPI - CE1, CE0 wpi 10 - 11
246 10, 9, 11, // SPI - MOSI, MISO, SCLK wpi 12 - 14
247 14, 15, // UART - Tx, Rx wpi 15 - 16
251 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 31
252 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
253 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
256 static int pinToGpioR2 [64] =
258 17, 18, 27, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7: wpi 0 - 7
259 2, 3, // I2C - SDA0, SCL0 wpi 8 - 9
260 8, 7, // SPI - CE1, CE0 wpi 10 - 11
261 10, 9, 11, // SPI - MOSI, MISO, SCLK wpi 12 - 14
262 14, 15, // UART - Tx, Rx wpi 15 - 16
263 28, 29, 30, 31, // New GPIOs 8 though 11 wpi 17 - 20
267 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 31
268 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
269 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
274 // Take a physical pin (1 through 26) and re-map it to the BCM_GPIO pin
275 // Cope for 2 different board revisions here.
276 // Also add in the P5 connector, so the P5 pins are 3,4,5,6, so 53,54,55,56
278 static int *physToGpio ;
280 static int physToGpioR1 [64] =
297 -1, -1, -1, -1, -1, // ... 31
298 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
299 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
302 static int physToGpioR2 [64] =
319 // the P5 connector on the Rev 2 boards:
321 -1, -1, -1, -1, -1, // ... 31
322 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
323 -1, -1, -1, -1, -1, // ... 52
324 28, 29, 30, 31, // ... 53, 54, 55, 56 - P5
325 -1, -1, -1, -1, -1, -1, -1, // ... 63
330 // Map a BCM_GPIO pin to it's Function Selection
331 // control port. (GPFSEL 0-5)
332 // Groups of 10 - 3 bits per Function - 30 bits per port
334 static uint8_t gpioToGPFSEL [] =
336 0,0,0,0,0,0,0,0,0,0,
337 1,1,1,1,1,1,1,1,1,1,
338 2,2,2,2,2,2,2,2,2,2,
339 3,3,3,3,3,3,3,3,3,3,
340 4,4,4,4,4,4,4,4,4,4,
341 5,5,5,5,5,5,5,5,5,5,
346 // Define the shift up for the 3 bits per pin in each GPFSEL port
348 static uint8_t gpioToShift [] =
350 0,3,6,9,12,15,18,21,24,27,
351 0,3,6,9,12,15,18,21,24,27,
352 0,3,6,9,12,15,18,21,24,27,
353 0,3,6,9,12,15,18,21,24,27,
354 0,3,6,9,12,15,18,21,24,27,
359 // (Word) offset to the GPIO Set registers for each GPIO pin
361 static uint8_t gpioToGPSET [] =
363 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,
364 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,
368 // (Word) offset to the GPIO Clear registers for each GPIO pin
370 static uint8_t gpioToGPCLR [] =
372 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,
373 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,
378 // (Word) offset to the GPIO Input level registers for each GPIO pin
380 static uint8_t gpioToGPLEV [] =
382 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,
383 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,
389 // (Word) offset to the Event Detect Status
391 static uint8_t gpioToEDS [] =
393 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,
394 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,
398 // (Word) offset to the Rising edgde ENable register
400 static uint8_t gpioToREN [] =
402 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,
403 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,
407 // (Word) offset to the Falling edgde ENable register
409 static uint8_t gpioToFEN [] =
411 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,
412 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,
418 // GPIO Pin pull up/down register
423 // (Word) offset to the Pull Up Down Clock regsiter
425 static uint8_t gpioToPUDCLK [] =
427 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,
428 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,
433 // the ALT value to put a GPIO pin into PWM mode
435 static uint8_t gpioToPwmALT [] =
437 0, 0, 0, 0, 0, 0, 0, 0, // 0 -> 7
438 0, 0, 0, 0, FSEL_ALT0, FSEL_ALT0, 0, 0, // 8 -> 15
439 0, 0, FSEL_ALT5, FSEL_ALT5, 0, 0, 0, 0, // 16 -> 23
440 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
441 0, 0, 0, 0, 0, 0, 0, 0, // 32 -> 39
442 FSEL_ALT0, FSEL_ALT0, 0, 0, 0, FSEL_ALT0, 0, 0, // 40 -> 47
443 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
444 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
449 // The port value to put a GPIO pin into PWM mode
451 static uint8_t gpioToPwmPort [] =
453 0, 0, 0, 0, 0, 0, 0, 0, // 0 -> 7
454 0, 0, 0, 0, PWM0_DATA, PWM1_DATA, 0, 0, // 8 -> 15
455 0, 0, PWM0_DATA, PWM1_DATA, 0, 0, 0, 0, // 16 -> 23
456 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
457 0, 0, 0, 0, 0, 0, 0, 0, // 32 -> 39
458 PWM0_DATA, PWM1_DATA, 0, 0, 0, PWM1_DATA, 0, 0, // 40 -> 47
459 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
460 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
465 // ALT value to put a GPIO pin into GP Clock mode.
466 // On the Pi we can really only use BCM_GPIO_4 and BCM_GPIO_21
467 // for clocks 0 and 1 respectively, however I'll include the full
468 // list for completeness - maybe one day...
470 #define GPIO_CLOCK_SOURCE 1
474 static uint8_t gpioToGpClkALT0 [] =
476 0, 0, 0, 0, FSEL_ALT0, FSEL_ALT0, FSEL_ALT0, 0, // 0 -> 7
477 0, 0, 0, 0, 0, 0, 0, 0, // 8 -> 15
478 0, 0, 0, 0, FSEL_ALT5, FSEL_ALT5, 0, 0, // 16 -> 23
479 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
480 FSEL_ALT0, 0, FSEL_ALT0, 0, 0, 0, 0, 0, // 32 -> 39
481 0, 0, FSEL_ALT0, FSEL_ALT0, FSEL_ALT0, 0, 0, 0, // 40 -> 47
482 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
483 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
487 // (word) Offsets to the clock Control and Divisor register
489 static uint8_t gpioToClkCon [] =
491 -1, -1, -1, -1, 28, 30, 32, -1, // 0 -> 7
492 -1, -1, -1, -1, -1, -1, -1, -1, // 8 -> 15
493 -1, -1, -1, -1, 28, 30, -1, -1, // 16 -> 23
494 -1, -1, -1, -1, -1, -1, -1, -1, // 24 -> 31
495 28, -1, 28, -1, -1, -1, -1, -1, // 32 -> 39
496 -1, -1, 28, 30, 28, -1, -1, -1, // 40 -> 47
497 -1, -1, -1, -1, -1, -1, -1, -1, // 48 -> 55
498 -1, -1, -1, -1, -1, -1, -1, -1, // 56 -> 63
501 static uint8_t gpioToClkDiv [] =
503 -1, -1, -1, -1, 29, 31, 33, -1, // 0 -> 7
504 -1, -1, -1, -1, -1, -1, -1, -1, // 8 -> 15
505 -1, -1, -1, -1, 29, 31, -1, -1, // 16 -> 23
506 -1, -1, -1, -1, -1, -1, -1, -1, // 24 -> 31
507 29, -1, 29, -1, -1, -1, -1, -1, // 32 -> 39
508 -1, -1, 29, 31, 29, -1, -1, -1, // 40 -> 47
509 -1, -1, -1, -1, -1, -1, -1, -1, // 48 -> 55
510 -1, -1, -1, -1, -1, -1, -1, -1, // 56 -> 63
516 *********************************************************************************
523 *********************************************************************************
526 int wiringPiFailure (int fatal, const char *message, ...)
531 if (!fatal && wiringPiReturnCodes)
534 va_start (argp, message) ;
535 vsnprintf (buffer, 1023, message, argp) ;
538 fprintf (stderr, "%s", buffer) ;
539 exit (EXIT_FAILURE) ;
547 * Return a number representing the hardware revision of the board.
548 * Revision is currently 1 or 2.
550 * Much confusion here )-:
551 * Seems there are some boards with 0000 in them (mistake in manufacture)
552 * So the distinction between boards that I can see is:
554 * 0001 - Not used (Compute - default to Rev 2)
555 * 0002 - Model B, Rev 1, 256MB
556 * 0003 - Model B, Rev 1.1, 256MB, Fuses/D14 removed.
557 * 0004 - Model B, Rev 2, 256MB, Sony
558 * 0005 - Model B, Rev 2, 256MB, Qisda
559 * 0006 - Model B, Rev 2, 256MB, Egoman
560 * 0007 - Model A, Rev 2, 256MB, Egoman
561 * 0008 - Model A, Rev 2, 256MB, Sony
562 * 0009 - Model A, Rev 2, 256MB, Qisda
563 * 000d - Model B, Rev 2, 512MB, Egoman
564 * 000e - Model B, Rev 2, 512MB, Sony
565 * 000f - Model B, Rev 2, 512MB, Qisda
566 * 0011 - Pi compute Module
568 * A small thorn is the olde style overvolting - that will add in
571 * The Pi compute module has an revision of 0011 - since we only check the
572 * last digit, then it's 1, therefore it'll default to not 2 or 3 for a
573 * Rev 1, so will appear as a Rev 2. This is fine for the most part, but
574 * we'll properly detect the Compute Module later and adjust accordingly.
576 *********************************************************************************
579 static void piBoardRevOops (const char *why)
581 fprintf (stderr, "piBoardRev: Unable to determine board revision from /proc/cpuinfo\n") ;
582 fprintf (stderr, " -> %s\n", why) ;
583 fprintf (stderr, " -> You may want to check:\n") ;
584 fprintf (stderr, " -> http://www.raspberrypi.org/phpBB3/viewtopic.php?p=184410#p184410\n") ;
585 exit (EXIT_FAILURE) ;
588 int piBoardRev (void)
593 static int boardRev = -1 ;
595 if (boardRev != -1) // No point checking twice
598 if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL)
599 piBoardRevOops ("Unable to open /proc/cpuinfo") ;
601 while (fgets (line, 120, cpuFd) != NULL)
602 if (strncmp (line, "Revision", 8) == 0)
607 if (strncmp (line, "Revision", 8) != 0)
608 piBoardRevOops ("No \"Revision\" line") ;
610 for (c = &line [strlen (line) - 1] ; (*c == '\n') || (*c == '\r') ; --c)
614 printf ("piboardRev: Revision string: %s\n", line) ;
616 for (c = line ; *c ; ++c)
621 piBoardRevOops ("No numeric revision string") ;
623 // If you have overvolted the Pi, then it appears that the revision
624 // has 100000 added to it!
628 printf ("piboardRev: This Pi has/is overvolted!\n") ;
630 lastChar = line [strlen (line) - 1] ;
633 printf ("piboardRev: lastChar is: '%c' (%d, 0x%02X)\n", lastChar, lastChar, lastChar) ;
635 /**/ if ((lastChar == '2') || (lastChar == '3'))
641 printf ("piBoardRev: Returning revision: %d\n", boardRev) ;
649 * Do more digging into the board revision string as above, but return
650 * as much details as we can.
651 *********************************************************************************
654 const char *piModelNames [] =
661 const char *piRevisionNames[] =
668 void piBoardId (int *model, int *rev, int *mem, char **maker)
674 piBoardRev () ; // Call this first to make sure all's OK. Don't care about the result.
676 if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL)
677 piBoardRevOops ("Unable to open /proc/cpuinfo") ;
679 while (fgets (line, 120, cpuFd) != NULL)
680 if (strncmp (line, "Revision", 8) == 0)
685 if (strncmp (line, "Revision", 8) != 0)
686 piBoardRevOops ("No \"Revision\" line") ;
688 // Chomp trailing CR/NL
690 for (c = &line [strlen (line) - 1] ; (*c == '\n') || (*c == '\r') ; --c)
694 printf ("piboardId: Revision string: %s\n", line) ;
696 // Scan to first digit
698 for (c = line ; *c ; ++c)
702 // Make sure its long enough
705 piBoardRevOops ("Bogus \"Revision\" line") ;
707 // Extract last 4 characters:
709 c = c + strlen (c) - 4 ;
711 // Fill out the replys as appropriate
713 /**/ if (strcmp (c, "0002") == 0) { *model = 1 ; *rev = 0 ; *mem = 256 ; *maker = "China" ; }
714 else if (strcmp (c, "0003") == 0) { *model = 1 ; *rev = 1 ; *mem = 256 ; *maker = "China" ; }
715 else if (strcmp (c, "0004") == 0) { *model = 1 ; *rev = 2 ; *mem = 256 ; *maker = "Sony" ; }
716 else if (strcmp (c, "0005") == 0) { *model = 1 ; *rev = 2 ; *mem = 256 ; *maker = "Qisda" ; }
717 else if (strcmp (c, "0006") == 0) { *model = 1 ; *rev = 2 ; *mem = 256 ; *maker = "Egoman" ; }
718 else if (strcmp (c, "0007") == 0) { *model = 0 ; *rev = 2 ; *mem = 256 ; *maker = "Egoman" ; }
719 else if (strcmp (c, "0008") == 0) { *model = 0 ; *rev = 2 ; *mem = 256 ; *maker = "Sony" ; }
720 else if (strcmp (c, "0009") == 0) { *model = 1 ; *rev = 2 ; *mem = 256 ; *maker = "Qisda" ; }
721 else if (strcmp (c, "000d") == 0) { *model = 1 ; *rev = 2 ; *mem = 512 ; *maker = "Egoman" ; }
722 else if (strcmp (c, "000e") == 0) { *model = 1 ; *rev = 2 ; *mem = 512 ; *maker = "Sony" ; }
723 else if (strcmp (c, "000f") == 0) { *model = 1 ; *rev = 2 ; *mem = 512 ; *maker = "Egoman" ; }
724 else if (strcmp (c, "0011") == 0) { *model = 2 ; *rev = 1 ; *mem = 512 ; *maker = "Sony" ; }
725 else { *model = 0 ; *rev = 0 ; *mem = 0 ; *maker = "Unkn" ; }
732 * Translate a wiringPi Pin number to native GPIO pin number.
733 * Provided for external support.
734 *********************************************************************************
737 int wpiPinToGpio (int wpiPin)
739 return pinToGpio [wpiPin & 63] ;
745 * Translate a physical Pin number to native GPIO pin number.
746 * Provided for external support.
747 *********************************************************************************
750 int physPinToGpio (int physPin)
752 return physToGpio [physPin & 63] ;
758 * Set the PAD driver value
759 *********************************************************************************
762 void setPadDrive (int group, int value)
766 if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
768 if ((group < 0) || (group > 2))
771 wrVal = BCM_PASSWORD | 0x18 | (value & 7) ;
772 *(pads + group + 11) = wrVal ;
776 printf ("setPadDrive: Group: %d, value: %d (%08X)\n", group, value, wrVal) ;
777 printf ("Read : %08X\n", *(pads + group + 11)) ;
785 * Returns the ALT bits for a given port. Only really of-use
786 * for the gpio readall command (I think)
787 *********************************************************************************
792 int fSel, shift, alt ;
796 /**/ if (wiringPiMode == WPI_MODE_PINS)
797 pin = pinToGpio [pin] ;
798 else if (wiringPiMode == WPI_MODE_PHYS)
799 pin = physToGpio [pin] ;
800 else if (wiringPiMode != WPI_MODE_GPIO)
803 fSel = gpioToGPFSEL [pin] ;
804 shift = gpioToShift [pin] ;
806 alt = (*(gpio + fSel) >> shift) & 7 ;
814 * Select the native "balanced" mode, or standard mark:space mode
815 *********************************************************************************
818 void pwmSetMode (int mode)
820 if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
822 if (mode == PWM_MODE_MS)
823 *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE | PWM0_MS_MODE | PWM1_MS_MODE ;
825 *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ;
832 * Set the PWM range register. We set both range registers to the same
833 * value. If you want different in your own code, then write your own.
834 *********************************************************************************
837 void pwmSetRange (unsigned int range)
839 if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
841 *(pwm + PWM0_RANGE) = range ; delayMicroseconds (10) ;
842 *(pwm + PWM1_RANGE) = range ; delayMicroseconds (10) ;
849 * Set/Change the PWM clock. Originally my code, but changed
850 * (for the better!) by Chris Hall, <chris@kchall.plus.com>
851 * after further study of the manual and testing with a 'scope
852 *********************************************************************************
855 void pwmSetClock (int divisor)
857 uint32_t pwm_control ;
860 if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
863 printf ("Setting to: %d. Current: 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;
865 pwm_control = *(pwm + PWM_CONTROL) ; // preserve PWM_CONTROL
867 // We need to stop PWM prior to stopping PWM clock in MS mode otherwise BUSY
870 *(pwm + PWM_CONTROL) = 0 ; // Stop PWM
872 // Stop PWM clock before changing divisor. The delay after this does need to
873 // this big (95uS occasionally fails, 100uS OK), it's almost as though the BUSY
874 // flag is not working properly in balanced mode. Without the delay when DIV is
875 // adjusted the clock sometimes switches to very slow, once slow further DIV
876 // adjustments do nothing and it's difficult to get out of this mode.
878 *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x01 ; // Stop PWM Clock
879 delayMicroseconds (110) ; // prevents clock going sloooow
881 while ((*(clk + PWMCLK_CNTL) & 0x80) != 0) // Wait for clock to be !BUSY
882 delayMicroseconds (1) ;
884 *(clk + PWMCLK_DIV) = BCM_PASSWORD | (divisor << 12) ;
886 *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x11 ; // Start PWM clock
887 *(pwm + PWM_CONTROL) = pwm_control ; // restore PWM_CONTROL
890 printf ("Set to: %d. Now : 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;
897 * Set the freuency on a GPIO clock pin
898 *********************************************************************************
901 void gpioClockSet (int pin, int freq)
903 int divi, divr, divf ;
907 /**/ if (wiringPiMode == WPI_MODE_PINS)
908 pin = pinToGpio [pin] ;
909 else if (wiringPiMode == WPI_MODE_PHYS)
910 pin = physToGpio [pin] ;
911 else if (wiringPiMode != WPI_MODE_GPIO)
914 divi = 19200000 / freq ;
915 divr = 19200000 % freq ;
916 divf = (int)((double)divr * 4096.0 / 19200000.0) ;
921 *(clk + gpioToClkCon [pin]) = BCM_PASSWORD | GPIO_CLOCK_SOURCE ; // Stop GPIO Clock
922 while ((*(clk + gpioToClkCon [pin]) & 0x80) != 0) // ... and wait
925 *(clk + gpioToClkDiv [pin]) = BCM_PASSWORD | (divi << 12) | divf ; // Set dividers
926 *(clk + gpioToClkCon [pin]) = BCM_PASSWORD | 0x10 | GPIO_CLOCK_SOURCE ; // Start Clock
932 * Locate our device node
933 *********************************************************************************
936 struct wiringPiNodeStruct *wiringPiFindNode (int pin)
938 struct wiringPiNodeStruct *node = wiringPiNodes ;
941 if ((pin >= node->pinBase) && (pin <= node->pinMax))
952 * Create a new GPIO node into the wiringPi handling system
953 *********************************************************************************
956 static void pinModeDummy (struct wiringPiNodeStruct *node, int pin, int mode) { return ; }
957 static void pullUpDnControlDummy (struct wiringPiNodeStruct *node, int pin, int pud) { return ; }
958 static int digitalReadDummy (struct wiringPiNodeStruct *node, int pin) { return LOW ; }
959 static void digitalWriteDummy (struct wiringPiNodeStruct *node, int pin, int value) { return ; }
960 static void pwmWriteDummy (struct wiringPiNodeStruct *node, int pin, int value) { return ; }
961 static int analogReadDummy (struct wiringPiNodeStruct *node, int pin) { return 0 ; }
962 static void analogWriteDummy (struct wiringPiNodeStruct *node, int pin, int value) { return ; }
964 struct wiringPiNodeStruct *wiringPiNewNode (int pinBase, int numPins)
967 struct wiringPiNodeStruct *node ;
969 // Minimum pin base is 64
972 (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: pinBase of %d is < 64\n", pinBase) ;
974 // Check all pins in-case there is overlap:
976 for (pin = pinBase ; pin < (pinBase + numPins) ; ++pin)
977 if (wiringPiFindNode (pin) != NULL)
978 (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: Pin %d overlaps with existing definition\n", pin) ;
980 node = (struct wiringPiNodeStruct *)calloc (sizeof (struct wiringPiNodeStruct), 1) ; // calloc zeros
982 (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: Unable to allocate memory: %s\n", strerror (errno)) ;
984 node->pinBase = pinBase ;
985 node->pinMax = pinBase + numPins - 1 ;
986 node->pinMode = pinModeDummy ;
987 node->pullUpDnControl = pullUpDnControlDummy ;
988 node->digitalRead = digitalReadDummy ;
989 node->digitalWrite = digitalWriteDummy ;
990 node->pwmWrite = pwmWriteDummy ;
991 node->analogRead = analogReadDummy ;
992 node->analogWrite = analogWriteDummy ;
993 node->next = wiringPiNodes ;
994 wiringPiNodes = node ;
1004 * Enables edge-detect mode on a pin - from a 0 to a 1 or 1 to 0
1005 * Pin must already be in input mode with appropriate pull up/downs set.
1006 *********************************************************************************
1009 void pinEnableED01Pi (int pin)
1011 pin = pinToGpio [pin & 63] ;
1017 *********************************************************************************
1019 *********************************************************************************
1024 * This is an un-documented special to let you set any pin to any mode
1025 *********************************************************************************
1028 void pinModeAlt (int pin, int mode)
1032 if ((pin & PI_GPIO_MASK) == 0) // On-board pin
1034 /**/ if (wiringPiMode == WPI_MODE_PINS)
1035 pin = pinToGpio [pin] ;
1036 else if (wiringPiMode == WPI_MODE_PHYS)
1037 pin = physToGpio [pin] ;
1038 else if (wiringPiMode != WPI_MODE_GPIO)
1041 fSel = gpioToGPFSEL [pin] ;
1042 shift = gpioToShift [pin] ;
1044 *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | ((mode & 0x7) << shift) ;
1051 * Sets the mode of a pin to be input, output or PWM output
1052 *********************************************************************************
1055 void pinMode (int pin, int mode)
1057 int fSel, shift, alt ;
1058 struct wiringPiNodeStruct *node = wiringPiNodes ;
1061 if ((pin & PI_GPIO_MASK) == 0) // On-board pin
1063 /**/ if (wiringPiMode == WPI_MODE_PINS)
1064 pin = pinToGpio [pin] ;
1065 else if (wiringPiMode == WPI_MODE_PHYS)
1066 pin = physToGpio [pin] ;
1067 else if (wiringPiMode != WPI_MODE_GPIO)
1070 softPwmStop (origPin) ;
1071 softToneStop (origPin) ;
1073 fSel = gpioToGPFSEL [pin] ;
1074 shift = gpioToShift [pin] ;
1076 /**/ if (mode == INPUT)
1077 *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) ; // Sets bits to zero = input
1078 else if (mode == OUTPUT)
1079 *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (1 << shift) ;
1080 else if (mode == SOFT_PWM_OUTPUT)
1081 softPwmCreate (origPin, 0, 100) ;
1082 else if (mode == SOFT_TONE_OUTPUT)
1083 softToneCreate (origPin) ;
1084 else if (mode == PWM_OUTPUT)
1086 if ((alt = gpioToPwmALT [pin]) == 0) // Not a hardware capable PWM pin
1089 // Set pin to PWM mode
1091 *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ;
1092 delayMicroseconds (110) ; // See comments in pwmSetClockWPi
1094 pwmSetMode (PWM_MODE_BAL) ; // Pi default mode
1095 pwmSetRange (1024) ; // Default range of 1024
1096 pwmSetClock (32) ; // 19.2 / 32 = 600KHz - Also starts the PWM
1098 else if (mode == GPIO_CLOCK)
1100 if ((alt = gpioToGpClkALT0 [pin]) == 0) // Not a GPIO_CLOCK pin
1103 // Set pin to GPIO_CLOCK mode and set the clock frequency to 100KHz
1105 *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ;
1106 delayMicroseconds (110) ;
1107 gpioClockSet (pin, 100000) ;
1112 if ((node = wiringPiFindNode (pin)) != NULL)
1113 node->pinMode (node, pin, mode) ;
1121 * Control the internal pull-up/down resistors on a GPIO pin
1122 * The Arduino only has pull-ups and these are enabled by writing 1
1123 * to a port when in input mode - this paradigm doesn't quite apply
1125 *********************************************************************************
1128 void pullUpDnControl (int pin, int pud)
1130 struct wiringPiNodeStruct *node = wiringPiNodes ;
1132 if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
1134 /**/ if (wiringPiMode == WPI_MODE_PINS)
1135 pin = pinToGpio [pin] ;
1136 else if (wiringPiMode == WPI_MODE_PHYS)
1137 pin = physToGpio [pin] ;
1138 else if (wiringPiMode != WPI_MODE_GPIO)
1141 *(gpio + GPPUD) = pud & 3 ; delayMicroseconds (5) ;
1142 *(gpio + gpioToPUDCLK [pin]) = 1 << (pin & 31) ; delayMicroseconds (5) ;
1144 *(gpio + GPPUD) = 0 ; delayMicroseconds (5) ;
1145 *(gpio + gpioToPUDCLK [pin]) = 0 ; delayMicroseconds (5) ;
1147 else // Extension module
1149 if ((node = wiringPiFindNode (pin)) != NULL)
1150 node->pullUpDnControl (node, pin, pud) ;
1158 * Read the value of a given Pin, returning HIGH or LOW
1159 *********************************************************************************
1162 int digitalRead (int pin)
1165 struct wiringPiNodeStruct *node = wiringPiNodes ;
1167 if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
1169 /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS) // Sys mode
1171 if (sysFds [pin] == -1)
1174 lseek (sysFds [pin], 0L, SEEK_SET) ;
1175 read (sysFds [pin], &c, 1) ;
1176 return (c == '0') ? LOW : HIGH ;
1178 else if (wiringPiMode == WPI_MODE_PINS)
1179 pin = pinToGpio [pin] ;
1180 else if (wiringPiMode == WPI_MODE_PHYS)
1181 pin = physToGpio [pin] ;
1182 else if (wiringPiMode != WPI_MODE_GPIO)
1185 if ((*(gpio + gpioToGPLEV [pin]) & (1 << (pin & 31))) != 0)
1192 if ((node = wiringPiFindNode (pin)) == NULL)
1194 return node->digitalRead (node, pin) ;
1202 *********************************************************************************
1205 void digitalWrite (int pin, int value)
1207 struct wiringPiNodeStruct *node = wiringPiNodes ;
1209 if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
1211 /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS) // Sys mode
1213 if (sysFds [pin] != -1)
1216 write (sysFds [pin], "0\n", 2) ;
1218 write (sysFds [pin], "1\n", 2) ;
1222 else if (wiringPiMode == WPI_MODE_PINS)
1223 pin = pinToGpio [pin] ;
1224 else if (wiringPiMode == WPI_MODE_PHYS)
1225 pin = physToGpio [pin] ;
1226 else if (wiringPiMode != WPI_MODE_GPIO)
1230 *(gpio + gpioToGPCLR [pin]) = 1 << (pin & 31) ;
1232 *(gpio + gpioToGPSET [pin]) = 1 << (pin & 31) ;
1236 if ((node = wiringPiFindNode (pin)) != NULL)
1237 node->digitalWrite (node, pin, value) ;
1244 * Set an output PWM value
1245 *********************************************************************************
1248 void pwmWrite (int pin, int value)
1250 struct wiringPiNodeStruct *node = wiringPiNodes ;
1252 if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
1254 /**/ if (wiringPiMode == WPI_MODE_PINS)
1255 pin = pinToGpio [pin] ;
1256 else if (wiringPiMode == WPI_MODE_PHYS)
1257 pin = physToGpio [pin] ;
1258 else if (wiringPiMode != WPI_MODE_GPIO)
1261 *(pwm + gpioToPwmPort [pin]) = value ;
1265 if ((node = wiringPiFindNode (pin)) != NULL)
1266 node->pwmWrite (node, pin, value) ;
1273 * Read the analog value of a given Pin.
1274 * There is no on-board Pi analog hardware,
1275 * so this needs to go to a new node.
1276 *********************************************************************************
1279 int analogRead (int pin)
1281 struct wiringPiNodeStruct *node = wiringPiNodes ;
1283 if ((node = wiringPiFindNode (pin)) == NULL)
1286 return node->analogRead (node, pin) ;
1292 * Write the analog value to the given Pin.
1293 * There is no on-board Pi analog hardware,
1294 * so this needs to go to a new node.
1295 *********************************************************************************
1298 void analogWrite (int pin, int value)
1300 struct wiringPiNodeStruct *node = wiringPiNodes ;
1302 if ((node = wiringPiFindNode (pin)) == NULL)
1305 node->analogWrite (node, pin, value) ;
1313 * Write an 8-bit byte to the first 8 GPIO pins - try to do it as
1315 * However it still needs 2 operations to set the bits, so any external
1316 * hardware must not rely on seeing a change as there will be a change
1317 * to set the outputs bits to zero, then another change to set the 1's
1318 *********************************************************************************
1321 void digitalWriteByte (int value)
1323 uint32_t pinSet = 0 ;
1324 uint32_t pinClr = 0 ;
1328 /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS)
1330 for (pin = 0 ; pin < 8 ; ++pin)
1332 digitalWrite (pin, value & mask) ;
1339 for (pin = 0 ; pin < 8 ; ++pin)
1341 if ((value & mask) == 0)
1342 pinClr |= (1 << pinToGpio [pin]) ;
1344 pinSet |= (1 << pinToGpio [pin]) ;
1349 *(gpio + gpioToGPCLR [0]) = pinClr ;
1350 *(gpio + gpioToGPSET [0]) = pinSet ;
1358 * Wait for Interrupt on a GPIO pin.
1359 * This is actually done via the /sys/class/gpio interface regardless of
1360 * the wiringPi access mode in-use. Maybe sometime it might get a better
1361 * way for a bit more efficiency.
1362 *********************************************************************************
1365 int waitForInterrupt (int pin, int mS)
1369 struct pollfd polls ;
1371 /**/ if (wiringPiMode == WPI_MODE_PINS)
1372 pin = pinToGpio [pin] ;
1373 else if (wiringPiMode == WPI_MODE_PHYS)
1374 pin = physToGpio [pin] ;
1376 if ((fd = sysFds [pin]) == -1)
1379 // Setup poll structure
1382 polls.events = POLLPRI ; // Urgent data!
1386 x = poll (&polls, 1, mS) ;
1388 // Do a dummy read to clear the interrupt
1389 // A one character read appars to be enough.
1391 (void)read (fd, &c, 1) ;
1399 * This is a thread and gets started to wait for the interrupt we're
1400 * hoping to catch. It will call the user-function when the interrupt
1402 *********************************************************************************
1405 static void *interruptHandler (void *arg)
1409 (void)piHiPri (55) ; // Only effective if we run as root
1415 if (waitForInterrupt (myPin, -1) > 0)
1416 isrFunctions [myPin] () ;
1425 * Take the details and create an interrupt handler that will do a call-
1426 * back to the user supplied function.
1427 *********************************************************************************
1430 int wiringPiISR (int pin, int mode, void (*function)(void))
1432 pthread_t threadId ;
1441 if ((pin < 0) || (pin > 63))
1442 return wiringPiFailure (WPI_FATAL, "wiringPiISR: pin must be 0-63 (%d)\n", pin) ;
1444 /**/ if (wiringPiMode == WPI_MODE_UNINITIALISED)
1445 return wiringPiFailure (WPI_FATAL, "wiringPiISR: wiringPi has not been initialised. Unable to continue.\n") ;
1446 else if (wiringPiMode == WPI_MODE_PINS)
1447 bcmGpioPin = pinToGpio [pin] ;
1448 else if (wiringPiMode == WPI_MODE_PHYS)
1449 bcmGpioPin = physToGpio [pin] ;
1453 // Now export the pin and set the right edge
1454 // We're going to use the gpio program to do this, so it assumes
1455 // a full installation of wiringPi. It's a bit 'clunky', but it
1456 // is a way that will work when we're running in "Sys" mode, as
1457 // a non-root user. (without sudo)
1459 if (mode != INT_EDGE_SETUP)
1461 /**/ if (mode == INT_EDGE_FALLING)
1463 else if (mode == INT_EDGE_RISING)
1468 sprintf (pinS, "%d", bcmGpioPin) ;
1470 if ((pid = fork ()) < 0) // Fail
1471 return wiringPiFailure (WPI_FATAL, "wiringPiISR: fork failed: %s\n", strerror (errno)) ;
1473 if (pid == 0) // Child, exec
1475 /**/ if (access ("/usr/local/bin/gpio", X_OK) == 0)
1477 execl ("/usr/local/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ;
1478 return wiringPiFailure (WPI_FATAL, "wiringPiISR: execl failed: %s\n", strerror (errno)) ;
1480 else if (access ("/usr/bin/gpio", X_OK) == 0)
1482 execl ("/usr/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ;
1483 return wiringPiFailure (WPI_FATAL, "wiringPiISR: execl failed: %s\n", strerror (errno)) ;
1486 return wiringPiFailure (WPI_FATAL, "wiringPiISR: Can't find gpio program\n") ;
1488 else // Parent, wait
1492 // Now pre-open the /sys/class node - but it may already be open if
1493 // we are in Sys mode...
1495 if (sysFds [bcmGpioPin] == -1)
1497 sprintf (fName, "/sys/class/gpio/gpio%d/value", bcmGpioPin) ;
1498 if ((sysFds [bcmGpioPin] = open (fName, O_RDWR)) < 0)
1499 return wiringPiFailure (WPI_FATAL, "wiringPiISR: unable to open %s: %s\n", fName, strerror (errno)) ;
1502 // Clear any initial pending interrupt
1504 ioctl (sysFds [bcmGpioPin], FIONREAD, &count) ;
1505 for (i = 0 ; i < count ; ++i)
1506 read (sysFds [bcmGpioPin], &c, 1) ;
1508 isrFunctions [pin] = function ;
1510 pthread_mutex_lock (&pinMutex) ;
1512 pthread_create (&threadId, NULL, interruptHandler, NULL) ;
1513 while (pinPass != -1)
1515 pthread_mutex_unlock (&pinMutex) ;
1523 * Initialise our start-of-time variable to be the current unix
1524 * time in milliseconds and microseconds.
1525 *********************************************************************************
1528 static void initialiseEpoch (void)
1532 gettimeofday (&tv, NULL) ;
1533 epochMilli = (uint64_t)tv.tv_sec * (uint64_t)1000 + (uint64_t)(tv.tv_usec / 1000) ;
1534 epochMicro = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)(tv.tv_usec) ;
1540 * Wait for some number of milliseconds
1541 *********************************************************************************
1544 void delay (unsigned int howLong)
1546 struct timespec sleeper, dummy ;
1548 sleeper.tv_sec = (time_t)(howLong / 1000) ;
1549 sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ;
1551 nanosleep (&sleeper, &dummy) ;
1556 * delayMicroseconds:
1557 * This is somewhat intersting. It seems that on the Pi, a single call
1558 * to nanosleep takes some 80 to 130 microseconds anyway, so while
1559 * obeying the standards (may take longer), it's not always what we
1562 * So what I'll do now is if the delay is less than 100uS we'll do it
1563 * in a hard loop, watching a built-in counter on the ARM chip. This is
1564 * somewhat sub-optimal in that it uses 100% CPU, something not an issue
1565 * in a microcontroller, but under a multi-tasking, multi-user OS, it's
1566 * wastefull, however we've no real choice )-:
1568 * Plan B: It seems all might not be well with that plan, so changing it
1569 * to use gettimeofday () and poll on that instead...
1570 *********************************************************************************
1573 void delayMicrosecondsHard (unsigned int howLong)
1575 struct timeval tNow, tLong, tEnd ;
1577 gettimeofday (&tNow, NULL) ;
1578 tLong.tv_sec = howLong / 1000000 ;
1579 tLong.tv_usec = howLong % 1000000 ;
1580 timeradd (&tNow, &tLong, &tEnd) ;
1582 while (timercmp (&tNow, &tEnd, <))
1583 gettimeofday (&tNow, NULL) ;
1586 void delayMicroseconds (unsigned int howLong)
1588 struct timespec sleeper ;
1589 unsigned int uSecs = howLong % 1000000 ;
1590 unsigned int wSecs = howLong / 1000000 ;
1592 /**/ if (howLong == 0)
1594 else if (howLong < 100)
1595 delayMicrosecondsHard (howLong) ;
1598 sleeper.tv_sec = wSecs ;
1599 sleeper.tv_nsec = (long)(uSecs * 1000L) ;
1600 nanosleep (&sleeper, NULL) ;
1607 * Return a number of milliseconds as an unsigned int.
1608 *********************************************************************************
1611 unsigned int millis (void)
1616 gettimeofday (&tv, NULL) ;
1617 now = (uint64_t)tv.tv_sec * (uint64_t)1000 + (uint64_t)(tv.tv_usec / 1000) ;
1619 return (uint32_t)(now - epochMilli) ;
1625 * Return a number of microseconds as an unsigned int.
1626 *********************************************************************************
1629 unsigned int micros (void)
1634 gettimeofday (&tv, NULL) ;
1635 now = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)tv.tv_usec ;
1637 return (uint32_t)(now - epochMicro) ;
1643 * Must be called once at the start of your program execution.
1645 * Default setup: Initialises the system into wiringPi Pin mode and uses the
1646 * memory mapped hardware directly.
1648 * Changed now to revert to "gpio" mode if we're running on a Compute Module.
1649 *********************************************************************************
1652 int wiringPiSetup (void)
1656 int model, rev, mem ;
1659 if (getenv (ENV_DEBUG) != NULL)
1660 wiringPiDebug = TRUE ;
1662 if (getenv (ENV_CODES) != NULL)
1663 wiringPiReturnCodes = TRUE ;
1665 if (geteuid () != 0)
1666 (void)wiringPiFailure (WPI_FATAL, "wiringPiSetup: Must be root. (Did you forget sudo?)\n") ;
1669 printf ("wiringPi: wiringPiSetup called\n") ;
1671 boardRev = piBoardRev () ;
1675 pinToGpio = pinToGpioR1 ;
1676 physToGpio = physToGpioR1 ;
1680 pinToGpio = pinToGpioR2 ;
1681 physToGpio = physToGpioR2 ;
1684 // Open the master /dev/memory device
1686 if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0)
1687 return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: Unable to open /dev/mem: %s\n", strerror (errno)) ;
1691 gpio = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_BASE) ;
1692 if ((int32_t)gpio == -1)
1693 return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (GPIO) failed: %s\n", strerror (errno)) ;
1697 pwm = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PWM) ;
1698 if ((int32_t)pwm == -1)
1699 return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PWM) failed: %s\n", strerror (errno)) ;
1701 // Clock control (needed for PWM)
1703 clk = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, CLOCK_BASE) ;
1704 if ((int32_t)clk == -1)
1705 return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (CLOCK) failed: %s\n", strerror (errno)) ;
1709 pads = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PADS) ;
1710 if ((int32_t)pads == -1)
1711 return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PADS) failed: %s\n", strerror (errno)) ;
1716 timer = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_TIMER) ;
1717 if ((int32_t)timer == -1)
1718 return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (TIMER) failed: %s\n", strerror (errno)) ;
1720 // Set the timer to free-running, 1MHz.
1721 // 0xF9 is 249, the timer divide is base clock / (divide+1)
1722 // so base clock is 250MHz / 250 = 1MHz.
1724 *(timer + TIMER_CONTROL) = 0x0000280 ;
1725 *(timer + TIMER_PRE_DIV) = 0x00000F9 ;
1726 timerIrqRaw = timer + TIMER_IRQ_RAW ;
1729 initialiseEpoch () ;
1731 // If we're running on a compute module, then wiringPi pin numbers don't really many anything...
1733 piBoardId (&model, &rev, &mem, &maker) ;
1734 if (model == PI_MODEL_CM)
1735 wiringPiMode = WPI_MODE_GPIO ;
1737 wiringPiMode = WPI_MODE_PINS ;
1744 * wiringPiSetupGpio:
1745 * Must be called once at the start of your program execution.
1747 * GPIO setup: Initialises the system into GPIO Pin mode and uses the
1748 * memory mapped hardware directly.
1749 *********************************************************************************
1752 int wiringPiSetupGpio (void)
1754 (void)wiringPiSetup () ;
1757 printf ("wiringPi: wiringPiSetupGpio called\n") ;
1759 wiringPiMode = WPI_MODE_GPIO ;
1766 * wiringPiSetupPhys:
1767 * Must be called once at the start of your program execution.
1769 * Phys setup: Initialises the system into Physical Pin mode and uses the
1770 * memory mapped hardware directly.
1771 *********************************************************************************
1774 int wiringPiSetupPhys (void)
1776 (void)wiringPiSetup () ;
1779 printf ("wiringPi: wiringPiSetupPhys called\n") ;
1781 wiringPiMode = WPI_MODE_PHYS ;
1789 * Must be called once at the start of your program execution.
1791 * Initialisation (again), however this time we are using the /sys/class/gpio
1792 * interface to the GPIO systems - slightly slower, but always usable as
1793 * a non-root user, assuming the devices are already exported and setup correctly.
1796 int wiringPiSetupSys (void)
1802 if (getenv (ENV_DEBUG) != NULL)
1803 wiringPiDebug = TRUE ;
1805 if (getenv (ENV_CODES) != NULL)
1806 wiringPiReturnCodes = TRUE ;
1809 printf ("wiringPi: wiringPiSetupSys called\n") ;
1811 boardRev = piBoardRev () ;
1815 pinToGpio = pinToGpioR1 ;
1816 physToGpio = physToGpioR1 ;
1820 pinToGpio = pinToGpioR2 ;
1821 physToGpio = physToGpioR2 ;
1824 // Open and scan the directory, looking for exported GPIOs, and pre-open
1825 // the 'value' interface to speed things up for later
1827 for (pin = 0 ; pin < 64 ; ++pin)
1829 sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
1830 sysFds [pin] = open (fName, O_RDWR) ;
1833 initialiseEpoch () ;
1835 wiringPiMode = WPI_MODE_GPIO_SYS ;