Skip to content

Commit bfefd8a

Browse files
committed
PIO I2C example: clear ISR counter on each start/repstart
This avoids a spurious shift on a write followed by a read. See discussion on #617
1 parent 18b6b37 commit bfefd8a

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

pio/i2c/pio_i2c.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,10 @@ uint8_t pio_i2c_get(PIO pio, uint sm) {
6767
}
6868

6969
void pio_i2c_start(PIO pio, uint sm) {
70-
pio_i2c_put_or_err(pio, sm, 1u << PIO_I2C_ICOUNT_LSB); // Escape code for 2 instruction sequence
70+
pio_i2c_put_or_err(pio, sm, 3u << PIO_I2C_ICOUNT_LSB); // Escape code for 3 instruction sequence
7171
pio_i2c_put_or_err(pio, sm, set_scl_sda_program_instructions[I2C_SC1_SD0]); // We are already in idle state, just pull SDA low
7272
pio_i2c_put_or_err(pio, sm, set_scl_sda_program_instructions[I2C_SC0_SD0]); // Also pull clock low so we can present data
73+
pio_i2c_put_or_err(pio, sm, pio_encode_mov(pio_isr, pio_null)); // Ensure ISR counter is clear following a write
7374
}
7475

7576
void pio_i2c_stop(PIO pio, uint sm) {
@@ -80,11 +81,12 @@ void pio_i2c_stop(PIO pio, uint sm) {
8081
};
8182

8283
void pio_i2c_repstart(PIO pio, uint sm) {
83-
pio_i2c_put_or_err(pio, sm, 3u << PIO_I2C_ICOUNT_LSB);
84+
pio_i2c_put_or_err(pio, sm, 4u << PIO_I2C_ICOUNT_LSB);
8485
pio_i2c_put_or_err(pio, sm, set_scl_sda_program_instructions[I2C_SC0_SD1]);
8586
pio_i2c_put_or_err(pio, sm, set_scl_sda_program_instructions[I2C_SC1_SD1]);
8687
pio_i2c_put_or_err(pio, sm, set_scl_sda_program_instructions[I2C_SC1_SD0]);
8788
pio_i2c_put_or_err(pio, sm, set_scl_sda_program_instructions[I2C_SC0_SD0]);
89+
pio_i2c_put_or_err(pio, sm, pio_encode_mov(pio_isr, pio_null));
8890
}
8991

9092
static void pio_i2c_wait_idle(PIO pio, uint sm) {

0 commit comments

Comments
 (0)