From b490e6917df7087ba0064d1973679c2b4ea7fda0 Mon Sep 17 00:00:00 2001 From: R Date: Sun, 14 Apr 2024 18:10:59 +0100 Subject: [PATCH] xxx try to fix ram boot bug ram boot doesn't seem to work if there's no flash application for some reason --- bootloader.c | 9 +++++++++ startup.S | 13 ++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/bootloader.c b/bootloader.c index 7d2438e..9ac3a8a 100644 --- a/bootloader.c +++ b/bootloader.c @@ -188,6 +188,8 @@ extern uint32_t UF2_GOT_BLOCKS[36]; #define R16_BKP_DATAR10 (*(volatile uint32_t*)0x40006C28) +#define R32_PWR_CTLR (*(volatile uint32_t*)0x40007000) + #define R32_RCC_CTLR (*(volatile uint32_t*)0x40021000) #define R32_RCC_CFGR0 (*(volatile uint32_t*)0x40021004) #define R32_RCC_APB2PCENR (*(volatile uint32_t*)0x40021018) @@ -348,6 +350,11 @@ static void make_msc_csw(uint32_t dCSWTag, uint32_t error) { } __attribute__((naked)) int main(void) { + // Make sure this stuff is enabled + R32_RCC_APB1PCENR |= (1 << 27) | (1 << 28); + R32_PWR_CTLR |= 1 << 8; + R16_BKP_DATAR10 = 0; + // PLL setup: system clock 96 MHz R32_EXTEN_CTR |= (1 << 4); // sneaky div2 R32_RCC_CFGR0 = (R32_RCC_CFGR0 & ~0xff0000) | (0b01101000 << 16); @@ -855,6 +862,8 @@ __attribute__((naked)) int main(void) { R32_EXTEN_CTR &= ~(1 << 1); if (ADDRESS_HI >> 8 == 0x20) { // ram boot + R32_RCC_CFGR0 = (R32_RCC_CFGR0 & ~0b11) | 0b00; + while ((R32_RCC_CFGR0 & 0b1100) != 0b0000) {} ((void (*)())0x20000000)(); } else { // wtf why does this work and the other stuff doesn't? diff --git a/startup.S b/startup.S index be0bf86..e3b7721 100644 --- a/startup.S +++ b/startup.S @@ -19,6 +19,12 @@ .global _start _start: + // Weird things seem to happen if you fall off the end of flash + la a0, _bootloader_limit + lw a0, (a0) + la a1, 0xe339e339 + beq a0, a1, _enter_bootloader + // Use Adafruit(?)-inspired boot entry, namely "double-reset in short succession to enter bootloader" // 1. If the reset wasn't triggered by the nRST pin, go to application // 2. On nRST, if not first reset, go to bootloader @@ -45,7 +51,7 @@ _start: la a3, R16_BKP_DATAR10 lhu a1, (a3) la a2, BOOT_MAGIC_APP_IMMEDIATELY - beq a1, a2, _enter_application_code_magic + beq a1, a2, _enter_application_code la a2, BOOT_MAGIC_BOOTLOADER beq a1, a2, _enter_bootloader @@ -68,9 +74,8 @@ _start: sw zero, (STK_SR-SYSTICK_BASE)(a0) // If the delay expires, boot the application after all -_enter_application_code_magic: - sh zero, (a3) _enter_application_code: + sh zero, (a3) // Clear all of these enables that we had set la a0, R32_PWR_CTLR lw a1, (a0) @@ -85,8 +90,6 @@ _enter_application_code: j _bootloader_limit _enter_bootloader: - sh zero, (a3) - // Set up global pointer .option push .option norelax