File tree 2 files changed +44
-2
lines changed
common-hal/microcontroller 2 files changed +44
-2
lines changed Original file line number Diff line number Diff line change 36
36
#include "shared-bindings/microcontroller/__init__.h"
37
37
#include "shared-bindings/microcontroller/Pin.h"
38
38
#include "shared-bindings/microcontroller/Processor.h"
39
-
39
+ #include "supervisor/port.h"
40
40
#include "supervisor/filesystem.h"
41
41
#include "supervisor/shared/safe_mode.h"
42
42
@@ -73,15 +73,25 @@ void common_hal_mcu_enable_interrupts(void) {
73
73
__enable_irq ();
74
74
}
75
75
76
+ static bool next_reset_to_bootloader = false;
77
+
76
78
void common_hal_mcu_on_next_reset (mcu_runmode_t runmode ) {
77
79
if (runmode == RUNMODE_SAFE_MODE ) {
78
80
safe_mode_on_next_reset (PROGRAMMATIC_SAFE_MODE );
79
81
}
82
+ if (runmode == RUNMODE_BOOTLOADER ) {
83
+ next_reset_to_bootloader = true;
84
+ }
80
85
}
81
86
82
87
void common_hal_mcu_reset (void ) {
83
88
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
+ }
85
95
}
86
96
87
97
// The singleton microcontroller.Processor object, bound to microcontroller.cpu
Original file line number Diff line number Diff line change @@ -276,7 +276,39 @@ void reset_port(void) {
276
276
}
277
277
278
278
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.
279
306
NVIC_SystemReset ();
307
+ #endif
308
+
309
+ while (true) {
310
+ asm ("nop;" );
311
+ }
280
312
}
281
313
282
314
void reset_cpu (void ) {
You can’t perform that action at this time.
0 commit comments