2022-03-26 20:51:40 +08:00

394 lines
8.8 KiB
C
Raw 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 : CH58x_SYS.c
* Author : WCH
* Version : V1.2
* Date : 2021/11/17
* Description
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* SPDX-License-Identifier: Apache-2.0
*******************************************************************************/
#include "CH58x_common.h"
/*********************************************************************
* @fn SetSysClock
*
* @brief <20><><EFBFBD><EFBFBD>ϵͳ<CFB5><CDB3><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
*
* @param sc - ϵͳʱ<CDB3><CAB1>Դѡ<D4B4><D1A1> refer to SYS_CLKTypeDef
*
* @return none
*/
__HIGH_CODE
void SetSysClock(SYS_CLKTypeDef sc)
{
uint32_t i;
R8_SAFE_ACCESS_SIG = SAFE_ACCESS_SIG1;
R8_SAFE_ACCESS_SIG = SAFE_ACCESS_SIG2;
SAFEOPERATE;
R8_PLL_CONFIG &= ~(1 << 5); //
R8_SAFE_ACCESS_SIG = 0;
if(sc & 0x20)
{ // HSE div
if(!(R8_HFCK_PWR_CTRL & RB_CLK_XT32M_PON))
{
R8_SAFE_ACCESS_SIG = SAFE_ACCESS_SIG1;
R8_SAFE_ACCESS_SIG = SAFE_ACCESS_SIG2;
SAFEOPERATE;
R8_HFCK_PWR_CTRL |= RB_CLK_XT32M_PON; // HSE power on
for(i = 0; i < 1200; i++)
{
__nop();
__nop();
}
}
R8_SAFE_ACCESS_SIG = SAFE_ACCESS_SIG1;
R8_SAFE_ACCESS_SIG = SAFE_ACCESS_SIG2;
SAFEOPERATE;
R16_CLK_SYS_CFG = (0 << 6) | (sc & 0x1f);
__nop();
__nop();
__nop();
__nop();
R8_SAFE_ACCESS_SIG = 0;
R8_SAFE_ACCESS_SIG = SAFE_ACCESS_SIG1;
R8_SAFE_ACCESS_SIG = SAFE_ACCESS_SIG2;
SAFEOPERATE;
R8_FLASH_CFG = 0X51;
R8_SAFE_ACCESS_SIG = 0;
}
else if(sc & 0x40)
{ // PLL div
if(!(R8_HFCK_PWR_CTRL & RB_CLK_PLL_PON))
{
R8_SAFE_ACCESS_SIG = SAFE_ACCESS_SIG1;
R8_SAFE_ACCESS_SIG = SAFE_ACCESS_SIG2;
SAFEOPERATE;
R8_HFCK_PWR_CTRL |= RB_CLK_PLL_PON; // PLL power on
for(i = 0; i < 2000; i++)
{
__nop();
__nop();
}
}
R8_SAFE_ACCESS_SIG = SAFE_ACCESS_SIG1;
R8_SAFE_ACCESS_SIG = SAFE_ACCESS_SIG2;
SAFEOPERATE;
R16_CLK_SYS_CFG = (1 << 6) | (sc & 0x1f);
__nop();
__nop();
__nop();
__nop();
R8_SAFE_ACCESS_SIG = 0;
R8_SAFE_ACCESS_SIG = SAFE_ACCESS_SIG1;
R8_SAFE_ACCESS_SIG = SAFE_ACCESS_SIG2;
SAFEOPERATE;
R8_FLASH_CFG = 0X52;
R8_SAFE_ACCESS_SIG = 0;
}
else
{
R8_SAFE_ACCESS_SIG = SAFE_ACCESS_SIG1;
R8_SAFE_ACCESS_SIG = SAFE_ACCESS_SIG2;
SAFEOPERATE;
R16_CLK_SYS_CFG |= RB_CLK_SYS_MOD;
}
//<2F><><EFBFBD><EFBFBD>FLASH clk<6C><6B><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
R8_SAFE_ACCESS_SIG = SAFE_ACCESS_SIG1;
R8_SAFE_ACCESS_SIG = SAFE_ACCESS_SIG2;
SAFEOPERATE;
R8_PLL_CONFIG |= 1 << 7;
R8_SAFE_ACCESS_SIG = 0;
}
/*********************************************************************
* @fn GetSysClock
*
* @brief <20><>ȡ<EFBFBD><C8A1>ǰϵͳʱ<CDB3><CAB1>
*
* @param none
*
* @return Hz
*/
uint32_t GetSysClock(void)
{
uint16_t rev;
rev = R16_CLK_SYS_CFG & 0xff;
if((rev & 0x40) == (0 << 6))
{ // 32M<32><4D><EFBFBD>з<EFBFBD>Ƶ
return (32000000 / (rev & 0x1f));
}
else if((rev & RB_CLK_SYS_MOD) == (1 << 6))
{ // PLL<4C><4C><EFBFBD>з<EFBFBD>Ƶ
return (480000000 / (rev & 0x1f));
}
else
{ // 32K<32><4B><EFBFBD><EFBFBD>Ƶ
return (32000);
}
}
/*********************************************************************
* @fn SYS_GetInfoSta
*
* @brief <20><>ȡ<EFBFBD><C8A1>ǰϵͳ<CFB5><CDB3>Ϣ״̬
*
* @param i - refer to SYS_InfoStaTypeDef
*
* @return <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
*/
uint8_t SYS_GetInfoSta(SYS_InfoStaTypeDef i)
{
if(i == STA_SAFEACC_ACT)
{
return (R8_SAFE_ACCESS_SIG & RB_SAFE_ACC_ACT);
}
else
{
return (R8_GLOB_CFG_INFO & (1 << i));
}
}
/*********************************************************************
* @fn SYS_ResetExecute
*
* @brief ִ<><D6B4>ϵͳ<CFB5><CDB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ
*
* @param none
*
* @return none
*/
__HIGH_CODE
void SYS_ResetExecute(void)
{
FLASH_ROM_SW_RESET();
R8_SAFE_ACCESS_SIG = SAFE_ACCESS_SIG1;
R8_SAFE_ACCESS_SIG = SAFE_ACCESS_SIG2;
SAFEOPERATE;
R8_RST_WDOG_CTRL |= RB_SOFTWARE_RESET;
R8_SAFE_ACCESS_SIG = 0;
}
/*********************************************************************
* @fn SYS_DisableAllIrq
*
* @brief <20>ر<EFBFBD><D8B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>жϣ<D0B6><CFA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD>ж<EFBFBD>ֵ
*
* @param pirqv - <20><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>ֵ
*
* @return none
*/
void SYS_DisableAllIrq(uint32_t *pirqv)
{
*pirqv = (PFIC->ISR[0] >> 8) | (PFIC->ISR[1] << 24);
PFIC->IRER[0] = 0xffffffff;
PFIC->IRER[1] = 0xffffffff;
}
/*********************************************************************
* @fn SYS_RecoverIrq
*
* @brief <20>ָ<EFBFBD>֮ǰ<D6AE>رյ<D8B1><D5B5>ж<EFBFBD>ֵ
*
* @param irq_status - <20><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>ֵ
*
* @return none
*/
void SYS_RecoverIrq(uint32_t irq_status)
{
PFIC->IENR[0] = (irq_status << 8);
PFIC->IENR[1] = (irq_status >> 24);
}
/*********************************************************************
* @fn SYS_GetSysTickCnt
*
* @brief <20><>ȡ<EFBFBD><C8A1>ǰϵͳ(SYSTICK)<29><><EFBFBD><EFBFBD>ֵ
*
* @param none
*
* @return <20><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>ֵ
*/
uint32_t SYS_GetSysTickCnt(void)
{
uint32_t val;
val = SysTick->CNT;
return (val);
}
/*********************************************************************
* @fn WWDG_ITCfg
*
* @brief <20><><EFBFBD>Ź<EFBFBD><C5B9><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>ʹ<EFBFBD><CAB9>
*
* @param s - <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>ж<EFBFBD>
*
* @return none
*/
void WWDG_ITCfg(FunctionalState s)
{
R8_SAFE_ACCESS_SIG = SAFE_ACCESS_SIG1;
R8_SAFE_ACCESS_SIG = SAFE_ACCESS_SIG2;
SAFEOPERATE;
if(s == DISABLE)
{
R8_RST_WDOG_CTRL &= ~RB_WDOG_INT_EN;
}
else
{
R8_RST_WDOG_CTRL |= RB_WDOG_INT_EN;
}
R8_SAFE_ACCESS_SIG = 0;
}
/*********************************************************************
* @fn WWDG_ResetCfg
*
* @brief <20><><EFBFBD>Ź<EFBFBD><C5B9><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>
*
* @param s - <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>λ
*
* @return none
*/
void WWDG_ResetCfg(FunctionalState s)
{
R8_SAFE_ACCESS_SIG = SAFE_ACCESS_SIG1;
R8_SAFE_ACCESS_SIG = SAFE_ACCESS_SIG2;
SAFEOPERATE;
if(s == DISABLE)
{
R8_RST_WDOG_CTRL &= ~RB_WDOG_RST_EN;
}
else
{
R8_RST_WDOG_CTRL |= RB_WDOG_RST_EN;
}
R8_SAFE_ACCESS_SIG = 0;
}
/*********************************************************************
* @fn WWDG_ClearFlag
*
* @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ź<EFBFBD><C5B9>жϱ<D0B6>־<EFBFBD><D6BE><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><C2BC>ؼ<EFBFBD><D8BC><EFBFBD>ֵҲ<D6B5><D2B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*
* @param none
*
* @return none
*/
void WWDG_ClearFlag(void)
{
R8_SAFE_ACCESS_SIG = SAFE_ACCESS_SIG1;
R8_SAFE_ACCESS_SIG = SAFE_ACCESS_SIG2;
SAFEOPERATE;
R8_RST_WDOG_CTRL |= RB_WDOG_INT_FLAG;
R8_SAFE_ACCESS_SIG = 0;
}
/*********************************************************************
* @fn HardFault_Handler
*
* @brief Ӳ<><D3B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>жϣ<D0B6><CFA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD>и<EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>Ϊ<EFBFBD>ϵ縴λ
*
* @param none
*
* @return none
*/
__INTERRUPT
__HIGH_CODE
void HardFault_Handler(void)
{
FLASH_ROM_SW_RESET();
R8_SAFE_ACCESS_SIG = SAFE_ACCESS_SIG1;
R8_SAFE_ACCESS_SIG = SAFE_ACCESS_SIG2;
SAFEOPERATE;
R16_INT32K_TUNE = 0xFFFF;
R8_RST_WDOG_CTRL |= RB_SOFTWARE_RESET;
R8_SAFE_ACCESS_SIG = 0;
while(1);
}
/*********************************************************************
* @fn mDelayuS
*
* @brief uS <20><>ʱ
*
* @param t - ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*
* @return none
*/
__HIGH_CODE
void mDelayuS(uint16_t t)
{
uint32_t i;
#if(FREQ_SYS == 60000000)
i = t * 15;
#elif(FREQ_SYS == 48000000)
i = t * 12;
#elif(FREQ_SYS == 40000000)
i = t * 10;
#elif(FREQ_SYS == 32000000)
i = t << 3;
#elif(FREQ_SYS == 24000000)
i = t * 6;
#elif(FREQ_SYS == 16000000)
i = t << 2;
#elif(FREQ_SYS == 8000000)
i = t << 1;
#elif(FREQ_SYS == 4000000)
i = t;
#elif(FREQ_SYS == 2000000)
i = t >> 1;
#elif(FREQ_SYS == 1000000)
i = t >> 2;
#endif
do
{
__nop();
} while(--i);
}
/*********************************************************************
* @fn mDelaymS
*
* @brief mS <20><>ʱ
*
* @param t - ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*
* @return none
*/
__HIGH_CODE
void mDelaymS(uint16_t t)
{
uint16_t i;
for(i = 0; i < t; i++)
{
mDelayuS(1000);
}
}
#ifdef DEBUG
void _putchar(char character)
{
#if DEBUG == Debug_UART0
while(R8_UART0_TFC == UART_FIFO_SIZE); /* <20>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD>ݷ<EFBFBD><DDB7><EFBFBD> */
R8_UART0_THR = (uint8_t)character; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
#elif DEBUG == Debug_UART1
while(R8_UART1_TFC == UART_FIFO_SIZE); /* <20>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD>ݷ<EFBFBD><DDB7><EFBFBD> */
R8_UART1_THR = (uint8_t)character; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
#elif DEBUG == Debug_UART2
while(R8_UART2_TFC == UART_FIFO_SIZE); /* <20>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD>ݷ<EFBFBD><DDB7><EFBFBD> */
R8_UART2_THR = (uint8_t)character; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
#elif DEBUG == Debug_UART3
while(R8_UART3_TFC == UART_FIFO_SIZE); /* <20>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD>ݷ<EFBFBD><DDB7><EFBFBD> */
R8_UART3_THR = (uint8_t)character; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
#endif
}
#endif