From 1621fde1f47c5b5f86942562b955bebfb45683df Mon Sep 17 00:00:00 2001 From: Uwe Hermann Date: Tue, 19 Oct 2010 02:00:28 +0200 Subject: Add proper C runtime init, add reset handler. The C runtime wasn't initialized correctly (there was garbage in the data and bss sections). Add a reset_handler which initializes these sections before calling the application's main() function. The initial stack pointer is also defined in the linker script, allowing the application to override with a linker command line option "-Wl,--defsym,_stack=0x20005000". Thanks to Gareth McMullin . --- lib/libopenstm32.ld | 10 ++++++++++ lib/vector.c | 23 +++++++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/libopenstm32.ld b/lib/libopenstm32.ld index 910b350..13efe44 100644 --- a/lib/libopenstm32.ld +++ b/lib/libopenstm32.ld @@ -33,15 +33,25 @@ SECTIONS *(.vectors) /* Vector table */ *(.text) /* Program code */ *(.rodata) /* Read-only data */ + _etext = .; } >rom . = ORIGIN(ram); .data : { + _data = .; *(.data) /* Read-write initialized data */ + _edata = .; } >ram AT >rom .bss : { *(.bss) /* Read-write zero initialized data */ + *(COMMON) + _ebss = .; } >ram AT >rom + + end = .; } + +PROVIDE(_stack = 0x20000800); + diff --git a/lib/vector.c b/lib/vector.c index 39f13f8..f83e64d 100644 --- a/lib/vector.c +++ b/lib/vector.c @@ -19,7 +19,11 @@ #define WEAK __attribute__ ((weak)) +/* Symbols exported by linker script */ +extern unsigned _etext, _data, _edata, _ebss, _stack; + void main(void); +void reset_handler(void); void blocking_handler(void); void null_handler(void); @@ -95,8 +99,8 @@ void WEAK dma2_channel4_5_isr(void); __attribute__ ((section(".vectors"))) void (*const vector_table[]) (void) = { - (void *)0x20000800, /* Use 2KB stack (0x800 bytes). */ - main, /* Use main() as reset vector for now. */ + (void*)&_stack, + reset_handler, nmi_handler, hard_fault_handler, mem_manage_handler, @@ -170,6 +174,21 @@ void (*const vector_table[]) (void) = { dma2_channel4_5_isr, }; +void reset_handler(void) +{ + volatile unsigned *src, *dest; + asm("MSR msp, %0" : : "r"(&_stack)); + + for (src = &_etext, dest = &_data; dest < &_edata; src++, dest++) + *dest = *src; + + while (dest < &_ebss) + *dest++ = 0; + + /* Call the application's entry point. */ + main(); +} + void blocking_handler(void) { while (1) ; -- cgit v1.2.3