From d42e39d87d37f20d74456222e603d0b6b409a556 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Mon, 2 Dec 2024 08:09:25 +0100 Subject: [PATCH] stm32/spi: Add spi_deinit_all function. SPI 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 `spi_deinit_all()` functions for SPI, similar to other peripherals such as UART, DAC, etc. Signed-off-by: iabdalkader --- ports/stm32/spi.c | 9 +++++++++ ports/stm32/spi.h | 1 + 2 files changed, 10 insertions(+) diff --git a/ports/stm32/spi.c b/ports/stm32/spi.c index 607b3fe685..aa459119c1 100644 --- a/ports/stm32/spi.c +++ b/ports/stm32/spi.c @@ -545,6 +545,15 @@ void spi_deinit(const spi_t *spi_obj) { } } +void spi_deinit_all(void) { + for (int i = 0; i < MP_ARRAY_SIZE(spi_obj); i++) { + const spi_t *spi = &spi_obj[i]; + if (spi->spi != NULL) { + spi_deinit(spi); + } + } +} + static HAL_StatusTypeDef spi_wait_dma_finished(const spi_t *spi, uint32_t t_start, uint32_t timeout) { volatile HAL_SPI_StateTypeDef *state = &spi->spi->State; for (;;) { diff --git a/ports/stm32/spi.h b/ports/stm32/spi.h index a8bc9d2cfd..204038a92f 100644 --- a/ports/stm32/spi.h +++ b/ports/stm32/spi.h @@ -67,6 +67,7 @@ extern const mp_obj_type_t pyb_spi_type; void spi_init0(void); int spi_init(const spi_t *spi, bool enable_nss_pin); void spi_deinit(const spi_t *spi_obj); +void spi_deinit_all(void); int spi_find_index(mp_obj_t id); void spi_set_params(const spi_t *spi_obj, uint32_t prescale, int32_t baudrate, int32_t polarity, int32_t phase, int32_t bits, int32_t firstbit);