-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathcrt0.cpp
More file actions
63 lines (48 loc) · 1.68 KB
/
crt0.cpp
File metadata and controls
63 lines (48 loc) · 1.68 KB
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
///////////////////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2007 - 2024.
// 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)
//
// ATMEL(R) AVR(R) startup code.
// Expressed with C++ for AtmegaX by Chris.
#include <mcal/mcal.h>
asm(".extern __initial_stack_pointer");
namespace crt
{
void init_ram () __attribute__((section(".startup"), used, noinline));
void init_ctors() __attribute__((section(".startup"), used, noinline));
}
extern "C" void __my_startup() __attribute__((section(".startup"), used, noinline));
extern auto main(void) -> int __attribute__((used, noinline));
void __my_startup()
{
// Load the sreg register.
asm volatile("eor r1, r1");
asm volatile("out 0x3f, r1");
// Set the stack pointer.
asm volatile("ldi r28, lo8(__initial_stack_pointer)");
asm volatile("ldi r29, hi8(__initial_stack_pointer)");
// Load the sph register (stack pointer high).
asm volatile("out 0x3e, r29");
// Load the spl register (stack pointer low).
asm volatile("out 0x3d, r28");
// CPU Initialization, including watchdog,
// port, oscillators and other clocks.
mcal::cpu::init();
// 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("jmp main");
// Catch an unexpected return from main.
for(;;)
{
// Replace with a loud error if desired.
mcal::wdg::secure::trigger();
}
}