打印
[应用相关]

将函数加载到Flash或SRAM指定地址的方法

[复制链接]
237|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
Questions:AT32 部分型号有零等待闪存和非零等待闪存,程序在零等待闪存执行速度比在非零等待闪存执行速度快,如果有函数对执行速度有要求,可以将该函数加载到零等待区执行。当零等待闪存使用完后,如果还有函数对执行速度有要求,可以将该函数加载到 SRAM 执行,前提是SRAM 还有足够的空间存放该函数代码。
Answer: 有两种将某个函数加载到 Flash 或 SRAM 指定地址执行的方法,以 AT32F403A 为例:


1. 方法一:修改分散加载描述文件
在 Keil 中:修改分散加载描述文件*.sct(scatter file),在工程选项的 linker 页面中,选择*.sct 文件,编辑*.sct。

如果是要放在Flash零等待区指定地址,添加*.o(ZWROMCODE),自定义一个叫做ZWROMCODE的section。LR_IROM1 为零等待区,LR_IROM2 为非零等待区,设置如下:
; *************************************************************
; *** Scatter-Loading Description File generated by uVision ***
; *************************************************************
LR_IROM1 0x08000000 0x00020000 { ; load region size_region
ER_IROM1 0x08000000 0x00020000 { ; load address = execution address
*.o (RESET, +First)
*(InRoot$Sections)
.ANY (+RO)
*.o(ZWROMCODE)
}
RW_IRAM1 0x20000000 0x00038000 { ; RW data
.ANY (+RW +ZI)
}
}
LR_IROM2 0x08020000 0x000E0000 { ; load region size_region
ER_IROM2 0x08020000 0x000E0000 { ; load address = execution address
.ANY (+RO)
}
}
也可以根据需要单独分配一块零等待区 LR_IROM2,添加*.o(ZWROMCODE),自定义一个叫做ZWROMCODE 的 section。LR_IROM1 和 LR_IROM2 为零等待区,LR_IROM3 为非零等待区,设置如下:

; *************************************************************
; *** Scatter-Loading Description File generated by uVision ***
; *************************************************************
LR_IROM1 0x08000000 0x00010000 { ; load region size_region
ER_IROM1 0x08000000 0x00010000 { ; load address = execution address
*.o (RESET, +First)
*(InRoot$Sections)
.ANY (+RO)
}
RW_IRAM1 0x20000000 0x00038000 { ; RW data
.ANY (+RW +ZI)
}
}
LR_IROM2 0x08010000 0x00010000 { ; load region size_region
ER_IROM2 0x08010000 0x00010000 { ; load address = execution address ZW
*.o(ZWROMCODE)
}
}
LR_IROM3 0x08020000 0x000E0000 { ; load region size_region
ER_IROM3 0x08020000 0x000E0000 { ; load address = execution address NZW
.ANY (+RO)
}
}
如果是要放在 SRAM 指定地址,根据需要分配 RW_IRAM2 大小,添加*.o(RAMCODE),自定义一个叫做RAMCODE 的 section。设置如下,以 SRAM 扩大到 224KB 为例,RW_IRAM1 大小 196KB,RW_IRAM2大小 28KB。
; *************************************************************
; *** Scatter-Loading Description File generated by uVision ***
; *************************************************************
LR_IROM1 0x08000000 0x000C0000 { ; load region size_region
ER_IROM1 0x08000000 0x000C0000 { ; load address = execution address
*.o (RESET, +First)
*(InRoot$Sections)
.ANY (+RO)
}
RW_IRAM1 0x20000000 0x00031000 { ; RW data
.ANY (+RW +ZI)
}
RW_IRAM2 0x20031000 0x00007000 { ; RW data
*.o(RAMCODE)
}
}
在需要加载到 Flash 或 SRAM 指定地址中的函数前,用__attribute__((section("SECTION_NAME")))声明该函数放在该 section 中,SECTION_NAME 可以自行命名,如下面是使用 RAMCODE。如果有多个函数,那么每个函数前面都需要加__attribute__((section("RAMCODE")))。设置如下:
__attribute__((section("RAMCODE")))
void delay(unsigned char num)
{
while(num)
{
num--;
}
}
__attribute__((section("RAMCODE")))
void test(void)
{
for(;;)
{
led_gpio_port[led]->odt ^= led_gpio_pin[led];
delay(10);
}
}
也可以将所有需要加载的函数放在以#pragma arm section code = “RAMCODE” 开头,以#pragma arm section 结尾的中间,声明所有函数放在 RAMCODE section 中。设置如下:

#pragma arm section code = “RAMCODE"
void delay(unsigned char num)
{
while(num)
{
num--;
}
}
void test(void)
{
for(;;)
{
led_gpio_port[led]->odt ^= led_gpio_pin[led];
delay(10);
}
}
#pragma arm section


使用特权

评论回复
沙发
呐咯密密|  楼主 | 2024-9-14 15:33 | 只看该作者
在 IAR 中:在安装目录 IAR Systems\Embedded Workbench\arm\config\linker\ArteryTek 下找到相应 MCU型号的分散加载描述文件*.ICF,复制到 project 路径下,按下图方式启用*.icf 文件。

如果是要放在 Flash 零等待区指定地址,根据需要在零等待区分配一段 region,增加下面红色字体的部分,即可将 my_code.c 内的代码都放入该零等待区内(ZWROM_CODE_region):

/*###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__ = 0x080FFFFF;
/* ZMROM CODE area */
define symbol __ICFEDIT_region_ZWROM_CODE_start__ = 0x08010000;
define symbol __ICFEDIT_region_ZWROM_CODE_end__ = 0x0801FFFF;
define symbol __ICFEDIT_region_RAM_start__ = 0x20000000;
define symbol __ICFEDIT_region_RAM_end__ = 0x20017FFF;
/*-Sizes-*/
define symbol __ICFEDIT_size_cstack__ = 0x1000;
define symbol __ICFEDIT_size_heap__ = 0x1000;
/**** End of ICF editor section. ###ICF###*/
define memory mem with size = 4G;
/* Reserved 0x08010000 ~ 0x0801FFFF as ZWROM CODE area */
define region ROM_region = mem:[from __ICFEDIT_region_ROM_start__ to
__ICFEDIT_region_ROM_end__];
-mem:[from
__ICFEDIT_region_ZWROM_CODE_start__ to __ICFEDIT_region_ZWROM_CODE_end__];
define region ZWROM_CODE_region = -mem:[from __ICFEDIT_region_ZWROM_CODE_start__
to __ICFEDIT_region_ZWROM_CODE_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 Code in ZWROM CODE */
place in ZWROM_CODE_region {ro object my_code.o};
place in RAM_region { readwrite,
block CSTACK, block HEAP };
如果有多个.c 文件需要放入零等待区,place in ZWROM_CODE_region 可以如下书写:
/* Place Code in ZWROM CODE */
place in ZWROM_CODE_region {ro object my_code1.o, ro object my_code2.o, ro object
my_code2.o};
如果是要放在 SRAM 指定地址,以 SRAM 扩大到 224KB 为例,根据需要在零等待区分配一段 region,增加下面红色框线内的部方式分,即可将 my_code.c 内的代码都放入该零等待区内(RAM_CODE_region)
/*###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__ = 0x080FFFFF;
define symbol __ICFEDIT_region_RAM_start__ = 0x20000000;
define symbol __ICFEDIT_region_RAM_end__ = 0x20037FFF;
define symbol __ICFEDIT_region_RAM_CODE_start__ = 0x20037000;
define symbol __ICFEDIT_region_RAM_CODE_end__ = 0x20037FFF;
/*-Sizes-*/
define symbol __ICFEDIT_size_cstack__ = 0x1000;
define symbol __ICFEDIT_size_heap__ = 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__];
/* Reserved 0x20037000 ~ 0x20037FFF as ZWROM CODE area */
define region RAM_region = mem:[from __ICFEDIT_region_RAM_start__ to
__ICFEDIT_region_RAM_end__];
-mem:[from
__ICFEDIT_region_RAM_CODE_start__ to __ICFEDIT_region_RAM_CODE_end__];
define region RAM_CODE_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 Code in RAM CODE */
place in RAM_CODE_region {ro object my_code.o};
place in RAM_region { readwrite,
block CSTACK, block HEAP };
如果有多个.c 文件需要放入零等待区,place in ZWROM_CODE_region 可以如下书写:
/* Place Code in RAM CODE */
place in RAM_CODE_region {ro object my_code1.o, ro object my_code2.o, ro object my_code2.o};


