Skip to content

Commit 017a516

Browse files
committed
Fix I2C bug in ports/analog/busio
- Add clear flags + Shutdown/Init to I2C constructor - Explicitly set frequency - Add failsafe in probe function if MST mode disabled (can happen in some error conditions) Signed-off-by: Brandon-Hurst <[email protected]>
1 parent ed92610 commit 017a516

File tree

1 file changed

+16
-2
lines changed
  • ports/analog/common-hal/busio

1 file changed

+16
-2
lines changed

ports/analog/common-hal/busio/I2C.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
7676
common_hal_mcu_pin_claim(self->sda);
7777
common_hal_mcu_pin_claim(self->scl);
7878

79+
// Clear all flags
80+
MXC_I2C_ClearFlags(self->i2c_regs, 0xFFFFFF, 0xFFFFFF);
81+
82+
// Init as master, no slave address
83+
MXC_I2C_Shutdown(self->i2c_regs);
84+
MXC_I2C_Init(self->i2c_regs, 1, 0);
85+
86+
// Set frequency arg (CircuitPython shared-bindings validate)
87+
MXC_I2C_SetFrequency(self->i2c_regs, frequency);
88+
7989
// Indicate to this module that the I2C is active
8090
i2c_active |= (1 << self->i2c_id);
8191

@@ -116,6 +126,11 @@ bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr) {
116126
uint32_t int_fl0;
117127
bool ret = 0;
118128

129+
// If not in Master mode, error out (can happen in some error conditions)
130+
if (!(self->i2c_regs->ctrl & MXC_F_I2C_CTRL_MST_MODE)) {
131+
return false;
132+
}
133+
119134
// Clear FIFOs & all interrupt flags
120135
MXC_I2C_ClearRXFIFO(self->i2c_regs);
121136
MXC_I2C_ClearTXFIFO(self->i2c_regs);
@@ -175,7 +190,6 @@ void common_hal_busio_i2c_unlock(busio_i2c_obj_t *self) {
175190
}
176191

177192
// Write data to the device selected by address
178-
// todo test
179193
uint8_t common_hal_busio_i2c_write(busio_i2c_obj_t *self, uint16_t addr,
180194
const uint8_t *data, size_t len) {
181195

@@ -221,7 +235,7 @@ uint8_t common_hal_busio_i2c_read(busio_i2c_obj_t *self,
221235
return 0;
222236
}
223237

224-
// Write the bytes from out_data to the device selected by address,
238+
// Write the bytes from out_data to the device selected by address
225239
uint8_t common_hal_busio_i2c_write_read(busio_i2c_obj_t *self, uint16_t addr,
226240
uint8_t *out_data, size_t out_len,
227241
uint8_t *in_data, size_t in_len) {

0 commit comments

Comments
 (0)