Many changes - tidying up the extensions interfaces.

Updating the GPIO command - new command - allreadall
ScrollPhat code
max31855 code (tested with adafruit breakout board)
more tests
updated rht03 code

Raspberry Pi v3 support.
This commit is contained in:
Gordon Henderson
2016-02-29 06:57:38 +00:00
parent 2dbecfca0a
commit b0a60c3302
27 changed files with 406 additions and 185 deletions

View File

@@ -278,7 +278,7 @@ int ads1115Setup (const int pinBase, int i2cAddr)
int fd ;
if ((fd = wiringPiI2CSetup (i2cAddr)) < 0)
return -1 ;
return FALSE ;
node = wiringPiNewNode (pinBase, 8) ;
@@ -289,5 +289,5 @@ int ads1115Setup (const int pinBase, int i2cAddr)
node->analogWrite = myAnalogWrite ;
node->digitalWrite = myDigitalWrite ;
return 0 ;
return TRUE ;
}

View File

@@ -1,7 +1,7 @@
/*
* drcSerial.c:
* Extend wiringPi with the DRC Serial protocol (e.g. to Arduino)
* Copyright (c) 2013 Gordon Henderson
* Copyright (c) 2013-2016 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
@@ -32,11 +32,6 @@
#include "drcSerial.h"
#ifndef TRUE
# define TRUE (1==1)
# define FALSE (1==2)
#endif
/*
* myPinMode:
@@ -156,7 +151,7 @@ int drcSetupSerial (const int pinBase, const int numPins, const char *device, co
struct wiringPiNodeStruct *node ;
if ((fd = serialOpen (device, baud)) < 0)
return wiringPiFailure (WPI_ALMOST, "Unable to open DRC device (%s): %s", device, strerror (errno)) ;
return FALSE ;
delay (10) ; // May need longer if it's an Uno that reboots on the open...
@@ -184,7 +179,7 @@ int drcSetupSerial (const int pinBase, const int numPins, const char *device, co
if (!ok)
{
serialClose (fd) ;
return wiringPiFailure (WPI_FATAL, "Unable to communicate with DRC serial device") ;
return FALSE ;
}
node = wiringPiNewNode (pinBase, numPins) ;
@@ -197,5 +192,5 @@ int drcSetupSerial (const int pinBase, const int numPins, const char *device, co
node->digitalWrite = myDigitalWrite ;
node->pwmWrite = myPwmWrite ;
return 0 ;
return TRUE ;
}

View File

@@ -88,12 +88,12 @@ int max31855Setup (const int pinBase, int spiChannel)
struct wiringPiNodeStruct *node ;
if (wiringPiSPISetup (spiChannel, 5000000) < 0) // 5MHz - prob 4 on the Pi
return -1 ;
return FALSE ;
node = wiringPiNewNode (pinBase, 4) ;
node->fd = spiChannel ;
node->analogRead = myAnalogRead ;
return 0 ;
return TRUE ;
}

View File

@@ -66,7 +66,7 @@ int max5322Setup (const int pinBase, int spiChannel)
unsigned char spiData [2] ;
if (wiringPiSPISetup (spiChannel, 8000000) < 0) // 10MHz Max
return -1 ;
return FALSE ;
node = wiringPiNewNode (pinBase, 2) ;
@@ -80,5 +80,5 @@ int max5322Setup (const int pinBase, int spiChannel)
wiringPiSPIDataRW (node->fd, spiData, 2) ;
return 0 ;
return TRUE ;
}

View File

@@ -132,7 +132,7 @@ int mcp23008Setup (const int pinBase, const int i2cAddress)
struct wiringPiNodeStruct *node ;
if ((fd = wiringPiI2CSetup (i2cAddress)) < 0)
return fd ;
return FALSE ;
wiringPiI2CWriteReg8 (fd, MCP23x08_IOCON, IOCON_INIT) ;
@@ -145,5 +145,5 @@ int mcp23008Setup (const int pinBase, const int i2cAddress)
node->digitalWrite = myDigitalWrite ;
node->data2 = wiringPiI2CReadReg8 (fd, MCP23x08_OLAT) ;
return 0 ;
return TRUE ;
}

View File

@@ -146,7 +146,7 @@ int mcp23016Setup (const int pinBase, const int i2cAddress)
struct wiringPiNodeStruct *node ;
if ((fd = wiringPiI2CSetup (i2cAddress)) < 0)
return fd ;
return FALSE ;
wiringPiI2CWriteReg8 (fd, MCP23016_IOCON0, IOCON_INIT) ;
wiringPiI2CWriteReg8 (fd, MCP23016_IOCON1, IOCON_INIT) ;
@@ -160,5 +160,5 @@ int mcp23016Setup (const int pinBase, const int i2cAddress)
node->data2 = wiringPiI2CReadReg8 (fd, MCP23016_OLAT0) ;
node->data3 = wiringPiI2CReadReg8 (fd, MCP23016_OLAT1) ;
return 0 ;
return TRUE ;
}

View File

@@ -177,7 +177,7 @@ int mcp23017Setup (const int pinBase, const int i2cAddress)
struct wiringPiNodeStruct *node ;
if ((fd = wiringPiI2CSetup (i2cAddress)) < 0)
return fd ;
return FALSE ;
wiringPiI2CWriteReg8 (fd, MCP23x17_IOCON, IOCON_INIT) ;
@@ -191,5 +191,5 @@ int mcp23017Setup (const int pinBase, const int i2cAddress)
node->data2 = wiringPiI2CReadReg8 (fd, MCP23x17_OLATA) ;
node->data3 = wiringPiI2CReadReg8 (fd, MCP23x17_OLATB) ;
return 0 ;
return TRUE ;
}

View File

@@ -167,11 +167,10 @@ static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
int mcp23s08Setup (const int pinBase, const int spiPort, const int devId)
{
int x ;
struct wiringPiNodeStruct *node ;
if ((x = wiringPiSPISetup (spiPort, MCP_SPEED)) < 0)
return x ;
if (wiringPiSPISetup (spiPort, MCP_SPEED) < 0)
return FALSE ;
writeByte (spiPort, devId, MCP23x08_IOCON, IOCON_INIT) ;
@@ -185,5 +184,5 @@ int mcp23s08Setup (const int pinBase, const int spiPort, const int devId)
node->digitalWrite = myDigitalWrite ;
node->data2 = readByte (spiPort, devId, MCP23x08_OLAT) ;
return 0 ;
return TRUE ;
}

View File

@@ -212,11 +212,10 @@ static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
int mcp23s17Setup (const int pinBase, const int spiPort, const int devId)
{
int x ;
struct wiringPiNodeStruct *node ;
if ((x = wiringPiSPISetup (spiPort, MCP_SPEED)) < 0)
return x ;
if (wiringPiSPISetup (spiPort, MCP_SPEED) < 0)
return FALSE ;
writeByte (spiPort, devId, MCP23x17_IOCON, IOCON_INIT | IOCON_HAEN) ;
writeByte (spiPort, devId, MCP23x17_IOCONB, IOCON_INIT | IOCON_HAEN) ;
@@ -232,5 +231,5 @@ int mcp23s17Setup (const int pinBase, const int spiPort, const int devId)
node->data2 = readByte (spiPort, devId, MCP23x17_OLATA) ;
node->data3 = readByte (spiPort, devId, MCP23x17_OLATB) ;
return 0 ;
return TRUE ;
}

View File

@@ -65,12 +65,12 @@ int mcp3002Setup (const int pinBase, int spiChannel)
struct wiringPiNodeStruct *node ;
if (wiringPiSPISetup (spiChannel, 1000000) < 0)
return -1 ;
return FALSE ;
node = wiringPiNewNode (pinBase, 2) ;
node->fd = spiChannel ;
node->analogRead = myAnalogRead ;
return 0 ;
return TRUE ;
}

View File

@@ -65,12 +65,12 @@ int mcp3004Setup (const int pinBase, int spiChannel)
struct wiringPiNodeStruct *node ;
if (wiringPiSPISetup (spiChannel, 1000000) < 0)
return -1 ;
return FALSE ;
node = wiringPiNewNode (pinBase, 8) ;
node->fd = spiChannel ;
node->analogRead = myAnalogRead ;
return 0 ;
return TRUE ;
}

View File

@@ -1,8 +1,9 @@
/*
* mcp3422.c:
* Extend wiringPi with the MCP3422 I2C ADC chip
* Also works for the MCP3423 and MCP3224 (4 channel) chips
* Copyright (c) 2013 Gordon Henderson
* Extend wiringPi with the MCP3422/3/4 I2C ADC chip
* This code assumes single-ended mode only.
* Tested on actual hardware: 20th Feb 2016.
* Copyright (c) 2013-2016 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
@@ -29,7 +30,6 @@
#include <stdint.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/spi/spidev.h>
#include <wiringPi.h>
#include <wiringPiI2C.h>
@@ -37,6 +37,23 @@
#include "mcp3422.h"
/*
* waitForConversion:
* Common code to wait for the ADC to finish conversion
*********************************************************************************
*/
void waitForConversion (int fd, unsigned char *buffer, int n)
{
for (;;)
{
read (fd, buffer, n) ;
if ((buffer [n-1] & 0x80) == 0)
break ;
delay (1) ;
}
}
/*
* myAnalogRead:
* Read a channel from the device
@@ -48,37 +65,34 @@ int myAnalogRead (struct wiringPiNodeStruct *node, int chan)
unsigned char config ;
unsigned char buffer [4] ;
int value = 0 ;
int realChan = (chan & 3) - node->pinBase ;
// One-shot mode, trigger plus the other configs.
config = 0x80 | ((chan - node->pinBase) << 5) | (node->data0 << 2) | (node->data1) ;
config = 0x80 | (realChan << 5) | (node->data0 << 2) | (node->data1) ;
wiringPiI2CWrite (node->fd, config) ;
switch (node->data0) // Sample rate
{
case MCP3422_SR_3_75: // 18 bits
delay (270) ;
read (node->fd, buffer, 4) ;
value = ((buffer [0] & 3) << 16) | (buffer [1] << 8) | buffer [0] ;
waitForConversion (node->fd, &buffer [0], 4) ;
value = ((buffer [0] & 3) << 16) | (buffer [1] << 8) | buffer [2] ;
break ;
case MCP3422_SR_15: // 16 bits
delay ( 70) ;
read (node->fd, buffer, 3) ;
waitForConversion (node->fd, buffer, 3) ;
value = (buffer [0] << 8) | buffer [1] ;
break ;
case MCP3422_SR_60: // 14 bits
delay ( 17) ;
read (node->fd, buffer, 3) ;
waitForConversion (node->fd, buffer, 3) ;
value = ((buffer [0] & 0x3F) << 8) | buffer [1] ;
break ;
case MCP3422_SR_240: // 12 bits
delay ( 5) ;
read (node->fd, buffer, 3) ;
value = ((buffer [0] & 0x0F) << 8) | buffer [0] ;
case MCP3422_SR_240: // 12 bits - default
waitForConversion (node->fd, buffer, 3) ;
value = ((buffer [0] & 0x0F) << 8) | buffer [1] ;
break ;
}
@@ -98,13 +112,14 @@ int mcp3422Setup (int pinBase, int i2cAddress, int sampleRate, int gain)
struct wiringPiNodeStruct *node ;
if ((fd = wiringPiI2CSetup (i2cAddress)) < 0)
return fd ;
return FALSE ;
node = wiringPiNewNode (pinBase, 4) ;
node->fd = fd ;
node->data0 = sampleRate ;
node->data1 = gain ;
node->analogRead = myAnalogRead ;
return 0 ;
return TRUE ;
}

View File

@@ -1,6 +1,6 @@
/*
* mcp3422.c:
* Extend wiringPi with the MCP3422 I2C ADC chip
* mcp3422.h:
* Extend wiringPi with the MCP3422/3/4 I2C ADC chip
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
@@ -21,10 +21,10 @@
***********************************************************************
*/
#define MCP3422_SR_3_75 0
#define MCP3422_SR_15 1
#define MCP3422_SR_60 2
#define MCP3422_SR_240 3
#define MCP3422_SR_240 0
#define MCP3422_SR_60 1
#define MCP3422_SR_15 2
#define MCP3422_SR_3_75 3
#define MCP3422_GAIN_1 0
#define MCP3422_GAIN_2 1

