From de121a308ae6c5137365315a195505d42ac61429 Mon Sep 17 00:00:00 2001 From: Alexandre Bourdiol Date: Mon, 26 Aug 2019 14:00:08 +0200 Subject: [PATCH] Fix I2C issue with test mbed_hal_fpga_ci_test_shield On last case #5 there was a last unexpected read. It happened when stop condition was generated --- targets/TARGET_STM/i2c_api.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/targets/TARGET_STM/i2c_api.c b/targets/TARGET_STM/i2c_api.c index 16ea84776d..995bda07cb 100644 --- a/targets/TARGET_STM/i2c_api.c +++ b/targets/TARGET_STM/i2c_api.c @@ -598,8 +598,6 @@ int i2c_stop(i2c_t *obj) return 0; } #endif - // Disable reload mode - handle->Instance->CR2 &= (uint32_t)~I2C_CR2_RELOAD; // Ensure the transmission is started before sending a stop if ((handle->Instance->CR2 & (uint32_t)I2C_CR2_RD_WRN) == 0) { @@ -612,7 +610,7 @@ int i2c_stop(i2c_t *obj) } // Generate the STOP condition - handle->Instance->CR2 |= I2C_CR2_STOP; + handle->Instance->CR2 = I2C_CR2_STOP; timeout = FLAG_TIMEOUT; while (!__HAL_I2C_GET_FLAG(handle, I2C_FLAG_STOPF)) { @@ -665,9 +663,16 @@ int i2c_byte_read(i2c_t *obj, int last) } } - /* Enable reload mode as we don't know how many bytes will be sent */ - /* and set transfer size to 1 */ - tmpreg |= I2C_CR2_RELOAD | (I2C_CR2_NBYTES & (1 << 16)); + if (last) { + /* Disable Address Acknowledge */ + tmpreg = tmpreg & (~I2C_CR2_RELOAD); + tmpreg |= I2C_CR2_NACK | (I2C_CR2_NBYTES & (1 << 16)); + } else { + /* Enable reload mode as we don't know how many bytes will be sent */ + /* and set transfer size to 1 */ + tmpreg |= I2C_CR2_RELOAD | (I2C_CR2_NBYTES & (1 << 16)); + } + /* Set the prepared configuration */ handle->Instance->CR2 = tmpreg; @@ -681,11 +686,6 @@ int i2c_byte_read(i2c_t *obj, int last) /* Then Get Byte */ data = handle->Instance->RXDR; - if (last) { - /* Disable Address Acknowledge */ - handle->Instance->CR2 |= I2C_CR2_NACK; - } - return data; }