Skip to content

Commit 1bd588c

Browse files
authored
Merge pull request #73 from BrianPugh/rtc-backup
directly dual boot if 0x544F4F42 is in RTC_BKP_DR0
2 parents f90daef + 1691421 commit 1bd588c

File tree

4 files changed

+501
-3
lines changed

4 files changed

+501
-3
lines changed

Core/Inc/stm32h7xx_hal_conf.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
#define HAL_LTDC_MODULE_ENABLED
6767
/* #define HAL_QSPI_MODULE_ENABLED */
6868
/* #define HAL_RNG_MODULE_ENABLED */
69-
/* #define HAL_RTC_MODULE_ENABLED */
69+
#define HAL_RTC_MODULE_ENABLED
7070
#define HAL_SAI_MODULE_ENABLED
7171
/* #define HAL_SD_MODULE_ENABLED */
7272
/* #define HAL_MMC_MODULE_ENABLED */

Core/Src/main.c

+54-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@
1515
#define BANK_2_ADDRESS 0x08100000
1616
#define SD_BOOTLOADER_ADDRESS 0x08032000
1717

18+
// Other software (like retro-go) should set this value
1819
#define BOOTLOADER_MAGIC 0x544F4F42 // "BOOT"
20+
21+
// Intended for internal-use only; bypasses other checks
22+
#define BOOTLOADER_MAGIC_FORCE 0x45435246 // "FRCE"
23+
1924
#define BOOTLOADER_MAGIC_ADDRESS ((uint32_t *)0x2001FFF8)
2025
#define BOOTLOADER_JUMP_ADDRESS ((uint32_t **)0x2001FFFC)
26+
2127
static void __attribute__((naked)) start_app(void (* const pc)(void), uint32_t sp) {
2228
__asm(" \n\
2329
msr msp, r1 /* load r1 into MSP */\n\
@@ -26,7 +32,7 @@ static void __attribute__((naked)) start_app(void (* const pc)(void), uint32_t
2632
}
2733

2834
static inline void set_bootloader(uint32_t address){
29-
*BOOTLOADER_MAGIC_ADDRESS = BOOTLOADER_MAGIC;
35+
*BOOTLOADER_MAGIC_ADDRESS = BOOTLOADER_MAGIC_FORCE;
3036
*BOOTLOADER_JUMP_ADDRESS = (uint32_t *)address;
3137
}
3238

@@ -38,13 +44,57 @@ static inline void set_bootloader(uint32_t address){
3844
* So to run that app, set those values and execute a reset.
3945
*/
4046
void bootloader(){
47+
if(*BOOTLOADER_MAGIC_ADDRESS == BOOTLOADER_MAGIC_FORCE) {
48+
*BOOTLOADER_MAGIC_ADDRESS = 0;
49+
uint32_t sp = (*BOOTLOADER_JUMP_ADDRESS)[0];
50+
uint32_t pc = (*BOOTLOADER_JUMP_ADDRESS)[1];
51+
start_app((void (* const)(void)) pc, (uint32_t) sp);
52+
}
53+
54+
HAL_Init();
55+
56+
HAL_PWR_EnableBkUpAccess();
57+
__HAL_RCC_RTC_ENABLE();
58+
__HAL_RCC_GPIOC_CLK_ENABLE();
59+
60+
RTC_HandleTypeDef hrtc = {0};
61+
hrtc.Instance = RTC;
62+
// Note: Don't need to call HAL_RTC_Init() since we're just reading backup register
63+
64+
GPIO_InitTypeDef GPIO_InitStruct = {0};
65+
GPIO_InitStruct.Pin = BTN_GAME_Pin;
66+
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
67+
GPIO_InitStruct.Pull = GPIO_PULLUP; // Button connects to GND.
68+
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
69+
70+
HAL_GPIO_Init(BTN_GAME_GPIO_Port, &GPIO_InitStruct);
71+
72+
if(HAL_GPIO_ReadPin(BTN_GAME_GPIO_Port, BTN_GAME_Pin) == GPIO_PIN_RESET) {
73+
// If GAME is pressed: reset all triggers that might cause us to dual-boot.
74+
*BOOTLOADER_MAGIC_ADDRESS = 0;
75+
HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR0, 0);
76+
}
77+
4178
if(*BOOTLOADER_MAGIC_ADDRESS == BOOTLOADER_MAGIC) {
4279
*BOOTLOADER_MAGIC_ADDRESS = 0;
4380
uint32_t sp = (*BOOTLOADER_JUMP_ADDRESS)[0];
4481
uint32_t pc = (*BOOTLOADER_JUMP_ADDRESS)[1];
4582
start_app((void (* const)(void)) pc, (uint32_t) sp);
4683
}
4784

85+
86+
if(HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR0) == BOOTLOADER_MAGIC){
87+
#if SD_BOOTLOADER
88+
uint32_t sp = *((uint32_t*)SD_BOOTLOADER_ADDRESS);
89+
uint32_t pc = *((uint32_t*)SD_BOOTLOADER_ADDRESS + 1);
90+
#else
91+
uint32_t sp = *((uint32_t*)BANK_2_ADDRESS);
92+
uint32_t pc = *((uint32_t*)BANK_2_ADDRESS + 1);
93+
#endif
94+
95+
start_app((void (* const)(void)) pc, (uint32_t) sp);
96+
}
97+
4898
start_app(stock_Reset_Handler, *(uint32_t *) MSP_ADDRESS);
4999
while(1);
50100
}
@@ -101,16 +151,19 @@ gamepad_t read_buttons() {
101151
NVIC_SystemReset();
102152
}
103153
#endif
154+
104155
#if CLOCK_ONLY
105156
if(gamepad & GAMEPAD_GAME){
106157
#else
107158
if((gamepad & GAMEPAD_LEFT) && (gamepad & GAMEPAD_GAME)){
108159
#endif
160+
109161
#if SD_BOOTLOADER
110162
set_bootloader(SD_BOOTLOADER_ADDRESS);
111163
#else
112164
set_bootloader(BANK_2_ADDRESS);
113165
#endif
166+
114167
NVIC_SystemReset();
115168
}
116169

@@ -208,7 +261,6 @@ gnw_mode_t get_gnw_mode(){
208261
}
209262
#endif
210263

211-
212264
void NMI_Handler(void) {
213265
__BKPT(0);
214266
}

0 commit comments

Comments
 (0)