New timing methods

This commit is contained in:
Gordon Drogon
2012-08-17 10:16:31 +01:00
committed by Philip Howard
parent 43aa862e97
commit d24a8fc4cb
24 changed files with 2308 additions and 392 deletions

View File

@@ -7,16 +7,16 @@
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* You should have received a copy of the GNU Lesser General Public License
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
@@ -37,10 +37,10 @@
static int wpMode ;
char *usage = "Usage: gpio -v\n"
" gpio [-g] <read/write/pwm/mode/> ...\n"
" gpio <export/unexport/exports> ..." ;
" gpio [-g] <read/write/pwm/mode> ...\n"
" gpio [-p] <read/write/mode> ...\n"
" gpio <export/edge/unexport/unexportall/exports> ..." ;
/*
@@ -54,7 +54,7 @@ void doExports (void)
int fd ;
int i, l, first ;
char fName [128] ;
char dir, val ;
char buf [16] ;
// Rather crude, but who knows what others are up to...
@@ -75,19 +75,14 @@ void doExports (void)
printf ("%4d: ", i) ;
if ((l = read (fd, &dir, 1)) == 0)
{
printf ("Empty direction file (why?)\n") ;
close (fd) ;
continue ;
}
if ((l = read (fd, buf, 16)) == 0)
sprintf (buf, "%s", "?") ;
buf [l] = 0 ;
if ((buf [strlen (buf) - 1]) == '\n')
buf [strlen (buf) - 1] = 0 ;
/**/ if (dir == 'o')
printf ("Output ") ;
else if (dir == 'i')
printf ("Input ") ;
else
printf ("Wrong ") ;
printf ("%-3s", buf) ;
close (fd) ;
@@ -100,19 +95,32 @@ void doExports (void)
continue ;
}
if ((l = read (fd, &val, 1)) == 0)
if ((l = read (fd, buf, 16)) == 0)
sprintf (buf, "%s", "?") ;
buf [l] = 0 ;
if ((buf [strlen (buf) - 1]) == '\n')
buf [strlen (buf) - 1] = 0 ;
printf (" %s", buf) ;
// Read any edge trigger file
sprintf (fName, "/sys/class/gpio/gpio%d/edge", i) ;
if ((fd = open (fName, O_RDONLY)) == -1)
{
printf ("Empty Value file (why?)\n") ;
close (fd) ;
printf ("\n") ;
continue ;
}
/**/ if (val == '0' )
printf ("(0)\n") ;
else if (val == '1')
printf ("(1)\n") ;
else
printf ("(?)\n") ;
if ((l = read (fd, buf, 16)) == 0)
sprintf (buf, "%s", "?") ;
buf [l] = 0 ;
if ((buf [strlen (buf) - 1]) == '\n')
buf [strlen (buf) - 1] = 0 ;
printf (" %-8s\n", buf) ;
close (fd) ;
}
@@ -161,9 +169,9 @@ void doExport (int argc, char *argv [])
exit (1) ;
}
/**/ if (strcasecmp (mode, "in") == 0)
/**/ if ((strcasecmp (mode, "in") == 0) || (strcasecmp (mode, "input") == 0))
fprintf (fd, "in\n") ;
else if (strcasecmp (mode, "out") == 0)
else if ((strcasecmp (mode, "out") == 0) || (strcasecmp (mode, "output") == 0))
fprintf (fd, "out\n") ;
else
{
@@ -184,7 +192,113 @@ void doExport (int argc, char *argv [])
fprintf (stderr, "%s: Unable to change ownership of the value file: %s\n", argv [1], strerror (errno)) ;
exit (1) ;
}
// Also change ownership of the edge file - if it exists
sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
if (chown (fName, uid, gid) != 0)
{
if (errno != ENOENT) // Silently ignore File not found - older kernel
{
fprintf (stderr, "%s: Unable to change ownership of the value file: %s\n", argv [1], strerror (errno)) ;
exit (1) ;
}
}
}
/*
* doEdge:
* gpio edge pin mode
* Easy access to changing the edge trigger on a GPIO pin
* This uses the /sys/class/gpio device interface.
*********************************************************************************
*/
void doEdge (int argc, char *argv [])
{
FILE *fd ;
int pin ;
char *mode ;
char fName [128] ;
uid_t uid ;
gid_t gid ;
if (argc != 4)
{
fprintf (stderr, "Usage: %s edge pin mode\n", argv [0]) ;
exit (1) ;
}
pin = atoi (argv [2]) ;
mode = argv [3] ;
// Export the pin and set direction to input
if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
{
fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
exit (1) ;
}
fprintf (fd, "%d\n", pin) ;
fclose (fd) ;
sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ;
if ((fd = fopen (fName, "w")) == NULL)
{
fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
exit (1) ;
}
fprintf (fd, "in\n") ;
fclose (fd) ;
sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
if ((fd = fopen (fName, "w")) == NULL)
{
fprintf (stderr, "%s: Unable to open GPIO edge interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
exit (1) ;
}
/**/ if (strcasecmp (mode, "none") == 0)
fprintf (fd, "none\n") ;
else if (strcasecmp (mode, "rising") == 0)
fprintf (fd, "rising\n") ;
else if (strcasecmp (mode, "falling") == 0)
fprintf (fd, "falling\n") ;
else if (strcasecmp (mode, "both") == 0)
fprintf (fd, "both\n") ;
else
{
fprintf (stderr, "%s: Invalid mode: %s. Should be none, rising, falling or both\n", argv [1], mode) ;
exit (1) ;
}
// Change ownership so the current user can actually use it!
uid = getuid () ;
gid = getgid () ;
sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
if (chown (fName, uid, gid) != 0)
{
fprintf (stderr, "%s: Unable to change ownership of the value file: %s\n", argv [1], strerror (errno)) ;
exit (1) ;
}
// Also change ownership of the edge file
sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
if (chown (fName, uid, gid) != 0)
{
fprintf (stderr, "%s: Unable to change ownership of the value file: %s\n", argv [1], strerror (errno)) ;
exit (1) ;
}
fclose (fd) ;
}
@@ -218,6 +332,33 @@ void doUnexport (int argc, char *argv [])
fclose (fd) ;
}
/*
* doUnexportAll:
* gpio unexportall
* Un-Export all the GPIO pins.
* This uses the /sys/class/gpio device interface.
*********************************************************************************
*/
void doUnexportall (int argc, char *argv [])
{
FILE *fd ;
int pin ;
for (pin = 0 ; pin < 63 ; ++pin)
{
if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
{
fprintf (stderr, "%s: Unable to open GPIO export interface\n", argv [0]) ;
exit (1) ;
}
fprintf (fd, "%d\n", pin) ;
fclose (fd) ;
}
}
/*
* doMode:
* gpio mode pin mode ...
@@ -381,11 +522,15 @@ int main (int argc, char *argv [])
// Initial test for /sys/class/gpio operations:
/**/ if (strcasecmp (argv [1], "exports" ) == 0)
{ doExports () ; return 0 ; }
{ doExports () ; return 0 ; }
else if (strcasecmp (argv [1], "export" ) == 0)
{ doExport (argc, argv) ; return 0 ; }
{ doExport (argc, argv) ; return 0 ; }
else if (strcasecmp (argv [1], "edge" ) == 0)
{ doEdge (argc, argv) ; return 0 ; }
else if (strcasecmp (argv [1], "unexportall") == 0)
{ doUnexportall (argc, argv) ; return 0 ; }
else if (strcasecmp (argv [1], "unexport") == 0)
{ doUnexport (argc, argv) ; return 0 ; }
{ doUnexport (argc, argv) ; return 0 ; }
// Check for -g argument
@@ -402,6 +547,25 @@ int main (int argc, char *argv [])
--argc ;
wpMode = WPI_MODE_GPIO ;
}
// Check for -p argument for PiFace
else if (strcasecmp (argv [1], "-p") == 0)
{
if (wiringPiSetupPiFaceForGpioProg () == -1)
{
fprintf (stderr, "%s: Unable to initialise PiFace.\n", argv [0]) ;
exit (1) ;
}
for (i = 2 ; i < argc ; ++i)
argv [i - 1] = argv [i] ;
--argc ;
wpMode = WPI_MODE_PIFACE ;
}
// Default to wiringPi mode
else
{
if (wiringPiSetup () == -1)