IoT_SCV_CH584M/HAL/SLEEP.c

110 lines
3.6 KiB
C
Raw Normal View History

2024-11-14 09:47:21 +08:00
/********************************** (C) COPYRIGHT *******************************
* File Name : SLEEP.c
* Author : WCH
* Version : V1.2
* Date : 2022/01/18
2024-12-01 14:00:17 +08:00
* Description : <EFBFBD>
2024-11-14 09:47:21 +08:00
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
2024-11-14 09:47:21 +08:00
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
/******************************************************************************/
2024-12-01 14:00:17 +08:00
/* 头锟侥硷拷锟斤拷锟斤拷 */
2024-11-14 09:47:21 +08:00
#include "HAL.h"
#include "bsp_wf5803.h"
2024-11-14 09:47:21 +08:00
/*******************************************************************************
* @fn CH58x_LowPower
*
2024-12-01 14:00:17 +08:00
* @brief
2024-11-14 09:47:21 +08:00
*
2024-12-01 14:00:17 +08:00
* @param time - TC锟斤拷锟斤拷值锟斤拷
2024-11-14 09:47:21 +08:00
*
* @return state.
*/
__HIGH_CODE
uint32_t CH58x_LowPower(uint32_t time)
{
2024-12-01 14:00:17 +08:00
#if(defined(HAL_SLEEP)) && (HAL_SLEEP == TRUE)
2024-11-14 09:47:21 +08:00
volatile uint32_t i;
uint32_t time_tign, time_sleep, time_curr;
unsigned long irq_status;
2024-12-01 14:00:17 +08:00
// 锟斤拷前锟斤拷锟斤拷
2024-11-14 09:47:21 +08:00
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();
2024-12-01 14:00:17 +08:00
// 锟斤拷锟剿拷锟绞憋拷锟<E68BB7>
2024-11-14 09:47:21 +08:00
if (time_tign < time_curr) {
time_sleep = time_tign + (RTC_MAX_COUNT - time_curr);
} else {
time_sleep = time_tign - time_curr;
}
2024-12-01 14:00:17 +08:00
// 锟斤拷睡锟斤拷时锟斤拷小锟斤拷锟斤拷小睡锟斤拷时锟斤拷锟斤拷锟斤拷锟斤拷锟剿拷锟绞憋拷洌拷锟剿拷锟<E68BB7>
if ((time_sleep < SLEEP_RTC_MIN_TIME) ||
2024-11-14 09:47:21 +08:00
(time_sleep > SLEEP_RTC_MAX_TIME)) {
SYS_RecoverIrq(irq_status);
return 2;
}
RTC_SetTignTime(time_tign);
SYS_RecoverIrq(irq_status);
2024-12-01 14:00:17 +08:00
// 这里只写了uart0的其他的也要有
#if(DEBUG == Debug_UART0) // 使锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟接★拷锟较拷锟揭拷薷锟斤拷锟斤拷写锟斤拷锟<E68BB7>
2024-11-14 09:47:21 +08:00
while((R8_UART0_LSR & RB_LSR_TX_ALL_EMP) == 0)
{
__nop();
}
2024-12-01 14:00:17 +08:00
#elif (DEBUG == Debug_UART3)
while((R8_UART3_LSR & RB_LSR_TX_ALL_EMP) == 0)
{
__nop();
}
2024-11-14 09:47:21 +08:00
#endif
2024-12-01 14:00:17 +08:00
// LOW POWER-sleep模式
2024-11-14 09:47:21 +08:00
if(!RTCTigFlag)
{
2024-12-01 14:00:17 +08:00
// 杩欓噷鑷繁娣诲姞鐨勶紝TMOS浼氳皟鐢<E79A9F>
2024-11-19 19:01:02 +08:00
PRESS_LowPower();
2024-11-14 09:47:21 +08:00
LowPower_Sleep(RB_PWR_RAM32K | RB_PWR_RAM96K | RB_PWR_EXTEND |RB_XT_PRE_EN );
2024-12-01 14:00:17 +08:00
HSECFG_Current(HSE_RCur_100); // 锟斤拷为锟筋定锟斤拷锟斤拷(锟酵癸拷锟侥猴拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷HSE偏锟矫碉拷锟斤拷)
2024-11-14 09:47:21 +08:00
return 0;
}
#endif
return 3;
}
/*******************************************************************************
* @fn HAL_SleepInit
*
2024-12-01 14:00:17 +08:00
* @brief - RTC锟斤拷锟窖拷锟斤拷锟斤拷模式
2024-11-14 09:47:21 +08:00
*
* @param None.
*
* @return None.
*/
void HAL_SleepInit(void)
{
#if(defined(HAL_SLEEP)) && (HAL_SLEEP == TRUE)
sys_safe_access_enable();
2024-12-01 14:00:17 +08:00
R8_SLP_WAKE_CTRL |= RB_SLP_RTC_WAKE; // RTC锟斤拷锟斤拷
2024-11-14 09:47:21 +08:00
sys_safe_access_disable();
sys_safe_access_enable();
2024-12-01 14:00:17 +08:00
R8_RTC_MODE_CTRL |= RB_RTC_TRIG_EN; // 锟斤拷锟斤拷模式
2024-11-14 09:47:21 +08:00
sys_safe_access_disable();
PFIC_EnableIRQ(RTC_IRQn);
#endif
}