chiark / gitweb /
Updated mostly to the gpio readall command to support the Raspberry Pi B+
[wiringPi.git] / wiringPi / wiringPi.c
1 /*
2  * wiringPi:
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>
6  *
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/
13  *
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.
18  *
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.
23  *
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  ***********************************************************************
28  */
29
30 // Revisions:
31 //      19 Jul 2012:
32 //              Moved to the LGPL
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
35 //              larger)
36 //              Added waitForInterrupt code
37 //              Added piHiPri code
38 //
39 //       9 Jul 2012:
40 //              Added in support to use the /sys/class/gpio interface.
41 //       2 Jul 2012:
42 //              Fixed a few more bugs to do with range-checking when in GPIO mode.
43 //      11 Jun 2012:
44 //              Fixed some typos.
45 //              Added c++ support for the .h file
46 //              Added a new function to allow for using my "pin" numbers, or native
47 //                      GPIO pin numbers.
48 //              Removed my busy-loop delay and replaced it with a call to delayMicroseconds
49 //
50 //      02 May 2012:
51 //              Added in the 2 UART pins
52 //              Change maxPins to numPins to more accurately reflect purpose
53
54
55 #include <stdio.h>
56 #include <stdarg.h>
57 #include <stdint.h>
58 #include <stdlib.h>
59 #include <ctype.h>
60 #include <poll.h>
61 #include <unistd.h>
62 #include <errno.h>
63 #include <string.h>
64 #include <time.h>
65 #include <fcntl.h>
66 #include <pthread.h>
67 #include <sys/time.h>
68 #include <sys/mman.h>
69 #include <sys/stat.h>
70 #include <sys/wait.h>
71 #include <sys/ioctl.h>
72
73 #include "softPwm.h"
74 #include "softTone.h"
75
76 #include "wiringPi.h"
77
78 #ifndef TRUE
79 #define TRUE    (1==1)
80 #define FALSE   (1==2)
81 #endif
82
83 // Environment Variables
84
85 #define ENV_DEBUG       "WIRINGPI_DEBUG"
86 #define ENV_CODES       "WIRINGPI_CODES"
87
88
89 // Mask for the bottom 64 pins which belong to the Raspberry Pi
90 //      The others are available for the other devices
91
92 #define PI_GPIO_MASK    (0xFFFFFFC0)
93
94 struct wiringPiNodeStruct *wiringPiNodes = NULL ;
95
96 // BCM Magic
97
98 #define BCM_PASSWORD            0x5A000000
99
100
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
104 //      of 10 pins.
105 //      Each control register has 10 sets of 3 bits per GPIO pin - the ALT values
106 //
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
115 //
116 // So the 3 bits for port X are:
117 //      X / 10 + ((X % 10) * 3)
118
119 // Port function select bits
120
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
129
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 )-:
133
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)
140
141 #define PAGE_SIZE               (4*1024)
142 #define BLOCK_SIZE              (4*1024)
143
144 // PWM
145 //      Word offsets into the PWM control region
146
147 #define PWM_CONTROL 0
148 #define PWM_STATUS  1
149 #define PWM0_RANGE  4
150 #define PWM0_DATA   5
151 #define PWM1_RANGE  8
152 #define PWM1_DATA   9
153
154 //      Clock regsiter offsets
155
156 #define PWMCLK_CNTL     40
157 #define PWMCLK_DIV      41
158
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
166
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
174
175 // Timer
176 //      Word offsets
177
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)
187
188 // Locals to hold pointers to the hardware
189
190 static volatile uint32_t *gpio ;
191 static volatile uint32_t *pwm ;
192 static volatile uint32_t *clk ;
193 static volatile uint32_t *pads ;
194
195 #ifdef  USE_TIMER
196 static volatile uint32_t *timer ;
197 static volatile uint32_t *timerIrqRaw ;
198 #endif
199
200 // Time for easy calculations
201
202 static uint64_t epochMilli, epochMicro ;
203
204 // Misc
205
206 static int wiringPiMode = WPI_MODE_UNINITIALISED ;
207 static volatile int    pinPass = -1 ;
208 static pthread_mutex_t pinMutex ;
209
210 // Debugging & Return codes
211
212 int wiringPiDebug       = FALSE ;
213 int wiringPiReturnCodes = FALSE ;
214
215 // sysFds:
216 //      Map a file descriptor from the /sys/class/gpio/gpioX/value
217
218 static int sysFds [64] =
219 {
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,
224 } ;
225
226 // ISR Data
227
228 static void (*isrFunctions [64])(void) ;
229
230
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!
234
235 // pinToGpio:
236 //      Take a Wiring pin (0 through X) and re-map it to the BCM_GPIO pin
237 //      Cope for 3 different board revisions here.
238
239 static int *pinToGpio ;
240
241 // Revision 1, 1.1:
242
243 static int pinToGpioR1 [64] =
244 {
245   17, 18, 21, 22, 23, 24, 25, 4,        // From the Original Wiki - GPIO 0 through 7:   wpi  0 -  7
246    0,  1,                               // I2C  - SDA1, SCL1                            wpi  8 -  9
247    8,  7,                               // SPI  - CE1, CE0                              wpi 10 - 11
248   10,  9, 11,                           // SPI  - MOSI, MISO, SCLK                      wpi 12 - 14
249   14, 15,                               // UART - Tx, Rx                                wpi 15 - 16
250
251 // Padding:
252
253       -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,       // ... 31
254   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,       // ... 47
255   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,       // ... 63
256 } ;
257
258 // Revision 2:
259
260 static int pinToGpioR2 [64] =
261 {
262   17, 18, 27, 22, 23, 24, 25, 4,        // From the Original Wiki - GPIO 0 through 7:   wpi  0 -  7
263    2,  3,                               // I2C  - SDA0, SCL0                            wpi  8 -  9
264    8,  7,                               // SPI  - CE1, CE0                              wpi 10 - 11
265   10,  9, 11,                           // SPI  - MOSI, MISO, SCLK                      wpi 12 - 14
266   14, 15,                               // UART - Tx, Rx                                wpi 15 - 16
267   28, 29, 30, 31,                       // Rev 2: New GPIOs 8 though 11                 wpi 17 - 20
268    5,  6, 13, 19, 26,                   // B+                                           wpi 21, 22, 23, 24, 25
269   12, 16, 20, 21,                       // B+                                           wpi 26, 27, 28, 29
270    0,  1,                               // B+                                           wpi 30, 31
271
272 // Padding:
273
274   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,       // ... 47
275   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,       // ... 63
276 } ;
277
278
279 // physToGpio:
280 //      Take a physical pin (1 through 26) and re-map it to the BCM_GPIO pin
281 //      Cope for 2 different board revisions here.
282 //      Also add in the P5 connector, so the P5 pins are 3,4,5,6, so 53,54,55,56
283
284 static int *physToGpio ;
285
286 static int physToGpioR1 [64] =
287 {
288   -1,           // 0
289   -1, -1,       // 1, 2
290    0, -1,
291    1, -1,
292    4, 14,
293   -1, 15,
294   17, 18,
295   21, -1,
296   22, 23,
297   -1, 24,
298   10, -1,
299    9, 25,
300   11,  8,
301   -1,  7,       // 25, 26
302
303                                               -1, -1, -1, -1, -1,       // ... 31
304   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,       // ... 47
305   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,       // ... 63
306 } ;
307
308 static int physToGpioR2 [64] =
309 {
310   -1,           // 0
311   -1, -1,       // 1, 2
312    2, -1,
313    3, -1,
314    4, 14,
315   -1, 15,
316   17, 18,
317   27, -1,
318   22, 23,
319   -1, 24,
320   10, -1,
321    9, 25,
322   11,  8,
323   -1,  7,       // 25, 26
324
325 // B+
326
327    0,  1,
328    5, -1,
329    6, 12,
330   13, -1,
331   19, 16,
332   26, 20,
333   -1, 21,
334
335 // the P5 connector on the Rev 2 boards:
336
337                            -1, -1, -1, -1, -1, -1, -1,                  // ... 47
338   -1, -1, -1, -1, -1,                                                   // ... 52
339   28, 29, 30, 31,                                                       // ... 53, 54, 55, 56 - P5
340   -1, -1, -1, -1, -1, -1, -1,                                           // ... 63
341 } ;
342
343 // gpioToGPFSEL:
344 //      Map a BCM_GPIO pin to it's Function Selection
345 //      control port. (GPFSEL 0-5)
346 //      Groups of 10 - 3 bits per Function - 30 bits per port
347
348 static uint8_t gpioToGPFSEL [] =
349 {
350   0,0,0,0,0,0,0,0,0,0,
351   1,1,1,1,1,1,1,1,1,1,
352   2,2,2,2,2,2,2,2,2,2,
353   3,3,3,3,3,3,3,3,3,3,
354   4,4,4,4,4,4,4,4,4,4,
355   5,5,5,5,5,5,5,5,5,5,
356 } ;
357
358
359 // gpioToShift
360 //      Define the shift up for the 3 bits per pin in each GPFSEL port
361
362 static uint8_t gpioToShift [] =
363 {
364   0,3,6,9,12,15,18,21,24,27,
365   0,3,6,9,12,15,18,21,24,27,
366   0,3,6,9,12,15,18,21,24,27,
367   0,3,6,9,12,15,18,21,24,27,
368   0,3,6,9,12,15,18,21,24,27,
369 } ;
370
371
372 // gpioToGPSET:
373 //      (Word) offset to the GPIO Set registers for each GPIO pin
374
375 static uint8_t gpioToGPSET [] =
376 {
377    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,
378    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,
379 } ;
380
381 // gpioToGPCLR:
382 //      (Word) offset to the GPIO Clear registers for each GPIO pin
383
384 static uint8_t gpioToGPCLR [] =
385 {
386   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,
387   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,
388 } ;
389
390
391 // gpioToGPLEV:
392 //      (Word) offset to the GPIO Input level registers for each GPIO pin
393
394 static uint8_t gpioToGPLEV [] =
395 {
396   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,
397   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,
398 } ;
399
400
401 #ifdef notYetReady
402 // gpioToEDS
403 //      (Word) offset to the Event Detect Status
404
405 static uint8_t gpioToEDS [] =
406 {
407   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,
408   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,
409 } ;
410
411 // gpioToREN
412 //      (Word) offset to the Rising edge ENable register
413
414 static uint8_t gpioToREN [] =
415 {
416   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,
417   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,
418 } ;
419
420 // gpioToFEN
421 //      (Word) offset to the Falling edgde ENable register
422
423 static uint8_t gpioToFEN [] =
424 {
425   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,
426   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,
427 } ;
428 #endif
429
430
431 // GPPUD:
432 //      GPIO Pin pull up/down register
433
434 #define GPPUD   37
435
436 // gpioToPUDCLK
437 //      (Word) offset to the Pull Up Down Clock regsiter
438
439 static uint8_t gpioToPUDCLK [] =
440 {
441   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,
442   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,
443 } ;
444
445
446 // gpioToPwmALT
447 //      the ALT value to put a GPIO pin into PWM mode
448
449 static uint8_t gpioToPwmALT [] =
450 {
451           0,         0,         0,         0,         0,         0,         0,         0,       //  0 ->  7
452           0,         0,         0,         0, FSEL_ALT0, FSEL_ALT0,         0,         0,       //  8 -> 15
453           0,         0, FSEL_ALT5, FSEL_ALT5,         0,         0,         0,         0,       // 16 -> 23
454           0,         0,         0,         0,         0,         0,         0,         0,       // 24 -> 31
455           0,         0,         0,         0,         0,         0,         0,         0,       // 32 -> 39
456   FSEL_ALT0, FSEL_ALT0,         0,         0,         0, FSEL_ALT0,         0,         0,       // 40 -> 47
457           0,         0,         0,         0,         0,         0,         0,         0,       // 48 -> 55
458           0,         0,         0,         0,         0,         0,         0,         0,       // 56 -> 63
459 } ;
460
461
462 // gpioToPwmPort
463 //      The port value to put a GPIO pin into PWM mode
464
465 static uint8_t gpioToPwmPort [] =
466 {
467           0,         0,         0,         0,         0,         0,         0,         0,       //  0 ->  7
468           0,         0,         0,         0, PWM0_DATA, PWM1_DATA,         0,         0,       //  8 -> 15
469           0,         0, PWM0_DATA, PWM1_DATA,         0,         0,         0,         0,       // 16 -> 23
470           0,         0,         0,         0,         0,         0,         0,         0,       // 24 -> 31
471           0,         0,         0,         0,         0,         0,         0,         0,       // 32 -> 39
472   PWM0_DATA, PWM1_DATA,         0,         0,         0, PWM1_DATA,         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
475
476 } ;
477
478 // gpioToGpClkALT:
479 //      ALT value to put a GPIO pin into GP Clock mode.
480 //      On the Pi we can really only use BCM_GPIO_4 and BCM_GPIO_21
481 //      for clocks 0 and 1 respectively, however I'll include the full
482 //      list for completeness - maybe one day...
483
484 #define GPIO_CLOCK_SOURCE       1
485
486 // gpioToGpClkALT0:
487
488 static uint8_t gpioToGpClkALT0 [] =
489 {
490           0,         0,         0,         0, FSEL_ALT0, FSEL_ALT0, FSEL_ALT0,         0,       //  0 ->  7
491           0,         0,         0,         0,         0,         0,         0,         0,       //  8 -> 15
492           0,         0,         0,         0, FSEL_ALT5, FSEL_ALT5,         0,         0,       // 16 -> 23
493           0,         0,         0,         0,         0,         0,         0,         0,       // 24 -> 31
494   FSEL_ALT0,         0, FSEL_ALT0,         0,         0,         0,         0,         0,       // 32 -> 39
495           0,         0, FSEL_ALT0, FSEL_ALT0, FSEL_ALT0,         0,         0,         0,       // 40 -> 47
496           0,         0,         0,         0,         0,         0,         0,         0,       // 48 -> 55
497           0,         0,         0,         0,         0,         0,         0,         0,       // 56 -> 63
498 } ;
499
500 // gpioToClk:
501 //      (word) Offsets to the clock Control and Divisor register
502
503 static uint8_t gpioToClkCon [] =
504 {
505          -1,        -1,        -1,        -1,        28,        30,        32,        -1,       //  0 ->  7
506          -1,        -1,        -1,        -1,        -1,        -1,        -1,        -1,       //  8 -> 15
507          -1,        -1,        -1,        -1,        28,        30,        -1,        -1,       // 16 -> 23
508          -1,        -1,        -1,        -1,        -1,        -1,        -1,        -1,       // 24 -> 31
509          28,        -1,        28,        -1,        -1,        -1,        -1,        -1,       // 32 -> 39
510          -1,        -1,        28,        30,        28,        -1,        -1,        -1,       // 40 -> 47
511          -1,        -1,        -1,        -1,        -1,        -1,        -1,        -1,       // 48 -> 55
512          -1,        -1,        -1,        -1,        -1,        -1,        -1,        -1,       // 56 -> 63
513 } ;
514
515 static uint8_t gpioToClkDiv [] =
516 {
517          -1,        -1,        -1,        -1,        29,        31,        33,        -1,       //  0 ->  7
518          -1,        -1,        -1,        -1,        -1,        -1,        -1,        -1,       //  8 -> 15
519          -1,        -1,        -1,        -1,        29,        31,        -1,        -1,       // 16 -> 23
520          -1,        -1,        -1,        -1,        -1,        -1,        -1,        -1,       // 24 -> 31
521          29,        -1,        29,        -1,        -1,        -1,        -1,        -1,       // 32 -> 39
522          -1,        -1,        29,        31,        29,        -1,        -1,        -1,       // 40 -> 47
523          -1,        -1,        -1,        -1,        -1,        -1,        -1,        -1,       // 48 -> 55
524          -1,        -1,        -1,        -1,        -1,        -1,        -1,        -1,       // 56 -> 63
525 } ;
526
527
528 /*
529  * Functions
530  *********************************************************************************
531  */
532
533
534 /*
535  * wiringPiFailure:
536  *      Fail. Or not.
537  *********************************************************************************
538  */
539
540 int wiringPiFailure (int fatal, const char *message, ...)
541 {
542   va_list argp ;
543   char buffer [1024] ;
544
545   if (!fatal && wiringPiReturnCodes)
546     return -1 ;
547
548   va_start (argp, message) ;
549     vsnprintf (buffer, 1023, message, argp) ;
550   va_end (argp) ;
551
552   fprintf (stderr, "%s", buffer) ;
553   exit (EXIT_FAILURE) ;
554
555   return 0 ;
556 }
557
558
559 /*
560  * piBoardRev:
561  *      Return a number representing the hardware revision of the board.
562  *      Revision is currently 1 or 2.
563  *
564  *      Much confusion here )-:
565  *      Seems there are some boards with 0000 in them (mistake in manufacture)
566  *      So the distinction between boards that I can see is:
567  *      0000 - Error
568  *      0001 - Not used (Compute - default to Rev 2)
569  *      0002 - Model B, Rev 1, 256MB
570  *      0003 - Model B, Rev 1.1, 256MB, Fuses/D14 removed.
571  *      0004 - Model B, Rev 2, 256MB, Sony
572  *      0005 - Model B, Rev 2, 256MB, Qisda
573  *      0006 - Model B, Rev 2, 256MB, Egoman
574  *      0007 - Model A, Rev 2, 256MB, Egoman
575  *      0008 - Model A, Rev 2, 256MB, Sony
576  *      0009 - Model A, Rev 2, 256MB, Qisda
577  *      000d - Model B, Rev 2, 512MB, Egoman
578  *      000e - Model B, Rev 2, 512MB, Sony
579  *      000f - Model B, Rev 2, 512MB, Qisda
580  *      0010 - Model B+        512MB, Sony
581  *      0011 - Pi compute Module
582  *
583  *      A small thorn is the olde style overvolting - that will add in
584  *              1000000
585  *
586  *      The Pi compute module has an revision of 0011 - since we only check the
587  *      last digit, then it's 1, therefore it'll default to not 2 or 3 for a
588  *      Rev 1, so will appear as a Rev 2. This is fine for the most part, but
589  *      we'll properly detect the Compute Module later and adjust accordingly.
590  *
591  *********************************************************************************
592  */
593
594 static void piBoardRevOops (const char *why)
595 {
596   fprintf (stderr, "piBoardRev: Unable to determine board revision from /proc/cpuinfo\n") ;
597   fprintf (stderr, " -> %s\n", why) ;
598   fprintf (stderr, " ->  You may want to check:\n") ;
599   fprintf (stderr, " ->  http://www.raspberrypi.org/phpBB3/viewtopic.php?p=184410#p184410\n") ;
600   exit (EXIT_FAILURE) ;
601 }
602
603 int piBoardRev (void)
604 {
605   FILE *cpuFd ;
606   char line [120] ;
607   char *c, lastChar ;
608   static int  boardRev = -1 ;
609
610   if (boardRev != -1)   // No point checking twice
611     return boardRev ;
612
613   if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL)
614     piBoardRevOops ("Unable to open /proc/cpuinfo") ;
615
616   while (fgets (line, 120, cpuFd) != NULL)
617     if (strncmp (line, "Revision", 8) == 0)
618       break ;
619
620   fclose (cpuFd) ;
621
622   if (strncmp (line, "Revision", 8) != 0)
623     piBoardRevOops ("No \"Revision\" line") ;
624
625   for (c = &line [strlen (line) - 1] ; (*c == '\n') || (*c == '\r') ; --c)
626     *c = 0 ;
627   
628   if (wiringPiDebug)
629     printf ("piboardRev: Revision string: %s\n", line) ;
630
631   for (c = line ; *c ; ++c)
632     if (isdigit (*c))
633       break ;
634
635   if (!isdigit (*c))
636     piBoardRevOops ("No numeric revision string") ;
637
638 // If you have overvolted the Pi, then it appears that the revision
639 //      has 100000 added to it!
640
641   if (wiringPiDebug)
642     if (strlen (c) != 4)
643       printf ("piboardRev: This Pi has/is overvolted!\n") ;
644
645   lastChar = line [strlen (line) - 1] ;
646
647   if (wiringPiDebug)
648     printf ("piboardRev: lastChar is: '%c' (%d, 0x%02X)\n", lastChar, lastChar, lastChar) ;
649
650   /**/ if ((lastChar == '2') || (lastChar == '3'))
651     boardRev = 1 ;
652   else
653     boardRev = 2 ;
654
655   if (wiringPiDebug)
656     printf ("piBoardRev: Returning revision: %d\n", boardRev) ;
657
658   return boardRev ;
659 }
660
661
662 /*
663  * piBoardId:
664  *      Do more digging into the board revision string as above, but return
665  *      as much details as we can.
666  *********************************************************************************
667  */
668
669 const char *piModelNames [] =
670 {
671   "Model A",
672   "Model B",
673   "Model B+",
674   "Compute Module",
675 } ;
676
677 const char *piRevisionNames[] =
678 {
679   "1",
680   "1.1",
681   "2",
682   "1.2",
683 } ;
684
685 void piBoardId (int *model, int *rev, int *mem, char **maker)
686 {
687   FILE *cpuFd ;
688   char line [120] ;
689   char *c ;
690
691   piBoardRev () ;       // Call this first to make sure all's OK. Don't care about the result.
692
693   if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL)
694     piBoardRevOops ("Unable to open /proc/cpuinfo") ;
695
696   while (fgets (line, 120, cpuFd) != NULL)
697     if (strncmp (line, "Revision", 8) == 0)
698       break ;
699
700   fclose (cpuFd) ;
701
702   if (strncmp (line, "Revision", 8) != 0)
703     piBoardRevOops ("No \"Revision\" line") ;
704
705 // Chomp trailing CR/NL
706
707   for (c = &line [strlen (line) - 1] ; (*c == '\n') || (*c == '\r') ; --c)
708     *c = 0 ;
709   
710   if (wiringPiDebug)
711     printf ("piboardId: Revision string: %s\n", line) ;
712
713 // Scan to first digit
714
715   for (c = line ; *c ; ++c)
716     if (isdigit (*c))
717       break ;
718
719 // Make sure its long enough
720
721   if (strlen (c) < 4)
722     piBoardRevOops ("Bogus \"Revision\" line") ;
723   
724 // Extract last 4 characters:
725
726   c = c + strlen (c) - 4 ;
727
728 // Fill out the replys as appropriate
729
730   /**/ if (strcmp (c, "0002") == 0) { *model = 1 ; *rev = 0 ; *mem = 256 ; *maker = "China"  ; }
731   else if (strcmp (c, "0003") == 0) { *model = 1 ; *rev = 1 ; *mem = 256 ; *maker = "China"  ; }
732   else if (strcmp (c, "0004") == 0) { *model = 1 ; *rev = 2 ; *mem = 256 ; *maker = "Sony"   ; }
733   else if (strcmp (c, "0005") == 0) { *model = 1 ; *rev = 2 ; *mem = 256 ; *maker = "Qisda"  ; }
734   else if (strcmp (c, "0006") == 0) { *model = 1 ; *rev = 2 ; *mem = 256 ; *maker = "Egoman" ; }
735   else if (strcmp (c, "0007") == 0) { *model = 0 ; *rev = 2 ; *mem = 256 ; *maker = "Egoman" ; }
736   else if (strcmp (c, "0008") == 0) { *model = 0 ; *rev = 2 ; *mem = 256 ; *maker = "Sony"   ; }
737   else if (strcmp (c, "0009") == 0) { *model = 1 ; *rev = 2 ; *mem = 256 ; *maker = "Qisda"  ; }
738   else if (strcmp (c, "000d") == 0) { *model = 1 ; *rev = 2 ; *mem = 512 ; *maker = "Egoman" ; }
739   else if (strcmp (c, "000e") == 0) { *model = 1 ; *rev = 2 ; *mem = 512 ; *maker = "Sony"   ; }
740   else if (strcmp (c, "000f") == 0) { *model = 1 ; *rev = 2 ; *mem = 512 ; *maker = "Egoman" ; }
741   else if (strcmp (c, "0010") == 0) { *model = 2 ; *rev = 3 ; *mem = 512 ; *maker = "Sony"   ; }
742   else if (strcmp (c, "0011") == 0) { *model = 3 ; *rev = 1 ; *mem = 512 ; *maker = "Sony"   ; }
743   else                              { *model = 0 ; *rev = 0 ; *mem =   0 ; *maker = "Unkn"   ; }
744 }
745  
746
747
748 /*
749  * wpiPinToGpio:
750  *      Translate a wiringPi Pin number to native GPIO pin number.
751  *      Provided for external support.
752  *********************************************************************************
753  */
754
755 int wpiPinToGpio (int wpiPin)
756 {
757   return pinToGpio [wpiPin & 63] ;
758 }
759
760
761 /*
762  * physPinToGpio:
763  *      Translate a physical Pin number to native GPIO pin number.
764  *      Provided for external support.
765  *********************************************************************************
766  */
767
768 int physPinToGpio (int physPin)
769 {
770   return physToGpio [physPin & 63] ;
771 }
772
773
774 /*
775  * setPadDrive:
776  *      Set the PAD driver value
777  *********************************************************************************
778  */
779
780 void setPadDrive (int group, int value)
781 {
782   uint32_t wrVal ;
783
784   if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
785   {
786     if ((group < 0) || (group > 2))
787       return ;
788
789     wrVal = BCM_PASSWORD | 0x18 | (value & 7) ;
790     *(pads + group + 11) = wrVal ;
791
792     if (wiringPiDebug)
793     {
794       printf ("setPadDrive: Group: %d, value: %d (%08X)\n", group, value, wrVal) ;
795       printf ("Read : %08X\n", *(pads + group + 11)) ;
796     }
797   }
798 }
799
800
801 /*
802  * getAlt:
803  *      Returns the ALT bits for a given port. Only really of-use
804  *      for the gpio readall command (I think)
805  *********************************************************************************
806  */
807
808 int getAlt (int pin)
809 {
810   int fSel, shift, alt ;
811
812   pin &= 63 ;
813
814   /**/ if (wiringPiMode == WPI_MODE_PINS)
815     pin = pinToGpio [pin] ;
816   else if (wiringPiMode == WPI_MODE_PHYS)
817     pin = physToGpio [pin] ;
818   else if (wiringPiMode != WPI_MODE_GPIO)
819     return 0 ;
820
821   fSel    = gpioToGPFSEL [pin] ;
822   shift   = gpioToShift  [pin] ;
823
824   alt = (*(gpio + fSel) >> shift) & 7 ;
825
826   return alt ;
827 }
828
829
830 /*
831  * pwmSetMode:
832  *      Select the native "balanced" mode, or standard mark:space mode
833  *********************************************************************************
834  */
835
836 void pwmSetMode (int mode)
837 {
838   if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
839   {
840     if (mode == PWM_MODE_MS)
841       *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE | PWM0_MS_MODE | PWM1_MS_MODE ;
842     else
843       *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ;
844   }
845 }
846
847
848 /*
849  * pwmSetRange:
850  *      Set the PWM range register. We set both range registers to the same
851  *      value. If you want different in your own code, then write your own.
852  *********************************************************************************
853  */
854
855 void pwmSetRange (unsigned int range)
856 {
857   if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
858   {
859     *(pwm + PWM0_RANGE) = range ; delayMicroseconds (10) ;
860     *(pwm + PWM1_RANGE) = range ; delayMicroseconds (10) ;
861   }
862 }
863
864
865 /*
866  * pwmSetClock:
867  *      Set/Change the PWM clock. Originally my code, but changed
868  *      (for the better!) by Chris Hall, <chris@kchall.plus.com>
869  *      after further study of the manual and testing with a 'scope
870  *********************************************************************************
871  */
872
873 void pwmSetClock (int divisor)
874 {
875   uint32_t pwm_control ;
876   divisor &= 4095 ;
877
878   if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
879   {
880     if (wiringPiDebug)
881       printf ("Setting to: %d. Current: 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;
882
883     pwm_control = *(pwm + PWM_CONTROL) ;                // preserve PWM_CONTROL
884
885 // We need to stop PWM prior to stopping PWM clock in MS mode otherwise BUSY
886 // stays high.
887
888     *(pwm + PWM_CONTROL) = 0 ;                          // Stop PWM
889
890 // Stop PWM clock before changing divisor. The delay after this does need to
891 // this big (95uS occasionally fails, 100uS OK), it's almost as though the BUSY
892 // flag is not working properly in balanced mode. Without the delay when DIV is
893 // adjusted the clock sometimes switches to very slow, once slow further DIV
894 // adjustments do nothing and it's difficult to get out of this mode.
895
896     *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x01 ;        // Stop PWM Clock
897       delayMicroseconds (110) ;                 // prevents clock going sloooow
898
899     while ((*(clk + PWMCLK_CNTL) & 0x80) != 0)  // Wait for clock to be !BUSY
900       delayMicroseconds (1) ;
901
902     *(clk + PWMCLK_DIV)  = BCM_PASSWORD | (divisor << 12) ;
903
904     *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x11 ;        // Start PWM clock
905     *(pwm + PWM_CONTROL) = pwm_control ;                // restore PWM_CONTROL
906
907     if (wiringPiDebug)
908       printf ("Set     to: %d. Now    : 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;
909   }
910 }
911
912
913 /*
914  * gpioClockSet:
915  *      Set the freuency on a GPIO clock pin
916  *********************************************************************************
917  */
918
919 void gpioClockSet (int pin, int freq)
920 {
921   int divi, divr, divf ;
922
923   pin &= 63 ;
924
925   /**/ if (wiringPiMode == WPI_MODE_PINS)
926     pin = pinToGpio [pin] ;
927   else if (wiringPiMode == WPI_MODE_PHYS)
928     pin = physToGpio [pin] ;
929   else if (wiringPiMode != WPI_MODE_GPIO)
930     return ;
931   
932   divi = 19200000 / freq ;
933   divr = 19200000 % freq ;
934   divf = (int)((double)divr * 4096.0 / 19200000.0) ;
935
936   if (divi > 4095)
937     divi = 4095 ;
938
939   *(clk + gpioToClkCon [pin]) = BCM_PASSWORD | GPIO_CLOCK_SOURCE ;              // Stop GPIO Clock
940   while ((*(clk + gpioToClkCon [pin]) & 0x80) != 0)                             // ... and wait
941     ;
942
943   *(clk + gpioToClkDiv [pin]) = BCM_PASSWORD | (divi << 12) | divf ;            // Set dividers
944   *(clk + gpioToClkCon [pin]) = BCM_PASSWORD | 0x10 | GPIO_CLOCK_SOURCE ;       // Start Clock
945 }
946
947
948 /*
949  * wiringPiFindNode:
950  *      Locate our device node
951  *********************************************************************************
952  */
953
954 struct wiringPiNodeStruct *wiringPiFindNode (int pin)
955 {
956   struct wiringPiNodeStruct *node = wiringPiNodes ;
957
958   while (node != NULL)
959     if ((pin >= node->pinBase) && (pin <= node->pinMax))
960       return node ;
961     else
962       node = node->next ;
963
964   return NULL ;
965 }
966
967
968 /*
969  * wiringPiNewNode:
970  *      Create a new GPIO node into the wiringPi handling system
971  *********************************************************************************
972  */
973
974 static void pinModeDummy             (struct wiringPiNodeStruct *node, int pin, int mode)  { return ; }
975 static void pullUpDnControlDummy     (struct wiringPiNodeStruct *node, int pin, int pud)   { return ; }
976 static int  digitalReadDummy         (struct wiringPiNodeStruct *node, int pin)            { return LOW ; }
977 static void digitalWriteDummy        (struct wiringPiNodeStruct *node, int pin, int value) { return ; }
978 static void pwmWriteDummy            (struct wiringPiNodeStruct *node, int pin, int value) { return ; }
979 static int  analogReadDummy          (struct wiringPiNodeStruct *node, int pin)            { return 0 ; }
980 static void analogWriteDummy         (struct wiringPiNodeStruct *node, int pin, int value) { return ; }
981
982 struct wiringPiNodeStruct *wiringPiNewNode (int pinBase, int numPins)
983 {
984   int    pin ;
985   struct wiringPiNodeStruct *node ;
986
987 // Minimum pin base is 64
988
989   if (pinBase < 64)
990     (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: pinBase of %d is < 64\n", pinBase) ;
991
992 // Check all pins in-case there is overlap:
993
994   for (pin = pinBase ; pin < (pinBase + numPins) ; ++pin)
995     if (wiringPiFindNode (pin) != NULL)
996       (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: Pin %d overlaps with existing definition\n", pin) ;
997
998   node = (struct wiringPiNodeStruct *)calloc (sizeof (struct wiringPiNodeStruct), 1) ;  // calloc zeros
999   if (node == NULL)
1000     (void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: Unable to allocate memory: %s\n", strerror (errno)) ;
1001
1002   node->pinBase         = pinBase ;
1003   node->pinMax          = pinBase + numPins - 1 ;
1004   node->pinMode         = pinModeDummy ;
1005   node->pullUpDnControl = pullUpDnControlDummy ;
1006   node->digitalRead     = digitalReadDummy ;
1007   node->digitalWrite    = digitalWriteDummy ;
1008   node->pwmWrite        = pwmWriteDummy ;
1009   node->analogRead      = analogReadDummy ;
1010   node->analogWrite     = analogWriteDummy ;
1011   node->next            = wiringPiNodes ;
1012   wiringPiNodes         = node ;
1013
1014   return node ;
1015 }
1016
1017
1018 #ifdef notYetReady
1019 /*
1020  * pinED01:
1021  * pinED10:
1022  *      Enables edge-detect mode on a pin - from a 0 to a 1 or 1 to 0
1023  *      Pin must already be in input mode with appropriate pull up/downs set.
1024  *********************************************************************************
1025  */
1026
1027 void pinEnableED01Pi (int pin)
1028 {
1029   pin = pinToGpio [pin & 63] ;
1030 }
1031 #endif
1032
1033
1034 /*
1035  *********************************************************************************
1036  * Core Functions
1037  *********************************************************************************
1038  */
1039
1040 /*
1041  * pinModeAlt:
1042  *      This is an un-documented special to let you set any pin to any mode
1043  *********************************************************************************
1044  */
1045
1046 void pinModeAlt (int pin, int mode)
1047 {
1048   int fSel, shift ;
1049
1050   if ((pin & PI_GPIO_MASK) == 0)                // On-board pin
1051   {
1052     /**/ if (wiringPiMode == WPI_MODE_PINS)
1053       pin = pinToGpio [pin] ;
1054     else if (wiringPiMode == WPI_MODE_PHYS)
1055       pin = physToGpio [pin] ;
1056     else if (wiringPiMode != WPI_MODE_GPIO)
1057       return ;
1058
1059     fSel  = gpioToGPFSEL [pin] ;
1060     shift = gpioToShift  [pin] ;
1061
1062     *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | ((mode & 0x7) << shift) ;
1063   }
1064 }
1065
1066
1067 /*
1068  * pinMode:
1069  *      Sets the mode of a pin to be input, output or PWM output
1070  *********************************************************************************
1071  */
1072
1073 void pinMode (int pin, int mode)
1074 {
1075   int    fSel, shift, alt ;
1076   struct wiringPiNodeStruct *node = wiringPiNodes ;
1077   int origPin = pin ;
1078
1079   if ((pin & PI_GPIO_MASK) == 0)                // On-board pin
1080   {
1081     /**/ if (wiringPiMode == WPI_MODE_PINS)
1082       pin = pinToGpio [pin] ;
1083     else if (wiringPiMode == WPI_MODE_PHYS)
1084       pin = physToGpio [pin] ;
1085     else if (wiringPiMode != WPI_MODE_GPIO)
1086       return ;
1087
1088     softPwmStop  (origPin) ;
1089     softToneStop (origPin) ;
1090
1091     fSel    = gpioToGPFSEL [pin] ;
1092     shift   = gpioToShift  [pin] ;
1093
1094     /**/ if (mode == INPUT)
1095       *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) ; // Sets bits to zero = input
1096     else if (mode == OUTPUT)
1097       *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (1 << shift) ;
1098     else if (mode == SOFT_PWM_OUTPUT)
1099       softPwmCreate (origPin, 0, 100) ;
1100     else if (mode == SOFT_TONE_OUTPUT)
1101       softToneCreate (origPin) ;
1102     else if (mode == PWM_TONE_OUTPUT)
1103     {
1104       pinMode (origPin, PWM_OUTPUT) ;   // Call myself to enable PWM mode
1105       pwmSetMode (PWM_MODE_MS) ;
1106     }
1107     else if (mode == PWM_OUTPUT)
1108     {
1109       if ((alt = gpioToPwmALT [pin]) == 0)      // Not a hardware capable PWM pin
1110         return ;
1111
1112 // Set pin to PWM mode
1113
1114       *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ;
1115       delayMicroseconds (110) ;         // See comments in pwmSetClockWPi
1116
1117       pwmSetMode  (PWM_MODE_BAL) ;      // Pi default mode
1118       pwmSetRange (1024) ;              // Default range of 1024
1119       pwmSetClock (32) ;                // 19.2 / 32 = 600KHz - Also starts the PWM
1120     }
1121     else if (mode == GPIO_CLOCK)
1122     {
1123       if ((alt = gpioToGpClkALT0 [pin]) == 0)   // Not a GPIO_CLOCK pin
1124         return ;
1125
1126 // Set pin to GPIO_CLOCK mode and set the clock frequency to 100KHz
1127
1128       *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ;
1129       delayMicroseconds (110) ;
1130       gpioClockSet      (pin, 100000) ;
1131     }
1132   }
1133   else
1134   {
1135     if ((node = wiringPiFindNode (pin)) != NULL)
1136       node->pinMode (node, pin, mode) ;
1137     return ;
1138   }
1139 }
1140
1141
1142 /*
1143  * pullUpDownCtrl:
1144  *      Control the internal pull-up/down resistors on a GPIO pin
1145  *      The Arduino only has pull-ups and these are enabled by writing 1
1146  *      to a port when in input mode - this paradigm doesn't quite apply
1147  *      here though.
1148  *********************************************************************************
1149  */
1150
1151 void pullUpDnControl (int pin, int pud)
1152 {
1153   struct wiringPiNodeStruct *node = wiringPiNodes ;
1154
1155   if ((pin & PI_GPIO_MASK) == 0)                // On-Board Pin
1156   {
1157     /**/ if (wiringPiMode == WPI_MODE_PINS)
1158       pin = pinToGpio [pin] ;
1159     else if (wiringPiMode == WPI_MODE_PHYS)
1160       pin = physToGpio [pin] ;
1161     else if (wiringPiMode != WPI_MODE_GPIO)
1162       return ;
1163
1164     *(gpio + GPPUD)              = pud & 3 ;            delayMicroseconds (5) ;
1165     *(gpio + gpioToPUDCLK [pin]) = 1 << (pin & 31) ;    delayMicroseconds (5) ;
1166     
1167     *(gpio + GPPUD)              = 0 ;                  delayMicroseconds (5) ;
1168     *(gpio + gpioToPUDCLK [pin]) = 0 ;                  delayMicroseconds (5) ;
1169   }
1170   else                                          // Extension module
1171   {
1172     if ((node = wiringPiFindNode (pin)) != NULL)
1173       node->pullUpDnControl (node, pin, pud) ;
1174     return ;
1175   }
1176 }
1177
1178
1179 /*
1180  * digitalRead:
1181  *      Read the value of a given Pin, returning HIGH or LOW
1182  *********************************************************************************
1183  */
1184
1185 int digitalRead (int pin)
1186 {
1187   char c ;
1188   struct wiringPiNodeStruct *node = wiringPiNodes ;
1189
1190   if ((pin & PI_GPIO_MASK) == 0)                // On-Board Pin
1191   {
1192     /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS) // Sys mode
1193     {
1194       if (sysFds [pin] == -1)
1195         return LOW ;
1196
1197       lseek  (sysFds [pin], 0L, SEEK_SET) ;
1198       read   (sysFds [pin], &c, 1) ;
1199       return (c == '0') ? LOW : HIGH ;
1200     }
1201     else if (wiringPiMode == WPI_MODE_PINS)
1202       pin = pinToGpio [pin] ;
1203     else if (wiringPiMode == WPI_MODE_PHYS)
1204       pin = physToGpio [pin] ;
1205     else if (wiringPiMode != WPI_MODE_GPIO)
1206       return LOW ;
1207
1208     if ((*(gpio + gpioToGPLEV [pin]) & (1 << (pin & 31))) != 0)
1209       return HIGH ;
1210     else
1211       return LOW ;
1212   }
1213   else
1214   {
1215     if ((node = wiringPiFindNode (pin)) == NULL)
1216       return LOW ;
1217     return node->digitalRead (node, pin) ;
1218   }
1219 }
1220
1221
1222 /*
1223  * digitalWrite:
1224  *      Set an output bit
1225  *********************************************************************************
1226  */
1227
1228 void digitalWrite (int pin, int value)
1229 {
1230   struct wiringPiNodeStruct *node = wiringPiNodes ;
1231
1232   if ((pin & PI_GPIO_MASK) == 0)                // On-Board Pin
1233   {
1234     /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS) // Sys mode
1235     {
1236       if (sysFds [pin] != -1)
1237       {
1238         if (value == LOW)
1239           write (sysFds [pin], "0\n", 2) ;
1240         else
1241           write (sysFds [pin], "1\n", 2) ;
1242       }
1243       return ;
1244     }
1245     else if (wiringPiMode == WPI_MODE_PINS)
1246       pin = pinToGpio [pin] ;
1247     else if (wiringPiMode == WPI_MODE_PHYS)
1248       pin = physToGpio [pin] ;
1249     else if (wiringPiMode != WPI_MODE_GPIO)
1250       return ;
1251
1252     if (value == LOW)
1253       *(gpio + gpioToGPCLR [pin]) = 1 << (pin & 31) ;
1254     else
1255       *(gpio + gpioToGPSET [pin]) = 1 << (pin & 31) ;
1256   }
1257   else
1258   {
1259     if ((node = wiringPiFindNode (pin)) != NULL)
1260       node->digitalWrite (node, pin, value) ;
1261   }
1262 }
1263
1264
1265 /*
1266  * pwmWrite:
1267  *      Set an output PWM value
1268  *********************************************************************************
1269  */
1270
1271 void pwmWrite (int pin, int value)
1272 {
1273   struct wiringPiNodeStruct *node = wiringPiNodes ;
1274
1275   if ((pin & PI_GPIO_MASK) == 0)                // On-Board Pin
1276   {
1277     /**/ if (wiringPiMode == WPI_MODE_PINS)
1278       pin = pinToGpio [pin] ;
1279     else if (wiringPiMode == WPI_MODE_PHYS)
1280       pin = physToGpio [pin] ;
1281     else if (wiringPiMode != WPI_MODE_GPIO)
1282       return ;
1283
1284     *(pwm + gpioToPwmPort [pin]) = value ;
1285   }
1286   else
1287   {
1288     if ((node = wiringPiFindNode (pin)) != NULL)
1289       node->pwmWrite (node, pin, value) ;
1290   }
1291 }
1292
1293
1294 /*
1295  * analogRead:
1296  *      Read the analog value of a given Pin. 
1297  *      There is no on-board Pi analog hardware,
1298  *      so this needs to go to a new node.
1299  *********************************************************************************
1300  */
1301
1302 int analogRead (int pin)
1303 {
1304   struct wiringPiNodeStruct *node = wiringPiNodes ;
1305
1306   if ((node = wiringPiFindNode (pin)) == NULL)
1307     return 0 ;
1308   else
1309     return node->analogRead (node, pin) ;
1310 }
1311
1312
1313 /*
1314  * analogWrite:
1315  *      Write the analog value to the given Pin. 
1316  *      There is no on-board Pi analog hardware,
1317  *      so this needs to go to a new node.
1318  *********************************************************************************
1319  */
1320
1321 void analogWrite (int pin, int value)
1322 {
1323   struct wiringPiNodeStruct *node = wiringPiNodes ;
1324
1325   if ((node = wiringPiFindNode (pin)) == NULL)
1326     return ;
1327
1328   node->analogWrite (node, pin, value) ;
1329 }
1330
1331
1332 /*
1333  * pwmToneWrite:
1334  *      Pi Specific.
1335  *      Output the given frequency on the Pi's PWM pin
1336  *********************************************************************************
1337  */
1338
1339 void pwmToneWrite (int pin, int freq)
1340 {
1341   int range ;
1342
1343   if (freq == 0)
1344     pwmWrite (pin, 0) ;             // Off
1345   else
1346   {
1347     range = 600000 / freq ;
1348     pwmSetRange (range) ;
1349     pwmWrite    (pin, freq / 2) ;
1350   }
1351 }
1352
1353
1354
1355 /*
1356  * digitalWriteByte:
1357  *      Pi Specific
1358  *      Write an 8-bit byte to the first 8 GPIO pins - try to do it as
1359  *      fast as possible.
1360  *      However it still needs 2 operations to set the bits, so any external
1361  *      hardware must not rely on seeing a change as there will be a change 
1362  *      to set the outputs bits to zero, then another change to set the 1's
1363  *********************************************************************************
1364  */
1365
1366 void digitalWriteByte (int value)
1367 {
1368   uint32_t pinSet = 0 ;
1369   uint32_t pinClr = 0 ;
1370   int mask = 1 ;
1371   int pin ;
1372
1373   /**/ if (wiringPiMode == WPI_MODE_GPIO_SYS)
1374   {
1375     for (pin = 0 ; pin < 8 ; ++pin)
1376     {
1377       digitalWrite (pin, value & mask) ;
1378       mask <<= 1 ;
1379     }
1380     return ;
1381   }
1382   else
1383   {
1384     for (pin = 0 ; pin < 8 ; ++pin)
1385     {
1386       if ((value & mask) == 0)
1387         pinClr |= (1 << pinToGpio [pin]) ;
1388       else
1389         pinSet |= (1 << pinToGpio [pin]) ;
1390
1391       mask <<= 1 ;
1392     }
1393
1394     *(gpio + gpioToGPCLR [0]) = pinClr ;
1395     *(gpio + gpioToGPSET [0]) = pinSet ;
1396   }
1397 }
1398
1399
1400 /*
1401  * waitForInterrupt:
1402  *      Pi Specific.
1403  *      Wait for Interrupt on a GPIO pin.
1404  *      This is actually done via the /sys/class/gpio interface regardless of
1405  *      the wiringPi access mode in-use. Maybe sometime it might get a better
1406  *      way for a bit more efficiency.
1407  *********************************************************************************
1408  */
1409
1410 int waitForInterrupt (int pin, int mS)
1411 {
1412   int fd, x ;
1413   uint8_t c ;
1414   struct pollfd polls ;
1415
1416   /**/ if (wiringPiMode == WPI_MODE_PINS)
1417     pin = pinToGpio [pin] ;
1418   else if (wiringPiMode == WPI_MODE_PHYS)
1419     pin = physToGpio [pin] ;
1420
1421   if ((fd = sysFds [pin]) == -1)
1422     return -2 ;
1423
1424 // Setup poll structure
1425
1426   polls.fd     = fd ;
1427   polls.events = POLLPRI ;      // Urgent data!
1428
1429 // Wait for it ...
1430
1431   x = poll (&polls, 1, mS) ;
1432
1433 // Do a dummy read to clear the interrupt
1434 //      A one character read appars to be enough.
1435
1436   (void)read (fd, &c, 1) ;
1437
1438   return x ;
1439 }
1440
1441
1442 /*
1443  * interruptHandler:
1444  *      This is a thread and gets started to wait for the interrupt we're
1445  *      hoping to catch. It will call the user-function when the interrupt
1446  *      fires.
1447  *********************************************************************************
1448  */
1449
1450 static void *interruptHandler (void *arg)
1451 {
1452   int myPin ;
1453
1454   (void)piHiPri (55) ;  // Only effective if we run as root
1455
1456   myPin   = pinPass ;
1457   pinPass = -1 ;
1458
1459   for (;;)
1460     if (waitForInterrupt (myPin, -1) > 0)
1461       isrFunctions [myPin] () ;
1462
1463   return NULL ;
1464 }
1465
1466
1467 /*
1468  * wiringPiISR:
1469  *      Pi Specific.
1470  *      Take the details and create an interrupt handler that will do a call-
1471  *      back to the user supplied function.
1472  *********************************************************************************
1473  */
1474
1475 int wiringPiISR (int pin, int mode, void (*function)(void))
1476 {
1477   pthread_t threadId ;
1478   const char *modeS ;
1479   char fName   [64] ;
1480   char  pinS [8] ;
1481   pid_t pid ;
1482   int   count, i ;
1483   char  c ;
1484   int   bcmGpioPin ;
1485
1486   if ((pin < 0) || (pin > 63))
1487     return wiringPiFailure (WPI_FATAL, "wiringPiISR: pin must be 0-63 (%d)\n", pin) ;
1488
1489   /**/ if (wiringPiMode == WPI_MODE_UNINITIALISED)
1490     return wiringPiFailure (WPI_FATAL, "wiringPiISR: wiringPi has not been initialised. Unable to continue.\n") ;
1491   else if (wiringPiMode == WPI_MODE_PINS)
1492     bcmGpioPin = pinToGpio [pin] ;
1493   else if (wiringPiMode == WPI_MODE_PHYS)
1494     bcmGpioPin = physToGpio [pin] ;
1495   else
1496     bcmGpioPin = pin ;
1497
1498 // Now export the pin and set the right edge
1499 //      We're going to use the gpio program to do this, so it assumes
1500 //      a full installation of wiringPi. It's a bit 'clunky', but it
1501 //      is a way that will work when we're running in "Sys" mode, as
1502 //      a non-root user. (without sudo)
1503
1504   if (mode != INT_EDGE_SETUP)
1505   {
1506     /**/ if (mode == INT_EDGE_FALLING)
1507       modeS = "falling" ;
1508     else if (mode == INT_EDGE_RISING)
1509       modeS = "rising" ;
1510     else
1511       modeS = "both" ;
1512
1513     sprintf (pinS, "%d", bcmGpioPin) ;
1514
1515     if ((pid = fork ()) < 0)    // Fail
1516       return wiringPiFailure (WPI_FATAL, "wiringPiISR: fork failed: %s\n", strerror (errno)) ;
1517
1518     if (pid == 0)       // Child, exec
1519     {
1520       /**/ if (access ("/usr/local/bin/gpio", X_OK) == 0)
1521       {
1522         execl ("/usr/local/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ;
1523         return wiringPiFailure (WPI_FATAL, "wiringPiISR: execl failed: %s\n", strerror (errno)) ;
1524       }
1525       else if (access ("/usr/bin/gpio", X_OK) == 0)
1526       {
1527         execl ("/usr/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ;
1528         return wiringPiFailure (WPI_FATAL, "wiringPiISR: execl failed: %s\n", strerror (errno)) ;
1529       }
1530       else
1531         return wiringPiFailure (WPI_FATAL, "wiringPiISR: Can't find gpio program\n") ;
1532     }
1533     else                // Parent, wait
1534       wait (NULL) ;
1535   }
1536
1537 // Now pre-open the /sys/class node - but it may already be open if
1538 //      we are in Sys mode...
1539
1540   if (sysFds [bcmGpioPin] == -1)
1541   {
1542     sprintf (fName, "/sys/class/gpio/gpio%d/value", bcmGpioPin) ;
1543     if ((sysFds [bcmGpioPin] = open (fName, O_RDWR)) < 0)
1544       return wiringPiFailure (WPI_FATAL, "wiringPiISR: unable to open %s: %s\n", fName, strerror (errno)) ;
1545   }
1546
1547 // Clear any initial pending interrupt
1548
1549   ioctl (sysFds [bcmGpioPin], FIONREAD, &count) ;
1550   for (i = 0 ; i < count ; ++i)
1551     read (sysFds [bcmGpioPin], &c, 1) ;
1552
1553   isrFunctions [pin] = function ;
1554
1555   pthread_mutex_lock (&pinMutex) ;
1556     pinPass = pin ;
1557     pthread_create (&threadId, NULL, interruptHandler, NULL) ;
1558     while (pinPass != -1)
1559       delay (1) ;
1560   pthread_mutex_unlock (&pinMutex) ;
1561
1562   return 0 ;
1563 }
1564
1565
1566 /*
1567  * initialiseEpoch:
1568  *      Initialise our start-of-time variable to be the current unix
1569  *      time in milliseconds and microseconds.
1570  *********************************************************************************
1571  */
1572
1573 static void initialiseEpoch (void)
1574 {
1575   struct timeval tv ;
1576
1577   gettimeofday (&tv, NULL) ;
1578   epochMilli = (uint64_t)tv.tv_sec * (uint64_t)1000    + (uint64_t)(tv.tv_usec / 1000) ;
1579   epochMicro = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)(tv.tv_usec) ;
1580 }
1581
1582
1583 /*
1584  * delay:
1585  *      Wait for some number of milliseconds
1586  *********************************************************************************
1587  */
1588
1589 void delay (unsigned int howLong)
1590 {
1591   struct timespec sleeper, dummy ;
1592
1593   sleeper.tv_sec  = (time_t)(howLong / 1000) ;
1594   sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ;
1595
1596   nanosleep (&sleeper, &dummy) ;
1597 }
1598
1599
1600 /*
1601  * delayMicroseconds:
1602  *      This is somewhat intersting. It seems that on the Pi, a single call
1603  *      to nanosleep takes some 80 to 130 microseconds anyway, so while
1604  *      obeying the standards (may take longer), it's not always what we
1605  *      want!
1606  *
1607  *      So what I'll do now is if the delay is less than 100uS we'll do it
1608  *      in a hard loop, watching a built-in counter on the ARM chip. This is
1609  *      somewhat sub-optimal in that it uses 100% CPU, something not an issue
1610  *      in a microcontroller, but under a multi-tasking, multi-user OS, it's
1611  *      wastefull, however we've no real choice )-:
1612  *
1613  *      Plan B: It seems all might not be well with that plan, so changing it
1614  *      to use gettimeofday () and poll on that instead...
1615  *********************************************************************************
1616  */
1617
1618 void delayMicrosecondsHard (unsigned int howLong)
1619 {
1620   struct timeval tNow, tLong, tEnd ;
1621
1622   gettimeofday (&tNow, NULL) ;
1623   tLong.tv_sec  = howLong / 1000000 ;
1624   tLong.tv_usec = howLong % 1000000 ;
1625   timeradd (&tNow, &tLong, &tEnd) ;
1626
1627   while (timercmp (&tNow, &tEnd, <))
1628     gettimeofday (&tNow, NULL) ;
1629 }
1630
1631 void delayMicroseconds (unsigned int howLong)
1632 {
1633   struct timespec sleeper ;
1634   unsigned int uSecs = howLong % 1000000 ;
1635   unsigned int wSecs = howLong / 1000000 ;
1636
1637   /**/ if (howLong ==   0)
1638     return ;
1639   else if (howLong  < 100)
1640     delayMicrosecondsHard (howLong) ;
1641   else
1642   {
1643     sleeper.tv_sec  = wSecs ;
1644     sleeper.tv_nsec = (long)(uSecs * 1000L) ;
1645     nanosleep (&sleeper, NULL) ;
1646   }
1647 }
1648
1649
1650 /*
1651  * millis:
1652  *      Return a number of milliseconds as an unsigned int.
1653  *********************************************************************************
1654  */
1655
1656 unsigned int millis (void)
1657 {
1658   struct timeval tv ;
1659   uint64_t now ;
1660
1661   gettimeofday (&tv, NULL) ;
1662   now  = (uint64_t)tv.tv_sec * (uint64_t)1000 + (uint64_t)(tv.tv_usec / 1000) ;
1663
1664   return (uint32_t)(now - epochMilli) ;
1665 }
1666
1667
1668 /*
1669  * micros:
1670  *      Return a number of microseconds as an unsigned int.
1671  *********************************************************************************
1672  */
1673
1674 unsigned int micros (void)
1675 {
1676   struct timeval tv ;
1677   uint64_t now ;
1678
1679   gettimeofday (&tv, NULL) ;
1680   now  = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)tv.tv_usec ;
1681
1682   return (uint32_t)(now - epochMicro) ;
1683 }
1684
1685
1686 /*
1687  * wiringPiSetup:
1688  *      Must be called once at the start of your program execution.
1689  *
1690  * Default setup: Initialises the system into wiringPi Pin mode and uses the
1691  *      memory mapped hardware directly.
1692  *
1693  * Changed now to revert to "gpio" mode if we're running on a Compute Module.
1694  *********************************************************************************
1695  */
1696
1697 int wiringPiSetup (void)
1698 {
1699   int   fd ;
1700   int   boardRev ;
1701   int   model, rev, mem ;
1702   char *maker ;
1703
1704   if (getenv (ENV_DEBUG) != NULL)
1705     wiringPiDebug = TRUE ;
1706
1707   if (getenv (ENV_CODES) != NULL)
1708     wiringPiReturnCodes = TRUE ;
1709
1710   if (geteuid () != 0)
1711     (void)wiringPiFailure (WPI_FATAL, "wiringPiSetup: Must be root. (Did you forget sudo?)\n") ;
1712
1713   if (wiringPiDebug)
1714     printf ("wiringPi: wiringPiSetup called\n") ;
1715
1716   boardRev = piBoardRev () ;
1717
1718   /**/ if (boardRev == 1)       // A, B, Rev 1, 1.1
1719   {
1720      pinToGpio =  pinToGpioR1 ;
1721     physToGpio = physToGpioR1 ;
1722   }
1723   else                          // A, B, Rev 2, B+, CM
1724   {
1725      pinToGpio =  pinToGpioR2 ;
1726     physToGpio = physToGpioR2 ;
1727   }
1728
1729 // Open the master /dev/memory device
1730
1731   if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0)
1732     return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: Unable to open /dev/mem: %s\n", strerror (errno)) ;
1733
1734 // GPIO:
1735
1736   gpio = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_BASE) ;
1737   if ((int32_t)gpio == -1)
1738     return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (GPIO) failed: %s\n", strerror (errno)) ;
1739
1740 // PWM
1741
1742   pwm = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PWM) ;
1743   if ((int32_t)pwm == -1)
1744     return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PWM) failed: %s\n", strerror (errno)) ;
1745  
1746 // Clock control (needed for PWM)
1747
1748   clk = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, CLOCK_BASE) ;
1749   if ((int32_t)clk == -1)
1750     return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (CLOCK) failed: %s\n", strerror (errno)) ;
1751  
1752 // The drive pads
1753
1754   pads = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PADS) ;
1755   if ((int32_t)pads == -1)
1756     return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PADS) failed: %s\n", strerror (errno)) ;
1757
1758 #ifdef  USE_TIMER
1759 // The system timer
1760
1761   timer = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_TIMER) ;
1762   if ((int32_t)timer == -1)
1763     return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (TIMER) failed: %s\n", strerror (errno)) ;
1764
1765 // Set the timer to free-running, 1MHz.
1766 //      0xF9 is 249, the timer divide is base clock / (divide+1)
1767 //      so base clock is 250MHz / 250 = 1MHz.
1768
1769   *(timer + TIMER_CONTROL) = 0x0000280 ;
1770   *(timer + TIMER_PRE_DIV) = 0x00000F9 ;
1771   timerIrqRaw = timer + TIMER_IRQ_RAW ;
1772 #endif
1773
1774   initialiseEpoch () ;
1775
1776 // If we're running on a compute module, then wiringPi pin numbers don't really many anything...
1777
1778   piBoardId (&model, &rev, &mem, &maker) ;
1779   if (model == PI_MODEL_CM)
1780     wiringPiMode = WPI_MODE_GPIO ;
1781   else
1782     wiringPiMode = WPI_MODE_PINS ;
1783
1784   return 0 ;
1785 }
1786
1787
1788 /*
1789  * wiringPiSetupGpio:
1790  *      Must be called once at the start of your program execution.
1791  *
1792  * GPIO setup: Initialises the system into GPIO Pin mode and uses the
1793  *      memory mapped hardware directly.
1794  *********************************************************************************
1795  */
1796
1797 int wiringPiSetupGpio (void)
1798 {
1799   (void)wiringPiSetup () ;
1800
1801   if (wiringPiDebug)
1802     printf ("wiringPi: wiringPiSetupGpio called\n") ;
1803
1804   wiringPiMode = WPI_MODE_GPIO ;
1805
1806   return 0 ;
1807 }
1808
1809
1810 /*
1811  * wiringPiSetupPhys:
1812  *      Must be called once at the start of your program execution.
1813  *
1814  * Phys setup: Initialises the system into Physical Pin mode and uses the
1815  *      memory mapped hardware directly.
1816  *********************************************************************************
1817  */
1818
1819 int wiringPiSetupPhys (void)
1820 {
1821   (void)wiringPiSetup () ;
1822
1823   if (wiringPiDebug)
1824     printf ("wiringPi: wiringPiSetupPhys called\n") ;
1825
1826   wiringPiMode = WPI_MODE_PHYS ;
1827
1828   return 0 ;
1829 }
1830
1831
1832 /*
1833  * wiringPiSetupSys:
1834  *      Must be called once at the start of your program execution.
1835  *
1836  * Initialisation (again), however this time we are using the /sys/class/gpio
1837  *      interface to the GPIO systems - slightly slower, but always usable as
1838  *      a non-root user, assuming the devices are already exported and setup correctly.
1839  */
1840
1841 int wiringPiSetupSys (void)
1842 {
1843   int boardRev ;
1844   int pin ;
1845   char fName [128] ;
1846
1847   if (getenv (ENV_DEBUG) != NULL)
1848     wiringPiDebug = TRUE ;
1849
1850   if (getenv (ENV_CODES) != NULL)
1851     wiringPiReturnCodes = TRUE ;
1852
1853   if (wiringPiDebug)
1854     printf ("wiringPi: wiringPiSetupSys called\n") ;
1855
1856   boardRev = piBoardRev () ;
1857
1858   if (boardRev == 1)
1859   {
1860      pinToGpio =  pinToGpioR1 ;
1861     physToGpio = physToGpioR1 ;
1862   }
1863   else
1864   {
1865      pinToGpio =  pinToGpioR2 ;
1866     physToGpio = physToGpioR2 ;
1867   }
1868
1869 // Open and scan the directory, looking for exported GPIOs, and pre-open
1870 //      the 'value' interface to speed things up for later
1871   
1872   for (pin = 0 ; pin < 64 ; ++pin)
1873   {
1874     sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
1875     sysFds [pin] = open (fName, O_RDWR) ;
1876   }
1877
1878   initialiseEpoch () ;
1879
1880   wiringPiMode = WPI_MODE_GPIO_SYS ;
1881
1882   return 0 ;
1883 }