问题描述:
在使用ESP8266创建任务时,由于使用了system_os_task以及system_os_post来创建与执行任务。下载运行程序后系统无限重启。
错误信息:Fatal exception 29(StoreProhibitedCause):
epc1=0x4023310a, epc2=0x00000000, epc3=0x00000000, excvaddr=0x00000000, depc=0x00000000 ?
ets Jan 8 2013,rst cause:2, boot mode:(3,6)
分析过程:
1. 使用system_os_post函数时就会出现一直重启的问题。并打印“Fatal exception 29(StoreProhibitedCause):
epc1=0x4023310a, epc2=0x00000000, epc3=0x00000000, excvaddr=0x00000000, depc=0x00000000 ?
ets Jan 8 2013,rst cause:2, boot mode:(3,6)”错误。
于是猜想可能是system_os_post函数问题,但是在使用了其他代码(有任务创建与调用的代码,即使用了system_os_task和system_os_post)不会出现这样系统重启的问题。
2. 注释任务函数中的代码,发现系统不再重启。后面在仔细查看任务函数发现定义了较大的数组变量。
uint8_t josn_buf[100] = {0};
uint8_t report_msg[256] = {0};
uint8_t* p_index = josn_buf;
uint8_t* last_index = NULL;
uint8_t temp_index = 0;
static struct REPORT_MSG_STRUCT{
uint8_t power_sw_sta :1;
uint8_t sw1_sta :1;
uint8_t sw2_sta :1;
uint8_t msg_report_flag :1;
uint8_t sw1_cur_sta: 1;
uint8_t sw2_cur_sta :1;
uint8_t reserve :2;
}report_msg_struct = {0,0,0,0,0,0,0};
3. 进一步查找问题,将这些大数组注释。发现ESP8266一切正常。猜想可能是是栈溢出了。
栈溢出解决方法:
解决:将栈设置得更大些或者使用堆空间来代码这部分局部变量(用malloc函数分配内存)。
4. 通过屏蔽大法,一部分一部分的注释代码,慢慢的查找定位问题时,又推翻了前面的猜想。跟局部变量定义没有关系
5. 最后仔细看代码,发现是使用了空指针导致的。以下为出问题的代码:
do
{
last_index = p_index;
p_index = os_strchr(p_index+1,',');
}
while (p_index != NULL);
*p_index = '\0'; //添加字符串结束标志;
解释:因为while后面,p_index一定是为NULL的而*p_index表示对NULL的内容操作。即操作空指针,这样就会导致程序奔溃
修改为正确代码:
do
{
last_index = p_index;
p_index = os_strchr(p_index+1,',');
}
while (p_index != NULL);
*last_index = '\0'; //添加字符串结束标志;
串口打印的完整的Log信息:
System started ...
mode : sta(e0:98:06:0e:e3:14)
add if0
scandone
Fatal exception 29(StoreProhibitedCause):
epc1=0x4023310a, epc2=0x00000000, epc3=0x00000000, excvaddr=0x00000000, depc=0x00000000 ?
ets Jan 8 2013,rst cause:2, boot mode:(3,6)
load 0x40100000, len 29068, room 16
tail 12
chksum 0x6d
ho 0 tail 12 room 4
load 0x3ffe8000, len 2456, room 12
tail 12
chksum 0xdc
ho 0 tail 12 room 4
load 0x3ffe89a0, len 5156, room 12
tail 8
chksum 0x49
csum 0x49