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>
54 #include "extensions.h"
56 extern int wiringPiDebug ;
63 // Local structure to hold details
65 struct extensionFunctionStruct
68 int (*function)(char *progName, int pinBase, char *params) ;
74 * Check & return an integer at the given location (prefixed by a :)
75 *********************************************************************************
78 static char *extractInt (char *progName, char *p, int *num)
82 fprintf (stderr, "%s: colon expected\n", progName) ;
90 fprintf (stderr, "%s: digit expected\n", progName) ;
94 *num = strtol (p, NULL, 0) ;
104 * doExtensionMcp23008:
105 * MCP23008 - 8-bit I2C GPIO expansion chip
106 * mcp23002:base:i2cAddr
107 *********************************************************************************
110 static int doExtensionMcp23008 (char *progName, int pinBase, char *params)
114 if ((params = extractInt (progName, params, &i2c)) == NULL)
117 if ((i2c < 0x03) || (i2c > 0x77))
119 fprintf (stderr, "%s: i2c address (0x%X) out of range\n", progName, i2c) ;
123 mcp23008Setup (pinBase, i2c) ;
130 * doExtensionMcp23016:
131 * MCP230016- 16-bit I2C GPIO expansion chip
132 * mcp23016:base:i2cAddr
133 *********************************************************************************
136 static int doExtensionMcp23016 (char *progName, int pinBase, char *params)
140 if ((params = extractInt (progName, params, &i2c)) == NULL)
143 if ((i2c < 0x03) || (i2c > 0x77))
145 fprintf (stderr, "%s: i2c address (0x%X) out of range\n", progName, i2c) ;
149 mcp23016Setup (pinBase, i2c) ;
156 * doExtensionMcp23017:
157 * MCP230017- 16-bit I2C GPIO expansion chip
158 * mcp23017:base:i2cAddr
159 *********************************************************************************
162 static int doExtensionMcp23017 (char *progName, int pinBase, char *params)
166 if ((params = extractInt (progName, params, &i2c)) == NULL)
169 if ((i2c < 0x03) || (i2c > 0x77))
171 fprintf (stderr, "%s: i2c address (0x%X) out of range\n", progName, i2c) ;
175 mcp23017Setup (pinBase, i2c) ;
182 * doExtensionMcp23s08:
183 * MCP23s08 - 8-bit SPI GPIO expansion chip
184 * mcp23s08:base:spi:port
185 *********************************************************************************
188 static int doExtensionMcp23s08 (char *progName, int pinBase, char *params)
192 if ((params = extractInt (progName, params, &spi)) == NULL)
195 if ((spi < 0) || (spi > 1))
197 fprintf (stderr, "%s: SPI address (%d) out of range\n", progName, spi) ;
201 if ((params = extractInt (progName, params, &port)) == NULL)
204 if ((port < 0) || (port > 7))
206 fprintf (stderr, "%s: port address (%d) out of range\n", progName, port) ;
210 mcp23s08Setup (pinBase, spi, port) ;
217 * doExtensionMcp23s17:
218 * MCP23s17 - 16-bit SPI GPIO expansion chip
219 * mcp23s17:base:spi:port
220 *********************************************************************************
223 static int doExtensionMcp23s17 (char *progName, int pinBase, char *params)
227 if ((params = extractInt (progName, params, &spi)) == NULL)
230 if ((spi < 0) || (spi > 1))
232 fprintf (stderr, "%s: SPI address (%d) out of range\n", progName, spi) ;
236 if ((params = extractInt (progName, params, &port)) == NULL)
239 if ((port < 0) || (port > 7))
241 fprintf (stderr, "%s: port address (%d) out of range\n", progName, port) ;
245 mcp23s17Setup (pinBase, spi, port) ;
253 * Shift Register 74x595
254 * sr595:base:pins:data:clock:latch
255 *********************************************************************************
258 static int doExtensionSr595 (char *progName, int pinBase, char *params)
260 int pins, data, clock, latch ;
264 if ((params = extractInt (progName, params, &pins)) == NULL)
267 if ((pins < 8) || (pins > 32))
269 fprintf (stderr, "%s: pin count (%d) out of range - 8-32 expected.\n", progName, pins) ;
273 if ((params = extractInt (progName, params, &data)) == NULL)
276 if ((params = extractInt (progName, params, &clock)) == NULL)
279 if ((params = extractInt (progName, params, &latch)) == NULL)
282 sr595Setup (pinBase, pins, data, clock, latch) ;
289 * doExtensionPcf8574:
290 * Digital IO (Crude!)
291 * pcf8574:base:i2cAddr
292 *********************************************************************************
295 static int doExtensionPcf8574 (char *progName, int pinBase, char *params)
299 if ((params = extractInt (progName, params, &i2c)) == NULL)
302 if ((i2c < 0x03) || (i2c > 0x77))
304 fprintf (stderr, "%s: i2c address (0x%X) out of range\n", progName, i2c) ;
308 pcf8574Setup (pinBase, i2c) ;
315 * doExtensionPcf8591:
317 * pcf8591:base:i2cAddr
318 *********************************************************************************
321 static int doExtensionPcf8591 (char *progName, int pinBase, char *params)
325 if ((params = extractInt (progName, params, &i2c)) == NULL)
328 if ((i2c < 0x03) || (i2c > 0x77))
330 fprintf (stderr, "%s: i2c address (0x%X) out of range\n", progName, i2c) ;
334 pcf8591Setup (pinBase, i2c) ;
341 * doExtensionMax31855:
343 * max31855:base:spiChan
344 *********************************************************************************
347 static int doExtensionMax31855 (char *progName, int pinBase, char *params)
351 if ((params = extractInt (progName, params, &spi)) == NULL)
354 if ((spi < 0) || (spi > 1))
356 fprintf (stderr, "%s: SPI channel (%d) out of range\n", progName, spi) ;
360 max31855Setup (pinBase, spi) ;
367 * doExtensionMcp3002:
369 * mcp3002:base:spiChan
370 *********************************************************************************
373 static int doExtensionMcp3002 (char *progName, int pinBase, char *params)
377 if ((params = extractInt (progName, params, &spi)) == NULL)
380 if ((spi < 0) || (spi > 1))
382 fprintf (stderr, "%s: SPI channel (%d) out of range\n", progName, spi) ;
386 mcp3002Setup (pinBase, spi) ;
393 * doExtensionMcp3004:
395 * mcp3004:base:spiChan
396 *********************************************************************************
399 static int doExtensionMcp3004 (char *progName, int pinBase, char *params)
403 if ((params = extractInt (progName, params, &spi)) == NULL)
406 if ((spi < 0) || (spi > 1))
408 fprintf (stderr, "%s: SPI channel (%d) out of range\n", progName, spi) ;
412 mcp3004Setup (pinBase, spi) ;
419 * doExtensionMax5322:
421 * max5322:base:spiChan
422 *********************************************************************************
425 static int doExtensionMax5322 (char *progName, int pinBase, char *params)
429 if ((params = extractInt (progName, params, &spi)) == NULL)
432 if ((spi < 0) || (spi > 1))
434 fprintf (stderr, "%s: SPI channel (%d) out of range\n", progName, spi) ;
438 max5322Setup (pinBase, spi) ;
445 * doExtensionMcp4802:
447 * mcp4802:base:spiChan
448 *********************************************************************************
451 static int doExtensionMcp4802 (char *progName, int pinBase, char *params)
455 if ((params = extractInt (progName, params, &spi)) == NULL)
458 if ((spi < 0) || (spi > 1))
460 fprintf (stderr, "%s: SPI channel (%d) out of range\n", progName, spi) ;
464 mcp4802Setup (pinBase, spi) ;
472 * Analog Output (LED Driver)
474 *********************************************************************************
477 static int doExtensionSn3218 (char *progName, int pinBase, char *params)
479 sn3218Setup (pinBase) ;
485 * doExtensionMcp3422:
487 * mcp3422:base:i2cAddr
488 *********************************************************************************
491 static int doExtensionMcp3422 (char *progName, int pinBase, char *params)
493 int i2c, sampleRate, gain ;
495 if ((params = extractInt (progName, params, &i2c)) == NULL)
498 if ((i2c < 0x03) || (i2c > 0x77))
500 fprintf (stderr, "%s: i2c address (0x%X) out of range\n", progName, i2c) ;
504 if ((params = extractInt (progName, params, &sampleRate)) == NULL)
507 if ((sampleRate < 0) || (sampleRate > 3))
509 fprintf (stderr, "%s: sample rate (%d) out of range\n", progName, sampleRate) ;
513 if ((params = extractInt (progName, params, &gain)) == NULL)
516 if ((gain < 0) || (gain > 3))
518 fprintf (stderr, "%s: gain (%d) out of range\n", progName, gain) ;
522 mcp3422Setup (pinBase, i2c, sampleRate, gain) ;
530 *********************************************************************************
533 struct extensionFunctionStruct extensionFunctions [] =
535 { "mcp23008", &doExtensionMcp23008 },
536 { "mcp23016", &doExtensionMcp23016 },
537 { "mcp23017", &doExtensionMcp23017 },
538 { "mcp23s08", &doExtensionMcp23s08 },
539 { "mcp23s17", &doExtensionMcp23s17 },
540 { "sr595", &doExtensionSr595 },
541 { "pcf8574", &doExtensionPcf8574 },
542 { "pcf8591", &doExtensionPcf8591 },
543 { "mcp3002", &doExtensionMcp3002 },
544 { "mcp3004", &doExtensionMcp3004 },
545 { "mcp4802", &doExtensionMcp4802 },
546 { "mcp3422", &doExtensionMcp3422 },
547 { "max31855", &doExtensionMax31855 },
548 { "max5322", &doExtensionMax5322 },
549 { "sn3218", &doExtensionSn3218 },
556 * Load in a wiringPi extension
557 *********************************************************************************
560 int doExtension (char *progName, char *extensionData)
563 char *extension = extensionData ;
564 struct extensionFunctionStruct *extensionFn ;
567 // Get the extension extension name by finding the first colon
572 if (!*p) // ran out of characters
574 fprintf (stderr, "%s: extension name not terminated by a colon\n", progName) ;
584 fprintf (stderr, "%s: pinBase number expected after extension name\n", progName) ;
590 if (pinBase > 1000000000)
592 fprintf (stderr, "%s: pinBase too large\n", progName) ;
596 pinBase = pinBase * 10 + (*p - '0') ;
602 fprintf (stderr, "%s: pinBase (%d) too small. Minimum is 64.\n", progName, pinBase) ;
606 // Search for extensions:
608 for (extensionFn = extensionFunctions ; extensionFn->name != NULL ; ++extensionFn)
610 if (strcmp (extensionFn->name, extension) == 0)
611 return extensionFn->function (progName, pinBase, p) ;
614 fprintf (stderr, "%s: extension %s not found\n", progName, extension) ;