Skip to content

Commit 217a8fb

Browse files
authored
Merge pull request #2972 from betzw/betzw_i2c_wb
Make (synchronous) I2C work again
2 parents 35d6682 + d258c14 commit 217a8fb

File tree

1 file changed

+7
-20
lines changed

1 file changed

+7
-20
lines changed

targets/TARGET_STM/TARGET_STM32F4/i2c_api.c

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
6868
// Enable I2C1 clock and pinout if not done
6969
if ((obj_s->i2c == I2C_1) && !i2c1_inited) {
7070
i2c1_inited = 1;
71-
__I2C1_CLK_ENABLE();
7271
// Configure I2C pins
7372
pinmap_pinout(sda, PinMap_I2C_SDA);
7473
pinmap_pinout(scl, PinMap_I2C_SCL);
@@ -78,11 +77,11 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
7877
obj_s->event_i2cIRQ = I2C1_EV_IRQn;
7978
obj_s->error_i2cIRQ = I2C1_ER_IRQn;
8079
#endif
80+
__I2C1_CLK_ENABLE();
8181
}
8282
// Enable I2C2 clock and pinout if not done
8383
if ((obj_s->i2c == I2C_2) && !i2c2_inited) {
8484
i2c2_inited = 1;
85-
__I2C2_CLK_ENABLE();
8685
// Configure I2C pins
8786
pinmap_pinout(sda, PinMap_I2C_SDA);
8887
pinmap_pinout(scl, PinMap_I2C_SCL);
@@ -92,12 +91,12 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
9291
obj_s->event_i2cIRQ = I2C2_EV_IRQn;
9392
obj_s->error_i2cIRQ = I2C2_ER_IRQn;
9493
#endif
94+
__I2C2_CLK_ENABLE();
9595
}
9696
#if defined I2C3_BASE
9797
// Enable I2C3 clock and pinout if not done
9898
if ((obj_s->i2c == I2C_3) && !i2c3_inited) {
9999
i2c3_inited = 1;
100-
__I2C3_CLK_ENABLE();
101100
// Configure I2C pins
102101
pinmap_pinout(sda, PinMap_I2C_SDA);
103102
pinmap_pinout(scl, PinMap_I2C_SCL);
@@ -107,14 +106,14 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
107106
obj_s->event_i2cIRQ = I2C3_EV_IRQn;
108107
obj_s->error_i2cIRQ = I2C3_ER_IRQn;
109108
#endif
109+
__I2C3_CLK_ENABLE();
110110
}
111111
#endif
112112

113113
#if defined FMPI2C1_BASE
114114
// Enable I2C3 clock and pinout if not done
115115
if ((obj_s->i2c == FMPI2C_1) && !fmpi2c1_inited) {
116116
fmpi2c1_inited = 1;
117-
__HAL_RCC_FMPI2C1_CLK_ENABLE();
118117
// Configure I2C pins
119118
pinmap_pinout(sda, PinMap_I2C_SDA);
120119
pinmap_pinout(scl, PinMap_I2C_SCL);
@@ -124,6 +123,7 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
124123
obj_s->event_i2cIRQ = FMPI2C1_EV_IRQn;
125124
obj_s->error_i2cIRQ = FMPI2C1_ER_IRQn;
126125
#endif
126+
__HAL_RCC_FMPI2C1_CLK_ENABLE();
127127
}
128128
#endif
129129

@@ -151,8 +151,6 @@ void i2c_frequency(i2c_t *obj, int hz)
151151
struct i2c_s *obj_s = I2C_S(obj);
152152
I2C_HandleTypeDef *handle = &(obj_s->handle);
153153

154-
handle->Instance = (I2C_TypeDef *)(obj_s->i2c);
155-
156154
MBED_ASSERT((hz > 0) && (hz <= 400000));
157155

158156
// wait before init
@@ -212,20 +210,8 @@ inline int i2c_start(i2c_t *obj) {
212210
}
213211

214212
inline int i2c_stop(i2c_t *obj) {
215-
216-
int timeout;
217213
struct i2c_s *obj_s = I2C_S(obj);
218214
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-
}
229215

230216
// Generate the STOP condition
231217
i2c->CR1 |= I2C_CR1_STOP;
@@ -361,7 +347,7 @@ int i2c_byte_write(i2c_t *obj, int data) {
361347

362348
handle->Instance->DR = (uint8_t)data;
363349

364-
// Wait until the byte (might be the adress) is transmitted
350+
// Wait until the byte (might be the address) is transmitted
365351
timeout = FLAG_TIMEOUT;
366352
while ((__HAL_I2C_GET_FLAG(handle, I2C_FLAG_TXE) == RESET) &&
367353
(__HAL_I2C_GET_FLAG(handle, I2C_FLAG_BTF) == RESET) &&
@@ -385,6 +371,8 @@ void i2c_reset(i2c_t *obj) {
385371
struct i2c_s *obj_s = I2C_S(obj);
386372
I2C_HandleTypeDef *handle = &(obj_s->handle);
387373

374+
handle->Instance = (I2C_TypeDef *)(obj_s->i2c);
375+
388376
// wait before reset
389377
timeout = LONG_TIMEOUT;
390378
while ((__HAL_I2C_GET_FLAG(handle, I2C_FLAG_BUSY)) && (timeout-- != 0));
@@ -570,7 +558,6 @@ int i2c_slave_write(i2c_t *obj, const char *data, int length) {
570558
}
571559
}
572560

573-
574561
/* Clear AF flag */
575562
__HAL_I2C_CLEAR_FLAG(handle, I2C_FLAG_AF);
576563

0 commit comments

Comments
 (0)