wiringpi 2.0.0 → 2.32.0
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/maxdetect.c +100 -27
- data/ext/wiringpi/WiringPi/devLib/maxdetect.h +0 -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/PiFace/ladder.c +0 -0
- data/ext/wiringpi/WiringPi/examples/max31855.c +60 -0
- data/ext/wiringpi/WiringPi/examples/rht03.c +32 -15
- data/ext/wiringpi/WiringPi/examples/scrollPhat/scphat.c +230 -0
- data/ext/wiringpi/WiringPi/examples/scrollPhat/test.c +115 -0
- data/ext/wiringpi/WiringPi/gpio/gpio.c +88 -37
- data/ext/wiringpi/WiringPi/gpio/readall.c +41 -9
- data/ext/wiringpi/WiringPi/gpio/version.h +1 -1
- data/ext/wiringpi/WiringPi/wiringPi/ads1115.c +293 -0
- data/ext/wiringpi/WiringPi/wiringPi/ads1115.h +55 -0
- data/ext/wiringpi/WiringPi/wiringPi/drcSerial.c +4 -9
- data/ext/wiringpi/WiringPi/wiringPi/max31855.c +41 -23
- data/ext/wiringpi/WiringPi/wiringPi/max5322.c +2 -2
- data/ext/wiringpi/WiringPi/wiringPi/mcp23008.c +2 -2
- data/ext/wiringpi/WiringPi/wiringPi/mcp23016.c +2 -2
- data/ext/wiringpi/WiringPi/wiringPi/mcp23017.c +2 -2
- data/ext/wiringpi/WiringPi/wiringPi/mcp23s08.c +3 -4
- data/ext/wiringpi/WiringPi/wiringPi/mcp23s17.c +3 -4
- data/ext/wiringpi/WiringPi/wiringPi/mcp3002.c +2 -2
- data/ext/wiringpi/WiringPi/wiringPi/mcp3004.c +2 -2
- data/ext/wiringpi/WiringPi/wiringPi/mcp3422.c +33 -18
- data/ext/wiringpi/WiringPi/wiringPi/mcp3422.h +6 -6
- data/ext/wiringpi/WiringPi/wiringPi/mcp4802.c +2 -2
- data/ext/wiringpi/WiringPi/wiringPi/pcf8574.c +2 -2
- data/ext/wiringpi/WiringPi/wiringPi/pcf8591.c +2 -2
- data/ext/wiringpi/WiringPi/wiringPi/sn3218.c +2 -2
- data/ext/wiringpi/WiringPi/wiringPi/sr595.c +1 -1
- data/ext/wiringpi/WiringPi/wiringPi/wiringPi.c +418 -132
- data/ext/wiringpi/WiringPi/wiringPi/wiringPi.h +47 -37
- data/ext/wiringpi/WiringPi/wiringPi/wpiExtensions.c +38 -9
- data/ext/wiringpi/extconf.rb +26 -2
- data/ext/wiringpi/wiringpi_wrap.c +3456 -981
- metadata +9 -1
@@ -47,6 +47,7 @@ extern int wiringPiDebug ;
|
|
47
47
|
// External functions I can't be bothered creating a separate .h file for:
|
48
48
|
|
49
49
|
extern void doReadall (void) ;
|
50
|
+
extern void doAllReadall (void) ;
|
50
51
|
extern void doPins (void) ;
|
51
52
|
|
52
53
|
#ifndef TRUE
|
@@ -115,10 +116,12 @@ static void changeOwner (char *cmd, char *file)
|
|
115
116
|
|
116
117
|
if (chown (file, uid, gid) != 0)
|
117
118
|
{
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
119
|
+
|
120
|
+
// Removed (ignoring) the check for not existing as I'm fed-up with morons telling me that
|
121
|
+
// the warning message is an error.
|
122
|
+
|
123
|
+
if (errno != ENOENT)
|
124
|
+
fprintf (stderr, "%s: Unable to change ownership of %s: %s\n", cmd, file, strerror (errno)) ;
|
122
125
|
}
|
123
126
|
}
|
124
127
|
|
@@ -138,7 +141,7 @@ static int moduleLoaded (char *modName)
|
|
138
141
|
|
139
142
|
if (fd == NULL)
|
140
143
|
{
|
141
|
-
fprintf (stderr, "gpio: Unable to check modules: %s\n", strerror (errno)) ;
|
144
|
+
fprintf (stderr, "gpio: Unable to check /proc/modules: %s\n", strerror (errno)) ;
|
142
145
|
exit (1) ;
|
143
146
|
}
|
144
147
|
|
@@ -163,6 +166,22 @@ static int moduleLoaded (char *modName)
|
|
163
166
|
*********************************************************************************
|
164
167
|
*/
|
165
168
|
|
169
|
+
static void checkDevTree (char *argv [])
|
170
|
+
{
|
171
|
+
struct stat statBuf ;
|
172
|
+
|
173
|
+
if (stat ("/proc/device-tree", &statBuf) == 0) // We're on a devtree system ...
|
174
|
+
{
|
175
|
+
fprintf (stderr,
|
176
|
+
"%s: Unable to load/unload modules as this Pi has the device tree enabled.\n"
|
177
|
+
" You need to run the raspi-config program (as root) and select the\n"
|
178
|
+
" modules (SPI or I2C) that you wish to load/unload there and reboot.\n"
|
179
|
+
" There is more information here:\n"
|
180
|
+
" https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=97314\n", argv [0]) ;
|
181
|
+
exit (1) ;
|
182
|
+
}
|
183
|
+
}
|
184
|
+
|
166
185
|
static void _doLoadUsage (char *argv [])
|
167
186
|
{
|
168
187
|
fprintf (stderr, "Usage: %s load <spi/i2c> [I2C baudrate in Kb/sec]\n", argv [0]) ;
|
@@ -176,6 +195,8 @@ static void doLoad (int argc, char *argv [])
|
|
176
195
|
char *file1, *file2 ;
|
177
196
|
char args1 [32], args2 [32] ;
|
178
197
|
|
198
|
+
checkDevTree (argv) ;
|
199
|
+
|
179
200
|
if (argc < 3)
|
180
201
|
_doLoadUsage (argv) ;
|
181
202
|
|
@@ -251,6 +272,8 @@ static void doUnLoad (int argc, char *argv [])
|
|
251
272
|
char *module1, *module2 ;
|
252
273
|
char cmd [80] ;
|
253
274
|
|
275
|
+
checkDevTree (argv) ;
|
276
|
+
|
254
277
|
if (argc != 3)
|
255
278
|
_doUnLoadUsage (argv) ;
|
256
279
|
|
@@ -754,13 +777,13 @@ static void doUsbP (int argc, char *argv [])
|
|
754
777
|
|
755
778
|
piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
|
756
779
|
|
757
|
-
if (model
|
780
|
+
if (!((model == PI_MODEL_BP) || (model == PI_MODEL_2)))
|
758
781
|
{
|
759
|
-
fprintf (stderr, "USB power contol is applicable to B+ boards only.\n") ;
|
782
|
+
fprintf (stderr, "USB power contol is applicable to B+ and v2 boards only.\n") ;
|
760
783
|
exit (1) ;
|
761
784
|
}
|
762
785
|
|
763
|
-
//
|
786
|
+
// Make sure we start in BCM_GPIO mode
|
764
787
|
|
765
788
|
wiringPiSetupGpio () ;
|
766
789
|
|
@@ -1128,6 +1151,45 @@ static void doPwmClock (int argc, char *argv [])
|
|
1128
1151
|
}
|
1129
1152
|
|
1130
1153
|
|
1154
|
+
/*
|
1155
|
+
* doVersion:
|
1156
|
+
* Handle the ever more complicated version command and print out
|
1157
|
+
* some usefull information.
|
1158
|
+
*********************************************************************************
|
1159
|
+
*/
|
1160
|
+
|
1161
|
+
static void doVersion (char *argv [])
|
1162
|
+
{
|
1163
|
+
int model, rev, mem, maker, warranty ;
|
1164
|
+
struct stat statBuf ;
|
1165
|
+
|
1166
|
+
printf ("gpio version: %s\n", VERSION) ;
|
1167
|
+
printf ("Copyright (c) 2012-2015 Gordon Henderson\n") ;
|
1168
|
+
printf ("This is free software with ABSOLUTELY NO WARRANTY.\n") ;
|
1169
|
+
printf ("For details type: %s -warranty\n", argv [0]) ;
|
1170
|
+
printf ("\n") ;
|
1171
|
+
piBoardId (&model, &rev, &mem, &maker, &warranty) ;
|
1172
|
+
|
1173
|
+
printf ("Raspberry Pi Details:\n") ;
|
1174
|
+
printf (" Type: %s, Revision: %s, Memory: %dMB, Maker: %s %s\n",
|
1175
|
+
piModelNames [model], piRevisionNames [rev], piMemorySize [mem], piMakerNames [maker], warranty ? "[Out of Warranty]" : "") ;
|
1176
|
+
|
1177
|
+
// Check for device tree
|
1178
|
+
|
1179
|
+
if (stat ("/proc/device-tree", &statBuf) == 0) // We're on a devtree system ...
|
1180
|
+
printf (" * Device tree is enabled.\n") ;
|
1181
|
+
|
1182
|
+
if (stat ("/dev/gpiomem", &statBuf) == 0) // User level GPIO is GO
|
1183
|
+
{
|
1184
|
+
printf (" * This Raspberry Pi supports user-level GPIO access.\n") ;
|
1185
|
+
printf (" -> See the man-page for more details\n") ;
|
1186
|
+
printf (" -> ie. export WIRINGPI_GPIOMEM=1\n") ;
|
1187
|
+
}
|
1188
|
+
else
|
1189
|
+
printf (" * Root or sudo required for GPIO access.\n") ;
|
1190
|
+
}
|
1191
|
+
|
1192
|
+
|
1131
1193
|
/*
|
1132
1194
|
* main:
|
1133
1195
|
* Start here
|
@@ -1137,7 +1199,6 @@ static void doPwmClock (int argc, char *argv [])
|
|
1137
1199
|
int main (int argc, char *argv [])
|
1138
1200
|
{
|
1139
1201
|
int i ;
|
1140
|
-
int model, rev, mem, maker, overVolted ;
|
1141
1202
|
|
1142
1203
|
if (getenv ("WIRINGPI_DEBUG") != NULL)
|
1143
1204
|
{
|
@@ -1159,42 +1220,20 @@ int main (int argc, char *argv [])
|
|
1159
1220
|
return 0 ;
|
1160
1221
|
}
|
1161
1222
|
|
1162
|
-
// Sort of a special:
|
1163
|
-
|
1164
|
-
if (strcmp (argv [1], "-R") == 0)
|
1165
|
-
{
|
1166
|
-
printf ("%d\n", piBoardRev ()) ;
|
1167
|
-
return 0 ;
|
1168
|
-
}
|
1169
|
-
|
1170
1223
|
// Version & Warranty
|
1224
|
+
// Wish I could remember why I have both -R and -V ...
|
1171
1225
|
|
1172
|
-
if (strcmp (argv [1], "-V") == 0)
|
1226
|
+
if ((strcmp (argv [1], "-R") == 0) || (strcmp (argv [1], "-V") == 0))
|
1173
1227
|
{
|
1174
1228
|
printf ("%d\n", piBoardRev ()) ;
|
1175
1229
|
return 0 ;
|
1176
1230
|
}
|
1177
1231
|
|
1232
|
+
// Version and information
|
1233
|
+
|
1178
1234
|
if (strcmp (argv [1], "-v") == 0)
|
1179
1235
|
{
|
1180
|
-
|
1181
|
-
printf ("Copyright (c) 2012-2015 Gordon Henderson\n") ;
|
1182
|
-
printf ("This is free software with ABSOLUTELY NO WARRANTY.\n") ;
|
1183
|
-
printf ("For details type: %s -warranty\n", argv [0]) ;
|
1184
|
-
printf ("\n") ;
|
1185
|
-
piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
|
1186
|
-
if (model == PI_MODEL_UNKNOWN)
|
1187
|
-
{
|
1188
|
-
printf ("Your Raspberry Pi has an unknown model type. Please report this to\n") ;
|
1189
|
-
printf (" projects@drogon.net\n") ;
|
1190
|
-
printf ("with a copy of your /proc/cpuinfo if possible\n") ;
|
1191
|
-
}
|
1192
|
-
else
|
1193
|
-
{
|
1194
|
-
printf ("Raspberry Pi Details:\n") ;
|
1195
|
-
printf (" Type: %s, Revision: %s, Memory: %dMB, Maker: %s %s\n",
|
1196
|
-
piModelNames [model], piRevisionNames [rev], mem, piMakerNames [maker], overVolted ? "[OV]" : "") ;
|
1197
|
-
}
|
1236
|
+
doVersion (argv) ;
|
1198
1237
|
return 0 ;
|
1199
1238
|
}
|
1200
1239
|
|
@@ -1238,11 +1277,24 @@ int main (int argc, char *argv [])
|
|
1238
1277
|
if (strcasecmp (argv [1], "load" ) == 0) { doLoad (argc, argv) ; return 0 ; }
|
1239
1278
|
if (strcasecmp (argv [1], "unload" ) == 0) { doUnLoad (argc, argv) ; return 0 ; }
|
1240
1279
|
|
1280
|
+
// Check for usb power command
|
1281
|
+
|
1282
|
+
if (strcasecmp (argv [1], "usbp" ) == 0) { doUsbP (argc, argv) ; return 0 ; }
|
1283
|
+
|
1241
1284
|
// Gertboard commands
|
1242
1285
|
|
1243
1286
|
if (strcasecmp (argv [1], "gbr" ) == 0) { doGbr (argc, argv) ; return 0 ; }
|
1244
1287
|
if (strcasecmp (argv [1], "gbw" ) == 0) { doGbw (argc, argv) ; return 0 ; }
|
1245
1288
|
|
1289
|
+
// Check for allreadall command, force Gpio mode
|
1290
|
+
|
1291
|
+
if (strcasecmp (argv [1], "allreadall") == 0)
|
1292
|
+
{
|
1293
|
+
wiringPiSetupGpio () ;
|
1294
|
+
doAllReadall () ;
|
1295
|
+
return 0 ;
|
1296
|
+
}
|
1297
|
+
|
1246
1298
|
// Check for -g argument
|
1247
1299
|
|
1248
1300
|
/**/ if (strcasecmp (argv [1], "-g") == 0)
|
@@ -1332,7 +1384,6 @@ int main (int argc, char *argv [])
|
|
1332
1384
|
else if (strcasecmp (argv [1], "pwmc" ) == 0) doPwmClock (argc, argv) ;
|
1333
1385
|
else if (strcasecmp (argv [1], "pwmTone" ) == 0) doPwmTone (argc, argv) ;
|
1334
1386
|
else if (strcasecmp (argv [1], "drive" ) == 0) doPadDrive (argc, argv) ;
|
1335
|
-
else if (strcasecmp (argv [1], "usbp" ) == 0) doUsbP (argc, argv) ;
|
1336
1387
|
else if (strcasecmp (argv [1], "readall" ) == 0) doReadall () ;
|
1337
1388
|
else if (strcasecmp (argv [1], "nreadall" ) == 0) doReadall () ;
|
1338
1389
|
else if (strcasecmp (argv [1], "pins" ) == 0) doPins () ;
|
@@ -215,7 +215,14 @@ static void readallPhys (int physPin)
|
|
215
215
|
}
|
216
216
|
|
217
217
|
|
218
|
-
|
218
|
+
/*
|
219
|
+
* allReadall:
|
220
|
+
* Read all the pins regardless of the model. Primarily of use for
|
221
|
+
* the compute module, but handy for other fiddling...
|
222
|
+
*********************************************************************************
|
223
|
+
*/
|
224
|
+
|
225
|
+
static void allReadall (void)
|
219
226
|
{
|
220
227
|
int pin ;
|
221
228
|
|
@@ -223,19 +230,20 @@ void cmReadall (void)
|
|
223
230
|
printf ("| Pin | Mode | Value | | Pin | Mode | Value |\n") ;
|
224
231
|
printf ("+-----+------+-------+ +-----+------+-------+\n") ;
|
225
232
|
|
226
|
-
for (pin = 0 ; pin <
|
233
|
+
for (pin = 0 ; pin < 27 ; ++pin)
|
227
234
|
{
|
228
235
|
printf ("| %3d ", pin) ;
|
229
236
|
printf ("| %-4s ", alts [getAlt (pin)]) ;
|
230
237
|
printf ("| %s ", digitalRead (pin) == HIGH ? "High" : "Low ") ;
|
231
238
|
printf ("| ") ;
|
232
|
-
printf ("| %3d ", pin +
|
233
|
-
printf ("| %-4s ", alts [getAlt (pin +
|
234
|
-
printf ("| %s ", digitalRead (pin +
|
239
|
+
printf ("| %3d ", pin + 27) ;
|
240
|
+
printf ("| %-4s ", alts [getAlt (pin + 27)]) ;
|
241
|
+
printf ("| %s ", digitalRead (pin + 27) == HIGH ? "High" : "Low ") ;
|
235
242
|
printf ("|\n") ;
|
236
243
|
}
|
237
244
|
|
238
245
|
printf ("+-----+------+-------+ +-----+------+-------+\n") ;
|
246
|
+
|
239
247
|
}
|
240
248
|
|
241
249
|
|
@@ -289,12 +297,18 @@ static void plus2header (int model)
|
|
289
297
|
printf (" +-----+-----+---------+------+---+--A Plus--+---+------+---------+-----+-----+\n") ;
|
290
298
|
else if (model == PI_MODEL_BP)
|
291
299
|
printf (" +-----+-----+---------+------+---+--B Plus--+---+------+---------+-----+-----+\n") ;
|
292
|
-
else
|
300
|
+
else if (model == PI_MODEL_ZERO)
|
301
|
+
printf (" +-----+-----+---------+------+---+-Pi Zero--+---+------+---------+-----+-----+\n") ;
|
302
|
+
else if (model == PI_MODEL_2)
|
293
303
|
printf (" +-----+-----+---------+------+---+---Pi 2---+---+------+---------+-----+-----+\n") ;
|
304
|
+
else if (model == PI_MODEL_3)
|
305
|
+
printf (" +-----+-----+---------+------+---+---Pi 3---+---+------+---------+-----+-----+\n") ;
|
306
|
+
else
|
307
|
+
printf (" +-----+-----+---------+------+---+---Pi ?---+---+------+---------+-----+-----+\n") ;
|
294
308
|
}
|
295
309
|
|
296
310
|
|
297
|
-
void piPlusReadall (int model)
|
311
|
+
static void piPlusReadall (int model)
|
298
312
|
{
|
299
313
|
int pin ;
|
300
314
|
|
@@ -311,6 +325,13 @@ void piPlusReadall (int model)
|
|
311
325
|
}
|
312
326
|
|
313
327
|
|
328
|
+
/*
|
329
|
+
* doReadall:
|
330
|
+
* Generic read all pins called from main program. Works out the Pi type
|
331
|
+
* and calls the appropriate function.
|
332
|
+
*********************************************************************************
|
333
|
+
*/
|
334
|
+
|
314
335
|
void doReadall (void)
|
315
336
|
{
|
316
337
|
int model, rev, mem, maker, overVolted ;
|
@@ -325,10 +346,21 @@ void doReadall (void)
|
|
325
346
|
|
326
347
|
/**/ if ((model == PI_MODEL_A) || (model == PI_MODEL_B))
|
327
348
|
abReadall (model, rev) ;
|
328
|
-
else if ((model == PI_MODEL_BP) || (model == PI_MODEL_AP) || (model == PI_MODEL_2))
|
349
|
+
else if ((model == PI_MODEL_BP) || (model == PI_MODEL_AP) || (model == PI_MODEL_2) || (model == PI_MODEL_3) || (model == PI_MODEL_ZERO))
|
329
350
|
piPlusReadall (model) ;
|
330
351
|
else if (model == PI_MODEL_CM)
|
331
|
-
|
352
|
+
allReadall () ;
|
332
353
|
else
|
333
354
|
printf ("Oops - unable to determine board type... model: %d\n", model) ;
|
334
355
|
}
|
356
|
+
|
357
|
+
/*
|
358
|
+
* doAllReadall:
|
359
|
+
* Force reading of all pins regardless of Pi model
|
360
|
+
*********************************************************************************
|
361
|
+
*/
|
362
|
+
|
363
|
+
void doAllReadall (void)
|
364
|
+
{
|
365
|
+
allReadall () ;
|
366
|
+
}
|
@@ -1 +1 @@
|
|
1
|
-
#define VERSION "2.
|
1
|
+
#define VERSION "2.32"
|
@@ -0,0 +1,293 @@
|
|
1
|
+
/*
|
2
|
+
* ads1115.c:
|
3
|
+
* Extend wiringPi with the ADS1115 I2C 16-bit ADC
|
4
|
+
* Copyright (c) 2016 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
|
+
/*
|
26
|
+
*********************************************************************************
|
27
|
+
* We're going to work in a hybrid mode to fit in with the wiringPi way of
|
28
|
+
* doing things, so there will be 4 analog pin which read the 4 single-ended
|
29
|
+
* channels as usual, also some fake digitalOutputs - these are the control
|
30
|
+
* registers that allow the user to put it into single/diff mode, set the
|
31
|
+
* gain and data rates.
|
32
|
+
*********************************************************************************
|
33
|
+
*/
|
34
|
+
|
35
|
+
#include <byteswap.h>
|
36
|
+
#include <stdio.h>
|
37
|
+
#include <stdint.h>
|
38
|
+
|
39
|
+
#include <wiringPi.h>
|
40
|
+
#include <wiringPiI2C.h>
|
41
|
+
|
42
|
+
#include "ads1115.h"
|
43
|
+
|
44
|
+
// Bits in the config register (it's a 16-bit register)
|
45
|
+
|
46
|
+
#define CONFIG_OS_MASK (0x8000) // Operational Status Register
|
47
|
+
#define CONFIG_OS_SINGLE (0x8000) // Write - Starts a single-conversion
|
48
|
+
// Read 1 = Conversion complete
|
49
|
+
|
50
|
+
// The multiplexor
|
51
|
+
|
52
|
+
#define CONFIG_MUX_MASK (0x7000)
|
53
|
+
|
54
|
+
// Differential modes
|
55
|
+
|
56
|
+
#define CONFIG_MUX_DIFF_0_1 (0x0000) // Pos = AIN0, Neg = AIN1 (default)
|
57
|
+
#define CONFIG_MUX_DIFF_0_3 (0x1000) // Pos = AIN0, Neg = AIN3
|
58
|
+
#define CONFIG_MUX_DIFF_1_3 (0x2000) // Pos = AIN1, Neg = AIN3
|
59
|
+
#define CONFIG_MUX_DIFF_2_3 (0x3000) // Pos = AIN2, Neg = AIN3 (2nd differential channel)
|
60
|
+
|
61
|
+
// Single-ended modes
|
62
|
+
|
63
|
+
#define CONFIG_MUX_SINGLE_0 (0x4000) // AIN0
|
64
|
+
#define CONFIG_MUX_SINGLE_1 (0x5000) // AIN1
|
65
|
+
#define CONFIG_MUX_SINGLE_2 (0x6000) // AIN2
|
66
|
+
#define CONFIG_MUX_SINGLE_3 (0x7000) // AIN3
|
67
|
+
|
68
|
+
// Programmable Gain Amplifier
|
69
|
+
|
70
|
+
#define CONFIG_PGA_MASK (0x0E00)
|
71
|
+
#define CONFIG_PGA_6_144V (0x0000) // +/-6.144V range = Gain 2/3
|
72
|
+
#define CONFIG_PGA_4_096V (0x0200) // +/-4.096V range = Gain 1
|
73
|
+
#define CONFIG_PGA_2_048V (0x0400) // +/-2.048V range = Gain 2 (default)
|
74
|
+
#define CONFIG_PGA_1_024V (0x0600) // +/-1.024V range = Gain 4
|
75
|
+
#define CONFIG_PGA_0_512V (0x0800) // +/-0.512V range = Gain 8
|
76
|
+
#define CONFIG_PGA_0_256V (0x0A00) // +/-0.256V range = Gain 16
|
77
|
+
|
78
|
+
#define CONFIG_MODE (0x0100) // 0 is continuous, 1 is single-shot (default)
|
79
|
+
|
80
|
+
// Data Rate
|
81
|
+
|
82
|
+
#define CONFIG_DR_MASK (0x00E0)
|
83
|
+
#define CONFIG_DR_8SPS (0x0000) // 8 samples per second
|
84
|
+
#define CONFIG_DR_16SPS (0x0020) // 16 samples per second
|
85
|
+
#define CONFIG_DR_32SPS (0x0040) // 32 samples per second
|
86
|
+
#define CONFIG_DR_64SPS (0x0060) // 64 samples per second
|
87
|
+
#define CONFIG_DR_128SPS (0x0080) // 128 samples per second (default)
|
88
|
+
#define CONFIG_DR_475SPS (0x00A0) // 475 samples per second
|
89
|
+
#define CONFIG_DR_860SPS (0x00C0) // 860 samples per second
|
90
|
+
|
91
|
+
// Comparator mode
|
92
|
+
|
93
|
+
#define CONFIG_CMODE_MASK (0x0010)
|
94
|
+
#define CONFIG_CMODE_TRAD (0x0000) // Traditional comparator with hysteresis (default)
|
95
|
+
#define CONFIG_CMODE_WINDOW (0x0010) // Window comparator
|
96
|
+
|
97
|
+
// Comparator polarity - the polarity of the output alert/rdy pin
|
98
|
+
|
99
|
+
#define CONFIG_CPOL_MASK (0x0008)
|
100
|
+
#define CONFIG_CPOL_ACTVLOW (0x0000) // Active low (default)
|
101
|
+
#define CONFIG_CPOL_ACTVHI (0x0008) // Active high
|
102
|
+
|
103
|
+
// Latching comparator - does the alert/rdy pin latch
|
104
|
+
|
105
|
+
#define CONFIG_CLAT_MASK (0x0004)
|
106
|
+
#define CONFIG_CLAT_NONLAT (0x0000) // Non-latching comparator (default)
|
107
|
+
#define CONFIG_CLAT_LATCH (0x0004) // Latching comparator
|
108
|
+
|
109
|
+
// Comparitor queue
|
110
|
+
|
111
|
+
#define CONFIG_CQUE_MASK (0x0003)
|
112
|
+
#define CONFIG_CQUE_1CONV (0x0000) // Assert after one conversions
|
113
|
+
#define CONFIG_CQUE_2CONV (0x0001) // Assert after two conversions
|
114
|
+
#define CONFIG_CQUE_4CONV (0x0002) // Assert after four conversions
|
115
|
+
#define CONFIG_CQUE_NONE (0x0003) // Disable the comparator (default)
|
116
|
+
|
117
|
+
#define CONFIG_DEFAULT (0x8583) // From the datasheet
|
118
|
+
|
119
|
+
|
120
|
+
static const uint16_t dataRates [8] =
|
121
|
+
{
|
122
|
+
CONFIG_DR_8SPS, CONFIG_DR_16SPS, CONFIG_DR_32SPS, CONFIG_DR_64SPS, CONFIG_DR_128SPS, CONFIG_DR_475SPS, CONFIG_DR_860SPS
|
123
|
+
} ;
|
124
|
+
|
125
|
+
static const uint16_t gains [6] =
|
126
|
+
{
|
127
|
+
CONFIG_PGA_6_144V, CONFIG_PGA_4_096V, CONFIG_PGA_2_048V, CONFIG_PGA_1_024V, CONFIG_PGA_0_512V, CONFIG_PGA_0_256V
|
128
|
+
} ;
|
129
|
+
|
130
|
+
|
131
|
+
/*
|
132
|
+
* analogRead:
|
133
|
+
* Pin is the channel to sample on the device.
|
134
|
+
* Channels 0-3 are single ended inputs,
|
135
|
+
* channels 4-7 are the various differential combinations.
|
136
|
+
*********************************************************************************
|
137
|
+
*/
|
138
|
+
|
139
|
+
static int myAnalogRead (struct wiringPiNodeStruct *node, int pin)
|
140
|
+
{
|
141
|
+
int chan = pin - node->pinBase ;
|
142
|
+
int16_t result ;
|
143
|
+
uint16_t config = CONFIG_DEFAULT ;
|
144
|
+
|
145
|
+
chan &= 7 ;
|
146
|
+
|
147
|
+
// Setup the configuration register
|
148
|
+
|
149
|
+
// Set PGA/voltage range
|
150
|
+
|
151
|
+
config &= ~CONFIG_PGA_MASK ;
|
152
|
+
config |= node->data0 ;
|
153
|
+
|
154
|
+
// Set sample speed
|
155
|
+
|
156
|
+
config &= ~CONFIG_DR_MASK ;
|
157
|
+
config |= node->data1 ;
|
158
|
+
|
159
|
+
// Set single-ended channel or differential mode
|
160
|
+
|
161
|
+
config &= ~CONFIG_MUX_MASK ;
|
162
|
+
|
163
|
+
switch (chan)
|
164
|
+
{
|
165
|
+
case 0: config |= CONFIG_MUX_SINGLE_0 ; break ;
|
166
|
+
case 1: config |= CONFIG_MUX_SINGLE_1 ; break ;
|
167
|
+
case 2: config |= CONFIG_MUX_SINGLE_2 ; break ;
|
168
|
+
case 3: config |= CONFIG_MUX_SINGLE_3 ; break ;
|
169
|
+
|
170
|
+
case 4: config |= CONFIG_MUX_DIFF_0_1 ; break ;
|
171
|
+
case 5: config |= CONFIG_MUX_DIFF_2_3 ; break ;
|
172
|
+
case 6: config |= CONFIG_MUX_DIFF_0_3 ; break ;
|
173
|
+
case 7: config |= CONFIG_MUX_DIFF_1_3 ; break ;
|
174
|
+
}
|
175
|
+
|
176
|
+
// Start a single conversion
|
177
|
+
|
178
|
+
config |= CONFIG_OS_SINGLE ;
|
179
|
+
config = __bswap_16 (config) ;
|
180
|
+
wiringPiI2CWriteReg16 (node->fd, 1, config) ;
|
181
|
+
|
182
|
+
// Wait for the conversion to complete
|
183
|
+
|
184
|
+
for (;;)
|
185
|
+
{
|
186
|
+
result = wiringPiI2CReadReg16 (node->fd, 1) ;
|
187
|
+
result = __bswap_16 (result) ;
|
188
|
+
if ((result & CONFIG_OS_MASK) != 0)
|
189
|
+
break ;
|
190
|
+
delayMicroseconds (100) ;
|
191
|
+
}
|
192
|
+
|
193
|
+
result = wiringPiI2CReadReg16 (node->fd, 0) ;
|
194
|
+
result = __bswap_16 (result) ;
|
195
|
+
|
196
|
+
// Sometimes with a 0v input on a single-ended channel the internal 0v reference
|
197
|
+
// can be higher than the input, so you get a negative result...
|
198
|
+
|
199
|
+
if ( (chan < 4) && (result < 0) )
|
200
|
+
return 0 ;
|
201
|
+
else
|
202
|
+
return (int)result ;
|
203
|
+
}
|
204
|
+
|
205
|
+
|
206
|
+
/*
|
207
|
+
* digitalWrite:
|
208
|
+
* It may seem odd to have a digital write here, but it's the best way
|
209
|
+
* to pass paramters into the chip in the wiringPi way of things.
|
210
|
+
* We have 2 digital registers:
|
211
|
+
* 0 is the gain control
|
212
|
+
* 1 is the data rate control
|
213
|
+
*********************************************************************************
|
214
|
+
*/
|
215
|
+
|
216
|
+
static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int data)
|
217
|
+
{
|
218
|
+
int chan = pin - node->pinBase ;
|
219
|
+
chan &= 3 ;
|
220
|
+
|
221
|
+
if (chan == 0) // Gain Control
|
222
|
+
{
|
223
|
+
if ( (data < 0) || (data > 6) ) // Use default if out of range
|
224
|
+
data = 2 ;
|
225
|
+
node->data0 = gains [data] ;
|
226
|
+
}
|
227
|
+
else // Data rate control
|
228
|
+
{
|
229
|
+
if ( (data < 0) || (data > 7) ) // Use default if out of range
|
230
|
+
data = 4 ;
|
231
|
+
node->data0 = dataRates [data] ;
|
232
|
+
}
|
233
|
+
|
234
|
+
}
|
235
|
+
|
236
|
+
|
237
|
+
/*
|
238
|
+
* analogWrite:
|
239
|
+
* We're using this to write to the 2 comparitor threshold registers.
|
240
|
+
* We could use a digitalWrite here but as it's an analog comparison
|
241
|
+
* then it feels better to do it this way.
|
242
|
+
*********************************************************************************
|
243
|
+
*/
|
244
|
+
|
245
|
+
static void myAnalogWrite (struct wiringPiNodeStruct *node, int pin, int data)
|
246
|
+
{
|
247
|
+
int chan = pin - node->pinBase ;
|
248
|
+
int reg ;
|
249
|
+
int16_t ndata ;
|
250
|
+
|
251
|
+
chan &= 3 ;
|
252
|
+
|
253
|
+
reg = chan + 2 ;
|
254
|
+
|
255
|
+
/**/ if (data < -32767)
|
256
|
+
ndata = -32767 ;
|
257
|
+
else if (data > 32767)
|
258
|
+
ndata = 32767 ;
|
259
|
+
else
|
260
|
+
ndata = (int16_t)data ;
|
261
|
+
|
262
|
+
ndata = __bswap_16 (ndata) ;
|
263
|
+
wiringPiI2CWriteReg16 (node->fd, reg, data) ;
|
264
|
+
}
|
265
|
+
|
266
|
+
|
267
|
+
|
268
|
+
/*
|
269
|
+
* ads1115Setup:
|
270
|
+
* Create a new wiringPi device node for an ads1115 on the Pi's
|
271
|
+
* I2C interface.
|
272
|
+
*********************************************************************************
|
273
|
+
*/
|
274
|
+
|
275
|
+
int ads1115Setup (const int pinBase, int i2cAddr)
|
276
|
+
{
|
277
|
+
struct wiringPiNodeStruct *node ;
|
278
|
+
int fd ;
|
279
|
+
|
280
|
+
if ((fd = wiringPiI2CSetup (i2cAddr)) < 0)
|
281
|
+
return FALSE ;
|
282
|
+
|
283
|
+
node = wiringPiNewNode (pinBase, 8) ;
|
284
|
+
|
285
|
+
node->fd = fd ;
|
286
|
+
node->data0 = CONFIG_PGA_4_096V ; // Gain in data0
|
287
|
+
node->data1 = CONFIG_DR_128SPS ; // Samples/sec in data1
|
288
|
+
node->analogRead = myAnalogRead ;
|
289
|
+
node->analogWrite = myAnalogWrite ;
|
290
|
+
node->digitalWrite = myDigitalWrite ;
|
291
|
+
|
292
|
+
return TRUE ;
|
293
|
+
}
|