From df19d33b779a2f2dea41e6e8b2e8e9e6b59ebba8 Mon Sep 17 00:00:00 2001 From: MrKevinWeiss Date: Fri, 25 Oct 2019 10:19:14 +0200 Subject: [PATCH] stm32_common/i2c_2: Fix repeated read condition Fix the condition to return -ENOPNOTSUPP when i2c repeated read attempted. Currently the error occures even if a read after write is attempted. This is the standard way to i2c_read_reg which should be supported. The -EOPNOTSUPP requires the previous R/W state to be reading. This means a `I2C_SR2_TRA` must be checked to be 0. --- cpu/stm32_common/periph/i2c_2.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/cpu/stm32_common/periph/i2c_2.c b/cpu/stm32_common/periph/i2c_2.c index b18ac8bd7d..5a68a0167e 100644 --- a/cpu/stm32_common/periph/i2c_2.c +++ b/cpu/stm32_common/periph/i2c_2.c @@ -218,8 +218,13 @@ int i2c_read_bytes(i2c_t dev, uint16_t address, void *data, size_t length, I2C_TypeDef *i2c = i2c_config[dev].dev; DEBUG("[i2c] read_bytes: Starting\n"); - /* Do not support repeated start reading */ - if ((i2c->SR2 & I2C_SR2_BUSY) && !(flags & I2C_NOSTART)) { + /* Do not support repeated start reading + * The repeated start read requires the bus to be busy (I2C_SR2_BUSY == 1) + * the previous R/W state to be a read (I2C_SR2_TRA == 0) + * and for the command not to be split frame (I2C_NOSTART == 0) + */ + if (((i2c->SR2 & (I2C_SR2_BUSY | I2C_SR2_TRA)) == I2C_SR2_BUSY) && + !(flags & I2C_NOSTART)) { return -EOPNOTSUPP; } int ret = _start(i2c, (address << 1) | I2C_FLAG_READ, flags, length);