Bootloader升级APP
从串口接收到hex文件的一行,校验后调用以下函数写入FLASH。以下是简单的一个写入流程
@ CSDN Tyrion.Mon
void UpgradeAPP(xxx)
{
uint16_t i = 0;
uint16_t line_data_len = xxx; //数据域长度
static uint32_t base_addr = 0;
uint32_t offset_addr = 0x0000ffff & xxx; //偏移地址
uint32_t write_addr = 0;
static uint32_t has_erase_page = 0;
uint32_t need_erase_page = 0;
static bool APP_StartAddr_OK = false;
uint8_t line_type = xxx;
switch (line_type) //行数据类型
{
case 4: //指定基地址
base_addr = xxx;
base_addr = (base_addr << 16) & 0xffff0000;
【解锁FLASH】
break;
case 0: //写入Flash的数据
write_addr = base_addr | offset_addr;
if (APP_StartAddr_OK == false && ((APP_LOADED_ADDR + 0x08000000) == write_addr))
{
APP_StartAddr_OK = true; //APP Flash首地址正确
}
if (APP_StartAddr_OK == true)
{
for (i = 0; i < line_data_len; i += 4)
{
need_erase_page = ((write_addr + i) - 0x08000000) / 2048; //算出当前地址所在页,2K每页
if (need_erase_page != has_erase_page)
{
has_erase_page = need_erase_page;
【擦除该页】
}
【向指定地址以整字方式写入FLASH,或者接收到一页的量后采用页编程方式写入】
【读出对比】
printf("%x, ", *((volatile uint32_t *)(write_addr + i)));
}
printf("\r\n");
}
break;
case 1: //文件结束
【FLASH上锁】
break;
default:
break;
}
}
|