From 3609c29b270aabeae854c6d60073ade802b19775 Mon Sep 17 00:00:00 2001 From: flom84 Date: Sun, 25 Sep 2022 19:49:46 +0200 Subject: [PATCH 1/2] Update STM DFU mode software implementation. - fix for sizeof array calculation. - follow ARM recommendation to turn off interruptions with NVIC. --- ports/stm/supervisor/port.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/ports/stm/supervisor/port.c b/ports/stm/supervisor/port.c index 7f31f3c7e5aad..b7ff229d36dd3 100644 --- a/ports/stm/supervisor/port.c +++ b/ports/stm/supervisor/port.c @@ -290,13 +290,22 @@ condition and generating hardware reset or using Go command to execute user code HAL_RCC_DeInit(); HAL_DeInit(); - // disable all interupts - __disable_irq(); + // Disable all pending interrupts using NVIC + for (uint8_t i = 0; i < (sizeof(NVIC->ICER) / sizeof(NVIC->ICER[0])); ++i) { + NVIC->ICER[i] = 0xFFFFFFFF; + } - // Clear all pending interrupts - for (uint8_t i = 0; i < (sizeof(NVIC->ICPR) / NVIC->ICPR[0]); ++i) { + // if it is necessary to ensure an interrupt will not be triggered after disabling it in the NVIC, + // add a DSB instruction and then an ISB instruction. (ARM Cortex™-M Programming Guide to + // Memory Barrier Instructions, 4.6 Disabling Interrupts using NVIC) + __DSB(); + __ISB(); + + // Clear all pending interrupts using NVIC + for (uint8_t i = 0; i < (sizeof(NVIC->ICPR) / sizeof(NVIC->ICPR[0])); ++i) { NVIC->ICPR[i] = 0xFFFFFFFF; } + // information about jump addresses has been taken from STM AN2606. #if defined(STM32F4) __set_MSP(*((uint32_t *)0x1FFF0000)); From 7af8a23ddfeeb3a1363251bc40fb34b312a6536f Mon Sep 17 00:00:00 2001 From: flom84 Date: Thu, 29 Sep 2022 22:10:21 +0200 Subject: [PATCH 2/2] Use macro for computing size of array. --- ports/stm/supervisor/port.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/stm/supervisor/port.c b/ports/stm/supervisor/port.c index b7ff229d36dd3..896d58b013bca 100644 --- a/ports/stm/supervisor/port.c +++ b/ports/stm/supervisor/port.c @@ -291,7 +291,7 @@ condition and generating hardware reset or using Go command to execute user code HAL_DeInit(); // Disable all pending interrupts using NVIC - for (uint8_t i = 0; i < (sizeof(NVIC->ICER) / sizeof(NVIC->ICER[0])); ++i) { + for (uint8_t i = 0; i < MP_ARRAY_SIZE(NVIC->ICER); ++i) { NVIC->ICER[i] = 0xFFFFFFFF; } @@ -302,7 +302,7 @@ condition and generating hardware reset or using Go command to execute user code __ISB(); // Clear all pending interrupts using NVIC - for (uint8_t i = 0; i < (sizeof(NVIC->ICPR) / sizeof(NVIC->ICPR[0])); ++i) { + for (uint8_t i = 0; i < MP_ARRAY_SIZE(NVIC->ICPR); ++i) { NVIC->ICPR[i] = 0xFFFFFFFF; }