152 lines
5.6 KiB
C
152 lines
5.6 KiB
C
#include "../main/SystemInclude.h"
|
||
|
||
/**------------------------------------------------------------------------
|
||
* @brief 低功耗时钟初始化函数
|
||
* @note 初始化结构体-选择时钟源
|
||
* @param
|
||
**/
|
||
static void LPTIM_Init(LPTIM_TypeDef *lptimer)
|
||
{
|
||
LPTIM_InitTypeDef LPTIM_InitStructure; /* 1. 初始化LPTimer Base */
|
||
LPTIM_InitStructure.Mode = LPTIM_MODE_TIMER_COUNTER; // 定时器模式
|
||
#if LPTIM_CLK_ENABLE_LSE
|
||
LHL_RCC_LSEConfig(ENABLE);
|
||
LPTIM_InitStructure.Clock = LPTIM_CLOCKSOURCE_LSE; // LPTIM时钟源LSE
|
||
#else
|
||
LHL_RCC_LSEConfig(DISABLE);
|
||
LPTIM_InitStructure.Clock = LPTIM_CLOCKSOURCE_LSI; // LPTIM时钟源LSI
|
||
#endif
|
||
LPTIM_InitStructure.Prescaler = LPTIM_PRESCALER_DIV1; // 预分频系数2
|
||
LPTIM_InitStructure.FreeRunning = DISABLE; // 禁用自由运行模式
|
||
LHL_LPTIM_Init(lptimer, &LPTIM_InitStructure);
|
||
}
|
||
/*==================================================================================*/
|
||
/*LPTimer1==========================================================================*/
|
||
/*==================================================================================*/
|
||
/**------------------------------------------------------------------------
|
||
* @brief 低功耗时钟初始化函数
|
||
* @note LPTimer 时钟频率32768Hz
|
||
* @param uploadCounter : 计算方法(uploadCounter=32.768 ->t=1ms)
|
||
**/
|
||
void StartLPTimer1(u16 uploadCounter)//125ms 250ms ......
|
||
{
|
||
/* 1. 初始化LPTimer Base */
|
||
LHL_LPTIM_DeInit(LPTIM1);
|
||
LPTIM_Init(LPTIM1);
|
||
/* 2. 开启LPTimer中断 */
|
||
LHL_LPTIM_ITConfig(LPTIM1, ENABLE);
|
||
NVIC_EnableIRQ(LPTIM1_IRQn);
|
||
/* 3. 启动LPTimer1 */
|
||
LHL_LPTIM_Start(LPTIM1, uploadCounter); // LPTIMER1 = (Period+1)/Clock = 32768/32768 = 1S
|
||
}
|
||
|
||
/**------------------------------------------------------------------------
|
||
* @brief 低功耗时钟重置函数(改变定时器周期)
|
||
* @note LPTimer 时钟频率32768Hz
|
||
* @param uploadCounter : 计算方法(uploadCounter=32.768 ->t=1ms)
|
||
**/
|
||
void ResetLPTimer1(u16 uploadCounter)
|
||
{
|
||
LPTIM1->CSR &= ~LPTIM_CSR_TEN_Msk;// 1. 禁用定时器
|
||
LPTIM1->CSR |= LPTIM_CSR_TCF_Msk; // 2. 清除比较中断标志
|
||
LPTIM1->CMR = uploadCounter; // 3. 设置新的比较值
|
||
LPTIM1->CSR |= LPTIM_CSR_TEN_Msk; // 4. 重新使能 → 计数器从 0 开始
|
||
}
|
||
/*==================================================================================*/
|
||
/*LPTimer1==========================================================================*/
|
||
/*==================================================================================*/
|
||
/**------------------------------------------------------------------------
|
||
* @brief 低功耗时钟初始化函数
|
||
* @note LPTimer 时钟频率32768Hz
|
||
* @param uploadCounter : 计算方法(uploadCounter=32.768 ->t=1ms)
|
||
**/
|
||
void StartLPTimer2(u16 uploadCounter)//125ms 250ms ......
|
||
{
|
||
LPTIM_InitTypeDef LPTIM_InitStructure;
|
||
|
||
/* 1. 初始化LPTimer Base */
|
||
LPTIM_InitStructure.Mode = LPTIM_MODE_TIMER_COUNTER; // 定时器模式
|
||
#if 1
|
||
LHL_RCC_LSEConfig(ENABLE);
|
||
LPTIM_InitStructure.Clock = LPTIM_CLOCKSOURCE_LSI; // LPTIM0时钟源LSI
|
||
#else
|
||
LPTIM_InitStructure.Clock = LPTIM_CLOCKSOURCE_LSI; // LPTIM0时钟源LSI
|
||
#endif
|
||
LPTIM_InitStructure.Prescaler = LPTIM_PRESCALER_DIV1; // 预分频系数2
|
||
LPTIM_InitStructure.FreeRunning = DISABLE; // 禁用自由运行模式
|
||
LHL_LPTIM_Init(pLPTIM2, &LPTIM_InitStructure);
|
||
/* 2. 开启LPTimer中断 */
|
||
LHL_LPTIM_ITConfig(pLPTIM2, ENABLE);
|
||
NVIC_EnableIRQ(LPTIM2_IRQn);
|
||
/* 3. 启动LPTimer1和2 */
|
||
LHL_LPTIM_Start(pLPTIM2, uploadCounter); // LPTIMER1 = (Period+1)/Clock = 32768/32768 = 1S
|
||
}
|
||
|
||
/**------------------------------------------------------------------------
|
||
* @brief 低功耗时钟重置函数
|
||
* @note LPTimer 时钟频率32768Hz
|
||
* @param uploadCounter : 计算方法(uploadCounter=32.768 ->t=1ms)
|
||
**/
|
||
void ResetLPTimer2(u16 uploadCounter)
|
||
{
|
||
LPTIM2->CSR &= ~LPTIM_CSR_TEN_Msk;// 1. 禁用定时器
|
||
LPTIM2->CSR |= LPTIM_CSR_TCF_Msk; // 2. 清除比较中断标志
|
||
LPTIM2->CMR = uploadCounter; // 3. 设置新的比较值
|
||
LPTIM2->CSR |= LPTIM_CSR_TEN_Msk; // 4. 重新使能 → 计数器从 0 开始
|
||
}
|
||
|
||
|
||
/*==================================================================================*/
|
||
/*低功耗回调函数====================================================================*/
|
||
/*==================================================================================*/
|
||
|
||
/**------------------------------------------------------------------------
|
||
* @brief 定时器回调函数注册
|
||
* @note
|
||
* @param LPTIM_TypeDef *lptimer:LPTIM1 LPTIM2 ; lptimer_irq_callback_t lptim_irq_callback :自定义回调函数名
|
||
* @example: SHAL_timer_register_irq_callback(pTIM1,callback1);
|
||
**/
|
||
static lptimer_irq_callback_t lptim1_irq_callback,lptim2_irq_callback ;
|
||
|
||
void lptimer_register_irq_callback(LPTIM_TypeDef *lptimer ,lptimer_irq_callback_t lptim_irq_callback)
|
||
{
|
||
if(lptimer == LPTIM1)
|
||
{
|
||
lptim1_irq_callback = lptim_irq_callback;//赋值回调函数
|
||
LHL_LPTIM_ClearPending(LPTIM1);// 清除中断标志
|
||
NVIC_EnableIRQ(LPTIM1_IRQn);//开启中断
|
||
}
|
||
else if(lptimer == LPTIM2)
|
||
{
|
||
lptim2_irq_callback = lptim_irq_callback;
|
||
LHL_LPTIM_ClearPending(LPTIM2);// 清除中断标志
|
||
NVIC_EnableIRQ(LPTIM2_IRQn);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @brief
|
||
* @param
|
||
* @retval
|
||
*/
|
||
void LPTIM1_IRQHandler(void)
|
||
{
|
||
// 如果有中断发生且注册了回调函数
|
||
if (lptim1_irq_callback != NULL) lptim1_irq_callback();// 调用用户注册的回调函数
|
||
|
||
LHL_LPTIM_ClearPending(LPTIM1); // 清除中断标志
|
||
|
||
}
|
||
/**
|
||
* @brief
|
||
* @param
|
||
* @retval
|
||
*/
|
||
void LPTIM2_IRQHandler(void)
|
||
{
|
||
// 如果有中断发生且注册了回调函数
|
||
if (lptim2_irq_callback != NULL) lptim2_irq_callback();// 调用用户注册的回调函数
|
||
|
||
LHL_LPTIM_ClearPending(LPTIM2); // 清除中断标志
|
||
}
|