mirror of
https://github.com/clockworkpi/WiringPi.git
synced 2025-12-12 16:08:49 +01:00
Fixed delayMicroseconds for more than 1 second.
Added new code for max31855
This commit is contained in:
parent
348bc739d1
commit
27698766b2
@ -35,7 +35,7 @@ LIBS = -lwiringPi -lwiringPiDev -lpthread -lm
|
||||
# May not need to alter anything below this line
|
||||
###############################################################################
|
||||
|
||||
SRC = gpio.c extensions.c
|
||||
SRC = gpio.c extensions.c readall.c
|
||||
|
||||
OBJ = $(SRC:.c=.o)
|
||||
|
||||
|
||||
@ -43,6 +43,7 @@
|
||||
#include <sr595.h>
|
||||
#include <pcf8591.h>
|
||||
#include <pcf8574.h>
|
||||
#include <max31855.h>
|
||||
#include <mcp3002.h>
|
||||
#include <mcp3004.h>
|
||||
#include <mcp4802.h>
|
||||
@ -334,6 +335,32 @@ static int doExtensionPcf8591 (char *progName, int pinBase, char *params)
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* doExtensionMax31855:
|
||||
* Analog IO
|
||||
* max31855:base:spiChan
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static int doExtensionMax31855 (char *progName, int pinBase, char *params)
|
||||
{
|
||||
int spi ;
|
||||
|
||||
if ((params = extractInt (progName, params, &spi)) == NULL)
|
||||
return FALSE ;
|
||||
|
||||
if ((spi < 0) || (spi > 1))
|
||||
{
|
||||
fprintf (stderr, "%s: SPI channel (%d) out of range\n", progName, spi) ;
|
||||
return FALSE ;
|
||||
}
|
||||
|
||||
max31855Setup (pinBase, spi) ;
|
||||
|
||||
return TRUE ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* doExtensionMcp3002:
|
||||
* Analog IO
|
||||
@ -475,6 +502,7 @@ struct extensionFunctionStruct extensionFunctions [] =
|
||||
{ "mcp3004", &doExtensionMcp3004 },
|
||||
{ "mcp4802", &doExtensionMcp4802 },
|
||||
{ "mcp3422", &doExtensionMcp3422 },
|
||||
{ "max31855", &doExtensionMax31855 },
|
||||
{ NULL, NULL },
|
||||
} ;
|
||||
|
||||
|
||||
124
gpio/gpio.c
124
gpio/gpio.c
@ -43,15 +43,18 @@
|
||||
|
||||
extern int wiringPiDebug ;
|
||||
|
||||
extern void doReadall (void) ;
|
||||
extern void doReadallOld (void) ;
|
||||
|
||||
#ifndef TRUE
|
||||
# define TRUE (1==1)
|
||||
# define FALSE (1==2)
|
||||
#endif
|
||||
|
||||
#define VERSION "2.07"
|
||||
#define VERSION "2.08"
|
||||
#define I2CDETECT "/usr/sbin/i2cdetect"
|
||||
|
||||
static int wpMode ;
|
||||
int wpMode ;
|
||||
|
||||
char *usage = "Usage: gpio -v\n"
|
||||
" gpio -h\n"
|
||||
@ -236,95 +239,6 @@ static void doI2Cdetect (int argc, char *argv [])
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* doReadall:
|
||||
* Read all the GPIO pins
|
||||
* We also want to use this to read the state of pins on an externally
|
||||
* connected device, so we need to do some fiddling with the internal
|
||||
* wiringPi node structures - since the gpio command can only use
|
||||
* one external device at a time, we'll use that to our advantage...
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static char *pinNames [] =
|
||||
{
|
||||
"GPIO 0", "GPIO 1", "GPIO 2", "GPIO 3", "GPIO 4", "GPIO 5", "GPIO 6", "GPIO 7",
|
||||
"SDA ", "SCL ",
|
||||
"CE0 ", "CE1 ", "MOSI ", "MISO ", "SCLK ",
|
||||
"TxD ", "RxD ",
|
||||
"GPIO 8", "GPIO 9", "GPIO10", "GPIO11",
|
||||
} ;
|
||||
|
||||
static char *alts [] =
|
||||
{
|
||||
"IN ", "OUT ", "ALT5", "ALT4", "ALT0", "ALT1", "ALT2", "ALT3"
|
||||
} ;
|
||||
|
||||
static int wpiToPhys [64] =
|
||||
{
|
||||
11, 12, 13, 15, 16, 18, 22, 7, // 0...7
|
||||
3, 5, // 8...9
|
||||
24, 26, 19, 21, 23, // 10..14
|
||||
8, 10, // 15..16
|
||||
3, 4, 5, 6, // 17..20
|
||||
0,0,0,0,0,0,0,0,0,0,0, // 20..31
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 32..47
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 47..63
|
||||
} ;
|
||||
|
||||
|
||||
/*
|
||||
* doReadallExternal:
|
||||
* A relatively crude way to read the pins on an external device.
|
||||
* We don't know the input/output mode of pins, but we can tell
|
||||
* if it's an analog pin or a digital one...
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void doReadallExternal (void)
|
||||
{
|
||||
int pin ;
|
||||
|
||||
printf ("+------+---------+--------+\n") ;
|
||||
printf ("| Pin | Digital | Analog |\n") ;
|
||||
printf ("+------+---------+--------+\n") ;
|
||||
|
||||
for (pin = wiringPiNodes->pinBase ; pin <= wiringPiNodes->pinMax ; ++pin)
|
||||
printf ("| %4d | %4d | %4d |\n", pin, digitalRead (pin), analogRead (pin)) ;
|
||||
|
||||
printf ("+------+---------+--------+\n") ;
|
||||
}
|
||||
|
||||
|
||||
static void doReadall (void)
|
||||
{
|
||||
int pin ;
|
||||
|
||||
if (wiringPiNodes != NULL) // External readall
|
||||
doReadallExternal () ;
|
||||
else
|
||||
{
|
||||
printf ("+----------+-Rev%d-+------+--------+------+-------+\n", piBoardRev ()) ;
|
||||
printf ("| wiringPi | GPIO | Phys | Name | Mode | Value |\n") ;
|
||||
printf ("+----------+------+------+--------+------+-------+\n") ;
|
||||
|
||||
for (pin = 0 ; pin < 64 ; ++pin) // Crude, but effective
|
||||
{
|
||||
if (wpiPinToGpio (pin) == -1)
|
||||
continue ;
|
||||
|
||||
printf ("| %6d | %3d | %3d | %s | %s | %s |\n",
|
||||
pin, wpiPinToGpio (pin), wpiToPhys [pin],
|
||||
pinNames [pin],
|
||||
alts [getAlt (pin)],
|
||||
digitalRead (pin) == HIGH ? "High" : "Low ") ;
|
||||
}
|
||||
|
||||
printf ("+----------+------+------+--------+------+-------+\n") ;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* doExports:
|
||||
* List all GPIO exports
|
||||
@ -716,12 +630,15 @@ void doMode (int argc, char *argv [])
|
||||
mode = argv [3] ;
|
||||
|
||||
/**/ if (strcasecmp (mode, "in") == 0) pinMode (pin, INPUT) ;
|
||||
else if (strcasecmp (mode, "input") == 0) pinMode (pin, INPUT) ;
|
||||
else if (strcasecmp (mode, "out") == 0) pinMode (pin, OUTPUT) ;
|
||||
else if (strcasecmp (mode, "output") == 0) pinMode (pin, OUTPUT) ;
|
||||
else if (strcasecmp (mode, "pwm") == 0) pinMode (pin, PWM_OUTPUT) ;
|
||||
else if (strcasecmp (mode, "clock") == 0) pinMode (pin, GPIO_CLOCK) ;
|
||||
else if (strcasecmp (mode, "up") == 0) pullUpDnControl (pin, PUD_UP) ;
|
||||
else if (strcasecmp (mode, "down") == 0) pullUpDnControl (pin, PUD_DOWN) ;
|
||||
else if (strcasecmp (mode, "tri") == 0) pullUpDnControl (pin, PUD_OFF) ;
|
||||
else if (strcasecmp (mode, "off") == 0) pullUpDnControl (pin, PUD_OFF) ;
|
||||
else
|
||||
{
|
||||
fprintf (stderr, "%s: Invalid mode: %s. Should be in/out/pwm/clock/up/down/tri\n", argv [1], mode) ;
|
||||
@ -1268,18 +1185,19 @@ int main (int argc, char *argv [])
|
||||
|
||||
// Pi Specifics
|
||||
|
||||
else if (strcasecmp (argv [1], "pwm-bal" ) == 0) doPwmMode (PWM_MODE_BAL) ;
|
||||
else if (strcasecmp (argv [1], "pwm-ms" ) == 0) doPwmMode (PWM_MODE_MS) ;
|
||||
else if (strcasecmp (argv [1], "pwmr" ) == 0) doPwmRange (argc, argv) ;
|
||||
else if (strcasecmp (argv [1], "pwmc" ) == 0) doPwmClock (argc, argv) ;
|
||||
else if (strcasecmp (argv [1], "drive" ) == 0) doPadDrive (argc, argv) ;
|
||||
else if (strcasecmp (argv [1], "readall" ) == 0) doReadall () ;
|
||||
else if (strcasecmp (argv [1], "i2cdetect") == 0) doI2Cdetect (argc, argv) ;
|
||||
else if (strcasecmp (argv [1], "i2cd" ) == 0) doI2Cdetect (argc, argv) ;
|
||||
else if (strcasecmp (argv [1], "reset" ) == 0) doReset (argv [0]) ;
|
||||
else if (strcasecmp (argv [1], "wb" ) == 0) doWriteByte (argc, argv) ;
|
||||
else if (strcasecmp (argv [1], "clock" ) == 0) doClock (argc, argv) ;
|
||||
else if (strcasecmp (argv [1], "wfi" ) == 0) doWfi (argc, argv) ;
|
||||
else if (strcasecmp (argv [1], "pwm-bal" ) == 0) doPwmMode (PWM_MODE_BAL) ;
|
||||
else if (strcasecmp (argv [1], "pwm-ms" ) == 0) doPwmMode (PWM_MODE_MS) ;
|
||||
else if (strcasecmp (argv [1], "pwmr" ) == 0) doPwmRange (argc, argv) ;
|
||||
else if (strcasecmp (argv [1], "pwmc" ) == 0) doPwmClock (argc, argv) ;
|
||||
else if (strcasecmp (argv [1], "drive" ) == 0) doPadDrive (argc, argv) ;
|
||||
else if (strcasecmp (argv [1], "readall" ) == 0) doReadall () ;
|
||||
else if (strcasecmp (argv [1], "oreadall" ) == 0) doReadallOld () ;
|
||||
else if (strcasecmp (argv [1], "i2cdetect") == 0) doI2Cdetect (argc, argv) ;
|
||||
else if (strcasecmp (argv [1], "i2cd" ) == 0) doI2Cdetect (argc, argv) ;
|
||||
else if (strcasecmp (argv [1], "reset" ) == 0) doReset (argv [0]) ;
|
||||
else if (strcasecmp (argv [1], "wb" ) == 0) doWriteByte (argc, argv) ;
|
||||
else if (strcasecmp (argv [1], "clock" ) == 0) doClock (argc, argv) ;
|
||||
else if (strcasecmp (argv [1], "wfi" ) == 0) doWfi (argc, argv) ;
|
||||
else
|
||||
{
|
||||
fprintf (stderr, "%s: Unknown command: %s.\n", argv [0], argv [1]) ;
|
||||
|
||||
@ -35,7 +35,8 @@ DYNAMIC=libwiringPi.so.$(VERSION)
|
||||
DEBUG = -O2
|
||||
CC = gcc
|
||||
INCLUDE = -I.
|
||||
CFLAGS = $(DEBUG) -Wformat=2 -Wall $(INCLUDE) -Winline -pipe -fPIC
|
||||
DEFS = -D_GNU_SOURCE
|
||||
CFLAGS = $(DEBUG) $(DEFS) -Wformat=2 -Wall -Winline $(INCLUDE) -pipe -fPIC
|
||||
|
||||
LIBS =
|
||||
|
||||
@ -52,6 +53,7 @@ SRC = wiringPi.c \
|
||||
sr595.c \
|
||||
pcf8574.c pcf8591.c \
|
||||
mcp3002.c mcp3004.c mcp4802.c mcp3422.c \
|
||||
max31855.c \
|
||||
drc.c
|
||||
|
||||
OBJ = $(SRC:.c=.o)
|
||||
@ -101,6 +103,7 @@ install-headers:
|
||||
@install -m 0644 mcp23017.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 mcp23s08.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 mcp23s17.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 max31855.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 mcp3002.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 mcp3004.h $(DESTDIR)$(PREFIX)/include
|
||||
@install -m 0644 mcp4802.h $(DESTDIR)$(PREFIX)/include
|
||||
@ -138,6 +141,7 @@ uninstall:
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/mcp23017.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/mcp23s08.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/mcp23s17.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/max31855.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/mcp3002.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/mcp3004.h
|
||||
@rm -f $(DESTDIR)$(PREFIX)/include/mcp4802.h
|
||||
@ -173,6 +177,8 @@ sr595.o: wiringPi.h sr595.h
|
||||
pcf8574.o: wiringPi.h wiringPiI2C.h pcf8574.h
|
||||
pcf8591.o: wiringPi.h wiringPiI2C.h pcf8591.h
|
||||
mcp3002.o: wiringPi.h wiringPiSPI.h mcp3002.h
|
||||
mcp3004.o: wiringPi.h wiringPiSPI.h mcp3004.h
|
||||
mcp4802.o: wiringPi.h wiringPiSPI.h mcp4802.h
|
||||
mcp3422.o: wiringPi.h wiringPiI2C.h mcp3422.h
|
||||
max31855.o: wiringPi.h wiringPiSPI.h max31855.h
|
||||
drc.o: wiringPi.h wiringSerial.h drc.h
|
||||
|
||||
@ -1448,6 +1448,8 @@ void delayMicrosecondsHard (unsigned int howLong)
|
||||
void delayMicroseconds (unsigned int howLong)
|
||||
{
|
||||
struct timespec sleeper ;
|
||||
unsigned int uSecs = howLong % 1000000 ;
|
||||
unsigned int wSecs = howLong / 1000000 ;
|
||||
|
||||
/**/ if (howLong == 0)
|
||||
return ;
|
||||
@ -1455,8 +1457,8 @@ void delayMicroseconds (unsigned int howLong)
|
||||
delayMicrosecondsHard (howLong) ;
|
||||
else
|
||||
{
|
||||
sleeper.tv_sec = 0 ;
|
||||
sleeper.tv_nsec = (long)(howLong * 1000) ;
|
||||
sleeper.tv_sec = wSecs ;
|
||||
sleeper.tv_nsec = (long)(uSecs * 1000L) ;
|
||||
nanosleep (&sleeper, NULL) ;
|
||||
}
|
||||
}
|
||||
@ -1539,7 +1541,7 @@ int wiringPiSetup (void)
|
||||
|
||||
// Open the master /dev/memory device
|
||||
|
||||
if ((fd = open ("/dev/mem", O_RDWR | O_SYNC) ) < 0)
|
||||
if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0)
|
||||
return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: Unable to open /dev/mem: %s\n", strerror (errno)) ;
|
||||
|
||||
// GPIO:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user