Both_Way_Comm_SY8833/TP3310_Demo.si4project/Backup/userapp(3122).c

314 lines
6.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 UserApp.c
* @brief UserApp module
*
*
* @version 1.0
* @date 2023/03/27 15:00: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/27 Alex build this file
******************************************************************************/
/*_____ I N C L U D E S ____________________________________________________*/
#include "userapp.h"
#include "gpio.h"
#include "adc.h"
#include "hall.h"
#include "key.h"
#include "sleep.h"
#include "system.h"
#include "bat.h"
#include "sys_tim.h"
#include "charger_module.h"
#include "discharge_module.h"
#include "led.h"
#include "vox_module.h"
/******************************************************************************\
Macro definitions
\******************************************************************************/
/******************************************************************************\
Variables definitions
\******************************************************************************/
//uint8_t Waring_Event_Flag = 0; //±¨¾¯Ê¼þ±ê־λ
s_sys_state Systerm_State = {SLEEP_STATE,AWAKE_STATE,AWAKE_STATE};
/******************************************************************************\
Functions definitions
\******************************************************************************/
/*****************************************************************************
* Function : SysTerm_State_Machine
* Description : System State Handle
* Input : None
* Output : None
* Return : None
* Note : None
*****************************************************************************/
#if SLEEP_ENABLE
void SysTem_State_Machine(void)
{
// uint16_t Adc1 = 0,Adc4 = 0;
if(Systerm_State.Current_State != Systerm_State.Next_State)
{
Systerm_State.Pre_State = Systerm_State.Current_State ;
Systerm_State.Current_State = Systerm_State.Next_State ;
switch(Systerm_State.Current_State)
{
case POWER_ON_STATE:
/*
ϵͳÉϵç³õʼ»¯Ïà¹Ø´¦ÀíÂß¼­
ÀýÈ磺LEDÏÔʾUI¡¢ºÍ¶ú»úͨÐŵȹ¦ÄÜ;
ÔÚÕâÀï½øÐмòµ¥Ê¾Àý£¬Á½¸öLEDÏÔʾÉÁ˸2´Î¡£
*/
#ifdef LED_DISPLAY
LED_R_FLASH(1000,2);
LED_G_FLASH(1000,2);
#endif
#ifdef _DEBUG_MAIN
printf("Power On State!Hall(0x%x) (line:%d)\r\n", (uint16_t)CoverStatus,(uint16_t)__LINE__);
#endif
break;
case AWAKE_STATE:
/*
ϵͳ»½ÐѺóÐèÒª´¦ÀíµÄ¹¦ÄÜ£»
1¡¢Ê¹ÄÜÖжϣ»
*/
#ifdef _DEBUG_MAIN
printf("Awake State (line:%d)\r\n", (uint16_t)__LINE__);
#endif
break;
case NORMAL_STATE:
/*ϵͳÕý³£¹¤×÷ÖÐÐèÒª´¦ÀíµÄ¹¦ÄÜ£»
1¡¢³õʼ»¯½øÈëSleepµÄ¶¨Ê±Æ÷£»
*/
Decnt_SleepDelay = ENTER_STANDBYMODE_CNT;
#ifdef _DEBUG_MAIN
printf("Normal State (line:%d)\r\n", (uint16_t)__LINE__);
#endif
break;
case SLEEP_STATE:
/*
½øÈëSleepģʽǰµÄһЩ׼±¸¹¤×÷£»
1¡¢Ê¹ÄÜ»½ÐÑÖжϣ»
*/
WakeUp_Init_Set(); //»½ÐÑÖжÏʹÄÜÉèÖÃ
#ifdef _DEBUG_MAIN
printf("Sleep State (line:%d)\r\n", (uint16_t)__LINE__);
#endif
break;
default:
break;
}
}
else
{
switch(Systerm_State.Current_State)
{
case POWER_ON_STATE:
/*Éϵç³õʼ»¯ÐèÒª´¦ÀíµÄ¹¦ÄÜ£»
1¡¢´¦Àíµç³ØµçÁ¿£»
2¡¢»ñÈ¡shipmode»½ÐѺóµÄ»½Ðѱê־λ£»
*/
#if 0
if(bat_level < BAT_LEVEL_LOW) //20%µçÁ¿£¬½øÐе͵籨¾¯¡£
{
// Waring_Event_Flag |= BIT_WARING_BATLOW_EVENT;
}
#endif
Systerm_State.Next_State = AWAKE_STATE;
break;
case AWAKE_STATE:
Systerm_State.Next_State = NORMAL_STATE;
break;
case NORMAL_STATE:
/*1¡¢»½ÐѺó¹¤×÷N msºó½øÈëSleep Mode,NÐèÒª´óÓÚ100ms¡£
2¡¢ÓÐVIN´æÔÚµÄÇé¿öÏ£¬Ïµ²»»á½øÈësleep mode¡£
*/
// if( (CHIP_STA4 & 0x80) || Vol_LiPro_Flag || Vor_LiPro_Flag) //Power Good
if(CHIP_STA4 & 0x80)
{
Decnt_SleepDelay = ENTER_STANDBYMODE_CNT;
}
else
{
if(F_sys_tim_1s)
{
F_sys_tim_1s = 0;
if(Decnt_SleepDelay > 0)
{
#ifdef _DEBUG_MAIN
printf("sleep decounter: %d,Adc1:%d,CHIP_STA1:0x%x,Adc4:%d\r\n", (uint16_t)Decnt_SleepDelay,(uint16_t)Adc1,(uint16_t)CHIP_STA1,(uint16_t)Adc4);
#endif
Decnt_SleepDelay--;
/*µ¹Êý½øSleep Mode¹ý³ÌÖгöÏÖÁËÖжϣ¬ÔòÖØÐ¼ÆÊ±¡£*/
if(Enter_Sleep_Cnt_Restart_Flag)
{
Enter_Sleep_Cnt_Restart_Flag = 0;
Decnt_SleepDelay = ENTER_STANDBYMODE_CNT;
}
if(Decnt_SleepDelay == 0)
{
#ifdef _DEBUG_MAIN
printf("Enter Standby Mode!\r\n");
#endif
Systerm_State.Next_State = SLEEP_STATE; //½øÈëSleep Mode
/*Çå±ê־λ*/
Protect_Type_Flg = 0;
gIrq_Event_Type = 0;
}
}
}
}
break;
case SLEEP_STATE:
/*
1¡¢¼ì²â»½ÐÑÔ´£¬ÊÇ·ñÐèÒª»½ÐÑϵͳ£»
2¡¢¶¨Ê±»½ÐÑ£¬¸üÐÂµç³ØµçÁ¿£»
*/
if(Check_Require_Sleep())
{
Enter_Sleep();
}
else
{
Systerm_State.Next_State = AWAKE_STATE;
}
break;
default:
break;
}
}
}
#endif
/*
*******************************************************************************
* void System_Drivers_Machine(void)
*
* Description : ϵͳÇý¶¯Ïà¹Ø³ÌÐò´¦Àí¡£ (×îС5msµ÷ÓÃÖÜÆÚ)
*
* Arguments : NONE
* Returns : NONE
* Notes : NONE
*
*******************************************************************************
*/
void System_Drivers_Machine(void)
{
#if KEY_HALL_ENABLE
Key_Handler(); //°´¼ü¼ì²â
Hall_Handler(); //¿ª¹Ø¸Ç¼ì²â
#endif
#if DISCHARGE_ENABLE
DisCharge_Handler(); //·Åµçboost¹¦ÄÜ
#endif
#if CHARGER_ENABLE
Charger_Handler(); //³äµç¹¦ÄÜ
#endif
}
/*****************************************************************************
* Function : UserAPP
* Description : 5msµ÷ÓÃÖÜÆÚ
* Input : None
* Output : None
* Return : None
* Note : None
*****************************************************************************/
void UserAPP(void)
{
static uint8_t Step_Cnt;
uint16_t Vbat_Adc = 0;
if(F_sys_tim_5ms)
{
F_sys_tim_5ms = 0;
switch(Step_Cnt) //case0 µ½case1 ½áÊø£¬ÖÜÆÚ10ms¡£ºóÐøÐèÒªÐÂÔöÆäËü¹¦ÄÜ¿ÉÔö¼Ócase
{
case 0:
System_Drivers_Machine(); //ÓÃÓÚÖ´ÐÐоƬÇý¶¯³ÌÐò
Step_Cnt++;
break;
case 1:
#if SLEEP_ENABLE
SysTem_State_Machine(); //ÓÃÓÚÓ¦ÓóÌÐò
#endif
Step_Cnt = 0;
break;
default:
Step_Cnt = 0;
break;
}
}
if(F_sys_tim_100ms)
{
F_sys_tim_100ms = 0;
#ifdef LED_DISPLAY
LED_Drv();
#endif
#if VOX_ENABLE
Vol_Det_Machine();
Vor_Det_Machine();
#endif
}
#if 0
if(F_sys_tim_1s)
{
F_sys_tim_1s = 0;
#if ADC_ENABLE
Vbat_Adc = Vbat_Value(); //»ñÈ¡Batµçѹ,ÓÃÓÚADC²âÊÔ
#endif
}
#endif
}