Both_Way_Comm_SY8833/TP3310_Demo.si4project/Backup/sleep(3736).c

210 lines
4.8 KiB
C

/*
******************************************************************************
*
* @file sleep.c
* @brief sleep module
*
*
* @version 1.0
* @date 2023/03/22 17:35:40
* @author Alex Xu
*
* Copyright (c) 2013-2099,Tkplusemi Technology Co.,Ltd.
* All Rights Reserved
*
* History:
* Revision Date Author Desc
* 1.0.0 2023/03/22 Alex build this file
******************************************************************************
*/
#include "sleep.h"
#include "led.h"
#include "system.h"
#include "vox_module.h"
#include "discharge_module.h"
#include "charger_module.h"
#include "bat.h"
#include "hall.h"
#include "gpio.h"
#include "sys_tim.h"
#if SLEEP_ENABLE
/******************************************************************************\
Macro definitions
\******************************************************************************/
/******************************************************************************\
Variables definitions
\******************************************************************************/
uint8_t Boot_ShipMode_Flg = 0; //Wake Up From ShipMode
uint16_t gAwake_Source = 0; //唤醒源标志位
uint8_t Decnt_SleepDelay = 0;
uint8_t Enter_ShipMode_Debounce = 0;
bit Enter_Sleep_Cnt_Restart_Flag = 0;
/******************************************************************************\
Functions definitions
\******************************************************************************/
/*
*******************************************************************************
* void WakeUp_Init_Set(void)
*
* Description : 系统唤醒源使能设置,系统初始化调用。
*
*
* Arguments : NONE
* Returns : NONE
* Notes : NONE
*
*******************************************************************************
*/
void WakeUp_Init_Set(void)
{
//set wakeup
SFRADDR = WKUP_EN0;
SFRDATA = 0x6F; //wake0 up enable.
SFRADDR = WKUP_EN1;
SFRDATA = 0x03; //wake1 up enable.vox output short wake up(bit0:bit1).
SFRADDR = WKUP_EN3;
SFRDATA = 0xFF; //wake3 up enable.bat low,Timer,VOX loadin\loadon wake up.
}
/*
*******************************************************************************
* bool Check_Require_Sleep(void )
*
* Description : 查询休眠唤醒源标志位函数。
*
* Arguments : None
* Returns : None
* Notes : bool Flag
*
*******************************************************************************
*/
bool Check_Require_Sleep(void )
{
bool Ret = 0;
if(Enter_Ship_Mode_Flag)
{
return true;
}
if(Boot_ShipMode_Flg != 0)
{
#ifdef _DEBUG_SLEEP
printf("Wakeup From ShipMode Flag:0x%bx (line:%d).\r\n",Boot_ShipMode_Flg,(uint16)__LINE__);
#endif
Boot_ShipMode_Flg = 0;
/*Coding 需要唤醒处理的功能。*/
#ifdef LED_DISPLAY
LED_G_FLASH(1000,3);
LED_R_FLASH(1000,3);
#endif
return false;
}
if(Protect_Type_Flg || gIrq_Event_Type)
{
#ifdef _DEBUG_SLEEP
printf("Pro_Type(0x%x)(Event_Type:0x%x).\r\n",(uint16_t)Protect_Type_Flg, (uint16_t)gIrq_Event_Type);
#endif
/*处理对应的保护和中断唤醒事件。*/
if(Protect_Type_Flg & DIE_OT_PRO_TYPE) //DIE OT wake up from standby. 。发生 DIE OT 后,关闭 boost 和 vox 等功率路径
{
//DisCharge_Boost_Close(); //Boost Disable
BST_EN = 0; //Boost Disable
#if VOX_ENABLE
VOR_EN_Type(VOX_ADT_Mode);
VOL_EN_Type(VOX_ADT_Mode);
#endif
}
return false;
}
return true;
}
/*
*******************************************************************************
* void Enter_Sleep(void)
*
* Description : 进入休眠函数。
*
* Arguments : None
* Returns : None
* Notes : None
*
*******************************************************************************
*/
void Enter_Sleep(void)
{
if(Enter_Ship_Mode_Flag)
{
if(F_sys_tim_1s)
{
F_sys_tim_1s = 0;
Enter_ShipMode_Debounce++;
if(Enter_ShipMode_Debounce >= ENTER_SHIPMODE_CNT)
{
Boot_ShipMode_Flg = 0;
SFRADDR = PMU_CTL0; //Key 在 shipmode 下 debounce 时间 2s
SFRDATA &= ~0x04;
RST_FLAG = 0xFF; //clear Shipmode reset flag
InSleep_Handler(); //Disable All IRQ
PCON |= 0x02; //Enter ShipMode
OutSleep_Handler();
Enter_Ship_Mode_Flag = 0;
Enter_ShipMode_Debounce = 0;
}
else
if(Enter_ShipMode_Debounce == ENTER_SHIPMODE_CNT - 1)
{
#ifdef _DEBUG_SLEEP
printf("Enter ShipMode!(line:%d)\r\n",__LINE__);
#endif
}
}
}
else
{
InSleep_Handler(); //Disable All IRQ
PCON |= 0x02; //Enter StandbyMode, Only int0 or int1 can wake up system
OutSleep_Handler(); //Enable IRQ
}
}
#endif