[应用相关] 将函数加载到Flash或SRAM指定地址的方法

[复制链接]
3405|3
 楼主| 呐咯密密 发表于 2024-9-14 15:31 | 显示全部楼层 |阅读模式
Questions:AT32 部分型号有零等待闪存和非零等待闪存,程序在零等待闪存执行速度比在非零等待闪存执行速度快,如果有函数对执行速度有要求,可以将该函数加载到零等待区执行。当零等待闪存使用完后,如果还有函数对执行速度有要求,可以将该函数加载到 SRAM 执行,前提是SRAM 还有足够的空间存放该函数代码。
Answer: 有两种将某个函数加载到 Flash 或 SRAM 指定地址执行的方法,以 AT32F403A 为例:


1. 方法一:修改分散加载描述文件
在 Keil 中:修改分散加载描述文件*.sct(scatter file),在工程选项的 linker 页面中,选择*.sct 文件,编辑*.sct。
994466e53b38a766a.png
如果是要放在Flash零等待区指定地址,添加*.o(ZWROMCODE),自定义一个叫做ZWROMCODE的section。LR_IROM1 为零等待区,LR_IROM2 为非零等待区,设置如下:
  1. ; *************************************************************
  2. ; *** Scatter-Loading Description File generated by uVision ***
  3. ; *************************************************************
  4. LR_IROM1 0x08000000 0x00020000 { ; load region size_region
  5. ER_IROM1 0x08000000 0x00020000 { ; load address = execution address
  6. *.o (RESET, +First)
  7. *(InRoot$Sections)
  8. .ANY (+RO)
  9. *.o(ZWROMCODE)
  10. }
  11. RW_IRAM1 0x20000000 0x00038000 { ; RW data
  12. .ANY (+RW +ZI)
  13. }
  14. }
  15. LR_IROM2 0x08020000 0x000E0000 { ; load region size_region
  16. ER_IROM2 0x08020000 0x000E0000 { ; load address = execution address
  17. .ANY (+RO)
  18. }
  19. }
也可以根据需要单独分配一块零等待区 LR_IROM2,添加*.o(ZWROMCODE),自定义一个叫做ZWROMCODE 的 section。LR_IROM1 和 LR_IROM2 为零等待区,LR_IROM3 为非零等待区,设置如下:

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

  1. #pragma arm section code = “RAMCODE"
  2. void delay(unsigned char num)
  3. {
  4. while(num)
  5. {
  6. num--;
  7. }
  8. }
  9. void test(void)
  10. {
  11. for(;;)
  12. {
  13. led_gpio_port[led]->odt ^= led_gpio_pin[led];
  14. delay(10);
  15. }
  16. }
  17. #pragma arm section


 楼主| 呐咯密密 发表于 2024-9-14 15:33 | 显示全部楼层
在 IAR 中:在安装目录 IAR Systems\Embedded Workbench\arm\config\linker\ArteryTek 下找到相应 MCU型号的分散加载描述文件*.ICF,复制到 project 路径下,按下图方式启用*.icf 文件。
4206066e53bd378a8f.png
如果是要放在 Flash 零等待区指定地址,根据需要在零等待区分配一段 region,增加下面红色字体的部分,即可将 my_code.c 内的代码都放入该零等待区内(ZWROM_CODE_region):

  1. /*###ICF### Section handled by ICF editor, don't touch! ****/
  2. /*-Editor annotation file-*/
  3. /* IcfEditorFile="$TOOLKIT_DIR$\config\ide\IcfEditor\cortex_v1_0.xml" */
  4. /*-Specials-*/
  5. define symbol __ICFEDIT_intvec_start__ = 0x08000000;
  6. /*-Memory Regions-*/
  7. define symbol __ICFEDIT_region_ROM_start__ = 0x08000000;
  8. define symbol __ICFEDIT_region_ROM_end__ = 0x080FFFFF;
  9. /* ZMROM CODE area */
  10. define symbol __ICFEDIT_region_ZWROM_CODE_start__ = 0x08010000;
  11. define symbol __ICFEDIT_region_ZWROM_CODE_end__ = 0x0801FFFF;
  12. define symbol __ICFEDIT_region_RAM_start__ = 0x20000000;
  13. define symbol __ICFEDIT_region_RAM_end__ = 0x20017FFF;
  14. /*-Sizes-*/
  15. define symbol __ICFEDIT_size_cstack__ = 0x1000;
  16. define symbol __ICFEDIT_size_heap__ = 0x1000;
  17. /**** End of ICF editor section. ###ICF###*/
  18. define memory mem with size = 4G;
  19. /* Reserved 0x08010000 ~ 0x0801FFFF as ZWROM CODE area */
  20. define region ROM_region = mem:[from __ICFEDIT_region_ROM_start__ to
  21. __ICFEDIT_region_ROM_end__];
  22. -mem:[from
  23. __ICFEDIT_region_ZWROM_CODE_start__ to __ICFEDIT_region_ZWROM_CODE_end__];
  24. define region ZWROM_CODE_region = -mem:[from __ICFEDIT_region_ZWROM_CODE_start__
  25. to __ICFEDIT_region_ZWROM_CODE_end__];
  26. define region RAM_region = mem:[from __ICFEDIT_region_RAM_start__ to
  27. __ICFEDIT_region_RAM_end__];
  28. define block CSTACK with alignment = 8, size = __ICFEDIT_size_cstack__ { };
  29. define block HEAP with alignment = 8, size = __ICFEDIT_size_heap__ { };
  30. initialize by copy { readwrite };
  31. do not initialize { section .noinit };
  32. place at address mem:__ICFEDIT_intvec_start__ { readonly section .intvec };
  33. place in ROM_region { readonly };
  34. /* Place Code in ZWROM CODE */
  35. place in ZWROM_CODE_region {ro object my_code.o};
  36. place in RAM_region { readwrite,
  37. block CSTACK, block HEAP };
