From e14e6ead92c3ae6abf180dd7d82bcea1fc9059a5 Mon Sep 17 00:00:00 2001 From: MrKevinWeiss Date: Mon, 10 Dec 2018 09:49:18 +0100 Subject: [PATCH 1/2] drivers/periph_common/i2c: Add not supported codes This commit returns accurate error codes and prevents improper i2c states. If since i2c_read_reg and i2c_write_reg are full frames the I2C_NOSTOP/START commands should not be supported --- drivers/periph_common/i2c.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/drivers/periph_common/i2c.c b/drivers/periph_common/i2c.c index 4cde5dd493..f6e3ad0b00 100644 --- a/drivers/periph_common/i2c.c +++ b/drivers/periph_common/i2c.c @@ -17,6 +17,7 @@ * * @} */ +#include #include "board.h" #include "cpu.h" @@ -36,6 +37,9 @@ int i2c_read_reg(i2c_t dev, uint16_t addr, uint16_t reg, int i2c_read_regs(i2c_t dev, uint16_t addr, uint16_t reg, void *data, size_t len, uint8_t flags) { + if (flags & (I2C_NOSTOP | I2C_NOSTART)) { + return -EOPNOTSUPP; + } /* First set ADDR and register with no stop */ int ret = i2c_write_bytes(dev, addr, ®, (flags & I2C_REG16) ? 2 : 1, flags | I2C_NOSTOP); @@ -69,6 +73,9 @@ int i2c_write_reg(i2c_t dev, uint16_t addr, uint16_t reg, int i2c_write_regs(i2c_t dev, uint16_t addr, uint16_t reg, const void *data, size_t len, uint8_t flags) { + if (flags & (I2C_NOSTOP | I2C_NOSTART)) { + return -EOPNOTSUPP; + } /* First set ADDR and register with no stop */ int ret = i2c_write_bytes(dev, addr, ®, (flags & I2C_REG16) ? 2 : 1, flags | I2C_NOSTOP); From 0ad94e1b4b14ce682b19b69d34480801b2933384 Mon Sep 17 00:00:00 2001 From: MrKevinWeiss Date: Mon, 10 Dec 2018 09:56:31 +0100 Subject: [PATCH 2/2] drivers/include/i2c: Fix API documentation The write_reg/s states a repeated start write is used but that is not how the i2c is implemeneted. Most devices also only use a continuous write so the API should be changed to match implementation and the majority of sensors. --- drivers/include/periph/i2c.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/include/periph/i2c.h b/drivers/include/periph/i2c.h index 42bcf1be2b..939884bc60 100644 --- a/drivers/include/periph/i2c.h +++ b/drivers/include/periph/i2c.h @@ -388,8 +388,8 @@ int i2c_write_bytes(i2c_t dev, uint16_t addr, const void *data, * @brief Convenience function for writing one byte to a given * register address * - * @note This function is using a repeated start sequence for writing to the - * specified register address. + * @note This function is using a continuous sequence for writing to the + * specified register address. It first writes the register then data. * * @pre i2c_acquire must be called before accessing the bus * @@ -414,8 +414,8 @@ int i2c_write_reg(i2c_t dev, uint16_t addr, uint16_t reg, /** * @brief Convenience function for writing data to a given register address * - * @note This function is using a repeated start sequence for writing to the - * specified register address. + * @note This function is using a continuous sequence for writing to the + * specified register address. It first writes the register then data. * * @pre i2c_acquire must be called before accessing the bus *