Use the serial port iap method to burn the initial firmware under the bootloader

This commit is contained in:
wgzAIIT 2023-04-24 10:24:56 +08:00
parent cfadebc3e8
commit 3d16b694dd
4 changed files with 67 additions and 16 deletions

View File

@ -385,10 +385,10 @@ static void OtaCmd(void)
{
ota_info.down.size = size;
ota_info.down.crc32= calculate_crc32(DOWN_FLAH_ADDRESS, size);
ota_info.down.version = 0x11223344;
ota_info.down.version = ota_info.os.version + 1;
strncpy(ota_info.down.description, "OTA Test!",sizeof(ota_info.down.description));
ota_info.status = OTA_STATUS_READY;
strncpy(ota_info.error_message, "Download the firmware to the download partition successfully!",sizeof(ota_info.error_message));
strncpy(ota_info.error_message, "No error message!",sizeof(ota_info.error_message));
UpdateOTAFlag(&ota_info);
}
else

View File

@ -149,8 +149,8 @@ void UpdateApplication(void)
// 从Flash中读取OTA信息
memcpy(&ota_info, (const void *)FLAG_FLAH_ADDRESS,sizeof(ota_info_t));
// 如果OTA升级状态为准备状态可以进行升级
if(ota_info.status == OTA_STATUS_READY)
// 如果OTA升级状态为准备状态且APP分区与download分区版本不同,才可以进行升级
if((ota_info.status == OTA_STATUS_READY) && (ota_info.os.crc32 != ota_info.down.crc32))
{
Serial_PutString("\r\n------Start to update the app!------\r\n");
// 校验downlad分区固件CRC
@ -159,7 +159,27 @@ void UpdateApplication(void)
ota_info.status = OTA_STATUS_UPDATING;
UpdateOTAFlag(&ota_info);
// 拷贝download分区到XiUOS System分区
// 1.如果CRC校验通过,开始升级,逐字节拷贝Flash,先备份当前XiUOS System分区内容
status = flash_copy(XIUOS_FLAH_ADDRESS, BAKUP_FLAH_ADDRESS, ota_info.os.size);
if((status == kStatus_Success) &&(calculate_crc32(BAKUP_FLAH_ADDRESS, ota_info.os.size) == ota_info.os.crc32))
{
Serial_PutString("\r\n------Backup app success!------\r\n");
ota_info.bak.size = ota_info.os.size;
ota_info.bak.crc32 = ota_info.os.crc32;
ota_info.bak.version = ota_info.os.version;
strncpy(ota_info.bak.description, ota_info.os.description, sizeof(ota_info.os.description));
UpdateOTAFlag(&ota_info);
}
else
{
Serial_PutString("\r\n------Backup app failed!------\r\n");
ota_info.status = OTA_STATUS_ERROR;
strncpy(ota_info.error_message, "Backup app failed!",sizeof(ota_info.error_message));
UpdateOTAFlag(&ota_info);
goto finish;
}
// 2.拷贝download分区到XiUOS System分区
status = flash_copy(DOWN_FLAH_ADDRESS, XIUOS_FLAH_ADDRESS, ota_info.down.size);
if((status == kStatus_Success) &&(calculate_crc32(XIUOS_FLAH_ADDRESS, ota_info.down.size) == ota_info.down.crc32))
{
@ -200,6 +220,5 @@ void UpdateApplication(void)
goto finish;
}
finish:
// 跳转到应用程序
jump_to_application();
return;
}

View File

@ -21,6 +21,25 @@
#ifdef MCUBOOT_BOOTLOADER
extern void ImxrtMsDelay(uint32 ms);
static void InitialVersion(void)
{
int32_t size;
ota_info_t ota_info;
memset(&ota_info, 0, sizeof(ota_info_t));
size = SerialDownload(XIUOS_FLAH_ADDRESS);
if(size > 0)
{
ota_info.os.size = size;
ota_info.os.crc32 = calculate_crc32(XIUOS_FLAH_ADDRESS, size);
ota_info.os.version = 0x1;
strncpy(ota_info.os.description, "This is the initial firmware for the device!", sizeof(ota_info.os.description));
UpdateOTAFlag(&ota_info);
}
}
void jump_to_application(void)
{
SCB->VTOR = (uint32_t)XIUOS_FLAH_ADDRESS;
@ -34,10 +53,12 @@ void jump_to_application(void)
asm volatile("BX R0");
}
void BootLoaderJumpApp(void)
{
uint8_t ch1, ch2;
uint32_t ret;
ota_info_t ota_info;
uint32_t timeout = 500;
BOARD_ConfigMPU();
@ -71,11 +92,24 @@ void BootLoaderJumpApp(void)
case 0x31:
jump_to_application();
break;
case 0x32:
FLASH_Init();
UpdateApplication();
memcpy(&ota_info, (const void *)FLAG_FLAH_ADDRESS,sizeof(ota_info_t));
/* 此时APP分区还没有有效的固件,需要在bootloader下通过iap烧写出厂固件 */
if((ota_info.os.size > APP_FLASH_SIZE) || (calculate_crc32(XIUOS_FLAH_ADDRESS, ota_info.os.size) != ota_info.os.crc32))
{
Serial_PutString("\r\nNeed to flash initial firmware!\r\n");
InitialVersion();
}
else
{
UpdateApplication();
}
FLASH_DeInit();
jump_to_application();
break;
case 0x33:
__set_FAULTMASK(1);
NVIC_SystemReset();

View File

@ -26,15 +26,14 @@
#include "flash.h"
#include "ymodem.h"
/* OTA升级过程中的状态描述 */
typedef enum {
OTA_STATUS_IDLE = 0, // 空闲状态没有进行OTA升级
OTA_STATUS_READY, // 准备状态可以进行OTA升级
OTA_STATUS_IDLE = 0, // 空闲状态,没有进行OTA升级
OTA_STATUS_READY, // 准备状态,可以进行OTA升级
OTA_STATUS_DOWNLOADING, // 正在下载固件
OTA_STATUS_DOWNLOADED, // 固件下载完成,可以进行升级
OTA_STATUS_DOWNLOADED, // 固件下载完成
OTA_STATUS_UPDATING, // 正在进行OTA升级
OTA_STATUS_ERROR, // 出现错误升级失败
OTA_STATUS_ERROR, // 出现错误,升级失败
} ota_status_t;
@ -54,9 +53,8 @@ typedef struct {
firmware_t bak; // Bakup分区属性信息
firmware_t down; // Download分区属性信息
uint32_t status; // 升级状态,取值来自于ota_status_t类型
uint32_t initversion; // 恢复出厂设置的标志,0xFFFFFFFF代表未烧写过,烧写过以后设置为0x12345678
uint32_t jumpfailed; // bootloaer跳转失败的标志,bootloader里置0xabababab,跳转成功后置0x00000000
uint32_t reserve; // 保留字段
uint32_t lastjumpflag; // bootloaer跳转失败的标志,bootloader里置0xABABABAB,跳转成功后在应用里置0x00000000
uint32_t reserve[2]; // 保留字段
uint8_t error_message[128]; // 错误信息,最多128个字符
} ota_info_t;