Skip to content

Commit 8406a99

Browse files
committed
STM32 I2C: avoid timeout to be 0
In continuation of previous IsDeviceReady case, let's add 1 in case length is 0 (even though not recommended)
1 parent 37c94a0 commit 8406a99

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

targets/TARGET_STM/i2c_api.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
668668
ret = HAL_I2C_Master_Sequential_Receive_IT(handle, address, (uint8_t *) data, length, obj_s->XferOperation);
669669

670670
if(ret == HAL_OK) {
671-
timeout = BYTE_TIMEOUT_US * length;
671+
timeout = BYTE_TIMEOUT_US * (length + 1);
672672
/* transfer started : wait completion or timeout */
673673
while(!(obj_s->event & I2C_EVENT_ALL) && (--timeout != 0)) {
674674
wait_us(1);
@@ -724,7 +724,7 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
724724
ret = HAL_I2C_Master_Sequential_Transmit_IT(handle, address, (uint8_t *) data, length, obj_s->XferOperation);
725725

726726
if(ret == HAL_OK) {
727-
timeout = BYTE_TIMEOUT_US * length;
727+
timeout = BYTE_TIMEOUT_US * (length + 1);
728728
/* transfer started : wait completion or timeout */
729729
while(!(obj_s->event & I2C_EVENT_ALL) && (--timeout != 0)) {
730730
wait_us(1);
@@ -903,7 +903,7 @@ int i2c_slave_read(i2c_t *obj, char *data, int length) {
903903
ret = HAL_I2C_Slave_Sequential_Receive_IT(handle, (uint8_t *) data, length, I2C_NEXT_FRAME);
904904

905905
if(ret == HAL_OK) {
906-
timeout = BYTE_TIMEOUT_US * length;
906+
timeout = BYTE_TIMEOUT_US * (length + 1);
907907
while(obj_s->pending_slave_rx_maxter_tx && (--timeout != 0)) {
908908
wait_us(1);
909909
}
@@ -928,7 +928,7 @@ int i2c_slave_write(i2c_t *obj, const char *data, int length) {
928928
ret = HAL_I2C_Slave_Sequential_Transmit_IT(handle, (uint8_t *) data, length, I2C_NEXT_FRAME);
929929

930930
if(ret == HAL_OK) {
931-
timeout = BYTE_TIMEOUT_US * length;
931+
timeout = BYTE_TIMEOUT_US * (length + 1);
932932
while(obj_s->pending_slave_tx_master_rx && (--timeout != 0)) {
933933
wait_us(1);
934934
}

0 commit comments

Comments
 (0)