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>
80 // Environment Variables
82 #define ENV_DEBUG "WIRINGPI_DEBUG"
83 #define ENV_CODES "WIRINGPI_CODES"
86 // Mask for the bottom 64 pins which belong to the Raspberry Pi
87 // The others are available for the other devices
89 #define PI_GPIO_MASK (0xFFFFFFC0)
91 static struct wiringPiNodeStruct *wiringPiNodes = NULL ;
95 #define BCM_PASSWORD 0x5A000000
98 // The BCM2835 has 54 GPIO pins.
99 // BCM2835 data sheet, Page 90 onwards.
100 // There are 6 control registers, each control the functions of a block
102 // Each control register has 10 sets of 3 bits per GPIO pin - the ALT values
104 // 000 = GPIO Pin X is an input
105 // 001 = GPIO Pin X is an output
106 // 100 = GPIO Pin X takes alternate function 0
107 // 101 = GPIO Pin X takes alternate function 1
108 // 110 = GPIO Pin X takes alternate function 2
109 // 111 = GPIO Pin X takes alternate function 3
110 // 011 = GPIO Pin X takes alternate function 4
111 // 010 = GPIO Pin X takes alternate function 5
113 // So the 3 bits for port X are:
114 // X / 10 + ((X % 10) * 3)
116 // Port function select bits
118 #define FSEL_INPT 0b000
119 #define FSEL_OUTP 0b001
120 #define FSEL_ALT0 0b100
121 #define FSEL_ALT0 0b100
122 #define FSEL_ALT1 0b101
123 #define FSEL_ALT2 0b110
124 #define FSEL_ALT3 0b111
125 #define FSEL_ALT4 0b011
126 #define FSEL_ALT5 0b010
128 // Access from ARM Running Linux
129 // Taken from Gert/Doms code. Some of this is not in the manual
130 // that I can find )-:
132 #define BCM2708_PERI_BASE 0x20000000
133 #define GPIO_PADS (BCM2708_PERI_BASE + 0x00100000)
134 #define CLOCK_BASE (BCM2708_PERI_BASE + 0x00101000)
135 #define GPIO_BASE (BCM2708_PERI_BASE + 0x00200000)
136 #define GPIO_TIMER (BCM2708_PERI_BASE + 0x0000B000)
137 #define GPIO_PWM (BCM2708_PERI_BASE + 0x0020C000)
139 #define PAGE_SIZE (4*1024)
140 #define BLOCK_SIZE (4*1024)
143 // Word offsets into the PWM control region
145 #define PWM_CONTROL 0
152 // Clock regsiter offsets
154 #define PWMCLK_CNTL 40
155 #define PWMCLK_DIV 41
157 #define PWM0_MS_MODE 0x0080 // Run in MS mode
158 #define PWM0_USEFIFO 0x0020 // Data from FIFO
159 #define PWM0_REVPOLAR 0x0010 // Reverse polarity
160 #define PWM0_OFFSTATE 0x0008 // Ouput Off state
161 #define PWM0_REPEATFF 0x0004 // Repeat last value if FIFO empty
162 #define PWM0_SERIAL 0x0002 // Run in serial mode
163 #define PWM0_ENABLE 0x0001 // Channel Enable
165 #define PWM1_MS_MODE 0x8000 // Run in MS mode
166 #define PWM1_USEFIFO 0x2000 // Data from FIFO
167 #define PWM1_REVPOLAR 0x1000 // Reverse polarity
168 #define PWM1_OFFSTATE 0x0800 // Ouput Off state
169 #define PWM1_REPEATFF 0x0400 // Repeat last value if FIFO empty
170 #define PWM1_SERIAL 0x0200 // Run in serial mode
171 #define PWM1_ENABLE 0x0100 // Channel Enable
176 #define TIMER_LOAD (0x400 >> 2)
177 #define TIMER_VALUE (0x404 >> 2)
178 #define TIMER_CONTROL (0x408 >> 2)
179 #define TIMER_IRQ_CLR (0x40C >> 2)
180 #define TIMER_IRQ_RAW (0x410 >> 2)
181 #define TIMER_IRQ_MASK (0x414 >> 2)
182 #define TIMER_RELOAD (0x418 >> 2)
183 #define TIMER_PRE_DIV (0x41C >> 2)
184 #define TIMER_COUNTER (0x420 >> 2)
186 // Locals to hold pointers to the hardware
188 static volatile uint32_t *gpio ;
189 static volatile uint32_t *pwm ;
190 static volatile uint32_t *clk ;
191 static volatile uint32_t *pads ;
194 static volatile uint32_t *timer ;
195 static volatile uint32_t *timerIrqRaw ;
198 // Time for easy calculations
200 static uint64_t epochMilli, epochMicro ;
204 static int wiringPiMode = WPI_MODE_UNINITIALISED ;
205 static volatile int pinPass = -1 ;
206 static pthread_mutex_t pinMutex ;
208 // Debugging & Return codes
210 int wiringPiDebug = FALSE ;
211 int wiringPiReturnCodes = FALSE ;
214 // Map a file descriptor from the /sys/class/gpio/gpioX/value
216 static int sysFds [64] ;
220 static void (*isrFunctions [64])(void) ;
223 // Doing it the Arduino way with lookup tables...
224 // Yes, it's probably more innefficient than all the bit-twidling, but it
225 // does tend to make it all a bit clearer. At least to me!
228 // Take a Wiring pin (0 through X) and re-map it to the BCM_GPIO pin
229 // Cope for 2 different board revisions here.
231 static int *pinToGpio ;
233 static int pinToGpioR1 [64] =
235 17, 18, 21, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7: wpi 0 - 7
236 0, 1, // I2C - SDA0, SCL0 wpi 8 - 9
237 8, 7, // SPI - CE1, CE0 wpi 10 - 11
238 10, 9, 11, // SPI - MOSI, MISO, SCLK wpi 12 - 14
239 14, 15, // UART - Tx, Rx wpi 15 - 16
243 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 31
244 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
245 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
248 static int pinToGpioR2 [64] =
250 17, 18, 27, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7: wpi 0 - 7
251 2, 3, // I2C - SDA0, SCL0 wpi 8 - 9
252 8, 7, // SPI - CE1, CE0 wpi 10 - 11
253 10, 9, 11, // SPI - MOSI, MISO, SCLK wpi 12 - 14
254 14, 15, // UART - Tx, Rx wpi 15 - 16
255 28, 29, 30, 31, // New GPIOs 8 though 11 wpi 17 - 20
259 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 31
260 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
261 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
266 // Take a physical pin (1 through 26) and re-map it to the BCM_GPIO pin
267 // Cope for 2 different board revisions here.
269 static int *physToGpio ;
271 static int physToGpioR1 [64] =
290 -1, -1, -1, -1, -1, // ... 31
291 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
292 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
295 static int physToGpioR2 [64] =
314 -1, -1, -1, -1, -1, // ... 31
315 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
316 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
321 // Map a BCM_GPIO pin to it's Function Selection
322 // control port. (GPFSEL 0-5)
323 // Groups of 10 - 3 bits per Function - 30 bits per port
325 static uint8_t gpioToGPFSEL [] =
327 0,0,0,0,0,0,0,0,0,0,
328 1,1,1,1,1,1,1,1,1,1,
329 2,2,2,2,2,2,2,2,2,2,
330 3,3,3,3,3,3,3,3,3,3,
331 4,4,4,4,4,4,4,4,4,4,
332 5,5,5,5,5,5,5,5,5,5,
337 // Define the shift up for the 3 bits per pin in each GPFSEL port
339 static uint8_t gpioToShift [] =
341 0,3,6,9,12,15,18,21,24,27,
342 0,3,6,9,12,15,18,21,24,27,
343 0,3,6,9,12,15,18,21,24,27,
344 0,3,6,9,12,15,18,21,24,27,
345 0,3,6,9,12,15,18,21,24,27,
350 // (Word) offset to the GPIO Set registers for each GPIO pin
352 static uint8_t gpioToGPSET [] =
354 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,
355 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,
359 // (Word) offset to the GPIO Clear registers for each GPIO pin
361 static uint8_t gpioToGPCLR [] =
363 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,
364 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,
369 // (Word) offset to the GPIO Input level registers for each GPIO pin
371 static uint8_t gpioToGPLEV [] =
373 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,
374 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,
380 // (Word) offset to the Event Detect Status
382 static uint8_t gpioToEDS [] =
384 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,
385 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,
389 // (Word) offset to the Rising edgde ENable register
391 static uint8_t gpioToREN [] =
393 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,
394 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,
398 // (Word) offset to the Falling edgde ENable register
400 static uint8_t gpioToFEN [] =
402 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,
403 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,
409 // GPIO Pin pull up/down register
414 // (Word) offset to the Pull Up Down Clock regsiter
416 static uint8_t gpioToPUDCLK [] =
418 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,
419 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,
424 // the ALT value to put a GPIO pin into PWM mode
426 static uint8_t gpioToPwmALT [] =
428 0, 0, 0, 0, 0, 0, 0, 0, // 0 -> 7
429 0, 0, 0, 0, FSEL_ALT0, FSEL_ALT0, 0, 0, // 8 -> 15
430 0, 0, FSEL_ALT5, FSEL_ALT5, 0, 0, 0, 0, // 16 -> 23
431 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
432 0, 0, 0, 0, 0, 0, 0, 0, // 32 -> 39
433 FSEL_ALT0, FSEL_ALT0, 0, 0, 0, FSEL_ALT0, 0, 0, // 40 -> 47
434 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
435 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
440 // The port value to put a GPIO pin into PWM mode
442 static uint8_t gpioToPwmPort [] =
444 0, 0, 0, 0, 0, 0, 0, 0, // 0 -> 7
445 0, 0, 0, 0, PWM0_DATA, PWM1_DATA, 0, 0, // 8 -> 15
446 0, 0, PWM0_DATA, PWM1_DATA, 0, 0, 0, 0, // 16 -> 23
447 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
448 0, 0, 0, 0, 0, 0, 0, 0, // 32 -> 39
449 PWM0_DATA, PWM1_DATA, 0, 0, 0, PWM1_DATA, 0, 0, // 40 -> 47
450 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
451 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
456 // ALT value to put a GPIO pin into GP Clock mode.
457 // On the Pi we can really only use BCM_GPIO_4 and BCM_GPIO_21
458 // for clocks 0 and 1 respectively, however I'll include the full
459 // list for completeness - maybe one day...
461 #define GPIO_CLOCK_SOURCE 1
465 static uint8_t gpioToGpClkALT0 [] =
467 0, 0, 0, 0, FSEL_ALT0, FSEL_ALT0, FSEL_ALT0, 0, // 0 -> 7
468 0, 0, 0, 0, 0, 0, 0, 0, // 8 -> 15
469 0, 0, 0, 0, FSEL_ALT5, FSEL_ALT5, 0, 0, // 16 -> 23
470 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
471 FSEL_ALT0, 0, FSEL_ALT0, 0, 0, 0, 0, 0, // 32 -> 39
472 0, 0, FSEL_ALT0, FSEL_ALT0, FSEL_ALT0, 0, 0, 0, // 40 -> 47
473 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
474 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
478 // (word) Offsets to the clock Control and Divisor register
480 static uint8_t gpioToClkCon [] =
482 -1, -1, -1, -1, 28, 30, 32, -1, // 0 -> 7
483 -1, -1, -1, -1, -1, -1, -1, -1, // 8 -> 15
484 -1, -1, -1, -1, 28, 30, -1, -1, // 16 -> 23
485 -1, -1, -1, -1, -1, -1, -1, -1, // 24 -> 31
486 28, -1, 28, -1, -1, -1, -1, -1, // 32 -> 39
487 -1, -1, 28, 30, 28, -1, -1, -1, // 40 -> 47
488 -1, -1, -1, -1, -1, -1, -1, -1, // 48 -> 55
489 -1, -1, -1, -1, -1, -1, -1, -1, // 56 -> 63
492 static uint8_t gpioToClkDiv [] =
494 -1, -1, -1, -1, 29, 31, 33, -1, // 0 -> 7
495 -1, -1, -1, -1, -1, -1, -1, -1, // 8 -> 15
496 -1, -1, -1, -1, 29, 31, -1, -1, // 16 -> 23
497 -1, -1, -1, -1, -1, -1, -1, -1, // 24 -> 31
498 29, -1, 29, -1, -1, -1, -1, -1, // 32 -> 39
499 -1, -1, 29, 31, 29, -1, -1, -1, // 40 -> 47
500 -1, -1, -1, -1, -1, -1, -1, -1, // 48 -> 55
501 -1, -1, -1, -1, -1, -1, -1, -1, // 56 -> 63
507 *********************************************************************************
514 *********************************************************************************
517 int wiringPiFailure (int fatal, const char *message, ...)
522 if (!fatal && wiringPiReturnCodes)
525 va_start (argp, message) ;
526 vsnprintf (buffer, 1023, message, argp) ;
529 fprintf (stderr, "%s", buffer) ;
530 exit (EXIT_FAILURE) ;
538 * Return a number representing the hardware revision of the board.
539 * Revision is currently 1 or 2.
541 * Much confusion here )-:
542 * Seems there are some boards with 0000 in them (mistake in manufacture)
543 * and some board with 0005 in them (another mistake in manufacture?)
544 * So the distinction between boards that I can see is:
549 * 0004 - Rev 2 (Early reports?
550 * 0005 - Rev 2 (but error?)
552 * 0008 - Rev 2 - Model A
553 * 000e - Rev 2 + 512MB
554 * 000f - Rev 2 + 512MB
556 * A small thorn is the olde style overvolting - that will add in
559 *********************************************************************************
562 static void piBoardRevOops (const char *why)
564 fprintf (stderr, "piBoardRev: Unable to determine board revision from /proc/cpuinfo\n") ;
565 fprintf (stderr, " -> %s\n", why) ;
566 fprintf (stderr, " -> You may want to check:\n") ;
567 fprintf (stderr, " -> http://www.raspberrypi.org/phpBB3/viewtopic.php?p=184410#p184410\n") ;
568 exit (EXIT_FAILURE) ;
571 int piBoardRev (void)
576 static int boardRev = -1 ;
578 if (boardRev != -1) // No point checking twice
581 if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL)
582 piBoardRevOops ("Unable to open /proc/cpuinfo") ;
584 while (fgets (line, 120, cpuFd) != NULL)
585 if (strncmp (line, "Revision", 8) == 0)
590 if (strncmp (line, "Revision", 8) != 0)
591 piBoardRevOops ("No \"Revision\" line") ;
593 for (c = &line [strlen (line) - 1] ; (*c == '\n') || (*c == '\r') ; --c)
597 printf ("piboardRev: Revision string: %s\n", line) ;
599 for (c = line ; *c ; ++c)
604 piBoardRevOops ("No numeric revision string") ;
606 // If you have overvolted the Pi, then it appears that the revision
607 // has 100000 added to it!
611 printf ("piboardRev: This Pi has/is overvolted!\n") ;
613 lastChar = line [strlen (line) - 1] ;
616 printf ("piboardRev: lastChar is: '%c' (%d, 0x%02X)\n", lastChar, lastChar, lastChar) ;
618 /**/ if ((lastChar == '2') || (lastChar == '3'))
624 printf ("piBoardRev: Returning revision: %d\n", boardRev) ;
632 * Translate a wiringPi Pin number to native GPIO pin number.
633 * Provided for external support.
634 *********************************************************************************
637 int wpiPinToGpio (int wpiPin)
639 return pinToGpio [wpiPin & 63] ;
645 * Translate a physical Pin number to native GPIO pin number.
646 * Provided for external support.
647 *********************************************************************************
650 int physPinToGpio (int physPin)
652 return physToGpio [physPin & 63] ;
658 * Set the PAD driver value
659 *********************************************************************************
662 void setPadDrive (int group, int value)
666 if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
668 if ((group < 0) || (group > 2))
671 wrVal = BCM_PASSWORD | 0x18 | (value & 7) ;
672 *(pads + group + 11) = wrVal ;
676 printf ("setPadDrive: Group: %d, value: %d (%08X)\n", group, value, wrVal) ;
677 printf ("Read : %08X\n", *(pads + group + 11)) ;
685 * Returns the ALT bits for a given port. Only really of-use
686 * for the gpio readall command (I think)
687 *********************************************************************************
692 int fSel, shift, alt ;
696 /**/ if (wiringPiMode == WPI_MODE_PINS)
697 pin = pinToGpio [pin] ;
698 else if (wiringPiMode == WPI_MODE_PHYS)
699 pin = physToGpio [pin] ;
700 else if (wiringPiMode != WPI_MODE_GPIO)
703 fSel = gpioToGPFSEL [pin] ;
704 shift = gpioToShift [pin] ;
706 alt = (*(gpio + fSel) >> shift) & 7 ;
714 * Select the native "balanced" mode, or standard mark:space mode
715 *********************************************************************************
718 void pwmSetMode (int mode)
720 if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
722 if (mode == PWM_MODE_MS)
723 *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE | PWM0_MS_MODE | PWM1_MS_MODE ;
725 *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ;
732 * Set the PWM range register. We set both range registers to the same
733 * value. If you want different in your own code, then write your own.
734 *********************************************************************************
737 void pwmSetRange (unsigned int range)
739 if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
741 *(pwm + PWM0_RANGE) = range ; delayMicroseconds (10) ;
742 *(pwm + PWM1_RANGE) = range ; delayMicroseconds (10) ;
749 * Set/Change the PWM clock. Originally my code, but changed
750 * (for the better!) by Chris Hall, <chris@kchall.plus.com>
751 * after further study of the manual and testing with a 'scope
752 *********************************************************************************
755 void pwmSetClock (int divisor)
757 uint32_t pwm_control ;
760 if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
763 printf ("Setting to: %d. Current: 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;
765 pwm_control = *(pwm + PWM_CONTROL) ; // preserve PWM_CONTROL
767 // We need to stop PWM prior to stopping PWM clock in MS mode otherwise BUSY
770 *(pwm + PWM_CONTROL) = 0 ; // Stop PWM
772 // Stop PWM clock before changing divisor. The delay after this does need to
773 // this big (95uS occasionally fails, 100uS OK), it's almost as though the BUSY
774 // flag is not working properly in balanced mode. Without the delay when DIV is
775 // adjusted the clock sometimes switches to very slow, once slow further DIV
776 // adjustments do nothing and it's difficult to get out of this mode.
778 *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x01 ; // Stop PWM Clock
779 delayMicroseconds (110) ; // prevents clock going sloooow
781 while ((*(clk + PWMCLK_CNTL) & 0x80) != 0) // Wait for clock to be !BUSY
782 delayMicroseconds (1) ;
784 *(clk + PWMCLK_DIV) = BCM_PASSWORD | (divisor << 12) ;
786 *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x11 ; // Start PWM clock
787 *(pwm + PWM_CONTROL) = pwm_control ; // restore PWM_CONTROL
790 printf ("Set to: %d. Now : 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;
797 * Set the freuency on a GPIO clock pin
798 *********************************************************************************
801 void gpioClockSet (int pin, int freq)
803 int divi, divr, divf ;
807 /**/ if (wiringPiMode == WPI_MODE_PINS)
808 pin = pinToGpio [pin] ;
809 else if (wiringPiMode == WPI_MODE_PHYS)
810 pin = physToGpio [pin] ;
811 else if (wiringPiMode != WPI_MODE_GPIO)
814 divi = 19200000 / freq ;
815 divr = 19200000 % freq ;
816 divf = (int)((double)divr * 4096.0 / 19200000.0) ;
821 *(clk + gpioToClkCon [pin]) = BCM_PASSWORD | GPIO_CLOCK_SOURCE ; // Stop GPIO Clock
822 while ((*(clk + gpioToClkCon [pin]) & 0x80) != 0) // ... and wait
825 *(clk + gpioToClkDiv [pin]) = BCM_PASSWORD | (divi << 12) | divf ; // Set dividers
826 *(clk + gpioToClkCon [pin]) = BCM_PASSWORD | 0x10 | GPIO_CLOCK_SOURCE ; // Start Clock
832 * Locate our device node
833 *********************************************************************************
836 static struct wiringPiNodeStruct *wiringPiFindNode (int pin)
838 struct wiringPiNodeStruct *node = wiringPiNodes ;
841 if ((pin >= node->pinBase) && (pin <= node->pinMax))
852 * Create a new GPIO node into the wiringPi handling system
853 *********************************************************************************
856 static void pinModeDummy (struct wiringPiNodeStruct *node, int pin, int mode) { return ; }
857 static void pullUpDnControlDummy (struct wiringPiNodeStruct *node, int pin, int pud) { return ; }
858 static int digitalReadDummy (struct wiringPiNodeStruct *node, int pin) { return LOW ; }
859 static void digitalWriteDummy (struct wiringPiNodeStruct *node, int pin, int value) { return ; }
860 static void pwmWriteDummy (struct wiringPiNodeStruct *node, int pin, int value) { return ; }
861 static int analogReadDummy (struct wiringPiNodeStruct *node, int pin) { return 0 ; }
862 static void analogWriteDummy (struct wiringPiNodeStruct *node, int pin, int value) { return ; }
864 struct wiringPiNodeStruct *wiringPiNewNode (int pinBase, int numPins)
867 struct wiringPiNodeStruct *node ;
869 // Minimum pin base is 64
872 (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: pinBase of %d is < 64\n", pinBase) ;
874 // Check all pins in-case there is overlap:
876 for (pin = pinBase ; pin < (pinBase + numPins) ; ++pin)
877 if (wiringPiFindNode (pin) != NULL)
878 (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: Pin %d overlaps with existing definition\n", pin) ;
880 node = (struct wiringPiNodeStruct *)calloc (sizeof (struct wiringPiNodeStruct), 1) ; // calloc zeros
882 (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: Unable to allocate memory: %s\n", strerror (errno)) ;
884 node->pinBase = pinBase ;
885 node->pinMax = pinBase + numPins - 1 ;
886 node->pinMode = pinModeDummy ;
887 node->pullUpDnControl = pullUpDnControlDummy ;
888 node->digitalRead = digitalReadDummy ;
889 node->digitalWrite = digitalWriteDummy ;
890 node->pwmWrite = pwmWriteDummy ;
891 node->analogRead = analogReadDummy ;
892 node->analogWrite = analogWriteDummy ;
893 node->next = wiringPiNodes ;
894 wiringPiNodes = node ;
904 * Enables edge-detect mode on a pin - from a 0 to a 1 or 1 to 0
905 * Pin must already be in input mode with appropriate pull up/downs set.
906 *********************************************************************************
909 void pinEnableED01Pi (int pin)
911 pin = pinToGpio [pin & 63] ;
917 *********************************************************************************
919 *********************************************************************************
925 * Sets the mode of a pin to be input, output or PWM output
926 *********************************************************************************
929 void pinMode (int pin, int mode)
931 int fSel, shift, alt ;
932 struct wiringPiNodeStruct *node = wiringPiNodes ;
934 if ((pin & PI_GPIO_MASK) == 0) // On-board pin
936 /**/ if (wiringPiMode == WPI_MODE_PINS)
937 pin = pinToGpio [pin] ;
938 else if (wiringPiMode == WPI_MODE_PHYS)
939 pin = physToGpio [pin] ;
940 else if (wiringPiMode != WPI_MODE_GPIO)
943 fSel = gpioToGPFSEL [pin] ;
944 shift = gpioToShift [pin] ;
946 /**/ if (mode == INPUT)
947 *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) ; // Sets bits to zero = input
948 else if (mode == OUTPUT)
949 *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (1 << shift) ;
950 else if (mode == PWM_OUTPUT)
952 if ((alt = gpioToPwmALT [pin]) == 0) // Not a PWM pin
955 // Set pin to PWM mode
957 *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ;
958 delayMicroseconds (110) ; // See comments in pwmSetClockWPi
960 pwmSetMode (PWM_MODE_BAL) ; // Pi default mode
961 pwmSetRange (1024) ; // Default range of 1024
962 pwmSetClock (32) ; // 19.2 / 32 = 600KHz - Also starts the PWM
964 else if (mode == GPIO_CLOCK)
966 if ((alt = gpioToGpClkALT0 [pin]) == 0) // Not a GPIO_CLOCK pin
969 // Set pin to GPIO_CLOCK mode and set the clock frequency to 100KHz
971 *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ;
972 delayMicroseconds (110) ;
973 gpioClockSet (pin, 100000) ;
978 if ((node = wiringPiFindNode (pin)) != NULL)
979 node->pinMode (node, pin, mode) ;
987 * Control the internal pull-up/down resistors on a GPIO pin
988 * The Arduino only has pull-ups and these are enabled by writing 1
989 * to a port when in input mode - this paradigm doesn't quite apply
991 *********************************************************************************
994 void pullUpDnControl (int pin, int pud)
996 struct wiringPiNodeStruct *node = wiringPiNodes ;
998 if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
1000 /**/ if (wiringPiMode == WPI_MODE_PINS)
1001 pin = pinToGpio [pin] ;
1002 else if (wiringPiMode == WPI_MODE_PHYS)
1003 pin = physToGpio [pin] ;
1004 else if (wiringPiMode != WPI_MODE_GPIO)
1007 *(gpio + GPPUD) = pud & 3 ; delayMicroseconds (5) ;
1008 *(gpio + gpioToPUDCLK [pin]) = 1 << (pin & 31) ; delayMicroseconds (5) ;
1010 *(gpio + GPPUD) = 0 ; delayMicroseconds (5) ;
1011 *(gpio + gpioToPUDCLK [pin]) = 0 ; delayMicroseconds (5) ;
1013 else // Extension module
1015 if ((node = wiringPiFindNode (pin)) != NULL)
1016 node->pullUpDnControl (node, pin, pud) ;
1024 * Read the value of a given Pin, returning HIGH or LOW
1025 *********************************************************************************
1028 int digitalRead (int pin)
1031 struct wiringPiNodeStruct *node = wiringPiNodes ;
1033 if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
1035 /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS) // Sys mode
1037 if (sysFds [pin] == -1)
1040 lseek (sysFds [pin], 0L, SEEK_SET) ;
1041 read (sysFds [pin], &c, 1) ;
1042 return (c == '0') ? LOW : HIGH ;
1044 else if (wiringPiMode == WPI_MODE_PINS)
1045 pin = pinToGpio [pin] ;
1046 else if (wiringPiMode == WPI_MODE_PHYS)
1047 pin = physToGpio [pin] ;
1048 else if (wiringPiMode != WPI_MODE_GPIO)
1051 if ((*(gpio + gpioToGPLEV [pin]) & (1 << (pin & 31))) != 0)
1058 if ((node = wiringPiFindNode (pin)) == NULL)
1060 return node->digitalRead (node, pin) ;
1068 *********************************************************************************
1071 void digitalWrite (int pin, int value)
1073 struct wiringPiNodeStruct *node = wiringPiNodes ;
1075 if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
1077 /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS) // Sys mode
1079 if (sysFds [pin] != -1)
1082 write (sysFds [pin], "0\n", 2) ;
1084 write (sysFds [pin], "1\n", 2) ;
1088 else if (wiringPiMode == WPI_MODE_PINS)
1089 pin = pinToGpio [pin] ;
1090 else if (wiringPiMode == WPI_MODE_PHYS)
1091 pin = physToGpio [pin] ;
1092 else if (wiringPiMode != WPI_MODE_GPIO)
1096 *(gpio + gpioToGPCLR [pin]) = 1 << (pin & 31) ;
1098 *(gpio + gpioToGPSET [pin]) = 1 << (pin & 31) ;
1102 if ((node = wiringPiFindNode (pin)) != NULL)
1103 node->digitalWrite (node, pin, value) ;
1110 * Set an output PWM value
1111 *********************************************************************************
1114 void pwmWrite (int pin, int value)
1116 struct wiringPiNodeStruct *node = wiringPiNodes ;
1118 if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
1120 /**/ if (wiringPiMode == WPI_MODE_PINS)
1121 pin = pinToGpio [pin] ;
1122 else if (wiringPiMode == WPI_MODE_PHYS)
1123 pin = physToGpio [pin] ;
1124 else if (wiringPiMode != WPI_MODE_GPIO)
1127 *(pwm + gpioToPwmPort [pin]) = value ;
1131 if ((node = wiringPiFindNode (pin)) != NULL)
1132 node->pwmWrite (node, pin, value) ;
1139 * Read the analog value of a given Pin.
1140 * There is no on-board Pi analog hardware,
1141 * so this needs to go to a new node.
1142 *********************************************************************************
1145 int analogRead (int pin)
1147 struct wiringPiNodeStruct *node = wiringPiNodes ;
1149 if ((node = wiringPiFindNode (pin)) == NULL)
1152 return node->analogRead (node, pin) ;
1158 * Write the analog value to the given Pin.
1159 * There is no on-board Pi analog hardware,
1160 * so this needs to go to a new node.
1161 *********************************************************************************
1164 void analogWrite (int pin, int value)
1166 struct wiringPiNodeStruct *node = wiringPiNodes ;
1168 if ((node = wiringPiFindNode (pin)) == NULL)
1171 node->analogWrite (node, pin, value) ;
1179 * Write an 8-bit byte to the first 8 GPIO pins - try to do it as
1181 * However it still needs 2 operations to set the bits, so any external
1182 * hardware must not rely on seeing a change as there will be a change
1183 * to set the outputs bits to zero, then another change to set the 1's
1184 *********************************************************************************
1187 void digitalWriteByte (int value)
1189 uint32_t pinSet = 0 ;
1190 uint32_t pinClr = 0 ;
1194 /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS)
1196 for (pin = 0 ; pin < 8 ; ++pin)
1198 digitalWrite (pin, value & mask) ;
1205 for (pin = 0 ; pin < 8 ; ++pin)
1207 if ((value & mask) == 0)
1208 pinClr |= (1 << pinToGpio [pin]) ;
1210 pinSet |= (1 << pinToGpio [pin]) ;
1215 *(gpio + gpioToGPCLR [0]) = pinClr ;
1216 *(gpio + gpioToGPSET [0]) = pinSet ;
1224 * Wait for Interrupt on a GPIO pin.
1225 * This is actually done via the /sys/class/gpio interface regardless of
1226 * the wiringPi access mode in-use. Maybe sometime it might get a better
1227 * way for a bit more efficiency.
1228 *********************************************************************************
1231 int waitForInterrupt (int pin, int mS)
1235 struct pollfd polls ;
1237 /**/ if (wiringPiMode == WPI_MODE_PINS)
1238 pin = pinToGpio [pin] ;
1239 else if (wiringPiMode == WPI_MODE_PHYS)
1240 pin = physToGpio [pin] ;
1242 if ((fd = sysFds [pin]) == -1)
1245 // Setup poll structure
1248 polls.events = POLLPRI ; // Urgent data!
1252 x = poll (&polls, 1, mS) ;
1254 // Do a dummy read to clear the interrupt
1255 // A one character read appars to be enough.
1257 (void)read (fd, &c, 1) ;
1265 * This is a thread and gets started to wait for the interrupt we're
1266 * hoping to catch. It will call the user-function when the interrupt
1268 *********************************************************************************
1271 static void *interruptHandler (void *arg)
1275 (void)piHiPri (55) ; // Only effective if we run as root
1281 if (waitForInterrupt (myPin, -1) > 0)
1282 isrFunctions [myPin] () ;
1291 * Take the details and create an interrupt handler that will do a call-
1292 * back to the user supplied function.
1293 *********************************************************************************
1296 int wiringPiISR (int pin, int mode, void (*function)(void))
1298 pthread_t threadId ;
1309 /**/ if (wiringPiMode == WPI_MODE_UNINITIALISED)
1310 return wiringPiFailure (WPI_FATAL, "wiringPiISR: wiringPi has not been initialised. Unable to continue.\n") ;
1311 else if (wiringPiMode == WPI_MODE_PINS)
1312 bcmGpioPin = pinToGpio [pin] ;
1313 else if (wiringPiMode == WPI_MODE_PHYS)
1314 bcmGpioPin = physToGpio [pin] ;
1318 // Now export the pin and set the right edge
1319 // We're going to use the gpio program to do this, so it assumes
1320 // a full installation of wiringPi. It's a bit 'clunky', but it
1321 // is a way that will work when we're running in "Sys" mode, as
1322 // a non-root user. (without sudo)
1324 if (mode != INT_EDGE_SETUP)
1326 /**/ if (mode == INT_EDGE_FALLING)
1328 else if (mode == INT_EDGE_RISING)
1333 sprintf (pinS, "%d", bcmGpioPin) ;
1335 if ((pid = fork ()) < 0) // Fail
1338 if (pid == 0) // Child, exec
1340 execl ("/usr/local/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ;
1341 return -1 ; // Failure ...
1343 else // Parent, wait
1347 // Now pre-open the /sys/class node - it may already be open if
1348 // we are in Sys mode or if we call here twice, if-so, we'll close it.
1350 if (sysFds [bcmGpioPin] != -1)
1351 close (sysFds [bcmGpioPin]) ;
1353 sprintf (fName, "/sys/class/gpio/gpio%d/value", bcmGpioPin) ;
1354 if ((sysFds [bcmGpioPin] = open (fName, O_RDWR)) < 0)
1357 // Clear any initial pending interrupt
1359 ioctl (sysFds [bcmGpioPin], FIONREAD, &count) ;
1360 for (i = 0 ; i < count ; ++i)
1361 read (sysFds [bcmGpioPin], &c, 1) ;
1363 isrFunctions [pin] = function ;
1365 pthread_mutex_lock (&pinMutex) ;
1367 pthread_create (&threadId, NULL, interruptHandler, NULL) ;
1368 while (pinPass != -1)
1370 pthread_mutex_unlock (&pinMutex) ;
1378 * Initialise our start-of-time variable to be the current unix
1379 * time in milliseconds and microseconds.
1380 *********************************************************************************
1383 static void initialiseEpoch (void)
1387 gettimeofday (&tv, NULL) ;
1388 epochMilli = (uint64_t)tv.tv_sec * (uint64_t)1000 + (uint64_t)(tv.tv_usec / 1000) ;
1389 epochMicro = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)(tv.tv_usec) ;
1395 * Wait for some number of milliseconds
1396 *********************************************************************************
1399 void delay (unsigned int howLong)
1401 struct timespec sleeper, dummy ;
1403 sleeper.tv_sec = (time_t)(howLong / 1000) ;
1404 sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ;
1406 nanosleep (&sleeper, &dummy) ;
1411 * delayMicroseconds:
1412 * This is somewhat intersting. It seems that on the Pi, a single call
1413 * to nanosleep takes some 80 to 130 microseconds anyway, so while
1414 * obeying the standards (may take longer), it's not always what we
1417 * So what I'll do now is if the delay is less than 100uS we'll do it
1418 * in a hard loop, watching a built-in counter on the ARM chip. This is
1419 * somewhat sub-optimal in that it uses 100% CPU, something not an issue
1420 * in a microcontroller, but under a multi-tasking, multi-user OS, it's
1421 * wastefull, however we've no real choice )-:
1423 * Plan B: It seems all might not be well with that plan, so changing it
1424 * to use gettimeofday () and poll on that instead...
1425 *********************************************************************************
1428 void delayMicrosecondsHard (unsigned int howLong)
1430 struct timeval tNow, tLong, tEnd ;
1432 gettimeofday (&tNow, NULL) ;
1433 tLong.tv_sec = howLong / 1000000 ;
1434 tLong.tv_usec = howLong % 1000000 ;
1435 timeradd (&tNow, &tLong, &tEnd) ;
1437 while (timercmp (&tNow, &tEnd, <))
1438 gettimeofday (&tNow, NULL) ;
1441 void delayMicroseconds (unsigned int howLong)
1443 struct timespec sleeper ;
1445 /**/ if (howLong == 0)
1447 else if (howLong < 100)
1448 delayMicrosecondsHard (howLong) ;
1451 sleeper.tv_sec = 0 ;
1452 sleeper.tv_nsec = (long)(howLong * 1000) ;
1453 nanosleep (&sleeper, NULL) ;
1460 * Return a number of milliseconds as an unsigned int.
1461 *********************************************************************************
1464 unsigned int millis (void)
1469 gettimeofday (&tv, NULL) ;
1470 now = (uint64_t)tv.tv_sec * (uint64_t)1000 + (uint64_t)(tv.tv_usec / 1000) ;
1472 return (uint32_t)(now - epochMilli) ;
1478 * Return a number of microseconds as an unsigned int.
1479 *********************************************************************************
1482 unsigned int micros (void)
1487 gettimeofday (&tv, NULL) ;
1488 now = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)tv.tv_usec ;
1490 return (uint32_t)(now - epochMicro) ;
1496 * Must be called once at the start of your program execution.
1498 * Default setup: Initialises the system into wiringPi Pin mode and uses the
1499 * memory mapped hardware directly.
1500 *********************************************************************************
1503 int wiringPiSetup (void)
1508 if (getenv (ENV_DEBUG) != NULL)
1509 wiringPiDebug = TRUE ;
1511 if (getenv (ENV_CODES) != NULL)
1512 wiringPiReturnCodes = TRUE ;
1514 if (geteuid () != 0)
1515 (void)wiringPiFailure (WPI_FATAL, "wiringPiSetup: Must be root. (Did you forget sudo?)\n") ;
1518 printf ("wiringPi: wiringPiSetup called\n") ;
1520 boardRev = piBoardRev () ;
1524 pinToGpio = pinToGpioR1 ;
1525 physToGpio = physToGpioR1 ;
1529 pinToGpio = pinToGpioR2 ;
1530 physToGpio = physToGpioR2 ;
1533 // Open the master /dev/memory device
1535 if ((fd = open ("/dev/mem", O_RDWR | O_SYNC) ) < 0)
1536 return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: Unable to open /dev/mem: %s\n", strerror (errno)) ;
1540 gpio = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_BASE) ;
1541 if ((int32_t)gpio == -1)
1542 return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (GPIO) failed: %s\n", strerror (errno)) ;
1546 pwm = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PWM) ;
1547 if ((int32_t)pwm == -1)
1548 return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PWM) failed: %s\n", strerror (errno)) ;
1550 // Clock control (needed for PWM)
1552 clk = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, CLOCK_BASE) ;
1553 if ((int32_t)clk == -1)
1554 return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (CLOCK) failed: %s\n", strerror (errno)) ;
1558 pads = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PADS) ;
1559 if ((int32_t)pads == -1)
1560 return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PADS) failed: %s\n", strerror (errno)) ;
1565 timer = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_TIMER) ;
1566 if ((int32_t)timer == -1)
1567 return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (TIMER) failed: %s\n", strerror (errno)) ;
1569 // Set the timer to free-running, 1MHz.
1570 // 0xF9 is 249, the timer divide is base clock / (divide+1)
1571 // so base clock is 250MHz / 250 = 1MHz.
1573 *(timer + TIMER_CONTROL) = 0x0000280 ;
1574 *(timer + TIMER_PRE_DIV) = 0x00000F9 ;
1575 timerIrqRaw = timer + TIMER_IRQ_RAW ;
1578 initialiseEpoch () ;
1580 wiringPiMode = WPI_MODE_PINS ;
1587 * wiringPiSetupGpio:
1588 * Must be called once at the start of your program execution.
1590 * GPIO setup: Initialises the system into GPIO Pin mode and uses the
1591 * memory mapped hardware directly.
1592 *********************************************************************************
1595 int wiringPiSetupGpio (void)
1597 (void)wiringPiSetup () ;
1600 printf ("wiringPi: wiringPiSetupGpio called\n") ;
1602 wiringPiMode = WPI_MODE_GPIO ;
1609 * wiringPiSetupPhys:
1610 * Must be called once at the start of your program execution.
1612 * Phys setup: Initialises the system into Physical Pin mode and uses the
1613 * memory mapped hardware directly.
1614 *********************************************************************************
1617 int wiringPiSetupPhys (void)
1619 (void)wiringPiSetup () ;
1622 printf ("wiringPi: wiringPiSetupPhys called\n") ;
1624 wiringPiMode = WPI_MODE_PHYS ;
1632 * Must be called once at the start of your program execution.
1634 * Initialisation (again), however this time we are using the /sys/class/gpio
1635 * interface to the GPIO systems - slightly slower, but always usable as
1636 * a non-root user, assuming the devices are already exported and setup correctly.
1639 int wiringPiSetupSys (void)
1645 if (getenv (ENV_DEBUG) != NULL)
1646 wiringPiDebug = TRUE ;
1648 if (getenv (ENV_CODES) != NULL)
1649 wiringPiReturnCodes = TRUE ;
1652 printf ("wiringPi: wiringPiSetupSys called\n") ;
1654 boardRev = piBoardRev () ;
1658 pinToGpio = pinToGpioR1 ;
1659 physToGpio = physToGpioR1 ;
1663 pinToGpio = pinToGpioR2 ;
1664 physToGpio = physToGpioR2 ;
1667 // Open and scan the directory, looking for exported GPIOs, and pre-open
1668 // the 'value' interface to speed things up for later
1670 for (pin = 0 ; pin < 64 ; ++pin)
1672 sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
1673 sysFds [pin] = open (fName, O_RDWR) ;
1676 initialiseEpoch () ;
1678 wiringPiMode = WPI_MODE_GPIO_SYS ;