c - gnu 链接描述文件 - stm32f4xx - 全局变量

标签 c gnu stm32 stm32f4discovery linker-scripts

我想让内存映射(部分的开始和结束地址)在我的固件中可用:示例:我想知道 user_data 部分的起点(0x8800000)以及它的长度(512K) .我曾经在一个例子中看到过,可以在链接描述文件中定义全局变量,然后固件可以访问它(extern unsigned long GLOBAL_VAR_USER_SECTION_BEGIN)。有人可以帮助我在哪里以及如何声明全局变量吗?

链接描述文件内存映射

MEMORY
{
RAM (xrw)      : ORIGIN = 0x20000000, LENGTH = 128K
CCMRAM (rw)    : ORIGIN = 0x10000000, LENGTH = 64K
/* BL (rx)         : ORIGIN = 0x8000000,  LENGTH = 32K */
FLASH (rx)     : ORIGIN = 0x8000000,  LENGTH = 512K
USER_DATA (rw) : ORIGIN = 0x8800000,  LENGTH = 512K /* user data section of 512K */
}

链接脚本

/*
*****************************************************************************
**
**  File        : LinkerScript.ld
**
**  Abstract    : Linker script for STM32F407VGTx Device with
**                1024KByte FLASH, 128KByte RAM
**
**                Set heap size, stack size and stack location according
**                to application requirements.
**
**                Set memory bank area and size if external memory is used.
**
**  Target      : STMicroelectronics STM32
**
**
**  Distribution: The file is distributed as is, without any warranty
**                of any kind.
**
**  (c)Copyright Ac6.
**  You may use this file as-is or modify it according to the needs of your
**  project. Distribution of this file (unmodified or modified) is not
**  permitted. Ac6 permit registered System Workbench for MCU users the
**  rights to distribute the assembled, compiled & linked contents of this
**  file as part of an application binary file, provided that it is built
**  using the System Workbench for MCU toolchain.
**
*****************************************************************************
*/

/* Entry Point */
ENTRY(Reset_Handler)

/* Highest address of the user mode stack */
_estack = 0x20020000;    /* end of RAM */

/* Generate a link error if heap and stack don't fit into RAM */
_Min_Heap_Size = 0x0;    /* required amount of heap  : heap is not used */
_Min_Stack_Size = 0x400; /* required amount of stack : 1K minimal stack size */

/* Specify the memory areas                                     */
/* (xrw) : execute, read, write                                 */
/* Flash: 1M, RAM 128K, CCMRAM: 64K                             */
/* Examples: xK == x'000 * 1.024 B : 128K == 131'072 (0x20'000) */
MEMORY
{
RAM (xrw)      : ORIGIN = 0x20000000, LENGTH = 128K
CCMRAM (rw)    : ORIGIN = 0x10000000, LENGTH = 64K
/* BL (rx)         : ORIGIN = 0x8000000,  LENGTH = 32K */
FLASH (rx)     : ORIGIN = 0x8000000,  LENGTH = 512K
USER_DATA (rw) : ORIGIN = 0x8800000,  LENGTH = 512K /* user data section of 512K */
}

/* 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) */
    *(.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 goes into FLASH */
  .rodata :
  {
    . = ALIGN(4);
    *(.rodata)         /* .rodata sections (constants, strings, etc.) */
    *(.rodata*)        /* .rodata* sections (constants, strings, etc.) */
    . = ALIGN(4);
  } >FLASH

  .ARM.extab   : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH
  .ARM : {
    __exidx_start = .;
    *(.ARM.exidx*)
    __exidx_end = .;
  } >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 (*(SORT(.fini_array.*)))
    KEEP (*(.fini_array*))
    PROVIDE_HIDDEN (__fini_array_end = .);
  } >FLASH

  /* used by the startup to initialize data */
  _sidata = LOADADDR(.data);

  /* Initialized data sections goes into RAM, load LMA copy after code */
  .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

  _siccmram = LOADADDR(.ccmram);

  /* CCM-RAM section 
  * 
  * IMPORTANT NOTE! 
  * If initialized variables will be placed in this section,
  * the startup code needs to be modified to copy the init-values.  
  */
  .ccmram :
  {
    . = ALIGN(4);
    _sccmram = .;       /* create a global symbol at ccmram start */
    *(.ccmram)
    *(.ccmram*)

    . = ALIGN(4);
    _eccmram = .;       /* create a global symbol at ccmram end */
  } >CCMRAM AT> FLASH

  /* user data section : 3 different names!                                 */
  /* this places every code with this sections attribute to this section    */ 
  /*                                                                        */
  /* __attribute__((section(".USER_Data"))) int placed_logic()              */
  /* {                                                                      */
  /*   TODO - Add your application code here                                */
  /*   return 1;                                                            */
  /* }                                                                      */
  .user_data :
  {
    *(.USER_Data*);
  } > USER_DATA

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

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

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

  .ARM.attributes 0 : { *(.ARM.attributes) }
}

最佳答案

您可以从 make 中生成内存和链接描述 rune 件,然后在编译器命令行上使用变量来定义常量(即 -D 选项)。或者您甚至可以生成一个充满 #define 的头文件:

内存表:

RAM_START = 1234
RAM_LENGTH = 5678

生成文件:

include mem.tbl

define MEM_DESC =
MEMORY
{
   RAM (xrw)      : ORIGIN = $(RAM_START), LENGTH = $(RAM_LENGTH)
   CCMRAM (rw)    : ORIGIN = 0x10000000, LENGTH = 64K
   /* BL (rx)         : ORIGIN = 0x8000000,  LENGTH = 32K */
   FLASH (rx)     : ORIGIN = 0x8000000,  LENGTH = 512K
   USER_DATA (rw) : ORIGIN = 0x8800000,  LENGTH = 512K /* user data section of 512K */
}
endef

define MEM_INCLUDE =
#define RAM_START $(RAM_START)
#define RAM_LENGTH $(RAM_LENGTH)
endef


all: mem.map mem.h
    @echo use the header file in your compilation
    @cat mem.h
    @echo and the memory map in your linking
    @cat mem.map

mem.h : mem.tbl
    $(file > mem.h,$(MEM_INCLUDE))

mem.map: mem.tbl
    $(file > mem.map,$(MEM_DESC))

或者您更进一步并使用 the GNUmake table toolkit它提供了从 make 内部构建管理的功能。它在编写时考虑了多平台和交叉开发/嵌入式,并提供算术、表格处理和其他方便的东西。

关于c - gnu 链接描述文件 - stm32f4xx - 全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47074801/

相关文章:

ubuntu - 在 ubuntu 中替代 niceload 命令?

assembly - GCC 和 STM32 的堆栈帧不正确

c - 消息发送,接收者未收到消息。套接字编程

c - 错误 : ld. 所以:无法预加载来自 LD_PRELOAD 的对象 'getpid.so':忽略

c - 我可以将char * [20]转换为char [] [20]吗?

c - C 中的链表插入

C++ Eclipse CDT 如何添加gcc 4.6.3

使用 makefile 编译 header 依赖项

stm32 - 有STM32直连CAN收发器的CAN总线库吗?

c - STM32F103 Timer2不中断