在嵌入式軟件開發(fā)領(lǐng)域,MCU芯片軟件的架構(gòu)設(shè)計(jì)與內(nèi)存布局的精細(xì)規(guī)劃對(duì)系統(tǒng)性能和穩(wěn)定性起著關(guān)鍵作用。本文檔聚焦于IAR Embedded Workbench環(huán)境下,為自研MCU芯片軟件提供了一套詳盡的函數(shù)和變量指定section放置方法與操作流程,兼具過程記錄與詳細(xì)說明,旨在打造一份實(shí)用的參考指南,助力開發(fā)者精準(zhǔn)掌控程序的內(nèi)存分布與執(zhí)行邏輯。文檔涵蓋從默認(rèn)section表的介紹,到多種放置手段的闡釋,以及實(shí)際配置示例的展示,為后續(xù)的開發(fā)工作奠定堅(jiān)實(shí)基礎(chǔ)。
IAR Embedded Workbench作為一款廣受認(rèn)可的嵌入式開發(fā)工具,具備豐富的功能與靈活的配置選項(xiàng)。在該環(huán)境下,軟件開發(fā)者可巧妙運(yùn)用多種方法,將函數(shù)和變量精準(zhǔn)放置于指定的section中。這一操作對(duì)于優(yōu)化程序的內(nèi)存使用效率、提升系統(tǒng)響應(yīng)速度以及增強(qiáng)代碼的可維護(hù)性具有重要意義。例如,通過將特定的代碼或數(shù)據(jù)放置在合適的內(nèi)存區(qū)域,可以充分利用MCU芯片的硬件特性,實(shí)現(xiàn)更高效的緩存利用、減少內(nèi)存訪問延遲等效果。
文檔深入淺出地講解了多種放置方式,包括使用@操作符、#pragma location命令、GCC風(fēng)格的attribute屬性以及#pragma default_variable_attributes和#pragma default_function_attributes命令等,開發(fā)者可根據(jù)實(shí)際需求靈活選擇。同時(shí),還提供了諸如as32x601_rom.icf、Port_MemMap.h和Port.c等實(shí)際配置示例,涵蓋了從內(nèi)存區(qū)域定義、section分配到函數(shù)與變量屬性設(shè)置的完整流程,為開發(fā)者提供了直觀且易于實(shí)踐的參考。
默認(rèn)section表
IAR Embedded Workbench中有很多默認(rèn)的section用于放置對(duì)應(yīng)的變量和函數(shù):
除了用于您的應(yīng)用程序的ELF部分之外,這些工具還出于多種目的使用許多其他ELF段:
- 以.debug開頭的段通常包含DWARF格式的調(diào)試信息。
- 以.iar.debug開頭的段包含IAR格式的補(bǔ)充調(diào)試信息
- 以.comment開頭的段包含用于構(gòu)建文件的工具和命令行
- 以.rel或.rela開頭的段包含ELF重定位信息
- 以.symtab開頭的段包含文件的符號(hào)表
- 以.strtab開頭的段包含符號(hào)表中符號(hào)的名稱
- 以.shstrtab開頭的段包含各段的名稱。
將變量放到指定的section
使用@操作符
可以使用 @ 將變量放到指定的section:
staticuint32_t TaskCounter @".mcal_const_cfg" = 1;
使用 #pragma location 命令
可以使用 #pragma location命令將變量放到指定的section:
#pragma location = ".mcal_const_cfg"staticuint32_t TaskCounter = 1;
使用 GCC 風(fēng)格的屬性 attribute ((section ))
可以使用 GCC 風(fēng)格的屬性 attribute ((section ))將變量放到指定的section:
staticuint32_t TaskCounter attribute ((section (".mcal_const_cfg"))) = 1;
使用 #pragma default_variable_attributes 命令
上面的方法可以將單個(gè)變量放到指定的section,如果需要將多個(gè)變量放到指定的section,上面的方法會(huì)顯得有點(diǎn)繁瑣。可以使用 #pragma default_variable_attributes 命令將多個(gè)變量放到指定的section:
#pragma default_variable_attributes = @ ".mcal_const_cfg"staticuint32_t TaskCounter = 1; staticuint32_t TaskLedRedCounter = 2; #pragma default_variable_attributes =
將函數(shù)放到指定的section
使用@操作符
可以使用 @ 將函數(shù)放到指定的section:
voidStartTaskLedRed(void *argument) @ ".mcal_text";
使用 #pragma location 命令
可以使用 #pragma location命令將函數(shù)放到指定的section:
#pragma location = ".mcal_text"voidStartTaskLedRed(void *argument);
使用 GCC 風(fēng)格的屬性 attribute ((section ))
可以使用 GCC 風(fēng)格的屬性 attribute ((section ))將函數(shù)放到指定的section:
voidStartTaskLedRed(void *argument) attribute ((section (".mcal_text")));
使用 #pragma default_variable_attributes 命令
上面的方法可以將單個(gè)函數(shù)放到指定的section,如果需要將多個(gè)函數(shù)放到指定的section,上面的方法會(huì)顯得有點(diǎn)繁瑣。可以使用 #pragma default_function_attributes命令將多個(gè)函數(shù)放到指定的section:
#pragma default_function_attributes = @ ".mcal_text"voidStartTaskLedRed(void *argument); voidStartTaskLedGreen(void *argument); voidStartTaskLedBlue(void *argument); #pragma default_function_attributes =
使用示例
as32x601_rom.icf
/****************************************************************************** * FILE VERSION / define exported symbol _link_file_version_2 = 1; / * SPECIALS // * * MEMORY REGIONS / define symbol ICFEDIT_region_FLASH_start = 0x10000000; define symbol ICFEDIT_region_FLASH_end = 0x11FFFFFF; define symbol ICFEDIT_region_SRAM0_start = 0x20000000; define symbol ICFEDIT_region_SRAM0_end = 0x2001FFFF; define symbol ICFEDIT_region_SRAM1_start = 0x20020000; define symbol ICFEDIT_region_SRAM1_end = 0x2003FFFF; define symbol ICFEDIT_region_SRAM2_start = 0x20040000; define symbol ICFEDIT_region_SRAM2_end = 0x2005FFFF; define symbol ICFEDIT_region_SRAM3_start = 0x20060000; define symbol ICFEDIT_region_SRAM3_end = 0x2007FFFF; / * * SIZES / define symbol ICFEDIT_size_cstack = 0x2000; define symbol ICFEDIT_size_proc_stack = 0x0; define symbol ICFEDIT_size_heap = 0x2000; / * * BUILD FOR ROM *****************************************************************************/ keep symbol __iar_cstart_init_gp; define memory mem with size = 4G; define region ROM_region = mem:[from ICFEDIT_region_FLASH_start to ICFEDIT_region_FLASH_end ]; define region RAMCODE_region = mem:[from ICFEDIT_region_SRAM0_start to ICFEDIT_region_SRAM0_end ]; define region RAM_region = mem:[from ICFEDIT_region_SRAM1_start to ICFEDIT_region_SRAM1_end ] | mem:[from ICFEDIT_region_SRAM2_start to ICFEDIT_region_SRAM2_end ]; initialize by copy { readwrite }; do not initialize { section .noinit }; define block CSTACK with alignment = 16, size = CSTACK_SIZE { }; define block HEAP with alignment = 16, size = HEAP_SIZE { }; define block RW_DATA { rw section .data}; define block RW_DATA_INIT { ro section .data_init}; define block RW_BSS {rw section .bss}; define block RW_DATA_ALL with static base GPREL { block RW_DATA, block RW_BSS }; "STARTUP" : place at start of ROM_region { readonly section .init }; place in ROM_region { readonly, block RW_DATA_INIT }; place in ROM_region { readonly section .text, section .mcal_text, section .access_code_rom}; place in ROM_region { readonly section .rodata, section .mcal_const_cfg, section .mcal_const, section .mcal_const_no_cacheable}; place in RAMCODE_region { readwrite section .text, section .ramcode, block RW_DATA_ALL }; place in RAM_region { readwrite, block CSTACK, block HEAP }; place in RAM_region { section .mcal_data, section .dma_dest_buffer, section .mcal_shared_data }; place in RAM_region { section .mcal_bss, section .mcal_bss_no_cacheable, section .dma_dest_buffer_bss, section .mcal_shared_bss };
第13、14行:定義FLASH,起始地址和結(jié)束地址。 第41行:定義了ROM_region區(qū)域,起始地址和結(jié)束地址。 第63行:定義一個(gè)名為 ROM_region 的內(nèi)存區(qū)域,并將三個(gè)只讀代碼段 .text、.mcal_text 和 .access_code_rom 放置在這個(gè)區(qū)域內(nèi)。。
Port_MemMap.h
#define MEMMAP_MISSMATCH_CHECKER#if defined (_IAR_C_AS32x601_)#ifdef PORT_START_SEC_CODE#undef PORT_START_SEC_CODE#undef MEMMAP_MISSMATCH_CHECKER#pragma default_function_attributes = @ ".mcal_text"#endif#ifdef PORT_STOP_SEC_CODE#undef PORT_STOP_SEC_CODE#undef MEMMAP_MISSMATCH_CHECKER#pragma default_function_attributes =#endif#endif#ifdef MEMMAP_MISSMATCH_CHECKER#error"MemMap.h, No valid section define found."#endif
第1行:定義了一個(gè)宏 MEMMAP_MISSMATCH_CHECKER,用于檢查包含的正確的符號(hào)。 第3行:定義了一個(gè)ifdef,當(dāng)定義了 IAR_C_AS32x601 時(shí),會(huì)執(zhí)行下面的代碼。 第5行:定義了一個(gè)ifdef,當(dāng)定義了 PORT_START_SEC_CODE 時(shí),會(huì)執(zhí)行下面的代碼。 第11行:定義了一個(gè)ifdef,當(dāng)定義了 PORT_STOP_SEC_CODE 時(shí),會(huì)執(zhí)行下面的代碼。 第19行:定義了一個(gè)ifdef,當(dāng)定義了 MEMMAP_MISSMATCH_CHECKER 時(shí),說明輸入定義錯(cuò)誤,將會(huì)拋出一個(gè)錯(cuò)誤。
Port.c
#define PORT_START_SEC_CODE#include"Port_MemMap.h" FUNC(void, PORT_CODE) Port_Init(P2CONST(Port_ConfigType, AUTOMATIC, PORT_APPL_CONST) ConfigPtr) { #if (PORT_ENABLE_DEV_ERROR_DETECT == STD_ON)/* When PostBuild is used and #(Variants) > 1, the input parameter 'ConfigPtr' is mandatory to be different than NULL_PTR. * In case of error, return immediately and report DET errors. */#if (PORT_PRECOMPILE_SUPPORT == STD_ON)if (NULL_PTR != ConfigPtr) { #elseif (NULL_PTR == ConfigPtr) { #endif/* (PORT_PRECOMPILE_SUPPORT == STD_ON) *//* If development error detection for the Port module is enabled: * The function shall raise the error PORT_E_INIT_FAILED if the parameter ConfigPtr is Null Pointer.*/ (void)Det_ReportError((uint16)PORT_MODULE_ID, PORT_INSTANCE_ID, PORT_INIT_ID, PORT_E_INIT_FAILED); } else { #endif/* (PORT_ENABLE_DEV_ERROR_DETECT == STD_ON) */#if (PORT_PRECOMPILE_SUPPORT == STD_ON) l_PortConfig_ptr = &Port_PreCompileConfig_st; /* Avoid compiler warning */ (void)ConfigPtr; #else/* (PORT_PRECOMPILE_SUPPORT == STD_OFF) */ l_PortConfig_ptr = ConfigPtr; #endif/* (PORT_PRECOMPILE_SUPPORT == STD_ON) *//* Initializes the Port driver with the given configuration */ Port_LLDriver_Init(l_PortConfig_ptr); #if (PORT_ENABLE_DEV_ERROR_DETECT == STD_ON) } #endif/* (PORT_ENABLE_DEV_ERROR_DETECT == STD_ON) */ } #define PORT_STOP_SEC_CODE#include"Port_MemMap.h"
第1行:定義了一個(gè)宏 PORT_START_SEC_CODE,將會(huì)執(zhí)行#pragma default_function_attributes = @ ".mcal_text",會(huì)將函數(shù)放置在.mcal_text區(qū)域。 第32行:定義了一個(gè)宏 PORT_STOP_SEC_CODE,會(huì)結(jié)束函數(shù)的默認(rèn)屬性設(shè)置。
效果示例
審核編輯 黃宇
-
mcu
+關(guān)注
關(guān)注
146文章
17824瀏覽量
360124 -
IAR
+關(guān)注
關(guān)注
5文章
369瀏覽量
37233
發(fā)布評(píng)論請(qǐng)先 登錄
自研MCU芯片閃存驅(qū)動(dòng)的實(shí)現(xiàn):OpenOCD詳細(xì)過程記錄與操作指南

IAR全面支持芯馳科技車規(guī)MCU芯片E3650
如何將項(xiàng)目從IAR遷移到Embedded Studio

IAR與紫光同芯合作,全面支持THA6系列汽車芯片
IAR Systems最新版開發(fā)環(huán)境全面賦能芯海科技32位MCU芯片

IAR支持旗芯微車規(guī)級(jí)MCU,助力汽車行業(yè)智能化發(fā)展
比亞迪最快于11月實(shí)現(xiàn)自研算法量產(chǎn),推進(jìn)智駕芯片自研進(jìn)程
【GD32 MCU 入門教程】一、GD32 MCU 開發(fā)環(huán)境搭建(3)使用 Embedded Builder 開發(fā) GD32

IAR與芯科集成攜手,賦能中國(guó)汽車行業(yè)RISC-V MCU創(chuàng)新研發(fā)
?IAR全面支持芯科集成CX3288系列車規(guī)RISC-V MCU

芯科集成與IAR展開生態(tài)合作,IAR全面支持CX3288系列車規(guī)MCU
IAR全面支持芯馳科技E3系列車規(guī)MCU產(chǎn)品E3119/E3118

評(píng)論