Files
RCU_C1P_Launcher/User/main.c
2026-02-10 17:48:22 +08:00

218 lines
5.5 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/********************************** (C) COPYRIGHT *******************************
* File Name : main.c
* Author : WCH
* Version : V1.0.0
* Date : 2024/05/05
* Description : Main program body.
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
#include "includes.h"
#include <stdio.h>
#include <string.h>
const char Prject_File_Name[] __attribute__((section(".project_info"))) = Project_Version;
uint32_t test_tick = 0;
/*********************************************************************
* @fn main
*
* @brief Main program.
*
* @return none
*/
int main(void)
{
uint8_t sys_reset = 0;
unsigned char Buffer;
/* - 上电前需要关闭看门狗
* - 原因:如果开启看门狗后,软件进行复位还看门狗存在,会一直产生复位
* */
WDT_Reinit();
SystemCoreClockUpdate(); //系统初始化
Systick_Init();
UARTx_Init(UART_1,512000);
UARTx_Init(UART_2,512000);
SYS_LED_Init();
SPI_SRAM_Init();
SPI_FLASH_Init();
Get_Flash_Size(&Buffer);
if(Buffer){
DBG_SYS_Printf("CH564 ROMA :0x0 - 0x6FFFF");
DBG_SYS_Printf("CH564 EEPROM :0x70000 - 0x77FFF");
}else {
DBG_SYS_Printf("CH564 ROMA :0x0 - 0x2FFFF");
DBG_SYS_Printf("CH564 EEPROM :0x30000 - 0x37FFF");
}
EEPROM_Init();
memset((uint8_t *)&g_app_feature,0,sizeof(G_SYS_FEATURE_T));
memset((uint8_t *)&g_mcu_app_feature,0,sizeof(G_SYS_FEATURE_T));
memset((uint8_t *)&g_update_recode,0,sizeof(UPDATE_RECORD_T));
DBG_SYS_Printf("Project Name:%s",Prject_File_Name);
DBG_SYS_Printf("Project - SYSCLK_FREQ_120MHz_HSI");
sys_reset = RCC_GET_GLOB_RST_KEEP(); //复位保持寄存器
if(sys_reset == 0x00)
{
DBG_SYS_Printf("上电复位 \r\n");
g_Boot_Time = 10000;
}else if(sys_reset == 0x02){
DBG_SYS_Printf("软件复位 \r\n");
g_Boot_Time = 10000;
}else if(sys_reset == 0x03){
DBG_SYS_Printf("MCU Flash软件复位 \r\n");
g_Boot_Time = 3000;
}else {
DBG_SYS_Printf("异常复位 \r\n");
g_Boot_Time = 10000;
}
RCC_SET_GLOB_RST_KEEP(0x01);
SPIFLASH_Read_Update_Recode(&g_update_recode); //读取APP写入次数
g_jump_flag = Read_APP_Feature();
//测试使用的 - 不是最终状态
if(g_jump_flag == 2) {
MCU_APP_Write(); //MCU需要写入
DBG_SYS_Printf("进行MCU Flash软件复位 \r\n");
RCC_SET_GLOB_RST_KEEP(0x03);
Delay_Ms(1000);
// - 进行全局复位让芯片重新转载Code Flash
//RCC_GlobleRstCFG(RB_GLOB_FORCE_RST, ENABLE);
// - 软件复位芯片重新转载Code Flash,需要启动文件中的LD文件 .vector字段中加入 KEEP,否则不能使用
NVIC_SystemReset();
while(1);
}
//Dbg_Switch = 0x00;
WDT_Init();
while (1)
{
WDT_Feed();
SYS_LED_Task();
Uart1_Task();
Uart2_Task();
//测试使用的 - 不是最终状态
if(g_jump_flag == 0x00)
{
if(SysTick_1ms - g_Boot_Tick >= g_Boot_Time)
{
g_Boot_Tick = SysTick_1ms;
DBG_SYS_Printf("Jump APP 2\r\n");
Delay_Ms(1000);
WDT_Reinit();
NVIC_EnableIRQ( Software_IRQn );
Delay_Ms( 20 );
HSI_ON();
CLKSEL_HSI();
SYSCLK_SOURCE_SELECT(SYSCLK_SOURCE_HSI_HSE);
RCC_UNLOCK_SAFE_ACCESS();
BITS_CFG( R32_EXTEN_CTLR0 , RB_FLASH_PRE_EN , DISABLE );
RCC_LOCK_SAFE_ACCESS();
NVIC_SetPendingIRQ( Software_IRQn );
}
}else {
if(SysTick_1ms - g_Boot_Tick >= g_Boot_Time)
{
DBG_SYS_Printf("进行软件复位 ....");
RCC_SET_GLOB_RST_KEEP(0x02);
Delay_Ms(1000);
//方式1
//RCC_GlobleRstCFG(RB_GLOB_FORCE_RST, ENABLE);
//方式2
NVIC_SystemReset();
while(1);
}
}
}
}
void NMI_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void HardFault_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
/*********************************************************************
* @fn NMI_Handler
*
* @brief This function handles NMI exception.
*
* @return none
*/
void NMI_Handler(void)
{
while (1)
{
}
}
/*********************************************************************
* @fn HardFault_Handler
*
* @brief This function handles Hard Fault exception.
*
* @return none
*/
void HardFault_Handler(void)
{
/* MRS_关于HardFault问题查找思路 : https://www.cnblogs.com/wchmcu/p/17545931.html */
uint32_t v_mepc,v_mcause,v_mtval;
//Dbg_Println(DBG_BIT_SYS_STATUS_bit,"hardfault\n");
v_mepc = __get_MEPC();
v_mcause = __get_MCAUSE();
v_mtval = __get_MTVAL();
printf("boot mepc:%x\n",v_mepc);
printf("boot mcause:%x\n",v_mcause);
printf("boot mtval:%x\n",v_mtval);
while(1);
}
/* - 跳转APP 程序必须在SW_Handler中断中跳转
* - 因为进入SW_Handler中断后芯片会进入机器模式才允许跳转APP */
void SW_Handler(void) {
__asm volatile("li a6, 0x01000");
__asm volatile("jr a6");
while(1);
}