You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
the builder patch is now auto-applied using a fork of the Arduino_Core_STM32 github.
TOTALB_funcs.h is similar, but functions are templated (sothat you can jump back to the bootloader if you really want)
Copy file name to clipboardExpand all lines: TOTALB_funcs.h
+33-10
Original file line number
Diff line number
Diff line change
@@ -42,9 +42,9 @@ LL_RCC_ClearResetFlags(); clears reset flags (Note: try to find, see if weak res
42
42
#ifndef TOTALB_FUNCS_h // include only once
43
43
#defineTOTALB_FUNCS_h
44
44
45
-
#ifndef TOTALB_PROGRAM_START
46
-
#error("TOTALB_funcs.h error: TOTALB_PROGRAM_START not defined! Only include this header if this is bootloader code")
47
-
#endif
45
+
//#ifndef TOTALB_PROGRAM_START
46
+
// #error("TOTALB_funcs.h error: TOTALB_PROGRAM_START not defined! Only include this header if this is bootloader code")
47
+
//#endif
48
48
49
49
#include<Arduino.h>// used for Serial (for debugging)
50
50
@@ -55,6 +55,7 @@ namespace TOTALB {
55
55
//// there is some variant-specific code below:
56
56
#ifdef ARDUINO_P_NUCLEO_WB55RG // Note: alternative defines: STM32WB55xx
57
57
#include<lock_resource.h>// for undo_SystemClock_Config()
58
+
// #include <shci.h> // from STM32duinoBLE library, for SHCI_C2_Reinit() (to reset CPU2 (BLE) before jump). Commented out because i don't want to include STM32duinoBLE
58
59
59
60
/// @brief undoes what SystemClock_Config() did (see variant_P_NUCLEO_WB55RG.cpp) to bring clocks to a state similar to just-after-reset
60
61
/// @param leave_LSE_same leaves LSE as it is (NOTE: might cause niche issues, BUT it does save ~124ms of re-initialization time after jump)
@@ -136,24 +137,33 @@ namespace TOTALB {
136
137
137
138
/// @brief check whether jumping to TOTALB_PROGRAM_START is (likely to be) safe
138
139
/// @return true if it looks safe enough, false if you should ABSOLUTELY NOT jump there
139
-
boolcheckJumpLocation() {
140
+
template<uint32_t __PROGRAM_START>
141
+
bool_checkJumpLocation() {
140
142
//// first and foremose, check if the flash
141
-
constuint32_t* userAppStartPtr = (uint32_t*)(FLASH_BASE + TOTALB_PROGRAM_START); // (a pointer to) the place in flash where the actual app starts
143
+
constuint32_t* userAppStartPtr = (uint32_t*)(FLASH_BASE + __PROGRAM_START); // (a pointer to) the place in flash where the actual app starts
142
144
constuint32_t resetHandlerAddress = *(userAppStartPtr + 1); // find the reset handler function (2nd 32bit value) in the app's vector table (Note +1=+4_bytes=+(sizeof(ptr)))
143
145
uint32_t newStackAddress = *userAppStartPtr; // read new stack pointer from flash (1st value of program) (usually it's 0x20030000)
/// @brief (semi-private template version (not recommended)) de-initialize clocks & peripherals and attempt to start program at <__PROGRAM_START>
154
162
/// @param leave_LSE_same leaves LSE as it is (NOTE: might cause niche issues, BUT it does save ~124ms of re-initialization time after jump)
155
-
voidjumpToProgram(bool leave_LSE_same=false) {
163
+
template<uint32_t __PROGRAM_START>
164
+
void_jumpTo(bool leave_LSE_same=false) {
156
165
//// before jumping to the new app's reset handler, the system needs to be brought to reset-like state
166
+
/* NOTE: call SHCI_C2_Reinit() from the STM32duinoBLE library (or Cube FW?) for resetting CPU2 (in case BLE was used before jump)*/
157
167
//// the most important factor is resetting the clock & oscillator configs (without this step, it would crash so hard that it takes JTAG debuggers down with it)
+ DWT is for debugging (JTAG?), so it can be left on
173
183
+ IPclock can be ignored (left enabled?), i think(?)
174
184
*/
175
-
constuint32_t* userAppStartPtr = (uint32_t*)(FLASH_BASE + TOTALB_PROGRAM_START); // (a pointer to) the place in flash where the actual app starts
185
+
constuint32_t* userAppStartPtr = (uint32_t*)(FLASH_BASE + __PROGRAM_START); // (a pointer to) the place in flash where the actual app starts
176
186
uint32_t newStackAddress = *userAppStartPtr; // read new stack pointer from flash (1st value of program) (usually it's 0x20030000)
177
187
typedefvoid (*fct_t)(void);
178
188
fct_t app_reset_handler = (fct_t) *(userAppStartPtr + 1); // find the reset handler function (2nd 32bit value) in the app's vector table (Note +1=+4_bytes=+(sizeof(ptr)))
179
189
//// you can do some last-minute reassuring checks here, like: (Note: the clocks and peripherals have been disabled though, so debug printing is not easy)
180
-
if(!checkJumpLocation()) { return; } // the stack pointer should not look like empty flash
190
+
//if(!checkJumpLocation()) { return; } // the stack pointer should not look like empty flash
181
191
//// now jump to the app:
182
192
SCB->VTOR = (volatileuint32_t) userAppStartPtr; // set the Vector table pointer to the app's one
183
193
__set_MSP(newStackAddress); // switch stack address pointer to the app's one (1st 32bit value in the vector table) (which is the stack pointer address) (probably the same as current)
0 commit comments