Both_Way_Comm_SY8833/TP3310_Demo.si4project/Backup/discharge_module(6754).c

346 lines
7.9 KiB
C
Raw Permalink 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
*
*
* @version 1.0
* @date 2022/07/12 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 2022/07/12 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"
#if DISCHARGE_ENABLE
/******************************************************************************\
Macro definitions
\******************************************************************************/
/******************************************************************************\
Variables definitions
\******************************************************************************/
uint8_t Boost_Short_Tim = 0;
uint8_t Boost_Protect_Type = 0;
bit gBoost_Prepared_Flag = 0; //Boost满足开启条件。
bit gBoost_Opened_Flag = 0; //Boost已正常开启。
bit gBoost_UVLO_Flag = 0;
/******************************************************************************\
Functions definitions
\******************************************************************************/
/*
*******************************************************************************
* void DisCharge_init(void)
*
* Description : DisCharger Initialization
*
* Arguments : uint8_t Boost_Vout:Boost输出电压调节Default:5V00000~00111:4.0V硬件设定最小值01000:4.05 .... 11111:5.2V。设置比电池电压低,会触发短路保护。
* Returns : NONE
* Notes : NONE
*
*******************************************************************************
*/
#if 0
void Boost_Init(void)
{
if(Boost_Vout <= 0x07)
{
BST_EN = 0x01;
}
}
#endif
#if 0
/*
*******************************************************************************
* void DisCharge_Boost_Open(void)
*
* Description : Boost Open
* VIN 存在时Boost 在电池电压高于 UVLO硬件关闭 2.8/3.0V,硬件清 boost 使能。开
* 软件实现,有 VIN3.6V,无 VIN3.3V)和电池存在的情况下才能开启。
*
* Arguments : NONE
* Returns : NONE
* Notes : NONE
*
*******************************************************************************
*/
void DisCharge_Boost_Open(void)
{
if(!(BST_EN & 0x01) && gBoost_Prepared_Flag) //Boost需满足开启条件
{
BST_EN = 1; //Boost Enable异常时由硬件主动关闭。
#ifdef _DEBUG_DISCHARGE
printf("Boost 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 & 0x01 )
{
BST_EN = 0; //Boost Disable
}
#ifdef _DEBUG_DISCHARGE
printf("Boost Close!\r\n");
#endif
gBoost_Prepared_Flag = 0;
}
#endif
/*
*******************************************************************************
* void DisCharge_Boost_Protect_Handle(void)
*
* Description : Boost放电的保护功能包括输出过压保护输出短路保护电池过压保护电池 UVLO 保护放电过流保护NTC 保护,过温保护。
*
* Arguments :
* Returns :
* Notes :
*
*******************************************************************************
*/
void DisCharge_Boost_Protect_Handle(void)
{
uint8_t Irq_Flag = 0;
uint16_t nNTC_Adc_Value = 0;
uint16_t Vbat_Adc = 0;
uint8_t Ret = 0;
/* 输出过压过流保护 --- 硬件实现,无标志位 */
/*放电过流保护 --- 硬件实现*/
/*过温保护 --- 硬件实现*/
if(CHIP_STA3 & 0x02)
{
gBoost_Prepared_Flag = 0;
gBoost_Opened_Flag = 0;
BST_EN = 0;
#ifdef _DEBUG_DISCHARGE
printf("Boost Pro DIR OT!\r\n");
#endif
return;
}
/*问题:接触保护后,功能是否恢复正常?*/
#if 1
/*NTC 保护*/
Ret = PMU_NTC_Control(); //使能Charger前先检测NTC温度异常就不开Boost。在放电时需要进入 standby 模式,定时器定时唤醒系统,检测 NTC 温度。
if(Ret == FALSE) //NTC保护不充电。
{
gBoost_Prepared_Flag = 0;
gBoost_Opened_Flag = 0;
BST_EN = 0;
#ifdef _DEBUG_DISCHARGE
printf("Boost Pro ntc error!\r\n");
#endif
return;
}
#endif
/*输出短路保护 -- 短路保护后一直打嗝或者打嗝BOOST_HICCUP_TIM次之后disable boost打嗝时间 250ms软件实现。*/
//Reg_Chip_Sta = CHIP_STA0;
if(Protect_Type_Flg & BOOST_OUTPUT_SHORT_PRO_TYPE) /**/
{
#ifdef _DEBUG_DISCHARGE
printf("Boost Pro Output Short!\r\n");
#endif
#if 0 //打嗝BOOST_HICCUP_TIM次后不再开启Boost。需系统重启
if(Boost_Short_Tim <= BOOST_HICCUP_TIM)
{
if(F_sys_tim_250ms)
{
F_sys_tim_250ms = 0;
Protect_Type_Flg &= ~BOOST_OUTPUT_SHORT_PRO_TYPE;
BST_EN = 1; //250ms定时开一次Boost
Boost_Short_Tim++;
}
}
else
{
gBoost_Prepared_Flag = 0;
gBoost_Opened_Flag = 0;
BST_EN = 0;
}
#else //一直打嗝
if(F_sys_tim_250ms)
{
F_sys_tim_250ms = 0;
Protect_Type_Flg &= ~BOOST_OUTPUT_SHORT_PRO_TYPE;
BST_EN = 1; //250ms定时开一次Boost
}
#endif
return;
}
/*Boost Input UVLO 硬件保护*/
if(!(CHIP_STA0 & 0x80))
{
Boost_Protect_Type |= BOOST_UVLO_PRO_TYPE;
gBoost_Prepared_Flag = 0;
gBoost_Opened_Flag = 0;
gBoost_UVLO_Flag = 1; //在电池检测过程中,
BST_EN = 0;
#ifdef _DEBUG_DISCHARGE
printf("Boost Pro Uvlo!\r\n");
#endif
return;
}
#if 1
/*BAT Low*/
if( CHIP_STA2 & 0x04 ) //bat low。不开boost BAT LOW detection enabled only when boost is enabled. 未开Boost在进入Standby Mode后还是会被电池低电唤醒。
{
Boost_Protect_Type |= BAT_LOW_PRO_TYPE;
gBoost_Prepared_Flag = 0;
gBoost_Opened_Flag = 0;
BST_EN = 0;
// LEDR_TOG();
#ifdef _DEBUG_DISCHARGE
printf("BAT LOW Pro!\r\n");
#endif
return;
}
#endif
gBoost_Prepared_Flag = 1; //未发生Boost相关保护可以开启Boost。
if( 1 == gBoost_UVLO_Flag )
{
#if ADC_ENABLE
Vbat_Adc = Vbat_Value(); //获取Bat电压
#endif
/*
VIN 存在时Boost 在电池电压高于 UVLO硬件关闭 2.8/3.0V,硬件清 boost 使能。开
软件实现,有 VIN3.6V,无 VIN3.3V)和电池存在的情况下才能开启。
软件上只作有 VIN3.6V开boost无 VIN3.3V开boost逻辑。
*/
if( CHIP_STA4 & 0x07 ) //Get In Charger Mode
{
if(Vbat_Adc >= OPEN_BOOST_VIN)
{
if(gBoost_Prepared_Flag)
{
gBoost_Opened_Flag = 1;
BST_EN = 1;
gBoost_UVLO_Flag = 0;
Boost_Protect_Type &= ~BOOST_OUTPUT_SHORT_PRO_TYPE;
Boost_Protect_Type &= ~BOOST_UVLO_PRO_TYPE;
Boost_Short_Tim = 0;
}
}
else
{
Boost_Protect_Type |= BOOST_UVLO_PRO_TYPE;
BST_EN = 0; //Boost Disable
gBoost_Prepared_Flag = 0;
gBoost_Opened_Flag = 0;
}
}
else
{
if(Vbat_Adc >= OPEN_BOOST_NOVIN)
{
if(gBoost_Prepared_Flag)
{
gBoost_Opened_Flag = 1;
BST_EN = 1;
gBoost_UVLO_Flag = 0;
Boost_Protect_Type &= ~BOOST_OUTPUT_SHORT_PRO_TYPE;
Boost_Protect_Type &= ~BOOST_UVLO_PRO_TYPE;
Boost_Short_Tim = 0;
}
}
else
{
Boost_Protect_Type |= BOOST_UVLO_PRO_TYPE;
BST_EN = 0; //Boost Disable
gBoost_Prepared_Flag = 0;
gBoost_Opened_Flag = 0;
}
}
}
else
{
if(gBoost_Prepared_Flag)
{
gBoost_Opened_Flag = 1;
BST_EN = 1;
}
}
}
/*
*******************************************************************************
* void DisCharge_While_Charging(void)
*
* Description : 边充边放。
*
* Arguments : uint8_t Ext_MOS --- 0:No External MOS;1:Exist External MOS, GPIO Control.
* Returns : NONE
* Notes : NONE
*
*******************************************************************************
*/
void DisCharge_Handler(void)
{
DisCharge_Boost_Protect_Handle();
}
#endif