armcm_boot: Introduce cross-platform boot to application code

It's a good idea to reset the cpu before starting the main application
code.  However it is difficult to reliably reset the cpu in software.

This changes the software to actually do a cpu hardware reset prior to
launching the main application - after each bootup the code checks to
see if the application should be started before entering the main
bootloader code.  This helps ensure the application code is started in
a "pristine" cpu state.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2022-05-04 20:46:23 -04:00 committed by Eric Callahan
parent 726e6e62fa
commit 9c6e72e3a5
3 changed files with 26 additions and 44 deletions

View File

@ -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);

View File

@ -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();
}

View File

@ -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();
}