186 lines
4.3 KiB
C
186 lines
4.3 KiB
C
/********************************** (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>
|
||
|
||
uint32_t test_tick = 0;
|
||
|
||
/*********************************************************************
|
||
* @fn main
|
||
*
|
||
* @brief Main program.
|
||
*
|
||
* @return none
|
||
*/
|
||
int main(void)
|
||
{
|
||
uint8_t sys_reset = 0;
|
||
uint32_t wdt_reste_tick = 0;
|
||
unsigned char Buffer;
|
||
|
||
unsigned long DATA_ROM_ADDR;
|
||
|
||
SystemCoreClockUpdate(); //系统初始化
|
||
Systick_Init();
|
||
|
||
UARTx_Init(UART_0,512000);
|
||
UARTx_Init(UART_1,512000);
|
||
UARTx_Init(UART_2,512000);
|
||
UARTx_Init(UART_3,512000);
|
||
|
||
SYS_LED_Init();
|
||
|
||
SPI_SRAM_Init();
|
||
|
||
SPI_FLASH_Init();
|
||
|
||
Get_Flash_Size(&Buffer);
|
||
|
||
if(Buffer){
|
||
DATA_ROM_ADDR = 0x70000;
|
||
}else {
|
||
DATA_ROM_ADDR = 0x30000;
|
||
}
|
||
|
||
DBG_SYS_Printf("RTC_Init \r\n");
|
||
|
||
RTC_Init();
|
||
|
||
DBG_SYS_Printf("EEPROM_Init - DATA_ROM_ADDR:%x\r\n",DATA_ROM_ADDR);
|
||
|
||
EEPROM_Init();
|
||
|
||
DBG_SYS_Printf("G PARA \r\n");
|
||
|
||
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("G_SYS_FEATURE_T : %d \r\n",sizeof(G_SYS_FEATURE_T));
|
||
|
||
sys_reset = RCC_GET_GLOB_RST_KEEP();
|
||
if(sys_reset == 0x00)
|
||
{
|
||
DBG_SYS_Printf("上电复位 \r\n");
|
||
}else if(sys_reset == 0x02){
|
||
|
||
DBG_SYS_Printf("看门狗复位 \r\n");
|
||
}else {
|
||
DBG_SYS_Printf("其他复位 \r\n");
|
||
}
|
||
|
||
RCC_SET_GLOB_RST_KEEP(0x01);
|
||
g_jump_flag = Read_APP_Feature();
|
||
|
||
//测试使用的 - 不是最终状态
|
||
if(g_jump_flag == 2) {
|
||
MCU_APP_Write(); //MCU需要写入
|
||
g_jump_flag = 0;
|
||
|
||
// printf("Jump APP 1\r\n");
|
||
//
|
||
// Delay_Ms(1000);
|
||
//
|
||
// __disable_irq();
|
||
// Jump_APP(g_mcu_app_feature.app_start_addr);
|
||
|
||
NVIC_EnableIRQ( Software_IRQn );
|
||
Delay_Ms( 20 );
|
||
NVIC_SetPendingIRQ( Software_IRQn );
|
||
}
|
||
|
||
while (1)
|
||
{
|
||
SYS_LED_Task();
|
||
|
||
Uart0_Task();
|
||
|
||
UART1_RECEIVE();
|
||
UART2_RECEIVE();
|
||
UART3_RECEIVE();
|
||
|
||
RTC_TASK();
|
||
|
||
//测试使用的 - 不是最终状态
|
||
if(SysTick_1ms - g_Boot_Tick >= 5000)
|
||
{
|
||
g_Boot_Tick = SysTick_1ms;
|
||
|
||
if(g_jump_flag == 0x00) //APP 校验成功,跳转APP
|
||
{
|
||
printf("Jump APP 2\r\n");
|
||
|
||
Delay_Ms(1000);
|
||
|
||
NVIC_EnableIRQ( Software_IRQn );
|
||
Delay_Ms( 20 );
|
||
NVIC_SetPendingIRQ( Software_IRQn );
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
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);
|
||
}
|
||
|
||
void SW_Handler(void) {
|
||
printf("SW_Handler Jump App\r\n");
|
||
Delay_Ms( 100 );
|
||
|
||
__disable_irq();
|
||
__asm volatile("li a6, 0x07000");
|
||
__asm volatile("jr a6");
|
||
while(1);
|
||
|
||
}
|
||
|