Skip to content

Commit 7ffa818

Browse files
author
Erwan GOURIOU
committed
[dev_i2c] 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.
1 parent ca64f3c commit 7ffa818

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

hal/targets/hal/TARGET_STM/TARGET_STM32F4/i2c_api.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,19 @@ inline int i2c_start(i2c_t *obj) {
213213

214214
inline int i2c_stop(i2c_t *obj) {
215215

216+
int timeout;
216217
struct i2c_s *obj_s = I2C_S(obj);
217218
I2C_TypeDef *i2c = (I2C_TypeDef *)obj_s->i2c;
219+
I2C_HandleTypeDef *handle = &(obj_s->handle);
220+
221+
//Wait Byte transfer finished before sending stop
222+
timeout = FLAG_TIMEOUT;
223+
while (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_BTF) == RESET) {
224+
timeout--;
225+
if (timeout == 0) {
226+
return 0;
227+
}
228+
}
218229

219230
// Generate the STOP condition
220231
i2c->CR1 |= I2C_CR1_STOP;
@@ -350,15 +361,21 @@ int i2c_byte_write(i2c_t *obj, int data) {
350361

351362
handle->Instance->DR = (uint8_t)data;
352363

353-
// Wait until the byte is transmitted
364+
// Wait until the byte (might be the adress) is transmitted
354365
timeout = FLAG_TIMEOUT;
355366
while ((__HAL_I2C_GET_FLAG(handle, I2C_FLAG_TXE) == RESET) &&
356-
(__HAL_I2C_GET_FLAG(handle, I2C_FLAG_BTF) == RESET)) {
367+
(__HAL_I2C_GET_FLAG(handle, I2C_FLAG_BTF) == RESET) &&
368+
(__HAL_I2C_GET_FLAG(handle, I2C_FLAG_ADDR) == RESET)) {
357369
if ((timeout--) == 0) {
358370
return 0;
359371
}
360372
}
361373

374+
if (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_ADDR) != RESET)
375+
{
376+
__HAL_I2C_CLEAR_ADDRFLAG(handle);
377+
}
378+
362379
return 1;
363380
}
364381

libraries/tests/mbed/i2c_master_slave/main.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
// pull-up resistors on both lines
1717
// ********************************************************
1818

19+
#if defined(TARGET_NUCLEO_F411RE)
20+
I2C master(PB_9, PB_8); // I2C_1 (Arduino: D14/D15)
21+
I2CSlave slave(PB_3, PB_10); // I2C_2 (Arduino: D3/D6)
22+
#endif
1923

2024
int main()
2125
{

tools/tests.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
8888
* i2c_loop:
8989
* LPC1768: (p28 <-> p9), (p27 <-> p10)
90+
* NUCLEO_F411RE: (PB_9 <-> PB_3), (PB_8 <-> PB_10)
9091
9192
* i2c_eeprom:
9293
* LPC1*: (SDA=p28 , SCL=p27)
@@ -264,7 +265,7 @@
264265
"id": "MBED_A20", "description": "I2C master/slave test",
265266
"source_dir": join(TEST_DIR, "mbed", "i2c_master_slave"),
266267
"dependencies": [MBED_LIBRARIES, TEST_MBED_LIB,],
267-
"mcu": ["LPC1768", "RZ_A1H"],
268+
"mcu": ["LPC1768", "RZ_A1H", "NUCLEO_F411RE"],
268269
"peripherals": ["i2c_loop"]
269270
},
270271
{

0 commit comments

Comments
 (0)