From 2478a549b9f64c50310da41c861b8f86fdea2861 Mon Sep 17 00:00:00 2001 From: Josh Rahm Date: Tue, 24 Nov 2020 00:38:09 -0700 Subject: Add new system for startup. Now instead of init() and main() being responsible for all initialization, individual modules can link in their own initialization routines. There are 7 levels for these initializiation routines. So far these are how the levels are defined level 0 - Here the world is dark. Nothing is initialized. This level is responsible for initializing the system clock. level 1 - The system clock has been configured, but nothing else. Not even global variables. This level is responsible for loading the data sections from flash and clearing the .bss section. level 2 - USART2 is enabled and set to be the main kernel logging vehicle. From this point on klogf(...) can be used. level 3 - The NVIC is reset to point to the flash. From this point on interrupts can be received. I expect this is where most core initialization routines will take place levels 4 to 7 - User initializiation levels. main - main() is called after all 8 initialization levels have executed, so in a sense main() is like a 9th initialization level, except that there is can be only one main() routine whereas there can be multiple initalization routines per level. --- 02-usart/src/kern/init.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 02-usart/src/kern/init.c (limited to '02-usart/src/kern/init.c') diff --git a/02-usart/src/kern/init.c b/02-usart/src/kern/init.c new file mode 100644 index 0000000..2531ca9 --- /dev/null +++ b/02-usart/src/kern/init.c @@ -0,0 +1,82 @@ +#include "kern/init.h" + +#include "arch.h" +#include "arch/stm32l4xxx/peripherals/system.h" +#include "arch/stm32l4xxx/peripherals/clock.h" + +#include "kern/log.h" + +/* Forward-declare the main function. This is implemented in main.c. */ +int main(); + +/* These are defined in the linker script. */ + +#ifdef ARCH_STM32L4 +extern uint32_t INIT_DATA_VALUES; +extern uint32_t DATA_SEGMENT_START; +extern uint32_t DATA_SEGMENT_STOP; +extern uint32_t BSS_START; +extern uint32_t BSS_END; + +extern void(*INIT_ROUTINES_FLASH_START)(); +extern void(*INIT_ROUTINES_FLASH_STOP)(); + +init0() +{ + /* Enable a higher clock speed. This is the first thing we do + * beacuse it will boost the boot up time. */ + set_system_clock_MHz(80); +} + +init1() +{ + /* Next, we'll copy the data sections from flash to ram. */ + uint32_t* src; + uint32_t* dest; + + src = &INIT_DATA_VALUES; + dest = &DATA_SEGMENT_START; + + /* Copy the values from flash into the data segment. */ + while (dest != &DATA_SEGMENT_STOP) { + *(dest++) = *(src++); + } + + /* Everything in the BSS segment is set to zero. */ + dest = &BSS_START; + while (dest != &BSS_END) { + *(dest++) = 0; + } +} + +init3() +{ + klogf("--- System Restart ---\n"); + klogf("Setting the vector offset table to point to the start of flash.\n"); + + /* Set the vector offset table to be at the start + * of FLASH memory. */ + SCB.vto_r = 0x08000000; +} + +/* + * Runs before main. Initializes the data and bss segments by loading them + * into memory. + */ +_Noreturn void on_reset() +{ + void (**initfn)(); + for(initfn = &INIT_ROUTINES_FLASH_START; + initfn < &INIT_ROUTINES_FLASH_STOP; + ++ initfn) { + + (*initfn)(); + } + + /* Jump to main. */ + main(); + + for(;;); +} + +#endif /* ARCH_STM32L4 */ -- cgit