@@ -68,7 +68,6 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
68
68
// Enable I2C1 clock and pinout if not done
69
69
if ((obj_s -> i2c == I2C_1 ) && !i2c1_inited ) {
70
70
i2c1_inited = 1 ;
71
- __I2C1_CLK_ENABLE ();
72
71
// Configure I2C pins
73
72
pinmap_pinout (sda , PinMap_I2C_SDA );
74
73
pinmap_pinout (scl , PinMap_I2C_SCL );
@@ -78,11 +77,11 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
78
77
obj_s -> event_i2cIRQ = I2C1_EV_IRQn ;
79
78
obj_s -> error_i2cIRQ = I2C1_ER_IRQn ;
80
79
#endif
80
+ __I2C1_CLK_ENABLE ();
81
81
}
82
82
// Enable I2C2 clock and pinout if not done
83
83
if ((obj_s -> i2c == I2C_2 ) && !i2c2_inited ) {
84
84
i2c2_inited = 1 ;
85
- __I2C2_CLK_ENABLE ();
86
85
// Configure I2C pins
87
86
pinmap_pinout (sda , PinMap_I2C_SDA );
88
87
pinmap_pinout (scl , PinMap_I2C_SCL );
@@ -92,12 +91,12 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
92
91
obj_s -> event_i2cIRQ = I2C2_EV_IRQn ;
93
92
obj_s -> error_i2cIRQ = I2C2_ER_IRQn ;
94
93
#endif
94
+ __I2C2_CLK_ENABLE ();
95
95
}
96
96
#if defined I2C3_BASE
97
97
// Enable I2C3 clock and pinout if not done
98
98
if ((obj_s -> i2c == I2C_3 ) && !i2c3_inited ) {
99
99
i2c3_inited = 1 ;
100
- __I2C3_CLK_ENABLE ();
101
100
// Configure I2C pins
102
101
pinmap_pinout (sda , PinMap_I2C_SDA );
103
102
pinmap_pinout (scl , PinMap_I2C_SCL );
@@ -107,14 +106,14 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
107
106
obj_s -> event_i2cIRQ = I2C3_EV_IRQn ;
108
107
obj_s -> error_i2cIRQ = I2C3_ER_IRQn ;
109
108
#endif
109
+ __I2C3_CLK_ENABLE ();
110
110
}
111
111
#endif
112
112
113
113
#if defined FMPI2C1_BASE
114
114
// Enable I2C3 clock and pinout if not done
115
115
if ((obj_s -> i2c == FMPI2C_1 ) && !fmpi2c1_inited ) {
116
116
fmpi2c1_inited = 1 ;
117
- __HAL_RCC_FMPI2C1_CLK_ENABLE ();
118
117
// Configure I2C pins
119
118
pinmap_pinout (sda , PinMap_I2C_SDA );
120
119
pinmap_pinout (scl , PinMap_I2C_SCL );
@@ -124,6 +123,7 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
124
123
obj_s -> event_i2cIRQ = FMPI2C1_EV_IRQn ;
125
124
obj_s -> error_i2cIRQ = FMPI2C1_ER_IRQn ;
126
125
#endif
126
+ __HAL_RCC_FMPI2C1_CLK_ENABLE ();
127
127
}
128
128
#endif
129
129
@@ -151,8 +151,6 @@ void i2c_frequency(i2c_t *obj, int hz)
151
151
struct i2c_s * obj_s = I2C_S (obj );
152
152
I2C_HandleTypeDef * handle = & (obj_s -> handle );
153
153
154
- handle -> Instance = (I2C_TypeDef * )(obj_s -> i2c );
155
-
156
154
MBED_ASSERT ((hz > 0 ) && (hz <= 400000 ));
157
155
158
156
// wait before init
@@ -212,20 +210,8 @@ inline int i2c_start(i2c_t *obj) {
212
210
}
213
211
214
212
inline int i2c_stop (i2c_t * obj ) {
215
-
216
- int timeout ;
217
213
struct i2c_s * obj_s = I2C_S (obj );
218
214
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
- }
229
215
230
216
// Generate the STOP condition
231
217
i2c -> CR1 |= I2C_CR1_STOP ;
@@ -361,7 +347,7 @@ int i2c_byte_write(i2c_t *obj, int data) {
361
347
362
348
handle -> Instance -> DR = (uint8_t )data ;
363
349
364
- // Wait until the byte (might be the adress ) is transmitted
350
+ // Wait until the byte (might be the address ) is transmitted
365
351
timeout = FLAG_TIMEOUT ;
366
352
while ((__HAL_I2C_GET_FLAG (handle , I2C_FLAG_TXE ) == RESET ) &&
367
353
(__HAL_I2C_GET_FLAG (handle , I2C_FLAG_BTF ) == RESET ) &&
@@ -385,6 +371,8 @@ void i2c_reset(i2c_t *obj) {
385
371
struct i2c_s * obj_s = I2C_S (obj );
386
372
I2C_HandleTypeDef * handle = & (obj_s -> handle );
387
373
374
+ handle -> Instance = (I2C_TypeDef * )(obj_s -> i2c );
375
+
388
376
// wait before reset
389
377
timeout = LONG_TIMEOUT ;
390
378
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) {
570
558
}
571
559
}
572
560
573
-
574
561
/* Clear AF flag */
575
562
__HAL_I2C_CLEAR_FLAG (handle , I2C_FLAG_AF );
576
563
0 commit comments