3 * Arduino compatable (ish) Wiring library for the Raspberry Pi
4 * Copyright (c) 2012 Gordon Henderson
6 * Thanks to code samples from Gert Jan van Loo and the
7 * BCM2835 ARM Peripherals manual, however it's missing
8 * the clock section /grr/mutter/
9 ***********************************************************************
10 * This file is part of wiringPi:
11 * https://projects.drogon.net/raspberry-pi/wiringpi/
13 * wiringPi is free software: you can redistribute it and/or modify
14 * it under the terms of the GNU Lesser General Public License as
15 * published by the Free Software Foundation, either version 3 of the
16 * License, or (at your option) any later version.
18 * wiringPi is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with wiringPi.
25 * If not, see <http://www.gnu.org/licenses/>.
26 ***********************************************************************
32 // Added an abstraction layer to the main routines to save a tiny
33 // bit of run-time and make the clode a little cleaner (if a little
35 // Added waitForInterrupt code
39 // Added in support to use the /sys/class/gpio interface.
41 // Fixed a few more bugs to do with range-checking when in GPIO mode.
44 // Added c++ support for the .h file
45 // Added a new function to allow for using my "pin" numbers, or native
47 // Removed my busy-loop delay and replaced it with a call to delayMicroseconds
50 // Added in the 2 UART pins
51 // Change maxPins to numPins to more accurately reflect purpose
53 // Pad drive current fiddling
69 #include <sys/types.h>
76 void (*pinMode) (int pin, int mode) ;
77 void (*pullUpDnControl) (int pin, int pud) ;
78 void (*digitalWrite) (int pin, int value) ;
79 void (*pwmWrite) (int pin, int value) ;
80 void (*setPadDrive) (int group, int value) ;
81 int (*digitalRead) (int pin) ;
82 int (*waitForInterrupt) (int pin, int mS) ;
83 void (*delayMicroseconds) (unsigned int howLong) ;
84 void (*pwmSetMode) (int mode) ;
85 void (*pwmSetRange) (unsigned int range) ;
95 #define BCM_PASSWORD 0x5A000000
98 // Port function select bits
100 #define FSEL_INPT 0b000
101 #define FSEL_OUTP 0b001
102 #define FSEL_ALT0 0b100
103 #define FSEL_ALT0 0b100
104 #define FSEL_ALT1 0b101
105 #define FSEL_ALT2 0b110
106 #define FSEL_ALT3 0b111
107 #define FSEL_ALT4 0b011
108 #define FSEL_ALT5 0b010
110 // Access from ARM Running Linux
111 // Take from Gert/Doms code. Some of this is not in the manual
112 // that I can find )-:
114 #define BCM2708_PERI_BASE 0x20000000
115 #define GPIO_PADS (BCM2708_PERI_BASE + 0x100000)
116 #define CLOCK_BASE (BCM2708_PERI_BASE + 0x101000)
117 #define GPIO_BASE (BCM2708_PERI_BASE + 0x200000)
118 #define GPIO_TIMER (BCM2708_PERI_BASE + 0x00B000)
119 #define GPIO_PWM (BCM2708_PERI_BASE + 0x20C000)
121 #define PAGE_SIZE (4*1024)
122 #define BLOCK_SIZE (4*1024)
126 #define PWM_CONTROL 0
133 #define PWMCLK_CNTL 40
134 #define PWMCLK_DIV 41
136 #define PWM1_MS_MODE 0x8000 // Run in MS mode
137 #define PWM1_USEFIFO 0x2000 // Data from FIFO
138 #define PWM1_REVPOLAR 0x1000 // Reverse polarity
139 #define PWM1_OFFSTATE 0x0800 // Ouput Off state
140 #define PWM1_REPEATFF 0x0400 // Repeat last value if FIFO empty
141 #define PWM1_SERIAL 0x0200 // Run in serial mode
142 #define PWM1_ENABLE 0x0100 // Channel Enable
144 #define PWM0_MS_MODE 0x0080 // Run in MS mode
145 #define PWM0_USEFIFO 0x0020 // Data from FIFO
146 #define PWM0_REVPOLAR 0x0010 // Reverse polarity
147 #define PWM0_OFFSTATE 0x0008 // Ouput Off state
148 #define PWM0_REPEATFF 0x0004 // Repeat last value if FIFO empty
149 #define PWM0_SERIAL 0x0002 // Run in serial mode
150 #define PWM0_ENABLE 0x0001 // Channel Enable
154 #define TIMER_LOAD (0x400 >> 2)
155 #define TIMER_VALUE (0x404 >> 2)
156 #define TIMER_CONTROL (0x408 >> 2)
157 #define TIMER_IRQ_CLR (0x40C >> 2)
158 #define TIMER_IRQ_RAW (0x410 >> 2)
159 #define TIMER_IRQ_MASK (0x414 >> 2)
160 #define TIMER_RELOAD (0x418 >> 2)
161 #define TIMER_PRE_DIV (0x41C >> 2)
162 #define TIMER_COUNTER (0x420 >> 2)
164 // Locals to hold pointers to the hardware
166 static volatile uint32_t *gpio ;
167 static volatile uint32_t *pwm ;
168 static volatile uint32_t *clk ;
169 static volatile uint32_t *pads ;
170 static volatile uint32_t *timer ;
172 static volatile uint32_t *timerIrqRaw ;
174 // Raspberry Pi board revision
176 static int boardRevision = -1 ;
180 static int wiringPiDebug = FALSE ;
182 // The BCM2835 has 54 GPIO pins.
183 // BCM2835 data sheet, Page 90 onwards.
184 // There are 6 control registers, each control the functions of a block
186 // Each control register has 10 sets of 3 bits per GPIO pin:
188 // 000 = GPIO Pin X is an input
189 // 001 = GPIO Pin X is an output
190 // 100 = GPIO Pin X takes alternate function 0
191 // 101 = GPIO Pin X takes alternate function 1
192 // 110 = GPIO Pin X takes alternate function 2
193 // 111 = GPIO Pin X takes alternate function 3
194 // 011 = GPIO Pin X takes alternate function 4
195 // 010 = GPIO Pin X takes alternate function 5
197 // So the 3 bits for port X are:
198 // X / 10 + ((X % 10) * 3)
201 // Map a file descriptor from the /sys/class/gpio/gpioX/value
203 static int sysFds [64] ;
205 // Doing it the Arduino way with lookup tables...
206 // Yes, it's probably more innefficient than all the bit-twidling, but it
207 // does tend to make it all a bit clearer. At least to me!
210 // Take a Wiring pin (0 through X) and re-map it to the BCM_GPIO pin
211 // Cope for 2 different board revieions here
213 static int *pinToGpio ;
215 static int pinToGpioR1 [64] =
217 17, 18, 21, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7
218 0, 1, // I2C - SDA0, SCL0
219 8, 7, // SPI - CE1, CE0
220 10, 9, 11, // SPI - MOSI, MISO, SCLK
221 14, 15, // UART - Tx, Rx
225 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 31
226 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
227 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
230 static int pinToGpioR2 [64] =
232 17, 18, 27, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7
233 2, 3, // I2C - SDA0, SCL0
234 8, 7, // SPI - CE1, CE0
235 10, 9, 11, // SPI - MOSI, MISO, SCLK
236 14, 15, // UART - Tx, Rx
237 28, 29, 30, 31, // New GPIOs 8 though 11
241 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 31
242 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
243 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
247 // Map a BCM_GPIO pin to it's control port. (GPFSEL 0-5)
249 static uint8_t gpioToGPFSEL [] =
251 0,0,0,0,0,0,0,0,0,0,
252 1,1,1,1,1,1,1,1,1,1,
253 2,2,2,2,2,2,2,2,2,2,
254 3,3,3,3,3,3,3,3,3,3,
255 4,4,4,4,4,4,4,4,4,4,
256 5,5,5,5,5,5,5,5,5,5,
260 // Define the shift up for the 3 bits per pin in each GPFSEL port
262 static uint8_t gpioToShift [] =
264 0,3,6,9,12,15,18,21,24,27,
265 0,3,6,9,12,15,18,21,24,27,
266 0,3,6,9,12,15,18,21,24,27,
267 0,3,6,9,12,15,18,21,24,27,
268 0,3,6,9,12,15,18,21,24,27,
272 // (Word) offset to the GPIO Set registers for each GPIO pin
274 static uint8_t gpioToGPSET [] =
276 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,
277 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,
281 // (Word) offset to the GPIO Clear registers for each GPIO pin
283 static uint8_t gpioToGPCLR [] =
285 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,
286 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,
290 // (Word) offset to the GPIO Input level registers for each GPIO pin
292 static uint8_t gpioToGPLEV [] =
294 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,
295 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,
300 // (Word) offset to the Event Detect Status
302 static uint8_t gpioToEDS [] =
304 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,
305 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,
309 // (Word) offset to the Rising edgde ENable register
311 static uint8_t gpioToREN [] =
313 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,
314 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,
318 // (Word) offset to the Falling edgde ENable register
320 static uint8_t gpioToFEN [] =
322 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,
323 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,
328 // (Word) offset to the Pull Up Down Clock regsiter
332 static uint8_t gpioToPUDCLK [] =
334 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,
335 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,
339 // the ALT value to put a GPIO pin into PWM mode
341 static uint8_t gpioToPwmALT [] =
343 0, 0, 0, 0, 0, 0, 0, 0, // 0 -> 7
344 0, 0, 0, 0, FSEL_ALT0, FSEL_ALT0, 0, 0, // 8 -> 15
345 0, 0, FSEL_ALT5, FSEL_ALT5, 0, 0, 0, 0, // 16 -> 23
346 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
347 0, 0, 0, 0, 0, 0, 0, 0, // 32 -> 39
348 FSEL_ALT0, FSEL_ALT0, 0, 0, 0, FSEL_ALT0, 0, 0, // 40 -> 47
349 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
350 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
353 static uint8_t gpioToPwmPort [] =
355 0, 0, 0, 0, 0, 0, 0, 0, // 0 -> 7
356 0, 0, 0, 0, PWM0_DATA, PWM1_DATA, 0, 0, // 8 -> 15
357 0, 0, PWM0_DATA, PWM1_DATA, 0, 0, 0, 0, // 16 -> 23
358 0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
359 0, 0, 0, 0, 0, 0, 0, 0, // 32 -> 39
360 PWM0_DATA, PWM1_DATA, 0, 0, 0, PWM1_DATA, 0, 0, // 40 -> 47
361 0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
362 0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
367 // Time for easy calculations
369 static unsigned long long epoch ;
371 //////////////////////////////////////////////////////////////////////////////////
376 * Sets the mode of a pin to be input, output or PWM output
377 *********************************************************************************
380 void pinModeGpio (int pin, int mode)
382 static int pwmRunning = FALSE ;
383 int fSel, shift, alt ;
387 fSel = gpioToGPFSEL [pin] ;
388 shift = gpioToShift [pin] ;
390 /**/ if (mode == INPUT)
391 *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) ; // Sets bits to zero = input
392 else if (mode == OUTPUT)
393 *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (1 << shift) ;
394 else if (mode == PWM_OUTPUT)
396 if ((alt = gpioToPwmALT [pin]) == 0) // Not a PWM pin
399 // Set pin to PWM mode
401 *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ;
403 // We didn't initialise the PWM hardware at setup time - because it's possible that
404 // something else is using the PWM - e.g. the Audio systems! So if we use PWM
405 // here, then we're assuming that nothing else is, otherwise things are going
406 // to sound a bit funny...
411 *(pwm + PWM_CONTROL) = 0 ; // Stop PWM
412 delayMicroseconds (10) ;
415 *(clk + PWMCLK_DIV) = BCM_PASSWORD | (32<<12) ; // set pwm div to 32 (19.2/32 = 600KHz)
416 *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x11 ; // Source=osc and enable
418 delayMicroseconds (10) ;
420 *(pwm + PWM0_RANGE) = 0x400 ; delayMicroseconds (10) ;
421 *(pwm + PWM1_RANGE) = 0x400 ; delayMicroseconds (10) ;
425 *(pwm + PWM0_DATA) = 512 ;
426 *(pwm + PWM1_DATA) = 512 ;
428 // Balanced mode (default)
430 *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ;
437 // When we change mode of any pin, we remove the pull up/downs
438 // Or we used to... Hm. Commented out now because for some wieird reason,
439 // it seems to block subsequent attempts to set the pull up/downs and I've
440 // not quite gotten to the bottom of why this happens
441 // The down-side is that the pull up/downs are rememberd in the SoC between
442 // power cycles, so it's going to be a good idea to explicitly set them in
445 // pullUpDnControl (pin, PUD_OFF) ;
449 void pinModeWPi (int pin, int mode)
451 pinModeGpio (pinToGpio [pin & 63], mode) ;
454 void pinModeSys (int pin, int mode)
462 * Allow the user to control some of the PWM functions
463 *********************************************************************************
466 void pwmSetModeWPi (int mode)
468 if (mode == PWM_MODE_MS)
469 *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE | PWM0_MS_MODE | PWM1_MS_MODE ;
471 *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ;
474 void pwmSetModeSys (int mode)
480 void pwmSetRangeWPi (unsigned int range)
482 *(pwm + PWM0_RANGE) = range ; delayMicroseconds (10) ;
483 *(pwm + PWM1_RANGE) = range ; delayMicroseconds (10) ;
486 void pwmSetRangeSys (unsigned int range)
496 * Enables edge-detect mode on a pin - from a 0 to a 1 or 1 to 0
497 * Pin must already be in input mode with appropriate pull up/downs set.
498 *********************************************************************************
501 void pinEnableED01Pi (int pin)
503 pin = pinToGpio [pin & 63] ;
512 *********************************************************************************
515 void digitalWriteWPi (int pin, int value)
517 pin = pinToGpio [pin & 63] ;
520 *(gpio + gpioToGPCLR [pin]) = 1 << (pin & 31) ;
522 *(gpio + gpioToGPSET [pin]) = 1 << (pin & 31) ;
525 void digitalWriteGpio (int pin, int value)
530 *(gpio + gpioToGPCLR [pin]) = 1 << (pin & 31) ;
532 *(gpio + gpioToGPSET [pin]) = 1 << (pin & 31) ;
535 void digitalWriteSys (int pin, int value)
539 if (sysFds [pin] != -1)
542 write (sysFds [pin], "0\n", 2) ;
544 write (sysFds [pin], "1\n", 2) ;
551 * Set an output PWM value
552 *********************************************************************************
555 void pwmWriteGpio (int pin, int value)
560 port = gpioToPwmPort [pin] ;
562 *(pwm + port) = value ;
565 void pwmWriteWPi (int pin, int value)
567 pwmWriteGpio (pinToGpio [pin & 63], value) ;
570 void pwmWriteSys (int pin, int value)
578 * Set the PAD driver value
579 *********************************************************************************
582 void setPadDriveWPi (int group, int value)
586 if ((group < 0) || (group > 2))
589 wrVal = BCM_PASSWORD | 0x18 | (value & 7) ;
590 *(pads + group + 11) = wrVal ;
593 printf ("setPadDrive: Group: %d, value: %d (%08X)\n", group, value, wrVal) ;
594 printf ("Read : %08X\n", *(pads + group + 11)) ;
598 void setPadDriveGpio (int group, int value)
600 setPadDriveWPi (group, value) ;
603 void setPadDriveSys (int group, int value)
611 * Read the value of a given Pin, returning HIGH or LOW
612 *********************************************************************************
615 int digitalReadWPi (int pin)
617 pin = pinToGpio [pin & 63] ;
619 if ((*(gpio + gpioToGPLEV [pin]) & (1 << (pin & 31))) != 0)
625 int digitalReadGpio (int pin)
629 if ((*(gpio + gpioToGPLEV [pin]) & (1 << (pin & 31))) != 0)
635 int digitalReadSys (int pin)
641 if (sysFds [pin] == -1)
644 lseek (sysFds [pin], 0L, SEEK_SET) ;
645 read (sysFds [pin], &c, 1) ;
646 return (c == '0') ? 0 : 1 ;
652 * Control the internal pull-up/down resistors on a GPIO pin
653 * The Arduino only has pull-ups and these are enabled by writing 1
654 * to a port when in input mode - this paradigm doesn't quite apply
656 *********************************************************************************
659 void pullUpDnControlGpio (int pin, int pud)
664 *(gpio + GPPUD) = pud ; delayMicroseconds (5) ;
665 *(gpio + gpioToPUDCLK [pin]) = 1 << (pin & 31) ; delayMicroseconds (5) ;
667 *(gpio + GPPUD) = 0 ; delayMicroseconds (5) ;
668 *(gpio + gpioToPUDCLK [pin]) = 0 ; delayMicroseconds (5) ;
671 void pullUpDnControlWPi (int pin, int pud)
673 pullUpDnControlGpio (pinToGpio [pin & 63], pud) ;
676 void pullUpDnControlSys (int pin, int pud)
684 * Wait for Interrupt on a GPIO pin.
685 * This is actually done via the /sys/class/gpio interface regardless of
686 * the wiringPi access mode in-use. Maybe sometime it might get a better
687 * way for a bit more efficiency.
688 *********************************************************************************
691 int waitForInterruptSys (int pin, int mS)
695 struct pollfd polls ;
697 if ((fd = sysFds [pin & 63]) == -1)
702 x = read (fd, buf, 6) ;
708 lseek (fd, 0, SEEK_SET) ;
710 // Setup poll structure
713 polls.events = POLLPRI ; // Urgent data!
717 return poll (&polls, 1, mS) ;
720 int waitForInterruptWPi (int pin, int mS)
722 return waitForInterruptSys (pinToGpio [pin & 63], mS) ;
725 int waitForInterruptGpio (int pin, int mS)
727 return waitForInterruptSys (pin, mS) ;
735 * Wait for some number of milli seconds
736 *********************************************************************************
739 void delay (unsigned int howLong)
741 struct timespec sleeper, dummy ;
743 sleeper.tv_sec = (time_t)(howLong / 1000) ;
744 sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ;
746 nanosleep (&sleeper, &dummy) ;
752 * This is somewhat intersting. It seems that on the Pi, a single call
753 * to nanosleep takes some 80 to 130 microseconds anyway, so while
754 * obeying the standards (may take longer), it's not always what we
757 * So what I'll do now is if the delay is less than 100uS we'll do it
758 * in a hard loop, watching a built-in counter on the ARM chip. This is
759 * somewhat sub-optimal in that it uses 100% CPU, something not an issue
760 * in a microcontroller, but under a multi-tasking, multi-user OS, it's
761 * wastefull, however we've no real choice )-:
762 *********************************************************************************
765 void delayMicrosecondsSys (unsigned int howLong)
767 struct timespec sleeper, dummy ;
770 sleeper.tv_nsec = (long)(howLong * 1000) ;
772 nanosleep (&sleeper, &dummy) ;
775 void delayMicrosecondsHard (unsigned int howLong)
777 *(timer + TIMER_LOAD) = howLong ;
778 *(timer + TIMER_IRQ_CLR) = 0 ;
780 while (*timerIrqRaw == 0)
784 void delayMicrosecondsWPi (unsigned int howLong)
786 struct timespec sleeper, dummy ;
788 /**/ if (howLong == 0)
790 else if (howLong < 100)
791 delayMicrosecondsHard (howLong) ;
795 sleeper.tv_nsec = (long)(howLong * 1000) ;
796 nanosleep (&sleeper, &dummy) ;
803 * Return a number of milliseconds as an unsigned int.
804 *********************************************************************************
807 unsigned int millis (void)
810 unsigned long long t1 ;
812 gettimeofday (&tv, NULL) ;
814 t1 = (tv.tv_sec * 1000000 + tv.tv_usec) / 1000 ;
816 return (uint32_t)(t1 - epoch) ;
822 * Must be called once at the start of your program execution.
824 * Default setup: Initialises the system into wiringPi Pin mode and uses the
825 * memory mapped hardware directly.
826 *********************************************************************************
829 int wiringPiSetup (void)
836 uint8_t *gpioMem, *pwmMem, *clkMem, *padsMem, *timerMem ;
839 if (getenv ("WIRINGPI_DEBUG") != NULL)
840 wiringPiDebug = TRUE ;
843 printf ("wiringPiSetup called\n") ;
845 pinMode = pinModeWPi ;
846 pullUpDnControl = pullUpDnControlWPi ;
847 digitalWrite = digitalWriteWPi ;
848 pwmWrite = pwmWriteWPi ;
849 setPadDrive = setPadDriveWPi ;
850 digitalRead = digitalReadWPi ;
851 waitForInterrupt = waitForInterruptWPi ;
852 delayMicroseconds = delayMicrosecondsWPi ;
853 pwmSetMode = pwmSetModeWPi ;
854 pwmSetRange = pwmSetRangeWPi ;
856 // Find board revision
858 if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL)
860 fprintf (stderr, "wiringPiSetup: Unable to open /proc/cpuinfo: %s\n", strerror (errno)) ;
864 while (fgets (line, 80, cpuFd) != NULL)
865 if (strncmp (line, "Revision", 8) == 0)
866 for (c = line ; *c ; ++c)
870 revision = atoi (c) ;
877 fprintf (stderr, "wiringPiSetup: Unable to determine board revision\n") ;
882 /**/ if ((revision == 2) || (revision == 3))
884 else if ((revision == 4) || (revision == 5) || (revision == 6))
888 fprintf (stderr, "wiringPiSetup: Unable to determine board revision: %d\n", revision) ;
894 if (boardRevision == 1)
895 pinToGpio = pinToGpioR1 ;
897 pinToGpio = pinToGpioR2 ;
900 printf ("Revision: %d, board revision: %d\n", revision, boardRevision) ;
902 // Open the master /dev/memory device
904 if ((fd = open ("/dev/mem", O_RDWR | O_SYNC) ) < 0)
906 fprintf (stderr, "wiringPiSetup: Unable to open /dev/mem: %s\n", strerror (errno)) ;
912 // Allocate 2 pages - 1 ...
914 if ((gpioMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
916 fprintf (stderr, "wiringPiSetup: malloc failed: %s\n", strerror (errno)) ;
920 // ... presumably to make sure we can round it up to a whole page size
922 if (((uint32_t)gpioMem % PAGE_SIZE) != 0)
923 gpioMem += PAGE_SIZE - ((uint32_t)gpioMem % PAGE_SIZE) ;
925 gpio = (uint32_t *)mmap((caddr_t)gpioMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, GPIO_BASE) ;
927 if ((int32_t)gpio < 0)
929 fprintf (stderr, "wiringPiSetup: mmap failed: %s\n", strerror (errno)) ;
935 if ((pwmMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
937 fprintf (stderr, "wiringPiSetup: pwmMem malloc failed: %s\n", strerror (errno)) ;
941 if (((uint32_t)pwmMem % PAGE_SIZE) != 0)
942 pwmMem += PAGE_SIZE - ((uint32_t)pwmMem % PAGE_SIZE) ;
944 pwm = (uint32_t *)mmap(pwmMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, GPIO_PWM) ;
946 if ((int32_t)pwm < 0)
948 fprintf (stderr, "wiringPiSetup: mmap failed (pwm): %s\n", strerror (errno)) ;
952 // Clock control (needed for PWM)
954 if ((clkMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
956 fprintf (stderr, "wiringPiSetup: clkMem malloc failed: %s\n", strerror (errno)) ;
960 if (((uint32_t)clkMem % PAGE_SIZE) != 0)
961 clkMem += PAGE_SIZE - ((uint32_t)clkMem % PAGE_SIZE) ;
963 clk = (uint32_t *)mmap(clkMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, CLOCK_BASE) ;
965 if ((int32_t)clk < 0)
967 fprintf (stderr, "wiringPiSetup: mmap failed (clk): %s\n", strerror (errno)) ;
973 if ((padsMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
975 fprintf (stderr, "wiringPiSetup: padsMem malloc failed: %s\n", strerror (errno)) ;
979 if (((uint32_t)padsMem % PAGE_SIZE) != 0)
980 padsMem += PAGE_SIZE - ((uint32_t)padsMem % PAGE_SIZE) ;
982 pads = (uint32_t *)mmap(padsMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, GPIO_PADS) ;
984 if ((int32_t)pads < 0)
986 fprintf (stderr, "wiringPiSetup: mmap failed (pads): %s\n", strerror (errno)) ;
991 printf ("Checking pads @ 0x%08X\n", (unsigned int)pads) ;
992 printf (" -> %08X %08X %08X\n", *(pads + 11), *(pads + 12), *(pads + 13)) ;
997 if ((timerMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
999 fprintf (stderr, "wiringPiSetup: timerMem malloc failed: %s\n", strerror (errno)) ;
1003 if (((uint32_t)timerMem % PAGE_SIZE) != 0)
1004 timerMem += PAGE_SIZE - ((uint32_t)timerMem % PAGE_SIZE) ;
1006 timer = (uint32_t *)mmap(timerMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, GPIO_TIMER) ;
1008 if ((int32_t)timer < 0)
1010 fprintf (stderr, "wiringPiSetup: mmap failed (timer): %s\n", strerror (errno)) ;
1014 // Set the timer to free-running, 1MHz.
1015 // 0xF9 is 249, the timer divide is base clock / (divide+1)
1016 // so base clock is 250MHz / 250 = 1MHz.
1018 *(timer + TIMER_CONTROL) = 0x0000280 ;
1019 *(timer + TIMER_PRE_DIV) = 0x00000F9 ;
1020 timerIrqRaw = timer + TIMER_IRQ_RAW ;
1022 // Initialise our epoch for millis()
1024 gettimeofday (&tv, NULL) ;
1025 epoch = (tv.tv_sec * 1000000 + tv.tv_usec) / 1000 ;
1032 * wiringPiSetupGpio:
1033 * Must be called once at the start of your program execution.
1035 * GPIO setup: Initialises the system into GPIO Pin mode and uses the
1036 * memory mapped hardware directly.
1037 *********************************************************************************
1040 int wiringPiSetupGpio (void)
1045 printf ("wiringPiSetupGpio called\n") ;
1047 if ((x = wiringPiSetup ()) < 0)
1050 pinMode = pinModeGpio ;
1051 pullUpDnControl = pullUpDnControlGpio ;
1052 digitalWrite = digitalWriteGpio ;
1053 pwmWrite = pwmWriteGpio ;
1054 setPadDrive = setPadDriveGpio ;
1055 digitalRead = digitalReadGpio ;
1056 waitForInterrupt = waitForInterruptGpio ;
1057 delayMicroseconds = delayMicrosecondsWPi ; // Same
1058 pwmSetMode = pwmSetModeWPi ;
1059 pwmSetRange = pwmSetRangeWPi ;
1067 * Must be called once at the start of your program execution.
1069 * Initialisation (again), however this time we are using the /sys/class/gpio
1070 * interface to the GPIO systems - slightly slower, but always usable as
1071 * a non-root user, assuming the devices are already exported and setup correctly.
1074 int wiringPiSetupSys (void)
1081 printf ("wiringPiSetupSys called\n") ;
1083 pinMode = pinModeSys ;
1084 pullUpDnControl = pullUpDnControlSys ;
1085 digitalWrite = digitalWriteSys ;
1086 pwmWrite = pwmWriteSys ;
1087 setPadDrive = setPadDriveSys ;
1088 digitalRead = digitalReadSys ;
1089 waitForInterrupt = waitForInterruptSys ;
1090 delayMicroseconds = delayMicrosecondsSys ;
1091 pwmSetMode = pwmSetModeSys ;
1092 pwmSetRange = pwmSetRangeSys ;
1095 // Open and scan the directory, looking for exported GPIOs, and pre-open
1096 // the 'value' interface to speed things up for later
1098 for (pin = 0 ; pin < 64 ; ++pin)
1100 sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
1101 sysFds [pin] = open (fName, O_RDWR) ;
1104 // Initialise the epoch for mills() ...
1106 gettimeofday (&tv, NULL) ;
1107 epoch = (tv.tv_sec * 1000000 + tv.tv_usec) / 1000 ;