Skip to content

Commit

Permalink
stm32/pyb_i2c: Add pyb_i2c_deinit_all function.
Browse files Browse the repository at this point in the history
I2C objects can remain active after a soft-reboot because they are
statically allocated and lack a finalizer to collect and deinitialize them.
This commit adds a `pyb_i2c_deinit_all()` function for I2C, similar to
other peripherals such as UART, DAC, etc.

Signed-off-by: iabdalkader <[email protected]>
  • Loading branch information
iabdalkader authored and dpgeorge committed Dec 9, 2024
1 parent d42e39d commit 405aa69
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions ports/stm32/i2c.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ void i2c_init0(void);
int pyb_i2c_init(I2C_HandleTypeDef *i2c);
int pyb_i2c_init_freq(const pyb_i2c_obj_t *self, mp_int_t freq);
uint32_t pyb_i2c_get_baudrate(I2C_HandleTypeDef *i2c);
void pyb_i2c_deinit_all(void);
void i2c_ev_irq_handler(mp_uint_t i2c_id);
void i2c_er_irq_handler(mp_uint_t i2c_id);

Expand Down
9 changes: 9 additions & 0 deletions ports/stm32/pyb_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,15 @@ int pyb_i2c_init_freq(const pyb_i2c_obj_t *self, mp_int_t freq) {
return pyb_i2c_init(self->i2c);
}

void pyb_i2c_deinit_all(void) {
for (int i = 0; i < MP_ARRAY_SIZE(pyb_i2c_obj); i++) {
const pyb_i2c_obj_t *pyb_i2c = &pyb_i2c_obj[i];
if (pyb_i2c->i2c != NULL) {
i2c_deinit(pyb_i2c->i2c);
}
}
}

static void i2c_reset_after_error(I2C_HandleTypeDef *i2c) {
// wait for bus-busy flag to be cleared, with a timeout
for (int timeout = 50; timeout > 0; --timeout) {
Expand Down

0 comments on commit 405aa69

Please sign in to comment.