3 * Part of the GPIO program to test, peek, poke and otherwise
4 * noodle with the GPIO hardware on the Raspberry Pi.
5 * Copyright (c) 2012-2013 Gordon Henderson
6 ***********************************************************************
7 * This file is part of wiringPi:
8 * https://projects.drogon.net/raspberry-pi/wiringpi/
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 published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
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.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
22 ***********************************************************************
33 #include <sys/types.h>
53 #include "extensions.h"
55 extern int wiringPiDebug ;
62 // Local structure to hold details
64 struct extensionFunctionStruct
67 int (*function)(char *progName, int pinBase, char *params) ;
73 * Check & return an integer at the given location (prefixed by a :)
74 *********************************************************************************
77 static char *extractInt (char *progName, char *p, int *num)
81 fprintf (stderr, "%s: colon expected\n", progName) ;
89 fprintf (stderr, "%s: digit expected\n", progName) ;
93 *num = strtol (p, NULL, 0) ;
103 * doExtensionMcp23008:
104 * MCP23008 - 8-bit I2C GPIO expansion chip
105 * mcp23002:base:i2cAddr
106 *********************************************************************************
109 static int doExtensionMcp23008 (char *progName, int pinBase, char *params)
113 if ((params = extractInt (progName, params, &i2c)) == NULL)
116 if ((i2c < 0x03) || (i2c > 0x77))
118 fprintf (stderr, "%s: i2c address (0x%X) out of range\n", progName, i2c) ;
122 mcp23008Setup (pinBase, i2c) ;
129 * doExtensionMcp23016:
130 * MCP230016- 16-bit I2C GPIO expansion chip
131 * mcp23016:base:i2cAddr
132 *********************************************************************************
135 static int doExtensionMcp23016 (char *progName, int pinBase, char *params)
139 if ((params = extractInt (progName, params, &i2c)) == NULL)
142 if ((i2c < 0x03) || (i2c > 0x77))
144 fprintf (stderr, "%s: i2c address (0x%X) out of range\n", progName, i2c) ;
148 mcp23016Setup (pinBase, i2c) ;
155 * doExtensionMcp23017:
156 * MCP230017- 16-bit I2C GPIO expansion chip
157 * mcp23017:base:i2cAddr
158 *********************************************************************************
161 static int doExtensionMcp23017 (char *progName, int pinBase, char *params)
165 if ((params = extractInt (progName, params, &i2c)) == NULL)
168 if ((i2c < 0x03) || (i2c > 0x77))
170 fprintf (stderr, "%s: i2c address (0x%X) out of range\n", progName, i2c) ;
174 mcp23017Setup (pinBase, i2c) ;
181 * doExtensionMcp23s08:
182 * MCP23s08 - 8-bit SPI GPIO expansion chip
183 * mcp23s08:base:spi:port
184 *********************************************************************************
187 static int doExtensionMcp23s08 (char *progName, int pinBase, char *params)
191 if ((params = extractInt (progName, params, &spi)) == NULL)
194 if ((spi < 0) || (spi > 1))
196 fprintf (stderr, "%s: SPI address (%d) out of range\n", progName, spi) ;
200 if ((params = extractInt (progName, params, &port)) == NULL)
203 if ((port < 0) || (port > 7))
205 fprintf (stderr, "%s: port address (%d) out of range\n", progName, port) ;
209 mcp23s08Setup (pinBase, spi, port) ;
216 * doExtensionMcp23s17:
217 * MCP23s17 - 16-bit SPI GPIO expansion chip
218 * mcp23s17:base:spi:port
219 *********************************************************************************
222 static int doExtensionMcp23s17 (char *progName, int pinBase, char *params)
226 if ((params = extractInt (progName, params, &spi)) == NULL)
229 if ((spi < 0) || (spi > 1))
231 fprintf (stderr, "%s: SPI address (%d) out of range\n", progName, spi) ;
235 if ((params = extractInt (progName, params, &port)) == NULL)
238 if ((port < 0) || (port > 7))
240 fprintf (stderr, "%s: port address (%d) out of range\n", progName, port) ;
244 mcp23s17Setup (pinBase, spi, port) ;
252 * Shift Register 74x595
253 * sr595:base:pins:data:clock:latch
254 *********************************************************************************
257 static int doExtensionSr595 (char *progName, int pinBase, char *params)
259 int pins, data, clock, latch ;
263 if ((params = extractInt (progName, params, &pins)) == NULL)
266 if ((pins < 8) || (pins > 32))
268 fprintf (stderr, "%s: pin count (%d) out of range - 8-32 expected.\n", progName, pins) ;
272 if ((params = extractInt (progName, params, &data)) == NULL)
275 if ((params = extractInt (progName, params, &clock)) == NULL)
278 if ((params = extractInt (progName, params, &latch)) == NULL)
281 sr595Setup (pinBase, pins, data, clock, latch) ;
288 * doExtensionPcf8574:
289 * Digital IO (Crude!)
290 * pcf8574:base:i2cAddr
291 *********************************************************************************
294 static int doExtensionPcf8574 (char *progName, int pinBase, char *params)
298 if ((params = extractInt (progName, params, &i2c)) == NULL)
301 if ((i2c < 0x03) || (i2c > 0x77))
303 fprintf (stderr, "%s: i2c address (0x%X) out of range\n", progName, i2c) ;
307 pcf8574Setup (pinBase, i2c) ;
314 * doExtensionPcf8591:
316 * pcf8591:base:i2cAddr
317 *********************************************************************************
320 static int doExtensionPcf8591 (char *progName, int pinBase, char *params)
324 if ((params = extractInt (progName, params, &i2c)) == NULL)
327 if ((i2c < 0x03) || (i2c > 0x77))
329 fprintf (stderr, "%s: i2c address (0x%X) out of range\n", progName, i2c) ;
333 pcf8591Setup (pinBase, i2c) ;
340 * doExtensionMax31855:
342 * max31855:base:spiChan
343 *********************************************************************************
346 static int doExtensionMax31855 (char *progName, int pinBase, char *params)
350 if ((params = extractInt (progName, params, &spi)) == NULL)
353 if ((spi < 0) || (spi > 1))
355 fprintf (stderr, "%s: SPI channel (%d) out of range\n", progName, spi) ;
359 max31855Setup (pinBase, spi) ;
366 * doExtensionMcp3002:
368 * mcp3002:base:spiChan
369 *********************************************************************************
372 static int doExtensionMcp3002 (char *progName, int pinBase, char *params)
376 if ((params = extractInt (progName, params, &spi)) == NULL)
379 if ((spi < 0) || (spi > 1))
381 fprintf (stderr, "%s: SPI channel (%d) out of range\n", progName, spi) ;
385 mcp3002Setup (pinBase, spi) ;
392 * doExtensionMcp3004:
394 * mcp3004:base:spiChan
395 *********************************************************************************
398 static int doExtensionMcp3004 (char *progName, int pinBase, char *params)
402 if ((params = extractInt (progName, params, &spi)) == NULL)
405 if ((spi < 0) || (spi > 1))
407 fprintf (stderr, "%s: SPI channel (%d) out of range\n", progName, spi) ;
411 mcp3004Setup (pinBase, spi) ;
418 * doExtensionMax5322:
420 * max5322:base:spiChan
421 *********************************************************************************
424 static int doExtensionMax5322 (char *progName, int pinBase, char *params)
428 if ((params = extractInt (progName, params, &spi)) == NULL)
431 if ((spi < 0) || (spi > 1))
433 fprintf (stderr, "%s: SPI channel (%d) out of range\n", progName, spi) ;
437 max5322Setup (pinBase, spi) ;
444 * doExtensionMcp4802:
446 * mcp4802:base:spiChan
447 *********************************************************************************
450 static int doExtensionMcp4802 (char *progName, int pinBase, char *params)
454 if ((params = extractInt (progName, params, &spi)) == NULL)
457 if ((spi < 0) || (spi > 1))
459 fprintf (stderr, "%s: SPI channel (%d) out of range\n", progName, spi) ;
463 mcp4802Setup (pinBase, spi) ;
470 * doExtensionMcp3422:
472 * mcp3422:base:i2cAddr
473 *********************************************************************************
476 static int doExtensionMcp3422 (char *progName, int pinBase, char *params)
478 int i2c, sampleRate, gain ;
480 if ((params = extractInt (progName, params, &i2c)) == NULL)
483 if ((i2c < 0x03) || (i2c > 0x77))
485 fprintf (stderr, "%s: i2c address (0x%X) out of range\n", progName, i2c) ;
489 if ((params = extractInt (progName, params, &sampleRate)) == NULL)
492 if ((sampleRate < 0) || (sampleRate > 3))
494 fprintf (stderr, "%s: sample rate (%d) out of range\n", progName, sampleRate) ;
498 if ((params = extractInt (progName, params, &gain)) == NULL)
501 if ((gain < 0) || (gain > 3))
503 fprintf (stderr, "%s: gain (%d) out of range\n", progName, gain) ;
507 mcp3422Setup (pinBase, i2c, sampleRate, gain) ;
515 *********************************************************************************
518 struct extensionFunctionStruct extensionFunctions [] =
520 { "mcp23008", &doExtensionMcp23008 },
521 { "mcp23016", &doExtensionMcp23016 },
522 { "mcp23017", &doExtensionMcp23017 },
523 { "mcp23s08", &doExtensionMcp23s08 },
524 { "mcp23s17", &doExtensionMcp23s17 },
525 { "sr595", &doExtensionSr595 },
526 { "pcf8574", &doExtensionPcf8574 },
527 { "pcf8591", &doExtensionPcf8591 },
528 { "mcp3002", &doExtensionMcp3002 },
529 { "mcp3004", &doExtensionMcp3004 },
530 { "mcp4802", &doExtensionMcp4802 },
531 { "mcp3422", &doExtensionMcp3422 },
532 { "max31855", &doExtensionMax31855 },
533 { "max5322", &doExtensionMax5322 },
540 * Load in a wiringPi extension
541 *********************************************************************************
544 int doExtension (char *progName, char *extensionData)
547 char *extension = extensionData ;
548 struct extensionFunctionStruct *extensionFn ;
551 // Get the extension extension name by finding the first colon
556 if (!*p) // ran out of characters
558 fprintf (stderr, "%s: extension name not terminated by a colon\n", progName) ;
568 fprintf (stderr, "%s: pinBase number expected after extension name\n", progName) ;
574 if (pinBase > 1000000000)
576 fprintf (stderr, "%s: pinBase too large\n", progName) ;
580 pinBase = pinBase * 10 + (*p - '0') ;
586 fprintf (stderr, "%s: pinBase (%d) too small. Minimum is 64.\n", progName, pinBase) ;
590 // Search for extensions:
592 for (extensionFn = extensionFunctions ; extensionFn->name != NULL ; ++extensionFn)
594 if (strcmp (extensionFn->name, extension) == 0)
595 return extensionFn->function (progName, pinBase, p) ;
598 fprintf (stderr, "%s: extension %s not found\n", progName, extension) ;