STM32CubeIDE 把固件信息直接定位在指定的ROM位置

一般我们实现把固件信息写到文件最后,都是通过工具去添加的,但是这样对于一些需求修改频繁的场景,这样显得相当的繁琐。而后续查找资料发现可以使用__attribute__((at(地址(0x8000000或0x2000000))关键字来解决。但是发现我们使用的是CubeIDE,使用的是GCC编译器,无法识别at。所以最后参考资料,没有发现专门有这样使用的方法,然后自己摸索后发现这样修改可以实现。需要修改STM32FXXX_FLASH.ld文件即可实现。看不懂这个文件的内容可以参考如下链接解释的相当详细

https://blog.csdn.net/Cui_Hongwei/article/details/104108044

 

首先我们需要在程序中这样书写

const char Appware_Name[] 		__attribute__((section(".app_name"))) = "SMCV300C001-EG";					//32个字节
const char Appware_Version[] 	__attribute__((section(".app_version"))) = "V1.0.0";						//32个字节
const char Compiler_Data[] 		__attribute__((section(".compiler_data"))) = "Date: "__DATE__;				//32个字节
const char Compiler_Time[] 		__attribute__((section(".compiler_time"))) = "Time: "__TIME__;				//32个字节

然后再进入STM32FXXX_FLASH.ld文件,再这个位置添加入下代码

这里得ORIGIN对应的就是我们需要把版本信息定位在ROM的哪个位置,比如我这里是100K减去128字节的位置,然后其他的就每32个字节弄一个,名字可以自己编写。

然后再这个位置添加如下代码

这里添加后,就可以和.c文件中定义的对应上了。并且还可以再图形现实中看到实际使用的大小

我把文件贴出来方便大家查考

/**
 ******************************************************************************
 * @file      LinkerScript.ld
 * @author    Auto-generated by STM32CubeIDE
 * @brief     Linker script for STM32F105RCTx Device from STM32F1 series
 *                      256Kbytes FLASH
 *                      64Kbytes 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>&copy; 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 = 0x800 ;	/* required amount of heap  */
_Min_Stack_Size = 0x1000 ;	/* required amount of stack */

/* Memories definition */
MEMORY
{
  RAM    (xrw)    : ORIGIN = 0x20000000,   LENGTH = 64K
  FLASH    (rx)    : ORIGIN = 0x8000000,   LENGTH = 256K
  APPNAME    (rx)    : ORIGIN = 0x8018F80,   LENGTH = 32
  APPVERSION    (rx)    : ORIGIN = 0x8018FA0,   LENGTH = 32
  COMPILERDATA    (rx)    : ORIGIN = 0x8018FC0,   LENGTH = 32
  COMPILERTIME    (rx)    : ORIGIN = 0x8018FE0,   LENGTH = 32
}

/* 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
  
  /*Vesrion Info*/
    .app_name :
  {
    . = ALIGN(4);
    KEEP(*(.app_name)) 		
    . = ALIGN(4);
  } >APPNAME
  
    .app_version :
  {
    . = ALIGN(4);
    KEEP(*(.app_version)) 	
    . = ALIGN(4);
  } >APPVERSION 
  
    .compiler_data :
  {
    . = ALIGN(4);
    KEEP(*(.compiler_data)) 	
    . = ALIGN(4);
  } >COMPILERDATA
  
    .compiler_time :
  {
    . = ALIGN(4);
    KEEP(*(.compiler_time)) 	
    . = ALIGN(4);
  } >COMPILERTIME
  

  /* 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 */
    *(.RamFunc)        /* .RamFunc sections */
    *(.RamFunc*)       /* .RamFunc* 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) }
}

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值