Skip to content

Commit 1691421

Browse files
committed
Introdce BOOTLOADER_MAGIC_FORCE; add press-game-on-boot-to-abort.
1 parent 9fff1ef commit 1691421

File tree

2 files changed

+48
-47
lines changed

2 files changed

+48
-47
lines changed

Core/Src/main.c

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +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)
2126

22-
static void MX_RTC_Init(RTC_HandleTypeDef *hrtc);
23-
2427
static void __attribute__((naked)) start_app(void (* const pc)(void), uint32_t sp) {
2528
__asm(" \n\
2629
msr msp, r1 /* load r1 into MSP */\n\
@@ -29,7 +32,7 @@ static void __attribute__((naked)) start_app(void (* const pc)(void), uint32_t
2932
}
3033

3134
static inline void set_bootloader(uint32_t address){
32-
*BOOTLOADER_MAGIC_ADDRESS = BOOTLOADER_MAGIC;
35+
*BOOTLOADER_MAGIC_ADDRESS = BOOTLOADER_MAGIC_FORCE;
3336
*BOOTLOADER_JUMP_ADDRESS = (uint32_t *)address;
3437
}
3538

@@ -41,20 +44,46 @@ static inline void set_bootloader(uint32_t address){
4144
* So to run that app, set those values and execute a reset.
4245
*/
4346
void bootloader(){
44-
if(*BOOTLOADER_MAGIC_ADDRESS == BOOTLOADER_MAGIC) {
47+
if(*BOOTLOADER_MAGIC_ADDRESS == BOOTLOADER_MAGIC_FORCE) {
4548
*BOOTLOADER_MAGIC_ADDRESS = 0;
4649
uint32_t sp = (*BOOTLOADER_JUMP_ADDRESS)[0];
4750
uint32_t pc = (*BOOTLOADER_JUMP_ADDRESS)[1];
4851
start_app((void (* const)(void)) pc, (uint32_t) sp);
4952
}
53+
5054
HAL_Init();
51-
__HAL_RCC_CRS_CLK_ENABLE();
52-
HAL_PWR_EnableBkUpAccess(); // Enable backup domain access
53-
__HAL_RCC_RTC_ENABLE(); // Enable RTC clock
54-
RTC_HandleTypeDef hrtc;
55-
MX_RTC_Init(&hrtc);
56-
uint32_t rtc_backup_value = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR0);
57-
if(rtc_backup_value == BOOTLOADER_MAGIC){
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+
78+
if(*BOOTLOADER_MAGIC_ADDRESS == BOOTLOADER_MAGIC) {
79+
*BOOTLOADER_MAGIC_ADDRESS = 0;
80+
uint32_t sp = (*BOOTLOADER_JUMP_ADDRESS)[0];
81+
uint32_t pc = (*BOOTLOADER_JUMP_ADDRESS)[1];
82+
start_app((void (* const)(void)) pc, (uint32_t) sp);
83+
}
84+
85+
86+
if(HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR0) == BOOTLOADER_MAGIC){
5887
#if SD_BOOTLOADER
5988
uint32_t sp = *((uint32_t*)SD_BOOTLOADER_ADDRESS);
6089
uint32_t pc = *((uint32_t*)SD_BOOTLOADER_ADDRESS + 1);
@@ -122,16 +151,19 @@ gamepad_t read_buttons() {
122151
NVIC_SystemReset();
123152
}
124153
#endif
154+
125155
#if CLOCK_ONLY
126156
if(gamepad & GAMEPAD_GAME){
127157
#else
128158
if((gamepad & GAMEPAD_LEFT) && (gamepad & GAMEPAD_GAME)){
129159
#endif
160+
130161
#if SD_BOOTLOADER
131162
set_bootloader(SD_BOOTLOADER_ADDRESS);
132163
#else
133164
set_bootloader(BANK_2_ADDRESS);
134165
#endif
166+
135167
NVIC_SystemReset();
136168
}
137169

@@ -229,38 +261,6 @@ gnw_mode_t get_gnw_mode(){
229261
}
230262
#endif
231263

232-
/**
233-
* @brief RTC Initialization Function
234-
* @param None
235-
* @retval None
236-
*/
237-
static void MX_RTC_Init(RTC_HandleTypeDef *hrtc)
238-
{
239-
240-
/* USER CODE BEGIN RTC_Init 0 */
241-
242-
/* USER CODE END RTC_Init 0 */
243-
244-
/* USER CODE BEGIN RTC_Init 1 */
245-
246-
/* USER CODE END RTC_Init 1 */
247-
/** Initialize RTC Only
248-
*/
249-
hrtc->Instance = RTC;
250-
hrtc->Init.HourFormat = RTC_HOURFORMAT_24;
251-
hrtc->Init.AsynchPrediv = 127; // Recommended value from application note for LSE, the higher the value the better the accuracy and power consumption
252-
hrtc->Init.SynchPrediv = 255; // Recommended value from application note for LSE
253-
hrtc->Init.OutPut = RTC_OUTPUT_DISABLE;
254-
hrtc->Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
255-
hrtc->Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
256-
hrtc->Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
257-
HAL_RTC_Init(hrtc);
258-
259-
/* USER CODE BEGIN Check_RTC_BKUP */
260-
return; // Retain RTC values on boot
261-
}
262-
263-
264264
void NMI_Handler(void) {
265265
__BKPT(0);
266266
}

Makefile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ Core/Src/main.c \
2020
Core/Src/system_stm32h7xx.c \
2121
Core/lzma/LzmaDec.c \
2222
Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal.c \
23-
Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_rtc_ex.c \
24-
Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_rtc.c \
25-
Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_rcc_ex.c \
26-
Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_rcc.c \
2723
Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_cortex.c \
24+
Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_gpio.c \
2825
Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_pwr.c \
26+
Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_rcc.c \
27+
Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_rcc_ex.c \
28+
Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_rtc.c \
29+
Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_rtc_ex.c \
2930

3031

3132
# ASM sources

0 commit comments

Comments
 (0)