View File

@@ -65,12 +65,12 @@ int mcp4802Setup (const int pinBase, int spiChannel)
struct wiringPiNodeStruct *node ;
if (wiringPiSPISetup (spiChannel, 1000000) < 0)
return -1 ;
return FALSE ;
node = wiringPiNewNode (pinBase, 2) ;
node->fd = spiChannel ;
node->analogWrite = myAnalogWrite ;
return 0 ;
return TRUE ;
}

View File

@@ -112,7 +112,7 @@ int pcf8574Setup (const int pinBase, const int i2cAddress)
struct wiringPiNodeStruct *node ;
if ((fd = wiringPiI2CSetup (i2cAddress)) < 0)
return fd ;
return FALSE ;
node = wiringPiNewNode (pinBase, 8) ;
@@ -122,5 +122,5 @@ int pcf8574Setup (const int pinBase, const int i2cAddress)
node->digitalWrite = myDigitalWrite ;
node->data2 = wiringPiI2CRead (fd) ;
return 0 ;
return TRUE ;
}

View File

@@ -78,7 +78,7 @@ int pcf8591Setup (const int pinBase, const int i2cAddress)
struct wiringPiNodeStruct *node ;
if ((fd = wiringPiI2CSetup (i2cAddress)) < 0)
return fd ;
return FALSE ;
node = wiringPiNewNode (pinBase, 4) ;
@@ -86,5 +86,5 @@ int pcf8591Setup (const int pinBase, const int i2cAddress)
node->analogRead = myAnalogRead ;
node->analogWrite = myAnalogWrite ;
return 0 ;
return TRUE ;
}

