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 <gareth@blacksphere.co.nz>.
This commit is contained in:
Uwe Hermann 2010-10-19 02:00:28 +02:00
parent 68b7e255ad
commit 1621fde1f4
2 changed files with 31 additions and 2 deletions

View File

@ -33,15 +33,25 @@ SECTIONS
*(.vectors) /* Vector table */ *(.vectors) /* Vector table */
*(.text) /* Program code */ *(.text) /* Program code */
*(.rodata) /* Read-only data */ *(.rodata) /* Read-only data */
_etext = .;
} >rom } >rom
. = ORIGIN(ram); . = ORIGIN(ram);
.data : { .data : {
_data = .;
*(.data) /* Read-write initialized data */ *(.data) /* Read-write initialized data */
_edata = .;
} >ram AT >rom } >ram AT >rom
.bss : { .bss : {
*(.bss) /* Read-write zero initialized data */ *(.bss) /* Read-write zero initialized data */
*(COMMON)
_ebss = .;
} >ram AT >rom } >ram AT >rom
end = .;
} }
PROVIDE(_stack = 0x20000800);

View File

@ -19,7 +19,11 @@
#define WEAK __attribute__ ((weak)) #define WEAK __attribute__ ((weak))
/* Symbols exported by linker script */
extern unsigned _etext, _data, _edata, _ebss, _stack;
void main(void); void main(void);
void reset_handler(void);
void blocking_handler(void); void blocking_handler(void);
void null_handler(void); void null_handler(void);
@ -95,8 +99,8 @@ void WEAK dma2_channel4_5_isr(void);
__attribute__ ((section(".vectors"))) __attribute__ ((section(".vectors")))
void (*const vector_table[]) (void) = { void (*const vector_table[]) (void) = {
(void *)0x20000800, /* Use 2KB stack (0x800 bytes). */ (void*)&_stack,
main, /* Use main() as reset vector for now. */ reset_handler,
nmi_handler, nmi_handler,
hard_fault_handler, hard_fault_handler,
mem_manage_handler, mem_manage_handler,
@ -170,6 +174,21 @@ void (*const vector_table[]) (void) = {
dma2_channel4_5_isr, 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) void blocking_handler(void)
{ {
while (1) ; while (1) ;