Skip to content

Commit 3a31e71

Browse files
author
Pavel Revak
committed
more functional startup
1 parent ac932ee commit 3a31e71

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

src/startup/startup.cpp

+32-24
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ extern unsigned _data_end;
99
extern unsigned _bss_start;
1010
extern unsigned _bss_end;
1111

12+
extern unsigned _heap_start;
13+
1214
extern unsigned _init_array_start;
1315
extern unsigned _init_array_end;
1416

@@ -18,48 +20,54 @@ extern unsigned _fini_array_end;
1820
// main application
1921
extern void main_app();
2022

21-
// reset handler
22-
void RESET_handler() {
23-
unsigned *src, *dst;
24-
25-
// copy data
26-
src = &_data_load;
27-
dst = &_data_start;
23+
void copy_data() {
24+
unsigned *src = &_data_load;
25+
unsigned *dst = &_data_start;
2826
while (dst < &_data_end) {
2927
*dst++ = *src++;
3028
}
29+
}
3130

32-
// zero bss
33-
dst = &_bss_start;
31+
void zero_bss() {
32+
unsigned *dst = &_bss_start;
3433
while (dst < &_bss_end) {
3534
*dst++ = 0;
3635
}
36+
}
3737

38-
// get current stack pointer
38+
void fill_heap(unsigned fill=0x55555555) {
39+
unsigned *dst = &_heap_start;
3940
register unsigned *msp_reg;
4041
__asm__("mrs %0, msp\n" : "=r" (msp_reg) );
41-
42-
// fill unused SRAM
43-
dst = &_bss_end;
4442
while (dst < msp_reg) {
45-
*dst++ = 0x55555555;
43+
*dst++ = fill;
4644
}
45+
}
4746

48-
// call constructors for static instances
49-
dst = &_init_array_start;
50-
while (dst < &_init_array_end) {
51-
((void (*)())*dst++)();
47+
void call_ctors() {
48+
unsigned *tbl = &_init_array_start;
49+
while (tbl < &_init_array_end) {
50+
((void (*)())*tbl++)();
5251
}
52+
}
5353

54+
void call_dtors() {
55+
unsigned *tbl = &_fini_array_start;
56+
while (tbl < &_fini_array_end) {
57+
((void (*)())*tbl++)();
58+
}
59+
}
60+
61+
// reset handler
62+
void RESET_handler() {
63+
copy_data();
64+
zero_bss();
65+
fill_heap();
66+
call_ctors();
5467
// run application
5568
main_app();
56-
5769
// call destructors for static instances
58-
dst = &_fini_array_start;
59-
while (dst < &_fini_array_end) {
60-
((void (*)())*dst++)();
61-
}
62-
70+
call_dtors();
6371
// stop
6472
while (true);
6573
}

0 commit comments

Comments
 (0)