Skip to content

Commit 2443878

Browse files
committed
stm32/i2cslave: Support i2c_slave_process_tx_end callback on F4.
The rounds out the F4 implementation to match the other supported MCUs. Signed-off-by: Damien George <[email protected]>
1 parent 17d0449 commit 2443878

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

ports/stm32/i2cslave.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,29 @@
2828

2929
#if defined(STM32F4)
3030

31+
// The hardware triggers the following IRQs for the given scenarios:
32+
// - scan (0-length write): ADDR STOPF
33+
// - write of n bytes: ADDR RXNE*n STOPF
34+
// - read of n bytes: ADDR TXE*(n+1) AF
35+
// - write of n bytes then read of m bytes: ADDR RXNE*n ADDR TXE*(m+1) AF
36+
3137
void i2c_slave_init_helper(i2c_slave_t *i2c, int addr) {
32-
i2c->CR2 = I2C_CR2_ITBUFEN | I2C_CR2_ITEVTEN | 4 << I2C_CR2_FREQ_Pos;
38+
i2c->CR2 = I2C_CR2_ITBUFEN | I2C_CR2_ITEVTEN | 4 << I2C_CR2_FREQ_Pos | I2C_CR2_ITERREN;
3339
i2c->OAR1 = 1 << 14 | addr << 1;
3440
i2c->OAR2 = 0;
3541
i2c->CR1 = I2C_CR1_ACK | I2C_CR1_PE;
3642
}
3743

3844
void i2c_slave_irq_handler(i2c_slave_t *i2c) {
3945
uint32_t sr1 = i2c->SR1;
46+
47+
// Clear all error flags.
48+
i2c->SR1 &= ~(I2C_SR1_SMBALERT | I2C_SR1_TIMEOUT | I2C_SR1_PECERR | I2C_SR1_OVR | I2C_SR1_AF | I2C_SR1_ARLO | I2C_SR1_BERR);
49+
50+
if (sr1 & I2C_SR1_AF) {
51+
// A NACK in TX mode, which is a stop condition.
52+
i2c_slave_process_tx_end(i2c);
53+
}
4054
if (sr1 & I2C_SR1_ADDR) {
4155
// Address matched
4256
// Read of SR1, SR2 needed to clear ADDR bit

0 commit comments

Comments
 (0)