V2.31
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : iap.c
|
||||
* Author : WCH
|
||||
* Version : V1.0.0
|
||||
* Date : 2020/12/16
|
||||
* Version : V1.0.2
|
||||
* Date : 2025/10/27
|
||||
* Description : IAP
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
@@ -17,44 +17,41 @@
|
||||
#include "usb_lib.h"
|
||||
/******************************************************************************/
|
||||
|
||||
#define FLASH_Base 0x08005000
|
||||
#define USBD_DATA_SIZE 64
|
||||
iapfun jump2app;
|
||||
u32 Program_addr = FLASH_Base;
|
||||
u32 Verity_addr = FLASH_Base;
|
||||
vu32 Program_addr = FLASH_Base;
|
||||
vu32 Verify_addr = FLASH_Base;
|
||||
u32 User_APP_Addr_offset = 0x5000;
|
||||
u8 Verity_Star_flag = 0;
|
||||
vu8 Verify_Star_flag = 0;
|
||||
u8 Fast_Program_Buf[390];
|
||||
u32 CodeLen = 0;
|
||||
u8 End_Flag = 0;
|
||||
u8 EP2_Rx_Buffer[USBD_DATA_SIZE];
|
||||
vu32 CodeLen = 0;
|
||||
vu8 End_Flag = 0;
|
||||
u8 EP2_Rx_Buffer[USBD_DATA_SIZE+4];
|
||||
#define isp_cmd_t ((isp_cmd *)EP2_Rx_Buffer)
|
||||
|
||||
/*********************************************************************
|
||||
* @fn RecData_Deal
|
||||
*
|
||||
* @brief UART-USB deal date
|
||||
* @brief USB deal data
|
||||
*
|
||||
* @return ERR_ERROR - ERROR
|
||||
* ERR_SCUESS - SCUESS
|
||||
* ERR_SUCCESS - SUCCESS
|
||||
* ERR_End - End
|
||||
*/
|
||||
|
||||
u8 RecData_Deal(void)
|
||||
{
|
||||
u8 i, s, Lenth;
|
||||
|
||||
Lenth = isp_cmd_t->Len;
|
||||
Lenth = isp_cmd_t->other.buf[1];
|
||||
|
||||
switch ( isp_cmd_t->Cmd) {
|
||||
switch ( isp_cmd_t->other.buf[0]) {
|
||||
case CMD_IAP_ERASE:
|
||||
FLASH_Unlock_Fast();
|
||||
s = ERR_SCUESS;
|
||||
s = ERR_SUCCESS;
|
||||
break;
|
||||
|
||||
case CMD_IAP_PROM:
|
||||
for (i = 0; i < Lenth; i++) {
|
||||
Fast_Program_Buf[CodeLen + i] = isp_cmd_t->data[i];
|
||||
Fast_Program_Buf[CodeLen + i] = isp_cmd_t->program.data[i];
|
||||
}
|
||||
CodeLen += Lenth;
|
||||
if (CodeLen >= 256) {
|
||||
@@ -69,13 +66,13 @@ u8 RecData_Deal(void)
|
||||
Program_addr += 0x100;
|
||||
|
||||
}
|
||||
s = ERR_SCUESS;
|
||||
s = ERR_SUCCESS;
|
||||
break;
|
||||
|
||||
case CMD_IAP_VERIFY:
|
||||
if (Verity_Star_flag == 0)
|
||||
if (Verify_Star_flag == 0)
|
||||
{
|
||||
Verity_Star_flag = 1;
|
||||
Verify_Star_flag = 1;
|
||||
if(CodeLen != 0)
|
||||
{
|
||||
for (i = 0; i < (256 - CodeLen); i++)
|
||||
@@ -88,29 +85,35 @@ u8 RecData_Deal(void)
|
||||
}
|
||||
}
|
||||
|
||||
s = ERR_SCUESS;
|
||||
s = ERR_SUCCESS;
|
||||
for (i = 0; i < Lenth; i++) {
|
||||
if (isp_cmd_t->data[i] != *(u8*) (Verity_addr + i)) {
|
||||
if (isp_cmd_t->verify.data[i] != *(u8*) (Verify_addr + i)) {
|
||||
s = ERR_ERROR;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Verity_addr += Lenth;
|
||||
Verify_addr += Lenth;
|
||||
|
||||
break;
|
||||
|
||||
case CMD_IAP_END:
|
||||
Verity_Star_flag = 0;
|
||||
Verify_Star_flag = 0;
|
||||
End_Flag = 1;
|
||||
Program_addr = FLASH_Base;
|
||||
Verity_addr = FLASH_Base;
|
||||
|
||||
Verify_addr = FLASH_Base;
|
||||
s = ERR_End;
|
||||
|
||||
FLASH_ErasePage_Fast(CalAddr & 0xFFFFFF00);
|
||||
FLASH->CTLR |= ((uint32_t)0x00008000);
|
||||
FLASH->CTLR |= ((uint32_t)0x00000080);
|
||||
break;
|
||||
|
||||
case CMD_JUMP_IAP:
|
||||
|
||||
s = ERR_SUCCESS;
|
||||
break;
|
||||
|
||||
default:
|
||||
s = ERR_ERROR;
|
||||
break;
|
||||
@@ -119,6 +122,98 @@ u8 RecData_Deal(void)
|
||||
return s;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn UART_RecData_Deal
|
||||
*
|
||||
* @brief UART deal data
|
||||
*
|
||||
* @return ERR_ERROR - ERROR
|
||||
* ERR_SUCCESS - SUCCESS
|
||||
* ERR_End - End
|
||||
*/
|
||||
u8 UART_RecData_Deal(void)
|
||||
{
|
||||
u8 i, s, Lenth;
|
||||
|
||||
Lenth = isp_cmd_t->UART.Len;
|
||||
switch ( isp_cmd_t->UART.Cmd) {
|
||||
case CMD_IAP_ERASE:
|
||||
|
||||
FLASH_Unlock_Fast();
|
||||
s = ERR_SUCCESS;
|
||||
break;
|
||||
|
||||
case CMD_IAP_PROM:
|
||||
for (i = 0; i < Lenth; i++) {
|
||||
Fast_Program_Buf[CodeLen + i] = isp_cmd_t->UART.data[i];
|
||||
}
|
||||
CodeLen += Lenth;
|
||||
if (CodeLen >= 256) {
|
||||
FLASH_Unlock_Fast();
|
||||
FLASH_ErasePage_Fast(Program_addr);
|
||||
CH32_IAP_Program(Program_addr, (u32*) Fast_Program_Buf);
|
||||
CodeLen -= 256;
|
||||
for (i = 0; i < CodeLen; i++) {
|
||||
Fast_Program_Buf[i] = Fast_Program_Buf[256 + i];
|
||||
}
|
||||
|
||||
Program_addr += 0x100;
|
||||
|
||||
}
|
||||
s = ERR_SUCCESS;
|
||||
break;
|
||||
|
||||
case CMD_IAP_VERIFY:
|
||||
|
||||
if (Verify_Star_flag == 0)
|
||||
{
|
||||
Verify_Star_flag = 1;
|
||||
if(CodeLen != 0)
|
||||
{
|
||||
for (i = 0; i < (256 - CodeLen); i++)
|
||||
{
|
||||
Fast_Program_Buf[CodeLen + i] = 0xff;
|
||||
}
|
||||
FLASH_ErasePage_Fast(Program_addr);
|
||||
CH32_IAP_Program(Program_addr, (u32*) Fast_Program_Buf);
|
||||
CodeLen = 0;
|
||||
}
|
||||
}
|
||||
s = ERR_SUCCESS;
|
||||
for (i = 0; i < Lenth; i++) {
|
||||
if (isp_cmd_t->UART.data[i] != *(u8*) (Verify_addr + i)) {
|
||||
s = ERR_ERROR;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Verify_addr += Lenth;
|
||||
break;
|
||||
|
||||
case CMD_IAP_END:
|
||||
Verify_Star_flag = 0;
|
||||
End_Flag = 1;
|
||||
Program_addr = FLASH_Base;
|
||||
Verify_addr = FLASH_Base;
|
||||
s = ERR_End;
|
||||
|
||||
FLASH_ErasePage_Fast(CalAddr & 0xFFFFFF00);
|
||||
FLASH->CTLR |= ((uint32_t)0x00008000);
|
||||
FLASH->CTLR |= ((uint32_t)0x00000080);
|
||||
|
||||
break;
|
||||
|
||||
case CMD_JUMP_IAP:
|
||||
|
||||
s = ERR_SUCCESS;
|
||||
break;
|
||||
default:
|
||||
s = ERR_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
/*********************************************************************
|
||||
* @fn EP2_RecData_Deal
|
||||
*
|
||||
@@ -298,43 +393,62 @@ u8 Uart3_Rx(void)
|
||||
void UART_Rx_Deal(void)
|
||||
{
|
||||
u8 i, s;
|
||||
u8 Data_add = 0;
|
||||
u16 Data_add = 0;
|
||||
|
||||
if (Uart3_Rx() == Uart_Sync_Head1)
|
||||
{
|
||||
if (Uart3_Rx() == Uart_Sync_Head2)
|
||||
{
|
||||
isp_cmd_t->Cmd = Uart3_Rx();
|
||||
Data_add += isp_cmd_t->Cmd;
|
||||
isp_cmd_t->Len = Uart3_Rx();
|
||||
Data_add += isp_cmd_t->Len;
|
||||
isp_cmd_t->Rev[0] = Uart3_Rx();
|
||||
Data_add += isp_cmd_t->Rev[0];
|
||||
isp_cmd_t->Rev[1] = Uart3_Rx();
|
||||
Data_add += isp_cmd_t->Rev[1];
|
||||
isp_cmd_t->UART.Cmd = Uart3_Rx();
|
||||
Data_add += isp_cmd_t->UART.Cmd;
|
||||
isp_cmd_t->UART.Len = Uart3_Rx();
|
||||
Data_add += isp_cmd_t->UART.Len;
|
||||
|
||||
if ((isp_cmd_t->Cmd == CMD_IAP_PROM) || (isp_cmd_t->Cmd == CMD_IAP_VERIFY))
|
||||
if(isp_cmd_t->UART.Cmd == CMD_IAP_ERASE ||isp_cmd_t->UART.Cmd == CMD_IAP_VERIFY)
|
||||
{
|
||||
for (i = 0; i < isp_cmd_t->Len; i++) {
|
||||
isp_cmd_t->data[i] = Uart3_Rx();
|
||||
Data_add += isp_cmd_t->data[i];
|
||||
isp_cmd_t->other.buf[2] = Uart3_Rx();
|
||||
Data_add += isp_cmd_t->other.buf[2];
|
||||
isp_cmd_t->other.buf[3] = Uart3_Rx();
|
||||
Data_add += isp_cmd_t->other.buf[3];
|
||||
isp_cmd_t->other.buf[4] = Uart3_Rx();
|
||||
Data_add += isp_cmd_t->other.buf[4];
|
||||
isp_cmd_t->other.buf[5] = Uart3_Rx();
|
||||
Data_add += isp_cmd_t->other.buf[5];
|
||||
}
|
||||
if ((isp_cmd_t->other.buf[0] == CMD_IAP_PROM) || (isp_cmd_t->other.buf[0] == CMD_IAP_VERIFY))
|
||||
{
|
||||
for (i = 0; i < isp_cmd_t->UART.Len; i++) {
|
||||
isp_cmd_t->UART.data[i] = Uart3_Rx();
|
||||
Data_add += isp_cmd_t->UART.data[i];
|
||||
}
|
||||
}
|
||||
|
||||
if (Uart3_Rx() == Data_add)
|
||||
if (Uart3_Rx() == (uint8_t)(Data_add & 0xFF))
|
||||
{
|
||||
s = RecData_Deal();
|
||||
|
||||
if (s != ERR_End)
|
||||
if(Uart3_Rx() == (uint8_t)(Data_add >>8))
|
||||
{
|
||||
UART3_SendData(0x00);
|
||||
if (s == ERR_ERROR)
|
||||
if (Uart3_Rx() == Uart_Sync_Head2)
|
||||
{
|
||||
UART3_SendData(0x01);
|
||||
}
|
||||
else
|
||||
{
|
||||
UART3_SendData(0x00);
|
||||
if (Uart3_Rx() == Uart_Sync_Head1)
|
||||
{
|
||||
s = UART_RecData_Deal();
|
||||
|
||||
if (s != ERR_End)
|
||||
{
|
||||
UART3_SendData(Uart_Sync_Head1);
|
||||
UART3_SendData(Uart_Sync_Head2);
|
||||
UART3_SendData(0x00);
|
||||
if (s == ERR_ERROR)
|
||||
{
|
||||
UART3_SendData(0x01);
|
||||
}
|
||||
else
|
||||
{
|
||||
UART3_SendData(0x00);
|
||||
}
|
||||
UART3_SendData(Uart_Sync_Head2);
|
||||
UART3_SendData(Uart_Sync_Head1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -342,6 +456,3 @@ void UART_Rx_Deal(void)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user