/* ****************************************************************************** * * @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 \******************************************************************************/ bit gBoost_Prepared_Flag = 0; //Boost满足开启条件。 #ifdef BOOST_AOUT bit gBoost_Opened_Flag = 0; //Boost已正常开启。 #endif bit gBoost_UVLO_Flag = 0; uint8_t Boost_Open_Debounce = 0; /******************************************************************************\ Functions definitions \******************************************************************************/ #ifndef BOOST_AOUT /* ******************************************************************************* * void DisCharge_Boost_Open(void) * * Description : Boost Open * VIN 存在时,Boost 在电池电压高于 UVLO(硬件关闭 2.8/3.0V,硬件清 boost 使能。开 * 软件实现,有 VIN,3.6V,无 VIN,3.3V)和电池存在的情况下才能开启。 * * Arguments : NONE * Returns : NONE * Notes : NONE * ******************************************************************************* */ void DisCharge_Boost_Open(void) { if( gBoost_Prepared_Flag ) //Boost需满足开启条件 { BST_EN = 1; //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 & 0x01) && gBoost_Prepared_Flag ) { BST_EN = 0; //Boost Disable gBoost_Prepared_Flag = 0; #ifdef _DEBUG_DISCHARGE printf("Bst Close\r\n"); #endif } } #endif /* ******************************************************************************* * void DisCharge_Handler(void) * * Description : Boost放电的保护功能包括:输出短路保护,电池 UVLO 保护,放电过流保护,过温保护。 * * Arguments : * Returns : * Notes : * ******************************************************************************* */ void DisCharge_Handler(void) { /*放电过流保护 --- 硬件实现*/ /*过温保护 --- 硬件实现*/ if( CHIP_STA3 & 0x02 ) { gBoost_Prepared_Flag = 0; #ifdef BOOST_AOUT gBoost_Opened_Flag = 0; #endif return; } /*Boost Input UVLO 硬件实现*/ if( (CHIP_STA0 & 0x80) ) //用于清软件标志位。 { gBoost_Prepared_Flag = 0; #ifdef BOOST_AOUT gBoost_Opened_Flag = 0; #endif gBoost_UVLO_Flag = 1; return; } /*输出短路保护 (使用中断标志位,Boost短路中断发生后会锁存,需要写1清零。)-- 短路保护后一直打嗝或者打嗝BOOST_HICCUP_TIM次之后disable boost,打嗝时间 250ms(软件实现)。*/ if( IRQ_FLAG0 & 0x01 ) { #ifdef _DEBUG_DISCHARGE printf("Boost Pro Output Short!\r\n"); #endif gBoost_Prepared_Flag = 0; #ifdef BOOST_AOUT gBoost_Opened_Flag = 0; #endif //一直打嗝 if( F_sys_tim_250ms ) { F_sys_tim_250ms = 0; IRQ_FLAG0 = 0x01; BST_EN = 1; //250ms定时开一次Boost } return; } gBoost_Prepared_Flag = 1; #if ADC_ENABLE /* VIN 存在时,Boost 在电池电压高于 UVLO(硬件关闭 2.8/3.0V,硬件清 boost 使能。开 软件实现,有 VIN,3.6V,无 VIN,3.3V)和电池存在的情况下才能开启。 软件上只作有 VIN,3.6V开boost,无 VIN,3.3V开boost逻辑。 只关注VIN存在时的UVLO恢复Boost情况。无VIN情况下由硬件处理。 */ if ( gBoost_UVLO_Flag ) { if( CHIP_STA4 & 0x80 ) //Power GOOD { if( Vbat_Adc >= OPEN_BOOST_VIN ) { gBoost_Prepared_Flag = 1; gBoost_UVLO_Flag = 0; } else { BST_EN = 0; //Boost Disable gBoost_Prepared_Flag = 0; #ifdef BOOST_AOUT gBoost_Opened_Flag = 0; #endif } } } #endif #ifdef BOOST_AOUT if( gBoost_Prepared_Flag ) { if( !(BST_EN & 0x01) ) { BST_EN = 1; } Boost_Open_Debounce++; if( Boost_Open_Debounce >= BOOST_OPEN_TIM ) //Boost 开启需要延时一段时间(至少1ms)。 { gBoost_Opened_Flag = 1; Boost_Open_Debounce = 0; } } #endif } #endif