chiark / gitweb /
More changes to help reflect usage on Rev 2 / 512MB Raspberry Pi's.
[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 // Pad drive current fiddling
55
56 #undef  DEBUG_PADS
57
58 #include <stdio.h>
59 #include <stdint.h>
60 #include <stdlib.h>
61 #include <ctype.h>
62 #include <poll.h>
63 #include <unistd.h>
64 #include <errno.h>
65 #include <string.h>
66 #include <time.h>
67 #include <fcntl.h>
68 #include <sys/time.h>
69 #include <sys/mman.h>
70 #include <sys/types.h>
71 #include <sys/stat.h>
72
73 #include "wiringPi.h"
74
75 // Function stubs
76
77 void (*pinMode)           (int pin, int mode) ;
78 void (*pullUpDnControl)   (int pin, int pud) ;
79 void (*digitalWrite)      (int pin, int value) ;
80 void (*pwmWrite)          (int pin, int value) ;
81 void (*setPadDrive)       (int group, int value) ;
82 int  (*digitalRead)       (int pin) ;
83 int  (*waitForInterrupt)  (int pin, int mS) ;
84 void (*delayMicroseconds) (unsigned int howLong) ;
85 void (*pwmSetMode)        (int mode) ;
86 void (*pwmSetRange)       (unsigned int range) ;
87 void (*pwmSetClock)       (int divisor) ;
88
89
90 #ifndef TRUE
91 #define TRUE    (1==1)
92 #define FALSE   (1==2)
93 #endif
94
95 // BCM Magic
96
97 #define BCM_PASSWORD            0x5A000000
98
99
100 // Port function select bits
101
102 #define FSEL_INPT               0b000
103 #define FSEL_OUTP               0b001
104 #define FSEL_ALT0               0b100
105 #define FSEL_ALT0               0b100
106 #define FSEL_ALT1               0b101
107 #define FSEL_ALT2               0b110
108 #define FSEL_ALT3               0b111
109 #define FSEL_ALT4               0b011
110 #define FSEL_ALT5               0b010
111
112 // Access from ARM Running Linux
113 //      Take from Gert/Doms code. Some of this is not in the manual
114 //      that I can find )-:
115
116 #define BCM2708_PERI_BASE                          0x20000000
117 #define GPIO_PADS               (BCM2708_PERI_BASE + 0x100000)
118 #define CLOCK_BASE              (BCM2708_PERI_BASE + 0x101000)
119 #define GPIO_BASE               (BCM2708_PERI_BASE + 0x200000)
120 #define GPIO_TIMER              (BCM2708_PERI_BASE + 0x00B000)
121 #define GPIO_PWM                (BCM2708_PERI_BASE + 0x20C000)
122
123 #define PAGE_SIZE               (4*1024)
124 #define BLOCK_SIZE              (4*1024)
125
126 // PWM
127
128 #define PWM_CONTROL 0
129 #define PWM_STATUS  1
130 #define PWM0_RANGE  4
131 #define PWM0_DATA   5
132 #define PWM1_RANGE  8
133 #define PWM1_DATA   9
134
135 #define PWMCLK_CNTL     40
136 #define PWMCLK_DIV      41
137
138 #define PWM1_MS_MODE    0x8000  // Run in MS mode
139 #define PWM1_USEFIFO    0x2000  // Data from FIFO
140 #define PWM1_REVPOLAR   0x1000  // Reverse polarity
141 #define PWM1_OFFSTATE   0x0800  // Ouput Off state
142 #define PWM1_REPEATFF   0x0400  // Repeat last value if FIFO empty
143 #define PWM1_SERIAL     0x0200  // Run in serial mode
144 #define PWM1_ENABLE     0x0100  // Channel Enable
145
146 #define PWM0_MS_MODE    0x0080  // Run in MS mode
147 #define PWM0_USEFIFO    0x0020  // Data from FIFO
148 #define PWM0_REVPOLAR   0x0010  // Reverse polarity
149 #define PWM0_OFFSTATE   0x0008  // Ouput Off state
150 #define PWM0_REPEATFF   0x0004  // Repeat last value if FIFO empty
151 #define PWM0_SERIAL     0x0002  // Run in serial mode
152 #define PWM0_ENABLE     0x0001  // Channel Enable
153
154 // Timer
155
156 #define TIMER_LOAD      (0x400 >> 2)
157 #define TIMER_VALUE     (0x404 >> 2)
158 #define TIMER_CONTROL   (0x408 >> 2)
159 #define TIMER_IRQ_CLR   (0x40C >> 2)
160 #define TIMER_IRQ_RAW   (0x410 >> 2)
161 #define TIMER_IRQ_MASK  (0x414 >> 2)
162 #define TIMER_RELOAD    (0x418 >> 2)
163 #define TIMER_PRE_DIV   (0x41C >> 2)
164 #define TIMER_COUNTER   (0x420 >> 2)
165
166 // Locals to hold pointers to the hardware
167
168 static volatile uint32_t *gpio ;
169 static volatile uint32_t *pwm ;
170 static volatile uint32_t *clk ;
171 static volatile uint32_t *pads ;
172 static volatile uint32_t *timer ;
173 static volatile uint32_t *timerIrqRaw ;
174
175 // Debugging
176
177 static int wiringPiDebug = FALSE ;
178
179 // The BCM2835 has 54 GPIO pins.
180 //      BCM2835 data sheet, Page 90 onwards.
181 //      There are 6 control registers, each control the functions of a block
182 //      of 10 pins.
183 //      Each control register has 10 sets of 3 bits per GPIO pin:
184 //
185 //      000 = GPIO Pin X is an input
186 //      001 = GPIO Pin X is an output
187 //      100 = GPIO Pin X takes alternate function 0
188 //      101 = GPIO Pin X takes alternate function 1
189 //      110 = GPIO Pin X takes alternate function 2
190 //      111 = GPIO Pin X takes alternate function 3
191 //      011 = GPIO Pin X takes alternate function 4
192 //      010 = GPIO Pin X takes alternate function 5
193 //
194 // So the 3 bits for port X are:
195 //      X / 10 + ((X % 10) * 3)
196
197 // sysFds:
198 //      Map a file descriptor from the /sys/class/gpio/gpioX/value
199
200 static int sysFds [64] ;
201
202 // Doing it the Arduino way with lookup tables...
203 //      Yes, it's probably more innefficient than all the bit-twidling, but it
204 //      does tend to make it all a bit clearer. At least to me!
205
206 // pinToGpio:
207 //      Take a Wiring pin (0 through X) and re-map it to the BCM_GPIO pin
208 //      Cope for 2 different board revieions here
209
210 static int *pinToGpio ;
211
212 static int pinToGpioR1 [64] =
213 {
214   17, 18, 21, 22, 23, 24, 25, 4,        // From the Original Wiki - GPIO 0 through 7
215    0,  1,                               // I2C  - SDA0, SCL0
216    8,  7,                               // SPI  - CE1, CE0
217   10,  9, 11,                           // SPI  - MOSI, MISO, SCLK
218   14, 15,                               // UART - Tx, Rx
219
220 // Padding:
221
222       -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,       // ... 31
223   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,       // ... 47
224   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,       // ... 63
225 } ;
226
227 static int pinToGpioR2 [64] =
228 {
229   17, 18, 27, 22, 23, 24, 25, 4,        // From the Original Wiki - GPIO 0 through 7:   wpi  0 -  7
230    2,  3,                               // I2C  - SDA0, SCL0                            wpi  8 -  9
231    8,  7,                               // SPI  - CE1, CE0                              wpi 10 - 11
232   10,  9, 11,                           // SPI  - MOSI, MISO, SCLK                      wpi 12 - 14
233   14, 15,                               // UART - Tx, Rx                                wpi 15 - 16
234   28, 29, 30, 31,                       // New GPIOs 8 though 11                        wpi 17 - 20
235
236 // Padding:
237
238                       -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,       // ... 31
239   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,       // ... 47
240   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,       // ... 63
241 } ;
242
243
244 // gpioToGPFSEL:
245 //      Map a BCM_GPIO pin to it's control port. (GPFSEL 0-5)
246
247 static uint8_t gpioToGPFSEL [] =
248 {
249   0,0,0,0,0,0,0,0,0,0,
250   1,1,1,1,1,1,1,1,1,1,
251   2,2,2,2,2,2,2,2,2,2,
252   3,3,3,3,3,3,3,3,3,3,
253   4,4,4,4,4,4,4,4,4,4,
254   5,5,5,5,5,5,5,5,5,5,
255 } ;
256
257
258 // gpioToShift
259 //      Define the shift up for the 3 bits per pin in each GPFSEL port
260
261 static uint8_t gpioToShift [] =
262 {
263   0,3,6,9,12,15,18,21,24,27,
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 } ;
269
270
271 // gpioToGPSET:
272 //      (Word) offset to the GPIO Set registers for each GPIO pin
273
274 static uint8_t gpioToGPSET [] =
275 {
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,
278 } ;
279
280
281 // gpioToGPCLR:
282 //      (Word) offset to the GPIO Clear registers for each GPIO pin
283
284 static uint8_t gpioToGPCLR [] =
285 {
286   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,
287   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,
288 } ;
289
290
291 // gpioToGPLEV:
292 //      (Word) offset to the GPIO Input level registers for each GPIO pin
293
294 static uint8_t gpioToGPLEV [] =
295 {
296   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,
297   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,
298 } ;
299
300
301 #ifdef notYetReady
302 // gpioToEDS
303 //      (Word) offset to the Event Detect Status
304
305 static uint8_t gpioToEDS [] =
306 {
307   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,
308   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 } ;
310
311 // gpioToREN
312 //      (Word) offset to the Rising edgde ENable register
313
314 static uint8_t gpioToREN [] =
315 {
316   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,
317   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 } ;
319
320 // gpioToFEN
321 //      (Word) offset to the Falling edgde ENable register
322
323 static uint8_t gpioToFEN [] =
324 {
325   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,
326   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,
327 } ;
328 #endif
329
330
331 // gpioToPUDCLK
332 //      (Word) offset to the Pull Up Down Clock regsiter
333
334 #define GPPUD   37
335
336 static uint8_t gpioToPUDCLK [] =
337 {
338   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,
339   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,
340 } ;
341
342
343 // gpioToPwmALT
344 //      the ALT value to put a GPIO pin into PWM mode
345
346 static uint8_t gpioToPwmALT [] =
347 {
348           0,         0,         0,         0,         0,         0,         0,         0,       //  0 ->  7
349           0,         0,         0,         0, FSEL_ALT0, FSEL_ALT0,         0,         0,       //  8 -> 15
350           0,         0, FSEL_ALT5, FSEL_ALT5,         0,         0,         0,         0,       // 16 -> 23
351           0,         0,         0,         0,         0,         0,         0,         0,       // 24 -> 31
352           0,         0,         0,         0,         0,         0,         0,         0,       // 32 -> 39
353   FSEL_ALT0, FSEL_ALT0,         0,         0,         0, FSEL_ALT0,         0,         0,       // 40 -> 47
354           0,         0,         0,         0,         0,         0,         0,         0,       // 48 -> 55
355           0,         0,         0,         0,         0,         0,         0,         0,       // 56 -> 63
356 } ;
357
358 static uint8_t gpioToPwmPort [] =
359 {
360           0,         0,         0,         0,         0,         0,         0,         0,       //  0 ->  7
361           0,         0,         0,         0, PWM0_DATA, PWM1_DATA,         0,         0,       //  8 -> 15
362           0,         0, PWM0_DATA, PWM1_DATA,         0,         0,         0,         0,       // 16 -> 23
363           0,         0,         0,         0,         0,         0,         0,         0,       // 24 -> 31
364           0,         0,         0,         0,         0,         0,         0,         0,       // 32 -> 39
365   PWM0_DATA, PWM1_DATA,         0,         0,         0, PWM1_DATA,         0,         0,       // 40 -> 47
366           0,         0,         0,         0,         0,         0,         0,         0,       // 48 -> 55
367           0,         0,         0,         0,         0,         0,         0,         0,       // 56 -> 63
368
369 } ;
370
371
372 // Time for easy calculations
373
374 static unsigned long long epoch ;
375
376 /*
377  * Functions
378  *********************************************************************************
379  */
380
381
382 /*
383  * wpiPinToGpio:
384  *      Translate a wiringPi Pin number to native GPIO pin number.
385  *      (We don't use this here, prefering to just do the lookup directly,
386  *      but it's been requested!)
387  *********************************************************************************
388  */
389
390 int wpiPinToGpio (int wpiPin)
391 {
392   return pinToGpio [wpiPin & 63] ;
393 }
394
395
396 /*
397  * piBoardRev:
398  *      Return a number representing the hardware revision of the board.
399  *      Revision is currently 1 or 2. -1 is returned on error.
400  *
401  *      Much confusion here )-:
402  *      Seems there ar esome boards with 0000 in them (mistake in manufacture)
403  *      and some board with 0005 in them (another mistake in manufacture).
404  *      So the distinction between boards that I can see is:
405  *      0000 - Error
406  *      0001 - Not used
407  *      0002 - Rev 1
408  *      0003 - Rev 1
409  *      0004 - Rev 2
410  *      0005 - Rev 2
411  *      0006 - Rev 2
412  *      000f - Rev 2 + 512MB
413  *
414  *      A small thorn is the olde style overvolting - that will add in
415  *              1000000
416  *
417  *********************************************************************************
418  */
419
420 int piBoardRev (void)
421 {
422   FILE *cpuFd ;
423   char line [120] ;
424   char *c, lastChar ;
425   static int  boardRev = -1 ;
426
427 // No point checking twice...
428
429   if (boardRev != -1)
430     return boardRev ;
431
432   if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL)
433     return -1 ;
434
435   while (fgets (line, 120, cpuFd) != NULL)
436     if (strncmp (line, "Revision", 8) == 0)
437       break ;
438
439   fclose (cpuFd) ;
440
441   if (line == NULL)
442   {
443     fprintf (stderr, "piBoardRev: Unable to determine board revision from /proc/cpuinfo\n") ;
444     fprintf (stderr, "  (No \"Revision\" line)\n") ;
445     errno = 0 ;
446     return -1 ;
447   }
448   
449   for (c = line ; *c ; ++c)
450     if (isdigit (*c))
451       break ;
452
453   if (!isdigit (*c))
454   {
455     fprintf (stderr, "piBoardRev: Unable to determine board revision from /proc/cpuinfo\n") ;
456     fprintf (stderr, "  (No numeric revision string in: \"%s\"\n", line) ;
457     errno = 0 ;
458     return -1 ;
459   }
460
461 // If you have overvolted the Pi, then it appears that the revision
462 //      has 100000 added to it!
463
464   if (wiringPiDebug)
465     if (strlen (c) != 4)
466       printf ("piboardRev: This Pi has/is overvolted!\n") ;
467
468   lastChar = c [strlen (c) - 2] ;
469
470   /**/ if ((lastChar == '2') || (lastChar == '3'))
471     boardRev = 1 ;
472   else
473     boardRev = 2 ;
474
475 #ifdef  DO_WE_CARE_ABOUT_THIS_NOW
476   else
477   {
478     fprintf (stderr, "WARNING: wiringPi: Unable to determine board revision from \"%d\"\n", r) ;
479     fprintf (stderr, " -> You may want to check:\n") ;
480     fprintf (stderr, " -> http://www.raspberrypi.org/phpBB3/viewtopic.php?p=184410#p184410\n") ;
481     fprintf (stderr, " -> Assuming a Rev 1 board\n") ;
482     boardRev = 1 ;
483   }
484 #endif
485
486   if (wiringPiDebug)
487     printf ("piboardRev: Revision string: %s, board revision: %d\n", c, boardRev) ;
488
489   return boardRev ;
490 }
491
492
493
494 /*
495  * pinMode:
496  *      Sets the mode of a pin to be input, output or PWM output
497  *********************************************************************************
498  */
499
500 void pinModeGpio (int pin, int mode)
501 {
502   int fSel, shift, alt ;
503
504   pin &= 63 ;
505
506   fSel    = gpioToGPFSEL [pin] ;
507   shift   = gpioToShift  [pin] ;
508
509   /**/ if (mode == INPUT)
510     *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) ; // Sets bits to zero = input
511   else if (mode == OUTPUT)
512     *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (1 << shift) ;
513   else if (mode == PWM_OUTPUT)
514   {
515     if ((alt = gpioToPwmALT [pin]) == 0)        // Not a PWM pin
516       return ;
517
518 // Set pin to PWM mode
519
520     *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ;
521
522 //  Page 107 of the BCM Peripherals manual talks about the GPIO clocks,
523 //      but I'm assuming (hoping!) that this applies to other clocks too.
524
525     *(pwm + PWM_CONTROL) = 0 ;                          // Stop PWM
526     *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x01 ;        // Stop PWM Clock
527       delayMicroseconds (110) ; // See comments in pwmSetClockWPi
528
529     (void)*(pwm + PWM_CONTROL) ;
530     while ((*(pwm + PWM_CONTROL) & 0x80) != 0)  // Wait for clock to be !BUSY
531       delayMicroseconds (1) ;
532
533     *(clk + PWMCLK_DIV)  = BCM_PASSWORD | (32 << 12) ;  // set pwm div to 32 (19.2/32 = 600KHz)
534     *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x11 ;        // enable clk
535
536 // Default range regsiter of 1024
537
538     *(pwm + PWM0_DATA) = 0 ; *(pwm + PWM0_RANGE) = 1024 ;
539     *(pwm + PWM1_DATA) = 0 ; *(pwm + PWM1_RANGE) = 1024 ;
540
541 // Enable PWMs in balanced mode (default)
542
543     *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ;
544   }
545
546 // When we change mode of any pin, we remove the pull up/downs
547 //      Or we used to... Hm. Commented out now because for some wieird reason,
548 //      it seems to block subsequent attempts to set the pull up/downs and I've
549 //      not quite gotten to the bottom of why this happens
550 //      The down-side is that the pull up/downs are rememberd in the SoC between
551 //      power cycles, so it's going to be a good idea to explicitly set them in
552 //      any new code.
553 //
554 //  pullUpDnControl (pin, PUD_OFF) ;
555
556 }
557
558 void pinModeWPi (int pin, int mode)
559 {
560   pinModeGpio (pinToGpio [pin & 63], mode) ;
561 }
562
563 void pinModeSys (int pin, int mode)
564 {
565   return ;
566 }
567
568
569 /*
570  * pwmControl:
571  *      Allow the user to control some of the PWM functions
572  *********************************************************************************
573  */
574
575 void pwmSetModeWPi (int mode)
576 {
577   if (mode == PWM_MODE_MS)
578     *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE | PWM0_MS_MODE | PWM1_MS_MODE ;
579   else
580     *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ;
581 }
582
583 void pwmSetModeSys (int mode)
584 {
585   return ;
586 }
587
588
589 void pwmSetRangeWPi (unsigned int range)
590 {
591   *(pwm + PWM0_RANGE) = range ; delayMicroseconds (10) ;
592   *(pwm + PWM1_RANGE) = range ; delayMicroseconds (10) ;
593 }
594
595 void pwmSetRangeSys (unsigned int range)
596 {
597   return ;
598 }
599
600 /*
601  * pwmSetClockWPi:
602  *      Set/Change the PWM clock. Originally my code, but changed
603  *      (for the better!) by Chris Hall, <chris@kchall.plus.com>
604  *      after further study of the manual and testing with a 'scope
605  *********************************************************************************
606  */
607
608 void pwmSetClockWPi (int divisor)
609 {
610   unsigned int pwm_control ;
611   divisor &= 4095 ;
612
613   if (wiringPiDebug)
614     printf ("Setting to: %d. Current: 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;
615
616   pwm_control = *(pwm + PWM_CONTROL) ;          // preserve PWM_CONTROL
617
618 // We need to stop PWM prior to stopping PWM clock in MS mode otherwise BUSY
619 // stays high.
620
621   *(pwm + PWM_CONTROL) = 0 ;                    // Stop PWM
622
623 // Stop PWM clock before changing divisor. The delay after this does need to
624 // this big (95uS occasionally fails, 100uS OK), it's almost as though the BUSY
625 // flag is not working properly in balanced mode. Without the delay when DIV is
626 // adjusted the clock sometimes switches to very slow, once slow further DIV
627 // adjustments do nothing and it's difficult to get out of this mode.
628
629   *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x01 ;  // Stop PWM Clock
630     delayMicroseconds (110) ;                   // prevents clock going sloooow
631
632   while ((*(pwm + PWM_CONTROL) & 0x80) != 0)    // Wait for clock to be !BUSY
633     delayMicroseconds (1) ;
634
635   *(clk + PWMCLK_DIV)  = BCM_PASSWORD | (divisor << 12) ;
636
637   *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x11 ;  // Start PWM clock
638   *(pwm + PWM_CONTROL) = pwm_control ;          // restore PWM_CONTROL
639
640   if (wiringPiDebug)
641     printf ("Set     to: %d. Now    : 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;
642 }
643
644 void pwmSetClockSys (int divisor)
645 {
646   return ;
647 }
648
649
650 #ifdef notYetReady
651 /*
652  * pinED01:
653  * pinED10:
654  *      Enables edge-detect mode on a pin - from a 0 to a 1 or 1 to 0
655  *      Pin must already be in input mode with appropriate pull up/downs set.
656  *********************************************************************************
657  */
658
659 void pinEnableED01Pi (int pin)
660 {
661   pin = pinToGpio [pin & 63] ;
662 }
663 #endif
664
665
666
667 /*
668  * digitalWrite:
669  *      Set an output bit
670  *********************************************************************************
671  */
672
673 void digitalWriteWPi (int pin, int value)
674 {
675   pin = pinToGpio [pin & 63] ;
676
677   if (value == LOW)
678     *(gpio + gpioToGPCLR [pin]) = 1 << (pin & 31) ;
679   else
680     *(gpio + gpioToGPSET [pin]) = 1 << (pin & 31) ;
681 }
682
683 void digitalWriteGpio (int pin, int value)
684 {
685   pin &= 63 ;
686
687   if (value == LOW)
688     *(gpio + gpioToGPCLR [pin]) = 1 << (pin & 31) ;
689   else
690     *(gpio + gpioToGPSET [pin]) = 1 << (pin & 31) ;
691 }
692
693 void digitalWriteSys (int pin, int value)
694 {
695   pin &= 63 ;
696
697   if (sysFds [pin] != -1)
698   {
699     if (value == LOW)
700       write (sysFds [pin], "0\n", 2) ;
701     else
702       write (sysFds [pin], "1\n", 2) ;
703   }
704 }
705
706
707 /*
708  * pwmWrite:
709  *      Set an output PWM value
710  *********************************************************************************
711  */
712
713 void pwmWriteGpio (int pin, int value)
714 {
715   int port ;
716
717   pin  = pin & 63 ;
718   port = gpioToPwmPort [pin] ;
719
720   *(pwm + port) = value ;
721 }
722
723 void pwmWriteWPi (int pin, int value)
724 {
725   pwmWriteGpio (pinToGpio [pin & 63], value) ;
726 }
727
728 void pwmWriteSys (int pin, int value)
729 {
730   return ;
731 }
732
733
734 /*
735  * setPadDrive:
736  *      Set the PAD driver value
737  *********************************************************************************
738  */
739
740 void setPadDriveWPi (int group, int value)
741 {
742   uint32_t wrVal ;
743
744   if ((group < 0) || (group > 2))
745     return ;
746
747   wrVal = BCM_PASSWORD | 0x18 | (value & 7) ;
748   *(pads + group + 11) = wrVal ;
749
750 #ifdef  DEBUG_PADS
751   printf ("setPadDrive: Group: %d, value: %d (%08X)\n", group, value, wrVal) ;
752   printf ("Read : %08X\n", *(pads + group + 11)) ;
753 #endif
754 }
755
756 void setPadDriveGpio (int group, int value)
757 {
758   setPadDriveWPi (group, value) ;
759 }
760
761 void setPadDriveSys (int group, int value)
762 {
763   return ;
764 }
765
766
767 /*
768  * digitalRead:
769  *      Read the value of a given Pin, returning HIGH or LOW
770  *********************************************************************************
771  */
772
773 int digitalReadWPi (int pin)
774 {
775   pin = pinToGpio [pin & 63] ;
776
777   if ((*(gpio + gpioToGPLEV [pin]) & (1 << (pin & 31))) != 0)
778     return HIGH ;
779   else
780     return LOW ;
781 }
782
783 int digitalReadGpio (int pin)
784 {
785   pin &= 63 ;
786
787   if ((*(gpio + gpioToGPLEV [pin]) & (1 << (pin & 31))) != 0)
788     return HIGH ;
789   else
790     return LOW ;
791 }
792
793 int digitalReadSys (int pin)
794 {
795   char c ;
796
797   pin &= 63 ;
798
799   if (sysFds [pin] == -1)
800     return 0 ;
801
802   lseek (sysFds [pin], 0L, SEEK_SET) ;
803   read  (sysFds [pin], &c, 1) ;
804   return (c == '0') ? 0 : 1 ;
805 }
806
807
808 /*
809  * pullUpDownCtrl:
810  *      Control the internal pull-up/down resistors on a GPIO pin
811  *      The Arduino only has pull-ups and these are enabled by writing 1
812  *      to a port when in input mode - this paradigm doesn't quite apply
813  *      here though.
814  *********************************************************************************
815  */
816
817 void pullUpDnControlGpio (int pin, int pud)
818 {
819   pin &= 63 ;
820   pud &=  3 ;
821
822   *(gpio + GPPUD)              = pud ;                  delayMicroseconds (5) ;
823   *(gpio + gpioToPUDCLK [pin]) = 1 << (pin & 31) ;      delayMicroseconds (5) ;
824   
825   *(gpio + GPPUD)              = 0 ;                    delayMicroseconds (5) ;
826   *(gpio + gpioToPUDCLK [pin]) = 0 ;                    delayMicroseconds (5) ;
827 }
828
829 void pullUpDnControlWPi (int pin, int pud)
830 {
831   pullUpDnControlGpio (pinToGpio [pin & 63], pud) ;
832 }
833
834 void pullUpDnControlSys (int pin, int pud)
835 {
836   return ;
837 }
838
839
840 /*
841  * waitForInterrupt:
842  *      Wait for Interrupt on a GPIO pin.
843  *      This is actually done via the /sys/class/gpio interface regardless of
844  *      the wiringPi access mode in-use. Maybe sometime it might get a better
845  *      way for a bit more efficiency.
846  *********************************************************************************
847  */
848
849 int waitForInterruptSys (int pin, int mS)
850 {
851   int fd, x ;
852   char buf [8] ;
853   struct pollfd polls ;
854
855   if ((fd = sysFds [pin & 63]) == -1)
856     return -2 ;
857
858 // Do a dummy read
859
860   x = read (fd, buf, 6) ;
861   if (x < 0)
862     return x ;
863
864 // And seek
865
866   lseek (fd, 0, SEEK_SET) ;
867
868 // Setup poll structure
869
870   polls.fd     = fd ;
871   polls.events = POLLPRI ;      // Urgent data!
872
873 // Wait for it ...
874
875   return poll (&polls, 1, mS) ;
876 }
877
878 int waitForInterruptWPi (int pin, int mS)
879 {
880   return waitForInterruptSys (pinToGpio [pin & 63], mS) ;
881 }
882
883 int waitForInterruptGpio (int pin, int mS)
884 {
885   return waitForInterruptSys (pin, mS) ;
886 }
887
888
889 /*
890  * delay:
891  *      Wait for some number of milli seconds
892  *********************************************************************************
893  */
894
895 void delay (unsigned int howLong)
896 {
897   struct timespec sleeper, dummy ;
898
899   sleeper.tv_sec  = (time_t)(howLong / 1000) ;
900   sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ;
901
902   nanosleep (&sleeper, &dummy) ;
903 }
904
905
906 /*
907  * delayMicroseconds:
908  *      This is somewhat intersting. It seems that on the Pi, a single call
909  *      to nanosleep takes some 80 to 130 microseconds anyway, so while
910  *      obeying the standards (may take longer), it's not always what we
911  *      want!
912  *
913  *      So what I'll do now is if the delay is less than 100uS we'll do it
914  *      in a hard loop, watching a built-in counter on the ARM chip. This is
915  *      somewhat sub-optimal in that it uses 100% CPU, something not an issue
916  *      in a microcontroller, but under a multi-tasking, multi-user OS, it's
917  *      wastefull, however we've no real choice )-:
918  *********************************************************************************
919  */
920
921 void delayMicrosecondsSys (unsigned int howLong)
922 {
923   struct timespec sleeper, dummy ;
924
925   sleeper.tv_sec  = 0 ;
926   sleeper.tv_nsec = (long)(howLong * 1000) ;
927
928   nanosleep (&sleeper, &dummy) ;
929 }
930
931 void delayMicrosecondsHard (unsigned int howLong)
932 {
933   *(timer + TIMER_LOAD)    = howLong ;
934   *(timer + TIMER_IRQ_CLR) = 0 ;
935
936   while (*timerIrqRaw == 0)
937     ;
938 }
939
940 void delayMicrosecondsWPi (unsigned int howLong)
941 {
942   struct timespec sleeper, dummy ;
943
944   /**/ if (howLong ==   0)
945     return ;
946   else if (howLong  < 100)
947     delayMicrosecondsHard (howLong) ;
948   else
949   {
950     sleeper.tv_sec  = 0 ;
951     sleeper.tv_nsec = (long)(howLong * 1000) ;
952     nanosleep (&sleeper, &dummy) ;
953   }
954 }
955
956
957 /*
958  * millis:
959  *      Return a number of milliseconds as an unsigned int.
960  *********************************************************************************
961  */
962
963 unsigned int millis (void)
964 {
965   struct timeval tv ;
966   unsigned long long t1 ;
967
968   gettimeofday (&tv, NULL) ;
969
970   t1 = (tv.tv_sec * 1000000 + tv.tv_usec) / 1000 ;
971
972   return (uint32_t)(t1 - epoch) ;
973 }
974
975
976 /*
977  * wiringPiSetup:
978  *      Must be called once at the start of your program execution.
979  *
980  * Default setup: Initialises the system into wiringPi Pin mode and uses the
981  *      memory mapped hardware directly.
982  *********************************************************************************
983  */
984
985 int wiringPiSetup (void)
986 {
987   int      fd ;
988   int      boardRev ;
989   uint8_t *gpioMem, *pwmMem, *clkMem, *padsMem, *timerMem ;
990   struct timeval tv ;
991
992   if (getenv ("WIRINGPI_DEBUG") != NULL)
993     wiringPiDebug = TRUE ;
994
995   if (wiringPiDebug)
996     printf ("wiringPi: wiringPiSetup called\n") ;
997
998             pinMode =           pinModeWPi ;
999     pullUpDnControl =   pullUpDnControlWPi ;
1000        digitalWrite =      digitalWriteWPi ;
1001            pwmWrite =          pwmWriteWPi ;
1002         setPadDrive =       setPadDriveWPi ;
1003         digitalRead =       digitalReadWPi ;
1004    waitForInterrupt =  waitForInterruptWPi ;
1005   delayMicroseconds = delayMicrosecondsWPi ;
1006          pwmSetMode =        pwmSetModeWPi ;
1007         pwmSetRange =       pwmSetRangeWPi ;
1008         pwmSetClock =       pwmSetClockWPi ;
1009   
1010   if ((boardRev = piBoardRev ()) < 0)
1011     return -1 ;
1012
1013   if (boardRev == 1)
1014     pinToGpio = pinToGpioR1 ;
1015   else
1016     pinToGpio = pinToGpioR2 ;
1017
1018 // Open the master /dev/memory device
1019
1020   if ((fd = open ("/dev/mem", O_RDWR | O_SYNC) ) < 0)
1021   {
1022     fprintf (stderr, "wiringPiSetup: Unable to open /dev/mem: %s\n", strerror (errno)) ;
1023     return -1 ;
1024   }
1025
1026 // GPIO:
1027
1028 // Allocate 2 pages - 1 ...
1029
1030   if ((gpioMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
1031   {
1032     fprintf (stderr, "wiringPiSetup: malloc failed: %s\n", strerror (errno)) ;
1033     return -1 ;
1034   }
1035
1036 // ... presumably to make sure we can round it up to a whole page size
1037
1038   if (((uint32_t)gpioMem % PAGE_SIZE) != 0)
1039     gpioMem += PAGE_SIZE - ((uint32_t)gpioMem % PAGE_SIZE) ;
1040
1041   gpio = (uint32_t *)mmap((caddr_t)gpioMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, GPIO_BASE) ;
1042
1043   if ((int32_t)gpio < 0)
1044   {
1045     fprintf (stderr, "wiringPiSetup: mmap failed: %s\n", strerror (errno)) ;
1046     return -1 ;
1047   }
1048
1049 // PWM
1050
1051   if ((pwmMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
1052   {
1053     fprintf (stderr, "wiringPiSetup: pwmMem malloc failed: %s\n", strerror (errno)) ;
1054     return -1 ;
1055   }
1056
1057   if (((uint32_t)pwmMem % PAGE_SIZE) != 0)
1058     pwmMem += PAGE_SIZE - ((uint32_t)pwmMem % PAGE_SIZE) ;
1059
1060   pwm = (uint32_t *)mmap(pwmMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, GPIO_PWM) ;
1061
1062   if ((int32_t)pwm < 0)
1063   {
1064     fprintf (stderr, "wiringPiSetup: mmap failed (pwm): %s\n", strerror (errno)) ;
1065     return -1 ;
1066   }
1067  
1068 // Clock control (needed for PWM)
1069
1070   if ((clkMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
1071   {
1072     fprintf (stderr, "wiringPiSetup: clkMem malloc failed: %s\n", strerror (errno)) ;
1073     return -1 ;
1074   }
1075
1076   if (((uint32_t)clkMem % PAGE_SIZE) != 0)
1077     clkMem += PAGE_SIZE - ((uint32_t)clkMem % PAGE_SIZE) ;
1078
1079   clk = (uint32_t *)mmap(clkMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, CLOCK_BASE) ;
1080
1081   if ((int32_t)clk < 0)
1082   {
1083     fprintf (stderr, "wiringPiSetup: mmap failed (clk): %s\n", strerror (errno)) ;
1084     return -1 ;
1085   }
1086  
1087 // The drive pads
1088
1089   if ((padsMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
1090   {
1091     fprintf (stderr, "wiringPiSetup: padsMem malloc failed: %s\n", strerror (errno)) ;
1092     return -1 ;
1093   }
1094
1095   if (((uint32_t)padsMem % PAGE_SIZE) != 0)
1096     padsMem += PAGE_SIZE - ((uint32_t)padsMem % PAGE_SIZE) ;
1097
1098   pads = (uint32_t *)mmap(padsMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, GPIO_PADS) ;
1099
1100   if ((int32_t)pads < 0)
1101   {
1102     fprintf (stderr, "wiringPiSetup: mmap failed (pads): %s\n", strerror (errno)) ;
1103     return -1 ;
1104   }
1105
1106 #ifdef  DEBUG_PADS
1107   printf ("Checking pads @ 0x%08X\n", (unsigned int)pads) ;
1108   printf (" -> %08X %08X %08X\n", *(pads + 11), *(pads + 12), *(pads + 13)) ;
1109 #endif
1110
1111 // The system timer
1112
1113   if ((timerMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
1114   {
1115     fprintf (stderr, "wiringPiSetup: timerMem malloc failed: %s\n", strerror (errno)) ;
1116     return -1 ;
1117   }
1118
1119   if (((uint32_t)timerMem % PAGE_SIZE) != 0)
1120     timerMem += PAGE_SIZE - ((uint32_t)timerMem % PAGE_SIZE) ;
1121
1122   timer = (uint32_t *)mmap(timerMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, GPIO_TIMER) ;
1123
1124   if ((int32_t)timer < 0)
1125   {
1126     fprintf (stderr, "wiringPiSetup: mmap failed (timer): %s\n", strerror (errno)) ;
1127     return -1 ;
1128   }
1129
1130 // Set the timer to free-running, 1MHz.
1131 //      0xF9 is 249, the timer divide is base clock / (divide+1)
1132 //      so base clock is 250MHz / 250 = 1MHz.
1133
1134   *(timer + TIMER_CONTROL) = 0x0000280 ;
1135   *(timer + TIMER_PRE_DIV) = 0x00000F9 ;
1136   timerIrqRaw = timer + TIMER_IRQ_RAW ;
1137
1138 // Initialise our epoch for millis()
1139
1140   gettimeofday (&tv, NULL) ;
1141   epoch = (tv.tv_sec * 1000000 + tv.tv_usec) / 1000 ;
1142
1143   return 0 ;
1144 }
1145
1146
1147 /*
1148  * wiringPiSetupGpio:
1149  *      Must be called once at the start of your program execution.
1150  *
1151  * GPIO setup: Initialises the system into GPIO Pin mode and uses the
1152  *      memory mapped hardware directly.
1153  *********************************************************************************
1154  */
1155
1156 int wiringPiSetupGpio (void)
1157 {
1158   int x  ;
1159
1160   if (wiringPiDebug)
1161     printf ("wiringPi: wiringPiSetupGpio called\n") ;
1162
1163   if ((x = wiringPiSetup ()) < 0)
1164     return x ;
1165
1166             pinMode =           pinModeGpio ;
1167     pullUpDnControl =   pullUpDnControlGpio ;
1168        digitalWrite =      digitalWriteGpio ;
1169            pwmWrite =          pwmWriteGpio ;
1170         setPadDrive =       setPadDriveGpio ;
1171         digitalRead =       digitalReadGpio ;
1172    waitForInterrupt =  waitForInterruptGpio ;
1173   delayMicroseconds = delayMicrosecondsWPi ;    // Same
1174          pwmSetMode =        pwmSetModeWPi ;
1175         pwmSetRange =       pwmSetRangeWPi ;
1176         pwmSetClock =       pwmSetClockWPi ;
1177
1178   return 0 ;
1179 }
1180
1181
1182 /*
1183  * wiringPiSetupSys:
1184  *      Must be called once at the start of your program execution.
1185  *
1186  * Initialisation (again), however this time we are using the /sys/class/gpio
1187  *      interface to the GPIO systems - slightly slower, but always usable as
1188  *      a non-root user, assuming the devices are already exported and setup correctly.
1189  */
1190
1191 int wiringPiSetupSys (void)
1192 {
1193   int pin ;
1194   struct timeval tv ;
1195   char fName [128] ;
1196
1197   if (wiringPiDebug)
1198     printf ("wiringPi: wiringPiSetupSys called\n") ;
1199
1200             pinMode =           pinModeSys ;
1201     pullUpDnControl =   pullUpDnControlSys ;
1202        digitalWrite =      digitalWriteSys ;
1203            pwmWrite =          pwmWriteSys ;
1204         setPadDrive =       setPadDriveSys ;
1205         digitalRead =       digitalReadSys ;
1206    waitForInterrupt =  waitForInterruptSys ;
1207   delayMicroseconds = delayMicrosecondsSys ;
1208          pwmSetMode =        pwmSetModeSys ;
1209         pwmSetRange =       pwmSetRangeSys ;
1210         pwmSetClock =       pwmSetClockSys ;
1211
1212
1213 // Open and scan the directory, looking for exported GPIOs, and pre-open
1214 //      the 'value' interface to speed things up for later
1215   
1216   for (pin = 0 ; pin < 64 ; ++pin)
1217   {
1218     sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
1219     sysFds [pin] = open (fName, O_RDWR) ;
1220   }
1221
1222 // Initialise the epoch for mills() ...
1223
1224   gettimeofday (&tv, NULL) ;
1225   epoch = (tv.tv_sec * 1000000 + tv.tv_usec) / 1000 ;
1226
1227   return 0 ;
1228 }