PCA9555: Extend interface to include methods for reading more than one bit at a time. From Sébastien Lorquet.

This commit is contained in:
Sebastien Lorquet 2015-07-10 10:33:07 -06:00 committed by Gregory Nutt
parent a5b68bb3c9
commit 9c9b6162d8
3 changed files with 267 additions and 25 deletions

View File

@ -3,6 +3,13 @@
# see the file kconfig-language.txt in the NuttX tools repository.
#
config IOEXPANDER_MULTIPIN
bool "Support multi-pin access routines"
default n
---help---
This settings enable the definition of routines for
optimized simultaneous access to multiple pins.
config IOEXPANDER_PCA9555
bool "PCA9555 I2C IO expander"
default n

View File

@ -70,16 +70,27 @@
* Private Function Prototypes
****************************************************************************/
static int pca9555_direction(FAR struct ioexpander_dev_s *dev, uint8_t pin,
int dir);
static int pca9555_option (FAR struct ioexpander_dev_s *dev, uint8_t pin,
int opt, void *val);
static int pca9555_write (FAR struct ioexpander_dev_s *dev, uint8_t pin,
bool value);
static int pca9555_readpin (FAR struct ioexpander_dev_s *dev, uint8_t pin,
bool *value);
static int pca9555_readbuf (FAR struct ioexpander_dev_s *dev, uint8_t pin,
bool *value);
static int pca9555_direction (FAR struct ioexpander_dev_s *dev,
uint8_t pin, int dir);
static int pca9555_option (FAR struct ioexpander_dev_s *dev,
uint8_t pin, int opt, void *val);
static int pca9555_write (FAR struct ioexpander_dev_s *dev,
uint8_t pin, bool value);
static int pca9555_readpin (FAR struct ioexpander_dev_s *dev,
uint8_t pin, FAR bool *value);
static int pca9555_readbuf (FAR struct ioexpander_dev_s *dev,
uint8_t pin, FAR bool *value);
#ifdef CONFIG_IOEXPANDER_MULTIPIN
static int pca9555_multiwrite (FAR struct ioexpander_dev_s *dev,
FAR uint8_t *pins, FAR bool *values,
int count);
static int pca9555_multireadpin(FAR struct ioexpander_dev_s *dev,
FAR uint8_t *pins, FAR bool *values,
int count);
static int pca9555_multireadbuf(FAR struct ioexpander_dev_s *dev,
FAR uint8_t *pins, FAR bool *values,
int count);
#endif
/****************************************************************************
* Private Data
@ -105,6 +116,11 @@ static const struct ioexpander_ops_s g_pca9555_ops =
pca9555_write,
pca9555_readpin,
pca9555_readbuf,
#ifdef CONFIG_IOEXPANDER_MULTIPIN
pca9555_multiwrite,
pca9555_multireadpin,
pca9555_multireadbuf,
#endif
};
/****************************************************************************
@ -137,18 +153,18 @@ static int pca9555_setbit(FAR struct i2c_dev_s *i2c, uint8_t addr,
}
ret = I2C_WRITEREAD(i2c, &addr, 1, &buf[1], 1);
if (ret != 0)
if (ret < 0)
{
return ret;
}
if (bitval)
{
buf[1] |= (1<<pin);
buf[1] |= (1 << pin);
}
else
{
buf[1] &= ~(1<<pin);
buf[1] &= ~(1 << pin);
}
return I2C_WRITE(i2c, buf, 2);
@ -163,29 +179,29 @@ static int pca9555_setbit(FAR struct i2c_dev_s *i2c, uint8_t addr,
****************************************************************************/
static int pca9555_getbit(FAR struct i2c_dev_s *i2c, uint8_t addr,
uint8_t pin, bool *val)
uint8_t pin, FAR bool *val)
{
uint8_t buf;
int ret;
if (pin>15)
if (pin > 15)
{
return -ENXIO;
}
else if (pin>7)
else if (pin > 7)
{
addr += 1;
pin -= 8;
}
ret = I2C_WRITEREAD(i2c, &addr, 1, &buf, 1);
if (ret != 0)
if (ret < 0)
{
return ret;
}
*val = (buf >> pin) & 1;
return 0;
*val = (buf >> pin) & 1;
return OK;
}
/****************************************************************************
@ -201,7 +217,7 @@ static int pca9555_direction(FAR struct ioexpander_dev_s *dev, uint8_t pin,
{
FAR struct pca9555_dev_s *pca = (FAR struct pca9555_dev_s*)dev;
return pca9555_setbit(pca->i2c, PCA9555_REG_CONFIG, pin,
(direction == IOEXPANDER_DIRECTION_IN));
(direction == IOEXPANDER_DIRECTION_IN));
}
/****************************************************************************
@ -222,7 +238,7 @@ static int pca9555_option(FAR struct ioexpander_dev_s *dev, uint8_t pin,
{
return pca9555_setbit(pca->i2c, PCA9555_REG_POLINV, pin, ival);
}
return -EINVAL;
}
@ -250,7 +266,7 @@ static int pca9555_write(FAR struct ioexpander_dev_s *dev, uint8_t pin,
****************************************************************************/
static int pca9555_readpin(FAR struct ioexpander_dev_s *dev, uint8_t pin,
bool *value)
FAR bool *value)
{
FAR struct pca9555_dev_s *pca = (FAR struct pca9555_dev_s*)dev;
return pca9555_getbit(pca->i2c, PCA9555_REG_INPUT, pin, value);
@ -265,12 +281,159 @@ static int pca9555_readpin(FAR struct ioexpander_dev_s *dev, uint8_t pin,
****************************************************************************/
static int pca9555_readbuf(FAR struct ioexpander_dev_s *dev, uint8_t pin,
bool *value)
FAR bool *value)
{
FAR struct pca9555_dev_s *pca = (FAR struct pca9555_dev_s*)dev;
FAR struct pca9555_dev_s *pca = (FAR struct pca9555_dev_s*)dev;
return pca9555_getbit(pca->i2c, PCA9555_REG_OUTPUT, pin, value);
}
#ifdef CONFIG_IOEXPANDER_MULTIPIN
/****************************************************************************
* Name: pca9555_getmultibits
*
* Description:
* Read multiple bits from PCA9555 registers.
*
****************************************************************************/
static int pca9555_getmultibits(FAR struct i2c_dev_s *i2c, uint8_t addr,
FAR uint8_t *pins, FAR bool *values,
int count)
{
uint8_t buf[2];
int ret = OK;
int i;
int index;
int pin;
ret = I2C_WRITEREAD(i2c, &addr, 1, buf, 2);
if (ret < 0)
{
return ret;
}
/* Read the requested bits */
for (i = 0; i < count; i++)
{
index = 0;
pin = pins[i];
if (pin > 15)
{
return -ENXIO;
}
else if(pin > 7)
{
index = 1;
pin -= 8;
}
values[i] = (buf[index] >> pin) & 1;
}
return OK;
}
/****************************************************************************
* Name: pca9555_multiwrite
*
* Description:
* See include/nuttx/ioexpander/ioexpander.h
*
****************************************************************************/
static int pca9555_multiwrite(FAR struct ioexpander_dev_s *dev,
FAR uint8_t *pins, FAR bool *values,
int count)
{
FAR struct pca9555_dev_s *pca = (FAR struct pca9555_dev_s*)dev;
uint8_t addr = PCA9555_REG_OUTPUT;
uint8_t buf[3];
int ret;
int i;
int index;
int pin;
/* Start by reading both registers, whatever the pins to change. We could
* attempt to read one port only if all pins were on the same port, but
* this would not save much. */
ret = I2C_WRITEREAD(pca->i2c, &addr, 1, &buf[1], 2);
if (ret < 0)
{
return ret;
}
/* Apply the user defined changes */
for (i = 0; i < count; i++)
{
index = 1;
pin = pins[i];
if (pin > 15)
{
return -ENXIO;
}
else if(pin > 7)
{
index = 2;
pin -= 8;
}
if (values[i])
{
buf[index] |= (1 << pin);
}
else
{
buf[index] &= ~(1 << pin);
}
}
/* Now write back the new pins states */
buf[0] = addr;
return I2C_WRITE(pca->i2c, buf, 3);
}
/****************************************************************************
* Name: pca9555_multireadpin
*
* Description:
* See include/nuttx/ioexpander/ioexpander.h
*
****************************************************************************/
static int pca9555_multireadpin(FAR struct ioexpander_dev_s *dev,
FAR uint8_t *pins, FAR bool *values,
int count)
{
FAR struct pca9555_dev_s *pca = (FAR struct pca9555_dev_s*)dev;
return pca9555_getmultibits(pca->i2c, PCA9555_REG_INPUT,
pins, values, count);
}
/****************************************************************************
* Name: pca9555_multireadbuf
*
* Description:
* See include/nuttx/ioexpander/ioexpander.h
*
****************************************************************************/
static int pca9555_multireadbuf(FAR struct ioexpander_dev_s *dev,
FAR uint8_t *pins, FAR bool *values,
int count)
{
FAR struct pca9555_dev_s *pca = (FAR struct pca9555_dev_s*)dev;
return pca9555_getmultibits(pca->i2c, PCA9555_REG_OUTPUT,
pins, values, count);
}
#endif
#ifndef CONFIG_PCA9555_INT_DISABLE
/****************************************************************************
@ -284,7 +447,7 @@ static int pca9555_readbuf(FAR struct ioexpander_dev_s *dev, uint8_t pin,
static int pca9555_attach(FAR struct ioexpander_dev_s *dev, uint8_t pin,
ioexpander_handler_t handler)
{
FAR struct pca9555_dev_s *pca = (FAR struct pca9555_dev_s*)dev;
FAR struct pca9555_dev_s *pca = (FAR struct pca9555_dev_s*)dev;
return 0;
}

View File

@ -154,6 +154,70 @@
#define IOEXP_READBUF(dev,pin,valptr) ((dev)->ops->ioe_readbuf(dev,pin,valptr))
#ifdef CONFIG_IOEXPANDER_MULTIPIN
/****************************************************************************
* Name: IOEXP_MULTIWRITE
*
* Description:
* Set the pin level for multiple pins. This routine may be faster than
* individual pin accesses. Optional.
*
* Input Parameters:
* dev - Device-specific state data
* pins - The list of pin indexes to alter in this call
* val - The list of pin levels.
*
* Returned Value:
* 0 on success, else a negative error code
*
****************************************************************************/
#define IOEXP_MULTIWRITEPIN(dev,pins,vals,count) \
((dev)->ops->ioe_multiwritepin(dev,pins,vals,count))
/****************************************************************************
* Name: IOEXP_MULTIREADPIN
*
* Description:
* Read the actual level for multiple pins. This routine may be faster than
* individual pin accesses. Optional.
*
* Input Parameters:
* dev - Device-specific state data
* pin - The list of pin indexes to read
* valptr - Pointer to a buffer where the pin levels are stored.
*
* Returned Value:
* 0 on success, else a negative error code
*
****************************************************************************/
#define IOEXP_MULTIREADPIN(dev,pins,vals,count) \
((dev)->ops->ioe_multireadpin(dev,pins,vals,count))
/****************************************************************************
* Name: IOEXP_MULTIREADBUF
*
* Description:
* Read the buffered level of multiple pins. This routine may be faster than
* individual pin accesses. Optional.
*
* Input Parameters:
* dev - Device-specific state data
* pin - The index of the pin
* valptr - Pointer to a buffer where the buffered levels are stored.
*
* Returned Value:
* 0 on success, else a negative error code
*
****************************************************************************/
#define IOEXP_MULTIREADBUF(dev,pins,vals,count) \
((dev)->ops->ioe_multireadbuf(dev,pin,vals,count))
#endif
/****************************************************************************
* Public Types
****************************************************************************/
@ -172,6 +236,14 @@ struct ioexpander_ops_s
bool *value);
CODE int (*ioe_readbuf)(FAR struct ioexpander_dev_s *dev, uint8_t pin,
bool *value);
#ifdef CONFIG_IOEXPANDER_MULTIPIN
CODE int (*ioe_multiwrite)(FAR struct ioexpander_dev_s *dev,
uint8_t *pins, bool *values, int count);
CODE int (*ioe_multireadpin)(FAR struct ioexpander_dev_s *dev,
uint8_t *pins, bool *values, int count);
CODE int (*ioe_multireadbuf)(FAR struct ioexpander_dev_s *dev,
uint8_t *pins, bool *values, int count);
#endif
};
struct ioexpander_dev_s