drivers: handle I2C_TRANSFER return value consistently. Some I2C peripherals transfers return zero on success, others number of completed transfers. Make drivers robust against this.
This commit is contained in:
parent
4cc13f8d94
commit
0113b0db95
@ -71,6 +71,7 @@ int i2c_read(FAR struct i2c_master_s *dev,
|
||||
{
|
||||
struct i2c_msg_s msg;
|
||||
unsigned int flags;
|
||||
int ret;
|
||||
|
||||
/* 7- or 10-bit? */
|
||||
|
||||
@ -86,5 +87,6 @@ int i2c_read(FAR struct i2c_master_s *dev,
|
||||
|
||||
/* Then perform the transfer. */
|
||||
|
||||
return I2C_TRANSFER(dev, &msg, 1);
|
||||
ret = I2C_TRANSFER(dev, &msg, 1);
|
||||
return (ret >= 0) ? OK : ret;
|
||||
}
|
||||
|
@ -71,6 +71,7 @@ int i2c_write(FAR struct i2c_master_s *dev,
|
||||
FAR const uint8_t *buffer, int buflen)
|
||||
{
|
||||
struct i2c_msg_s msg;
|
||||
int ret;
|
||||
|
||||
/* Setup for the transfer */
|
||||
|
||||
@ -82,5 +83,6 @@ int i2c_write(FAR struct i2c_master_s *dev,
|
||||
|
||||
/* Then perform the transfer. */
|
||||
|
||||
return I2C_TRANSFER(dev, &msg, 1);
|
||||
ret = I2C_TRANSFER(dev, &msg, 1);
|
||||
return (ret >= 0) ? OK : ret;
|
||||
}
|
||||
|
@ -74,6 +74,7 @@ int i2c_writeread(FAR struct i2c_master_s *dev,
|
||||
{
|
||||
struct i2c_msg_s msg[2];
|
||||
unsigned int flags;
|
||||
int ret;
|
||||
|
||||
/* 7- or 10-bit address? */
|
||||
|
||||
@ -109,6 +110,7 @@ int i2c_writeread(FAR struct i2c_master_s *dev,
|
||||
|
||||
/* Then perform the transfer. */
|
||||
|
||||
return I2C_TRANSFER(dev, msg, 2);
|
||||
ret = I2C_TRANSFER(dev, msg, 2);
|
||||
return (ret >= 0) ? OK : ret;
|
||||
}
|
||||
|
||||
|
@ -1837,6 +1837,7 @@ static int mxt_hwinitialize(FAR struct mxt_dev_s *priv)
|
||||
if (priv->sample == NULL)
|
||||
{
|
||||
ierr("ERROR: Failed to allocate object table\n");
|
||||
ret = -ENOMEM;
|
||||
goto errout_with_objtab;
|
||||
}
|
||||
|
||||
|
@ -172,6 +172,7 @@ static inline int pca9555_write(FAR struct pca9555_dev_s *pca,
|
||||
FAR const uint8_t *wbuffer, int wbuflen)
|
||||
{
|
||||
struct i2c_msg_s msg;
|
||||
int ret;
|
||||
|
||||
/* Setup for the transfer */
|
||||
|
||||
@ -183,7 +184,8 @@ static inline int pca9555_write(FAR struct pca9555_dev_s *pca,
|
||||
|
||||
/* Then perform the transfer. */
|
||||
|
||||
return I2C_TRANSFER(pca->i2c, &msg, 1);
|
||||
ret = I2C_TRANSFER(pca->i2c, &msg, 1);
|
||||
return (ret >= 0) ? OK : ret;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
|
@ -182,6 +182,7 @@ static void pcf8574_lock(FAR struct pcf8574_dev_s *priv)
|
||||
static int pcf8574_read(FAR struct pcf8574_dev_s *priv, FAR uint8_t *portval)
|
||||
{
|
||||
struct i2c_msg_s msg;
|
||||
int ret;
|
||||
|
||||
DEBUGASSERT(priv != NULL && priv->i2c != NULL && priv->config != NULL);
|
||||
|
||||
@ -195,7 +196,8 @@ static int pcf8574_read(FAR struct pcf8574_dev_s *priv, FAR uint8_t *portval)
|
||||
|
||||
/* Then perform the transfer. */
|
||||
|
||||
return I2C_TRANSFER(priv->i2c, &msg, 1);
|
||||
ret = I2C_TRANSFER(priv->i2c, &msg, 1);
|
||||
return (ret >= 0) ? OK : ret;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
@ -212,6 +214,7 @@ static int pcf8574_read(FAR struct pcf8574_dev_s *priv, FAR uint8_t *portval)
|
||||
static int pcf8574_write(struct pcf8574_dev_s *priv, uint8_t portval)
|
||||
{
|
||||
struct i2c_msg_s msg;
|
||||
int ret;
|
||||
|
||||
DEBUGASSERT(priv != NULL && priv->i2c != NULL && priv->config != NULL);
|
||||
|
||||
@ -225,7 +228,8 @@ static int pcf8574_write(struct pcf8574_dev_s *priv, uint8_t portval)
|
||||
|
||||
/* Then perform the transfer. */
|
||||
|
||||
return I2C_TRANSFER(priv->i2c, &msg, 1);
|
||||
ret = I2C_TRANSFER(priv->i2c, &msg, 1);
|
||||
return (ret >= 0) ? OK : ret;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
|
@ -342,14 +342,14 @@ static int tca64_getreg(FAR struct tca64_dev_s *priv, uint8_t regaddr,
|
||||
{
|
||||
gpioerr("ERROR: I2C addr=%02x regaddr=%02x: failed, ret=%d!\n",
|
||||
priv->config->address, regaddr, ret);
|
||||
return ret;
|
||||
}
|
||||
else
|
||||
{
|
||||
gpioinfo("I2C addr=%02x regaddr=%02x: read %02x\n",
|
||||
priv->config->address, regaddr, *regval);
|
||||
return OK;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
@ -390,14 +390,14 @@ static int tca64_putreg(struct tca64_dev_s *priv, uint8_t regaddr,
|
||||
{
|
||||
gpioerr("ERROR: claddr=%02x, regaddr=%02x: failed, ret=%d!\n",
|
||||
priv->config->address, regaddr, ret);
|
||||
return ret;
|
||||
}
|
||||
else
|
||||
{
|
||||
gpioinfo("claddr=%02x, regaddr=%02x, regval=%02x\n",
|
||||
priv->config->address, regaddr, regval);
|
||||
return OK;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
|
@ -551,12 +551,16 @@ static ssize_t lis2dh_write(FAR struct file *filep, FAR const char *buffer,
|
||||
|
||||
static int lis2dh_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
||||
{
|
||||
FAR struct inode *inode = filep->f_inode;
|
||||
FAR struct lis2dh_dev_s *priv = inode->i_private;
|
||||
FAR struct inode *inode;
|
||||
FAR struct lis2dh_dev_s *priv;
|
||||
int ret;
|
||||
uint8_t buf;
|
||||
|
||||
DEBUGASSERT(filep != NULL);
|
||||
DEBUGASSERT(filep);
|
||||
inode = filep->f_inode;
|
||||
|
||||
DEBUGASSERT(inode && inode->i_private);
|
||||
priv = (FAR struct lis2dh_dev_s *)inode->i_private;
|
||||
|
||||
ret = sem_wait(&priv->devsem);
|
||||
if (ret < 0)
|
||||
@ -564,17 +568,16 @@ static int lis2dh_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
||||
return -EINTR;
|
||||
}
|
||||
|
||||
ret = OK;
|
||||
switch (cmd)
|
||||
{
|
||||
case SNIOC_WRITESETUP:
|
||||
{
|
||||
/* Write to the configuration registers. Arg: uint8_t value */
|
||||
/* Write to the configuration registers. */
|
||||
|
||||
ret = lis2dh_setup(priv, (struct lis2dh_setup *)arg);
|
||||
lis2dh_dbg("lis2dh: conf: %02x ret: %d\n", *(uint8_t*)arg, ret);
|
||||
lis2dh_dbg("lis2dh: conf: %p ret: %d\n", (struct lis2dh_setup *)arg, ret);
|
||||
|
||||
/* Make sure interrupt will get cleared (by reading this register) in
|
||||
/* Make sure interrupt will get cleared in
|
||||
* case of latched configuration.
|
||||
*/
|
||||
|
||||
@ -1614,7 +1617,7 @@ static int lis2dh_access(FAR struct lis2dh_dev_s *dev, uint8_t subaddr,
|
||||
};
|
||||
|
||||
retval = I2C_TRANSFER(dev->i2c, msgv, 2);
|
||||
if (retval == OK)
|
||||
if (retval >= 0)
|
||||
{
|
||||
return length;
|
||||
}
|
||||
|
@ -125,7 +125,7 @@ struct lis331dl_dev_s
|
||||
* of -length
|
||||
*
|
||||
* Returned Value:
|
||||
* Returns OK on success or errno is set.
|
||||
* Returns actual length of data on success or negative value on error.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
@ -206,7 +206,8 @@ static int lis331dl_access(FAR struct lis331dl_dev_s *dev, uint8_t subaddr,
|
||||
}
|
||||
};
|
||||
|
||||
if ((retval = I2C_TRANSFER(dev->i2c, msgv, 2)) == OK)
|
||||
retval = I2C_TRANSFER(dev->i2c, msgv, 2);
|
||||
if (retval >= 0)
|
||||
{
|
||||
return length;
|
||||
}
|
||||
|
@ -137,6 +137,7 @@ static int lm75_i2c_write(FAR struct lm75_dev_s *priv,
|
||||
FAR const uint8_t *buffer, int buflen)
|
||||
{
|
||||
struct i2c_msg_s msg;
|
||||
int ret;
|
||||
|
||||
/* Setup for the transfer */
|
||||
|
||||
@ -148,7 +149,8 @@ static int lm75_i2c_write(FAR struct lm75_dev_s *priv,
|
||||
|
||||
/* Then perform the transfer. */
|
||||
|
||||
return I2C_TRANSFER(priv->i2c, &msg, 1);
|
||||
ret = I2C_TRANSFER(priv->i2c, &msg, 1);
|
||||
return (ret >= 0) ? OK : ret;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
@ -163,6 +165,7 @@ static int lm75_i2c_read(FAR struct lm75_dev_s *priv,
|
||||
FAR uint8_t *buffer, int buflen)
|
||||
{
|
||||
struct i2c_msg_s msg;
|
||||
int ret;
|
||||
|
||||
/* Setup for the transfer */
|
||||
|
||||
@ -174,7 +177,8 @@ static int lm75_i2c_read(FAR struct lm75_dev_s *priv,
|
||||
|
||||
/* Then perform the transfer. */
|
||||
|
||||
return I2C_TRANSFER(priv->i2c, &msg, 1);
|
||||
ret = I2C_TRANSFER(priv->i2c, &msg, 1);
|
||||
return (ret >= 0) ? OK : ret;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
|
@ -139,6 +139,7 @@ static int lm92_i2c_write(FAR struct lm92_dev_s *priv,
|
||||
FAR const uint8_t *buffer, int buflen)
|
||||
{
|
||||
struct i2c_msg_s msg;
|
||||
int ret;
|
||||
|
||||
/* Setup for the transfer */
|
||||
|
||||
@ -150,7 +151,8 @@ static int lm92_i2c_write(FAR struct lm92_dev_s *priv,
|
||||
|
||||
/* Then perform the transfer. */
|
||||
|
||||
return I2C_TRANSFER(priv->i2c, &msg, 1);
|
||||
ret = I2C_TRANSFER(priv->i2c, &msg, 1);
|
||||
return (ret >= 0) ? OK : ret;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
@ -165,6 +167,7 @@ static int lm92_i2c_read(FAR struct lm92_dev_s *priv,
|
||||
FAR uint8_t *buffer, int buflen)
|
||||
{
|
||||
struct i2c_msg_s msg;
|
||||
int ret;
|
||||
|
||||
/* Setup for the transfer */
|
||||
|
||||
@ -176,7 +179,8 @@ static int lm92_i2c_read(FAR struct lm92_dev_s *priv,
|
||||
|
||||
/* Then perform the transfer. */
|
||||
|
||||
return I2C_TRANSFER(priv->i2c, &msg, 1);
|
||||
ret = I2C_TRANSFER(priv->i2c, &msg, 1);
|
||||
return (ret >= 0) ? OK : ret;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
|
@ -253,6 +253,7 @@ static int ms58xx_i2c_write(FAR struct ms58xx_dev_s *priv,
|
||||
FAR const uint8_t *buffer, int buflen)
|
||||
{
|
||||
struct i2c_msg_s msg;
|
||||
int ret;
|
||||
|
||||
/* Setup for the transfer */
|
||||
|
||||
@ -264,7 +265,8 @@ static int ms58xx_i2c_write(FAR struct ms58xx_dev_s *priv,
|
||||
|
||||
/* Then perform the transfer. */
|
||||
|
||||
return I2C_TRANSFER(priv->i2c, &msg, 1);
|
||||
ret = I2C_TRANSFER(priv->i2c, &msg, 1);
|
||||
return (ret >= 0) ? OK : ret;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
@ -279,6 +281,7 @@ static int ms58xx_i2c_read(FAR struct ms58xx_dev_s *priv,
|
||||
FAR uint8_t *buffer, int buflen)
|
||||
{
|
||||
struct i2c_msg_s msg;
|
||||
int ret;
|
||||
|
||||
/* Setup for the transfer */
|
||||
|
||||
@ -290,7 +293,8 @@ static int ms58xx_i2c_read(FAR struct ms58xx_dev_s *priv,
|
||||
|
||||
/* Then perform the transfer. */
|
||||
|
||||
return I2C_TRANSFER(priv->i2c, &msg, 1);
|
||||
ret = I2C_TRANSFER(priv->i2c, &msg, 1);
|
||||
return (ret >= 0) ? OK : ret;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
|
@ -151,7 +151,6 @@ config RTC_DSXXXX
|
||||
bool "DS130x/DS323x RTC Driver"
|
||||
default n
|
||||
select I2C
|
||||
select I2C_TRANSFER
|
||||
select RTC_DATETIME
|
||||
depends on RTC_EXTERNAL
|
||||
---help---
|
||||
@ -204,7 +203,6 @@ config RTC_PCF85263
|
||||
bool "PCF85263 RTC Driver"
|
||||
default n
|
||||
select I2C
|
||||
select I2C_TRANSFER
|
||||
select RTC_DATETIME
|
||||
depends on RTC_EXTERNAL
|
||||
---help---
|
||||
@ -239,7 +237,6 @@ endif # WATCHDOG
|
||||
config TIMERS_CS2100CP
|
||||
bool "CS2100-CP Fraction-N Clock Multiplier"
|
||||
depends on I2C
|
||||
select I2C_TRANSFER
|
||||
|
||||
if TIMERS_CS2100CP
|
||||
|
||||
|
@ -120,6 +120,7 @@ static int cs2100_write_reg(FAR const struct cs2100_config_s *config,
|
||||
uint8_t regaddr, uint8_t regval)
|
||||
{
|
||||
struct i2c_msg_s msgs[2];
|
||||
int ret;
|
||||
|
||||
reginfo("%02x<-%02x\n", regaddr, regval);
|
||||
DEBUGASSERT(config->i2c->ops && config->i2c->ops->transfer);
|
||||
@ -140,7 +141,8 @@ static int cs2100_write_reg(FAR const struct cs2100_config_s *config,
|
||||
|
||||
/* Send the message */
|
||||
|
||||
return I2C_TRANSFER(config->i2c, msgs, 2);
|
||||
ret = I2C_TRANSFER(config->i2c, msgs, 2);
|
||||
return (ret >= 0) ? OK : ret;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
@ -179,7 +181,7 @@ static int cs2100_read_reg(FAR const struct cs2100_config_s *config,
|
||||
/* Send the address followed by a STOP */
|
||||
|
||||
ret = I2C_TRANSFER(config->i2c, &msg, 1);
|
||||
if (ret == OK)
|
||||
if (ret >= 0)
|
||||
{
|
||||
msg.frequency = config->i2cfreq;
|
||||
msg.addr = config->i2caddr;
|
||||
@ -190,13 +192,13 @@ static int cs2100_read_reg(FAR const struct cs2100_config_s *config,
|
||||
/* Read the register beginning with another START */
|
||||
|
||||
ret = I2C_TRANSFER(config->i2c, &msg, 1);
|
||||
if (ret == OK)
|
||||
if (ret >= 0)
|
||||
{
|
||||
reginfo("%02x->%02x\n", regaddr, *regval);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
return (ret >= 0) ? OK : ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -220,6 +222,7 @@ static int cs2100_write_ratio(FAR const struct cs2100_config_s *config,
|
||||
{
|
||||
struct i2c_msg_s msg;
|
||||
uint8_t buffer[5];
|
||||
int ret;
|
||||
|
||||
reginfo("%02x<-%04l\n", CS2100_RATIO0, (unsigned long)ratio);
|
||||
DEBUGASSERT(config->i2c->ops && config->i2c->ops->transfer);
|
||||
@ -240,7 +243,8 @@ static int cs2100_write_ratio(FAR const struct cs2100_config_s *config,
|
||||
|
||||
/* Send the message */
|
||||
|
||||
return I2C_TRANSFER(config->i2c, &msg, 1);
|
||||
ret = I2C_TRANSFER(config->i2c, &msg, 1);
|
||||
return (ret >= 0) ? OK : ret;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
@ -281,7 +285,7 @@ static int cs2100_read_ratio(FAR const struct cs2100_config_s *config,
|
||||
/* Send the address followed by a STOP */
|
||||
|
||||
ret = I2C_TRANSFER(config->i2c, &msg, 1);
|
||||
if (ret == OK)
|
||||
if (ret >= 0)
|
||||
{
|
||||
msg.frequency = config->i2cfreq;
|
||||
msg.addr = config->i2caddr;
|
||||
@ -295,7 +299,7 @@ static int cs2100_read_ratio(FAR const struct cs2100_config_s *config,
|
||||
|
||||
/* Return the ratio */
|
||||
|
||||
if (ret == OK)
|
||||
if (ret >= 0)
|
||||
{
|
||||
*ratio = ((uint32_t)buffer[0] << 24) |
|
||||
((uint32_t)buffer[1] << 16) |
|
||||
@ -306,7 +310,7 @@ static int cs2100_read_ratio(FAR const struct cs2100_config_s *config,
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
return (ret >= 0) ? OK : ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -167,7 +167,7 @@ static int fusb301_getreg(FAR struct fusb301_dev_s *priv, uint8_t reg)
|
||||
for (retries = 0; retries < FUSB301_I2C_RETRIES; retries++)
|
||||
{
|
||||
ret = I2C_TRANSFER(priv->i2c, msg, 2);
|
||||
if (ret == OK)
|
||||
if (ret >= 0)
|
||||
{
|
||||
fusb301_info("reg:%02X, value:%02X\n", reg, regval);
|
||||
return regval;
|
||||
|
Loading…
Reference in New Issue
Block a user