cc2538: Initialize .data/.bss using ROM functions

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 <benoit.thebaudeau.dev@gmail.com>
This commit is contained in:
Benoît Thébaudeau 2015-05-17 22:28:01 +02:00
parent 609c615303
commit 1cd3c9e7e5

View file

@ -40,6 +40,7 @@
#include "reg.h"
#include "flash-cca.h"
#include "sys-ctrl.h"
#include "rom-util.h"
#include <stdint.h>
/*---------------------------------------------------------------------------*/
@ -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();