Skip to content

Commit 05fae5d

Browse files
committed
raspberrypi/I2CTarget: Fixed bug where I2C starts were seen as restarts.
The Rasperry Pi Pico, based on the RP2040, has a register that maintains the status of I2C interrupt flags called IC_INTR_STAT. The bits of this register are set by hardware and cleared by software. Before this commit, the I2CTarget library did not clear the restart bit (R_RESTART_DET) in this register after an I2C transaction ended, causing the is_restart field of the i2ctarget_i2c_target_request_obj_t struct to always be true after the first I2C transaction. This commit causes the restart and stop bits to get cleared when the I2C transaction ends. Signed-off-by: Amaar Ebrahim <[email protected]>
1 parent 7809b8d commit 05fae5d

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

ports/raspberrypi/common-hal/i2ctarget/I2CTarget.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ void common_hal_i2ctarget_i2c_target_deinit(i2ctarget_i2c_target_obj_t *self) {
8585
}
8686

8787
int common_hal_i2ctarget_i2c_target_is_addressed(i2ctarget_i2c_target_obj_t *self, uint8_t *address, bool *is_read, bool *is_restart) {
88+
common_hal_i2ctarget_i2c_target_is_stop(self);
89+
8890
if (!((self->peripheral->hw->raw_intr_stat & I2C_IC_INTR_STAT_R_RX_FULL_BITS) || (self->peripheral->hw->raw_intr_stat & I2C_IC_INTR_STAT_R_RD_REQ_BITS))) {
8991
return 0;
9092
}
@@ -123,6 +125,19 @@ int common_hal_i2ctarget_i2c_target_write_byte(i2ctarget_i2c_target_obj_t *self,
123125
}
124126
}
125127

128+
int common_hal_i2ctarget_i2c_target_is_stop(i2ctarget_i2c_target_obj_t *self) {
129+
// Interrupt bits must be cleared by software. Clear the STOP and
130+
// RESTART interrupt bits after an I2C transaction finishes.
131+
if (self->peripheral->hw->raw_intr_stat & I2C_IC_INTR_STAT_R_STOP_DET_BITS) {
132+
self->peripheral->hw->clr_stop_det;
133+
self->peripheral->hw->clr_restart_det;
134+
return 1;
135+
} else {
136+
return 0;
137+
}
138+
139+
}
140+
126141
void common_hal_i2ctarget_i2c_target_ack(i2ctarget_i2c_target_obj_t *self, bool ack) {
127142
return;
128143
}

ports/raspberrypi/common-hal/i2ctarget/I2CTarget.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ typedef struct {
2121
uint8_t scl_pin;
2222
uint8_t sda_pin;
2323
} i2ctarget_i2c_target_obj_t;
24+
25+
int common_hal_i2ctarget_i2c_target_is_stop(i2ctarget_i2c_target_obj_t *self);

0 commit comments

Comments
 (0)