已解决,可以参考如下代码。
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include <fal.h>
#include <dfs_fs.h>
#include <dfs_posix.h>
#include "dfs_file.h"
#include <stdio.h>
#include <stdlib.h>
#define DBG_TAG "ota"
#define DBG_LVL DBG_INFO // DBG_LOG, DBG_INFO, DBG_WARNING, DBG_ERROR
#include <rtdbg.h> // this line must after -> #define DBG_TAG
#define DATA_PARTITION_NAME "download"
#define APP_PARTITION_NAME "app"
typedef void (*pFunction)(void);
static const struct fal_partition *dl_part = RT_NULL;
static char *recv_partition = APP_PARTITION_NAME;
pFunction JumpToApplication;
uint32_t JumpAddress;
rt_thread_t tidOTA;
void ota(void *parameter)
{
static struct stat stat_buf;
static struct dfs_fd fd;
uint32_t length;
uint32_t pos = 0;
char buffer[100];
if (stat("os.bin", &stat_buf) == 0)
{
LOG_I("find new firmware file os.bin!");
LOG_I("OS file size = %d", stat_buf.st_size);
/* find app partition */
if ((dl_part = fal_partition_find(recv_partition)) == RT_NULL)
{
LOG_E("APP partition (%s) find error!", recv_partition);
return;
}
/* check os size */
if (stat_buf.st_size > dl_part->len)
{
LOG_E("Firmware is too large! File size (%d), '%s' partition size (%d)", stat_buf.st_size, recv_partition, dl_part->len);
return;
}
/* erase app partition */
LOG_I("Start erase. Size (%d)", stat_buf.st_size);
if (fal_partition_erase(dl_part, 0, stat_buf.st_size) < 0)
{
LOG_E("Firmware download failed! Partition (%s) erase error!", dl_part->name);
return;
}
/* copy os file from download to app partition */
if (dfs_file_open(&fd, "/os.bin", O_RDONLY) < 0)
{
LOG_E("Open %s failed!", "/os.bin");
}
do
{
memset(buffer, 0, sizeof(buffer));
length = dfs_file_read(&fd, buffer, sizeof(buffer));
if (length > 0)
{
fal_partition_write(dl_part, pos, (uint8_t *)buffer, sizeof(buffer));
pos += length;
}
} while (length > 0);
dfs_file_close(&fd);
/* remove the os file */
if(dfs_file_unlink("os.bin") != 0)
{
LOG_E("Romove os.bin file fail!");
}
/* restart bootloader to jump to app */
LOG_I("OS file loaded successfully!");
LOG_W("About to restart...");
rt_thread_mdelay(100);
rt_hw_cpu_reset();
}
else
{
LOG_W("NOT find new firmware file!");
LOG_I("Start program execution...");
/* Test if user code is programmed starting from address "RT_APP_PART_ADDR" */
if (((*(__IO uint32_t *)RT_APP_PART_ADDR) & 0x2FFE0000) == 0x20000000)
{
/* Jump to user application */
JumpAddress = *(__IO uint32_t *)(RT_APP_PART_ADDR + 4);
JumpToApplication = (pFunction)JumpAddress;
/* Initialize user application's Stack Pointer */
__set_MSP(*(__IO uint32_t *)RT_APP_PART_ADDR);
JumpToApplication();
}
}
}
int ota_entry(void)
{
tidOTA = rt_thread_create("ota", ota, RT_NULL, 1024, 11, 10);
if (tidOTA != RT_NULL)
{
rt_thread_startup(tidOTA);
}
else
{
if (tidOTA)
rt_thread_delete(tidOTA);
return -RT_ERROR;
}
return RT_EOK;
}
看解读楼主用的fal进行分区的,这里简单说下使用fal操作流程:
app = fal_partition_find("app");
fal_partition_erase(app, addr, size);
down = fal_partition_find("download");
fal_partition_read(down, read_addr, buf, buf_size);
fal_partition_write(app, write_addr, buf, buf_size);
谢谢,问题已解决。