/*
* gpio.c:
- * Set-UID command-line interface to the Raspberry Pi's GPIO
+ * Swiss-Army-Knife, Set-UID command-line interface to the Raspberry
+ * Pi's GPIO.
* Copyright (c) 2012 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
***********************************************************************
*/
-#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>
+#include <wiringPi.h>
+#include <gertboard.h>
+
#ifndef TRUE
# define TRUE (1==1)
# define FALSE (1==2)
#endif
-#define VERSION "1.1"
+#define VERSION "1.2"
static int wpMode ;
" gpio [-p] <read/write/mode> ...\n"
" gpio export/edge/unexport/unexportall/exports ...\n"
" gpio drive <group> <value>\n"
- " gpio load spi/i2c" ;
+ " gpio pwm-bal/pwm-ms \n"
+ " gpio pwmr <range> \n"
+ " gpio pwmc <divider> \n"
+ " gpio load spi/i2c\n"
+ " gpio gbr <channel>\n"
+ " gpio gbw <channel> <value>" ; // No trailing newline needed here.
/*
*********************************************************************************
*/
-void doPadDrive (int argc, char *argv [])
+static void doPadDrive (int argc, char *argv [])
{
int group, val ;
}
+/*
+ * doGbw:
+ * gpio gbw channel value
+ *********************************************************************************
+ */
+
+static void doGbw (int argc, char *argv [])
+{
+ int channel, value ;
+
+ if (argc != 4)
+ {
+ fprintf (stderr, "Usage: %s gbr <channel> <value>\n", argv [0]) ;
+ exit (1) ;
+ }
+
+ channel = atoi (argv [2]) ;
+ value = atoi (argv [3]) ;
+
+ if ((channel < 0) || (channel > 1))
+ {
+ fprintf (stderr, "%s: channel must be 0 or 1\n", argv [0]) ;
+ exit (1) ;
+ }
+
+ if ((value < 0) || (value > 1023))
+ {
+ fprintf (stderr, "%s: value must be from 0 to 255\n", argv [0]) ;
+ exit (1) ;
+ }
+
+ if (gertboardSPISetup () == -1)
+ {
+ fprintf (stderr, "Unable to initialise the Gertboard SPI interface: %s\n", strerror (errno)) ;
+ exit (1) ;
+ }
+
+ gertboardAnalogWrite (channel, value) ;
+}
+
+
+/*
+ * doGbr:
+ * gpio gbr channel
+ *********************************************************************************
+ */
+
+static void doGbr (int argc, char *argv [])
+{
+ int channel ;
+
+ if (argc != 3)
+ {
+ fprintf (stderr, "Usage: %s gbr <channel>\n", argv [0]) ;
+ exit (1) ;
+ }
+
+ channel = atoi (argv [2]) ;
+
+ if ((channel < 0) || (channel > 1))
+ {
+ fprintf (stderr, "%s: channel must be 0 or 1\n", argv [0]) ;
+ exit (1) ;
+ }
+
+ if (gertboardSPISetup () == -1)
+ {
+ fprintf (stderr, "Unable to initialise the Gertboard SPI interface: %s\n", strerror (errno)) ;
+ exit (1) ;
+ }
+
+ printf ("%d\n",gertboardAnalogRead (channel)) ;
+}
+
+
+
/*
* doWrite:
* gpio write pin value
*********************************************************************************
*/
-void doWrite (int argc, char *argv [])
+static void doWrite (int argc, char *argv [])
{
int pin, val ;
}
+/*
+ * doPwmMode: doPwmRange: doPwmClock:
+ * Change the PWM mode, range and clock divider values
+ *********************************************************************************
+ */
+
+static void doPwmMode (int mode)
+{
+ pwmSetMode (mode) ;
+}
+
+static void doPwmRange (int argc, char *argv [])
+{
+ unsigned int range ;
+
+ if (argc != 3)
+ {
+ fprintf (stderr, "Usage: %s pwmr <range>\n", argv [0]) ;
+ exit (1) ;
+ }
+
+ range = (unsigned int)strtoul (argv [2], NULL, 10) ;
+
+ if (range == 0)
+ {
+ fprintf (stderr, "%s: range must be > 0\n", argv [0]) ;
+ exit (1) ;
+ }
+
+ pwmSetRange (range) ;
+}
+
+static void doPwmClock (int argc, char *argv [])
+{
+ unsigned int clock ;
+
+ if (argc != 3)
+ {
+ fprintf (stderr, "Usage: %s pwmc <clock>\n", argv [0]) ;
+ exit (1) ;
+ }
+
+ clock = (unsigned int)strtoul (argv [2], NULL, 10) ;
+
+ if ((clock < 1) || (clock > 4095))
+ {
+ fprintf (stderr, "%s: clock must be between 0 and 4096\n", argv [0]) ;
+ exit (1) ;
+ }
+
+ pwmSetClock (clock) ;
+}
+
+
/*
* main:
* Start here
if (strcasecmp (argv [1], "drive") == 0) { doPadDrive (argc, argv) ; return 0 ; }
if (strcasecmp (argv [1], "load" ) == 0) { doLoad (argc, argv) ; return 0 ; }
+// Gertboard commands
+
+ if (strcasecmp (argv [1], "gbr" ) == 0) { doGbr (argc, argv) ; return 0 ; }
+ if (strcasecmp (argv [1], "gbw" ) == 0) { doGbw (argc, argv) ; return 0 ; }
+
// Check for -g argument
if (strcasecmp (argv [1], "-g") == 0)
wpMode = WPI_MODE_PINS ;
}
+// Check for PWM operations
+
+ if (wpMode != WPI_MODE_PIFACE)
+ {
+ if (strcasecmp (argv [1], "pwm-bal") == 0) { doPwmMode (PWM_MODE_BAL) ; return 0 ; }
+ if (strcasecmp (argv [1], "pwm-ms") == 0) { doPwmMode (PWM_MODE_MS) ; return 0 ; }
+ if (strcasecmp (argv [1], "pwmr") == 0) { doPwmRange (argc, argv) ; return 0 ; }
+ if (strcasecmp (argv [1], "pwmc") == 0) { doPwmClock (argc, argv) ; return 0 ; }
+ }
+
// Check for wiring commands
/**/ if (strcasecmp (argv [1], "read" ) == 0) doRead (argc, argv) ;