跳转到主要内容

使用MCUXpresso IDE将数据、函数与文件存入指定位置

cathy 提交于

在进行MCU开发时,根据实际需要,将数据、函数与文件存入指定位置,对合理使用存储器的十分重要。经常有客户问如何将某一数据、函数或文件存入指定的地址空间,结合客户的问题,本文主要对此进行讲解。

<strong><font color="#004a85">构建工程后,代码与数据的默认存放位置</font> </strong>

以LPC54628中helloworld例程为例,开发环境:MCUXpresso IDE。

构建工程(Build)后,内存分配如以下console窗口所示:

<center><img src="http://mcu.eetrend.com/files/2021-07/wen_zhang_/100114763-213678-1.png&…; alt=“使用MCUXpresso IDE将数据、函数与文件存入指定位置"></center>

其中.text,.data,.bss,.dec与Flash,RAM的关系如下所示:

<center><img src="http://mcu.eetrend.com/files/2021-07/wen_zhang_/100114763-213679-2.png&…; alt=“使用MCUXpresso IDE将数据、函数与文件存入指定位置"></center>

<strong><font color="#004a85">自定义Flash与RAM分区</font> </strong>

为了将某一数据、函数或文件存入指定的地址空间,我们先要自定义分区。

打开工程属性设置界面,在MCU settings选项中分区出MY_FLASH与MY_RAM两个区用于测试,可以自定义这两个区的大小,如下所示:

<center><img src="http://mcu.eetrend.com/files/2021-07/wen_zhang_/100114763-213680-3.png&…; alt=“使用MCUXpresso IDE将数据、函数与文件存入指定位置"></center>

配置完Flash与RAM之后,点击Apply and Close后会在工程栏看到我们分区出来的Flash2和RAM2,如下所示:

<center><img src="http://mcu.eetrend.com/files/2021-07/wen_zhang_/100114763-213681-4.png&…; alt=“使用MCUXpresso IDE将数据、函数与文件存入指定位置"></center>

<strong><font color="#004a85">数据存入指定位置</font> </strong>

<strong>1) 变量与常量默认存放位置</strong>

我们先查看变量和数组的默认位置。例如:

初始化过的变量:uint16_t value1 = 1;

未初始化的数组:char data_buffer1[1024];

常量数组: const char data_buffer2[1024] = "hello nxp";

使用MCUXpressoIDE自带的Image Info窗口来查看数组存放位置:

<center><img src="http://mcu.eetrend.com/files/2021-07/wen_zhang_/100114763-213682-5.png&…; alt=“使用MCUXpresso IDE将数据、函数与文件存入指定位置"></center>

可读写变量和数组默认存放在名为“SRAM_UPPER”的RAM(0x20000000-0x20014000)中,const类型的数组存放在名为“PROGRAM_FLASH”的Flash(0x0-0x40000)中。

<strong>2)将指定的变量与常量存入指定位置</strong>

将数组存入自定义的Flash与RAM中,需要调用C语言中的

<pre style="overflow-x:auto; background-color:#e9e9e9;">__attribute__ ((section(#type#bank)))</pre>

例如 将数据放入Flash2的 .text中

<pre style="overflow-x:auto; background-color:#e9e9e9;">__attribute__ ((section("text_Flash2"".$Flash2"))) + 数据声明</pre>

官方已封装并定义到cr_section_macros.h中,__DATA(RAM2)将可读写数组放入RAM2的.data段,__RODATA(Flash2)指将只读数组放入Flash2的 .rodata段,即:

<pre style="overflow-x:auto; background-color:#e9e9e9;">__DATA(RAM2) char data_buffer3[1024];

__RODATA(Flash2) const chardata_buffer4[1024] = "hello nxp";</pre>

注意使用时要先包含头文件

<pre style="overflow-x:auto; background-color:#e9e9e9;">#include "cr_section_macros.h".</pre>

<center><img src="http://mcu.eetrend.com/files/2021-07/wen_zhang_/100114763-213683-6.png&…; alt=“使用MCUXpresso IDE将数据、函数与文件存入指定位置"></center>

全局变量和数组存放在名为“MY_RAM”的自定义RAM2(0x20014000-0x20028000)中,只读的const类型数组存放在名为“MY_FLASH”的自定义Flash2(0x40000-0x80000)中。

<strong><font color="#004a85">函数存入指定位置</font> </strong>

<strong>1)function默认放置位置</strong>

代码默认会放置在名为“PROGRAM_FLASH”的Flash(0x0-0x40000)中,定义函数

<pre style="overflow-x:auto; background-color:#e9e9e9;">int hello1(void)

{

return 1;

}</pre>

2)function存入指定位置

将函数存入指定Flash,需要调用C语言中的这个定义:

<pre style="overflow-x:auto; background-color:#e9e9e9;">__attribute__ ((section(#type#bank)))</pre>

如函数存入Flash2中,可以写作:

<pre style="overflow-x:auto; background-color:#e9e9e9;">__attribute__ ((section("text_Flash2"".$Flash2")))+函数声明</pre>

同样官方进行了封装,使用__TEXT(Flash2)+函数声明即可。

更改代码放置位置的方法如下所示,将代码存入名为“MY_FLASH”的自定义Flash(0x40000-0x80000)中,定义函数:

<pre style="overflow-x:auto; background-color:#e9e9e9;">__TEXT(Flash2) int hello2(void)
{
return 2;
}</pre>

<center><img src="http://mcu.eetrend.com/files/2021-07/wen_zhang_/100114763-213684-7.png&…; alt=“使用MCUXpresso IDE将数据、函数与文件存入指定位置"></center>

<strong><font color="#004a85">指定文件存放到指定位置</font> </strong>

当存在大量函数需要存入指定Flash时,使用__TEXT(Flash)的方法设置每一个函数就略显笨拙。
如需要某个C源文件中的所有函数放入指定Flash区域,只需将编译完成的.o文件放入指定Flash即可。
我们新分区出名为“MY_FLASH_O”的Flash3,在source文件夹下新建hello.c,编译生成hello.o,并配置Linker Script将hello.o放入新分区出的Flash中,如下:

<center><img src="http://mcu.eetrend.com/files/2021-07/wen_zhang_/100114763-213685-8.png&…; alt=“使用MCUXpresso IDE将数据、函数与文件存入指定位置"></center>
<center><img src="http://mcu.eetrend.com/files/2021-07/wen_zhang_/100114763-213686-9.png&…; alt=“使用MCUXpresso IDE将数据、函数与文件存入指定位置"></center>
<center><img src="http://mcu.eetrend.com/files/2021-07/wen_zhang_/100114763-213687-10.png…; alt=“使用MCUXpresso IDE将数据、函数与文件存入指定位置"></center>

本文涉及到的演示代码,可以<a target="_blank" href="https://www.nxpic.org.cn/module/forum/forum.php?mod=viewthread&amp;tid=…; textvalue="在NXP社区下载" tab="outerlink" data-linktype="2">在NXP社区下载</a>。

来源:<a href="https://mp.weixin.qq.com/s/cEXzIlEgZS89snz4w6Bouw">恩智浦MCU加油站</a&gt;
免责声明:本文为转载文章,转载此文目的在于传递更多信息,版权归原作者所有。本文所用视频、图片、文字如涉及作品版权问题,请联系小编进行处理(联系邮箱:cathy@eetrend.com)。