根据网上搜索到的方法,把 代码放到 ram中执行,但是一直未能把rt-thread 整个内核放到ram中, context_iar.s 一直在flash中,导致编译出来的bin文件十分的巨大(因为bin文件中,有很多地方的填充 0xff)。
看考文献:1. 【实战经验】IAR下如何让程序在RAM中运行 - STM32/STM8单片机论坛 - ST MCU意法半导体官方技术支持论坛 - 21ic电子技术开发论坛
2. IAR 下将函数指定到RAM中_leumber的博客-优快云博客_iar_data_init3
按照以上的方法都未能把 context_iar.s放到ram中。
经过后面察看 iar IDE中配套的EWARM_DevelopmentGuide.ENU.pdf。打开路径如下:
根据在 Manual initialization 章节中
步骤一:编写链接 icf。制定 code 放到 ram的那个位置。
步骤二:在rt-thread 系统components.c中,编写代码,实现 code 搬运到ram中。
调用代码:
步骤三:修改 context_iar.s 段名 .text 改为 MYOVERLAY1,跟 ICF定义的对应。(注意段名千万不要用 "." 开头,可能是IAR的bug。放.开头就是不行)
经过以上步骤,就实现把 context_iar.s带到ram中。结果如下:
未改之前:
改了之后:
根据以上两个图片,可以看到,context_iar.s 代码放到了ram中。
补充,整个内核代码放到ram中,通过工程配置:
由于components.c中用到了 rt-thread 初始化启动 宏,建议代码还是放到 .text中
此是实现方法可能比较 笨重,没有这么灵活,如有更多方法,请各位大佬分享。
方便复制:
#include <string.h>
#pragma section = "MyOverlay"
#pragma section = "MyOverlay1InRom"
void SwitchToOverlay1(void)
{
char *target = __section_begin("MyOverlay");
char *source = __section_begin("MyOverlay1InRom");
char *source_end = __section_end("MyOverlay1InRom");
memcpy(target, source, source_end-source);
}
**************************************************************************************************************************************************************************
/*###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_CODE_start__ = 0x20000000;
define symbol __ICFEDIT_region_RAM_CODE_end__ = 0x20003FFF;
define symbol __ICFEDIT_region_RAM_start__ = 0x20004000;
define symbol __ICFEDIT_region_RAM_end__ = 0x2000FEFF;
define symbol __ICFEDIT_region_Noinit_RAM_start__ = 0x2000FF00;
define symbol __ICFEDIT_region_Noinit_RAM_end__ = 0x2000FFFF;
/*-Sizes-*/
define symbol __ICFEDIT_size_cstack__ = 0x0400;
define symbol __ICFEDIT_size_heap__ = 0x000;
/**** 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_Code_region = mem:[from __ICFEDIT_region_RAM_CODE_start__ to __ICFEDIT_region_RAM_CODE_end__];
define region RAM_region = mem:[from __ICFEDIT_region_RAM_start__ to __ICFEDIT_region_RAM_end__];
define region RAM_Noinit_region = mem:[from __ICFEDIT_region_Noinit_RAM_start__ to __ICFEDIT_region_Noinit_RAM_end__];
define block CSTACK with alignment = 8, size = __ICFEDIT_size_cstack__ { };
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, last block CSTACK};
place in RAM_Noinit_region { readwrite section .noinit};
define overlay MyOverlay { section MYOVERLAY1};
initialize manually { section MYOVERLAY*};
define block MyOverlayInRom { section MYOVERLAY1_init};
place in RAM_Code_region { overlay MyOverlay };
place in ROM_region { block MyOverlayInRom};