aboutsummaryrefslogtreecommitdiff
path: root/src/kern/init.c
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2020-11-24 13:46:41 -0700
committerJosh Rahm <joshuarahm@gmail.com>2020-11-24 13:46:41 -0700
commit93b063fedfcf7409a67df035170ea5670cad22e1 (patch)
treea23321a7465d966b1ccf196ca00e65a70c9f9110 /src/kern/init.c
parentb040195d31df6ad759f16ea3456471897f55daa1 (diff)
downloadstm32l4-93b063fedfcf7409a67df035170ea5670cad22e1.tar.gz
stm32l4-93b063fedfcf7409a67df035170ea5670cad22e1.tar.bz2
stm32l4-93b063fedfcf7409a67df035170ea5670cad22e1.zip
Moved action to top level.
Removed old iterations of the project and moved the files from 02-usart to the root directory since that's the sole place where the action is and that subproject has outgrown its initial title.
Diffstat (limited to 'src/kern/init.c')
-rw-r--r--src/kern/init.c82
1 files changed, 82 insertions, 0 deletions
diff --git a/src/kern/init.c b/src/kern/init.c
new file mode 100644
index 0000000..2531ca9
--- /dev/null
+++ b/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 */