From 9b204f8be20d686a61ceda81dba186def7b471cf Mon Sep 17 00:00:00 2001 From: Erwan GOURIOU Date: Thu, 1 Sep 2016 17:25:45 +0200 Subject: [PATCH] Fix I2C byte transfer API to enable Master/Slave test on F411RE i2c_byte_write could be used to send byte and address. In case used for address, ADDR Flag should be reset. --- .../hal/TARGET_STM/TARGET_STM32F4/i2c_api.c | 21 +++++++++++++++++-- .../tests/mbed/i2c_master_slave/main.cpp | 4 ++++ tools/tests.py | 3 ++- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/hal/targets/hal/TARGET_STM/TARGET_STM32F4/i2c_api.c b/hal/targets/hal/TARGET_STM/TARGET_STM32F4/i2c_api.c index 0efae0421a..dc5f6cc4d5 100644 --- a/hal/targets/hal/TARGET_STM/TARGET_STM32F4/i2c_api.c +++ b/hal/targets/hal/TARGET_STM/TARGET_STM32F4/i2c_api.c @@ -213,8 +213,19 @@ inline int i2c_start(i2c_t *obj) { inline int i2c_stop(i2c_t *obj) { + int timeout; struct i2c_s *obj_s = I2C_S(obj); I2C_TypeDef *i2c = (I2C_TypeDef *)obj_s->i2c; + I2C_HandleTypeDef *handle = &(obj_s->handle); + + //Wait Byte transfer finished before sending stop + timeout = FLAG_TIMEOUT; + while (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_BTF) == RESET) { + timeout--; + if (timeout == 0) { + return 0; + } + } // Generate the STOP condition i2c->CR1 |= I2C_CR1_STOP; @@ -350,15 +361,21 @@ int i2c_byte_write(i2c_t *obj, int data) { handle->Instance->DR = (uint8_t)data; - // Wait until the byte is transmitted + // Wait until the byte (might be the adress) is transmitted timeout = FLAG_TIMEOUT; while ((__HAL_I2C_GET_FLAG(handle, I2C_FLAG_TXE) == RESET) && - (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_BTF) == RESET)) { + (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_BTF) == RESET) && + (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_ADDR) == RESET)) { if ((timeout--) == 0) { return 0; } } + if (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_ADDR) != RESET) + { + __HAL_I2C_CLEAR_ADDRFLAG(handle); + } + return 1; } diff --git a/libraries/tests/mbed/i2c_master_slave/main.cpp b/libraries/tests/mbed/i2c_master_slave/main.cpp index 76fdb1a337..10b1e82117 100644 --- a/libraries/tests/mbed/i2c_master_slave/main.cpp +++ b/libraries/tests/mbed/i2c_master_slave/main.cpp @@ -16,6 +16,10 @@ // pull-up resistors on both lines // ******************************************************** +#if defined(TARGET_NUCLEO_F411RE) +I2C master(PB_9, PB_8); // I2C_1 (Arduino: D14/D15) +I2CSlave slave(PB_3, PB_10); // I2C_2 (Arduino: D3/D6) +#endif int main() { diff --git a/tools/tests.py b/tools/tests.py index 67570fe868..b93661c3eb 100644 --- a/tools/tests.py +++ b/tools/tests.py @@ -87,6 +87,7 @@ Wiring: * i2c_loop: * LPC1768: (p28 <-> p9), (p27 <-> p10) + * NUCLEO_F411RE: (PB_9 <-> PB_3), (PB_8 <-> PB_10) * i2c_eeprom: * LPC1*: (SDA=p28 , SCL=p27) @@ -264,7 +265,7 @@ TESTS = [ "id": "MBED_A20", "description": "I2C master/slave test", "source_dir": join(TEST_DIR, "mbed", "i2c_master_slave"), "dependencies": [MBED_LIBRARIES, TEST_MBED_LIB,], - "mcu": ["LPC1768", "RZ_A1H"], + "mcu": ["LPC1768", "RZ_A1H", "NUCLEO_F411RE"], "peripherals": ["i2c_loop"] }, {