TrueSTUDIO中在flash指定位置保存常量
在TrueSTUDIO开发软件中如何实现在flash指定位置保存常量,使用官方文档“user_guide.pdf”修改多次不能实现。
通过阅读“ld.pdf”以及网上该文档的翻译版本,历时2天,实现方法总结如下。
步骤1
STM32L071RB_FLASH.ld
/* Specify the memory areas */
MEMORY
{
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 20K
FLASH0 (rx) : ORIGIN = 0x08008000, LENGTH = 512
FLASH (rx) : ORIGIN = 0x08008200, LENGTH = 96K
}
/* Define output sections */
SECTIONS
{
/* The startup code goes first into FLASH */
.isr_vector :
{
. = ALIGN(4);
KEEP(*(.isr_vector)) /* Startup code */
. = ALIGN(4);
} >FLASH0
.myvars : /* const var 2019.9.27 */
{
*(.MYVARS)
} >FLASH
/* 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
...
}
步骤2
main.c
__attribute__((section(".MYVARS"))) const uint8_t MyVars[] ={"IDENTIFY"};
注意,程序中需确保使用MyVars变量,而不会被编译器优化掉!
查看