存储在SRAM

时间:2018-08-28 08:38:53

标签: c stm32 sections

我们对于存储在SRAM中的变量和默认值有一些麻烦。

我们正在使用STM32F745ZE micro。我们必须为SRAM定义一个部分:

/* Specify the memory areas */
MEMORY
{
ITCM_RAM (rx)  : ORIGIN = 0x00000000, LENGTH = 16K   /* +MW+ */
/*RAM (xrw)      : ORIGIN = 0x20000000, LENGTH = 319K*/
RAM (xrw)      : ORIGIN = 0x20000000, LENGTH = 64K   /* +MW+ DTCM RAM */
RAM_GEN (xrw)  : ORIGIN = 0x20010000, LENGTH = 255K  /* +MW+ SRAM1 + SRAM2 */

TRACER (xrw)   : ORIGIN = 0x2004FC00, LENGTH = 1K    /* +MW+ TRACER memory space */
FLASH (rx)     : ORIGIN =  0x8000000, LENGTH = 512K
}

(...)

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

  /* +MW+ SRAM data section */
  .ram_gen :
  {
    . = ALIGN(4);
    _sram_gen = .;     /* create a global symbol at data start */
    *(.ram_gen)        /* .data sections */
    *(.ram_gen*)       /* .data* sections */

    . = ALIGN(4);
    _eram_gen = .;     /* define a global symbol at data end */
  } >RAM_GEN

我们要为某些变量分配默认值。它适用于未存储在SRAM中的变量,但不适用于存储在SRAM:中的变量。例子

static Test_Ram_t testRam = { .value = 1 };
static Test_Ram_Value_t testRamValue __attribute__((section(".ram_gen"))) = { .value = 1 };

static void Test_Ram_Task1(void const *pvParameters)
{
    Test_Ram_Printf("Test Ram: START -----------------------------------------------");
    Test_Ram_Printf("Test Ram: Pre value: %d %d", testRam.value, testRamValue.value);

    testRam.value += 10;
    testRamValue.value += 10;

    Test_Ram_Printf("Test Ram: Post value: %d %d", testRam.value, testRamValue.value);

    osDelay(osWaitForever);
}

如果我们重新启动应用程序3次,则应用程序输出为:

[10:30:25.002]
[10:30:25.004] Test Ram: START -----------------------------------------------
[10:30:25.005] Test Ram: Pre value: 1 1
[10:30:25.005] Test Ram: Post value: 11 11
[10:30:27.599]
[10:30:27.601] Test Ram: START -----------------------------------------------
[10:30:27.603] Test Ram: Pre value: 1 11
[10:30:27.604] Test Ram: Post value: 11 21
[10:30:29.497]
[10:30:29.500] Test Ram: START -----------------------------------------------
[10:30:29.502] Test Ram: Pre value: 1 21
[10:30:29.503] Test Ram: Post value: 11 31

存储在RAM中的变量testRam在每次重新启动时均被指定为默认值,但是存储在SRAM中的变量testRamValue在每次重新启动时均保留该值。

有人知道如何在每次重新启动时强制分配默认值吗?

0 个答案:

没有答案