IoT_SCV_CH584M/HAL/SLEEP.c

110 lines
3.6 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 : SLEEP.c
* Author : WCH
* Version : V1.2
* Date : 2022/01/18
* Description : 睡锟斤拷锟斤拷锟矫硷拷锟斤拷锟绞硷拷锟<E68BB7>
*********************************************************************************
* 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 "HAL.h"
#include "bsp_wf5803.h"
/*******************************************************************************
* @fn CH58x_LowPower
*
* @brief 锟斤拷锟斤拷睡锟斤拷
*
* @param time - 锟斤拷锟窖碉拷时锟斤拷悖≧TC锟斤拷锟斤拷值锟斤拷
*
* @return state.
*/
__HIGH_CODE
uint32_t CH58x_LowPower(uint32_t time)
{
#if(defined(HAL_SLEEP)) && (HAL_SLEEP == TRUE)
volatile uint32_t i;
uint32_t time_tign, time_sleep, time_curr;
unsigned long irq_status;
// 锟斤拷前锟斤拷锟斤拷
if (time <= WAKE_UP_RTC_MAX_TIME) {
time_tign = time + (RTC_MAX_COUNT - WAKE_UP_RTC_MAX_TIME);
} else {
time_tign = time - WAKE_UP_RTC_MAX_TIME;
}
SYS_DisableAllIrq(&irq_status);
time_curr = RTC_GetCycle32k();
// 锟斤拷锟剿拷锟绞憋拷锟<E68BB7>
if (time_tign < time_curr) {
time_sleep = time_tign + (RTC_MAX_COUNT - time_curr);
} else {
time_sleep = time_tign - time_curr;
}
// 锟斤拷睡锟斤拷时锟斤拷小锟斤拷锟斤拷小睡锟斤拷时锟斤拷锟斤拷锟斤拷锟斤拷锟剿拷锟绞憋拷洌拷锟剿拷锟<E68BB7>
if ((time_sleep < SLEEP_RTC_MIN_TIME) ||
(time_sleep > SLEEP_RTC_MAX_TIME)) {
SYS_RecoverIrq(irq_status);
return 2;
}
RTC_SetTignTime(time_tign);
SYS_RecoverIrq(irq_status);
// 这里只写了uart0的其他的也要有
#if(DEBUG == Debug_UART0) // 使锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟接★拷锟较拷锟揭拷薷锟斤拷锟斤拷写锟斤拷锟<E68BB7>
while((R8_UART0_LSR & RB_LSR_TX_ALL_EMP) == 0)
{
__nop();
}
#elif (DEBUG == Debug_UART3)
while((R8_UART3_LSR & RB_LSR_TX_ALL_EMP) == 0)
{
__nop();
}
#endif
// LOW POWER-sleep模式
if(!RTCTigFlag)
{
// 杩欓噷鑷繁娣诲姞鐨勶紝TMOS浼氳皟鐢<E79A9F>
PRESS_LowPower();
LowPower_Sleep(RB_PWR_RAM32K | RB_PWR_RAM96K | RB_PWR_EXTEND |RB_XT_PRE_EN );
HSECFG_Current(HSE_RCur_100); // 锟斤拷为锟筋定锟斤拷锟斤拷(锟酵癸拷锟侥猴拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷HSE偏锟矫碉拷锟斤拷)
return 0;
}
#endif
return 3;
}
/*******************************************************************************
* @fn HAL_SleepInit
*
* @brief 锟斤拷锟斤拷睡锟竭伙拷锟窖的凤拷式 - RTC锟斤拷锟窖拷锟斤拷锟斤拷模式
*
* @param None.
*
* @return None.
*/
void HAL_SleepInit(void)
{
#if(defined(HAL_SLEEP)) && (HAL_SLEEP == TRUE)
sys_safe_access_enable();
R8_SLP_WAKE_CTRL |= RB_SLP_RTC_WAKE; // RTC锟斤拷锟斤拷
sys_safe_access_disable();
sys_safe_access_enable();
R8_RTC_MODE_CTRL |= RB_RTC_TRIG_EN; // 锟斤拷锟斤拷模式
sys_safe_access_disable();
PFIC_EnableIRQ(RTC_IRQn);
#endif
}