R1UWaterRS485/hardinterface/src/UWater_lptimer_driver.c

204 lines
6.0 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.

/*******************************************************************************
* @Copyright (c) :(C)2020, Qingdao ieslab Co., Ltd
* @FileName :hc32_rtcc_driver.c
* @Author :Kv-L
* @Version :V1.0
* @Date :2020-07-01 15:30:52
* @Description :the function of the entity of GP22Gas_rtcc_driver.c
*******************************************************************************/
#ifdef __cplusplus
extern "C"
{
#endif
/* Includes ------------------------------------------------------------------*/
#include "UWater_lptimer_driver.h"
/* Private variables ---------------------------------------------------------*/
u8 lpTimer_flag = FALSE;
/* Private function prototypes -----------------------------------------------*/
//用于定时,基本定时器使用基时2个32.768CLK
void LPTimer0Init(u8 Rtc_Clk_Source, u32 time)
{
stc_lptim_cfg_t stcLptCfg;
DDL_ZERO_STRUCT(stcLptCfg);
///< 使能LPTIM0 外设时钟
Sysctrl_SetPeripheralGate(SysctrlPeripheralLpTim0, TRUE);
stcLptCfg.enPrs = LptimPrsDiv256; //256分频 32768/256 = 128
stcLptCfg.enGate = LptimGateLow;
stcLptCfg.enGatep = LptimGatePLow;
if(Rtc_Clk_Source)//内部低速时钟
{
stcLptCfg.enTcksel = LptimRcl; //选择内部低速 RC 时钟 RCL 32768 或 38400 本机选择32768(Clk_Init中配置)
}
else//外部低速时钟
{
stcLptCfg.enTcksel = LptimXtl; //选择外部低速 RC 时钟 RCL 32768 或 38400 本机选择32768
}
//stcLptCfg.enTcksel = LptimXtl; //选择外部低速 RC 时钟 RCL 32768 或 38400 本机选择32768
//stcLptCfg.enTcksel = LptimRcl; //选择内部低速 RC 时钟 RCL 32768 或 38400 本机选择32768
stcLptCfg.enTogen = LptimTogEnLow;
stcLptCfg.enCt = LptimTimerFun; //定时器功能
stcLptCfg.enMd = LptimMode2; //工作模式为模式2自动重装载16位计数器/定时器
stcLptCfg.u16Arr = 65536 - time; //预装载寄存器值
Lptim_Init(M0P_LPTIMER0, &stcLptCfg);
Lptim_ClrItStatus(M0P_LPTIMER0); //清除中断标志位
Lptim_ConfIt(M0P_LPTIMER0, TRUE); //允许LPTIMER中断
EnableNvic(LPTIM_0_1_IRQn, IrqLevel3, TRUE);
Lptim_Cmd(M0P_LPTIMER0, TRUE);
// while (!M0P_LPTIMER0->CR_f.WT_FLAG)
// {
// ;
// }
// M0P_LPTIMER0->ARR = 65536 - time;
// M0P_LPTIMER0->CNT = 0;
// M0P_LPTIMER0->CR = 0x422; //中断使能 时钟选择XTL 16位定时重载方式
// M0P_LPTIMER0->CR_f.PRS = 2; //1
// M0P_LPTIMER0->ICLR = 0;
// EnableNvic(LPTIM_0_1_IRQn, IrqLevel3, TRUE);
}
/****************************************************************************
Function: LPTimer1Init
Description: 低功耗时钟1初始化
Input:
Return:
Others:
*****************************************************************************/
void LPTimer1Init(u8 Rtc_Clk_Source)
{
stc_lptim_cfg_t stcLptCfg;
DDL_ZERO_STRUCT(stcLptCfg);
///< 使能LPTIM1 外设时钟
Sysctrl_SetPeripheralGate(SysctrlPeripheralLpTim1, TRUE);
stcLptCfg.enPrs = LptimPrsDiv256; //256分频 32768/256 = 128
stcLptCfg.enGate = LptimGateLow;
stcLptCfg.enGatep = LptimGatePLow;
if(Rtc_Clk_Source)//内部低速时钟
{
stcLptCfg.enTcksel = LptimRcl; //选择内部低速 RC 时钟 RCL 32768 或 38400 本机选择32768(Clk_Init中配置)
}
else//外部低速时钟
{
stcLptCfg.enTcksel = LptimXtl; //选择外部低速 RC 时钟 RCL 32768 或 38400 本机选择32768
}
stcLptCfg.enTogen = LptimTogEnLow;
stcLptCfg.enCt = LptimTimerFun; //计时器功能
stcLptCfg.enMd = LptimMode1; //工作模式为模式116 位自由计数模式
stcLptCfg.u16Arr = 0; //预装载寄存器值
Lptim_Init(M0P_LPTIMER1, &stcLptCfg);
Lptim_ClrItStatus(M0P_LPTIMER1); //清除中断标志位
Lptim_ConfIt(M0P_LPTIMER1, FALSE); //取消LPTIMER1中断
EnableNvic(LPTIM_0_1_IRQn, IrqLevel3, TRUE);
Lptim_Cmd(M0P_LPTIMER1, TRUE);
}
/*************************************************
Function: u32 LPTimerGetTick(u8 timerNum)
Description: 获取当前计数器的数值
Input
Return:
Others:
*************************************************/
u16 LPTimer1GetTick()
{
u16 b;
b = M0P_LPTIMER1->CNT;
return b ;
}
void LPTimer0_Work(u8 enCmd)
{
if (enCmd == TRUE)
{
Sysctrl_SetPeripheralGate(SysctrlPeripheralLpTim0, TRUE);
Lptim_Cmd(M0P_LPTIMER0, TRUE);
}
else
{
Sysctrl_SetPeripheralGate(SysctrlPeripheralLpTim0, FALSE); //关时钟
Lptim_Cmd(M0P_LPTIMER0, FALSE);
}
}
u8 GetLpTimerFlag(void)
{
return lpTimer_flag;
}
void ClearLpTimerFlag(void)
{
lpTimer_flag = FALSE;
}
/*******************************************************************************
* @fun_name LpTim0_IRQHandler
* @brief
* @param[in] None
* @param[out] None
* @retval None
* @other None
*******************************************************************************/
void LpTim0_IRQHandler(void)
{
if(Lptim_GetItStatus(M0P_LPTIMER0))
{
Lptim_ClrItStatus(M0P_LPTIMER0);
lpTimer_flag = TRUE;
}
}
/*******************************************************************************
* @fun_name LpTim0WakeSleep
* @brief 唤醒后处理
* @param[in] None
* @param[out] None
* @retval None
* @other None
*******************************************************************************/
void LpTim0WakeSleep(void)
{
;//唤醒不开启LpTime0使用前再开启。
}
/*******************************************************************************
* @fun_name LPTPreSleep
* @brief LPT休眠前处理
* @param[in] None
* @param[out] None
* @retval None
* @other None
*******************************************************************************/
void LpTime0PreSleep(void)
{
Lptim_Cmd(M0P_LPTIMER0, FALSE);
Sysctrl_SetPeripheralGate(SysctrlPeripheralLpTim0, FALSE); //关时钟
}
/*******************************************************************************
* @fun_name LpTim0IfSleep
* @brief 是否允许休眠
* @param[in] None
* @param[out] None
* @retval None
* @other None
*******************************************************************************/
u8 LpTime0IfSleep(void)
{
return TRUE;
}
#ifdef __cplusplus
}
#endif