aboutsummaryrefslogtreecommitdiff
path: root/src/kern/init.c
blob: 2531ca91ce06e2f0fc6db30f9389006fa6b35149 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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 */