stm32/pyb_i2c: Add pyb_i2c_deinit_all function.

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 <i.abdalkader@gmail.com>
This commit is contained in:
iabdalkader
2024-12-02 08:09:47 +01:00
committed by Damien George
parent d42e39d87d
commit 405aa69887
2 changed files with 10 additions and 0 deletions

View File

@@ -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);

View File

@@ -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) {