diff --git a/main.c b/main.c --- a/main.c +++ b/main.c @@ -65,7 +65,10 @@ SPI_HandleTypeDef hspi1; static __IO uint32_t TimingDelay; - +void deinit(void) +{ + HAL_DeInit(); +} volatile int i=0; int main(void) @@ -519,8 +522,17 @@ void machine() state = STATE_SETP; break; case 0: + { + ssd1306_clearscreen(); + ssd1306_DrawString("Entering Bootloader", 1, 0); + ssd1306_DrawString("(hopefully)", 2, 0); + HAL_Delay(1000); + *((unsigned long *)0x200017F0) = 0xDEADBEEF; // 6KB STM32F042 + NVIC_SystemReset(); + state = STATE_IDLE; - break; + } break; + default: state = STATE_PREHEAT_BREW; } diff --git a/startup_stm32f042x6.s b/startup_stm32f042x6.s --- a/startup_stm32f042x6.s +++ b/startup_stm32f042x6.s @@ -64,6 +64,17 @@ defined in linker script */ .weak Reset_Handler .type Reset_Handler, %function Reset_Handler: + + /* Bootloader jumping */ + ldr r0, =0x200017F0 /* address of magic token, is addr within memory range? */ + ldr r1, =0xDEADBEEF /* magical beef token */ + ldr r2, [r0, #0] /* load data from magic address */ + str r0, [r0, #0] /* zero data at magic address so we don't bootloop */ + cmp r2, r1 /* compare data at magic address to magic token */ + beq Reboot_Loader /* jump to bootloader if token match */ + /* End bootloader jumping */ + + ldr r0, =_estack mov sp, r0 /* set stack pointer */ @@ -71,6 +82,19 @@ Reset_Handler: movs r1, #0 b LoopCopyDataInit +/* Boot into bootloader */ +Reboot_Loader: + ldr r0, =0x1FFFF6A6 /* Address of bootloader on f042 from CD00167594 pg 15 table 3 */ + /* This replaces ldr sp, [r0, #0] which doesn't work on m0 */ + // Set stack pointer + ldr r1, [r0, #0] + mov sp, r1 + + // Branch to bootloader + ldr r0, [r0, #4] + bx r0 + + CopyDataInit: ldr r3, =_sidata ldr r3, [r3, r1]