From 1cd3c9e7e5235d0aca5f3d4fa461036f6749b4b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Th=C3=A9baudeau?= Date: Sun, 17 May 2015 22:28:01 +0200 Subject: [PATCH] cc2538: Initialize .data/.bss using ROM functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is safer because the previous code assumed that the start and end VMAs of .data and .bss were word-aligned, which is not always the case, so the initialization code could write data outside these sections. The ROM functions support any address boundary. This is faster because the ROM functions are ultra optimized, using realignment and the LDM/STM instructions, which is much better than the previous simple loops of single word accesses. This is smaller because the ROM functions don't require to add any code to the target device other than simple function calls. This makes the code simpler and more maintainable because standard functions are not reimplemented and no assembly is used. Note that this is also faster and smaller than the corresponding functions from the standard string library. Signed-off-by: Benoît Thébaudeau --- cpu/cc2538/startup-gcc.c | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/cpu/cc2538/startup-gcc.c b/cpu/cc2538/startup-gcc.c index bf00c90f9..b4155fa29 100644 --- a/cpu/cc2538/startup-gcc.c +++ b/cpu/cc2538/startup-gcc.c @@ -40,6 +40,7 @@ #include "reg.h" #include "flash-cca.h" #include "sys-ctrl.h" +#include "rom-util.h" #include /*---------------------------------------------------------------------------*/ @@ -275,11 +276,11 @@ void(*const vectors[])(void) = }; /*---------------------------------------------------------------------------*/ /* Linker constructs indicating .data and .bss segment locations */ -extern unsigned long _ldata; -extern unsigned long _data; -extern unsigned long _edata; -extern unsigned long _bss; -extern unsigned long _ebss; +extern uint8_t _ldata; +extern uint8_t _data; +extern uint8_t _edata; +extern uint8_t _bss; +extern uint8_t _ebss; /*---------------------------------------------------------------------------*/ /* Weak interrupt handlers. */ void @@ -298,26 +299,13 @@ default_handler(void) void reset_handler(void) { - unsigned long *pul_src, *pul_dst; - REG(SYS_CTRL_EMUOVR) = 0xFF; /* Copy the data segment initializers from flash to SRAM. */ - pul_src = &_ldata; - - for(pul_dst = &_data; pul_dst < &_edata;) { - *pul_dst++ = *pul_src++; - } + rom_util_memcpy(&_data, &_ldata, &_edata - &_data); /* Zero-fill the bss segment. */ - __asm(" ldr r0, =_bss\n" - " ldr r1, =_ebss\n" - " mov r2, #0\n" - " .thumb_func\n" - "zero_loop:\n" - " cmp r0, r1\n" - " it lt\n" - " strlt r2, [r0], #4\n" " blt zero_loop"); + rom_util_memset(&_bss, 0, &_ebss - &_bss); /* call the application's entry point. */ main();