aboutsummaryrefslogtreecommitdiff
path: root/02-usart/src/init.c
diff options
context:
space:
mode:
Diffstat (limited to '02-usart/src/init.c')
-rw-r--r--02-usart/src/init.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/02-usart/src/init.c b/02-usart/src/init.c
new file mode 100644
index 0000000..70703aa
--- /dev/null
+++ b/02-usart/src/init.c
@@ -0,0 +1,45 @@
+#include "arch.h"
+#include "system.h"
+
+/* Forward-declare the main function. This is implemented in main.c. */
+void main();
+
+/* These are defined in the linker script. */
+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;
+
+/*
+ * Runs before main. Initializes the data and bss segments by loading them
+ * into memory.
+ */
+_Noreturn void on_reset()
+{
+ 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;
+ }
+
+ /* Set the vector offset table to be at the start
+ * of FLASH memory. */
+ SCB.vto_r = 0x08000000;
+
+ /* Jump to main. */
+ main();
+
+ for(;;);
+}