环境:IAR uCOS STM32(20K RAM大小)
今个再次遇见App_Fault_ISR,这次挺让我意外的,毕竟项目没加什么功能,只是使用了malloc堆上弄块内存,然后把地址给消息队列。当然,ucos有自己的内存管理文件,而且不会产生内存碎片,但脑子上来冲劲了,就想这么用,然问题就来了,日—-
问题描述
编译链接都安好,只要执行malloc就game over,如下这样子。
指针变量=(数据类型*)malloc(数据类型*N),
调试时看到,执行完上边就跳转到App_Fault_ISR异常。
分析
就当前的环境来说,也就内存分配出岔子了。问题是:就朝系统要了8个字节的堆内存。为了解心疑,任务栈size*2,结果也确实不是这块的事儿;继续查,,,,,最后定位到.icf文件 对系统堆区、栈区大小分配这一块。
由此引入正文
.icf文件
:
/*###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__ = 0x0803FFFF;
define symbol __ICFEDIT_region_RAM_start__ = 0x20000000;
define symbol __ICFEDIT_region_RAM_end__ = 0x2000FFFF;
/*-Sizes-*/
define symbol __ICFEDIT_size_cstack__ = 0x1000;
define symbol __ICFEDIT_size_heap__ = 0x200;
/**** 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_region = mem:[from __ICFEDIT_region_RAM_start__ to __ICFEDIT_region_RAM_end__];
define block CSTACK with alignment = 8, size = __ICFEDIT_size_cstack__ { };
define block HEAP with alignment = 8, size = __ICFEDIT_size_heap__ { };
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,
block CSTACK, block HEAP };
详情参见:stm32存储器结构、 IAR 开发环境下的.icf 文件、.map文件 解析
这里说说 堆、栈的大小设置:
堆区+栈区+其他=20K
其他:静态区+文字常量区等,要看划分种类
所以,堆区+栈区<20K,
类似molloc函数会在堆上非配内存,按应用场景划分大小,如不过分使用堆,给他小些,栈区大写,毕竟ucos用户任务+2个系统任务的任务栈也在这块呢。
另,IAR编译器 需要提前对 栈、堆进行划分。