diff --git a/src/generic/armcm_boot.c b/src/generic/armcm_boot.c index 87fc821..4f1b8a7 100644 --- a/src/generic/armcm_boot.c +++ b/src/generic/armcm_boot.c @@ -7,17 +7,13 @@ #include "armcm_boot.h" // DECL_ARMCM_IRQ #include "autoconf.h" // CONFIG_MCU #include "board/internal.h" // SysTick - +#include "board/irq.h" // irq_disable // Symbols created by armcm_link.lds.S linker script extern uint32_t _data_start, _data_end, _data_flash; extern uint32_t _bss_start, _bss_end, _stack_start; extern uint32_t _stack_end; -/**************************************************************** - * Basic interrupt handlers - ****************************************************************/ - uint64_t get_bootup_code(void) { @@ -32,9 +28,34 @@ set_bootup_code(uint64_t code) *req_code = code; } +#define REQUEST_START_APP 0x7b06ec45a9a8243d + +void +jump_to_application(void) +{ + irq_disable(); + set_bootup_code(REQUEST_START_APP); + NVIC_SystemReset(); +} + +static void +start_application(void) +{ + set_bootup_code(0); + uint32_t *vtor = (void*)CONFIG_APPLICATION_START; +#if __VTOR_PRESENT + SCB->VTOR = (uint32_t)vtor; +#endif + asm volatile("MSR msp, %0\n bx %1" : : "r"(vtor[0]), "r"(vtor[1])); +} + void __noreturn __visible reset_handler_stage_two(void) { + uint64_t bootup_code = get_bootup_code(); + if (bootup_code == REQUEST_START_APP) + start_application(); + // Copy global variables from flash to ram uint32_t count = (&_data_end - &_data_start) * 4; __builtin_memcpy(&_data_start, &_data_flash, count); diff --git a/src/stm32/stm32f0.c b/src/stm32/stm32f0.c index c8c4fb3..3a2e438 100644 --- a/src/stm32/stm32f0.c +++ b/src/stm32/stm32f0.c @@ -115,19 +115,3 @@ armcm_main(void) timer_init(); canboot_main(); } - -typedef void (*func_ptr)(void); - -void -jump_to_application(void) -{ - func_ptr application = (func_ptr) *(volatile uint32_t*) - (CONFIG_APPLICATION_START + 0x04); - - // Set the main stack pointer - asm volatile ("MSR msp, %0" : : "r" (*(volatile uint32_t*) - CONFIG_APPLICATION_START) : ); - - // Jump to application - application(); -} diff --git a/src/stm32/stm32f1.c b/src/stm32/stm32f1.c index 8e56185..fd26ca5 100644 --- a/src/stm32/stm32f1.c +++ b/src/stm32/stm32f1.c @@ -237,26 +237,3 @@ armcm_main(void) canboot_main(); } - -typedef void (*func_ptr)(void); - -void -jump_to_application(void) -{ - func_ptr application = (func_ptr) *(volatile uint32_t*) - (CONFIG_APPLICATION_START + 0x04); - - // Reset clocks - RCC->AHBENR = 0x14; - RCC->APB1ENR = 0; - RCC->APB2ENR = 0; - - // Reset the Vector table to the application address - SCB->VTOR = CONFIG_APPLICATION_START; - // Set the main stack pointer - asm volatile ("MSR msp, %0" : : "r" (*(volatile uint32_t*) - CONFIG_APPLICATION_START) : ); - - // Jump to application - application(); -}