Skip to content

Commit 5593d23

Browse files
authored
Merge pull request #6919 from flom84/stm_dfu_mode
Software DFU mode implementation for STM32F4 MCU.
2 parents 1091d51 + 745d83a commit 5593d23

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

ports/stm/common-hal/microcontroller/__init__.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#include "shared-bindings/microcontroller/__init__.h"
3737
#include "shared-bindings/microcontroller/Pin.h"
3838
#include "shared-bindings/microcontroller/Processor.h"
39-
39+
#include "supervisor/port.h"
4040
#include "supervisor/filesystem.h"
4141
#include "supervisor/shared/safe_mode.h"
4242

@@ -73,15 +73,25 @@ void common_hal_mcu_enable_interrupts(void) {
7373
__enable_irq();
7474
}
7575

76+
static bool next_reset_to_bootloader = false;
77+
7678
void common_hal_mcu_on_next_reset(mcu_runmode_t runmode) {
7779
if (runmode == RUNMODE_SAFE_MODE) {
7880
safe_mode_on_next_reset(PROGRAMMATIC_SAFE_MODE);
7981
}
82+
if (runmode == RUNMODE_BOOTLOADER) {
83+
next_reset_to_bootloader = true;
84+
}
8085
}
8186

8287
void common_hal_mcu_reset(void) {
8388
filesystem_flush(); // TODO: implement as part of flash improvements
84-
NVIC_SystemReset();
89+
90+
if (next_reset_to_bootloader) {
91+
reset_to_bootloader();
92+
} else {
93+
NVIC_SystemReset();
94+
}
8595
}
8696

8797
// The singleton microcontroller.Processor object, bound to microcontroller.cpu

ports/stm/supervisor/port.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,39 @@ void reset_port(void) {
276276
}
277277

278278
void reset_to_bootloader(void) {
279+
280+
/*
281+
From STM AN2606:
282+
Before jumping to bootloader user must:
283+
• Disable all peripheral clocks
284+
• Disable used PLL
285+
• Disable interrupts
286+
• Clear pending interrupts
287+
System memory boot mode can be exited by getting out from bootloader activation
288+
condition and generating hardware reset or using Go command to execute user code
289+
*/
290+
HAL_RCC_DeInit();
291+
HAL_DeInit();
292+
293+
// disable all interupts
294+
__disable_irq();
295+
296+
// Clear all pending interrupts
297+
for (uint8_t i = 0; i < (sizeof(NVIC->ICPR) / NVIC->ICPR[0]); ++i) {
298+
NVIC->ICPR[i] = 0xFFFFFFFF;
299+
}
300+
// information about jump addresses has been taken from STM AN2606.
301+
#if defined(STM32F4)
302+
__set_MSP(*((uint32_t *)0x1FFF0000));
303+
((void (*)(void)) * ((uint32_t *)0x1FFF0004))();
304+
#else
305+
// DFU mode for STM32 variant note implemented.
279306
NVIC_SystemReset();
307+
#endif
308+
309+
while (true) {
310+
asm ("nop;");
311+
}
280312
}
281313

282314
void reset_cpu(void) {

0 commit comments

Comments
 (0)