~ruther/stm32-templates

17b62d8d2b2691610f5b14853c4456fd58b913ef — Rutherther 6 months ago 6625c42
feat: more robust ram data loading

The .data section was assumed to start
after .text. This condition is satisfied now,
but why rely on it? The load address can be exported
from the linker script with LOADADDR call.
M devices/general/startup.c => devices/general/startup.c +4 -4
@@ 1,14 1,14 @@
#include <stdint.h>
#include "defines.h"

extern uint32_t _etext, _sdata, _edata, _sbss, _ebss;
extern uint32_t _data_size, _bss_size, _data_loadaddr, _sdata, _sbss;
void main(void);

void reset_handler(void)
{
  // Copy .data from FLASH to SRAM
  uint32_t data_size = (uint32_t)&_edata - (uint32_t)&_sdata;
  uint8_t *flash_data = (uint8_t*) &_etext;
  uint32_t data_size = (uint32_t)&_data_size;
  uint8_t *flash_data = (uint8_t*) &_data_loadaddr;
  uint8_t *sram_data = (uint8_t*) &_sdata;

  for (uint32_t i = 0; i < data_size; i++)


@@ 17,7 17,7 @@ void reset_handler(void)
  }

  // Zero-fill .bss section in SRAM
  uint32_t bss_size = (uint32_t)&_ebss - (uint32_t)&_sbss;
  uint32_t bss_size = (uint32_t)&_bss_size;
  uint8_t *bss = (uint8_t*) &_sbss;

  for (uint32_t i = 0; i < bss_size; i++)

M devices/stm32f401/linker_script.ld => devices/stm32f401/linker_script.ld +5 -0
@@ 33,6 33,9 @@ SECTIONS
    _edata = .;
  } >SRAM AT> FLASH

  _data_size = _edata - _sdata;
  _data_loadaddr = LOADADDR(.data);

  .bss :
  {
    . = ALIGN(4);


@@ 43,6 46,8 @@ SECTIONS
    . = ALIGN(4);
    _ebss = .;
  } >SRAM

  _bss_size = _ebss - _sbss;
}

ENTRY(reset_handler)

M devices/stm32f446/linker_script.ld => devices/stm32f446/linker_script.ld +5 -0
@@ 33,6 33,9 @@ SECTIONS
    _edata = .;
  } >SRAM AT> FLASH

  _data_size = _edata - _sdata;
  _data_loadaddr = LOADADDR(.data);

  .bss :
  {
    . = ALIGN(4);


@@ 43,6 46,8 @@ SECTIONS
    . = ALIGN(4);
    _ebss = .;
  } >SRAM

  _bss_size = _ebss - _sbss;
}

ENTRY(reset_handler)

Do not follow this link