more reliable but still wtf

This commit is contained in:
R
2024-04-13 23:56:36 +01:00
parent a609c3ddfb
commit 4e1d1ea4a3
2 changed files with 16 additions and 6 deletions
+13 -5
View File
@@ -196,6 +196,10 @@ const uint8_t ROOT_DIR[32 * 3] __attribute__((aligned(2))) = {
#define R32_EXTEN_CTR (*(volatile uint32_t*)0x40023800) #define R32_EXTEN_CTR (*(volatile uint32_t*)0x40023800)
#define STK_CTLR (*(volatile uint32_t*)0xE000F000)
#define STK_SR (*(volatile uint32_t*)0xE000F004)
#define STK_CMPLR (*(volatile uint32_t*)0xE000F010)
#define PFIC_CFGR (*(volatile uint32_t*)0xE000E048) #define PFIC_CFGR (*(volatile uint32_t*)0xE000E048)
// state[7:0] = addr // state[7:0] = addr
@@ -286,9 +290,8 @@ __attribute__((always_inline)) static inline void synthesize_block(uint32_t bloc
} }
__attribute__((naked)) int main(void) { __attribute__((naked)) int main(void) {
// clear all reset causes // Don't reenter bootloader again
R16_BKP_DATAR10 = 0; R16_BKP_DATAR10 = 0;
R32_RCC_RSTSCKR = 1 << 24;
// PLL setup: system clock 96 MHz // PLL setup: system clock 96 MHz
R32_EXTEN_CTR |= (1 << 4); // sneaky div2 R32_EXTEN_CTR |= (1 << 4); // sneaky div2
@@ -810,11 +813,16 @@ error_csw:
break; break;
case STATE_SENT_CSW_REBOOT: case STATE_SENT_CSW_REBOOT:
// todo reboot into ram // todo reboot into ram
for (int i = 0; i < 1000000; i++) STK_CMPLR = (50 /* ms */ * 12000 /* assume 96 MHz system clock, div8 */);
asm volatile(""); STK_CTLR = 0b111000;
STK_CTLR = 0b111001;
while (!(STK_SR & 1)) {}
STK_CTLR = 0;
STK_SR = 0;
R16_USBD_CNTR = 0b11; R16_USBD_CNTR = 0b11;
R32_EXTEN_CTR &= ~(1 << 1); R32_EXTEN_CTR &= ~(1 << 1);
asm volatile("csrw mepc, zero\n"); // wtf why does this work and the other stuff doesn't?
R16_BKP_DATAR10 = 0x4170;
PFIC_CFGR = 0xbeef0080; PFIC_CFGR = 0xbeef0080;
while (1) { asm volatile(""); } while (1) { asm volatile(""); }
break; break;
+3 -1
View File
@@ -45,7 +45,7 @@ _start:
la a3, R16_BKP_DATAR10 la a3, R16_BKP_DATAR10
lhu a1, (a3) lhu a1, (a3)
la a2, BOOT_MAGIC_APP_IMMEDIATELY la a2, BOOT_MAGIC_APP_IMMEDIATELY
beq a1, a2, _enter_application_code beq a1, a2, _enter_application_code_magic
la a2, BOOT_MAGIC_BOOTLOADER la a2, BOOT_MAGIC_BOOTLOADER
beq a1, a2, _enter_bootloader beq a1, a2, _enter_bootloader
@@ -69,6 +69,8 @@ _start:
sh zero, (a3) sh zero, (a3)
// If the delay expires, boot the application after all // If the delay expires, boot the application after all
_enter_application_code_magic:
sh zero, (a3)
_enter_application_code: _enter_application_code:
// Clear all of these enables that we had set // Clear all of these enables that we had set
la a0, R32_PWR_CTLR la a0, R32_PWR_CTLR