接上一篇文章:单Bank OTA升级:STM32G071 BootLoader (一):跳转链接
什么是单Bank升级:将Flash划分为以下3个区域。
BootLoader区:程序进行升级的引导程序,根据Upade_Flag来判断跳转Bank区运行程序或是接收升级数据写入Bank,接收完成后擦写Upade_Flag区,进行跳转Bank区运行程序。
Upade_Flag区:判断BootLoader是否能跳转IAP程序的标志位。
Bank区:用户程序区。
IAP程序只需要在BootLoader的基础上进行如下修改
对程序起始地址和内存大小进行设置。
main.c文件
int main(void)
{
SCB->VTOR = FLASH_APP_ADDR;
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_USART2_UART_Init();
ClearRxQueue();
printf("IAP Code \r\n");
while (1)
{
Usart_Data_Handler();
}
}
Update.c文件
bool Usart_Data_Handler(void)
{
static uint8_t Data;
static uint8_t Cmd;
static uint8_t Check;
static uint8_t Send_Data[10];
static uint16_t Update_Pack_Num;
static uint16_t Update_Pack_Num_Temp;
static uint32_t Update_Data_Len;
static uint32_t Write_Date_Len;
static uint16_t Receive_Data_Len;
static uint16_t Receive_Data_Count;
while(GetRxQueueLen() > 0)
{
Data = GetRxQueueData();
if(Receiver_State == RECEIVER_HEAD_H)
{
if(Data == 0x55)
{
Receiver_State++;
}
}
else if(Receiver_State == RECEIVER_HEAD_L)
{
if(Data == 0x55)
{
Receiver_State++;
}
}
else if(Receiver_State == RECEIVER_CMD)
{
Receiver_State++;
Receive_Data_Len = 0;
Receive_Data_Count = 0;
Cmd = Data;
if(Cmd == 0x00)
{
Erase_Updata_Flag_Flash();
printf("Erase_Updata_Flag_Flash \r\n");
NVIC_SystemReset();
}
}
else if(Receiver_State == RECEIVER_LEN_H)
{
Receiver_State++;
}
else if(Receiver_State == RECEIVER_LEN_L)
{
Receiver_State++;
}
else if(Receiver_State == RECEIVER_DATA)
{
if(Receive_Data_Count == Receive_Data_Len)
{
Receiver_State++;
}
}
else if(Receiver_State == RECEIVER_CHECK)
{
Receiver_State = RECEIVER_HEAD_H;
}
}
}
上位机升级工具:升级工具