SY8837_Demo_For_OurSelf/UsrSrc/discharge/discharge_module.c

242 lines
5.3 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.

/*
******************************************************************************
*
* @file discharge_module.c
* @brief discharge module
* @ic sy8835
*
* @version 1.0
* @date 2024/11/01 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 2024/11/01 Alex build this file
******************************************************************************
*/
#include "discharge_module.h"
#include "vox_module.h"
#include "adc.h"
#include "charger_module.h"
#include "sys_tim.h"
#include "system.h"
#include "led.h"
#include "bat.h"
bit gBoost_Prepared_Flag = 0; //Boost满足开启条件。
#if DISCHARGE_ENABLE
/******************************************************************************\
Macro definitions
\******************************************************************************/
/******************************************************************************\
Variables definitions
\******************************************************************************/
bit gBoost_UVLO_Flag = 0;
/******************************************************************************\
Functions definitions
\******************************************************************************/
/*
*******************************************************************************
* void DisCharge_Boost_Open(uint8_t Boost_ptm ,uint8_t Vout_Cfg)
*
* Description : Boost Open
* VIN 存在时Boost 在电池电压高于 UVLO硬件关闭 2.8/3.0V,硬件清 boost 使能。开
* 软件实现,有 VIN3.6V,无 VIN3.3V)和电池存在的情况下才能开启。
*
* Arguments : Boost_ptm:是否开启直通模式Vout_Cfg:输出电压值
* Returns : NONE
* Notes : NONE
*
*******************************************************************************
*/
void DisCharge_Boost_Open(uint8_t Boost_ptm ,uint8_t Vout_Cfg)
{
if( gBoost_Prepared_Flag ) //Boost需满足开启条件
{
if( Boost_ptm ==ON )
{
BST_EN = Boost_En | Boost_DirIn;
}
else
{
BST_EN &= ~Boost_DirIn;
BST_EN = Boost_En | Vout_Cfg ; //Boost Enable异常时由硬件主动关闭。
}
#ifdef _DEBUG_DISCHARGE
printf("Bst Open\r\n");
#endif
}
}
/*
*******************************************************************************
* void DisCharge_Boost_Close(void)
*
* Description : Boost Close主要是出现异常时由硬件主动关闭软件在电池LOW电量状态下是否需要关闭boost输出
*
* Arguments :
* Returns :
* Notes :
*
*******************************************************************************
*/
void DisCharge_Boost_Close(void)
{
if( (BST_EN & Boost_En) && gBoost_Prepared_Flag )
{
BST_EN = 0x00; //Boost Disable
gBoost_Prepared_Flag = 0;
#ifdef _DEBUG_DISCHARGE
printf("Bst Close\r\n");
#endif
}
}
/*
*******************************************************************************
* void DisCharge_Handler(void)
*
* Description : Boost放电的保护功能包括输出短路保护电池 UVLO 保护,放电过流保护,过温保护。
*
* Arguments : NONE
* Returns : NONE
* Notes : NONE
*
*******************************************************************************
*/
/*
1、输出短路保护
2、输出过流保护
3、电池UVLO保护
4、过温保护
5、电池低电保护
*/
void DisCharge_Handler(void)
{
/*放电过流保护 --- 硬件实现*/
#if 0
gBoost_Prepared_Flag = 1;
#else
gBoost_Prepared_Flag = 0;
/* 过温保护 --- 硬件实现 */
if( CHIP_STA3 & 0x02 )
{
return;
}
/*Boost Input UVLO 硬件实现*/
if( (CHIP_STA0 & BOOST_BATUV) ) //用于清软件标志位。
{
gBoost_UVLO_Flag = 1;
return;
}
#if 0
/* BAT LOW Interrupt */
if( IRQ_FLAG0 & BAT_Low )
{
return;
}
#endif
/*输出短路保护 (使用中断标志位Boost短路中断发生后会锁存需要写1清零。)-- 短路保护后一直打嗝或者打嗝BOOST_HICCUP_TIM次之后disable boost打嗝时间 250ms软件实现。*/
if( IRQ_FLAG0 & Boost_OutPut_Short )
{
#ifdef _DEBUG_DISCHARGE
printf("Boost Pro Output Short!\r\n");
#endif
if( F_sys_tim_250ms )
{
IRQ_FLAG0 = Boost_OutPut_Short;
DisCharge_Boost_Open(OFF, Boost_Vout_5_05V); //250ms定时开一次Boost这个VOUT输出电压是直接输出固定值还是输出跟随充当前电压值
}
return;
}
#ifdef NTC_ENABLE
/*NTC Handle*/ //使能Boost前先检测NTC温度异常就不开放电。
if( PMU_NTC_Handle( DISCHG_ON ) ) //NTC保护不放电。
{
DisCharge_Boost_Close();
NTC_Pro_Flag = 1;
return;
}
NTC_Pro_Flag = 0;
#endif
#if BAT_VALUE
if( F_batlevel_protect )
{
DisCharge_Boost_Close();
return;
}
#endif
gBoost_Prepared_Flag = 1;
/*
VIN 存在时Boost 在电池电压高于 UVLO硬件关闭 2.8/3.0V,硬件清 boost 使能。开
软件实现,有 VIN3.6V,无 VIN3.3V)和电池存在的情况下才能开启。
软件上只作有 VIN3.6V开boost无 VIN3.3V开boost逻辑。
只关注VIN存在时的UVLO恢复Boost情况。无VIN情况下由硬件处理。
*/
if ( gBoost_UVLO_Flag )
{
if( ChgStatus == CHG_STA_ING ) //充电中
{
if( g_Vbat_Adc >= OPEN_BOOST_VIN )
{
gBoost_Prepared_Flag = 1;
gBoost_UVLO_Flag = 0;
}
else
{
DisCharge_Boost_Close(); //Boost Disable
gBoost_Prepared_Flag = 0;
}
}
}
#endif
}
#endif