Skip to content

Commit de121a3

Browse files
committed
Fix I2C issue with test mbed_hal_fpga_ci_test_shield
On last case #5 there was a last unexpected read. It happened when stop condition was generated
1 parent 7910de2 commit de121a3

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

targets/TARGET_STM/i2c_api.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -598,8 +598,6 @@ int i2c_stop(i2c_t *obj)
598598
return 0;
599599
}
600600
#endif
601-
// Disable reload mode
602-
handle->Instance->CR2 &= (uint32_t)~I2C_CR2_RELOAD;
603601

604602
// Ensure the transmission is started before sending a stop
605603
if ((handle->Instance->CR2 & (uint32_t)I2C_CR2_RD_WRN) == 0) {
@@ -612,7 +610,7 @@ int i2c_stop(i2c_t *obj)
612610
}
613611

614612
// Generate the STOP condition
615-
handle->Instance->CR2 |= I2C_CR2_STOP;
613+
handle->Instance->CR2 = I2C_CR2_STOP;
616614

617615
timeout = FLAG_TIMEOUT;
618616
while (!__HAL_I2C_GET_FLAG(handle, I2C_FLAG_STOPF)) {
@@ -665,9 +663,16 @@ int i2c_byte_read(i2c_t *obj, int last)
665663
}
666664
}
667665

668-
/* Enable reload mode as we don't know how many bytes will be sent */
669-
/* and set transfer size to 1 */
670-
tmpreg |= I2C_CR2_RELOAD | (I2C_CR2_NBYTES & (1 << 16));
666+
if (last) {
667+
/* Disable Address Acknowledge */
668+
tmpreg = tmpreg & (~I2C_CR2_RELOAD);
669+
tmpreg |= I2C_CR2_NACK | (I2C_CR2_NBYTES & (1 << 16));
670+
} else {
671+
/* Enable reload mode as we don't know how many bytes will be sent */
672+
/* and set transfer size to 1 */
673+
tmpreg |= I2C_CR2_RELOAD | (I2C_CR2_NBYTES & (1 << 16));
674+
}
675+
671676
/* Set the prepared configuration */
672677
handle->Instance->CR2 = tmpreg;
673678

@@ -681,11 +686,6 @@ int i2c_byte_read(i2c_t *obj, int last)
681686
/* Then Get Byte */
682687
data = handle->Instance->RXDR;
683688

684-
if (last) {
685-
/* Disable Address Acknowledge */
686-
handle->Instance->CR2 |= I2C_CR2_NACK;
687-
}
688-
689689
return data;
690690
}
691691

0 commit comments

Comments
 (0)