在嵌入式軟件開發領域,MCU芯片軟件的架構設計與內存布局的精細規劃對系統性能和穩定性起著關鍵作用。本文檔聚焦于IAR Embedded Workbench環境下,為自研MCU芯片軟件提供了一套詳盡的函數和變量指定section放置方法與操作流程,兼具過程記錄與詳細說明,旨在打造一份實用的參考指南,助力開發者精準掌控程序的內存分布與執行邏輯。文檔涵蓋從默認section表的介紹,到多種放置手段的闡釋,以及實際配置示例的展示,為后續的開發工作奠定堅實基礎。
IAR Embedded Workbench作為一款廣受認可的嵌入式開發工具,具備豐富的功能與靈活的配置選項。在該環境下,軟件開發者可巧妙運用多種方法,將函數和變量精準放置于指定的section中。這一操作對于優化程序的內存使用效率、提升系統響應速度以及增強代碼的可維護性具有重要意義。例如,通過將特定的代碼或數據放置在合適的內存區域,可以充分利用MCU芯片的硬件特性,實現更高效的緩存利用、減少內存訪問延遲等效果。
文檔深入淺出地講解了多種放置方式,包括使用@操作符、#pragma location命令、GCC風格的attribute屬性以及#pragma default_variable_attributes和#pragma default_function_attributes命令等,開發者可根據實際需求靈活選擇。同時,還提供了諸如as32x601_rom.icf、Port_MemMap.h和Port.c等實際配置示例,涵蓋了從內存區域定義、section分配到函數與變量屬性設置的完整流程,為開發者提供了直觀且易于實踐的參考。
默認section表
IAR Embedded Workbench中有很多默認的section用于放置對應的變量和函數:
除了用于您的應用程序的ELF部分之外,這些工具還出于多種目的使用許多其他ELF段:
- 以.debug開頭的段通常包含DWARF格式的調試信息。
- 以.iar.debug開頭的段包含IAR格式的補充調試信息
- 以.comment開頭的段包含用于構建文件的工具和命令行
- 以.rel或.rela開頭的段包含ELF重定位信息
- 以.symtab開頭的段包含文件的符號表
- 以.strtab開頭的段包含符號表中符號的名稱
- 以.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 風格的屬性 attribute ((section ))
可以使用 GCC 風格的屬性 attribute ((section ))將變量放到指定的section:
staticuint32_t TaskCounter attribute ((section (".mcal_const_cfg"))) = 1;
使用 #pragma default_variable_attributes 命令
上面的方法可以將單個變量放到指定的section,如果需要將多個變量放到指定的section,上面的方法會顯得有點繁瑣。可以使用 #pragma default_variable_attributes 命令將多個變量放到指定的section:
#pragma default_variable_attributes = @ ".mcal_const_cfg"staticuint32_t TaskCounter = 1; staticuint32_t TaskLedRedCounter = 2; #pragma default_variable_attributes =
將函數放到指定的section
使用@操作符
可以使用 @ 將函數放到指定的section:
voidStartTaskLedRed(void *argument) @ ".mcal_text";
使用 #pragma location 命令
可以使用 #pragma location命令將函數放到指定的section:
#pragma location = ".mcal_text"voidStartTaskLedRed(void *argument);
使用 GCC 風格的屬性 attribute ((section ))
可以使用 GCC 風格的屬性 attribute ((section ))將函數放到指定的section:
voidStartTaskLedRed(void *argument) attribute ((section (".mcal_text")));
使用 #pragma default_variable_attributes 命令
上面的方法可以將單個函數放到指定的section,如果需要將多個函數放到指定的section,上面的方法會顯得有點繁瑣。可以使用 #pragma default_function_attributes命令將多個函數放到指定的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,起始地址和結束地址。 第41行:定義了ROM_region區域,起始地址和結束地址。 第63行:定義一個名為 ROM_region 的內存區域,并將三個只讀代碼段 .text、.mcal_text 和 .access_code_rom 放置在這個區域內。。
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行:定義了一個宏 MEMMAP_MISSMATCH_CHECKER,用于檢查包含的正確的符號。 第3行:定義了一個ifdef,當定義了 IAR_C_AS32x601 時,會執行下面的代碼。 第5行:定義了一個ifdef,當定義了 PORT_START_SEC_CODE 時,會執行下面的代碼。 第11行:定義了一個ifdef,當定義了 PORT_STOP_SEC_CODE 時,會執行下面的代碼。 第19行:定義了一個ifdef,當定義了 MEMMAP_MISSMATCH_CHECKER 時,說明輸入定義錯誤,將會拋出一個錯誤。
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行:定義了一個宏 PORT_START_SEC_CODE,將會執行#pragma default_function_attributes = @ ".mcal_text",會將函數放置在.mcal_text區域。 第32行:定義了一個宏 PORT_STOP_SEC_CODE,會結束函數的默認屬性設置。
效果示例
審核編輯 黃宇
-
mcu
+關注
關注
146文章
17718瀏覽量
358155 -
IAR
+關注
關注
5文章
367瀏覽量
37123
發布評論請先 登錄
相關推薦
IAR全面支持芯馳科技車規MCU芯片E3650
IAR與紫光同芯合作,全面支持THA6系列汽車芯片
IAR Systems最新版開發環境全面賦能芯海科技32位MCU芯片

IAR支持旗芯微車規級MCU,助力汽車行業智能化發展
比亞迪最快于11月實現自研算法量產,推進智駕芯片自研進程
IAR與芯科集成攜手,賦能中國汽車行業RISC-V MCU創新研發
?IAR全面支持芯科集成CX3288系列車規RISC-V MCU

芯科集成與IAR展開生態合作,IAR全面支持CX3288系列車規MCU
IAR全面支持芯馳科技E3系列車規MCU產品E3119/E3118

評論