rt-thead、SPI flash、FAL分区、AT框架 CPU通过ESP8266 AT指令使用AT框架连接http服务器,下载文件过程中发生内存不足的错误打印 1、造成这个报警原因:(1)spi flash单数据线,写入速率较慢。(2)对比以前通过NB IOT升级没问题的原因,也可能是WIFI的速率太快,系统缓存不过来 2、查看http_ota_fw_download函数源码,OTA先获取文件大小,再擦除相就的大小flash的空间,而且提示信息也是先有擦除消息打印,再有内存不足打印 3、将flash擦除移到HTTP通讯的前面,先擦除再通讯,问题解决
static int http_ota_fw_download(const char* uri)
{
int ret = 0, resp_status;
int file_size = 0, length, total_length = 0;
rt_uint8_t *buffer_read = RT_NULL;
struct webclient_session* session = RT_NULL;
const struct fal_partition * dl_part = RT_NULL;
/* Get download partition information and erase download partition data */
if ((dl_part = fal_partition_find("fm_area")) == RT_NULL)
{
LOG_E("Firmware download failed! Partition (%s) find error!", "fm_area");
ret = -RT_ERROR;
goto __exit;
}
LOG_I("Start erase flash (%s) partition!", dl_part->name);
if (fal_partition_erase(dl_part, 0, dl_part->len) < 0)
{
LOG_E("Firmware download failed! Partition (%s) erase error!", dl_part->name);
ret = -RT_ERROR;
goto __exit;
}
LOG_I("Erase flash (%s) partition success!", dl_part->name);
/* create webclient session and set header response size */
session = webclient_session_create(GET_HEADER_BUFSZ);
if (!session)
{
LOG_E("open uri failed.");
ret = -RT_ERROR;
goto __exit;
}
/* send GET request by default header */
if ((resp_status = webclient_get(session, uri)) != 200)
{
LOG_E("webclient GET request failed, response(%d) error.", resp_status);
ret = -RT_ERROR;
goto __exit;
}
file_size = webclient_content_length_get(session);
rt_kprintf("http file_size:%d\n",file_size);
if (file_size == 0)
{
LOG_E("Request file size is 0!");
ret = -RT_ERROR;
goto __exit;
}
else if (file_size < 0)
{
LOG_E("webclient GET request type is chunked.");
ret = -RT_ERROR;
goto __exit;
}
buffer_read = web_malloc(HTTP_OTA_BUFF_LEN);
if (buffer_read == RT_NULL)
{
LOG_E("No memory for http ota!");
ret = -RT_ERROR;
goto __exit;
}
memset(buffer_read, 0x00, HTTP_OTA_BUFF_LEN);
LOG_I("OTA file size is (%d)", file_size);
do
{
length = webclient_read(session, buffer_read, file_size - total_length > HTTP_OTA_BUFF_LEN ?
HTTP_OTA_BUFF_LEN : file_size - total_length);
if (length > 0)
{
/* Write the data to the corresponding partition address */
if (fal_partition_write(dl_part, total_length, buffer_read, length) < 0)
{
LOG_E("Firmware download failed! Partition (%s) write data error!", dl_part->name);
ret = -RT_ERROR;
goto __exit;
}
total_length += length;
print_progress(total_length, file_size);
}
else
{
LOG_E("Exit: server return err (%d)!", length);
ret = -RT_ERROR;
goto __exit;
}
} while(total_length != file_size);
ret = RT_EOK;
if (total_length == file_size)
{
if (session != RT_NULL)
{
webclient_close(session);
session = RT_NULL;
}
if (buffer_read != RT_NULL)
{
web_free(buffer_read);
buffer_read = RT_NULL;
}
LOG_I("Download firmware to flash success.");
LOG_I("System now will restart...");
rt_thread_delay(rt_tick_from_millisecond(5));
/* Reset the device, Start new firmware */
extern void rt_hw_cpu_reset(void);
rt_hw_cpu_reset();
}
__exit:
if (session != RT_NULL)
webclient_close(session);
if (buffer_read != RT_NULL)
web_free(buffer_read);
return ret;
}
|