From bc5e8a2cb6f17bc5a9fec108a2421adc77f5cb3c Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 15 Jun 2018 14:10:53 +1000 Subject: [PATCH] stm32/i2c: Fix num_acks calculation in i2c_write for F0 and F7 MCU's. Due to buffering of outgoing bytes on the I2C bus, detection of a NACK using the ISR_NACKF flag needs to account for the case where ISR_NACKF corresponds to the previous-to-previous byte. --- ports/stm32/i2c.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ports/stm32/i2c.c b/ports/stm32/i2c.c index e0eea90ab7..109b9418f8 100644 --- a/ports/stm32/i2c.c +++ b/ports/stm32/i2c.c @@ -421,8 +421,13 @@ int i2c_write(i2c_t *i2c, const uint8_t *src, size_t len, size_t next_len) { if ((ret = i2c_wait_isr_set(i2c, I2C_ISR_TCR | I2C_ISR_TC | I2C_ISR_STOPF))) { return ret; } - if (i2c->ISR & I2C_ISR_NACKF) { + uint32_t isr = i2c->ISR; + if (isr & I2C_ISR_NACKF) { // Slave did not respond to byte so stop sending + if (!(isr & I2C_ISR_TXE)) { + // The TXDR is still full so the byte previous to that wasn't actually ACK'd + --num_acks; + } break; } ++num_acks;