blob: 70e6c06da00ce83d5a37d69c8cd8e46e12cdbe03 (
plain)
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
|
/*
Created by: roman3017
Inspired by: https://jacobmossberg.se/posts/2018/08/11/run-c-program-bare-metal-on-arm-cortex-m3.html
*/
extern unsigned int __global_pointer$;
extern unsigned int _stack_start;
extern unsigned int _bss_start;
extern unsigned int _bss_end;
extern unsigned int _ctors_start;
extern unsigned int _ctors_end;
void crtStart();
void irqCallback();
void main();
static void trapEntry();
static void bssInit();
static void ctorsInit();
unsigned int * initVectors[2] __attribute__ ((section("vectors"))) = {
(unsigned int *) crtStart, // code entry
(unsigned int *) trapEntry, // trap entry
};
void crtStart()
{
/* Initialize sp and gp registers */
asm volatile (
"la gp, %0\n\t"
"la sp, %1\n\t"
:: "i"(&__global_pointer$), "i"(&_stack_start)
);
bssInit();
ctorsInit();
main();
asm volatile("j .");
}
static void trapEntry()
{
irqCallback();
}
static void bssInit()
{
unsigned int * bss_start_p = &_bss_start;
unsigned int * bss_end_p = &_bss_end;
while(bss_start_p != bss_end_p) {
*bss_start_p++ = 0;
}
}
static void ctorsInit()
{
unsigned int * ctors_start_p = &_ctors_start;
unsigned int * ctors_end_p = &_ctors_end;
void(*ctor)();
while(ctors_start_p != ctors_end_p) {
ctor = (void *)*ctors_start_p++;
ctor();
}
}
|