309 lines
6.6 KiB
C
309 lines
6.6 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
|
||
\******************************************************************************/
|
||
|
||
/******************************************************************************\
|
||
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 后续根据系统功耗情况调整。
|
||
|
||
/* test,开启IO LDO bias和增加100uA负载。*/
|
||
SFRADDR = 0x89;
|
||
SFRDATA |= 0x80;
|
||
|
||
/*test*/
|
||
SFRADDR = 0x01;
|
||
SFRDATA &= ~0x03;
|
||
|
||
#if UART0_ENABLE
|
||
UART0_Init();
|
||
#endif
|
||
|
||
#if VOX_ENABLE
|
||
//Vox_init(); //上电后,尽快初始化VOX为自动识别模式,为后续识别VOX是否有LOADON做好准备。
|
||
VOX_CTL0 = 0xC3; //VOX 负载接入、存在识别功能使能,负载自动识别。打开Vox vds钳压环使能(主要用于VOX 5V输出)。
|
||
/*
|
||
SFRADDR = VOX_CON0; //设置VOX输出500mA,负载识别电流2.5uA。
|
||
SFRDATA &= 0xE0;
|
||
SFRDATA |= (VOX_IOUT_500MA << 3) | VOX_ADT_Cur_2_5UA;
|
||
*/
|
||
#endif
|
||
|
||
Sys_Tim0_Init();
|
||
|
||
#if TIMER1_ENABLE
|
||
Sys_Tim1_Init();
|
||
#endif
|
||
|
||
// SFRADDR = P1_OE;
|
||
// SFRDATA = 0x01;
|
||
|
||
#if 0
|
||
|
||
SFRADDR = P0_OE;
|
||
SFRDATA = 0x14;
|
||
|
||
SFRADDR = P0_DRV;
|
||
SFRDATA = 0x33;
|
||
|
||
P0 &= ~0x14;
|
||
|
||
#endif
|
||
// BES_Puls_Start = 1;
|
||
|
||
/*COM_CTRL Set*/
|
||
SFRADDR = P0_OE;
|
||
SFRDATA = 0x80;
|
||
|
||
P07 = 0x01; //P07设置高:用于VOUT;设置低:用于UART。
|
||
|
||
#ifdef LED_DISPLAY
|
||
LED_Init(); // LED pin 配置为GPIO输出
|
||
#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
|
||
|
||
#ifdef ADC_NTC_ENABLE
|
||
|
||
SFRADDR = MFP_CTL0; //Set P00 Pinmux As AD0 Function
|
||
SFRDATA |= 0x02;
|
||
|
||
SFRADDR = ADCCS_CTL0; //AD0~AD4 GPIO constant 20uA current source enable.
|
||
SFRDATA = 0x01;
|
||
|
||
#endif
|
||
|
||
|
||
/* Interrupt Enable 根据应用选择必要的中断 */
|
||
SFRADDR = IRQ_EN3; //INT2 --- VOX EDGE LOADON
|
||
SFRDATA = 0x3C;
|
||
|
||
SFRADDR = IRQ_EN10; //INT4 --- Hall interrupt enable.
|
||
SFRDATA = 0x0C;
|
||
|
||
/* 开外部中断使能 */
|
||
EX0 = 1;
|
||
EX2 = 1;
|
||
EX4 = 1;
|
||
|
||
EAL = 1; //开总中断。
|
||
|
||
IRQ_FLAG9 = 0x1C; //清 Charge complete Int、Tricle/Fast Charge Time expire Int
|
||
IRQ_FLAG10 = 0xC0; //清 VBUS Plugin\Plugout Int
|
||
IRQ_FLAG0 = 0x0C; //Vox短路保护
|
||
|
||
#if CHARGER_ENABLE
|
||
|
||
SFRADDR = PMU_CTL7; //合封芯片关闭VBUS OV检测使能
|
||
SFRDATA &= ~0x20;
|
||
|
||
CHG_CTL |= 0x10; //开启充电超时使能
|
||
|
||
SFRADDR = REG_CHG0;
|
||
SFRDATA |= 0x04; //关VDPM环
|
||
|
||
#endif
|
||
|
||
|
||
#if SLEEP_ENABLE
|
||
Decnt_SleepDelay = ENTER_STANDBYMODE_CNT;
|
||
#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
|
||
{
|
||
IRQ_FLAG3 = 0x3C;
|
||
|
||
#if SLEEP_ENABLE
|
||
if( !Enter_Sleep_Cnt_Restart_Flag )
|
||
{
|
||
Enter_Sleep_Cnt_Restart_Flag = 1;
|
||
}
|
||
#endif
|
||
}
|
||
#if 1
|
||
|
||
/*
|
||
*******************************************************************************
|
||
* void EX4_isr(void) interrupt Interrupt_Vector_IE4
|
||
*
|
||
* Description : System External Intterupt 4
|
||
*
|
||
* Arguments : NONE
|
||
|
||
* Returns : NONE
|
||
|
||
* Notes : NONE
|
||
*
|
||
*******************************************************************************
|
||
*/
|
||
|
||
void EX4_isr(void) interrupt Interrupt_Vector_IE4
|
||
{
|
||
#if 0
|
||
uint8_t IRQ10_Flag = 0;
|
||
uint8_t IRQ7_Flag = 0;
|
||
|
||
IRQ10_Flag = IRQ_FLAG10;
|
||
IRQ_FLAG10 = IRQ10_Flag;
|
||
|
||
IRQ7_Flag = IRQ_FLAG7;
|
||
IRQ_FLAG7 = IRQ7_Flag;
|
||
#endif
|
||
|
||
#if KEY_HALL_ENABLE
|
||
#if 0
|
||
if( IRQ_FLAG7 & 0x10 ) //Key short press for 16ms-1s interrupt flag
|
||
{
|
||
Key_Press_short_irq = 1;
|
||
}
|
||
|
||
if( IRQ_FLAG7 & 0x20 ) //Key long press for 2s interrupt flag and Key release interrupt flag
|
||
{
|
||
Key_Press_l_irq = 1;
|
||
}
|
||
|
||
if( IRQ_FLAG7 & 0x40 ) //Key super long press for 8s interrupt flag
|
||
{
|
||
Key_Press_ll_irq = 1;
|
||
Key_Press_l_irq = 0;
|
||
}
|
||
|
||
if( IRQ_FLAG7 & 0x08 )
|
||
{
|
||
if( Key_Press_l_irq )
|
||
{
|
||
Key_Press_l_irq = 0;
|
||
Key_l_Flag = 1;
|
||
}
|
||
}
|
||
#endif
|
||
#if 0
|
||
if( IRQ_FLAG10 & 0x08 )
|
||
{
|
||
Hall_Negative_Flg = 1;
|
||
}
|
||
else
|
||
if( IRQ_FLAG10 & 0x04 )
|
||
{
|
||
Hall_Positive_Flg = 1;
|
||
}
|
||
|
||
// IRQ_FLAG7 = 0xF8;
|
||
IRQ_FLAG10 = 0x0C;
|
||
#endif
|
||
#endif
|
||
IRQ_FLAG10 = 0x0C;
|
||
#if SLEEP_ENABLE
|
||
|
||
if( !Enter_Sleep_Cnt_Restart_Flag )
|
||
{
|
||
Enter_Sleep_Cnt_Restart_Flag = 1;
|
||
}
|
||
|
||
#endif
|
||
}
|
||
#endif
|