summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2018-06-15 14:10:53 +1000
committerDamien George <damien.p.george@gmail.com>2018-06-15 14:10:53 +1000
commitbc5e8a2cb6f17bc5a9fec108a2421adc77f5cb3c (patch)
tree95295651b19926ce35760e63e0dd37c7718e0500
parent37a7257affa5e1ad449d911797dfc4b9e46677b3 (diff)
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.
-rw-r--r--ports/stm32/i2c.c7
1 files changed, 6 insertions, 1 deletions
diff --git a/ports/stm32/i2c.c b/ports/stm32/i2c.c
index e0eea90ab..109b9418f 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;