Skip to content

Commit 179a4d8

Browse files
Fix initialization of XferOperation for API v1, optimize stop()
1 parent c929dd7 commit 179a4d8

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

targets/TARGET_STM/i2c_api.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,9 @@ void i2c_init_internal(i2c_t *obj, const i2c_pinmap_t *pinmap)
525525

526526
#ifdef I2C_IP_VERSION_V2
527527
obj_s->current_hz = obj_s->hz;
528+
#else
529+
// I2C Xfer operation init
530+
obj_s->XferOperation = I2C_FIRST_AND_LAST_FRAME;
528531
#endif
529532

530533
#if DEVICE_I2CSLAVE
@@ -534,7 +537,6 @@ void i2c_init_internal(i2c_t *obj, const i2c_pinmap_t *pinmap)
534537
obj_s->pending_slave_rx_maxter_tx = 0;
535538
#endif
536539

537-
// I2C Xfer operation init
538540
obj_s->event = 0;
539541
STM_I2C_SET_STATE(obj_s, STM_I2C_IDLE);
540542
}
@@ -820,11 +822,14 @@ int i2c_stop(i2c_t *obj)
820822
// Generate the STOP condition
821823
i2c->CR1 |= I2C_CR1_STOP;
822824

823-
/* In case of mixed usage of the APIs (unitary + SYNC)
824-
* re-init HAL state
825-
*/
826-
if (obj_s->XferOperation != I2C_FIRST_AND_LAST_FRAME) {
827-
i2c_init_internal(obj, NULL);
825+
obj_s->XferOperation = I2C_FIRST_AND_LAST_FRAME;
826+
827+
// Wait until condition is generated
828+
int timeout = FLAG_TIMEOUT;
829+
while (__HAL_I2C_GET_FLAG(&obj_s->handle, I2C_FLAG_BUSY)) {
830+
if ((timeout--) == 0) {
831+
return -1;
832+
}
828833
}
829834

830835
return 0;

0 commit comments

Comments
 (0)