From 5ddd1488bd5ac7a41d76e68b84ff858d7fa0aad7 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 2 Feb 2018 19:01:11 +1100 Subject: [PATCH] stm32/spi: Allow SPI peripheral state to persist across a soft reset. The SPI sub-system is independent from the uPy state (eg the heap) and so can safely persist across a soft reset. And this is actually necessary for drivers that rely on SPI and that also need to persist across soft reset (eg external SPI flash memory). --- ports/stm32/main.c | 2 +- ports/stm32/spi.c | 9 ++------- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/ports/stm32/main.c b/ports/stm32/main.c index 60615736d3..7a530ff83a 100644 --- a/ports/stm32/main.c +++ b/ports/stm32/main.c @@ -457,6 +457,7 @@ int main(void) { #if MICROPY_HW_HAS_SWITCH switch_init0(); #endif + spi_init0(); #if defined(USE_DEVICE_MODE) // default to internal flash being the usb medium @@ -556,7 +557,6 @@ soft_reset: i2c_init0(); #endif - spi_init0(); pyb_usb_init0(); // Initialise the local flash filesystem. diff --git a/ports/stm32/spi.c b/ports/stm32/spi.c index 2b743bdfae..31157d8825 100644 --- a/ports/stm32/spi.c +++ b/ports/stm32/spi.c @@ -135,29 +135,24 @@ STATIC const pyb_spi_obj_t pyb_spi_obj[] = { }; void spi_init0(void) { - // reset the SPI handles + // Initialise the SPI handles. + // The structs live on the BSS so all other fields will be zero after a reset. #if defined(MICROPY_HW_SPI1_SCK) - memset(&SPIHandle1, 0, sizeof(SPI_HandleTypeDef)); SPIHandle1.Instance = SPI1; #endif #if defined(MICROPY_HW_SPI2_SCK) - memset(&SPIHandle2, 0, sizeof(SPI_HandleTypeDef)); SPIHandle2.Instance = SPI2; #endif #if defined(MICROPY_HW_SPI3_SCK) - memset(&SPIHandle3, 0, sizeof(SPI_HandleTypeDef)); SPIHandle3.Instance = SPI3; #endif #if defined(MICROPY_HW_SPI4_SCK) - memset(&SPIHandle4, 0, sizeof(SPI_HandleTypeDef)); SPIHandle4.Instance = SPI4; #endif #if defined(MICROPY_HW_SPI5_SCK) - memset(&SPIHandle5, 0, sizeof(SPI_HandleTypeDef)); SPIHandle5.Instance = SPI5; #endif #if defined(MICROPY_HW_SPI6_SCK) - memset(&SPIHandle6, 0, sizeof(SPI_HandleTypeDef)); SPIHandle6.Instance = SPI6; #endif }