IAR 的ICF文件之新建一个BLOCK

本文详细介绍了如何使用IAR编译器的icf文件进行存储空间的配置,包括定义ROM、RAM区域,设置堆栈大小,以及如何在ROM区域中定义额外的区块。同时,还展示了如何在代码中指定特定变量或段的存储位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在IAR编译环境设置存储空间是通过“*.icf”文件来完成的。

icf文件中:region > block > section

修改前:

/*###ICF### Section handled by ICF editor, don't touch! ****/
/*-Editor annotation file-*/
/* IcfEditorFile="$TOOLKIT_DIR$\config\ide\IcfEditor\cortex_v1_0.xml" */
/*-Specials-*/
define symbol __ICFEDIT_intvec_start__ = 0x08000000;
/*-Memory Regions-*/
define symbol __ICFEDIT_region_ROM_start__ = 0x08000000;
define symbol __ICFEDIT_region_ROM_end__   = 0x0807FFFF;
define symbol __ICFEDIT_region_RAM_start__ = 0x20000000;
define symbol __ICFEDIT_region_RAM_end__   = 0x2001FFFF;
/*-Sizes-*/
define symbol __ICFEDIT_size_cstack__ = 0x8000;
define symbol __ICFEDIT_size_heap__   = 0x0000;
/**** End of ICF editor section. ###ICF###*/

define memory mem with size = 4G;
define region ROM_region   = mem:[from __ICFEDIT_region_ROM_start__   to __ICFEDIT_region_ROM_end__];
define region RAM_region   = mem:[from __ICFEDIT_region_RAM_start__   to __ICFEDIT_region_RAM_end__];

define block CSTACK    with alignment = 8, size = __ICFEDIT_size_cstack__   { };
define block HEAP      with alignment = 8, size = __ICFEDIT_size_heap__     { };


initialize by copy { readwrite };
do not initialize  { section .noinit };

place at address mem:__ICFEDIT_intvec_start__ { readonly section .intvec };

place in ROM_region   { readonly };
place in RAM_region   { readwrite,
                        block CSTACK, block HEAP };

export symbol __ICFEDIT_region_RAM_start__;
export symbol __ICFEDIT_region_RAM_end__;

修改后:

/*###ICF### Section handled by ICF editor, don't touch! ****/
/*-Editor annotation file-*/
/* IcfEditorFile="$TOOLKIT_DIR$\config\ide\IcfEditor\cortex_v1_0.xml" */
/*-Specials-*/
define symbol __ICFEDIT_intvec_start__ = 0x08000000;
/*-Memory Regions-*/
define symbol __ICFEDIT_region_ROM_start__ = 0x08000000;
define symbol __ICFEDIT_region_ROM_end__   = 0x0807FFFF;
define symbol __ICFEDIT_region_RAM_start__ = 0x20000000;
define symbol __ICFEDIT_region_RAM_end__   = 0x2001FFFF;
/*-Sizes-*/
define symbol __ICFEDIT_size_cstack__ = 0x8000;
define symbol __ICFEDIT_size_heap__   = 0x0000;
define symbol __ICFEDIT_size_myblock__   = 0x1000;
/**** End of ICF editor section. ###ICF###*/

define memory mem with size = 4G;
define region ROM_region   = mem:[from __ICFEDIT_region_ROM_start__   to __ICFEDIT_region_ROM_end__];
define region RAM_region   = mem:[from __ICFEDIT_region_RAM_start__   to __ICFEDIT_region_RAM_end__];

define block CSTACK    with alignment = 8, size = __ICFEDIT_size_cstack__   { };
define block HEAP      with alignment = 8, size = __ICFEDIT_size_heap__     { };
define block MYBLOCK    with alignment = 8, size = __ICFEDIT_size_myblock__     { };


initialize by copy { readwrite };
do not initialize  { section .noinit };

place at address mem:__ICFEDIT_intvec_start__ { readonly section .intvec };

place in ROM_region   { readonly, block MYBLOCK };
place in RAM_region   { readwrite,
                        block CSTACK, block HEAP };

export symbol __ICFEDIT_region_RAM_start__;
export symbol __ICFEDIT_region_RAM_end__;

说明:修改的地方有三个

define symbol __ICFEDIT_size_myblock__   = 0x1000;    //设置BLOCK的大小

define block MYBLOCK    with alignment = 8, size = __ICFEDIT_size_myblock__     { };    //定义BLOCK对象,并设置对齐方式

place in ROM_region   { readonly, block MYBLOCK };    //将BLOCK放入ROM区

这样你在用IAR编译代码的时候,编译器就会自动为你分配一块大小为 0x1000名为“MYBLOCK”的ROM块了。

你还可以在MYBLOCK中再定义一个section,只需要:

define block MYBLOCK    with alignment = 8, size = __ICFEDIT_size_module__     { readonly section mysection };

那么在MYBLOCK块中,有包含了一块名为“mysection”的段了

在代码中:

#pragma section="mysection"
#define MODULE_BEGIN    (__segment_begin("mysection"))
#define MODULE_END    (__segment_end("mysection"))

int myparameter @ "mysection" = 0; // 变量 myparameter 就被放在 “MYBLOCK”块中的 “mysection”段里了。

是不是想问这样做有什么好处呢?看下一篇IAR 实现类linux驱动模块框架module_init(init_fun)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值