SY883x_For_Client_JLAB_JS07/UsrSrc/userapp/userapp.c

463 lines
8.7 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 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"
#include "gpio_comm.h"
/******************************************************************************\
Macro definitions
\******************************************************************************/
#define LED_ON_CNT 30 //LEDÏÔʾ2sºóϨÃð
#define LED_ON_KEY_CNT 38
#define LED_ON_LOW_CNT 32
#define LED_OFF_CNT 5
#define LED_KEY_BLINK_CNT 5
#define LED_BLINK_CNT 5
#define LED_PRO_TIM 40
#define LED_PRO_CNT 2
/******************************************************************************\
Variables definitions
\******************************************************************************/
bit LED_On_Flag = 0;
bit Wkup_Earphone_Flag = 0;
/******************************************************************************\
Functions definitions
\******************************************************************************/
#ifdef LED_DISPLAY
/*****************************************************************************
* Function : void LED_Disp_Off(void)
* Description : NONE
* Input : None
* Output : None
* Return : None
* Note : None
*****************************************************************************/
void LED_Disp_Off(void)
{
#ifdef BREATHING_LIGHT
BL_LED.Breathing_Light_On = 0;
#endif
LEDR_OFF();
LEDB_OFF();
#if SLEEP_ENABLE
Enter_Sleep_Cnt_Restart_Flag = 1;
#endif
}
/*****************************************************************************
* Function : void LED_Display_App(void)
* Description : 100msµ÷ÓÃÖÜÆÚ
* Input : None
* Output : None
* Return : None
* Note : None
*****************************************************************************/
void LED_Display_App(void)
{
static uint8_t LED_On_Timer; //ledÁÁʱ¼ä¼ÆÊ±
static bit LED_On_Flag1;
static bit LED_On_Chg_Flag;
static bit LED_On_ChgDone_Flag;
static uint8_t LED_On_Cnt;
/*NTC±£»¤£¬4µÆÒ»Ö±¿ìÉÁ(2Hz)*/
if( NTC_Pro_Flag || Vox_Output_Short_Flag )
{
if( !LED_On_Flag1 )
{
LED_On_Flag1 = 1;
LED_Disp_Off();
}
else
{
if( LED_On_Timer <= LED_PRO_TIM )
{
LED_On_Timer++;
if( (LED_On_Timer % LED_PRO_CNT == 0) )
{
LEDR_TOG();
}
}
else
{
LEDR_OFF();
}
}
#if SLEEP_ENABLE
Enter_Sleep_Cnt_Restart_Flag = 1;
#endif
return;
}
/* ±£»¤Ïûʧºó£¬ÐèÒª¿ªÆô5VÉýѹһ¶Îʱ¼ä¡£ */
if( LED_On_Flag1 )
{
LED_On_Flag1 = 0;
LED_Disp_Off();
LED_On_Chg_Flag = 0;
LED_On_ChgDone_Flag = 0;
LED_On_Flag = 1;
Wkup_Earphone_Flag = 1;
}
if ( CHIP_STA4 & 0x80 ) //³äµçÖÐ
{
if ( Charger_Done ) // ³äµçÍê³É£¬LED2³£ÁÁ
{
if( !LED_On_ChgDone_Flag )
{
LED_On_ChgDone_Flag = 1;
LED_Disp_Off();
}
LEDB_ON();
}
else //³äµçÖУ¬LED1ºôÎü
{
if( !LED_On_Chg_Flag )
{
LED_On_Chg_Flag = 1;
LED_On_ChgDone_Flag = 0;
LED_Disp_Off();
}
else
{
#ifdef BREATHING_LIGHT
BL_LED.Breathing_Light_On = 1;
#endif
}
}
Key_Press_irq = 0;
Key_Press_ll_irq_led = 0;
CoverEvent_Flg_led = 0;
LED_On_Flag = 0;
}
else
{
if( LED_On_Chg_Flag )
{
LED_On_Chg_Flag = 0;
LED_On_ChgDone_Flag = 0;
LED_On_Flag = 1;
Wkup_Earphone_Flag = 1;
}
if( LED_On_Flag )
{
LED_On_Timer = 0;
LED_On_Flag = 0;
LED_Disp_Off();
}
if( F_batlevel_protect )
{
Key_Press_irq = 0;
Key_Press_ll_irq_led = 0;
CoverEvent_Flg_led = 0;
Earphone_Chg_Flag = 0;
LED_On_Flag = 1; //ÇåLEDÏÔʾ
}
else
{
if( (Earphone_Chg_Flag || Key_Press_irq || Key_Press_ll_irq_led || CoverEvent_Flg_led) ) //¶ú»ú³öÈëºÐ¡¢°´¼ü´¥·¢£¬ÏÔʾµÆÐ§
{
if( Key_Press_ll_irq_led )
{
LED_On_Cnt = LED_ON_KEY_CNT + LED_OFF_CNT;
}
else
{
LED_On_Cnt = LED_ON_CNT + LED_OFF_CNT;
}
if( LED_On_Timer < LED_OFF_CNT )
{
LED_On_Timer++;
}
else
if( LED_On_Timer < LED_On_Cnt )
{
LED_On_Timer++;
if( Key_Press_ll_irq_led )
{
if( (LED_On_Timer % LED_KEY_BLINK_CNT == 0) )
{
LEDB_TOG();
}
}
else
if( F_batlevel_low )
{
if( (LED_On_Timer % LED_BLINK_CNT == 0) )
{
LEDR_TOG();
}
}
else
{
LEDB_ON();
}
}
else
{
Key_Press_irq = 0;
Key_Press_ll_irq_led = 0;
CoverEvent_Flg_led = 0;
Earphone_Chg_Flag = 0;
LED_On_Flag = 1; //ÇåLEDÏÔʾ
}
}
}
}
}
#endif
/*****************************************************************************
* Function : UserAPP
* Description : 5msµ÷ÓÃÖÜÆÚ
* Input : None
* Output : None
* Return : None
* Note : None
*****************************************************************************/
void UserAPP(void)
{
/*LEDµÆÏÔ¹¦ÄÜÇý¶¯º¯Êý*/
if(F_sys_tim_1ms)
{
F_sys_tim_1ms = 0;
#if VOX_TX_PATTERN
Gpio_Comm();
#endif
}
if(F_sys_tim_100ms)
{
F_sys_tim_100ms = 0;
#ifdef HALL_ENABLE
Hall_Handler(); //¿ª¹Ø¸Ç¼ì²â
#endif
#if DISCHARGE_ENABLE
DisCharge_Handler(); //·Åµçboost¹¦ÄÜ
#endif
#if CHARGER_ENABLE
Charger_Handler(); //³äµç¹¦ÄÜ
#endif
#ifdef KEY_ENABLE
Key_Handler(); //°´¼ü¼ì²â
#endif
#if ADC_ENABLE
Vbat_Adc = Vbat_Value(); //»ñÈ¡Batµçѹ
#ifdef NTC_ENABLE
NTC_Pro_Flag = PMU_NTC_Control();
#endif
Vref_Adc = ADC_Chn_Data(ADC_VREF); //»ñÈ¡²Î¿¼µçѹ
#endif
#if VOX_ENABLE
Vox_Det_Machine(); //¿ª¹Ø¸ÇºÐ×Ó¹¦ÄÜÂß¼­¡£
#endif
#if BAT_VALUE
check_bat_level(); //µçÁ¿¼Æ
#endif
#if LED_DISPLAY
LED_Display_App(); //4¿ÅLEDµÆÏÔʾUI
#endif
}
if( F_sys_tim_1s )
{
F_sys_tim_1s = 0;
#if SLEEP_ENABLE
/*1¡¢»½ÐѺóµ¹ÊýNsºó½øÈëSleep Mode¡£
2¡¢ÓÐVIN´æÔÚ¡¢Boost·Åµç¡¢ÓÐÖжϴ¥·¢µÄÇé¿öÏ£¬Ïµ²»»á½øÈësleep mode¡£
*/
if( Decnt_SleepDelay > 0 )
{
Decnt_SleepDelay--;
/*µ¹Êý½øSleep Mode¹ý³ÌÖгöÏÖÁËKEY\HALL\Loadon\IOFFÖжϣ¬ÔòÖØÐ¼ÆÊ±¡£*/
if( Enter_Sleep_Cnt_Restart_Flag )
{
Enter_Sleep_Cnt_Restart_Flag = 0;
Decnt_SleepDelay = ENTER_STANDBYMODE_CNT;
}
#ifdef _DEBUG_MAIN
printf("sleep decnt:%d,STA0:0x%x,STA1:0x%x,STA2:0x%x,STA4:0x%x\r\n", (uint16_t)Decnt_SleepDelay,(uint16_t)CHIP_STA0,(uint16_t)CHIP_STA1,(uint16_t)CHIP_STA2,(uint16_t)CHIP_STA4);
#endif
return;
}
{
if( Check_Require_Sleep() )
{
/* Boost UVLO±£»¤£¬ÐèÒª¹Ø±ÕADT»½ÐÑ£¬·ñÔò»á³öÏÖ½ø²»ÁËÐÝÃßµÄÎÊÌâ¡£ */
if( CHIP_STA0 & 0x80 )
{
/* Enter Sleep */
SFRADDR = WKUP_EN3; //vox loadon wake up enable
SFRDATA &= ~0x3C;
}
else
{
/* Enter Sleep */
SFRADDR = WKUP_EN3; //vox loadon wake up enable
SFRDATA |= 0x3C;
}
/* InSleep Handler */ //Disable All IRQ
#ifdef TIMER1_ENABLE
ET1 = 0; //timer1 IRQ Disable
#endif
#if UART0_ENABLE
ES0 = 0; //UART0 IRQ Disable
#endif
EX2 = 0;
ET0 = 0; //timer0 IRQ Disable
VOX_CTL0 &= ~0x30; //VOX 5v Off
#if DISCHARGE_ENABLE
BST_EN = 0; //Boost Disable
#endif
SFRADDR = ADC_CTL0;
SFRDATA &= ~0x80; //ADC Disable.
SFRADDR = PMU_CTL0; //оƬӲ¼þÐèÇ󣬹̶¨
SFRDATA &= ~0x30;
PCON |= 0x02; //Enter StandbyMode, Only int0 or int1 can wake up system
SFRADDR = PMU_CTL0; //оƬӲ¼þÐèÇ󣬹̶¨
SFRDATA |= 0x30;
/* OutSleep Handler */ //Enable IRQ
#if UART0_ENABLE
ES0 = 1; //UART0 IRQ
#endif
EX2 = 1;
ET0 = 1; //timer0 IRQ
#ifdef TIMER1_ENABLE
ET1 = 1; //timer1 IRQ
#endif
Decnt_SleepDelay = ENTER_STANDBYMODE_CNT;
}
else
{
Decnt_SleepDelay = ENTER_STANDBYMODE_CNT;
}
}
#endif
}
}