This commit is contained in:
qiyongshuang
2026-01-07 10:43:29 +08:00
parent 6209b6e7f9
commit d161f84c90
551 changed files with 23169 additions and 1518 deletions
@@ -1,8 +1,8 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : ch32v20x_usbfs_device.c
* Author : WCH
* Version : V1.0.0
* Date : 2024/01/06
* Version : V1.0.1
* Date : 2025/08/05
* Description : This file provides all the USBFS firmware functions.
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
@@ -43,7 +43,7 @@ volatile UINT16 SetupReqLen;
/******************************************************************************/
/* Device Descriptor */
const UINT8 MyDevDescrHD[] = { 0x12, 0x01, 0x10, 0x01, 0xFF, 0x80, 0x55,
DevEP0SIZE, 0x48, 0x43, 0xe0, 0x55, //USB MODULE
DevEP0SIZE, 0x86, 0x1A, 0xe0, 0x55, //USB MODULE
(u8) Version_Num, (u8) (Version_Num >> 8),
0x00, 0x00, 0x00, 0x01 };
@@ -1,8 +1,8 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : iap.c
* Author : WCH
* Version : V1.0.0
* Date : 2020/12/16
* Version : V1.0.1
* Date : 2025/01/23
* Description : CH32V203 fast program
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
@@ -12,9 +12,7 @@
#include "flash.h"
#include "string.h"
u32 Verity_buf[32];
u32 Verify_buf[32];
/*********************************************************************
* @fn CH32_IAP_Program
@@ -28,5 +26,3 @@ void CH32_IAP_Program(u32 adr, u32* buf)
{
FLASH_ProgramPage_Fast(adr, buf);
}
@@ -1,8 +1,8 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : falsh.h
* Author : WCH
* Version : V1.0.0
* Date : 2020/12/16
* Version : V1.0.1
* Date : 2025/01/23
* Description :
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
+165 -54
View File
@@ -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)
}
}
+37 -18
View File
@@ -1,8 +1,8 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : iap.h
* Author : WCH
* Version : V1.0.0
* Date : 2020/12/16
* Version : V1.0.1
* Date : 2025/01/09
* Description : IAP
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
@@ -16,43 +16,62 @@
#include "stdio.h"
#include "ch32v20x_usbfs_device.h"
#define USBD_DATA_SIZE 64
#define USBD_DATA_SIZE 64
#define FLASH_Base 0x08005000
#define BUILD_UINT16(loByte, hiByte) ((UINT16)(((loByte) & 0x00FF) | (((hiByte) & 0x00FF) << 8)))
#define BUILD_UINT32(Byte0, Byte1, Byte2, Byte3) \
((UINT32)((UINT32)((Byte0) & 0x00FF) \
+ ((((UINT32)Byte1) & 0x00FF) << 8) \
+ ((((UINT32)Byte2) & 0x00FF) << 16) \
+ ((((UINT32)Byte3) & 0x00FF) << 24)))
#define Uart_Sync_Head1 0x57
#define Uart_Sync_Head2 0xab
#define Uart_Sync_Head1 0xaa
#define Uart_Sync_Head2 0x55
#define CMD_IAP_PROM 0x80
#define CMD_IAP_ERASE 0x81
#define CMD_IAP_VERIFY 0x82
#define CMD_IAP_END 0x83
#define CMD_JUMP_IAP 0x84
#define ERR_SCUESS 0x00
#define ERR_SUCCESS 0x00
#define ERR_ERROR 0x01
#define ERR_End 0x02
typedef struct __attribute__ ((aligned(4)))_ISP_CMD {
#define CalAddr (0x08038000-4)
#define CheckNum (0x5aa55aa5)
typedef union __attribute__ ((aligned(4)))_ISP_CMD {
struct{
u8 Cmd;
u8 Len;
u8 Rev[2];
u8 data[60];
u8 data[64];
}UART;
struct{
u8 Cmd;
u8 Len;
u8 data[62];
}program;
struct{
u8 Cmd;
u8 Len;
u8 addr[4];
u8 data[56];
}verify;
struct{
u8 buf[64+4];
}other;
} isp_cmd;
typedef void (*iapfun)(void);
extern u32 Program_Verity_addr;
extern u32 Program_Verify_addr;
extern u32 User_APP_Addr_offset;
extern u8 EP2_IN_Flag;
extern u8 EP2_OUT_Flag;
extern u16 EP2_Rx_Cnt;
extern u8 EP2_Tx_Buffer[2];
extern u16 EP2_Tx_Cnt;
extern u8 EP2_Rx_Buffer[USBD_DATA_SIZE];
extern u8 EP2_Rx_Buffer[USBD_DATA_SIZE+4];
u8 RecData_Deal(void);
void GPIO_Cfg_init(void);
+39 -17
View File
@@ -1,8 +1,8 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : main.c
* Author : WCH
* Version : V1.0.0
* Date : 2021/06/06
* Version : V1.0.2
* Date : 2025/10/27
* Description : Main program body.
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
@@ -11,16 +11,12 @@
*******************************************************************************/
/*
*@Note
*IAP upgrade routine:
*Support serial port and USB for FLASH burning
*
*1. The routine needs to install the CH372 driver.
*Note: FLASH operation keeps the frequency below 100Mhz, it is recommended that the main
*frequency of IAP be below 100Mhz
*
* IAP routine: this routine support USB and UART mode,
* and you can choose the command method or the IO method to jump to the APP .
* Key parameters: CalAddr - address in flash (same in APP), note that this address needs to be unused.
* CheckNum - The value of 'CalAddr' that needs to be modified.
* Tips :the routine need IAP software version 1.50.
*/
#include "debug.h"
#include "ch32v20x_usbfs_device.h"
#include "iap.h"
@@ -29,7 +25,12 @@
#include "usb_lib.h"
#include "hw_config.h"
#include "usb_pwr.h"
extern u8 End_Flag;
extern vu8 End_Flag;
#define UPGRADE_MODE_COMMAND 0
#define UPGRADE_MODE_IO 1
#define UPGRADE_MODE UPGRADE_MODE_COMMAND
/*********************************************************************
* @fn IAP_2_APP
*
@@ -59,6 +60,7 @@ void IAP_2_APP(void) {
NVIC_EnableIRQ(Software_IRQn);
NVIC_SetPendingIRQ(Software_IRQn);
}
/*********************************************************************
* @fn main
*
@@ -66,12 +68,31 @@ void IAP_2_APP(void) {
*
* @return none
*/
int main(void) {
int main(void)
{
SystemCoreClockUpdate();
Delay_Init();
USART_Printf_Init(115200);
printf("SystemClk:%d\r\n", SystemCoreClock);
printf( "ChipID:%08x\r\n", DBGMCU_GetCHIPID() );
printf("IAP\r\n");
#if UPGRADE_MODE == UPGRADE_MODE_COMMAND
if(*(vu32 *)FLASH_Base != 0xe339e339 )
{
if(*(vu32 *)CalAddr != CheckNum)
{
IAP_2_APP();
while(1);
}
}
#elif UPGRADE_MODE == UPGRADE_MODE_IO
if(PA0_Check() == 0)
{
IAP_2_APP();
while(1);
}
#endif
Set_USBConfig();
USB_Init();
@@ -81,18 +102,19 @@ int main(void) {
USB_Interrupts_Config();
USBFS_Init();
USART3_CFG(57600);
USART3_CFG(460800);
while(1)
{
EP2_RecData_Deal();
if( USART_GetFlagStatus(USART3, USART_FLAG_RXNE) != RESET) {
UART_Rx_Deal();
}
#if UPGRADE_MODE == UPGRADE_MODE_COMMAND
if (End_Flag)
{
Delay_Ms(10);
{
IAP_2_APP();
while(1);
}
}
#endif
}
}
@@ -4,7 +4,7 @@
* Version : V1.0.0
* Date : 2021/06/06
* Description : CH32V20x Device Peripheral Access Layer System Source File.
* For HSE = 32Mhz (CH32V208x/CH32V203RBT6)
* For HSE = 32Mhz (CH32V208x/CH32V203RBT)
* For HSE = 8Mhz (other CH32V203x)
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.