【问题标题】:Linking and memory issues for STM32F1 microcontrollersSTM32F1 微控制器的链接和内存问题
【发布时间】:2017-05-17 19:10:54
【问题描述】:

让我们看一下STM32F103的链接脚本:

/* Entry Point */
ENTRY(Reset_Handler)

/* Highest address of the user mode stack */
_estack = 0x20005000;    /* End of 20K RAM */

/* Generate a link error if heap and stack don't fit into RAM */
_Min_Heap_Size = 0;      /* Required amount of heap  */
_Min_Stack_Size = 0x100; /* Required amount of stack */

/* Specify the memory areas */
MEMORY
{
  FLASH (rx)      : ORIGIN = 0x08000000, LENGTH = 128K
  RAM (xrw)       : ORIGIN = 0x20000000, LENGTH = 20K
  MEMORY_B1 (rx)  : ORIGIN = 0x60000000, LENGTH = 0K
}

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

  /* The program code and other data goes into FLASH */
  .text :
  {
    . = ALIGN(4);
    *(.text)           /* .text sections (code) */
    *(.text*)          /* .text* sections (code) */
    *(.rodata)         /* .rodata sections (constants, strings, etc.) */
    *(.rodata*)        /* .rodata* sections (constants, strings, etc.) */
    *(.glue_7)         /* Glue arm to thumb code */
    *(.glue_7t)        /* Glue thumb to arm code */

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

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


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

  .ARM.attributes : { *(.ARM.attributes) } > 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 (*(.fini_array*))
    KEEP (*(SORT(.fini_array.*)))
    PROVIDE_HIDDEN (__fini_array_end = .);
  } >FLASH

  /* Used by the startup to initialize data */
  _sidata = .;

  /* Initialized data sections goes into RAM, load LMA copy after code */
  .data : AT ( _sidata )
  {
    . = 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

  /* 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

  PROVIDE ( end = _ebss );
  PROVIDE ( _end = _ebss );

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

  /* MEMORY_bank1 section, code must be located here explicitly            */
  /* Example: extern int foo(void) __attribute__ ((section (".mb1text"))); */
  .memory_b1_text :
  {
    *(.mb1text)        /* .mb1text sections (code) */
    *(.mb1text*)       /* .mb1text* sections (code)  */
    *(.mb1rodata)      /* read-only data (constants) */
    *(.mb1rodata*)
  } >MEMORY_B1

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

我可以看到 ISR 向量是放在 flash 中的,所以如果程序需要调用某个向量中存储的地址,它会从 flash 中读取。

第一个问题:硬件在从 RAM 和闪存读取之间没有区别吗?为什么我需要特殊的寄存器在代码中写入或读取闪存,而不能明确地从其地址写入或读取?

第二个问题是从闪存读取比从 RAM 读取慢多少?如果我知道我的代码中使用最多的函数,那么我是否能够将它移到 RAM 部分以极大地加快它的执行速度?我相信这个脚本中的MEMORY_B1 是专门为此目的而制作的。

第三个问题:如果 MEMORY_B1 的长度为 0,我们如何在其中放置任何东西?

最后一个问题:如果我在闪存中创建一个附加部分,那么我可以创建一些简单的虚拟内存模拟吗?我认为这个问题的答案取决于第一个问题。

【问题讨论】:

    标签: memory linker arm ld stm32


    【解决方案1】:
    1. 硬件不在乎,因为两个内存(以及其他任何内存)都映射在公共地址空间中。但是,这并不意味着您可以轻松地写入闪存以将其视为 RAM,因为这很慢并且会迅速损坏内存(它的典型写入寿命为 100k 周期)。此外,它只能在整页中擦除(某些 STM32 芯片可能高达 128 kB),这确实给用作 RAM 的替代品带来了问题。

    2. 速度差异可以忽略不计。在 ARM Cortex-M 微控制器上从 RAM 运行代码将比您预期的要慢,因为 RAM 连接到不同的总线(用于数据)并且使用它执行代码需要使用较慢的“互连”。

    3. 你不能。如果您想在那里放置一些东西,则必须增加内存的大小(并且“普通 RAM”的大小 - 减少)。

    4. 通常你可以这样做,但它会非常慢,而且你会很快损坏闪光灯。

    【讨论】:

      【解决方案2】:

      1/2. STM32F1 上没有。只有核心速度超过 100 Mhz 时,闪存预取和缓存未命中才会让您付出代价。甚至这个芯片也有缓存和预取。 如果在某些特殊情况下将向量表放在 RAM 中,使用此内核可能会产生微不足道的微薄利润。

      但是,可能存在适用于所用访问宽度的硬件限制。但是这个闪光不受那个影响。

      3. 是的,你当然可以。你可以在里面放一个文件系统。但是您可以可靠地写入闪存的温度范围是有限的。而且,由于只有一组闪存,所有活动都会停止,直到擦除/闪存成功。除非代码从 RAM 运行。 两个额外的警告是闪存编程/擦除可能需要几毫秒,并且您必须考虑每个 2 kB 的页面擦除,但所有闪存控制器都必须这样做。

      如果您需要额外的 RAM,请在您的板上放置一些 SPI FRAM。

      【讨论】:

        猜你喜欢
        • 2012-08-26
        • 1970-01-01
        • 2010-10-20
        • 2011-11-11
        • 2022-09-26
        • 2013-02-04
        • 2013-11-02
        • 2018-07-03
        • 1970-01-01
        相关资源
        最近更新 更多