-
Notifications
You must be signed in to change notification settings - Fork 166
/
Copy pathcrt0.cpp
89 lines (70 loc) · 2.68 KB
/
crt0.cpp
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
83
84
85
86
87
88
89
///////////////////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2013 - 2020.
// Distributed under the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// AM335x ARM(R) A8 startup code
// Expressed with C++ for TI AM3359 ARM(R) A8 (BeagleBone) by Christopher Kormanyos.
#include <mcal/mcal.h>
namespace crt
{
void init_ram ();
void init_ctors();
}
extern "C" int main ();
extern "C" void __initial_stack_pointer();
extern "C" void __my_startup () __attribute__((section(".startup"), used, noinline));
void __my_startup()
{
// Setup the stack pointers and the stacks.
// For additional information on stack setup,
// see parts of Sitara Ware's "init.s".
// Setup the stack pointer for the supervisor mode.
asm volatile("msr cpsr_c, #0x13 | 0xC0");
asm volatile("ldr r3, =__SVC_STACK_TOP");
asm volatile("mov sp, r3");
// Relocate the interrupt vector table.
asm volatile("ldr r3, =__INTVECT_BASE_ADDRESS");
asm volatile("mcr p15, 0, r3, c12, c0, 0");
// Switch to Undefined mode and setup the relevant stack pointer.
asm volatile("msr cpsr_c, #0x1B | 0xC0");
asm volatile("ldr r3, =__UND_STACK_TOP");
asm volatile("mov sp, r3");
// Switch to Abort mode and setup the relevant stack pointer.
asm volatile("msr cpsr_c, #0x17 | 0xC0");
asm volatile("ldr r3, =__ABT_STACK_TOP");
asm volatile("mov sp, r3");
// Switch to IRQ mode and setup the relevant stack pointer.
asm volatile("msr cpsr_c, #0x12 | 0xC0");
asm volatile("ldr r3, =__IRQ_STACK_TOP");
asm volatile("mov sp, r3");
// Switch to FIQ mode and setup the relevant stack pointer.
asm volatile("msr cpsr_c, #0x11 | 0xC0");
asm volatile("ldr r3, =__FIQ_STACK_TOP");
asm volatile("mov sp, r3");
// Switch to System mode and setup the relevant stack pointer.
asm volatile("msr cpsr_c, #0x1f | 0xC0");
asm volatile("ldr r3, =__SYS_STACK_TOP");
asm volatile("mov sp, r3");
// Chip init: Watchdog, port, and oscillator.
mcal::cpu::init();
// Initialize the FPU.
mcal::cpu::fpu();
// Initialize statics from ROM to RAM.
// Zero-clear default-initialized static RAM.
crt::init_ram();
mcal::wdg::secure::trigger();
// Call all ctor initializations.
crt::init_ctors();
mcal::wdg::secure::trigger();
// Jump to main (and never return).
asm volatile("ldr r3, =main");
asm volatile("blx r3");
// Catch an unexpected return from main.
for(;;)
{
// Replace with a loud error if desired.
mcal::wdg::secure::trigger();
}
}