某10

懒得写,咕了

STM32 Linker Script 划分存储空间

STM32没有内置的EEPROM,但是我们可以通过修改Linker Script重新划分内部存储空间以达到模拟EEPROM的效果,本文就STM32F042G6U来演示如何方便的将一页内部Flash存储空间映射到代码变量中。

QQ截图20190420182426.png
上图为STM32F042x6的内存布局,如图所示,其中Flash Memory 0x08000000到0x08008000是我们程序存储的区域,我们要从这一段地址中划分出独立的部分模拟EEPROM。

修改STM32F042G6Ux_FLASH.ld文件

STM32F042G6Ux_FLASH.ld是由CubeMx软件自动生成的,代码生成设置中选择以Makefile工程进行生成。

/* Specify the memory areas */
MEMORY
{
RAM (xrw)      : ORIGIN = 0x20000000, LENGTH = 6K
FLASH (rx)     : ORIGIN = 0x8000000, LENGTH = 32K
}

这段脚本用于定义内存区域,可以看到FLASH分区开始地址为0x8000000,长度为32K。我们在这里修改一下长度,挤出1K空间出来

/* Specify the memory areas */
MEMORY
{
RAM (xrw)      : ORIGIN = 0x20000000, LENGTH = 6K
FLASH (rx)     : ORIGIN = 0x8000000, LENGTH = 31K
EEPROM (rw)    : ORIGIN = 0x8007C00, LENGTH = 1K
}

然后我们成功从FLASH中挤出了1K给了EEPROM,EEPROM这个名字是可以根据自己需求修改的。

起始地址0x8007C00是如何计算出来的呢?答案是:用FLASH分区的起始地址加上31K*1024即可得出EEPROM分区的起始地址,即

0x8000000+(31*1024)
= 0x8000000+0x7C00
= 0x8007C00

然后修改SECTIONS

/* Define output sections */
SECTIONS
{
  /* The startup code goes first into FLASH */
  .isr_vector :
  {
    . = ALIGN(4);
    KEEP(*(.isr_vector)) /* Startup code */
    . = ALIGN(4);
  } >FLASH
 /* 后面省略 */

在该段代码后面添加

.eeprom :
{
  . = ALIGN(4);
  *(.eeprom)
  . = ALIGN(4);
} >EEPROM

其中eeprom和EEPROM名对应上面分区设置的名字,文章最后有贴出完整的Linker Script文件参考。

代码应用

const uint8_t EEPArray[1024] __attribute__ ((section(".eeprom"),used)) = {0xAA,0xBB,0xCC,0xDD,0x12};

定义一个只读的长度为1024的数组 后面为它添加属性__attribute__ ((section(".eeprom"),used)),这个代码适用于gcc编译器,其他编译器可能不吃这个,如果在代码中未使用这个数组,加入,used可以防止编译器优化掉这个部分。
经过测试,直接修改该数组是无法对Flash内容进行修改的,还是需要常规方式先解锁Flash擦除后写入的。

Linker Script完整内容

/* Entry Point */
ENTRY(Reset_Handler)

/* Highest address of the user mode stack */
_estack = 0x20001800;    /* end of RAM */
/* Generate a link error if heap and stack don't fit into RAM */
_Min_Heap_Size = 0x200;      /* required amount of heap  */
_Min_Stack_Size = 0x400; /* required amount of stack */

/* Specify the memory areas */
MEMORY
{
RAM (xrw)      : ORIGIN = 0x20000000, LENGTH = 6K
FLASH (rx)     : ORIGIN = 0x8000000, LENGTH = 31K
EEPROM (rw)  : ORIGIN = 0x8007C00, LENGTH = 1K
}

/* Define output sections */
SECTIONS
{
  /* The startup code goes first into FLASH */
  .isr_vector :
  {
    . = ALIGN(4);
    KEEP(*(.isr_vector)) /* Startup code */
    . = ALIGN(4);
  } >FLASH

  .eeprom :
  {
    . = ALIGN(4);
    *(.eeprom)
    . = ALIGN(4);
  } >EEPROM
  
  /* The program code and other data goes into FLASH */
  .text :
  {
    . = ALIGN(4);
    *(.text)           /* .text sections (code) */
    *(.text*)          /* .text* sections (code) */
    *(.glue_7)         /* glue arm to thumb code */
    *(.glue_7t)        /* glue thumb to arm code */
    *(.eh_frame)

    KEEP (*(.init))
    KEEP (*(.fini))

    . = ALIGN(4);
    _etext = .;        /* define a global symbols at end of code */
  } >FLASH

  /* Constant data goes into FLASH */
  .rodata :
  {
    . = ALIGN(4);
    *(.rodata)         /* .rodata sections (constants, strings, etc.) */
    *(.rodata*)        /* .rodata* sections (constants, strings, etc.) */
    . = ALIGN(4);
  } >FLASH

  .ARM.extab   : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH
  .ARM : {
    __exidx_start = .;
    *(.ARM.exidx*)
    __exidx_end = .;
  } >FLASH

  .preinit_array     :
  {
    PROVIDE_HIDDEN (__preinit_array_start = .);
    KEEP (*(.preinit_array*))
    PROVIDE_HIDDEN (__preinit_array_end = .);
  } >FLASH
  .init_array :
  {
    PROVIDE_HIDDEN (__init_array_start = .);
    KEEP (*(SORT(.init_array.*)))
    KEEP (*(.init_array*))
    PROVIDE_HIDDEN (__init_array_end = .);
  } >FLASH
  .fini_array :
  {
    PROVIDE_HIDDEN (__fini_array_start = .);
    KEEP (*(SORT(.fini_array.*)))
    KEEP (*(.fini_array*))
    PROVIDE_HIDDEN (__fini_array_end = .);
  } >FLASH

  /* used by the startup to initialize data */
  _sidata = LOADADDR(.data);

  /* Initialized data sections goes into RAM, load LMA copy after code */
  .data : 
  {
    . = ALIGN(4);
    _sdata = .;        /* create a global symbol at data start */
    *(.data)           /* .data sections */
    *(.data*)          /* .data* sections */

    . = ALIGN(4);
    _edata = .;        /* define a global symbol at data end */
  } >RAM AT> FLASH

  
  /* Uninitialized data section */
  . = ALIGN(4);
  .bss :
  {
    /* This is used by the startup in order to initialize the .bss secion */
    _sbss = .;         /* define a global symbol at bss start */
    __bss_start__ = _sbss;
    *(.bss)
    *(.bss*)
    *(COMMON)

    . = ALIGN(4);
    _ebss = .;         /* define a global symbol at bss end */
    __bss_end__ = _ebss;
  } >RAM

  /* User_heap_stack section, used to check that there is enough RAM left */
  ._user_heap_stack :
  {
    . = ALIGN(8);
    PROVIDE ( end = . );
    PROVIDE ( _end = . );
    . = . + _Min_Heap_Size;
    . = . + _Min_Stack_Size;
    . = ALIGN(8);
  } >RAM

  

  /* Remove information from the standard libraries */
  /DISCARD/ :
  {
    libc.a ( * )
    libm.a ( * )
    libgcc.a ( * )
  }

  .ARM.attributes 0 : { *(.ARM.attributes) }
}

评论卡