From 405aa69887558d561a993d5e9aa47b2ec7e76d9a Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Mon, 2 Dec 2024 08:09:47 +0100 Subject: [PATCH] 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 --- ports/stm32/i2c.h | 1 + ports/stm32/pyb_i2c.c | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/ports/stm32/i2c.h b/ports/stm32/i2c.h index 04a7e928bdbdf..a48076842cbbf 100644 --- a/ports/stm32/i2c.h +++ b/ports/stm32/i2c.h @@ -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); diff --git a/ports/stm32/pyb_i2c.c b/ports/stm32/pyb_i2c.c index 0529d3bd56e40..7e1489010d097 100644 --- a/ports/stm32/pyb_i2c.c +++ b/ports/stm32/pyb_i2c.c @@ -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) {