使用特权

评论回复
板凳
呐咯密密|  楼主 | 2024-9-14 15:34 | 只看该作者
在 AT32 IDE 中: 在代码文件中,将需要放到SRAM的function添加__attribute__((section(".ramfunc")))宣告, 将这些 function 全部放到 .ramfunc 这个 section,这个 section 的名称可自行定义,例程这里定义为 .ramfunc。

__attribute__((section(".ramfunc")))
void function_in_ram(void)
{
at32_led_toggle(LED2);
delay_ms(100);
at32_led_toggle(LED3);
delay_ms(100);
at32_led_toggle(LED4);
delay_ms(100);
}
在 *.ld 文件中,修改以下两个地方
1) 新增_ram_func_size 定义, 用来设定需要存放 ram function 的 ram size 大小,例程中定义为 4K;修改_estack 定义,_estack 是 STACK pointer 的初始值, 修改后是要把 stack pointer 初始值设定到 ram function 开始的地址之前, 避免 stack 空间跟 ram function 的空间重迭。
/* Highest address of the user mode stack */
//_estack = 0x20018000; /* end of RAM */
_ram_function_size = 4K;
_estack = 0x20018000 - _ram_function_size; /* end of RAM */

2) 添加一段描述,将.ramfuc 这个 section 的起始地址指定到 SRAM 的最后 4K,*(.ramfunc)这个宣告代表所有用__attribute__((section(".ramfunc")))宣告的 function 都会被编排到这个区域.
. = ALIGN(4);
. = ORIGIN(RAM) + LENGTH(RAM) - _ram_function_size;
_ramfunc = .;
*(.ramfunc);

照以上的方式修改之后,compiler 编译过后就会把这些 function 的 code 编译备份在 flash 的区域,然后每次上电跑起来时,启动文件 startup_at32xxx.s 会把这些 function 的 code,从 flash 里 copy 到指定的 SRAM地址。

使用特权

评论回复
地板
呐咯密密|  楼主 | 2024-9-14 15:36 | 只看该作者
2. 方法二:只对某些编译器有效比如 Keil,在需要加载到 Flash 或 SRAM 指定地址中的函数前,用__attribute__((section(".ARM.__at_address")))声明该函数放在该 section 中,address 根据需要设置。如果有多个函数,那么每个函数前面都需要加__attribute__((section(".ARM.__at_address")))。 将函数加载到 Flash 指定地址:
<div>void Test(void) __attribute__((section(".ARM.__at_0x08001000"))); </div><div>void Test(void)
{}</div>


将函数加载到 SRAM 指定地址
<div> void Test(void) __attribute__((section(".ARM.__at_0x20001000"))); </div><div>void Test(void)
{} </div>


类型:MCU 应用
适用型号:AT32 全系列
主功能:FLASH、SRAM
次功能:无


使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

认证:苏州澜宭自动化科技嵌入式工程师
简介:本人从事磁编码器研发工作,负责开发2500线增量式磁编码器以及17位、23位绝对值式磁编码器,拥有多年嵌入式开发经验,精通STM32、GD32、N32等多种品牌单片机,熟练使用单片机各种外设。

458

主题

3563

帖子

46

粉丝