356 lines
7.5 KiB
C
356 lines
7.5 KiB
C
/*
|
||
******************************************************************************
|
||
*
|
||
* @file system.c
|
||
* @brief system module
|
||
*
|
||
*
|
||
* @version 1.0
|
||
* @date 2022/08/04 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 2022/08/15 Alex build this file
|
||
* Attention:1、由于同一个Byte的中断标志位,会分配在不同的中断中,对于清中断标志位(写1清),不能用 |= ,需要用 = ;
|
||
例如:IRQ_FLAG10 = 0x02; //Clear bit0,bit1 interrupt flag
|
||
2、外部中断0,主要是用于系统唤醒功能,中断处理函数中无需作逻辑功能处理,必需预留接口!
|
||
******************************************************************************/
|
||
/*_____ I N C L U D E S ____________________________________________________*/
|
||
|
||
#include "system.h"
|
||
#include "gpio.h"
|
||
#include "adc.h"
|
||
#include "hall.h"
|
||
#include "key.h"
|
||
#include "sleep.h"
|
||
#include "uart.h"
|
||
#include "bat.h"
|
||
#include "sys_tim.h"
|
||
#include "charger_module.h"
|
||
#include "discharge_module.h"
|
||
#include "led.h"
|
||
#include "vox_module.h"
|
||
#include "watchdog.h"
|
||
#include "userapp.h"
|
||
|
||
/******************************************************************************\
|
||
Macro definitions
|
||
\******************************************************************************/
|
||
|
||
/******************************************************************************\
|
||
Variables definitions
|
||
\******************************************************************************/
|
||
|
||
idata uint16_t Protect_Type_Flg = 0;
|
||
idata uint8_t gIrq_Event_Type = 0; //中断事件记录
|
||
//idata uint8_t g_Plug_Irq_Flg = 0;
|
||
|
||
/******************************************************************************\
|
||
Functions definitions
|
||
\******************************************************************************/
|
||
|
||
|
||
/*
|
||
*******************************************************************************
|
||
* void System_Init(void)
|
||
*
|
||
* Description : System Initialization
|
||
*
|
||
* Arguments : NONE
|
||
|
||
* Returns : NONE
|
||
|
||
* Notes : NONE
|
||
*
|
||
*******************************************************************************
|
||
*/
|
||
void System_Init(void)
|
||
{
|
||
CLKPRE = 0x00; //frequency division: 2^0, 12M 后续根据系统功耗情况调整。
|
||
|
||
#if UART0_ENABLE
|
||
UART0_Init();
|
||
#endif
|
||
|
||
#if VOX_ENABLE
|
||
Vox_init(); //上电后,尽快初始化VOX为自动识别模式,为后续识别VOX是否有LOADON做好准备。
|
||
#endif
|
||
|
||
Sys_Tim0_Init();
|
||
|
||
#if TIMER1_ENABLE
|
||
Sys_Tim1_Init();
|
||
#endif
|
||
|
||
//Boost_Init();
|
||
|
||
SFRADDR = P1_OE;
|
||
SFRDATA = 0x01;
|
||
#if 0
|
||
|
||
SFRADDR = P0_OE;
|
||
SFRDATA = 0x14;
|
||
|
||
SFRADDR = P0_DRV;
|
||
SFRDATA = 0x33;
|
||
|
||
P0 &= ~0x14;
|
||
|
||
#endif
|
||
|
||
#ifdef LED_DISPLAY
|
||
|
||
LED_Init(); // LED pin 配置为GPIO输出
|
||
|
||
LED_Clr();
|
||
|
||
#endif
|
||
|
||
#ifdef DISPLAY_LED_188
|
||
g_188_Num = 0;
|
||
LED_188_Init();
|
||
#endif
|
||
|
||
#if WTG_ENABLE
|
||
/* 65ms */
|
||
WDTREL = 0x00; //WDTPS = 0,wdt_f = wdt_f1/2;WDTPS = 1,wdt_f = wdt_f1/32;
|
||
/* 1s */
|
||
// WDTREL = 0x80; //WDTPS = 0,wdt_f = wdt_f1/2;WDTPS = 1,wdt_f = wdt_f1/32;
|
||
#endif
|
||
|
||
#if ADC_ENABLE
|
||
Adc_Init();
|
||
#endif
|
||
|
||
#if KEY_HALL_ENABLE
|
||
// Key_Init();
|
||
Hall_Init();
|
||
#endif
|
||
|
||
//Charger_Init();
|
||
/* test,开启IO LDO bias和增加100uA负载。*/
|
||
SFRADDR = 0x89;
|
||
SFRDATA |= 0x80;
|
||
|
||
/* Interrupt Enable 根据应用选择必要的中断 */
|
||
|
||
SFRADDR = IRQ_EN0; //INT0 --- OCP\DIE OT\VBUS\BAT LOW\VOR\VOL\Boost UVLO\Boost output short.
|
||
SFRDATA = 0xFF;
|
||
|
||
SFRADDR = IRQ_EN3; //INT2 --- VOX EDGE LOADON
|
||
SFRDATA = 0x3C;
|
||
|
||
SFRADDR = IRQ_EN4; //INT2 --- VOX IOFF EDGE
|
||
SFRDATA = 0xF0;
|
||
|
||
SFRADDR = IRQ_EN7; //INT4 --- Key Interrupt Flag
|
||
SFRDATA = 0xFC;
|
||
|
||
SFRADDR = IRQ_EN9; //INT7 --- VIN DPM\Temp Loop\BAT Overvoltage\Charge Complete\Fast charge timer expire\Tricle charge timer expire\VBUS power good edge.
|
||
SFRDATA = 0xFF;
|
||
|
||
SFRADDR = IRQ_EN10; //INT3\INT4 --- VBUS plugout\VBUS plugin\Hall
|
||
SFRDATA = 0xFE;
|
||
|
||
/* 开外部中断使能 */
|
||
EX0 = 1;
|
||
EX2 = 1;
|
||
EX3 = 1;
|
||
EX4 = 1;
|
||
EX7 = 1;
|
||
|
||
EAL = 1; //开总中断。
|
||
|
||
#if SLEEP_ENABLE
|
||
Decnt_SleepDelay = ENTER_STANDBYMODE_CNT;
|
||
#endif
|
||
|
||
#ifdef LED_DISPLAY
|
||
LED_B_FLASH(50,5);
|
||
#endif
|
||
}
|
||
|
||
#if 0
|
||
void Delay_us(volatile uint16_t us) //@11.0592MHz
|
||
{
|
||
do
|
||
{
|
||
_nop_();
|
||
_nop_();
|
||
_nop_();
|
||
}while(us--);
|
||
}
|
||
#endif
|
||
/*
|
||
*******************************************************************************
|
||
* void EX0_isr(void ) interrupt Interrupt_Vector_IE0
|
||
*
|
||
* Description : System External Intterupt 0
|
||
*
|
||
* Arguments : NONE
|
||
|
||
* Returns : NONE
|
||
|
||
* Notes : NONE
|
||
*
|
||
*******************************************************************************
|
||
*/
|
||
void EX0_isr(void) interrupt Interrupt_Vector_IE0
|
||
{
|
||
|
||
}
|
||
|
||
/*
|
||
*******************************************************************************
|
||
* void EX2_isr(void) interrupt Interrupt_Vector_IE2
|
||
*
|
||
* Description : System External Intterupt 2
|
||
*
|
||
* Arguments : NONE
|
||
|
||
* Returns : NONE
|
||
|
||
* Notes : NONE
|
||
*
|
||
*******************************************************************************
|
||
*/
|
||
|
||
void EX2_isr(void) interrupt Interrupt_Vector_IE2
|
||
{
|
||
uint8_t IRQ3_Flag = 0;
|
||
uint8_t IRQ4_Flag = 0;
|
||
|
||
IRQ3_Flag = IRQ_FLAG3;
|
||
IRQ_FLAG3 = IRQ3_Flag;
|
||
|
||
IRQ4_Flag = IRQ_FLAG4;
|
||
IRQ_FLAG4 = IRQ4_Flag;
|
||
|
||
if(IRQ3_Flag & 0x3C) //vox loadon interrupt flag
|
||
{
|
||
gIrq_Event_Type |= VOX_LOADON_IRQ_EVENT;
|
||
}
|
||
|
||
if(IRQ4_Flag & 0xF0) //VOx IOFF Edge Interrupt Flag
|
||
{
|
||
gIrq_Event_Type |= VOX_IOFF_IRQ_EVENT;
|
||
}
|
||
|
||
#if SLEEP_ENABLE
|
||
if(!Enter_Sleep_Cnt_Restart_Flag)
|
||
{
|
||
Enter_Sleep_Cnt_Restart_Flag = 1;
|
||
}
|
||
#endif
|
||
}
|
||
|
||
/*
|
||
*******************************************************************************
|
||
* void EX3_isr(void ) interrupt Interrupt_Vector_IE3
|
||
*
|
||
* Description : System External Intterupt 3 ,系统异常中断、电源插拔中断、Standby定时器中断
|
||
*
|
||
* Arguments : NONE
|
||
|
||
* Returns : NONE
|
||
|
||
* Notes : NONE
|
||
*
|
||
*******************************************************************************
|
||
*/
|
||
void EX3_isr(void) interrupt Interrupt_Vector_IE3
|
||
{
|
||
uint8_t IRQ0_Flag = 0;
|
||
uint8_t IRQ10_Flag = 0;
|
||
|
||
IRQ0_Flag = IRQ_FLAG0;
|
||
IRQ_FLAG0 = IRQ0_Flag;
|
||
|
||
IRQ10_Flag = IRQ_FLAG10;
|
||
IRQ_FLAG10 = IRQ10_Flag; //Clear interrupt flag
|
||
|
||
if(IRQ0_Flag != 0)
|
||
{
|
||
/* Coding */
|
||
Protect_Type_Flg |= 0xFF;
|
||
}
|
||
|
||
if( IRQ10_Flag & 0xC0 )
|
||
{
|
||
gIrq_Event_Type |= VBUS_PLUG_IRQ_EVENT;
|
||
}
|
||
|
||
#if SLEEP_ENABLE
|
||
|
||
if(!Enter_Sleep_Cnt_Restart_Flag)
|
||
{
|
||
Enter_Sleep_Cnt_Restart_Flag = 1;
|
||
}
|
||
|
||
#endif
|
||
}
|
||
|
||
/*
|
||
*******************************************************************************
|
||
* void EX7_isr(void ) interrupt Interrupt_Vector_IE7
|
||
*
|
||
* Description : System External Intterupt 7 I2C/PWM/Charger中断
|
||
*
|
||
* Arguments : NONE
|
||
|
||
* Returns : NONE
|
||
|
||
* Notes : NONE
|
||
*
|
||
*******************************************************************************
|
||
*/
|
||
void EX7_isr(void) interrupt Interrupt_Vector_IE7
|
||
{
|
||
uint8_t IRQ9_Flag = 0;
|
||
|
||
IRQ9_Flag = IRQ_FLAG9;
|
||
IRQ_FLAG9 = IRQ9_Flag; //Clear interrupt flag
|
||
|
||
/*VIN DPM*/
|
||
if(IRQ9_Flag & 0x80) //VIN DPM
|
||
{
|
||
/*将充电超时时间设长,具体时间需要测试。*/
|
||
/* Coding */
|
||
Protect_Type_Flg |= VIN_DPM_PRO_TYPE;
|
||
}
|
||
|
||
/*电池过压保护*/
|
||
if(IRQ9_Flag & 0x20)
|
||
{
|
||
/*软件检测到电池过压一段时间,可以关闭充电, 直到电压恢复正常,重新开始充电流程*/
|
||
/* Coding */
|
||
Protect_Type_Flg |= BAT_OVERVOL_PRO_TYPE;
|
||
}
|
||
|
||
/*充电超时,关闭CHG*/
|
||
if(IRQ9_Flag & 0x08)
|
||
{
|
||
CHG_CTL &= ~0x01; //Disable Charger
|
||
}
|
||
|
||
if(IRQ9_Flag & 0x10) //Charge Done
|
||
{
|
||
Charger_Done = 1;
|
||
}
|
||
|
||
#if SLEEP_ENABLE
|
||
|
||
if(!Enter_Sleep_Cnt_Restart_Flag)
|
||
{
|
||
Enter_Sleep_Cnt_Restart_Flag = 1;
|
||
}
|
||
|
||
#endif
|
||
|
||
}
|
||
|