Write the startup copy
Copy .data from flash to RAM and zero .bss, as the reset handler does. An easy embedded C problem (Memory layout and startup code), run and graded in the…
easy · Memory layout and startup code · startup · memory · pointers
Before main() runs, the reset handler prepares RAM:
- copy the initial values of .data from their load address in flash (
data_load) to their run address in RAM (data_startup to, not including,data_end), - set every word of .bss (
bss_starttobss_end) to zero.
Write reset_init(data_load, data_start, data_end, bss_start, bss_end) working word by word, as the linker script's symbols are word-aligned.
Starter code
void reset_init(const uint32_t *data_load, uint32_t *data_start, uint32_t *data_end,
uint32_t *bss_start, uint32_t *bss_end) {
for (uint32_t *p = data_start; p <= data_end; p++) *p = *data_load++;
(void)bss_start; (void)bss_end;
}Example tests
Three words of data, four of bss
for (int i = 0; i < 10; i++) ram[i] = 0xAAAAAAAAu; /* RAM holds garbage at power-up */
reset_init(flash_image, &ram[0], &ram[3], &ram[3], &ram[7]);
dump();Expected output:
11111111 22222222 33333333 00000000 00000000 00000000 00000000 AAAAAAAA AAAAAAAA AAAAAAAALearn it first
- Memory layout and startup code · From reset to main: copying .data, zeroing .bss and the C runtime.