如何在IAR Embedded Workbench中把变量和函数放到指定的section

Technical Note 27498

Architectures:

ARM

Component:

linker

Updated:

2023/7/24 4:37

在嵌入式软件中,为了更有效地使用内存,有时需要把变量和函数放到指定的section。IAR Embedded Workbench中提供了丰富的机制来控制变量和函数在内存中的放置。本文主要介绍如何在IAR Embedded Workbench把变量和函数放到指定的section。

默认section

IAR Embedded Workbench中有很多默认的section用于放置对应的变量和函数:

将变量放到指定的section

使用 @ 操作符

可以使用 @ 将变量放到指定的section:

static uint32_t TaskCounter @"MY_DATA" = 1;

使用 #pragma location 命令

可以使用 #pragma location命令将变量放到指定的section:

#pragma location = "MY_DATA"
static uint32_t TaskCounter = 1;

使用 GCC 风格的属性 __attribute__ ((section ))

可以使用 GCC 风格的属性 __attribute__ ((section ))将变量放到指定的section:

static uint32_t TaskCounter __attribute__ ((section ("MY_DATA"))) = 1;

使用 #pragma default_variable_attributes 命令

上面的方法可以将单个变量放到指定的section,如果需要将多个变量放到指定的section,上面的方法会显得有点繁琐。可以使用 #pragma default_variable_attributes 命令将多个变量放到指定的section:

#pragma default_variable_attributes = @ "MY_DATA"
 
static uint32_t TaskCounter = 1;
static uint32_t TaskLedRedCounter = 2;
 
#pragma default_variable_attributes =

将函数放到指定的section

使用 @ 操作符

可以使用 @ 将函数放到指定的section:

void StartTaskLedRed(void *argument) @ "MY_CODE";

使用 #pragma location 命令

可以使用 #pragma location命令将函数放到指定的section:

#pragma location = "MY_CODE"
void StartTaskLedRed(void *argument);

使用 GCC 风格的属性 __attribute__ ((section ))

可以使用 GCC 风格的属性 __attribute__ ((section ))将函数放到指定的section:

void StartTaskLedRed(void *argument) __attribute__ ((section ("MY_CODE")));

使用 #pragma default_function_attributes 命令

上面的方法可以将单个函数放到指定的section,如果需要将多个函数放到指定的section,上面的方法会显得有点繁琐。可以使用 #pragma default_function_attributes命令将多个函数放到指定的section:

#pragma default_function_attributes = @ "MY_CODE"

void StartTaskLedRed(void *argument);
void StartTaskLedGreen(void *argument);
void StartTaskLedBlue(void *argument);

#pragma default_function_attributes =

总结

本文主要介绍了如何在IAR Embedded Workbench把变量和函数放到指定的section。

 

参考文献:

  1. IAR C/C++ Development Guide (Controlling data and function placement in memory)
  2. IAR C/C++ Development Guide (Pragma directives)
  3. IAR C/C++ Development Guide (Supported GCC attributes)

We do no longer support Internet Explorer. To get the best experience of iar.com, we recommend upgrading to a modern browser such as Chrome or Edge.