wiringpi-ruby 2.0.0 → 2.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/ext/wiringpi/WiringPi/devLib/ds1302.c +240 -0
- data/ext/wiringpi/WiringPi/devLib/ds1302.h +44 -0
- data/ext/wiringpi/WiringPi/devLib/font.h +2577 -0
- data/ext/wiringpi/WiringPi/devLib/gertboard.c +164 -0
- data/ext/wiringpi/WiringPi/devLib/gertboard.h +45 -0
- data/ext/wiringpi/WiringPi/devLib/lcd.c +495 -0
- data/ext/wiringpi/WiringPi/devLib/lcd.h +52 -0
- data/ext/wiringpi/WiringPi/devLib/lcd128x64.c +673 -0
- data/ext/wiringpi/WiringPi/devLib/lcd128x64.h +39 -0
- data/ext/wiringpi/WiringPi/devLib/maxdetect.c +238 -0
- data/ext/wiringpi/WiringPi/devLib/maxdetect.h +40 -0
- data/ext/wiringpi/WiringPi/devLib/piFace.c +112 -0
- data/ext/wiringpi/WiringPi/devLib/piFace.h +32 -0
- data/ext/wiringpi/WiringPi/devLib/piFaceOld.c +177 -0
- data/ext/wiringpi/WiringPi/devLib/piGlow.c +118 -0
- data/ext/wiringpi/WiringPi/devLib/piGlow.h +45 -0
- data/ext/wiringpi/WiringPi/devLib/piNes.c +113 -0
- data/ext/wiringpi/WiringPi/devLib/piNes.h +45 -0
- data/ext/wiringpi/WiringPi/devLib/scrollPhat.c +430 -0
- data/ext/wiringpi/WiringPi/devLib/scrollPhat.h +39 -0
- data/ext/wiringpi/WiringPi/devLib/scrollPhatFont.h +544 -0
- data/ext/wiringpi/WiringPi/examples/Gertboard/7segments.c +221 -0
- data/ext/wiringpi/WiringPi/examples/Gertboard/buttons.c +83 -0
- data/ext/wiringpi/WiringPi/examples/Gertboard/gertboard.c +96 -0
- data/ext/wiringpi/WiringPi/examples/Gertboard/record.c +60 -0
- data/ext/wiringpi/WiringPi/examples/Gertboard/temperature.c +78 -0
- data/ext/wiringpi/WiringPi/examples/Gertboard/voltmeter.c +73 -0
- data/ext/wiringpi/WiringPi/examples/Gertboard/vumeter.c +152 -0
- data/ext/wiringpi/WiringPi/examples/PiFace/blink.c +59 -0
- data/ext/wiringpi/WiringPi/examples/PiFace/buttons.c +103 -0
- data/ext/wiringpi/WiringPi/examples/PiFace/ladder.c +337 -0
- data/ext/wiringpi/WiringPi/examples/PiFace/metro.c +111 -0
- data/ext/wiringpi/WiringPi/examples/PiFace/motor.c +120 -0
- data/ext/wiringpi/WiringPi/examples/PiFace/reaction.c +194 -0
- data/ext/wiringpi/WiringPi/examples/PiGlow/piGlow0.c +51 -0
- data/ext/wiringpi/WiringPi/examples/PiGlow/piGlow1.c +258 -0
- data/ext/wiringpi/WiringPi/examples/PiGlow/piglow.c +176 -0
- data/ext/wiringpi/WiringPi/examples/blink-thread.c +61 -0
- data/ext/wiringpi/WiringPi/examples/blink.c +48 -0
- data/ext/wiringpi/WiringPi/examples/blink12.c +111 -0
- data/ext/wiringpi/WiringPi/examples/blink12drcs.c +125 -0
- data/ext/wiringpi/WiringPi/examples/blink6drcs.c +115 -0
- data/ext/wiringpi/WiringPi/examples/blink8-drcn.c +61 -0
- data/ext/wiringpi/WiringPi/examples/blink8.c +57 -0
- data/ext/wiringpi/WiringPi/examples/clock.c +201 -0
- data/ext/wiringpi/WiringPi/examples/delayTest.c +102 -0
- data/ext/wiringpi/WiringPi/examples/ds1302.c +238 -0
- data/ext/wiringpi/WiringPi/examples/header.h +23 -0
- data/ext/wiringpi/WiringPi/examples/isr-osc.c +118 -0
- data/ext/wiringpi/WiringPi/examples/isr.c +110 -0
- data/ext/wiringpi/WiringPi/examples/lcd-adafruit.c +347 -0
- data/ext/wiringpi/WiringPi/examples/lcd.c +286 -0
- data/ext/wiringpi/WiringPi/examples/lowPower.c +68 -0
- data/ext/wiringpi/WiringPi/examples/max31855.c +60 -0
- data/ext/wiringpi/WiringPi/examples/nes.c +67 -0
- data/ext/wiringpi/WiringPi/examples/okLed.c +82 -0
- data/ext/wiringpi/WiringPi/examples/pwm.c +58 -0
- data/ext/wiringpi/WiringPi/examples/q2w/binary.c +79 -0
- data/ext/wiringpi/WiringPi/examples/q2w/blink-io.c +61 -0
- data/ext/wiringpi/WiringPi/examples/q2w/blink.c +50 -0
- data/ext/wiringpi/WiringPi/examples/q2w/bright.c +59 -0
- data/ext/wiringpi/WiringPi/examples/q2w/button.c +63 -0
- data/ext/wiringpi/WiringPi/examples/q2w/volts.c +62 -0
- data/ext/wiringpi/WiringPi/examples/rht03.c +86 -0
- data/ext/wiringpi/WiringPi/examples/scrollPhat/scphat.c +230 -0
- data/ext/wiringpi/WiringPi/examples/scrollPhat/test.c +115 -0
- data/ext/wiringpi/WiringPi/examples/serialRead.c +48 -0
- data/ext/wiringpi/WiringPi/examples/serialTest.c +75 -0
- data/ext/wiringpi/WiringPi/examples/servo.c +57 -0
- data/ext/wiringpi/WiringPi/examples/softPwm.c +89 -0
- data/ext/wiringpi/WiringPi/examples/softTone.c +54 -0
- data/ext/wiringpi/WiringPi/examples/speed.c +95 -0
- data/ext/wiringpi/WiringPi/examples/spiSpeed.c +118 -0
- data/ext/wiringpi/WiringPi/examples/wfi.c +161 -0
- data/ext/wiringpi/WiringPi/gpio/gpio.c +1538 -0
- data/ext/wiringpi/WiringPi/gpio/readall.c +403 -0
- data/ext/wiringpi/WiringPi/version.h +3 -0
- data/ext/wiringpi/WiringPi/wiringPi/ads1115.c +293 -0
- data/ext/wiringpi/WiringPi/wiringPi/ads1115.h +55 -0
- data/ext/wiringpi/WiringPi/wiringPi/bmp180.c +237 -0
- data/ext/wiringpi/WiringPi/wiringPi/bmp180.h +34 -0
- data/ext/wiringpi/WiringPi/wiringPi/drcNet.c +405 -0
- data/ext/wiringpi/WiringPi/wiringPi/drcNet.h +42 -0
- data/ext/wiringpi/WiringPi/wiringPi/drcSerial.c +196 -0
- data/ext/wiringpi/WiringPi/wiringPi/drcSerial.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/ds18b20.c +146 -0
- data/ext/wiringpi/WiringPi/wiringPi/ds18b20.h +34 -0
- data/ext/wiringpi/WiringPi/wiringPi/htu21d.c +150 -0
- data/ext/wiringpi/WiringPi/wiringPi/htu21d.h +34 -0
- data/ext/wiringpi/WiringPi/wiringPi/max31855.c +99 -0
- data/ext/wiringpi/WiringPi/wiringPi/max31855.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/max5322.c +84 -0
- data/ext/wiringpi/WiringPi/wiringPi/max5322.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23008.c +149 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23008.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23016.c +164 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23016.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23016reg.h +48 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23017.c +195 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23017.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23s08.c +188 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23s08.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23s17.c +235 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23s17.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23x08.h +73 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23x0817.h +87 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp3002.c +76 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp3002.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp3004.c +76 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp3004.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp3422.c +125 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp3422.h +43 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp4802.c +76 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp4802.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/pcf8574.c +126 -0
- data/ext/wiringpi/WiringPi/wiringPi/pcf8574.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/pcf8591.c +90 -0
- data/ext/wiringpi/WiringPi/wiringPi/pcf8591.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/piHiPri.c +51 -0
- data/ext/wiringpi/WiringPi/wiringPi/piThread.c +63 -0
- data/ext/wiringpi/WiringPi/wiringPi/pseudoPins.c +95 -0
- data/ext/wiringpi/WiringPi/wiringPi/pseudoPins.h +26 -0
- data/ext/wiringpi/WiringPi/wiringPi/rht03.c +252 -0
- data/ext/wiringpi/WiringPi/wiringPi/rht03.h +25 -0
- data/ext/wiringpi/WiringPi/wiringPi/sn3218.c +75 -0
- data/ext/wiringpi/WiringPi/wiringPi/sn3218.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/softPwm.c +186 -0
- data/ext/wiringpi/WiringPi/wiringPi/softPwm.h +35 -0
- data/ext/wiringpi/WiringPi/wiringPi/softServo.c +211 -0
- data/ext/wiringpi/WiringPi/wiringPi/softServo.h +35 -0
- data/ext/wiringpi/WiringPi/wiringPi/softTone.c +150 -0
- data/ext/wiringpi/WiringPi/wiringPi/softTone.h +39 -0
- data/ext/wiringpi/WiringPi/wiringPi/sr595.c +109 -0
- data/ext/wiringpi/WiringPi/wiringPi/sr595.h +34 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringPi.c +2507 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringPi.h +269 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringPiI2C.c +233 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringPiI2C.h +42 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringPiSPI.c +144 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringPiSPI.h +36 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringSerial.c +225 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringSerial.h +38 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringShift.c +83 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringShift.h +41 -0
- data/ext/wiringpi/WiringPi/wiringPi/wpiExtensions.c +928 -0
- data/ext/wiringpi/WiringPi/wiringPi/wpiExtensions.h +26 -0
- data/ext/wiringpi/WiringPi/wiringPiD/daemonise.c +82 -0
- data/ext/wiringpi/WiringPi/wiringPiD/daemonise.h +9 -0
- data/ext/wiringpi/WiringPi/wiringPiD/drcNetCmd.h +43 -0
- data/ext/wiringpi/WiringPi/wiringPiD/network.c +330 -0
- data/ext/wiringpi/WiringPi/wiringPiD/network.h +31 -0
- data/ext/wiringpi/WiringPi/wiringPiD/runRemote.c +126 -0
- data/ext/wiringpi/WiringPi/wiringPiD/runRemote.h +29 -0
- data/ext/wiringpi/WiringPi/wiringPiD/wiringpid.c +382 -0
- data/ext/wiringpi/wiringpi_wrap.c +636 -219
- data/lib/wiringpi.rb +25 -12
- data/lib/wiringpi/gpio.rb +90 -83
- data/lib/wiringpi/i2c.rb +29 -22
- data/lib/wiringpi/modules/mcp23017.rb +15 -0
- data/lib/wiringpi/modules/mcp23s17.rb +16 -0
- data/lib/wiringpi/scrollphat.rb +14 -14
- data/lib/wiringpi/serial.rb +8 -18
- data/lib/wiringpi/spi.rb +14 -11
- data/lib/wiringpi/watch.rb +24 -0
- metadata +160 -4
- data/lib/wiringpi/event.rb +0 -21
- data/lib/wiringpi/mcp23x17.rb +0 -31
@@ -0,0 +1,35 @@
|
|
1
|
+
/*
|
2
|
+
* softServo.h:
|
3
|
+
* Provide N channels of software driven PWM suitable for RC
|
4
|
+
* servo motors.
|
5
|
+
* Copyright (c) 2012 Gordon Henderson
|
6
|
+
***********************************************************************
|
7
|
+
* This file is part of wiringPi:
|
8
|
+
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
9
|
+
*
|
10
|
+
* wiringPi is free software: you can redistribute it and/or modify
|
11
|
+
* it under the terms of the GNU Lesser General Public License as
|
12
|
+
* published by the Free Software Foundation, either version 3 of the
|
13
|
+
* License, or (at your option) any later version.
|
14
|
+
*
|
15
|
+
* wiringPi is distributed in the hope that it will be useful,
|
16
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
+
* GNU Lesser General Public License for more details.
|
19
|
+
*
|
20
|
+
* You should have received a copy of the GNU Lesser General Public
|
21
|
+
* License along with wiringPi.
|
22
|
+
* If not, see <http://www.gnu.org/licenses/>.
|
23
|
+
***********************************************************************
|
24
|
+
*/
|
25
|
+
|
26
|
+
#ifdef __cplusplus
|
27
|
+
extern "C" {
|
28
|
+
#endif
|
29
|
+
|
30
|
+
extern void softServoWrite (int pin, int value) ;
|
31
|
+
extern int softServoSetup (int p0, int p1, int p2, int p3, int p4, int p5, int p6, int p7) ;
|
32
|
+
|
33
|
+
#ifdef __cplusplus
|
34
|
+
}
|
35
|
+
#endif
|
@@ -0,0 +1,150 @@
|
|
1
|
+
/*
|
2
|
+
* softTone.c:
|
3
|
+
* For that authentic retro sound...
|
4
|
+
* Er... A little experiment to produce tones out of a Pi using
|
5
|
+
* one (or 2) GPIO pins and a piezeo "speaker" element.
|
6
|
+
* (Or a high impedance speaker, but don'y blame me if you blow-up
|
7
|
+
* the GPIO pins!)
|
8
|
+
* Copyright (c) 2012 Gordon Henderson
|
9
|
+
***********************************************************************
|
10
|
+
* This file is part of wiringPi:
|
11
|
+
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
12
|
+
*
|
13
|
+
* wiringPi is free software: you can redistribute it and/or modify
|
14
|
+
* it under the terms of the GNU Lesser General Public License as
|
15
|
+
* published by the Free Software Foundation, either version 3 of the
|
16
|
+
* License, or (at your option) any later version.
|
17
|
+
*
|
18
|
+
* wiringPi is distributed in the hope that it will be useful,
|
19
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
20
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
21
|
+
* GNU Lesser General Public License for more details.
|
22
|
+
*
|
23
|
+
* You should have received a copy of the GNU Lesser General Public
|
24
|
+
* License along with wiringPi.
|
25
|
+
* If not, see <http://www.gnu.org/licenses/>.
|
26
|
+
***********************************************************************
|
27
|
+
*/
|
28
|
+
|
29
|
+
#include <stdio.h>
|
30
|
+
#include <pthread.h>
|
31
|
+
|
32
|
+
#include "wiringPi.h"
|
33
|
+
#include "softTone.h"
|
34
|
+
|
35
|
+
#define MAX_PINS 64
|
36
|
+
|
37
|
+
#define PULSE_TIME 100
|
38
|
+
|
39
|
+
static int freqs [MAX_PINS] ;
|
40
|
+
static pthread_t threads [MAX_PINS] ;
|
41
|
+
|
42
|
+
static int newPin = -1 ;
|
43
|
+
|
44
|
+
|
45
|
+
/*
|
46
|
+
* softToneThread:
|
47
|
+
* Thread to do the actual PWM output
|
48
|
+
*********************************************************************************
|
49
|
+
*/
|
50
|
+
|
51
|
+
static PI_THREAD (softToneThread)
|
52
|
+
{
|
53
|
+
int pin, freq, halfPeriod ;
|
54
|
+
struct sched_param param ;
|
55
|
+
|
56
|
+
param.sched_priority = sched_get_priority_max (SCHED_RR) ;
|
57
|
+
pthread_setschedparam (pthread_self (), SCHED_RR, ¶m) ;
|
58
|
+
|
59
|
+
pin = newPin ;
|
60
|
+
newPin = -1 ;
|
61
|
+
|
62
|
+
piHiPri (50) ;
|
63
|
+
|
64
|
+
for (;;)
|
65
|
+
{
|
66
|
+
freq = freqs [pin] ;
|
67
|
+
if (freq == 0)
|
68
|
+
delay (1) ;
|
69
|
+
else
|
70
|
+
{
|
71
|
+
halfPeriod = 500000 / freq ;
|
72
|
+
|
73
|
+
digitalWrite (pin, HIGH) ;
|
74
|
+
delayMicroseconds (halfPeriod) ;
|
75
|
+
|
76
|
+
digitalWrite (pin, LOW) ;
|
77
|
+
delayMicroseconds (halfPeriod) ;
|
78
|
+
}
|
79
|
+
}
|
80
|
+
|
81
|
+
return NULL ;
|
82
|
+
}
|
83
|
+
|
84
|
+
|
85
|
+
/*
|
86
|
+
* softToneWrite:
|
87
|
+
* Write a frequency value to the given pin
|
88
|
+
*********************************************************************************
|
89
|
+
*/
|
90
|
+
|
91
|
+
void softToneWrite (int pin, int freq)
|
92
|
+
{
|
93
|
+
pin &= 63 ;
|
94
|
+
|
95
|
+
/**/ if (freq < 0)
|
96
|
+
freq = 0 ;
|
97
|
+
else if (freq > 5000) // Max 5KHz
|
98
|
+
freq = 5000 ;
|
99
|
+
|
100
|
+
freqs [pin] = freq ;
|
101
|
+
}
|
102
|
+
|
103
|
+
|
104
|
+
/*
|
105
|
+
* softToneCreate:
|
106
|
+
* Create a new tone thread.
|
107
|
+
*********************************************************************************
|
108
|
+
*/
|
109
|
+
|
110
|
+
int softToneCreate (int pin)
|
111
|
+
{
|
112
|
+
int res ;
|
113
|
+
pthread_t myThread ;
|
114
|
+
|
115
|
+
pinMode (pin, OUTPUT) ;
|
116
|
+
digitalWrite (pin, LOW) ;
|
117
|
+
|
118
|
+
if (threads [pin] != 0)
|
119
|
+
return -1 ;
|
120
|
+
|
121
|
+
freqs [pin] = 0 ;
|
122
|
+
|
123
|
+
newPin = pin ;
|
124
|
+
res = pthread_create (&myThread, NULL, softToneThread, NULL) ;
|
125
|
+
|
126
|
+
while (newPin != -1)
|
127
|
+
delay (1) ;
|
128
|
+
|
129
|
+
threads [pin] = myThread ;
|
130
|
+
|
131
|
+
return res ;
|
132
|
+
}
|
133
|
+
|
134
|
+
|
135
|
+
/*
|
136
|
+
* softToneStop:
|
137
|
+
* Stop an existing softTone thread
|
138
|
+
*********************************************************************************
|
139
|
+
*/
|
140
|
+
|
141
|
+
void softToneStop (int pin)
|
142
|
+
{
|
143
|
+
if (threads [pin] != 0)
|
144
|
+
{
|
145
|
+
pthread_cancel (threads [pin]) ;
|
146
|
+
pthread_join (threads [pin], NULL) ;
|
147
|
+
threads [pin] = 0 ;
|
148
|
+
digitalWrite (pin, LOW) ;
|
149
|
+
}
|
150
|
+
}
|
@@ -0,0 +1,39 @@
|
|
1
|
+
/*
|
2
|
+
* softTone.c:
|
3
|
+
* For that authentic retro sound...
|
4
|
+
* Er... A little experiment to produce tones out of a Pi using
|
5
|
+
* one (or 2) GPIO pins and a piezeo "speaker" element.
|
6
|
+
* (Or a high impedance speaker, but don'y blame me if you blow-up
|
7
|
+
* the GPIO pins!)
|
8
|
+
* Copyright (c) 2012 Gordon Henderson
|
9
|
+
***********************************************************************
|
10
|
+
* This file is part of wiringPi:
|
11
|
+
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
12
|
+
*
|
13
|
+
* wiringPi is free software: you can redistribute it and/or modify
|
14
|
+
* it under the terms of the GNU Lesser General Public License as
|
15
|
+
* published by the Free Software Foundation, either version 3 of the
|
16
|
+
* License, or (at your option) any later version.
|
17
|
+
*
|
18
|
+
* wiringPi is distributed in the hope that it will be useful,
|
19
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
20
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
21
|
+
* GNU Lesser General Public License for more details.
|
22
|
+
*
|
23
|
+
* You should have received a copy of the GNU Lesser General Public
|
24
|
+
* License along with wiringPi.
|
25
|
+
* If not, see <http://www.gnu.org/licenses/>.
|
26
|
+
***********************************************************************
|
27
|
+
*/
|
28
|
+
|
29
|
+
#ifdef __cplusplus
|
30
|
+
extern "C" {
|
31
|
+
#endif
|
32
|
+
|
33
|
+
extern int softToneCreate (int pin) ;
|
34
|
+
extern void softToneStop (int pin) ;
|
35
|
+
extern void softToneWrite (int pin, int freq) ;
|
36
|
+
|
37
|
+
#ifdef __cplusplus
|
38
|
+
}
|
39
|
+
#endif
|
@@ -0,0 +1,109 @@
|
|
1
|
+
/*
|
2
|
+
* sr595.c:
|
3
|
+
* Extend wiringPi with the 74x595 shift register as a GPIO
|
4
|
+
* expander chip.
|
5
|
+
* Note that the code can cope with a number of 595's
|
6
|
+
* daisy-chained together - up to 4 for now as we're storing
|
7
|
+
* the output "register" in a single unsigned int.
|
8
|
+
*
|
9
|
+
* Copyright (c) 2013 Gordon Henderson
|
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
|
+
#include <stdio.h>
|
31
|
+
#include <stdint.h>
|
32
|
+
|
33
|
+
#include "wiringPi.h"
|
34
|
+
|
35
|
+
#include "sr595.h"
|
36
|
+
|
37
|
+
|
38
|
+
/*
|
39
|
+
* myDigitalWrite:
|
40
|
+
*********************************************************************************
|
41
|
+
*/
|
42
|
+
|
43
|
+
static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
|
44
|
+
{
|
45
|
+
unsigned int mask ;
|
46
|
+
int dataPin, clockPin, latchPin ;
|
47
|
+
int bit, bits, output ;
|
48
|
+
|
49
|
+
pin -= node->pinBase ; // Normalise pin number
|
50
|
+
bits = node->pinMax - node->pinBase + 1 ; // ie. number of clock pulses
|
51
|
+
dataPin = node->data0 ;
|
52
|
+
clockPin = node->data1 ;
|
53
|
+
latchPin = node->data2 ;
|
54
|
+
output = node->data3 ;
|
55
|
+
|
56
|
+
mask = 1 << pin ;
|
57
|
+
|
58
|
+
if (value == LOW)
|
59
|
+
output &= (~mask) ;
|
60
|
+
else
|
61
|
+
output |= mask ;
|
62
|
+
|
63
|
+
node->data3 = output ;
|
64
|
+
|
65
|
+
// A low -> high latch transition copies the latch to the output pins
|
66
|
+
|
67
|
+
digitalWrite (latchPin, LOW) ; delayMicroseconds (1) ;
|
68
|
+
for (bit = bits - 1 ; bit >= 0 ; --bit)
|
69
|
+
{
|
70
|
+
digitalWrite (dataPin, output & (1 << bit)) ;
|
71
|
+
|
72
|
+
digitalWrite (clockPin, HIGH) ; delayMicroseconds (1) ;
|
73
|
+
digitalWrite (clockPin, LOW) ; delayMicroseconds (1) ;
|
74
|
+
}
|
75
|
+
digitalWrite (latchPin, HIGH) ; delayMicroseconds (1) ;
|
76
|
+
}
|
77
|
+
|
78
|
+
|
79
|
+
/*
|
80
|
+
* sr595Setup:
|
81
|
+
* Create a new instance of a 74x595 shift register GPIO expander.
|
82
|
+
*********************************************************************************
|
83
|
+
*/
|
84
|
+
|
85
|
+
int sr595Setup (const int pinBase, const int numPins,
|
86
|
+
const int dataPin, const int clockPin, const int latchPin)
|
87
|
+
{
|
88
|
+
struct wiringPiNodeStruct *node ;
|
89
|
+
|
90
|
+
node = wiringPiNewNode (pinBase, numPins) ;
|
91
|
+
|
92
|
+
node->data0 = dataPin ;
|
93
|
+
node->data1 = clockPin ;
|
94
|
+
node->data2 = latchPin ;
|
95
|
+
node->data3 = 0 ; // Output register
|
96
|
+
node->digitalWrite = myDigitalWrite ;
|
97
|
+
|
98
|
+
// Initialise the underlying hardware
|
99
|
+
|
100
|
+
digitalWrite (dataPin, LOW) ;
|
101
|
+
digitalWrite (clockPin, LOW) ;
|
102
|
+
digitalWrite (latchPin, HIGH) ;
|
103
|
+
|
104
|
+
pinMode (dataPin, OUTPUT) ;
|
105
|
+
pinMode (clockPin, OUTPUT) ;
|
106
|
+
pinMode (latchPin, OUTPUT) ;
|
107
|
+
|
108
|
+
return TRUE ;
|
109
|
+
}
|
@@ -0,0 +1,34 @@
|
|
1
|
+
/*
|
2
|
+
* sr595.h:
|
3
|
+
* Extend wiringPi with the 74x595 shift registers.
|
4
|
+
* Copyright (c) 2013 Gordon Henderson
|
5
|
+
***********************************************************************
|
6
|
+
* This file is part of wiringPi:
|
7
|
+
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
8
|
+
*
|
9
|
+
* wiringPi is free software: you can redistribute it and/or modify
|
10
|
+
* it under the terms of the GNU Lesser General Public License as
|
11
|
+
* published by the Free Software Foundation, either version 3 of the
|
12
|
+
* License, or (at your option) any later version.
|
13
|
+
*
|
14
|
+
* wiringPi is distributed in the hope that it will be useful,
|
15
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
17
|
+
* GNU Lesser General Public License for more details.
|
18
|
+
*
|
19
|
+
* You should have received a copy of the GNU Lesser General Public
|
20
|
+
* License along with wiringPi.
|
21
|
+
* If not, see <http://www.gnu.org/licenses/>.
|
22
|
+
***********************************************************************
|
23
|
+
*/
|
24
|
+
|
25
|
+
#ifdef __cplusplus
|
26
|
+
extern "C" {
|
27
|
+
#endif
|
28
|
+
|
29
|
+
extern int sr595Setup (const int pinBase, const int numPins,
|
30
|
+
const int dataPin, const int clockPin, const int latchPin) ;
|
31
|
+
|
32
|
+
#ifdef __cplusplus
|
33
|
+
}
|
34
|
+
#endif
|
@@ -0,0 +1,2507 @@
|
|
1
|
+
/*
|
2
|
+
* wiringPi:
|
3
|
+
* Arduino look-a-like Wiring library for the Raspberry Pi
|
4
|
+
* Copyright (c) 2012-2017 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
|
+
#include <asm/ioctl.h>
|
73
|
+
|
74
|
+
#include "softPwm.h"
|
75
|
+
#include "softTone.h"
|
76
|
+
|
77
|
+
#include "wiringPi.h"
|
78
|
+
#include "../version.h"
|
79
|
+
|
80
|
+
// Environment Variables
|
81
|
+
|
82
|
+
#define ENV_DEBUG "WIRINGPI_DEBUG"
|
83
|
+
#define ENV_CODES "WIRINGPI_CODES"
|
84
|
+
#define ENV_GPIOMEM "WIRINGPI_GPIOMEM"
|
85
|
+
|
86
|
+
|
87
|
+
// Extend wiringPi with other pin-based devices and keep track of
|
88
|
+
// them in this structure
|
89
|
+
|
90
|
+
struct wiringPiNodeStruct *wiringPiNodes = NULL ;
|
91
|
+
|
92
|
+
// BCM Magic
|
93
|
+
|
94
|
+
#define BCM_PASSWORD 0x5A000000
|
95
|
+
|
96
|
+
|
97
|
+
// The BCM2835 has 54 GPIO pins.
|
98
|
+
// BCM2835 data sheet, Page 90 onwards.
|
99
|
+
// There are 6 control registers, each control the functions of a block
|
100
|
+
// of 10 pins.
|
101
|
+
// Each control register has 10 sets of 3 bits per GPIO pin - the ALT values
|
102
|
+
//
|
103
|
+
// 000 = GPIO Pin X is an input
|
104
|
+
// 001 = GPIO Pin X is an output
|
105
|
+
// 100 = GPIO Pin X takes alternate function 0
|
106
|
+
// 101 = GPIO Pin X takes alternate function 1
|
107
|
+
// 110 = GPIO Pin X takes alternate function 2
|
108
|
+
// 111 = GPIO Pin X takes alternate function 3
|
109
|
+
// 011 = GPIO Pin X takes alternate function 4
|
110
|
+
// 010 = GPIO Pin X takes alternate function 5
|
111
|
+
//
|
112
|
+
// So the 3 bits for port X are:
|
113
|
+
// X / 10 + ((X % 10) * 3)
|
114
|
+
|
115
|
+
// Port function select bits
|
116
|
+
|
117
|
+
#define FSEL_INPT 0b000
|
118
|
+
#define FSEL_OUTP 0b001
|
119
|
+
#define FSEL_ALT0 0b100
|
120
|
+
#define FSEL_ALT1 0b101
|
121
|
+
#define FSEL_ALT2 0b110
|
122
|
+
#define FSEL_ALT3 0b111
|
123
|
+
#define FSEL_ALT4 0b011
|
124
|
+
#define FSEL_ALT5 0b010
|
125
|
+
|
126
|
+
// Access from ARM Running Linux
|
127
|
+
// Taken from Gert/Doms code. Some of this is not in the manual
|
128
|
+
// that I can find )-:
|
129
|
+
//
|
130
|
+
// Updates in September 2015 - all now static variables (and apologies for the caps)
|
131
|
+
// due to the Pi v2, v3, etc. and the new /dev/gpiomem interface
|
132
|
+
|
133
|
+
static volatile unsigned int GPIO_PADS ;
|
134
|
+
static volatile unsigned int GPIO_CLOCK_BASE ;
|
135
|
+
static volatile unsigned int GPIO_BASE ;
|
136
|
+
static volatile unsigned int GPIO_TIMER ;
|
137
|
+
static volatile unsigned int GPIO_PWM ;
|
138
|
+
|
139
|
+
#define PAGE_SIZE (4*1024)
|
140
|
+
#define BLOCK_SIZE (4*1024)
|
141
|
+
|
142
|
+
static unsigned int usingGpioMem = FALSE ;
|
143
|
+
static int wiringPiSetuped = FALSE ;
|
144
|
+
|
145
|
+
// PWM
|
146
|
+
// Word offsets into the PWM control region
|
147
|
+
|
148
|
+
#define PWM_CONTROL 0
|
149
|
+
#define PWM_STATUS 1
|
150
|
+
#define PWM0_RANGE 4
|
151
|
+
#define PWM0_DATA 5
|
152
|
+
#define PWM1_RANGE 8
|
153
|
+
#define PWM1_DATA 9
|
154
|
+
|
155
|
+
// Clock regsiter offsets
|
156
|
+
|
157
|
+
#define PWMCLK_CNTL 40
|
158
|
+
#define PWMCLK_DIV 41
|
159
|
+
|
160
|
+
#define PWM0_MS_MODE 0x0080 // Run in MS mode
|
161
|
+
#define PWM0_USEFIFO 0x0020 // Data from FIFO
|
162
|
+
#define PWM0_REVPOLAR 0x0010 // Reverse polarity
|
163
|
+
#define PWM0_OFFSTATE 0x0008 // Ouput Off state
|
164
|
+
#define PWM0_REPEATFF 0x0004 // Repeat last value if FIFO empty
|
165
|
+
#define PWM0_SERIAL 0x0002 // Run in serial mode
|
166
|
+
#define PWM0_ENABLE 0x0001 // Channel Enable
|
167
|
+
|
168
|
+
#define PWM1_MS_MODE 0x8000 // Run in MS mode
|
169
|
+
#define PWM1_USEFIFO 0x2000 // Data from FIFO
|
170
|
+
#define PWM1_REVPOLAR 0x1000 // Reverse polarity
|
171
|
+
#define PWM1_OFFSTATE 0x0800 // Ouput Off state
|
172
|
+
#define PWM1_REPEATFF 0x0400 // Repeat last value if FIFO empty
|
173
|
+
#define PWM1_SERIAL 0x0200 // Run in serial mode
|
174
|
+
#define PWM1_ENABLE 0x0100 // Channel Enable
|
175
|
+
|
176
|
+
// Timer
|
177
|
+
// Word offsets
|
178
|
+
|
179
|
+
#define TIMER_LOAD (0x400 >> 2)
|
180
|
+
#define TIMER_VALUE (0x404 >> 2)
|
181
|
+
#define TIMER_CONTROL (0x408 >> 2)
|
182
|
+
#define TIMER_IRQ_CLR (0x40C >> 2)
|
183
|
+
#define TIMER_IRQ_RAW (0x410 >> 2)
|
184
|
+
#define TIMER_IRQ_MASK (0x414 >> 2)
|
185
|
+
#define TIMER_RELOAD (0x418 >> 2)
|
186
|
+
#define TIMER_PRE_DIV (0x41C >> 2)
|
187
|
+
#define TIMER_COUNTER (0x420 >> 2)
|
188
|
+
|
189
|
+
// Locals to hold pointers to the hardware
|
190
|
+
|
191
|
+
static volatile unsigned int *gpio ;
|
192
|
+
static volatile unsigned int *pwm ;
|
193
|
+
static volatile unsigned int *clk ;
|
194
|
+
static volatile unsigned int *pads ;
|
195
|
+
static volatile unsigned int *timer ;
|
196
|
+
static volatile unsigned int *timerIrqRaw ;
|
197
|
+
|
198
|
+
// Export variables for the hardware pointers
|
199
|
+
|
200
|
+
volatile unsigned int *_wiringPiGpio ;
|
201
|
+
volatile unsigned int *_wiringPiPwm ;
|
202
|
+
volatile unsigned int *_wiringPiClk ;
|
203
|
+
volatile unsigned int *_wiringPiPads ;
|
204
|
+
volatile unsigned int *_wiringPiTimer ;
|
205
|
+
volatile unsigned int *_wiringPiTimerIrqRaw ;
|
206
|
+
|
207
|
+
|
208
|
+
// Data for use with the boardId functions.
|
209
|
+
// The order of entries here to correspond with the PI_MODEL_X
|
210
|
+
// and PI_VERSION_X defines in wiringPi.h
|
211
|
+
// Only intended for the gpio command - use at your own risk!
|
212
|
+
|
213
|
+
// piGpioBase:
|
214
|
+
// The base address of the GPIO memory mapped hardware IO
|
215
|
+
|
216
|
+
#define GPIO_PERI_BASE_OLD 0x20000000
|
217
|
+
#define GPIO_PERI_BASE_2835 0x3F000000
|
218
|
+
#define GPIO_PERI_BASE_2711 0xFE000000
|
219
|
+
|
220
|
+
static volatile unsigned int piGpioBase = 0 ;
|
221
|
+
|
222
|
+
const char *piModelNames [21] =
|
223
|
+
{
|
224
|
+
"Model A", // 0
|
225
|
+
"Model B", // 1
|
226
|
+
"Model A+", // 2
|
227
|
+
"Model B+", // 3
|
228
|
+
"Pi 2", // 4
|
229
|
+
"Alpha", // 5
|
230
|
+
"CM", // 6
|
231
|
+
"Unknown07", // 07
|
232
|
+
"Pi 3", // 08
|
233
|
+
"Pi Zero", // 09
|
234
|
+
"CM3", // 10
|
235
|
+
"Unknown11", // 11
|
236
|
+
"Pi Zero-W", // 12
|
237
|
+
"Pi 3B+", // 13
|
238
|
+
"Pi 3A+", // 14
|
239
|
+
"Unknown15", // 15
|
240
|
+
"CM3+", // 16
|
241
|
+
"Pi 4B", // 17
|
242
|
+
"Unknown18", // 18
|
243
|
+
"Pi 400", // 19
|
244
|
+
"CM4", // 20
|
245
|
+
} ;
|
246
|
+
|
247
|
+
const char *piRevisionNames [16] =
|
248
|
+
{
|
249
|
+
"00",
|
250
|
+
"01",
|
251
|
+
"02",
|
252
|
+
"03",
|
253
|
+
"04",
|
254
|
+
"05",
|
255
|
+
"06",
|
256
|
+
"07",
|
257
|
+
"08",
|
258
|
+
"09",
|
259
|
+
"10",
|
260
|
+
"11",
|
261
|
+
"12",
|
262
|
+
"13",
|
263
|
+
"14",
|
264
|
+
"15",
|
265
|
+
} ;
|
266
|
+
|
267
|
+
const char *piMakerNames [16] =
|
268
|
+
{
|
269
|
+
"Sony", // 0
|
270
|
+
"Egoman", // 1
|
271
|
+
"Embest", // 2
|
272
|
+
"Unknown", // 3
|
273
|
+
"Embest", // 4
|
274
|
+
"Unknown05", // 5
|
275
|
+
"Unknown06", // 6
|
276
|
+
"Unknown07", // 7
|
277
|
+
"Unknown08", // 8
|
278
|
+
"Unknown09", // 9
|
279
|
+
"Unknown10", // 10
|
280
|
+
"Unknown11", // 11
|
281
|
+
"Unknown12", // 12
|
282
|
+
"Unknown13", // 13
|
283
|
+
"Unknown14", // 14
|
284
|
+
"Unknown15", // 15
|
285
|
+
} ;
|
286
|
+
|
287
|
+
const int piMemorySize [8] =
|
288
|
+
{
|
289
|
+
256, // 0
|
290
|
+
512, // 1
|
291
|
+
1024, // 2
|
292
|
+
2048, // 3
|
293
|
+
4096, // 4
|
294
|
+
8192, // 5
|
295
|
+
0, // 6
|
296
|
+
0, // 7
|
297
|
+
} ;
|
298
|
+
|
299
|
+
// Time for easy calculations
|
300
|
+
|
301
|
+
static uint64_t epochMilli, epochMicro ;
|
302
|
+
|
303
|
+
// Misc
|
304
|
+
|
305
|
+
static int wiringPiMode = WPI_MODE_UNINITIALISED ;
|
306
|
+
static volatile int pinPass = -1 ;
|
307
|
+
static pthread_mutex_t pinMutex ;
|
308
|
+
|
309
|
+
// Debugging & Return codes
|
310
|
+
|
311
|
+
int wiringPiDebug = FALSE ;
|
312
|
+
int wiringPiReturnCodes = FALSE ;
|
313
|
+
|
314
|
+
// Use /dev/gpiomem ?
|
315
|
+
|
316
|
+
int wiringPiTryGpioMem = FALSE ;
|
317
|
+
|
318
|
+
// sysFds:
|
319
|
+
// Map a file descriptor from the /sys/class/gpio/gpioX/value
|
320
|
+
|
321
|
+
static int sysFds [64] =
|
322
|
+
{
|
323
|
+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
324
|
+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
325
|
+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
326
|
+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
327
|
+
} ;
|
328
|
+
|
329
|
+
// ISR Data
|
330
|
+
|
331
|
+
static void (*isrFunctions [64])(void) ;
|
332
|
+
|
333
|
+
|
334
|
+
// Doing it the Arduino way with lookup tables...
|
335
|
+
// Yes, it's probably more innefficient than all the bit-twidling, but it
|
336
|
+
// does tend to make it all a bit clearer. At least to me!
|
337
|
+
|
338
|
+
// pinToGpio:
|
339
|
+
// Take a Wiring pin (0 through X) and re-map it to the BCM_GPIO pin
|
340
|
+
// Cope for 3 different board revisions here.
|
341
|
+
|
342
|
+
static int *pinToGpio ;
|
343
|
+
|
344
|
+
// Revision 1, 1.1:
|
345
|
+
|
346
|
+
static int pinToGpioR1 [64] =
|
347
|
+
{
|
348
|
+
17, 18, 21, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7: wpi 0 - 7
|
349
|
+
0, 1, // I2C - SDA1, SCL1 wpi 8 - 9
|
350
|
+
8, 7, // SPI - CE1, CE0 wpi 10 - 11
|
351
|
+
10, 9, 11, // SPI - MOSI, MISO, SCLK wpi 12 - 14
|
352
|
+
14, 15, // UART - Tx, Rx wpi 15 - 16
|
353
|
+
|
354
|
+
// Padding:
|
355
|
+
|
356
|
+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 31
|
357
|
+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
|
358
|
+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
|
359
|
+
} ;
|
360
|
+
|
361
|
+
// Revision 2:
|
362
|
+
|
363
|
+
static int pinToGpioR2 [64] =
|
364
|
+
{
|
365
|
+
17, 18, 27, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7: wpi 0 - 7
|
366
|
+
2, 3, // I2C - SDA0, SCL0 wpi 8 - 9
|
367
|
+
8, 7, // SPI - CE1, CE0 wpi 10 - 11
|
368
|
+
10, 9, 11, // SPI - MOSI, MISO, SCLK wpi 12 - 14
|
369
|
+
14, 15, // UART - Tx, Rx wpi 15 - 16
|
370
|
+
28, 29, 30, 31, // Rev 2: New GPIOs 8 though 11 wpi 17 - 20
|
371
|
+
5, 6, 13, 19, 26, // B+ wpi 21, 22, 23, 24, 25
|
372
|
+
12, 16, 20, 21, // B+ wpi 26, 27, 28, 29
|
373
|
+
0, 1, // B+ wpi 30, 31
|
374
|
+
|
375
|
+
// Padding:
|
376
|
+
|
377
|
+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
|
378
|
+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
|
379
|
+
} ;
|
380
|
+
|
381
|
+
|
382
|
+
// physToGpio:
|
383
|
+
// Take a physical pin (1 through 26) and re-map it to the BCM_GPIO pin
|
384
|
+
// Cope for 2 different board revisions here.
|
385
|
+
// Also add in the P5 connector, so the P5 pins are 3,4,5,6, so 53,54,55,56
|
386
|
+
|
387
|
+
static int *physToGpio ;
|
388
|
+
|
389
|
+
static int physToGpioR1 [64] =
|
390
|
+
{
|
391
|
+
-1, // 0
|
392
|
+
-1, -1, // 1, 2
|
393
|
+
0, -1,
|
394
|
+
1, -1,
|
395
|
+
4, 14,
|
396
|
+
-1, 15,
|
397
|
+
17, 18,
|
398
|
+
21, -1,
|
399
|
+
22, 23,
|
400
|
+
-1, 24,
|
401
|
+
10, -1,
|
402
|
+
9, 25,
|
403
|
+
11, 8,
|
404
|
+
-1, 7, // 25, 26
|
405
|
+
|
406
|
+
-1, -1, -1, -1, -1, // ... 31
|
407
|
+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
|
408
|
+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
|
409
|
+
} ;
|
410
|
+
|
411
|
+
static int physToGpioR2 [64] =
|
412
|
+
{
|
413
|
+
-1, // 0
|
414
|
+
-1, -1, // 1, 2
|
415
|
+
2, -1,
|
416
|
+
3, -1,
|
417
|
+
4, 14,
|
418
|
+
-1, 15,
|
419
|
+
17, 18,
|
420
|
+
27, -1,
|
421
|
+
22, 23,
|
422
|
+
-1, 24,
|
423
|
+
10, -1,
|
424
|
+
9, 25,
|
425
|
+
11, 8,
|
426
|
+
-1, 7, // 25, 26
|
427
|
+
|
428
|
+
// B+
|
429
|
+
|
430
|
+
0, 1,
|
431
|
+
5, -1,
|
432
|
+
6, 12,
|
433
|
+
13, -1,
|
434
|
+
19, 16,
|
435
|
+
26, 20,
|
436
|
+
-1, 21,
|
437
|
+
|
438
|
+
// the P5 connector on the Rev 2 boards:
|
439
|
+
|
440
|
+
-1, -1,
|
441
|
+
-1, -1,
|
442
|
+
-1, -1,
|
443
|
+
-1, -1,
|
444
|
+
-1, -1,
|
445
|
+
28, 29,
|
446
|
+
30, 31,
|
447
|
+
-1, -1,
|
448
|
+
-1, -1,
|
449
|
+
-1, -1,
|
450
|
+
-1, -1,
|
451
|
+
} ;
|
452
|
+
|
453
|
+
// gpioToGPFSEL:
|
454
|
+
// Map a BCM_GPIO pin to it's Function Selection
|
455
|
+
// control port. (GPFSEL 0-5)
|
456
|
+
// Groups of 10 - 3 bits per Function - 30 bits per port
|
457
|
+
|
458
|
+
static uint8_t gpioToGPFSEL [] =
|
459
|
+
{
|
460
|
+
0,0,0,0,0,0,0,0,0,0,
|
461
|
+
1,1,1,1,1,1,1,1,1,1,
|
462
|
+
2,2,2,2,2,2,2,2,2,2,
|
463
|
+
3,3,3,3,3,3,3,3,3,3,
|
464
|
+
4,4,4,4,4,4,4,4,4,4,
|
465
|
+
5,5,5,5,5,5,5,5,5,5,
|
466
|
+
} ;
|
467
|
+
|
468
|
+
|
469
|
+
// gpioToShift
|
470
|
+
// Define the shift up for the 3 bits per pin in each GPFSEL port
|
471
|
+
|
472
|
+
static uint8_t gpioToShift [] =
|
473
|
+
{
|
474
|
+
0,3,6,9,12,15,18,21,24,27,
|
475
|
+
0,3,6,9,12,15,18,21,24,27,
|
476
|
+
0,3,6,9,12,15,18,21,24,27,
|
477
|
+
0,3,6,9,12,15,18,21,24,27,
|
478
|
+
0,3,6,9,12,15,18,21,24,27,
|
479
|
+
0,3,6,9,12,15,18,21,24,27,
|
480
|
+
} ;
|
481
|
+
|
482
|
+
|
483
|
+
// gpioToGPSET:
|
484
|
+
// (Word) offset to the GPIO Set registers for each GPIO pin
|
485
|
+
|
486
|
+
static uint8_t gpioToGPSET [] =
|
487
|
+
{
|
488
|
+
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,
|
489
|
+
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,
|
490
|
+
} ;
|
491
|
+
|
492
|
+
// gpioToGPCLR:
|
493
|
+
// (Word) offset to the GPIO Clear registers for each GPIO pin
|
494
|
+
|
495
|
+
static uint8_t gpioToGPCLR [] =
|
496
|
+
{
|
497
|
+
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,
|
498
|
+
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,
|
499
|
+
} ;
|
500
|
+
|
501
|
+
|
502
|
+
// gpioToGPLEV:
|
503
|
+
// (Word) offset to the GPIO Input level registers for each GPIO pin
|
504
|
+
|
505
|
+
static uint8_t gpioToGPLEV [] =
|
506
|
+
{
|
507
|
+
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,
|
508
|
+
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,
|
509
|
+
} ;
|
510
|
+
|
511
|
+
|
512
|
+
#ifdef notYetReady
|
513
|
+
// gpioToEDS
|
514
|
+
// (Word) offset to the Event Detect Status
|
515
|
+
|
516
|
+
static uint8_t gpioToEDS [] =
|
517
|
+
{
|
518
|
+
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,
|
519
|
+
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,
|
520
|
+
} ;
|
521
|
+
|
522
|
+
// gpioToREN
|
523
|
+
// (Word) offset to the Rising edge ENable register
|
524
|
+
|
525
|
+
static uint8_t gpioToREN [] =
|
526
|
+
{
|
527
|
+
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,
|
528
|
+
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,
|
529
|
+
} ;
|
530
|
+
|
531
|
+
// gpioToFEN
|
532
|
+
// (Word) offset to the Falling edgde ENable register
|
533
|
+
|
534
|
+
static uint8_t gpioToFEN [] =
|
535
|
+
{
|
536
|
+
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,
|
537
|
+
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,
|
538
|
+
} ;
|
539
|
+
#endif
|
540
|
+
|
541
|
+
|
542
|
+
// GPPUD:
|
543
|
+
// GPIO Pin pull up/down register
|
544
|
+
|
545
|
+
#define GPPUD 37
|
546
|
+
|
547
|
+
/* 2711 has a different mechanism for pin pull-up/down/enable */
|
548
|
+
#define GPPUPPDN0 57 /* Pin pull-up/down for pins 15:0 */
|
549
|
+
#define GPPUPPDN1 58 /* Pin pull-up/down for pins 31:16 */
|
550
|
+
#define GPPUPPDN2 59 /* Pin pull-up/down for pins 47:32 */
|
551
|
+
#define GPPUPPDN3 60 /* Pin pull-up/down for pins 57:48 */
|
552
|
+
|
553
|
+
static volatile unsigned int piGpioPupOffset = 0 ;
|
554
|
+
|
555
|
+
// gpioToPUDCLK
|
556
|
+
// (Word) offset to the Pull Up Down Clock regsiter
|
557
|
+
|
558
|
+
static uint8_t gpioToPUDCLK [] =
|
559
|
+
{
|
560
|
+
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,
|
561
|
+
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,
|
562
|
+
} ;
|
563
|
+
|
564
|
+
|
565
|
+
// gpioToPwmALT
|
566
|
+
// the ALT value to put a GPIO pin into PWM mode
|
567
|
+
|
568
|
+
static uint8_t gpioToPwmALT [] =
|
569
|
+
{
|
570
|
+
0, 0, 0, 0, 0, 0, 0, 0, // 0 -> 7
|
571
|
+
0, 0, 0, 0, FSEL_ALT0, FSEL_ALT0, 0, 0, // 8 -> 15
|
572
|
+
0, 0, FSEL_ALT5, FSEL_ALT5, 0, 0, 0, 0, // 16 -> 23
|
573
|
+
0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
|
574
|
+
0, 0, 0, 0, 0, 0, 0, 0, // 32 -> 39
|
575
|
+
FSEL_ALT0, FSEL_ALT0, 0, 0, 0, FSEL_ALT0, 0, 0, // 40 -> 47
|
576
|
+
0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
|
577
|
+
0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
|
578
|
+
} ;
|
579
|
+
|
580
|
+
|
581
|
+
// gpioToPwmPort
|
582
|
+
// The port value to put a GPIO pin into PWM mode
|
583
|
+
|
584
|
+
static uint8_t gpioToPwmPort [] =
|
585
|
+
{
|
586
|
+
0, 0, 0, 0, 0, 0, 0, 0, // 0 -> 7
|
587
|
+
0, 0, 0, 0, PWM0_DATA, PWM1_DATA, 0, 0, // 8 -> 15
|
588
|
+
0, 0, PWM0_DATA, PWM1_DATA, 0, 0, 0, 0, // 16 -> 23
|
589
|
+
0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
|
590
|
+
0, 0, 0, 0, 0, 0, 0, 0, // 32 -> 39
|
591
|
+
PWM0_DATA, PWM1_DATA, 0, 0, 0, PWM1_DATA, 0, 0, // 40 -> 47
|
592
|
+
0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
|
593
|
+
0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
|
594
|
+
|
595
|
+
} ;
|
596
|
+
|
597
|
+
// gpioToGpClkALT:
|
598
|
+
// ALT value to put a GPIO pin into GP Clock mode.
|
599
|
+
// On the Pi we can really only use BCM_GPIO_4 and BCM_GPIO_21
|
600
|
+
// for clocks 0 and 1 respectively, however I'll include the full
|
601
|
+
// list for completeness - maybe one day...
|
602
|
+
|
603
|
+
#define GPIO_CLOCK_SOURCE 1
|
604
|
+
|
605
|
+
// gpioToGpClkALT0:
|
606
|
+
|
607
|
+
static uint8_t gpioToGpClkALT0 [] =
|
608
|
+
{
|
609
|
+
0, 0, 0, 0, FSEL_ALT0, FSEL_ALT0, FSEL_ALT0, 0, // 0 -> 7
|
610
|
+
0, 0, 0, 0, 0, 0, 0, 0, // 8 -> 15
|
611
|
+
0, 0, 0, 0, FSEL_ALT5, FSEL_ALT5, 0, 0, // 16 -> 23
|
612
|
+
0, 0, 0, 0, 0, 0, 0, 0, // 24 -> 31
|
613
|
+
FSEL_ALT0, 0, FSEL_ALT0, 0, 0, 0, 0, 0, // 32 -> 39
|
614
|
+
0, 0, FSEL_ALT0, FSEL_ALT0, FSEL_ALT0, 0, 0, 0, // 40 -> 47
|
615
|
+
0, 0, 0, 0, 0, 0, 0, 0, // 48 -> 55
|
616
|
+
0, 0, 0, 0, 0, 0, 0, 0, // 56 -> 63
|
617
|
+
} ;
|
618
|
+
|
619
|
+
// gpioToClk:
|
620
|
+
// (word) Offsets to the clock Control and Divisor register
|
621
|
+
|
622
|
+
static uint8_t gpioToClkCon [] =
|
623
|
+
{
|
624
|
+
-1, -1, -1, -1, 28, 30, 32, -1, // 0 -> 7
|
625
|
+
-1, -1, -1, -1, -1, -1, -1, -1, // 8 -> 15
|
626
|
+
-1, -1, -1, -1, 28, 30, -1, -1, // 16 -> 23
|
627
|
+
-1, -1, -1, -1, -1, -1, -1, -1, // 24 -> 31
|
628
|
+
28, -1, 28, -1, -1, -1, -1, -1, // 32 -> 39
|
629
|
+
-1, -1, 28, 30, 28, -1, -1, -1, // 40 -> 47
|
630
|
+
-1, -1, -1, -1, -1, -1, -1, -1, // 48 -> 55
|
631
|
+
-1, -1, -1, -1, -1, -1, -1, -1, // 56 -> 63
|
632
|
+
} ;
|
633
|
+
|
634
|
+
static uint8_t gpioToClkDiv [] =
|
635
|
+
{
|
636
|
+
-1, -1, -1, -1, 29, 31, 33, -1, // 0 -> 7
|
637
|
+
-1, -1, -1, -1, -1, -1, -1, -1, // 8 -> 15
|
638
|
+
-1, -1, -1, -1, 29, 31, -1, -1, // 16 -> 23
|
639
|
+
-1, -1, -1, -1, -1, -1, -1, -1, // 24 -> 31
|
640
|
+
29, -1, 29, -1, -1, -1, -1, -1, // 32 -> 39
|
641
|
+
-1, -1, 29, 31, 29, -1, -1, -1, // 40 -> 47
|
642
|
+
-1, -1, -1, -1, -1, -1, -1, -1, // 48 -> 55
|
643
|
+
-1, -1, -1, -1, -1, -1, -1, -1, // 56 -> 63
|
644
|
+
} ;
|
645
|
+
|
646
|
+
|
647
|
+
/*
|
648
|
+
* Functions
|
649
|
+
*********************************************************************************
|
650
|
+
*/
|
651
|
+
|
652
|
+
|
653
|
+
/*
|
654
|
+
* wiringPiFailure:
|
655
|
+
* Fail. Or not.
|
656
|
+
*********************************************************************************
|
657
|
+
*/
|
658
|
+
|
659
|
+
int wiringPiFailure (int fatal, const char *message, ...)
|
660
|
+
{
|
661
|
+
va_list argp ;
|
662
|
+
char buffer [1024] ;
|
663
|
+
|
664
|
+
if (!fatal && wiringPiReturnCodes)
|
665
|
+
return -1 ;
|
666
|
+
|
667
|
+
va_start (argp, message) ;
|
668
|
+
vsnprintf (buffer, 1023, message, argp) ;
|
669
|
+
va_end (argp) ;
|
670
|
+
|
671
|
+
fprintf (stderr, "%s", buffer) ;
|
672
|
+
exit (EXIT_FAILURE) ;
|
673
|
+
|
674
|
+
return 0 ;
|
675
|
+
}
|
676
|
+
|
677
|
+
|
678
|
+
/*
|
679
|
+
* setupCheck
|
680
|
+
* Another sanity check because some users forget to call the setup
|
681
|
+
* function. Mosty because they need feeding C drip by drip )-:
|
682
|
+
*********************************************************************************
|
683
|
+
*/
|
684
|
+
|
685
|
+
static void setupCheck (const char *fName)
|
686
|
+
{
|
687
|
+
if (!wiringPiSetuped)
|
688
|
+
{
|
689
|
+
fprintf (stderr, "%s: You have not called one of the wiringPiSetup\n"
|
690
|
+
" functions, so I'm aborting your program before it crashes anyway.\n", fName) ;
|
691
|
+
exit (EXIT_FAILURE) ;
|
692
|
+
}
|
693
|
+
}
|
694
|
+
|
695
|
+
/*
|
696
|
+
* gpioMemCheck:
|
697
|
+
* See if we're using the /dev/gpiomem interface, if-so then some operations
|
698
|
+
* can't be done and will crash the Pi.
|
699
|
+
*********************************************************************************
|
700
|
+
*/
|
701
|
+
|
702
|
+
static void usingGpioMemCheck (const char *what)
|
703
|
+
{
|
704
|
+
if (usingGpioMem)
|
705
|
+
{
|
706
|
+
fprintf (stderr, "%s: Unable to do this when using /dev/gpiomem. Try sudo?\n", what) ;
|
707
|
+
exit (EXIT_FAILURE) ;
|
708
|
+
}
|
709
|
+
}
|
710
|
+
|
711
|
+
|
712
|
+
|
713
|
+
/*
|
714
|
+
* piGpioLayout:
|
715
|
+
* Return a number representing the hardware revision of the board.
|
716
|
+
* This is not strictly the board revision but is used to check the
|
717
|
+
* layout of the GPIO connector - and there are 2 types that we are
|
718
|
+
* really interested in here. The very earliest Pi's and the
|
719
|
+
* ones that came after that which switched some pins ....
|
720
|
+
*
|
721
|
+
* Revision 1 really means the early Model A and B's.
|
722
|
+
* Revision 2 is everything else - it covers the B, B+ and CM.
|
723
|
+
* ... and the Pi 2 - which is a B+ ++ ...
|
724
|
+
* ... and the Pi 0 - which is an A+ ...
|
725
|
+
*
|
726
|
+
* The main difference between the revision 1 and 2 system that I use here
|
727
|
+
* is the mapping of the GPIO pins. From revision 2, the Pi Foundation changed
|
728
|
+
* 3 GPIO pins on the (original) 26-way header - BCM_GPIO 22 was dropped and
|
729
|
+
* replaced with 27, and 0 + 1 - I2C bus 0 was changed to 2 + 3; I2C bus 1.
|
730
|
+
*
|
731
|
+
* Additionally, here we set the piModel2 flag too. This is again, nothing to
|
732
|
+
* do with the actual model, but the major version numbers - the GPIO base
|
733
|
+
* hardware address changed at model 2 and above (not the Zero though)
|
734
|
+
*
|
735
|
+
*********************************************************************************
|
736
|
+
*/
|
737
|
+
|
738
|
+
static void piGpioLayoutOops (const char *why)
|
739
|
+
{
|
740
|
+
fprintf (stderr, "Oops: Unable to determine board revision from /proc/cpuinfo\n") ;
|
741
|
+
fprintf (stderr, " -> %s\n", why) ;
|
742
|
+
fprintf (stderr, " -> You'd best google the error to find out why.\n") ;
|
743
|
+
//fprintf (stderr, " -> http://www.raspberrypi.org/phpBB3/viewtopic.php?p=184410#p184410\n") ;
|
744
|
+
exit (EXIT_FAILURE) ;
|
745
|
+
}
|
746
|
+
|
747
|
+
int piGpioLayout (void)
|
748
|
+
{
|
749
|
+
FILE *cpuFd ;
|
750
|
+
char line [120] ;
|
751
|
+
char *c ;
|
752
|
+
static int gpioLayout = -1 ;
|
753
|
+
|
754
|
+
if (gpioLayout != -1) // No point checking twice
|
755
|
+
return gpioLayout ;
|
756
|
+
|
757
|
+
if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL)
|
758
|
+
piGpioLayoutOops ("Unable to open /proc/cpuinfo") ;
|
759
|
+
|
760
|
+
// Start by looking for the Architecture to make sure we're really running
|
761
|
+
// on a Pi. I'm getting fed-up with people whinging at me because
|
762
|
+
// they can't get it to work on weirdFruitPi boards...
|
763
|
+
|
764
|
+
while (fgets (line, 120, cpuFd) != NULL)
|
765
|
+
if (strncmp (line, "Hardware", 8) == 0)
|
766
|
+
break ;
|
767
|
+
|
768
|
+
if (strncmp (line, "Hardware", 8) != 0)
|
769
|
+
piGpioLayoutOops ("No \"Hardware\" line") ;
|
770
|
+
|
771
|
+
if (wiringPiDebug)
|
772
|
+
printf ("piGpioLayout: Hardware: %s\n", line) ;
|
773
|
+
|
774
|
+
// See if it's BCM2708 or BCM2709 or the new BCM2835.
|
775
|
+
|
776
|
+
// OK. As of Kernel 4.8, we have BCM2835 only, regardless of model.
|
777
|
+
// However I still want to check because it will trap the cheapskates and rip-
|
778
|
+
// off merchants who want to use wiringPi on non-Raspberry Pi platforms - which
|
779
|
+
// I do not support so don't email me your bleating whinges about anything
|
780
|
+
// other than a genuine Raspberry Pi.
|
781
|
+
|
782
|
+
#ifdef DONT_CARE_ANYMORE
|
783
|
+
if (! (strstr (line, "BCM2708") || strstr (line, "BCM2709") || strstr (line, "BCM2835")))
|
784
|
+
{
|
785
|
+
fprintf (stderr, "Unable to determine hardware version. I see: %s,\n", line) ;
|
786
|
+
fprintf (stderr, " - expecting BCM2708, BCM2709 or BCM2835.\n") ;
|
787
|
+
fprintf (stderr, "If this is a genuine Raspberry Pi then please report this\n") ;
|
788
|
+
fprintf (stderr, "to projects@drogon.net. If this is not a Raspberry Pi then you\n") ;
|
789
|
+
fprintf (stderr, "are on your own as wiringPi is designed to support the\n") ;
|
790
|
+
fprintf (stderr, "Raspberry Pi ONLY.\n") ;
|
791
|
+
exit (EXIT_FAILURE) ;
|
792
|
+
}
|
793
|
+
#endif
|
794
|
+
|
795
|
+
// Actually... That has caused me more than 10,000 emails so-far. Mosty by
|
796
|
+
// people who think they know better by creating a statically linked
|
797
|
+
// version that will not run with a new 4.9 kernel. I utterly hate and
|
798
|
+
// despise those people.
|
799
|
+
//
|
800
|
+
// I also get bleats from people running other than Raspbian with another
|
801
|
+
// distros compiled kernel rather than a foundation compiled kernel, so
|
802
|
+
// this might actually help them. It might not - I only have the capacity
|
803
|
+
// to support Raspbian.
|
804
|
+
//
|
805
|
+
// However, I've decided to leave this check out and rely purely on the
|
806
|
+
// Revision: line for now. It will not work on a non-pi hardware or weird
|
807
|
+
// kernels that don't give you a suitable revision line.
|
808
|
+
|
809
|
+
// So - we're Probably on a Raspberry Pi. Check the revision field for the real
|
810
|
+
// hardware type
|
811
|
+
// In-future, I ought to use the device tree as there are now Pi entries in
|
812
|
+
// /proc/device-tree/ ...
|
813
|
+
// but I'll leave that for the next revision. Or the next.
|
814
|
+
|
815
|
+
// Isolate the Revision line
|
816
|
+
|
817
|
+
rewind (cpuFd) ;
|
818
|
+
while (fgets (line, 120, cpuFd) != NULL)
|
819
|
+
if (strncmp (line, "Revision", 8) == 0)
|
820
|
+
break ;
|
821
|
+
|
822
|
+
fclose (cpuFd) ;
|
823
|
+
|
824
|
+
if (strncmp (line, "Revision", 8) != 0)
|
825
|
+
piGpioLayoutOops ("No \"Revision\" line") ;
|
826
|
+
|
827
|
+
// Chomp trailing CR/NL
|
828
|
+
|
829
|
+
for (c = &line [strlen (line) - 1] ; (*c == '\n') || (*c == '\r') ; --c)
|
830
|
+
*c = 0 ;
|
831
|
+
|
832
|
+
if (wiringPiDebug)
|
833
|
+
printf ("piGpioLayout: Revision string: %s\n", line) ;
|
834
|
+
|
835
|
+
// Scan to the first character of the revision number
|
836
|
+
|
837
|
+
for (c = line ; *c ; ++c)
|
838
|
+
if (*c == ':')
|
839
|
+
break ;
|
840
|
+
|
841
|
+
if (*c != ':')
|
842
|
+
piGpioLayoutOops ("Bogus \"Revision\" line (no colon)") ;
|
843
|
+
|
844
|
+
// Chomp spaces
|
845
|
+
|
846
|
+
++c ;
|
847
|
+
while (isspace (*c))
|
848
|
+
++c ;
|
849
|
+
|
850
|
+
if (!isxdigit (*c))
|
851
|
+
piGpioLayoutOops ("Bogus \"Revision\" line (no hex digit at start of revision)") ;
|
852
|
+
|
853
|
+
// Make sure its long enough
|
854
|
+
|
855
|
+
if (strlen (c) < 4)
|
856
|
+
piGpioLayoutOops ("Bogus revision line (too small)") ;
|
857
|
+
|
858
|
+
// Isolate last 4 characters: (in-case of overvolting or new encoding scheme)
|
859
|
+
|
860
|
+
c = c + strlen (c) - 4 ;
|
861
|
+
|
862
|
+
if (wiringPiDebug)
|
863
|
+
printf ("piGpioLayout: last4Chars are: \"%s\"\n", c) ;
|
864
|
+
|
865
|
+
if ( (strcmp (c, "0002") == 0) || (strcmp (c, "0003") == 0))
|
866
|
+
gpioLayout = 1 ;
|
867
|
+
else
|
868
|
+
gpioLayout = 2 ; // Covers everything else from the B revision 2 to the B+, the Pi v2, v3, zero and CM's.
|
869
|
+
|
870
|
+
if (wiringPiDebug)
|
871
|
+
printf ("piGpioLayoutOops: Returning revision: %d\n", gpioLayout) ;
|
872
|
+
|
873
|
+
return gpioLayout ;
|
874
|
+
}
|
875
|
+
|
876
|
+
/*
|
877
|
+
* piBoardRev:
|
878
|
+
* Deprecated, but does the same as piGpioLayout
|
879
|
+
*********************************************************************************
|
880
|
+
*/
|
881
|
+
|
882
|
+
int piBoardRev (void)
|
883
|
+
{
|
884
|
+
return piGpioLayout () ;
|
885
|
+
}
|
886
|
+
|
887
|
+
|
888
|
+
|
889
|
+
/*
|
890
|
+
* piBoardId:
|
891
|
+
* Return the real details of the board we have.
|
892
|
+
*
|
893
|
+
* This is undocumented and really only intended for the GPIO command.
|
894
|
+
* Use at your own risk!
|
895
|
+
*
|
896
|
+
* Seems there are some boards with 0000 in them (mistake in manufacture)
|
897
|
+
* So the distinction between boards that I can see is:
|
898
|
+
*
|
899
|
+
* 0000 - Error
|
900
|
+
* 0001 - Not used
|
901
|
+
*
|
902
|
+
* Original Pi boards:
|
903
|
+
* 0002 - Model B, Rev 1, 256MB, Egoman
|
904
|
+
* 0003 - Model B, Rev 1.1, 256MB, Egoman, Fuses/D14 removed.
|
905
|
+
*
|
906
|
+
* Newer Pi's with remapped GPIO:
|
907
|
+
* 0004 - Model B, Rev 1.2, 256MB, Sony
|
908
|
+
* 0005 - Model B, Rev 1.2, 256MB, Egoman
|
909
|
+
* 0006 - Model B, Rev 1.2, 256MB, Egoman
|
910
|
+
*
|
911
|
+
* 0007 - Model A, Rev 1.2, 256MB, Egoman
|
912
|
+
* 0008 - Model A, Rev 1.2, 256MB, Sony
|
913
|
+
* 0009 - Model A, Rev 1.2, 256MB, Egoman
|
914
|
+
*
|
915
|
+
* 000d - Model B, Rev 1.2, 512MB, Egoman (Red Pi, Blue Pi?)
|
916
|
+
* 000e - Model B, Rev 1.2, 512MB, Sony
|
917
|
+
* 000f - Model B, Rev 1.2, 512MB, Egoman
|
918
|
+
*
|
919
|
+
* 0010 - Model B+, Rev 1.2, 512MB, Sony
|
920
|
+
* 0013 - Model B+ Rev 1.2, 512MB, Embest
|
921
|
+
* 0016 - Model B+ Rev 1.2, 512MB, Sony
|
922
|
+
* 0019 - Model B+ Rev 1.2, 512MB, Egoman
|
923
|
+
*
|
924
|
+
* 0011 - Pi CM, Rev 1.1, 512MB, Sony
|
925
|
+
* 0014 - Pi CM, Rev 1.1, 512MB, Embest
|
926
|
+
* 0017 - Pi CM, Rev 1.1, 512MB, Sony
|
927
|
+
* 001a - Pi CM, Rev 1.1, 512MB, Egoman
|
928
|
+
*
|
929
|
+
* 0012 - Model A+ Rev 1.1, 256MB, Sony
|
930
|
+
* 0015 - Model A+ Rev 1.1, 512MB, Embest
|
931
|
+
* 0018 - Model A+ Rev 1.1, 256MB, Sony
|
932
|
+
* 001b - Model A+ Rev 1.1, 256MB, Egoman
|
933
|
+
*
|
934
|
+
* A small thorn is the olde style overvolting - that will add in
|
935
|
+
* 1000000
|
936
|
+
*
|
937
|
+
* The Pi compute module has an revision of 0011 or 0014 - since we only
|
938
|
+
* check the last digit, then it's 1, therefore it'll default to not 2 or
|
939
|
+
* 3 for a Rev 1, so will appear as a Rev 2. This is fine for the most part, but
|
940
|
+
* we'll properly detect the Compute Module later and adjust accordingly.
|
941
|
+
*
|
942
|
+
* And then things changed with the introduction of the v2...
|
943
|
+
*
|
944
|
+
* For Pi v2 and subsequent models - e.g. the Zero:
|
945
|
+
*
|
946
|
+
* [USER:8] [NEW:1] [MEMSIZE:3] [MANUFACTURER:4] [PROCESSOR:4] [TYPE:8] [REV:4]
|
947
|
+
* NEW 23: will be 1 for the new scheme, 0 for the old scheme
|
948
|
+
* MEMSIZE 20: 0=256M 1=512M 2=1G
|
949
|
+
* MANUFACTURER 16: 0=SONY 1=EGOMAN 2=EMBEST
|
950
|
+
* PROCESSOR 12: 0=2835 1=2836
|
951
|
+
* TYPE 04: 0=MODELA 1=MODELB 2=MODELA+ 3=MODELB+ 4=Pi2 MODEL B 5=ALPHA 6=CM
|
952
|
+
* REV 00: 0=REV0 1=REV1 2=REV2
|
953
|
+
*********************************************************************************
|
954
|
+
*/
|
955
|
+
|
956
|
+
void piBoardId (int *model, int *rev, int *mem, int *maker, int *warranty)
|
957
|
+
{
|
958
|
+
FILE *cpuFd ;
|
959
|
+
char line [120] ;
|
960
|
+
char *c ;
|
961
|
+
unsigned int revision ;
|
962
|
+
int bRev, bType, bProc, bMfg, bMem, bWarranty ;
|
963
|
+
|
964
|
+
// Will deal with the properly later on - for now, lets just get it going...
|
965
|
+
// unsigned int modelNum ;
|
966
|
+
|
967
|
+
(void)piGpioLayout () ; // Call this first to make sure all's OK. Don't care about the result.
|
968
|
+
|
969
|
+
if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL)
|
970
|
+
piGpioLayoutOops ("Unable to open /proc/cpuinfo") ;
|
971
|
+
|
972
|
+
while (fgets (line, 120, cpuFd) != NULL)
|
973
|
+
if (strncmp (line, "Revision", 8) == 0)
|
974
|
+
break ;
|
975
|
+
|
976
|
+
fclose (cpuFd) ;
|
977
|
+
|
978
|
+
if (strncmp (line, "Revision", 8) != 0)
|
979
|
+
piGpioLayoutOops ("No \"Revision\" line") ;
|
980
|
+
|
981
|
+
// Chomp trailing CR/NL
|
982
|
+
|
983
|
+
for (c = &line [strlen (line) - 1] ; (*c == '\n') || (*c == '\r') ; --c)
|
984
|
+
*c = 0 ;
|
985
|
+
|
986
|
+
if (wiringPiDebug)
|
987
|
+
printf ("piBoardId: Revision string: %s\n", line) ;
|
988
|
+
|
989
|
+
// Need to work out if it's using the new or old encoding scheme:
|
990
|
+
|
991
|
+
// Scan to the first character of the revision number
|
992
|
+
|
993
|
+
for (c = line ; *c ; ++c)
|
994
|
+
if (*c == ':')
|
995
|
+
break ;
|
996
|
+
|
997
|
+
if (*c != ':')
|
998
|
+
piGpioLayoutOops ("Bogus \"Revision\" line (no colon)") ;
|
999
|
+
|
1000
|
+
// Chomp spaces
|
1001
|
+
|
1002
|
+
++c ;
|
1003
|
+
while (isspace (*c))
|
1004
|
+
++c ;
|
1005
|
+
|
1006
|
+
if (!isxdigit (*c))
|
1007
|
+
piGpioLayoutOops ("Bogus \"Revision\" line (no hex digit at start of revision)") ;
|
1008
|
+
|
1009
|
+
revision = (unsigned int)strtol (c, NULL, 16) ; // Hex number with no leading 0x
|
1010
|
+
|
1011
|
+
// Check for new way:
|
1012
|
+
|
1013
|
+
if ((revision & (1 << 23)) != 0) // New way
|
1014
|
+
{
|
1015
|
+
if (wiringPiDebug)
|
1016
|
+
printf ("piBoardId: New Way: revision is: %08X\n", revision) ;
|
1017
|
+
|
1018
|
+
bRev = (revision & (0x0F << 0)) >> 0 ;
|
1019
|
+
bType = (revision & (0xFF << 4)) >> 4 ;
|
1020
|
+
bProc = (revision & (0x0F << 12)) >> 12 ; // Not used for now.
|
1021
|
+
bMfg = (revision & (0x0F << 16)) >> 16 ;
|
1022
|
+
bMem = (revision & (0x07 << 20)) >> 20 ;
|
1023
|
+
bWarranty = (revision & (0x03 << 24)) != 0 ;
|
1024
|
+
|
1025
|
+
*model = bType ;
|
1026
|
+
*rev = bRev ;
|
1027
|
+
*mem = bMem ;
|
1028
|
+
*maker = bMfg ;
|
1029
|
+
*warranty = bWarranty ;
|
1030
|
+
|
1031
|
+
if (wiringPiDebug)
|
1032
|
+
printf ("piBoardId: rev: %d, type: %d, proc: %d, mfg: %d, mem: %d, warranty: %d\n",
|
1033
|
+
bRev, bType, bProc, bMfg, bMem, bWarranty) ;
|
1034
|
+
}
|
1035
|
+
else // Old way
|
1036
|
+
{
|
1037
|
+
if (wiringPiDebug)
|
1038
|
+
printf ("piBoardId: Old Way: revision is: %s\n", c) ;
|
1039
|
+
|
1040
|
+
if (!isdigit (*c))
|
1041
|
+
piGpioLayoutOops ("Bogus \"Revision\" line (no digit at start of revision)") ;
|
1042
|
+
|
1043
|
+
// Make sure its long enough
|
1044
|
+
|
1045
|
+
if (strlen (c) < 4)
|
1046
|
+
piGpioLayoutOops ("Bogus \"Revision\" line (not long enough)") ;
|
1047
|
+
|
1048
|
+
// If longer than 4, we'll assume it's been overvolted
|
1049
|
+
|
1050
|
+
*warranty = strlen (c) > 4 ;
|
1051
|
+
|
1052
|
+
// Extract last 4 characters:
|
1053
|
+
|
1054
|
+
c = c + strlen (c) - 4 ;
|
1055
|
+
|
1056
|
+
// Fill out the replys as appropriate
|
1057
|
+
|
1058
|
+
/**/ if (strcmp (c, "0002") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
|
1059
|
+
else if (strcmp (c, "0003") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_1 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
|
1060
|
+
|
1061
|
+
else if (strcmp (c, "0004") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_2 ; *mem = 0 ; *maker = PI_MAKER_SONY ; }
|
1062
|
+
else if (strcmp (c, "0005") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_2 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
|
1063
|
+
else if (strcmp (c, "0006") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_2 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
|
1064
|
+
|
1065
|
+
else if (strcmp (c, "0007") == 0) { *model = PI_MODEL_A ; *rev = PI_VERSION_1_2 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
|
1066
|
+
else if (strcmp (c, "0008") == 0) { *model = PI_MODEL_A ; *rev = PI_VERSION_1_2 ; *mem = 0 ; *maker = PI_MAKER_SONY ; ; }
|
1067
|
+
else if (strcmp (c, "0009") == 0) { *model = PI_MODEL_A ; *rev = PI_VERSION_1_2 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
|
1068
|
+
|
1069
|
+
else if (strcmp (c, "000d") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_EGOMAN ; }
|
1070
|
+
else if (strcmp (c, "000e") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_SONY ; }
|
1071
|
+
else if (strcmp (c, "000f") == 0) { *model = PI_MODEL_B ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_EGOMAN ; }
|
1072
|
+
|
1073
|
+
else if (strcmp (c, "0010") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_SONY ; }
|
1074
|
+
else if (strcmp (c, "0013") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_EMBEST ; }
|
1075
|
+
else if (strcmp (c, "0016") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_SONY ; }
|
1076
|
+
else if (strcmp (c, "0019") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 1 ; *maker = PI_MAKER_EGOMAN ; }
|
1077
|
+
|
1078
|
+
else if (strcmp (c, "0011") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_1 ; *mem = 1 ; *maker = PI_MAKER_SONY ; }
|
1079
|
+
else if (strcmp (c, "0014") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_1 ; *mem = 1 ; *maker = PI_MAKER_EMBEST ; }
|
1080
|
+
else if (strcmp (c, "0017") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_1 ; *mem = 1 ; *maker = PI_MAKER_SONY ; }
|
1081
|
+
else if (strcmp (c, "001a") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_1 ; *mem = 1 ; *maker = PI_MAKER_EGOMAN ; }
|
1082
|
+
|
1083
|
+
else if (strcmp (c, "0012") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_1 ; *mem = 0 ; *maker = PI_MAKER_SONY ; }
|
1084
|
+
else if (strcmp (c, "0015") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_1 ; *mem = 1 ; *maker = PI_MAKER_EMBEST ; }
|
1085
|
+
else if (strcmp (c, "0018") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_1 ; *mem = 0 ; *maker = PI_MAKER_SONY ; }
|
1086
|
+
else if (strcmp (c, "001b") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_1 ; *mem = 0 ; *maker = PI_MAKER_EGOMAN ; }
|
1087
|
+
|
1088
|
+
else { *model = 0 ; *rev = 0 ; *mem = 0 ; *maker = 0 ; }
|
1089
|
+
}
|
1090
|
+
}
|
1091
|
+
|
1092
|
+
|
1093
|
+
|
1094
|
+
/*
|
1095
|
+
* wpiPinToGpio:
|
1096
|
+
* Translate a wiringPi Pin number to native GPIO pin number.
|
1097
|
+
* Provided for external support.
|
1098
|
+
*********************************************************************************
|
1099
|
+
*/
|
1100
|
+
|
1101
|
+
int wpiPinToGpio (int wpiPin)
|
1102
|
+
{
|
1103
|
+
return pinToGpio [wpiPin & 63] ;
|
1104
|
+
}
|
1105
|
+
|
1106
|
+
|
1107
|
+
/*
|
1108
|
+
* physPinToGpio:
|
1109
|
+
* Translate a physical Pin number to native GPIO pin number.
|
1110
|
+
* Provided for external support.
|
1111
|
+
*********************************************************************************
|
1112
|
+
*/
|
1113
|
+
|
1114
|
+
int physPinToGpio (int physPin)
|
1115
|
+
{
|
1116
|
+
return physToGpio [physPin & 63] ;
|
1117
|
+
}
|
1118
|
+
|
1119
|
+
|
1120
|
+
/*
|
1121
|
+
* setPadDrive:
|
1122
|
+
* Set the PAD driver value
|
1123
|
+
*********************************************************************************
|
1124
|
+
*/
|
1125
|
+
|
1126
|
+
void setPadDrive (int group, int value)
|
1127
|
+
{
|
1128
|
+
uint32_t wrVal ;
|
1129
|
+
|
1130
|
+
if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
|
1131
|
+
{
|
1132
|
+
if ((group < 0) || (group > 2))
|
1133
|
+
return ;
|
1134
|
+
|
1135
|
+
wrVal = BCM_PASSWORD | 0x18 | (value & 7) ;
|
1136
|
+
*(pads + group + 11) = wrVal ;
|
1137
|
+
|
1138
|
+
if (wiringPiDebug)
|
1139
|
+
{
|
1140
|
+
printf ("setPadDrive: Group: %d, value: %d (%08X)\n", group, value, wrVal) ;
|
1141
|
+
printf ("Read : %08X\n", *(pads + group + 11)) ;
|
1142
|
+
}
|
1143
|
+
}
|
1144
|
+
}
|
1145
|
+
|
1146
|
+
|
1147
|
+
/*
|
1148
|
+
* getAlt:
|
1149
|
+
* Returns the ALT bits for a given port. Only really of-use
|
1150
|
+
* for the gpio readall command (I think)
|
1151
|
+
*********************************************************************************
|
1152
|
+
*/
|
1153
|
+
|
1154
|
+
int getAlt (int pin)
|
1155
|
+
{
|
1156
|
+
int fSel, shift, alt ;
|
1157
|
+
|
1158
|
+
pin &= 63 ;
|
1159
|
+
|
1160
|
+
/**/ if (wiringPiMode == WPI_MODE_PINS)
|
1161
|
+
pin = pinToGpio [pin] ;
|
1162
|
+
else if (wiringPiMode == WPI_MODE_PHYS)
|
1163
|
+
pin = physToGpio [pin] ;
|
1164
|
+
else if (wiringPiMode != WPI_MODE_GPIO)
|
1165
|
+
return 0 ;
|
1166
|
+
|
1167
|
+
fSel = gpioToGPFSEL [pin] ;
|
1168
|
+
shift = gpioToShift [pin] ;
|
1169
|
+
|
1170
|
+
alt = (*(gpio + fSel) >> shift) & 7 ;
|
1171
|
+
|
1172
|
+
return alt ;
|
1173
|
+
}
|
1174
|
+
|
1175
|
+
|
1176
|
+
/*
|
1177
|
+
* pwmSetMode:
|
1178
|
+
* Select the native "balanced" mode, or standard mark:space mode
|
1179
|
+
*********************************************************************************
|
1180
|
+
*/
|
1181
|
+
|
1182
|
+
void pwmSetMode (int mode)
|
1183
|
+
{
|
1184
|
+
if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
|
1185
|
+
{
|
1186
|
+
if (mode == PWM_MODE_MS)
|
1187
|
+
*(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE | PWM0_MS_MODE | PWM1_MS_MODE ;
|
1188
|
+
else
|
1189
|
+
*(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ;
|
1190
|
+
}
|
1191
|
+
}
|
1192
|
+
|
1193
|
+
|
1194
|
+
/*
|
1195
|
+
* pwmSetRange:
|
1196
|
+
* Set the PWM range register. We set both range registers to the same
|
1197
|
+
* value. If you want different in your own code, then write your own.
|
1198
|
+
*********************************************************************************
|
1199
|
+
*/
|
1200
|
+
|
1201
|
+
void pwmSetRange (unsigned int range)
|
1202
|
+
{
|
1203
|
+
if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
|
1204
|
+
{
|
1205
|
+
*(pwm + PWM0_RANGE) = range ; delayMicroseconds (10) ;
|
1206
|
+
*(pwm + PWM1_RANGE) = range ; delayMicroseconds (10) ;
|
1207
|
+
}
|
1208
|
+
}
|
1209
|
+
|
1210
|
+
|
1211
|
+
/*
|
1212
|
+
* pwmSetClock:
|
1213
|
+
* Set/Change the PWM clock. Originally my code, but changed
|
1214
|
+
* (for the better!) by Chris Hall, <chris@kchall.plus.com>
|
1215
|
+
* after further study of the manual and testing with a 'scope
|
1216
|
+
*********************************************************************************
|
1217
|
+
*/
|
1218
|
+
|
1219
|
+
void pwmSetClock (int divisor)
|
1220
|
+
{
|
1221
|
+
uint32_t pwm_control ;
|
1222
|
+
|
1223
|
+
if (piGpioBase == GPIO_PERI_BASE_2711)
|
1224
|
+
{
|
1225
|
+
divisor = 540*divisor/192;
|
1226
|
+
}
|
1227
|
+
divisor &= 4095 ;
|
1228
|
+
|
1229
|
+
if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))
|
1230
|
+
{
|
1231
|
+
if (wiringPiDebug)
|
1232
|
+
printf ("Setting to: %d. Current: 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;
|
1233
|
+
|
1234
|
+
pwm_control = *(pwm + PWM_CONTROL) ; // preserve PWM_CONTROL
|
1235
|
+
|
1236
|
+
// We need to stop PWM prior to stopping PWM clock in MS mode otherwise BUSY
|
1237
|
+
// stays high.
|
1238
|
+
|
1239
|
+
*(pwm + PWM_CONTROL) = 0 ; // Stop PWM
|
1240
|
+
|
1241
|
+
// Stop PWM clock before changing divisor. The delay after this does need to
|
1242
|
+
// this big (95uS occasionally fails, 100uS OK), it's almost as though the BUSY
|
1243
|
+
// flag is not working properly in balanced mode. Without the delay when DIV is
|
1244
|
+
// adjusted the clock sometimes switches to very slow, once slow further DIV
|
1245
|
+
// adjustments do nothing and it's difficult to get out of this mode.
|
1246
|
+
|
1247
|
+
*(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x01 ; // Stop PWM Clock
|
1248
|
+
delayMicroseconds (110) ; // prevents clock going sloooow
|
1249
|
+
|
1250
|
+
while ((*(clk + PWMCLK_CNTL) & 0x80) != 0) // Wait for clock to be !BUSY
|
1251
|
+
delayMicroseconds (1) ;
|
1252
|
+
|
1253
|
+
*(clk + PWMCLK_DIV) = BCM_PASSWORD | (divisor << 12) ;
|
1254
|
+
|
1255
|
+
*(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x11 ; // Start PWM clock
|
1256
|
+
*(pwm + PWM_CONTROL) = pwm_control ; // restore PWM_CONTROL
|
1257
|
+
|
1258
|
+
if (wiringPiDebug)
|
1259
|
+
printf ("Set to: %d. Now : 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;
|
1260
|
+
}
|
1261
|
+
}
|
1262
|
+
|
1263
|
+
|
1264
|
+
/*
|
1265
|
+
* gpioClockSet:
|
1266
|
+
* Set the frequency on a GPIO clock pin
|
1267
|
+
*********************************************************************************
|
1268
|
+
*/
|
1269
|
+
|
1270
|
+
void gpioClockSet (int pin, int freq)
|
1271
|
+
{
|
1272
|
+
int divi, divr, divf ;
|
1273
|
+
|
1274
|
+
pin &= 63 ;
|
1275
|
+
|
1276
|
+
/**/ if (wiringPiMode == WPI_MODE_PINS)
|
1277
|
+
pin = pinToGpio [pin] ;
|
1278
|
+
else if (wiringPiMode == WPI_MODE_PHYS)
|
1279
|
+
pin = physToGpio [pin] ;
|
1280
|
+
else if (wiringPiMode != WPI_MODE_GPIO)
|
1281
|
+
return ;
|
1282
|
+
|
1283
|
+
divi = 19200000 / freq ;
|
1284
|
+
divr = 19200000 % freq ;
|
1285
|
+
divf = (int)((double)divr * 4096.0 / 19200000.0) ;
|
1286
|
+
|
1287
|
+
if (divi > 4095)
|
1288
|
+
divi = 4095 ;
|
1289
|
+
|
1290
|
+
*(clk + gpioToClkCon [pin]) = BCM_PASSWORD | GPIO_CLOCK_SOURCE ; // Stop GPIO Clock
|
1291
|
+
while ((*(clk + gpioToClkCon [pin]) & 0x80) != 0) // ... and wait
|
1292
|
+
;
|
1293
|
+
|
1294
|
+
*(clk + gpioToClkDiv [pin]) = BCM_PASSWORD | (divi << 12) | divf ; // Set dividers
|
1295
|
+
*(clk + gpioToClkCon [pin]) = BCM_PASSWORD | 0x10 | GPIO_CLOCK_SOURCE ; // Start Clock
|
1296
|
+
}
|
1297
|
+
|
1298
|
+
|
1299
|
+
/*
|
1300
|
+
* wiringPiFindNode:
|
1301
|
+
* Locate our device node
|
1302
|
+
*********************************************************************************
|
1303
|
+
*/
|
1304
|
+
|
1305
|
+
struct wiringPiNodeStruct *wiringPiFindNode (int pin)
|
1306
|
+
{
|
1307
|
+
struct wiringPiNodeStruct *node = wiringPiNodes ;
|
1308
|
+
|
1309
|
+
while (node != NULL)
|
1310
|
+
if ((pin >= node->pinBase) && (pin <= node->pinMax))
|
1311
|
+
return node ;
|
1312
|
+
else
|
1313
|
+
node = node->next ;
|
1314
|
+
|
1315
|
+
return NULL ;
|
1316
|
+
}
|
1317
|
+
|
1318
|
+
|
1319
|
+
/*
|
1320
|
+
* wiringPiNewNode:
|
1321
|
+
* Create a new GPIO node into the wiringPi handling system
|
1322
|
+
*********************************************************************************
|
1323
|
+
*/
|
1324
|
+
|
1325
|
+
static void pinModeDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int mode) { return ; }
|
1326
|
+
static void pullUpDnControlDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int pud) { return ; }
|
1327
|
+
//static unsigned int digitalRead8Dummy (UNU struct wiringPiNodeStruct *node, UNU int UNU pin) { return 0 ; }
|
1328
|
+
//static void digitalWrite8Dummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int value) { return ; }
|
1329
|
+
static int digitalReadDummy (UNU struct wiringPiNodeStruct *node, UNU int UNU pin) { return LOW ; }
|
1330
|
+
static void digitalWriteDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int value) { return ; }
|
1331
|
+
static void pwmWriteDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int value) { return ; }
|
1332
|
+
static int analogReadDummy (UNU struct wiringPiNodeStruct *node, UNU int pin) { return 0 ; }
|
1333
|
+
static void analogWriteDummy (UNU struct wiringPiNodeStruct *node, UNU int pin, UNU int value) { return ; }
|
1334
|
+
|
1335
|
+
struct wiringPiNodeStruct *wiringPiNewNode (int pinBase, int numPins)
|
1336
|
+
{
|
1337
|
+
int pin ;
|
1338
|
+
struct wiringPiNodeStruct *node ;
|
1339
|
+
|
1340
|
+
// Minimum pin base is 64
|
1341
|
+
|
1342
|
+
if (pinBase < 64)
|
1343
|
+
(void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: pinBase of %d is < 64\n", pinBase) ;
|
1344
|
+
|
1345
|
+
// Check all pins in-case there is overlap:
|
1346
|
+
|
1347
|
+
for (pin = pinBase ; pin < (pinBase + numPins) ; ++pin)
|
1348
|
+
if (wiringPiFindNode (pin) != NULL)
|
1349
|
+
(void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: Pin %d overlaps with existing definition\n", pin) ;
|
1350
|
+
|
1351
|
+
node = (struct wiringPiNodeStruct *)calloc (sizeof (struct wiringPiNodeStruct), 1) ; // calloc zeros
|
1352
|
+
if (node == NULL)
|
1353
|
+
(void)wiringPiFailure (WPI_FATAL, "wiringPiNewNode: Unable to allocate memory: %s\n", strerror (errno)) ;
|
1354
|
+
|
1355
|
+
node->pinBase = pinBase ;
|
1356
|
+
node->pinMax = pinBase + numPins - 1 ;
|
1357
|
+
node->pinMode = pinModeDummy ;
|
1358
|
+
node->pullUpDnControl = pullUpDnControlDummy ;
|
1359
|
+
node->digitalRead = digitalReadDummy ;
|
1360
|
+
//node->digitalRead8 = digitalRead8Dummy ;
|
1361
|
+
node->digitalWrite = digitalWriteDummy ;
|
1362
|
+
//node->digitalWrite8 = digitalWrite8Dummy ;
|
1363
|
+
node->pwmWrite = pwmWriteDummy ;
|
1364
|
+
node->analogRead = analogReadDummy ;
|
1365
|
+
node->analogWrite = analogWriteDummy ;
|
1366
|
+
node->next = wiringPiNodes ;
|
1367
|
+
wiringPiNodes = node ;
|
1368
|
+
|
1369
|
+
return node ;
|
1370
|
+
}
|
1371
|
+
|
1372
|
+
|
1373
|
+
#ifdef notYetReady
|
1374
|
+
/*
|
1375
|
+
* pinED01:
|
1376
|
+
* pinED10:
|
1377
|
+
* Enables edge-detect mode on a pin - from a 0 to a 1 or 1 to 0
|
1378
|
+
* Pin must already be in input mode with appropriate pull up/downs set.
|
1379
|
+
*********************************************************************************
|
1380
|
+
*/
|
1381
|
+
|
1382
|
+
void pinEnableED01Pi (int pin)
|
1383
|
+
{
|
1384
|
+
pin = pinToGpio [pin & 63] ;
|
1385
|
+
}
|
1386
|
+
#endif
|
1387
|
+
|
1388
|
+
|
1389
|
+
/*
|
1390
|
+
*********************************************************************************
|
1391
|
+
* Core Functions
|
1392
|
+
*********************************************************************************
|
1393
|
+
*/
|
1394
|
+
|
1395
|
+
/*
|
1396
|
+
* pinModeAlt:
|
1397
|
+
* This is an un-documented special to let you set any pin to any mode
|
1398
|
+
*********************************************************************************
|
1399
|
+
*/
|
1400
|
+
|
1401
|
+
void pinModeAlt (int pin, int mode)
|
1402
|
+
{
|
1403
|
+
int fSel, shift ;
|
1404
|
+
|
1405
|
+
setupCheck ("pinModeAlt") ;
|
1406
|
+
|
1407
|
+
if ((pin & PI_GPIO_MASK) == 0) // On-board pin
|
1408
|
+
{
|
1409
|
+
/**/ if (wiringPiMode == WPI_MODE_PINS)
|
1410
|
+
pin = pinToGpio [pin] ;
|
1411
|
+
else if (wiringPiMode == WPI_MODE_PHYS)
|
1412
|
+
pin = physToGpio [pin] ;
|
1413
|
+
else if (wiringPiMode != WPI_MODE_GPIO)
|
1414
|
+
return ;
|
1415
|
+
|
1416
|
+
fSel = gpioToGPFSEL [pin] ;
|
1417
|
+
shift = gpioToShift [pin] ;
|
1418
|
+
|
1419
|
+
*(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | ((mode & 0x7) << shift) ;
|
1420
|
+
}
|
1421
|
+
}
|
1422
|
+
|
1423
|
+
|
1424
|
+
/*
|
1425
|
+
* pinMode:
|
1426
|
+
* Sets the mode of a pin to be input, output or PWM output
|
1427
|
+
*********************************************************************************
|
1428
|
+
*/
|
1429
|
+
|
1430
|
+
void pinMode (int pin, int mode)
|
1431
|
+
{
|
1432
|
+
int fSel, shift, alt ;
|
1433
|
+
struct wiringPiNodeStruct *node = wiringPiNodes ;
|
1434
|
+
int origPin = pin ;
|
1435
|
+
|
1436
|
+
setupCheck ("pinMode") ;
|
1437
|
+
|
1438
|
+
if ((pin & PI_GPIO_MASK) == 0) // On-board pin
|
1439
|
+
{
|
1440
|
+
/**/ if (wiringPiMode == WPI_MODE_PINS)
|
1441
|
+
pin = pinToGpio [pin] ;
|
1442
|
+
else if (wiringPiMode == WPI_MODE_PHYS)
|
1443
|
+
pin = physToGpio [pin] ;
|
1444
|
+
else if (wiringPiMode != WPI_MODE_GPIO)
|
1445
|
+
return ;
|
1446
|
+
|
1447
|
+
softPwmStop (origPin) ;
|
1448
|
+
softToneStop (origPin) ;
|
1449
|
+
|
1450
|
+
fSel = gpioToGPFSEL [pin] ;
|
1451
|
+
shift = gpioToShift [pin] ;
|
1452
|
+
|
1453
|
+
/**/ if (mode == INPUT)
|
1454
|
+
*(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) ; // Sets bits to zero = input
|
1455
|
+
else if (mode == OUTPUT)
|
1456
|
+
*(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (1 << shift) ;
|
1457
|
+
else if (mode == SOFT_PWM_OUTPUT)
|
1458
|
+
softPwmCreate (origPin, 0, 100) ;
|
1459
|
+
else if (mode == SOFT_TONE_OUTPUT)
|
1460
|
+
softToneCreate (origPin) ;
|
1461
|
+
else if (mode == PWM_TONE_OUTPUT)
|
1462
|
+
{
|
1463
|
+
pinMode (origPin, PWM_OUTPUT) ; // Call myself to enable PWM mode
|
1464
|
+
pwmSetMode (PWM_MODE_MS) ;
|
1465
|
+
}
|
1466
|
+
else if (mode == PWM_OUTPUT)
|
1467
|
+
{
|
1468
|
+
if ((alt = gpioToPwmALT [pin]) == 0) // Not a hardware capable PWM pin
|
1469
|
+
return ;
|
1470
|
+
|
1471
|
+
usingGpioMemCheck ("pinMode PWM") ;
|
1472
|
+
|
1473
|
+
// Set pin to PWM mode
|
1474
|
+
|
1475
|
+
*(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ;
|
1476
|
+
delayMicroseconds (110) ; // See comments in pwmSetClockWPi
|
1477
|
+
|
1478
|
+
pwmSetMode (PWM_MODE_BAL) ; // Pi default mode
|
1479
|
+
pwmSetRange (1024) ; // Default range of 1024
|
1480
|
+
pwmSetClock (32) ; // 19.2 / 32 = 600KHz - Also starts the PWM
|
1481
|
+
}
|
1482
|
+
else if (mode == GPIO_CLOCK)
|
1483
|
+
{
|
1484
|
+
if ((alt = gpioToGpClkALT0 [pin]) == 0) // Not a GPIO_CLOCK pin
|
1485
|
+
return ;
|
1486
|
+
|
1487
|
+
usingGpioMemCheck ("pinMode CLOCK") ;
|
1488
|
+
|
1489
|
+
// Set pin to GPIO_CLOCK mode and set the clock frequency to 100KHz
|
1490
|
+
|
1491
|
+
*(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (alt << shift) ;
|
1492
|
+
delayMicroseconds (110) ;
|
1493
|
+
gpioClockSet (pin, 100000) ;
|
1494
|
+
}
|
1495
|
+
}
|
1496
|
+
else
|
1497
|
+
{
|
1498
|
+
if ((node = wiringPiFindNode (pin)) != NULL)
|
1499
|
+
node->pinMode (node, pin, mode) ;
|
1500
|
+
return ;
|
1501
|
+
}
|
1502
|
+
}
|
1503
|
+
|
1504
|
+
|
1505
|
+
/*
|
1506
|
+
* pullUpDownCtrl:
|
1507
|
+
* Control the internal pull-up/down resistors on a GPIO pin.
|
1508
|
+
*********************************************************************************
|
1509
|
+
*/
|
1510
|
+
|
1511
|
+
void pullUpDnControl (int pin, int pud)
|
1512
|
+
{
|
1513
|
+
struct wiringPiNodeStruct *node = wiringPiNodes ;
|
1514
|
+
|
1515
|
+
setupCheck ("pullUpDnControl") ;
|
1516
|
+
|
1517
|
+
if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
|
1518
|
+
{
|
1519
|
+
/**/ if (wiringPiMode == WPI_MODE_PINS)
|
1520
|
+
pin = pinToGpio [pin] ;
|
1521
|
+
else if (wiringPiMode == WPI_MODE_PHYS)
|
1522
|
+
pin = physToGpio [pin] ;
|
1523
|
+
else if (wiringPiMode != WPI_MODE_GPIO)
|
1524
|
+
return ;
|
1525
|
+
|
1526
|
+
if (piGpioPupOffset == GPPUPPDN0)
|
1527
|
+
{
|
1528
|
+
// Pi 4B pull up/down method
|
1529
|
+
int pullreg = GPPUPPDN0 + (pin>>4);
|
1530
|
+
int pullshift = (pin & 0xf) << 1;
|
1531
|
+
unsigned int pullbits;
|
1532
|
+
unsigned int pull;
|
1533
|
+
|
1534
|
+
switch (pud)
|
1535
|
+
{
|
1536
|
+
case PUD_OFF: pull = 0; break;
|
1537
|
+
case PUD_UP: pull = 1; break;
|
1538
|
+
case PUD_DOWN: pull = 2; break;
|
1539
|
+
default: return ; /* An illegal value */
|
1540
|
+
}
|
1541
|
+
|
1542
|
+
pullbits = *(gpio + pullreg);
|
1543
|
+
pullbits &= ~(3 << pullshift);
|
1544
|
+
pullbits |= (pull << pullshift);
|
1545
|
+
*(gpio + pullreg) = pullbits;
|
1546
|
+
}
|
1547
|
+
else
|
1548
|
+
{
|
1549
|
+
// legacy pull up/down method
|
1550
|
+
*(gpio + GPPUD) = pud & 3 ; delayMicroseconds (5) ;
|
1551
|
+
*(gpio + gpioToPUDCLK [pin]) = 1 << (pin & 31) ; delayMicroseconds (5) ;
|
1552
|
+
|
1553
|
+
*(gpio + GPPUD) = 0 ; delayMicroseconds (5) ;
|
1554
|
+
*(gpio + gpioToPUDCLK [pin]) = 0 ; delayMicroseconds (5) ;
|
1555
|
+
}
|
1556
|
+
}
|
1557
|
+
else // Extension module
|
1558
|
+
{
|
1559
|
+
if ((node = wiringPiFindNode (pin)) != NULL)
|
1560
|
+
node->pullUpDnControl (node, pin, pud) ;
|
1561
|
+
return ;
|
1562
|
+
}
|
1563
|
+
}
|
1564
|
+
|
1565
|
+
|
1566
|
+
/*
|
1567
|
+
* digitalRead:
|
1568
|
+
* Read the value of a given Pin, returning HIGH or LOW
|
1569
|
+
*********************************************************************************
|
1570
|
+
*/
|
1571
|
+
|
1572
|
+
int digitalRead (int pin)
|
1573
|
+
{
|
1574
|
+
char c ;
|
1575
|
+
struct wiringPiNodeStruct *node = wiringPiNodes ;
|
1576
|
+
if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
|
1577
|
+
{
|
1578
|
+
/**/ if (wiringPiMode == WPI_MODE_GPIO_SYS) // Sys mode
|
1579
|
+
{
|
1580
|
+
if (sysFds [pin] == -1)
|
1581
|
+
return LOW ;
|
1582
|
+
|
1583
|
+
lseek (sysFds [pin], 0L, SEEK_SET) ;
|
1584
|
+
read (sysFds [pin], &c, 1) ;
|
1585
|
+
return (c == '0') ? LOW : HIGH ;
|
1586
|
+
}
|
1587
|
+
else if (wiringPiMode == WPI_MODE_PINS)
|
1588
|
+
pin = pinToGpio [pin] ;
|
1589
|
+
else if (wiringPiMode == WPI_MODE_PHYS)
|
1590
|
+
pin = physToGpio [pin] ;
|
1591
|
+
else if (wiringPiMode != WPI_MODE_GPIO)
|
1592
|
+
return LOW ;
|
1593
|
+
|
1594
|
+
if ((*(gpio + gpioToGPLEV [pin]) & (1 << (pin & 31))) != 0)
|
1595
|
+
return HIGH ;
|
1596
|
+
else
|
1597
|
+
return LOW ;
|
1598
|
+
}
|
1599
|
+
else
|
1600
|
+
{
|
1601
|
+
if ((node = wiringPiFindNode (pin)) == NULL)
|
1602
|
+
return LOW ;
|
1603
|
+
return node->digitalRead (node, pin) ;
|
1604
|
+
}
|
1605
|
+
}
|
1606
|
+
|
1607
|
+
|
1608
|
+
/*
|
1609
|
+
* digitalRead8:
|
1610
|
+
* Read 8-bits (a byte) from given start pin.
|
1611
|
+
*********************************************************************************
|
1612
|
+
|
1613
|
+
unsigned int digitalRead8 (int pin)
|
1614
|
+
{
|
1615
|
+
struct wiringPiNodeStruct *node = wiringPiNodes ;
|
1616
|
+
|
1617
|
+
if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
|
1618
|
+
return 0 ;
|
1619
|
+
else
|
1620
|
+
{
|
1621
|
+
if ((node = wiringPiFindNode (pin)) == NULL)
|
1622
|
+
return LOW ;
|
1623
|
+
return node->digitalRead8 (node, pin) ;
|
1624
|
+
}
|
1625
|
+
}
|
1626
|
+
*/
|
1627
|
+
|
1628
|
+
|
1629
|
+
/*
|
1630
|
+
* digitalWrite:
|
1631
|
+
* Set an output bit
|
1632
|
+
*********************************************************************************
|
1633
|
+
*/
|
1634
|
+
|
1635
|
+
void digitalWrite (int pin, int value)
|
1636
|
+
{
|
1637
|
+
struct wiringPiNodeStruct *node = wiringPiNodes ;
|
1638
|
+
|
1639
|
+
if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
|
1640
|
+
{
|
1641
|
+
/**/ if (wiringPiMode == WPI_MODE_GPIO_SYS) // Sys mode
|
1642
|
+
{
|
1643
|
+
if (sysFds [pin] != -1)
|
1644
|
+
{
|
1645
|
+
if (value == LOW)
|
1646
|
+
write (sysFds [pin], "0\n", 2) ;
|
1647
|
+
else
|
1648
|
+
write (sysFds [pin], "1\n", 2) ;
|
1649
|
+
}
|
1650
|
+
return ;
|
1651
|
+
}
|
1652
|
+
else if (wiringPiMode == WPI_MODE_PINS)
|
1653
|
+
pin = pinToGpio [pin] ;
|
1654
|
+
else if (wiringPiMode == WPI_MODE_PHYS)
|
1655
|
+
pin = physToGpio [pin] ;
|
1656
|
+
else if (wiringPiMode != WPI_MODE_GPIO)
|
1657
|
+
return ;
|
1658
|
+
|
1659
|
+
if (value == LOW)
|
1660
|
+
*(gpio + gpioToGPCLR [pin]) = 1 << (pin & 31) ;
|
1661
|
+
else
|
1662
|
+
*(gpio + gpioToGPSET [pin]) = 1 << (pin & 31) ;
|
1663
|
+
}
|
1664
|
+
else
|
1665
|
+
{
|
1666
|
+
if ((node = wiringPiFindNode (pin)) != NULL)
|
1667
|
+
node->digitalWrite (node, pin, value) ;
|
1668
|
+
}
|
1669
|
+
}
|
1670
|
+
|
1671
|
+
|
1672
|
+
/*
|
1673
|
+
* digitalWrite8:
|
1674
|
+
* Set an output 8-bit byte on the device from the given pin number
|
1675
|
+
*********************************************************************************
|
1676
|
+
|
1677
|
+
void digitalWrite8 (int pin, int value)
|
1678
|
+
{
|
1679
|
+
struct wiringPiNodeStruct *node = wiringPiNodes ;
|
1680
|
+
|
1681
|
+
if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
|
1682
|
+
return ;
|
1683
|
+
else
|
1684
|
+
{
|
1685
|
+
if ((node = wiringPiFindNode (pin)) != NULL)
|
1686
|
+
node->digitalWrite8 (node, pin, value) ;
|
1687
|
+
}
|
1688
|
+
}
|
1689
|
+
*/
|
1690
|
+
|
1691
|
+
|
1692
|
+
/*
|
1693
|
+
* pwmWrite:
|
1694
|
+
* Set an output PWM value
|
1695
|
+
*********************************************************************************
|
1696
|
+
*/
|
1697
|
+
|
1698
|
+
void pwmWrite (int pin, int value)
|
1699
|
+
{
|
1700
|
+
struct wiringPiNodeStruct *node = wiringPiNodes ;
|
1701
|
+
|
1702
|
+
setupCheck ("pwmWrite") ;
|
1703
|
+
|
1704
|
+
if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
|
1705
|
+
{
|
1706
|
+
/**/ if (wiringPiMode == WPI_MODE_PINS)
|
1707
|
+
pin = pinToGpio [pin] ;
|
1708
|
+
else if (wiringPiMode == WPI_MODE_PHYS)
|
1709
|
+
pin = physToGpio [pin] ;
|
1710
|
+
else if (wiringPiMode != WPI_MODE_GPIO)
|
1711
|
+
return ;
|
1712
|
+
|
1713
|
+
usingGpioMemCheck ("pwmWrite") ;
|
1714
|
+
*(pwm + gpioToPwmPort [pin]) = value ;
|
1715
|
+
}
|
1716
|
+
else
|
1717
|
+
{
|
1718
|
+
if ((node = wiringPiFindNode (pin)) != NULL)
|
1719
|
+
node->pwmWrite (node, pin, value) ;
|
1720
|
+
}
|
1721
|
+
}
|
1722
|
+
|
1723
|
+
|
1724
|
+
/*
|
1725
|
+
* analogRead:
|
1726
|
+
* Read the analog value of a given Pin.
|
1727
|
+
* There is no on-board Pi analog hardware,
|
1728
|
+
* so this needs to go to a new node.
|
1729
|
+
*********************************************************************************
|
1730
|
+
*/
|
1731
|
+
|
1732
|
+
int analogRead (int pin)
|
1733
|
+
{
|
1734
|
+
struct wiringPiNodeStruct *node = wiringPiNodes ;
|
1735
|
+
|
1736
|
+
if ((node = wiringPiFindNode (pin)) == NULL)
|
1737
|
+
return 0 ;
|
1738
|
+
else
|
1739
|
+
return node->analogRead (node, pin) ;
|
1740
|
+
}
|
1741
|
+
|
1742
|
+
|
1743
|
+
/*
|
1744
|
+
* analogWrite:
|
1745
|
+
* Write the analog value to the given Pin.
|
1746
|
+
* There is no on-board Pi analog hardware,
|
1747
|
+
* so this needs to go to a new node.
|
1748
|
+
*********************************************************************************
|
1749
|
+
*/
|
1750
|
+
|
1751
|
+
void analogWrite (int pin, int value)
|
1752
|
+
{
|
1753
|
+
struct wiringPiNodeStruct *node = wiringPiNodes ;
|
1754
|
+
|
1755
|
+
if ((node = wiringPiFindNode (pin)) == NULL)
|
1756
|
+
return ;
|
1757
|
+
|
1758
|
+
node->analogWrite (node, pin, value) ;
|
1759
|
+
}
|
1760
|
+
|
1761
|
+
|
1762
|
+
/*
|
1763
|
+
* pwmToneWrite:
|
1764
|
+
* Pi Specific.
|
1765
|
+
* Output the given frequency on the Pi's PWM pin
|
1766
|
+
*********************************************************************************
|
1767
|
+
*/
|
1768
|
+
|
1769
|
+
void pwmToneWrite (int pin, int freq)
|
1770
|
+
{
|
1771
|
+
int range ;
|
1772
|
+
|
1773
|
+
setupCheck ("pwmToneWrite") ;
|
1774
|
+
|
1775
|
+
if (freq == 0)
|
1776
|
+
pwmWrite (pin, 0) ; // Off
|
1777
|
+
else
|
1778
|
+
{
|
1779
|
+
range = 600000 / freq ;
|
1780
|
+
pwmSetRange (range) ;
|
1781
|
+
pwmWrite (pin, freq / 2) ;
|
1782
|
+
}
|
1783
|
+
}
|
1784
|
+
|
1785
|
+
|
1786
|
+
|
1787
|
+
/*
|
1788
|
+
* digitalWriteByte:
|
1789
|
+
* digitalReadByte:
|
1790
|
+
* Pi Specific
|
1791
|
+
* Write an 8-bit byte to the first 8 GPIO pins - try to do it as
|
1792
|
+
* fast as possible.
|
1793
|
+
* However it still needs 2 operations to set the bits, so any external
|
1794
|
+
* hardware must not rely on seeing a change as there will be a change
|
1795
|
+
* to set the outputs bits to zero, then another change to set the 1's
|
1796
|
+
* Reading is just bit fiddling.
|
1797
|
+
* These are wiringPi pin numbers 0..7, or BCM_GPIO pin numbers
|
1798
|
+
* 17, 18, 22, 23, 24, 24, 4 on a Pi v1 rev 0-3
|
1799
|
+
* 17, 18, 27, 23, 24, 24, 4 on a Pi v1 rev 3 onwards or B+, 2, 3, zero
|
1800
|
+
*********************************************************************************
|
1801
|
+
*/
|
1802
|
+
|
1803
|
+
void digitalWriteByte (const int value)
|
1804
|
+
{
|
1805
|
+
uint32_t pinSet = 0 ;
|
1806
|
+
uint32_t pinClr = 0 ;
|
1807
|
+
int mask = 1 ;
|
1808
|
+
int pin ;
|
1809
|
+
|
1810
|
+
/**/ if (wiringPiMode == WPI_MODE_GPIO_SYS)
|
1811
|
+
{
|
1812
|
+
for (pin = 0 ; pin < 8 ; ++pin)
|
1813
|
+
{
|
1814
|
+
digitalWrite (pinToGpio [pin], value & mask) ;
|
1815
|
+
mask <<= 1 ;
|
1816
|
+
}
|
1817
|
+
return ;
|
1818
|
+
}
|
1819
|
+
else
|
1820
|
+
{
|
1821
|
+
for (pin = 0 ; pin < 8 ; ++pin)
|
1822
|
+
{
|
1823
|
+
if ((value & mask) == 0)
|
1824
|
+
pinClr |= (1 << pinToGpio [pin]) ;
|
1825
|
+
else
|
1826
|
+
pinSet |= (1 << pinToGpio [pin]) ;
|
1827
|
+
|
1828
|
+
mask <<= 1 ;
|
1829
|
+
}
|
1830
|
+
|
1831
|
+
*(gpio + gpioToGPCLR [0]) = pinClr ;
|
1832
|
+
*(gpio + gpioToGPSET [0]) = pinSet ;
|
1833
|
+
}
|
1834
|
+
}
|
1835
|
+
|
1836
|
+
unsigned int digitalReadByte (void)
|
1837
|
+
{
|
1838
|
+
int pin, x ;
|
1839
|
+
uint32_t raw ;
|
1840
|
+
uint32_t data = 0 ;
|
1841
|
+
|
1842
|
+
/**/ if (wiringPiMode == WPI_MODE_GPIO_SYS)
|
1843
|
+
{
|
1844
|
+
for (pin = 0 ; pin < 8 ; ++pin)
|
1845
|
+
{
|
1846
|
+
x = digitalRead (pinToGpio [pin]) ;
|
1847
|
+
data = (data << 1) | x ;
|
1848
|
+
}
|
1849
|
+
}
|
1850
|
+
else
|
1851
|
+
{
|
1852
|
+
raw = *(gpio + gpioToGPLEV [0]) ; // First bank for these pins
|
1853
|
+
for (pin = 0 ; pin < 8 ; ++pin)
|
1854
|
+
{
|
1855
|
+
x = pinToGpio [pin] ;
|
1856
|
+
data = (data << 1) | (((raw & (1 << x)) == 0) ? 0 : 1) ;
|
1857
|
+
}
|
1858
|
+
}
|
1859
|
+
return data ;
|
1860
|
+
}
|
1861
|
+
|
1862
|
+
|
1863
|
+
/*
|
1864
|
+
* digitalWriteByte2:
|
1865
|
+
* digitalReadByte2:
|
1866
|
+
* Pi Specific
|
1867
|
+
* Write an 8-bit byte to the second set of 8 GPIO pins. This is marginally
|
1868
|
+
* faster than the first lot as these are consecutive BCM_GPIO pin numbers.
|
1869
|
+
* However they overlap with the original read/write bytes.
|
1870
|
+
*********************************************************************************
|
1871
|
+
*/
|
1872
|
+
|
1873
|
+
void digitalWriteByte2 (const int value)
|
1874
|
+
{
|
1875
|
+
register int mask = 1 ;
|
1876
|
+
register int pin ;
|
1877
|
+
|
1878
|
+
/**/ if (wiringPiMode == WPI_MODE_GPIO_SYS)
|
1879
|
+
{
|
1880
|
+
for (pin = 20 ; pin < 28 ; ++pin)
|
1881
|
+
{
|
1882
|
+
digitalWrite (pin, value & mask) ;
|
1883
|
+
mask <<= 1 ;
|
1884
|
+
}
|
1885
|
+
return ;
|
1886
|
+
}
|
1887
|
+
else
|
1888
|
+
{
|
1889
|
+
*(gpio + gpioToGPCLR [0]) = (~value & 0xFF) << 20 ; // 0x0FF00000; ILJ > CHANGE: Old causes glitch
|
1890
|
+
*(gpio + gpioToGPSET [0]) = ( value & 0xFF) << 20 ;
|
1891
|
+
}
|
1892
|
+
}
|
1893
|
+
|
1894
|
+
unsigned int digitalReadByte2 (void)
|
1895
|
+
{
|
1896
|
+
int pin, x ;
|
1897
|
+
uint32_t data = 0 ;
|
1898
|
+
|
1899
|
+
/**/ if (wiringPiMode == WPI_MODE_GPIO_SYS)
|
1900
|
+
{
|
1901
|
+
for (pin = 20 ; pin < 28 ; ++pin)
|
1902
|
+
{
|
1903
|
+
x = digitalRead (pin) ;
|
1904
|
+
data = (data << 1) | x ;
|
1905
|
+
}
|
1906
|
+
}
|
1907
|
+
else
|
1908
|
+
data = ((*(gpio + gpioToGPLEV [0])) >> 20) & 0xFF ; // First bank for these pins
|
1909
|
+
|
1910
|
+
return data ;
|
1911
|
+
}
|
1912
|
+
|
1913
|
+
|
1914
|
+
/*
|
1915
|
+
* waitForInterrupt:
|
1916
|
+
* Pi Specific.
|
1917
|
+
* Wait for Interrupt on a GPIO pin.
|
1918
|
+
* This is actually done via the /sys/class/gpio interface regardless of
|
1919
|
+
* the wiringPi access mode in-use. Maybe sometime it might get a better
|
1920
|
+
* way for a bit more efficiency.
|
1921
|
+
*********************************************************************************
|
1922
|
+
*/
|
1923
|
+
|
1924
|
+
int waitForInterrupt (int pin, int mS)
|
1925
|
+
{
|
1926
|
+
int fd, x ;
|
1927
|
+
uint8_t c ;
|
1928
|
+
struct pollfd polls ;
|
1929
|
+
|
1930
|
+
/**/ if (wiringPiMode == WPI_MODE_PINS)
|
1931
|
+
pin = pinToGpio [pin] ;
|
1932
|
+
else if (wiringPiMode == WPI_MODE_PHYS)
|
1933
|
+
pin = physToGpio [pin] ;
|
1934
|
+
|
1935
|
+
if ((fd = sysFds [pin]) == -1)
|
1936
|
+
return -2 ;
|
1937
|
+
|
1938
|
+
// Setup poll structure
|
1939
|
+
|
1940
|
+
polls.fd = fd ;
|
1941
|
+
polls.events = POLLPRI | POLLERR ;
|
1942
|
+
|
1943
|
+
// Wait for it ...
|
1944
|
+
|
1945
|
+
x = poll (&polls, 1, mS) ;
|
1946
|
+
|
1947
|
+
// If no error, do a dummy read to clear the interrupt
|
1948
|
+
// A one character read appars to be enough.
|
1949
|
+
|
1950
|
+
if (x > 0)
|
1951
|
+
{
|
1952
|
+
lseek (fd, 0, SEEK_SET) ; // Rewind
|
1953
|
+
(void)read (fd, &c, 1) ; // Read & clear
|
1954
|
+
}
|
1955
|
+
|
1956
|
+
return x ;
|
1957
|
+
}
|
1958
|
+
|
1959
|
+
|
1960
|
+
/*
|
1961
|
+
* interruptHandler:
|
1962
|
+
* This is a thread and gets started to wait for the interrupt we're
|
1963
|
+
* hoping to catch. It will call the user-function when the interrupt
|
1964
|
+
* fires.
|
1965
|
+
*********************************************************************************
|
1966
|
+
*/
|
1967
|
+
|
1968
|
+
static void *interruptHandler (UNU void *arg)
|
1969
|
+
{
|
1970
|
+
int myPin ;
|
1971
|
+
|
1972
|
+
(void)piHiPri (55) ; // Only effective if we run as root
|
1973
|
+
|
1974
|
+
myPin = pinPass ;
|
1975
|
+
pinPass = -1 ;
|
1976
|
+
|
1977
|
+
for (;;)
|
1978
|
+
if (waitForInterrupt (myPin, -1) > 0)
|
1979
|
+
isrFunctions [myPin] () ;
|
1980
|
+
|
1981
|
+
return NULL ;
|
1982
|
+
}
|
1983
|
+
|
1984
|
+
|
1985
|
+
/*
|
1986
|
+
* wiringPiISR:
|
1987
|
+
* Pi Specific.
|
1988
|
+
* Take the details and create an interrupt handler that will do a call-
|
1989
|
+
* back to the user supplied function.
|
1990
|
+
*********************************************************************************
|
1991
|
+
*/
|
1992
|
+
|
1993
|
+
int wiringPiISR (int pin, int mode, void (*function)(void))
|
1994
|
+
{
|
1995
|
+
pthread_t threadId ;
|
1996
|
+
const char *modeS ;
|
1997
|
+
char fName [64] ;
|
1998
|
+
char pinS [8] ;
|
1999
|
+
pid_t pid ;
|
2000
|
+
int count, i ;
|
2001
|
+
char c ;
|
2002
|
+
int bcmGpioPin ;
|
2003
|
+
|
2004
|
+
if ((pin < 0) || (pin > 63))
|
2005
|
+
return wiringPiFailure (WPI_FATAL, "wiringPiISR: pin must be 0-63 (%d)\n", pin) ;
|
2006
|
+
|
2007
|
+
/**/ if (wiringPiMode == WPI_MODE_UNINITIALISED)
|
2008
|
+
return wiringPiFailure (WPI_FATAL, "wiringPiISR: wiringPi has not been initialised. Unable to continue.\n") ;
|
2009
|
+
else if (wiringPiMode == WPI_MODE_PINS)
|
2010
|
+
bcmGpioPin = pinToGpio [pin] ;
|
2011
|
+
else if (wiringPiMode == WPI_MODE_PHYS)
|
2012
|
+
bcmGpioPin = physToGpio [pin] ;
|
2013
|
+
else
|
2014
|
+
bcmGpioPin = pin ;
|
2015
|
+
|
2016
|
+
// Now export the pin and set the right edge
|
2017
|
+
// We're going to use the gpio program to do this, so it assumes
|
2018
|
+
// a full installation of wiringPi. It's a bit 'clunky', but it
|
2019
|
+
// is a way that will work when we're running in "Sys" mode, as
|
2020
|
+
// a non-root user. (without sudo)
|
2021
|
+
|
2022
|
+
if (mode != INT_EDGE_SETUP)
|
2023
|
+
{
|
2024
|
+
/**/ if (mode == INT_EDGE_FALLING)
|
2025
|
+
modeS = "falling" ;
|
2026
|
+
else if (mode == INT_EDGE_RISING)
|
2027
|
+
modeS = "rising" ;
|
2028
|
+
else
|
2029
|
+
modeS = "both" ;
|
2030
|
+
|
2031
|
+
sprintf (pinS, "%d", bcmGpioPin) ;
|
2032
|
+
|
2033
|
+
if ((pid = fork ()) < 0) // Fail
|
2034
|
+
return wiringPiFailure (WPI_FATAL, "wiringPiISR: fork failed: %s\n", strerror (errno)) ;
|
2035
|
+
|
2036
|
+
if (pid == 0) // Child, exec
|
2037
|
+
{
|
2038
|
+
/**/ if (access ("/usr/local/bin/gpio", X_OK) == 0)
|
2039
|
+
{
|
2040
|
+
execl ("/usr/local/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ;
|
2041
|
+
return wiringPiFailure (WPI_FATAL, "wiringPiISR: execl failed: %s\n", strerror (errno)) ;
|
2042
|
+
}
|
2043
|
+
else if (access ("/usr/bin/gpio", X_OK) == 0)
|
2044
|
+
{
|
2045
|
+
execl ("/usr/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ;
|
2046
|
+
return wiringPiFailure (WPI_FATAL, "wiringPiISR: execl failed: %s\n", strerror (errno)) ;
|
2047
|
+
}
|
2048
|
+
else
|
2049
|
+
return wiringPiFailure (WPI_FATAL, "wiringPiISR: Can't find gpio program\n") ;
|
2050
|
+
}
|
2051
|
+
else // Parent, wait
|
2052
|
+
waitpid (pid, NULL, 0) ;
|
2053
|
+
}
|
2054
|
+
|
2055
|
+
// Now pre-open the /sys/class node - but it may already be open if
|
2056
|
+
// we are in Sys mode...
|
2057
|
+
|
2058
|
+
if (sysFds [bcmGpioPin] == -1)
|
2059
|
+
{
|
2060
|
+
sprintf (fName, "/sys/class/gpio/gpio%d/value", bcmGpioPin) ;
|
2061
|
+
if ((sysFds [bcmGpioPin] = open (fName, O_RDWR)) < 0)
|
2062
|
+
return wiringPiFailure (WPI_FATAL, "wiringPiISR: unable to open %s: %s\n", fName, strerror (errno)) ;
|
2063
|
+
}
|
2064
|
+
|
2065
|
+
// Clear any initial pending interrupt
|
2066
|
+
|
2067
|
+
ioctl (sysFds [bcmGpioPin], FIONREAD, &count) ;
|
2068
|
+
for (i = 0 ; i < count ; ++i)
|
2069
|
+
read (sysFds [bcmGpioPin], &c, 1) ;
|
2070
|
+
|
2071
|
+
isrFunctions [pin] = function ;
|
2072
|
+
|
2073
|
+
pthread_mutex_lock (&pinMutex) ;
|
2074
|
+
pinPass = pin ;
|
2075
|
+
pthread_create (&threadId, NULL, interruptHandler, NULL) ;
|
2076
|
+
while (pinPass != -1)
|
2077
|
+
delay (1) ;
|
2078
|
+
pthread_mutex_unlock (&pinMutex) ;
|
2079
|
+
|
2080
|
+
return 0 ;
|
2081
|
+
}
|
2082
|
+
|
2083
|
+
|
2084
|
+
/*
|
2085
|
+
* initialiseEpoch:
|
2086
|
+
* Initialise our start-of-time variable to be the current unix
|
2087
|
+
* time in milliseconds and microseconds.
|
2088
|
+
*********************************************************************************
|
2089
|
+
*/
|
2090
|
+
|
2091
|
+
static void initialiseEpoch (void)
|
2092
|
+
{
|
2093
|
+
#ifdef OLD_WAY
|
2094
|
+
struct timeval tv ;
|
2095
|
+
|
2096
|
+
gettimeofday (&tv, NULL) ;
|
2097
|
+
epochMilli = (uint64_t)tv.tv_sec * (uint64_t)1000 + (uint64_t)(tv.tv_usec / 1000) ;
|
2098
|
+
epochMicro = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)(tv.tv_usec) ;
|
2099
|
+
#else
|
2100
|
+
struct timespec ts ;
|
2101
|
+
|
2102
|
+
clock_gettime (CLOCK_MONOTONIC_RAW, &ts) ;
|
2103
|
+
epochMilli = (uint64_t)ts.tv_sec * (uint64_t)1000 + (uint64_t)(ts.tv_nsec / 1000000L) ;
|
2104
|
+
epochMicro = (uint64_t)ts.tv_sec * (uint64_t)1000000 + (uint64_t)(ts.tv_nsec / 1000L) ;
|
2105
|
+
#endif
|
2106
|
+
}
|
2107
|
+
|
2108
|
+
|
2109
|
+
/*
|
2110
|
+
* delay:
|
2111
|
+
* Wait for some number of milliseconds
|
2112
|
+
*********************************************************************************
|
2113
|
+
*/
|
2114
|
+
|
2115
|
+
void delay (unsigned int howLong)
|
2116
|
+
{
|
2117
|
+
struct timespec sleeper, dummy ;
|
2118
|
+
|
2119
|
+
sleeper.tv_sec = (time_t)(howLong / 1000) ;
|
2120
|
+
sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ;
|
2121
|
+
|
2122
|
+
nanosleep (&sleeper, &dummy) ;
|
2123
|
+
}
|
2124
|
+
|
2125
|
+
|
2126
|
+
/*
|
2127
|
+
* delayMicroseconds:
|
2128
|
+
* This is somewhat intersting. It seems that on the Pi, a single call
|
2129
|
+
* to nanosleep takes some 80 to 130 microseconds anyway, so while
|
2130
|
+
* obeying the standards (may take longer), it's not always what we
|
2131
|
+
* want!
|
2132
|
+
*
|
2133
|
+
* So what I'll do now is if the delay is less than 100uS we'll do it
|
2134
|
+
* in a hard loop, watching a built-in counter on the ARM chip. This is
|
2135
|
+
* somewhat sub-optimal in that it uses 100% CPU, something not an issue
|
2136
|
+
* in a microcontroller, but under a multi-tasking, multi-user OS, it's
|
2137
|
+
* wastefull, however we've no real choice )-:
|
2138
|
+
*
|
2139
|
+
* Plan B: It seems all might not be well with that plan, so changing it
|
2140
|
+
* to use gettimeofday () and poll on that instead...
|
2141
|
+
*********************************************************************************
|
2142
|
+
*/
|
2143
|
+
|
2144
|
+
void delayMicrosecondsHard (unsigned int howLong)
|
2145
|
+
{
|
2146
|
+
struct timeval tNow, tLong, tEnd ;
|
2147
|
+
|
2148
|
+
gettimeofday (&tNow, NULL) ;
|
2149
|
+
tLong.tv_sec = howLong / 1000000 ;
|
2150
|
+
tLong.tv_usec = howLong % 1000000 ;
|
2151
|
+
timeradd (&tNow, &tLong, &tEnd) ;
|
2152
|
+
|
2153
|
+
while (timercmp (&tNow, &tEnd, <))
|
2154
|
+
gettimeofday (&tNow, NULL) ;
|
2155
|
+
}
|
2156
|
+
|
2157
|
+
void delayMicroseconds (unsigned int howLong)
|
2158
|
+
{
|
2159
|
+
struct timespec sleeper ;
|
2160
|
+
unsigned int uSecs = howLong % 1000000 ;
|
2161
|
+
unsigned int wSecs = howLong / 1000000 ;
|
2162
|
+
|
2163
|
+
/**/ if (howLong == 0)
|
2164
|
+
return ;
|
2165
|
+
else if (howLong < 100)
|
2166
|
+
delayMicrosecondsHard (howLong) ;
|
2167
|
+
else
|
2168
|
+
{
|
2169
|
+
sleeper.tv_sec = wSecs ;
|
2170
|
+
sleeper.tv_nsec = (long)(uSecs * 1000L) ;
|
2171
|
+
nanosleep (&sleeper, NULL) ;
|
2172
|
+
}
|
2173
|
+
}
|
2174
|
+
|
2175
|
+
|
2176
|
+
/*
|
2177
|
+
* millis:
|
2178
|
+
* Return a number of milliseconds as an unsigned int.
|
2179
|
+
* Wraps at 49 days.
|
2180
|
+
*********************************************************************************
|
2181
|
+
*/
|
2182
|
+
|
2183
|
+
unsigned int millis (void)
|
2184
|
+
{
|
2185
|
+
uint64_t now ;
|
2186
|
+
|
2187
|
+
#ifdef OLD_WAY
|
2188
|
+
struct timeval tv ;
|
2189
|
+
|
2190
|
+
gettimeofday (&tv, NULL) ;
|
2191
|
+
now = (uint64_t)tv.tv_sec * (uint64_t)1000 + (uint64_t)(tv.tv_usec / 1000) ;
|
2192
|
+
|
2193
|
+
#else
|
2194
|
+
struct timespec ts ;
|
2195
|
+
|
2196
|
+
clock_gettime (CLOCK_MONOTONIC_RAW, &ts) ;
|
2197
|
+
now = (uint64_t)ts.tv_sec * (uint64_t)1000 + (uint64_t)(ts.tv_nsec / 1000000L) ;
|
2198
|
+
#endif
|
2199
|
+
|
2200
|
+
return (uint32_t)(now - epochMilli) ;
|
2201
|
+
}
|
2202
|
+
|
2203
|
+
|
2204
|
+
/*
|
2205
|
+
* micros:
|
2206
|
+
* Return a number of microseconds as an unsigned int.
|
2207
|
+
* Wraps after 71 minutes.
|
2208
|
+
*********************************************************************************
|
2209
|
+
*/
|
2210
|
+
|
2211
|
+
unsigned int micros (void)
|
2212
|
+
{
|
2213
|
+
uint64_t now ;
|
2214
|
+
#ifdef OLD_WAY
|
2215
|
+
struct timeval tv ;
|
2216
|
+
|
2217
|
+
gettimeofday (&tv, NULL) ;
|
2218
|
+
now = (uint64_t)tv.tv_sec * (uint64_t)1000000 + (uint64_t)tv.tv_usec ;
|
2219
|
+
#else
|
2220
|
+
struct timespec ts ;
|
2221
|
+
|
2222
|
+
clock_gettime (CLOCK_MONOTONIC_RAW, &ts) ;
|
2223
|
+
now = (uint64_t)ts.tv_sec * (uint64_t)1000000 + (uint64_t)(ts.tv_nsec / 1000) ;
|
2224
|
+
#endif
|
2225
|
+
|
2226
|
+
|
2227
|
+
return (uint32_t)(now - epochMicro) ;
|
2228
|
+
}
|
2229
|
+
|
2230
|
+
/*
|
2231
|
+
* wiringPiVersion:
|
2232
|
+
* Return our current version number
|
2233
|
+
*********************************************************************************
|
2234
|
+
*/
|
2235
|
+
|
2236
|
+
void wiringPiVersion (int *major, int *minor)
|
2237
|
+
{
|
2238
|
+
*major = VERSION_MAJOR ;
|
2239
|
+
*minor = VERSION_MINOR ;
|
2240
|
+
}
|
2241
|
+
|
2242
|
+
|
2243
|
+
/*
|
2244
|
+
* wiringPiSetup:
|
2245
|
+
* Must be called once at the start of your program execution.
|
2246
|
+
*
|
2247
|
+
* Default setup: Initialises the system into wiringPi Pin mode and uses the
|
2248
|
+
* memory mapped hardware directly.
|
2249
|
+
*
|
2250
|
+
* Changed now to revert to "gpio" mode if we're running on a Compute Module.
|
2251
|
+
*********************************************************************************
|
2252
|
+
*/
|
2253
|
+
|
2254
|
+
int wiringPiSetup (void)
|
2255
|
+
{
|
2256
|
+
int fd ;
|
2257
|
+
int model, rev, mem, maker, overVolted ;
|
2258
|
+
|
2259
|
+
// It's actually a fatal error to call any of the wiringPiSetup routines more than once,
|
2260
|
+
// (you run out of file handles!) but I'm fed-up with the useless twats who email
|
2261
|
+
// me bleating that there is a bug in my code, so screw-em.
|
2262
|
+
|
2263
|
+
if (wiringPiSetuped)
|
2264
|
+
return 0 ;
|
2265
|
+
|
2266
|
+
wiringPiSetuped = TRUE ;
|
2267
|
+
|
2268
|
+
if (getenv (ENV_DEBUG) != NULL)
|
2269
|
+
wiringPiDebug = TRUE ;
|
2270
|
+
|
2271
|
+
if (getenv (ENV_CODES) != NULL)
|
2272
|
+
wiringPiReturnCodes = TRUE ;
|
2273
|
+
|
2274
|
+
if (wiringPiDebug)
|
2275
|
+
printf ("wiringPi: wiringPiSetup called\n") ;
|
2276
|
+
|
2277
|
+
// Get the board ID information. We're not really using the information here,
|
2278
|
+
// but it will give us information like the GPIO layout scheme (2 variants
|
2279
|
+
// on the older 26-pin Pi's) and the GPIO peripheral base address.
|
2280
|
+
// and if we're running on a compute module, then wiringPi pin numbers
|
2281
|
+
// don't really many anything, so force native BCM mode anyway.
|
2282
|
+
|
2283
|
+
piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
|
2284
|
+
|
2285
|
+
if ((model == PI_MODEL_CM) ||
|
2286
|
+
(model == PI_MODEL_CM3) ||
|
2287
|
+
(model == PI_MODEL_CM3P) ||
|
2288
|
+
(model == PI_MODEL_CM4))
|
2289
|
+
wiringPiMode = WPI_MODE_GPIO ;
|
2290
|
+
else
|
2291
|
+
wiringPiMode = WPI_MODE_PINS ;
|
2292
|
+
|
2293
|
+
/**/ if (piGpioLayout () == 1) // A, B, Rev 1, 1.1
|
2294
|
+
{
|
2295
|
+
pinToGpio = pinToGpioR1 ;
|
2296
|
+
physToGpio = physToGpioR1 ;
|
2297
|
+
}
|
2298
|
+
else // A2, B2, A+, B+, CM, Pi2, Pi3, Zero
|
2299
|
+
{
|
2300
|
+
pinToGpio = pinToGpioR2 ;
|
2301
|
+
physToGpio = physToGpioR2 ;
|
2302
|
+
}
|
2303
|
+
|
2304
|
+
// ...
|
2305
|
+
|
2306
|
+
switch (model)
|
2307
|
+
{
|
2308
|
+
case PI_MODEL_A: case PI_MODEL_B:
|
2309
|
+
case PI_MODEL_AP: case PI_MODEL_BP:
|
2310
|
+
case PI_ALPHA: case PI_MODEL_CM:
|
2311
|
+
case PI_MODEL_ZERO: case PI_MODEL_ZERO_W:
|
2312
|
+
piGpioBase = GPIO_PERI_BASE_OLD ;
|
2313
|
+
piGpioPupOffset = GPPUD ;
|
2314
|
+
break ;
|
2315
|
+
|
2316
|
+
case PI_MODEL_4B:
|
2317
|
+
case PI_MODEL_400:
|
2318
|
+
case PI_MODEL_CM4:
|
2319
|
+
piGpioBase = GPIO_PERI_BASE_2711 ;
|
2320
|
+
piGpioPupOffset = GPPUPPDN0 ;
|
2321
|
+
break ;
|
2322
|
+
|
2323
|
+
default:
|
2324
|
+
piGpioBase = GPIO_PERI_BASE_2835 ;
|
2325
|
+
piGpioPupOffset = GPPUD ;
|
2326
|
+
break ;
|
2327
|
+
}
|
2328
|
+
|
2329
|
+
// Open the master /dev/ memory control device
|
2330
|
+
// Device strategy: December 2016:
|
2331
|
+
// Try /dev/mem. If that fails, then
|
2332
|
+
// try /dev/gpiomem. If that fails then game over.
|
2333
|
+
|
2334
|
+
if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC)) < 0)
|
2335
|
+
{
|
2336
|
+
if ((fd = open ("/dev/gpiomem", O_RDWR | O_SYNC | O_CLOEXEC) ) >= 0) // We're using gpiomem
|
2337
|
+
{
|
2338
|
+
piGpioBase = 0 ;
|
2339
|
+
usingGpioMem = TRUE ;
|
2340
|
+
}
|
2341
|
+
else
|
2342
|
+
return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: Unable to open /dev/mem or /dev/gpiomem: %s.\n"
|
2343
|
+
" Aborting your program because if it can not access the GPIO\n"
|
2344
|
+
" hardware then it most certianly won't work\n"
|
2345
|
+
" Try running with sudo?\n", strerror (errno)) ;
|
2346
|
+
}
|
2347
|
+
|
2348
|
+
// Set the offsets into the memory interface.
|
2349
|
+
|
2350
|
+
GPIO_PADS = piGpioBase + 0x00100000 ;
|
2351
|
+
GPIO_CLOCK_BASE = piGpioBase + 0x00101000 ;
|
2352
|
+
GPIO_BASE = piGpioBase + 0x00200000 ;
|
2353
|
+
GPIO_TIMER = piGpioBase + 0x0000B000 ;
|
2354
|
+
GPIO_PWM = piGpioBase + 0x0020C000 ;
|
2355
|
+
|
2356
|
+
// Map the individual hardware components
|
2357
|
+
|
2358
|
+
// GPIO:
|
2359
|
+
|
2360
|
+
gpio = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_BASE) ;
|
2361
|
+
if (gpio == MAP_FAILED)
|
2362
|
+
return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (GPIO) failed: %s\n", strerror (errno)) ;
|
2363
|
+
|
2364
|
+
// PWM
|
2365
|
+
|
2366
|
+
pwm = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PWM) ;
|
2367
|
+
if (pwm == MAP_FAILED)
|
2368
|
+
return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PWM) failed: %s\n", strerror (errno)) ;
|
2369
|
+
|
2370
|
+
// Clock control (needed for PWM)
|
2371
|
+
|
2372
|
+
clk = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_CLOCK_BASE) ;
|
2373
|
+
if (clk == MAP_FAILED)
|
2374
|
+
return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (CLOCK) failed: %s\n", strerror (errno)) ;
|
2375
|
+
|
2376
|
+
// The drive pads
|
2377
|
+
|
2378
|
+
pads = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PADS) ;
|
2379
|
+
if (pads == MAP_FAILED)
|
2380
|
+
return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PADS) failed: %s\n", strerror (errno)) ;
|
2381
|
+
|
2382
|
+
// The system timer
|
2383
|
+
|
2384
|
+
timer = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_TIMER) ;
|
2385
|
+
if (timer == MAP_FAILED)
|
2386
|
+
return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (TIMER) failed: %s\n", strerror (errno)) ;
|
2387
|
+
|
2388
|
+
// Set the timer to free-running, 1MHz.
|
2389
|
+
// 0xF9 is 249, the timer divide is base clock / (divide+1)
|
2390
|
+
// so base clock is 250MHz / 250 = 1MHz.
|
2391
|
+
|
2392
|
+
*(timer + TIMER_CONTROL) = 0x0000280 ;
|
2393
|
+
*(timer + TIMER_PRE_DIV) = 0x00000F9 ;
|
2394
|
+
timerIrqRaw = timer + TIMER_IRQ_RAW ;
|
2395
|
+
|
2396
|
+
// Export the base addresses for any external software that might need them
|
2397
|
+
|
2398
|
+
_wiringPiGpio = gpio ;
|
2399
|
+
_wiringPiPwm = pwm ;
|
2400
|
+
_wiringPiClk = clk ;
|
2401
|
+
_wiringPiPads = pads ;
|
2402
|
+
_wiringPiTimer = timer ;
|
2403
|
+
|
2404
|
+
initialiseEpoch () ;
|
2405
|
+
|
2406
|
+
return 0 ;
|
2407
|
+
}
|
2408
|
+
|
2409
|
+
|
2410
|
+
/*
|
2411
|
+
* wiringPiSetupGpio:
|
2412
|
+
* Must be called once at the start of your program execution.
|
2413
|
+
*
|
2414
|
+
* GPIO setup: Initialises the system into GPIO Pin mode and uses the
|
2415
|
+
* memory mapped hardware directly.
|
2416
|
+
*********************************************************************************
|
2417
|
+
*/
|
2418
|
+
|
2419
|
+
int wiringPiSetupGpio (void)
|
2420
|
+
{
|
2421
|
+
(void)wiringPiSetup () ;
|
2422
|
+
|
2423
|
+
if (wiringPiDebug)
|
2424
|
+
printf ("wiringPi: wiringPiSetupGpio called\n") ;
|
2425
|
+
|
2426
|
+
wiringPiMode = WPI_MODE_GPIO ;
|
2427
|
+
|
2428
|
+
return 0 ;
|
2429
|
+
}
|
2430
|
+
|
2431
|
+
|
2432
|
+
/*
|
2433
|
+
* wiringPiSetupPhys:
|
2434
|
+
* Must be called once at the start of your program execution.
|
2435
|
+
*
|
2436
|
+
* Phys setup: Initialises the system into Physical Pin mode and uses the
|
2437
|
+
* memory mapped hardware directly.
|
2438
|
+
*********************************************************************************
|
2439
|
+
*/
|
2440
|
+
|
2441
|
+
int wiringPiSetupPhys (void)
|
2442
|
+
{
|
2443
|
+
(void)wiringPiSetup () ;
|
2444
|
+
|
2445
|
+
if (wiringPiDebug)
|
2446
|
+
printf ("wiringPi: wiringPiSetupPhys called\n") ;
|
2447
|
+
|
2448
|
+
wiringPiMode = WPI_MODE_PHYS ;
|
2449
|
+
|
2450
|
+
return 0 ;
|
2451
|
+
}
|
2452
|
+
|
2453
|
+
|
2454
|
+
/*
|
2455
|
+
* wiringPiSetupSys:
|
2456
|
+
* Must be called once at the start of your program execution.
|
2457
|
+
*
|
2458
|
+
* Initialisation (again), however this time we are using the /sys/class/gpio
|
2459
|
+
* interface to the GPIO systems - slightly slower, but always usable as
|
2460
|
+
* a non-root user, assuming the devices are already exported and setup correctly.
|
2461
|
+
*/
|
2462
|
+
|
2463
|
+
int wiringPiSetupSys (void)
|
2464
|
+
{
|
2465
|
+
int pin ;
|
2466
|
+
char fName [128] ;
|
2467
|
+
|
2468
|
+
if (wiringPiSetuped)
|
2469
|
+
return 0 ;
|
2470
|
+
|
2471
|
+
wiringPiSetuped = TRUE ;
|
2472
|
+
|
2473
|
+
if (getenv (ENV_DEBUG) != NULL)
|
2474
|
+
wiringPiDebug = TRUE ;
|
2475
|
+
|
2476
|
+
if (getenv (ENV_CODES) != NULL)
|
2477
|
+
wiringPiReturnCodes = TRUE ;
|
2478
|
+
|
2479
|
+
if (wiringPiDebug)
|
2480
|
+
printf ("wiringPi: wiringPiSetupSys called\n") ;
|
2481
|
+
|
2482
|
+
if (piGpioLayout () == 1)
|
2483
|
+
{
|
2484
|
+
pinToGpio = pinToGpioR1 ;
|
2485
|
+
physToGpio = physToGpioR1 ;
|
2486
|
+
}
|
2487
|
+
else
|
2488
|
+
{
|
2489
|
+
pinToGpio = pinToGpioR2 ;
|
2490
|
+
physToGpio = physToGpioR2 ;
|
2491
|
+
}
|
2492
|
+
|
2493
|
+
// Open and scan the directory, looking for exported GPIOs, and pre-open
|
2494
|
+
// the 'value' interface to speed things up for later
|
2495
|
+
|
2496
|
+
for (pin = 0 ; pin < 64 ; ++pin)
|
2497
|
+
{
|
2498
|
+
sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
|
2499
|
+
sysFds [pin] = open (fName, O_RDWR) ;
|
2500
|
+
}
|
2501
|
+
|
2502
|
+
initialiseEpoch () ;
|
2503
|
+
|
2504
|
+
wiringPiMode = WPI_MODE_GPIO_SYS ;
|
2505
|
+
|
2506
|
+
return 0 ;
|
2507
|
+
}
|