View File

@@ -55,7 +55,7 @@ int sn3218Setup (const int pinBase)
struct wiringPiNodeStruct *node ;
if ((fd = wiringPiI2CSetup (0x54)) < 0)
return fd ;
return FALSE ;
// Setup the chip - initialise all 18 LEDs to off
@@ -71,5 +71,5 @@ int sn3218Setup (const int pinBase)
node->fd = fd ;
node->analogWrite = myAnalogWrite ;
return 0 ;
return TRUE ;
}

View File

@@ -105,5 +105,5 @@ int sr595Setup (const int pinBase, const int numPins,
pinMode (clockPin, OUTPUT) ;
pinMode (latchPin, OUTPUT) ;
return 0 ;
return TRUE ;
}

View File

@@ -69,17 +69,13 @@
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <asm/ioctl.h>
#include "softPwm.h"
#include "softTone.h"
#include "wiringPi.h"
#ifndef TRUE
#define TRUE (1==1)
#define FALSE (1==2)
#endif
// Environment Variables
#define ENV_DEBUG "WIRINGPI_DEBUG"
@@ -219,7 +215,7 @@ const char *piModelNames [16] =
"Alpha", // 5
"CM", // 6
"Unknown07", // 07
"Unknown08", // 08
"Pi 3", // 08
"Pi Zero", // 09
"Unknown10", // 10
"Unknown11", // 11
@@ -894,7 +890,7 @@ void piBoardId (int *model, int *rev, int *mem, int *maker, int *warranty)
*c = 0 ;
if (wiringPiDebug)
printf ("piboardId: Revision string: %s\n", line) ;
printf ("piBoardId: Revision string: %s\n", line) ;
// Need to work out if it's using the new or old encoding scheme:
@@ -1624,16 +1620,21 @@ void pwmToneWrite (int pin, int freq)
/*
* digitalWriteByte:
* digitalReadByte:
* Pi Specific
* Write an 8-bit byte to the first 8 GPIO pins - try to do it as
* fast as possible.
* However it still needs 2 operations to set the bits, so any external
* hardware must not rely on seeing a change as there will be a change
* to set the outputs bits to zero, then another change to set the 1's
* Reading is just bit fiddling.
* These are wiringPi pin numbers 0..7, or BCM_GPIO pin numbers
* 17, 18, 22, 23, 24, 24, 4 on a Pi v1 rev 0-3
* 17, 18, 27, 23, 24, 24, 4 on a Pi v1 rev 3 onwards or B+, 2, zero
*********************************************************************************
*/
void digitalWriteByte (int value)
void digitalWriteByte (const int value)
{
uint32_t pinSet = 0 ;
uint32_t pinClr = 0 ;
@@ -1644,7 +1645,7 @@ void digitalWriteByte (int value)
{
for (pin = 0 ; pin < 8 ; ++pin)
{
digitalWrite (pin, value & mask) ;
digitalWrite (pinToGpio [pin], value & mask) ;
mask <<= 1 ;
}
return ;
@@ -1666,6 +1667,83 @@ void digitalWriteByte (int value)
}
}
unsigned int digitalReadByte (void)
{
int pin, x ;
uint32_t raw ;
uint32_t data = 0 ;
/**/ if (wiringPiMode == WPI_MODE_GPIO_SYS)
{
for (pin = 0 ; pin < 8 ; ++pin)
{
x = digitalRead (pinToGpio [pin]) ;
data = (data << 1) | x ;
}
}
else
{
raw = *(gpio + gpioToGPLEV [0]) ; // First bank for these pins
for (pin = 0 ; pin < 8 ; ++pin)
{
x = pinToGpio [pin] ;
data = (data << 1) | (((raw & (1 << x)) == 0) ? 0 : 1) ;
}
}
return data ;
}
/*
* digitalWriteByte2:
* digitalReadByte2:
* Pi Specific
* Write an 8-bit byte to the second set of 8 GPIO pins. This is marginally
* faster than the first lot as these are consecutive BCM_GPIO pin numbers.
* However they overlap with the original read/write bytes.
*********************************************************************************
*/
void digitalWriteByte2 (const int value)
{
register int mask = 1 ;
register int pin ;
/**/ if (wiringPiMode == WPI_MODE_GPIO_SYS)
{
for (pin = 20 ; pin < 28 ; ++pin)
{
digitalWrite (pin, value & mask) ;
mask <<= 1 ;
}
return ;
}
else
{
*(gpio + gpioToGPCLR [0]) = 0x0FF00000 ;
*(gpio + gpioToGPSET [0]) = (value & 0xFF) << 20 ;
}
}
unsigned int digitalReadByte2 (void)
{
int pin, x ;
uint32_t data = 0 ;
/**/ if (wiringPiMode == WPI_MODE_GPIO_SYS)
{
for (pin = 20 ; pin < 28 ; ++pin)
{
x = digitalRead (pin) ;
data = (data << 1) | x ;
}
}
else
data = ((*(gpio + gpioToGPLEV [0])) >> 20) & 0xFF ; // First bank for these pins
return data ;
}
/*
* waitForInterrupt:

View File

@@ -1,7 +1,7 @@
/*
* wiringPi:
* Arduino compatable (ish) Wiring library for the Raspberry Pi
* Copyright (c) 2012 Gordon Henderson
* wiringPi.h:
* Arduino like Wiring library for the Raspberry Pi.
* Copyright (c) 2012-2016 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
@@ -24,6 +24,14 @@
#ifndef __WIRING_PI_H__
#define __WIRING_PI_H__
// C doesn't have true/false by default and I can never remember which
// way round they are, so ...
#ifndef TRUE
# define TRUE (1==1)
# define FALSE (!TRUE)
#endif
// Handy defines
// wiringPi modes
@@ -77,7 +85,7 @@
#define PI_ALPHA 5
#define PI_MODEL_CM 6
#define PI_MODEL_07 7
#define PI_MODEL_08 8
#define PI_MODEL_3 8
#define PI_MODEL_ZERO 9
#define PI_VERSION_1 0
@@ -182,18 +190,19 @@ extern int wiringPiSetupPiFaceForGpioProg (void) ; // Don't use this - for gpio
// On-Board Raspberry Pi hardware specific stuff
extern int piBoardRev (void) ;
extern void piBoardId (int *model, int *rev, int *mem, int *maker, int *overVolted) ;
extern int wpiPinToGpio (int wpiPin) ;
extern int physPinToGpio (int physPin) ;
extern void setPadDrive (int group, int value) ;
extern int getAlt (int pin) ;
extern void pwmToneWrite (int pin, int freq) ;
extern void digitalWriteByte (int value) ;
extern void pwmSetMode (int mode) ;
extern void pwmSetRange (unsigned int range) ;
extern void pwmSetClock (int divisor) ;
extern void gpioClockSet (int pin, int freq) ;
extern int piBoardRev (void) ;
extern void piBoardId (int *model, int *rev, int *mem, int *maker, int *overVolted) ;
extern int wpiPinToGpio (int wpiPin) ;
extern int physPinToGpio (int physPin) ;
extern void setPadDrive (int group, int value) ;
extern int getAlt (int pin) ;
extern void pwmToneWrite (int pin, int freq) ;
extern void digitalWriteByte (int value) ;
extern unsigned int digitalReadByte (void) ;
extern void pwmSetMode (int mode) ;
extern void pwmSetRange (unsigned int range) ;
extern void pwmSetClock (int divisor) ;
extern void gpioClockSet (int pin, int freq) ;
// Interrupts
// (Also Pi hardware specific)

View File

@@ -64,11 +64,6 @@ static int verbose ;
static char errorMessage [1024] ;
#ifndef TRUE
# define TRUE (1==1)
# define FALSE (1==2)
#endif
// Local structure to hold details
struct extensionFunctionStruct
@@ -119,7 +114,13 @@ static char *extractInt (char *progName, char *p, int *num)
}
*num = strtol (p, NULL, 0) ;
while (isdigit (*p))
// Increment p, but we need to check for hex 0x
if ((*p == '0') && (*(p + 1) == 'x'))
p +=2 ;
while (isxdigit (*p))
++p ;
return p ;
@@ -702,7 +703,7 @@ int loadWPiExtension (char *progName, char *extensionData, int printErrors)
char *p ;
char *extension = extensionData ;
struct extensionFunctionStruct *extensionFn ;
int pinBase = 0 ;
unsigned pinBase = 0 ;
verbose = printErrors ;
@@ -724,13 +725,13 @@ int loadWPiExtension (char *progName, char *extensionData, int printErrors)
if (!isdigit (*p))
{
verbError ("%s: pinBase number expected after extension name", progName) ;
verbError ("%s: decimal pinBase number expected after extension name", progName) ;
return FALSE ;
}
while (isdigit (*p))
{
if (pinBase > 1000000000) // Lets be realistic here...
if (pinBase > 2147483647) // 2^31-1 ... Lets be realistic here...
{
verbError ("%s: pinBase too large", progName) ;
return FALSE ;