为什么使用__attribute__
attribute__的作用就是将变量或者数组存在指定的地址。在实际应用中一般将程序的版本信息固定在特定的flash地址。
比如在keil中使用const u16 gflashdata__attribute((at(0x0800F000))) = 0xAEAE;,就是将AEAE写入对应的地址,这样做的好处是,在程序烧录进flash的时候,对应的地址就会写入对应的数据。
来举一个在工作中经常用到的使用场景,在工作中批量生产的产品一般都是一个bootloader文件,一个APP应用程序文件。在批量烧录的时候,一般都是将bootloader文件和APP文件合并到一个文件中一次烧录。在初次上电运行时,bootloader会检测对应的flash地址中是否存在有APP存在的标志位,这时候使用attribute就非常方便了。
但是因为keil和cubeide使用的编译链是不一样的,所以在cubeide中使用__attribute__,需要修改的地方就多一点了。
修改ld文件
使用cubeide的时候,需要修改ld文件,关于ld文件的具体细节,可以参考这篇博客ld文件描述
__attribute__((section(".programcodeendflag"))) const uint8_t program_code_end_flag[] = {0x0F, 0x1E};
const uint8_t Bootloader_Version[] __attribute__((section(".bootloader_version"))) = "V 1.0.0";
在main函数之前添上这两行代码,特意写了两种格式,这两种格式都是可以的,看个人的喜好。
要注意**section()**中的名字,和数组名字哦,我们等下还会用到。
接下来就需要修改ld文件了。
首先在这个位置添加代码,ORIGIN后面就是变量存储的地址,根据自己的需求进行修改。注意此时的BOOTLOADERVERSION, m_programcodeendflag 这两个名字,是自己命名的。
在继续在ld文件的下面进行修改。这里就要特别注意了。
.Bootloader_Version : 是在main函数之前定义的数组的名字。
KEEP(*(.bootloader_version)) 是main函数之前定义的section()中的名字。
BOOTLOADERVERSION 尖括号后的是在前面的ld文件中添加的。
到这里 ,就能正常编译了。
完成
在cubeide中可以看到我们自己定义的部分。
在使用stm32cubeprogramm我们的mcu,查看flash到底有没有被我们更改。
可以看到,对应的flash位置已经被修改了。
附录
附录为完整的ld文件。
/**
******************************************************************************
* @file LinkerScript.ld
* @author Auto-generated by STM32CubeIDE
* @brief Linker script for STM32F407VETx Device from STM32F4 series
* 512Kbytes FLASH
* 64Kbytes CCMRAM
* 128Kbytes RAM
*
* Set heap size, stack size and stack location according
* to application requirements.
*
* Set memory bank area and size if external memory is used
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2020 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* Entry Point */
ENTRY(Reset_Handler)
/* Highest address of the user mode stack */
_estack = ORIGIN(RAM) + LENGTH(RAM); /* end of "RAM" Ram type memory */
_Min_Heap_Size = 0x200 ; /* required amount of heap */
_Min_Stack_Size = 0x400 ; /* required amount of stack */
/* Memories definition */
MEMORY
{
CCMRAM (xrw) : ORIGIN = 0x10000000, LENGTH = 64K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 512K
BOOTLOADERVERSION ( rx): ORIGIN =0x8007ff0, LENGTH = 10
m_programcodeendflag (RX) : ORIGIN = 0x8007fe0, LENGTH = 0x00000008
}
/* Sections */
SECTIONS
{
/* The startup code into "FLASH" Rom type memory */
.isr_vector :
{
. = ALIGN(4);
KEEP(*(.isr_vector)) /* Startup code */
. = ALIGN(4);
} >FLASH
/* The program code and other data into "FLASH" Rom type memory */
.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 into "FLASH" Rom type memory */
.rodata :
{
. = ALIGN(4);
*(.rodata) /* .rodata sections (constants, strings, etc.) */
*(.rodata*) /* .rodata* sections (constants, strings, etc.) */
. = ALIGN(4);
} >FLASH
.ARM.extab : {
. = ALIGN(4);
*(.ARM.extab* .gnu.linkonce.armextab.*)
. = ALIGN(4);
} >FLASH
.ARM : {
. = ALIGN(4);
__exidx_start = .;
*(.ARM.exidx*)
__exidx_end = .;
. = ALIGN(4);
} >FLASH
.preinit_array :
{
. = ALIGN(4);
PROVIDE_HIDDEN (__preinit_array_start = .);
KEEP (*(.preinit_array*))
PROVIDE_HIDDEN (__preinit_array_end = .);
. = ALIGN(4);
} >FLASH
.init_array :
{
. = ALIGN(4);
PROVIDE_HIDDEN (__init_array_start = .);
KEEP (*(SORT(.init_array.*)))
KEEP (*(.init_array*))
PROVIDE_HIDDEN (__init_array_end = .);
. = ALIGN(4);
} >FLASH
.fini_array :
{
. = ALIGN(4);
PROVIDE_HIDDEN (__fini_array_start = .);
KEEP (*(SORT(.fini_array.*)))
KEEP (*(.fini_array*))
PROVIDE_HIDDEN (__fini_array_end = .);
. = ALIGN(4);
} >FLASH
.Bootloader_Version :
{
. = ALIGN(4);
KEEP(*(.bootloader_version))
. = ALIGN(4);
} >BOOTLOADERVERSION
.program_code_end_flag :
{
KEEP(*(.programcodeendflag)) /* */
} > m_programcodeendflag
/* Used by the startup to initialize data */
_sidata = LOADADDR(.data);
/* Initialized data sections into "RAM" Ram type memory */
.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 into "RAM" Ram type memory */
. = ALIGN(4);
.bss :
{
/* This is used by the startup in order to initialize the .bss section */
_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" Ram type memory left */
._user_heap_stack :
{
. = ALIGN(8);
PROVIDE ( end = . );
PROVIDE ( _end = . );
. = . + _Min_Heap_Size;
. = . + _Min_Stack_Size;
. = ALIGN(8);
} >RAM
/* Remove information from the compiler libraries */
/DISCARD/ :
{
libc.a ( * )
libm.a ( * )
libgcc.a ( * )
}
.ARM.attributes 0 : { *(.ARM.attributes) }
}