2024-12-02 16:26:55 +08:00
|
|
|
|
/********************************** (C) COPYRIGHT *******************************
|
|
|
|
|
* File Name : SLEEP.c
|
|
|
|
|
* Author : WCH
|
|
|
|
|
* Version : V1.2
|
|
|
|
|
* Date : 2022/01/18
|
|
|
|
|
* Description : ˯<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ü<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>
|
|
|
|
|
*********************************************************************************
|
|
|
|
|
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
2024-12-02 17:03:41 +08:00
|
|
|
|
* Attention: This software (modified or not) and binary are used for
|
2024-12-02 16:26:55 +08:00
|
|
|
|
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
/* ͷ<>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD> */
|
|
|
|
|
#include "HAL.h"
|
|
|
|
|
|
2024-12-14 13:31:23 +08:00
|
|
|
|
#include "bsp_key.h"
|
|
|
|
|
|
2024-12-14 16:00:58 +08:00
|
|
|
|
#include "log.h"
|
|
|
|
|
#include "bsp_uart.h"
|
2024-12-14 13:31:23 +08:00
|
|
|
|
|
2024-12-17 14:51:12 +08:00
|
|
|
|
#include "bsp_beep_led_emv.h"
|
|
|
|
|
|
2024-12-14 16:00:58 +08:00
|
|
|
|
#undef LOG_ENABLE
|
|
|
|
|
#define LOG_ENABLE 1
|
|
|
|
|
|
|
|
|
|
#undef LOG_TAG
|
|
|
|
|
#define LOG_TAG "sleep"
|
|
|
|
|
|
|
|
|
|
static volatile bool block_sleep_flag = false;
|
2024-12-17 14:51:12 +08:00
|
|
|
|
static volatile bool block_boost_flag = false;
|
2024-12-14 16:00:58 +08:00
|
|
|
|
|
2025-02-21 14:38:41 +08:00
|
|
|
|
__HIGH_CODE
|
2024-12-14 16:00:58 +08:00
|
|
|
|
void BSP_RequestSleep(void)
|
|
|
|
|
{
|
|
|
|
|
block_sleep_flag = false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-21 14:38:41 +08:00
|
|
|
|
__HIGH_CODE
|
2024-12-14 16:00:58 +08:00
|
|
|
|
void BSP_BlockSleep(void)
|
|
|
|
|
{
|
|
|
|
|
block_sleep_flag = true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-21 14:38:41 +08:00
|
|
|
|
__HIGH_CODE
|
2024-12-17 14:51:12 +08:00
|
|
|
|
void BSP_RequestBoost(void)
|
|
|
|
|
{
|
|
|
|
|
BOOST_EN;
|
|
|
|
|
block_boost_flag = false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-21 14:38:41 +08:00
|
|
|
|
__HIGH_CODE
|
2024-12-17 14:51:12 +08:00
|
|
|
|
void BSP_NoNeedBoost(void)
|
|
|
|
|
{
|
|
|
|
|
block_boost_flag = true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-12 13:58:43 +08:00
|
|
|
|
|
|
|
|
|
pfnLowPowerGapProcessCB_t LowPowerGapProcess;
|
2024-12-15 17:22:09 +08:00
|
|
|
|
|
2024-12-02 16:26:55 +08:00
|
|
|
|
/*******************************************************************************
|
2025-02-21 14:38:41 +08:00
|
|
|
|
* @fn CH58x_LowPower
|
2024-12-02 16:26:55 +08:00
|
|
|
|
*
|
|
|
|
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>˯<EFBFBD><EFBFBD>
|
|
|
|
|
*
|
2025-02-21 14:38:41 +08:00
|
|
|
|
* @param time - <EFBFBD><EFBFBD><EFBFBD>ѵ<EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>㣨RTC<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD>
|
2024-12-02 16:26:55 +08:00
|
|
|
|
*
|
|
|
|
|
* @return state.
|
|
|
|
|
*/
|
2025-02-21 14:38:41 +08:00
|
|
|
|
__HIGH_CODE
|
|
|
|
|
uint32_t CH58x_LowPower(uint32_t time)
|
2024-12-02 16:26:55 +08:00
|
|
|
|
{
|
2025-02-21 14:38:41 +08:00
|
|
|
|
// BSP_UART1_TxLowPower();
|
2024-12-17 14:51:12 +08:00
|
|
|
|
if (block_boost_flag == false)
|
|
|
|
|
{
|
|
|
|
|
BOOST_EN;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
BOOST_OFF_DEINIT;
|
|
|
|
|
}
|
2025-02-21 14:38:41 +08:00
|
|
|
|
if (true == block_sleep_flag)
|
2024-12-14 16:00:58 +08:00
|
|
|
|
{
|
|
|
|
|
// logDebug("block_sleep");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2024-12-02 17:03:41 +08:00
|
|
|
|
#if (defined(HAL_SLEEP)) && (HAL_SLEEP == TRUE)
|
2024-12-02 16:26:55 +08:00
|
|
|
|
volatile uint32_t i;
|
2025-02-21 14:38:41 +08:00
|
|
|
|
uint32_t time_tign, time_sleep, time_curr;
|
2024-12-02 16:26:55 +08:00
|
|
|
|
unsigned long irq_status;
|
2024-12-02 17:03:41 +08:00
|
|
|
|
|
2024-12-02 16:26:55 +08:00
|
|
|
|
// <20><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>
|
2024-12-02 17:03:41 +08:00
|
|
|
|
if (time <= WAKE_UP_RTC_MAX_TIME)
|
|
|
|
|
{
|
2025-02-21 14:38:41 +08:00
|
|
|
|
time_tign = time + (RTC_MAX_COUNT - WAKE_UP_RTC_MAX_TIME);
|
2024-12-02 17:03:41 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-02-21 14:38:41 +08:00
|
|
|
|
time_tign = time - WAKE_UP_RTC_MAX_TIME;
|
2024-12-02 16:26:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SYS_DisableAllIrq(&irq_status);
|
|
|
|
|
time_curr = RTC_GetCycle32k();
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>˯<EFBFBD><CBAF>ʱ<EFBFBD><CAB1>
|
2025-02-21 14:38:41 +08:00
|
|
|
|
if (time_tign < time_curr)
|
2024-12-02 17:03:41 +08:00
|
|
|
|
{
|
2025-02-21 14:38:41 +08:00
|
|
|
|
time_sleep = time_tign + (RTC_MAX_COUNT - time_curr);
|
2024-12-02 17:03:41 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-02-21 14:38:41 +08:00
|
|
|
|
time_sleep = time_tign - time_curr;
|
2024-12-02 16:26:55 +08:00
|
|
|
|
}
|
2024-12-02 17:03:41 +08:00
|
|
|
|
|
2024-12-02 16:26:55 +08:00
|
|
|
|
// <20><>˯<EFBFBD><CBAF>ʱ<EFBFBD><CAB1>С<EFBFBD><D0A1><EFBFBD><EFBFBD>С˯<D0A1><CBAF>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˯<EFBFBD><CBAF>ʱ<EFBFBD>䣬<EFBFBD><E4A3AC><EFBFBD><EFBFBD>˯<EFBFBD><CBAF>
|
2024-12-02 17:03:41 +08:00
|
|
|
|
if ((time_sleep < SLEEP_RTC_MIN_TIME) ||
|
|
|
|
|
(time_sleep > SLEEP_RTC_MAX_TIME))
|
|
|
|
|
{
|
2024-12-02 16:26:55 +08:00
|
|
|
|
SYS_RecoverIrq(irq_status);
|
|
|
|
|
return 2;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-21 14:38:41 +08:00
|
|
|
|
RTC_SetTignTime(time_tign);
|
2024-12-02 16:26:55 +08:00
|
|
|
|
SYS_RecoverIrq(irq_status);
|
2024-12-02 17:03:41 +08:00
|
|
|
|
#if (DEBUG == Debug_UART0) // ʹ<><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD><D3A1>Ϣ<EFBFBD><CFA2>Ҫ<EFBFBD><EFBFBD><DEB8><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD>
|
|
|
|
|
while ((R8_UART0_LSR & RB_LSR_TX_ALL_EMP) == 0)
|
|
|
|
|
{
|
|
|
|
|
__nop();
|
|
|
|
|
}
|
|
|
|
|
#elif (DEBUG == Debug_UART1) // ʹ<><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD><D3A1>Ϣ<EFBFBD><CFA2>Ҫ<EFBFBD><EFBFBD><DEB8><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD>
|
|
|
|
|
while ((R8_UART1_LSR & RB_LSR_TX_ALL_EMP) == 0)
|
2024-12-02 16:26:55 +08:00
|
|
|
|
{
|
|
|
|
|
__nop();
|
|
|
|
|
}
|
2024-12-02 17:03:41 +08:00
|
|
|
|
#endif
|
2024-12-02 16:26:55 +08:00
|
|
|
|
// LOW POWER-sleepģʽ
|
2024-12-02 17:03:41 +08:00
|
|
|
|
if (!RTCTigFlag)
|
2024-12-02 16:26:55 +08:00
|
|
|
|
{
|
2025-02-21 14:38:41 +08:00
|
|
|
|
if (block_boost_flag == false)
|
|
|
|
|
{
|
|
|
|
|
BOOST_EN;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
BOOST_OFF_DEINIT;
|
|
|
|
|
}
|
|
|
|
|
if (true == block_sleep_flag)
|
|
|
|
|
{
|
|
|
|
|
// logDebug("block_sleep");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2024-12-14 16:53:49 +08:00
|
|
|
|
BSP_KEY_EnterLowpower();
|
2024-12-14 13:31:23 +08:00
|
|
|
|
|
2025-04-12 13:58:43 +08:00
|
|
|
|
LowPower_Sleep(RB_PWR_RAM32K | RB_PWR_RAM96K | RB_PWR_EXTEND);
|
|
|
|
|
R8_RTC_FLAG_CTRL = (RB_RTC_TMR_CLR | RB_RTC_TRIG_CLR);
|
|
|
|
|
RTC_SetTignTime(time);
|
|
|
|
|
sys_safe_access_enable();
|
|
|
|
|
R8_HFCK_PWR_CTRL |= RB_CLK_XT32M_KEEP;
|
|
|
|
|
sys_safe_access_disable();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(!RTCTigFlag)
|
|
|
|
|
{
|
|
|
|
|
LowPower_Halt();
|
|
|
|
|
}
|
|
|
|
|
R8_RTC_FLAG_CTRL = (RB_RTC_TMR_CLR | RB_RTC_TRIG_CLR);
|
2024-12-02 16:26:55 +08:00
|
|
|
|
HSECFG_Current(HSE_RCur_100); // <20><>Ϊ<EFBFBD><EFBFBD><EEB6A8><EFBFBD><EFBFBD>(<28><EFBFBD><CDB9>ĺ<EFBFBD><C4BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>HSEƫ<45>õ<EFBFBD><C3B5><EFBFBD>)
|
2025-02-21 14:38:41 +08:00
|
|
|
|
|
2025-04-12 13:58:43 +08:00
|
|
|
|
// i = RTC_GetCycle32k();
|
|
|
|
|
// while (i == RTC_GetCycle32k());
|
2024-12-14 13:31:23 +08:00
|
|
|
|
|
2024-12-02 16:26:55 +08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
return 3;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-12 13:58:43 +08:00
|
|
|
|
/*******************************************************************************
|
|
|
|
|
* @fn LowPowerGapProcess_Register
|
|
|
|
|
*
|
|
|
|
|
* @brief ע<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><EFBFBD>Ѽ<EFBFBD>϶ִ<EFBFBD>лص<EFBFBD>
|
|
|
|
|
*
|
|
|
|
|
* @param None.
|
|
|
|
|
*
|
|
|
|
|
* @return None.
|
|
|
|
|
*/
|
|
|
|
|
void LowPowerGapProcess_Register(pfnLowPowerGapProcessCB_t cb)
|
|
|
|
|
{
|
|
|
|
|
if((uint32_t)cb & 0x20000000)
|
|
|
|
|
{
|
|
|
|
|
LowPowerGapProcess = cb;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-02 16:26:55 +08:00
|
|
|
|
/*******************************************************************************
|
|
|
|
|
* @fn HAL_SleepInit
|
|
|
|
|
*
|
|
|
|
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>˯<EFBFBD><EFBFBD><EFBFBD>ѵķ<EFBFBD>ʽ - RTC<EFBFBD><EFBFBD><EFBFBD>ѣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
|
|
|
|
|
*
|
|
|
|
|
* @param None.
|
|
|
|
|
*
|
|
|
|
|
* @return None.
|
|
|
|
|
*/
|
|
|
|
|
void HAL_SleepInit(void)
|
|
|
|
|
{
|
2025-04-12 13:58:43 +08:00
|
|
|
|
#if(defined(HAL_SLEEP)) && (HAL_SLEEP == TRUE)
|
2024-12-02 16:26:55 +08:00
|
|
|
|
sys_safe_access_enable();
|
|
|
|
|
R8_SLP_WAKE_CTRL |= RB_SLP_RTC_WAKE; // RTC<54><43><EFBFBD><EFBFBD>
|
|
|
|
|
sys_safe_access_disable();
|
|
|
|
|
sys_safe_access_enable();
|
2025-04-12 13:58:43 +08:00
|
|
|
|
R8_RTC_MODE_CTRL |= RB_RTC_TRIG_EN; // <20><><EFBFBD><EFBFBD>ģʽ
|
2024-12-02 16:26:55 +08:00
|
|
|
|
sys_safe_access_disable();
|
|
|
|
|
PFIC_EnableIRQ(RTC_IRQn);
|
|
|
|
|
#endif
|
|
|
|
|
}
|