如果有多个.c 文件需要放入零等待区,place in ZWROM_CODE_region 可以如下书写:
  1. /* Place Code in ZWROM CODE */
  2. place in ZWROM_CODE_region {ro object my_code1.o, ro object my_code2.o, ro object
  3. my_code2.o};
如果是要放在 SRAM 指定地址,以 SRAM 扩大到 224KB 为例,根据需要在零等待区分配一段 region,增加下面红色框线内的部方式分,即可将 my_code.c 内的代码都放入该零等待区内(RAM_CODE_region)
  1. /*###ICF### Section handled by ICF editor, don't touch! ****/
  2. /*-Editor annotation file-*/
  3. /* IcfEditorFile="$TOOLKIT_DIR$\config\ide\IcfEditor\cortex_v1_0.xml" */
  4. /*-Specials-*/
  5. define symbol __ICFEDIT_intvec_start__ = 0x08000000;
  6. /*-Memory Regions-*/
  7. define symbol __ICFEDIT_region_ROM_start__ = 0x08000000;
  8. define symbol __ICFEDIT_region_ROM_end__ = 0x080FFFFF;
  9. define symbol __ICFEDIT_region_RAM_start__ = 0x20000000;
  10. define symbol __ICFEDIT_region_RAM_end__ = 0x20037FFF;
  11. define symbol __ICFEDIT_region_RAM_CODE_start__ = 0x20037000;
  12. define symbol __ICFEDIT_region_RAM_CODE_end__ = 0x20037FFF;
  13. /*-Sizes-*/
  14. define symbol __ICFEDIT_size_cstack__ = 0x1000;
  15. define symbol __ICFEDIT_size_heap__ = 0x1000;
  16. /**** End of ICF editor section. ###ICF###*/
  17. define memory mem with size = 4G;
  18. define region ROM_region = mem:[from __ICFEDIT_region_ROM_start__ to
  19. __ICFEDIT_region_ROM_end__];
  20. /* Reserved 0x20037000 ~ 0x20037FFF as ZWROM CODE area */
  21. define region RAM_region = mem:[from __ICFEDIT_region_RAM_start__ to
  22. __ICFEDIT_region_RAM_end__];
  23. -mem:[from
  24. __ICFEDIT_region_RAM_CODE_start__ to __ICFEDIT_region_RAM_CODE_end__];
  25. define region RAM_CODE_region = mem:[from __ICFEDIT_region_RAM_start__ to
  26. __ICFEDIT_region_RAM_end__];
  27. define block CSTACK with alignment = 8, size = __ICFEDIT_size_cstack__ { };
  28. define block HEAP with alignment = 8, size = __ICFEDIT_size_heap__ { };
  29. initialize by copy { readwrite };
  30. do not initialize { section .noinit };
  31. place at address mem:__ICFEDIT_intvec_start__ { readonly section .intvec };
  32. place in ROM_region { readonly };
  33. /* Place Code in RAM CODE */
  34. place in RAM_CODE_region {ro object my_code.o};
  35. place in RAM_region { readwrite,
  36. block CSTACK, block HEAP };
如果有多个.c 文件需要放入零等待区,place in ZWROM_CODE_region 可以如下书写:
  1. /* Place Code in RAM CODE */
  2. 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。

  1. __attribute__((section(".ramfunc")))
  2. void function_in_ram(void)
  3. {
  4. at32_led_toggle(LED2);
  5. delay_ms(100);
  6. at32_led_toggle(LED3);
  7. delay_ms(100);
  8. at32_led_toggle(LED4);
  9. delay_ms(100);
  10. }
在 *.ld 文件中,修改以下两个地方
1) 新增_ram_func_size 定义, 用来设定需要存放 ram function 的 ram size 大小,例程中定义为 4K;修改_estack 定义,_estack 是 STACK pointer 的初始值, 修改后是要把 stack pointer 初始值设定到 ram function 开始的地址之前, 避免 stack 空间跟 ram function 的空间重迭。
  1. /* Highest address of the user mode stack */
  2. //_estack = 0x20018000; /* end of RAM */
  3. _ram_function_size = 4K;
  4. _estack = 0x20018000 - _ram_function_size; /* end of RAM */
181966e53c7bdc359.png
2) 添加一段描述,将.ramfuc 这个 section 的起始地址指定到 SRAM 的最后 4K,*(.ramfunc)这个宣告代表所有用__attribute__((section(".ramfunc")))宣告的 function 都会被编排到这个区域.
  1. . = ALIGN(4);
  2. . = ORIGIN(RAM) + LENGTH(RAM) - _ram_function_size;
  3. _ramfunc = .;
  4. *(.ramfunc);
5402566e53c96d8681.png
照以上的方式修改之后,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 指定地址:
  1. <div>void Test(void) __attribute__((section(".ARM.__at_0x08001000"))); </div><div>void Test(void)
  2. {}</div>


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


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


您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

568

主题

4085

帖子

56

粉丝
快速回复 在线客服 返回列表 返回顶部