561 lines
13 KiB
C
561 lines
13 KiB
C
|
|
#include "includes.h"
|
|||
|
|
|
|||
|
|
// <20><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>ģʽ
|
|||
|
|
uint8_t Wakeup_workMode = WORK_MODE_NORMAL;
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͱ<EFBFBD>־
|
|||
|
|
uint8_t bat_veryLow = 0;
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD>߱<EFBFBD>ʶ
|
|||
|
|
volatile uint8_t Wakeup_Sleeping = 0;
|
|||
|
|
|
|||
|
|
// ʵ<><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>INITIAL_YEAR<41><52><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>GPS<50><53>λ<EFBFBD><CEBB>GPRS<52><53><EFBFBD>ӻ<EFBFBD>վ<EFBFBD>Ժ<EFBFBD><D4BA><EFBFBD><EFBFBD>ã<EFBFBD>
|
|||
|
|
volatile uint32_t RTC_offsetSeconds = 0;
|
|||
|
|
|
|||
|
|
void Wakeup_ShowTime(char *preStr, S_RTC_TIME_DATA_T *pRTC)
|
|||
|
|
{
|
|||
|
|
S_RTC_TIME_DATA_T sRTC;
|
|||
|
|
|
|||
|
|
uint32_t totalSeconds = Calc_SecondsFromYear(INITIAL_YEAR, pRTC->u32Year, pRTC->u32Month, pRTC->u32Day,
|
|||
|
|
pRTC->u32Hour, pRTC->u32Minute, pRTC->u32Second);
|
|||
|
|
totalSeconds += RTC_offsetSeconds;
|
|||
|
|
Wakeup_CalcUTCTime(totalSeconds, &sRTC);
|
|||
|
|
printf("\n%s%04d-%02d-%02d %02d:%02d:%02d\n", preStr, sRTC.u32Year, sRTC.u32Month,
|
|||
|
|
sRTC.u32Day, sRTC.u32Hour, sRTC.u32Minute, sRTC.u32Second);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
uint8_t IsLeapYear(uint16_t year)
|
|||
|
|
{
|
|||
|
|
if(year % 4)
|
|||
|
|
return 0;
|
|||
|
|
if(year % 100 == 0 && year % 400)
|
|||
|
|
return 0;
|
|||
|
|
return 1;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD>꿪ʼ<EABFAA><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
uint32_t Calc_SecondsFromYear(uint16_t startYear, uint16_t year, uint8_t mon, uint8_t day, uint8_t hour, uint8_t min, uint8_t sec)
|
|||
|
|
{
|
|||
|
|
uint32_t totalSeconds = 0ul;
|
|||
|
|
uint16_t i;
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
for(i = startYear; i < year; i++)
|
|||
|
|
{
|
|||
|
|
totalSeconds += 31536000ul; // 365 * 86400
|
|||
|
|
if(IsLeapYear(i)) // <20><><EFBFBD><EFBFBD>+1<><31>
|
|||
|
|
totalSeconds += 86400ul;
|
|||
|
|
}
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD>
|
|||
|
|
for(i = 1; i < mon; i++)
|
|||
|
|
{
|
|||
|
|
totalSeconds += 2678400ul; // 31 * 86400
|
|||
|
|
if(i == 4 || i == 6 || i == 9 || i == 11)
|
|||
|
|
totalSeconds -= 86400ul;
|
|||
|
|
else if(i == 2)
|
|||
|
|
{
|
|||
|
|
totalSeconds -= 259200ul; // 3 * 86400
|
|||
|
|
if(IsLeapYear(year)) // <20><><EFBFBD><EFBFBD>+1<><31>
|
|||
|
|
totalSeconds += 86400ul;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
totalSeconds += (day - 1) * 86400ul;
|
|||
|
|
// <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
totalSeconds += hour * 3600ul;
|
|||
|
|
totalSeconds += min * 60ul;
|
|||
|
|
totalSeconds += sec;
|
|||
|
|
|
|||
|
|
return totalSeconds;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
uint16_t get_year_days(uint16_t year)
|
|||
|
|
{
|
|||
|
|
if(IsLeapYear(year))
|
|||
|
|
return 366;
|
|||
|
|
return 365;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
uint16_t get_month_days(uint16_t year, uint8_t month)
|
|||
|
|
{
|
|||
|
|
if(month == 4 || month == 6 || month == 9 || month == 11)
|
|||
|
|
return 30;
|
|||
|
|
if(month == 2)
|
|||
|
|
{
|
|||
|
|
if(IsLeapYear(year))
|
|||
|
|
return 29;
|
|||
|
|
return 28;
|
|||
|
|
}
|
|||
|
|
return 31;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD>INITIAL_YEAR<41><52>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
void Wakeup_CalcUTCTime(uint32_t totalSeconds, S_RTC_TIME_DATA_T *pRTC)
|
|||
|
|
{
|
|||
|
|
uint32_t totalDays, days;
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>´λ<C2B4><CEBB>ѵ<EFBFBD><D1B5>ꡢ<EFBFBD>¡<EFBFBD><C2A1><EFBFBD>
|
|||
|
|
pRTC->u32Year = INITIAL_YEAR;
|
|||
|
|
pRTC->u32Month = 1;
|
|||
|
|
pRTC->u32Day = 1;
|
|||
|
|
totalDays = totalSeconds / 86400ul;
|
|||
|
|
while(totalDays >= 1)
|
|||
|
|
{
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
days = get_year_days(pRTC->u32Year);
|
|||
|
|
if(totalDays >= days)
|
|||
|
|
{
|
|||
|
|
pRTC->u32Year++;
|
|||
|
|
totalDays -= days;
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD>
|
|||
|
|
days = get_month_days(pRTC->u32Year, pRTC->u32Month);
|
|||
|
|
if(totalDays >= days)
|
|||
|
|
{
|
|||
|
|
pRTC->u32Month++;
|
|||
|
|
totalDays -= days;
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
pRTC->u32Day += totalDays;
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>´λ<C2B4><CEBB>ѵ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>֡<EFBFBD><D6A1><EFBFBD>
|
|||
|
|
totalSeconds %= 86400ul;
|
|||
|
|
pRTC->u32Hour = totalSeconds / 3600ul;
|
|||
|
|
totalSeconds %= 3600;
|
|||
|
|
pRTC->u32Minute = totalSeconds / 60ul;
|
|||
|
|
totalSeconds %= 60ul;
|
|||
|
|
pRTC->u32Second = totalSeconds;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
uint32_t Wakeup_CalcPeriod(uint8_t charging)
|
|||
|
|
{
|
|||
|
|
uint32_t period = 300;
|
|||
|
|
|
|||
|
|
if(dcBuff.configData.intervalTrans > 0 && period > dcBuff.configData.intervalTrans)
|
|||
|
|
period = dcBuff.configData.intervalTrans;
|
|||
|
|
if(dcBuff.configData.intervalSample > 0 && period > dcBuff.configData.intervalSample)
|
|||
|
|
period = dcBuff.configData.intervalSample;
|
|||
|
|
|
|||
|
|
if(charging && period > 60)
|
|||
|
|
period = 60;
|
|||
|
|
|
|||
|
|
return period;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>´λ<C2B4><CEBB>ѵ<EFBFBD>ʱ<EFBFBD><CAB1>
|
|||
|
|
void Wakeup_CalcWakeupTime(S_RTC_TIME_DATA_T *pRTC, uint8_t charging)
|
|||
|
|
{
|
|||
|
|
uint32_t period = Wakeup_CalcPeriod(charging);
|
|||
|
|
uint32_t sec;
|
|||
|
|
|
|||
|
|
// <20>ȼ<EFBFBD><C8BC><EFBFBD><EFBFBD>ӳ<EFBFBD>ʼʱ<CABC><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
uint32_t totalSeconds = Calc_SecondsFromYear(INITIAL_YEAR, pRTC->u32Year, pRTC->u32Month, pRTC->u32Day,
|
|||
|
|
pRTC->u32Hour, pRTC->u32Minute, pRTC->u32Second);
|
|||
|
|
|
|||
|
|
// ˫·<CBAB>л<EFBFBD><D0BB><EFBFBD>ʾ
|
|||
|
|
if(MeterNeedRoll() && period > 20)
|
|||
|
|
period = 20;
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>´λ<C2B4><CEBB><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>뵽<EFBFBD><EBB5BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵı<DAB5><C4B1><EFBFBD>
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>ÿ<EFBFBD>θ<EFBFBD>λ<EFBFBD>Ժ<EFBFBD><D4BA><EFBFBD>Ȼʱ<C8BB><CAB1><EFBFBD>Ǽ<EFBFBD><C7BC><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
sec = totalSeconds + period;
|
|||
|
|
sec -= sec % period;
|
|||
|
|
// <20><>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>̫<EFBFBD><CCAB>
|
|||
|
|
if(sec <= totalSeconds + 2)
|
|||
|
|
totalSeconds = sec + period;
|
|||
|
|
else
|
|||
|
|
totalSeconds = sec;
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>´λ<C2B4><CEBB>ѵ<EFBFBD><D1B5>ꡢ<EFBFBD>¡<EFBFBD><C2A1><EFBFBD>
|
|||
|
|
Wakeup_CalcUTCTime(totalSeconds, pRTC);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20>趨<EFBFBD>´<EFBFBD><C2B4><EFBFBD><EFBFBD>ӻ<EFBFBD><D3BB><EFBFBD>ʱ<EFBFBD><CAB1>
|
|||
|
|
void Wakeup_SetAlarm(uint8_t charging)
|
|||
|
|
{
|
|||
|
|
S_RTC_TIME_DATA_T sRTC;
|
|||
|
|
|
|||
|
|
// <20>趨<EFBFBD>´<EFBFBD><C2B4><EFBFBD><EFBFBD>ӻ<EFBFBD><D3BB><EFBFBD>ʱ<EFBFBD><CAB1>
|
|||
|
|
RTC_GetDateAndTime(&sRTC);
|
|||
|
|
// <20><>ʾ<EFBFBD><CABE>ǰʱ<C7B0><CAB1>
|
|||
|
|
Wakeup_ShowTime("RTC time is: ", &sRTC);
|
|||
|
|
Wakeup_CalcWakeupTime(&sRTC, charging);
|
|||
|
|
RTC_SetAlarmDateAndTime(&sRTC);
|
|||
|
|
// <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
|
|||
|
|
Wakeup_ShowTime("***********************************************\nWakeup time is: ", &sRTC);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void RTC_IRQHandler(void)
|
|||
|
|
{
|
|||
|
|
S_RTC_TIME_DATA_T sRTC;
|
|||
|
|
static uint32_t gpsSeconds = 0;
|
|||
|
|
static uint32_t rollTime = 0;
|
|||
|
|
static uint32_t semTime = 0;
|
|||
|
|
uint32_t totalSeconds;
|
|||
|
|
uint32_t period = Wakeup_CalcPeriod(dcBuff.sampleData.charging);
|
|||
|
|
|
|||
|
|
if(CLK->WK_INTSTS & CLK_WK_INTSTS_PD_WK_IS_Msk)
|
|||
|
|
{
|
|||
|
|
// <20><><EFBFBD>´<C2B4><F2BFAABF>Ź<EFBFBD><C5B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
WDT->CTL |= WDT_CTL_WTE_Msk;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD>ڻ<EFBFBD><DABB><EFBFBD>MCU
|
|||
|
|
if(RTC_GET_ALARM_INT_FLAG() == 1)
|
|||
|
|
{
|
|||
|
|
/* Clear RTC alarm interrupt flag */
|
|||
|
|
RTC_CLEAR_ALARM_INT_FLAG();
|
|||
|
|
|
|||
|
|
// <20><>ȡ<EFBFBD><C8A1>ǰʱ<C7B0><CAB1>
|
|||
|
|
RTC_GetDateAndTime(&sRTC);
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD>gps<70><73>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
|
|||
|
|
totalSeconds = Calc_SecondsFromYear(INITIAL_YEAR, sRTC.u32Year, sRTC.u32Month, sRTC.u32Day,
|
|||
|
|
sRTC.u32Hour, sRTC.u32Minute, sRTC.u32Second);
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>6<EFBFBD><36><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD><DAA3><EFBFBD>С2Сʱ<D0A1><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD>죩δ<ECA3A9><CEB4><EFBFBD>ͳɹ<CDB3><C9B9><EFBFBD><EFBFBD><EFBFBD>λ
|
|||
|
|
if((totalSeconds >= DTU_succTime + 7200 && totalSeconds >= DTU_succTime + dcBuff.configData.intervalTrans * 6) || totalSeconds >= DTU_succTime + 86400)
|
|||
|
|
NVIC_SystemReset();
|
|||
|
|
|
|||
|
|
// ÿ<><C3BF>24Сʱ<D0A1><CAB1>λһ<CEBB><D2BB>(3<><33><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD>
|
|||
|
|
if(totalSeconds + 3 >= gpsSeconds + 86400ul)
|
|||
|
|
{
|
|||
|
|
// <20><><EFBFBD>¶<EFBFBD>λ
|
|||
|
|
GPS_Located = 0;
|
|||
|
|
GPS_Locate = 1;
|
|||
|
|
// <20><><EFBFBD>¼<EFBFBD>ʱ
|
|||
|
|
gpsSeconds = totalSeconds;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if(CLK->WK_INTSTS & CLK_WK_INTSTS_PD_WK_IS_Msk)
|
|||
|
|
{
|
|||
|
|
delay_ms(10);
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>жϻ<D0B6><CFBB>ѱ<EFBFBD>־
|
|||
|
|
// CLK->WK_INTSTS |= CLK_WK_INTSTS_PD_WK_IS_Msk;
|
|||
|
|
|
|||
|
|
Vcc_Enable();
|
|||
|
|
delay_ms(40);
|
|||
|
|
Wakeup_Sleeping = 0;
|
|||
|
|
|
|||
|
|
// printf("\nTickCount: %d\n", GetTickCount());
|
|||
|
|
printf("\n\nWake up by RTC.\n\n");
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if(totalSeconds >= semTime + period)
|
|||
|
|
{
|
|||
|
|
semTime = totalSeconds;
|
|||
|
|
if(CLK->WK_INTSTS & CLK_WK_INTSTS_PD_WK_IS_Msk)
|
|||
|
|
DTU_semSync = 1;
|
|||
|
|
else
|
|||
|
|
DTU_semGPRS = 1;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20>л<EFBFBD>˫ѹ<CBAB><D1B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>
|
|||
|
|
if(MeterInMeterPage() && MeterNeedRoll() && totalSeconds >= rollTime + 20)
|
|||
|
|
{
|
|||
|
|
rollTime = totalSeconds;
|
|||
|
|
Form_rollIdx = 1 - Form_rollIdx;
|
|||
|
|
Form_Refresh();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20>趨<EFBFBD>´<EFBFBD><C2B4><EFBFBD><EFBFBD>ӻ<EFBFBD><D3BB><EFBFBD>ʱ<EFBFBD><CAB1>
|
|||
|
|
Wakeup_SetAlarm(dcBuff.sampleData.charging);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void GPABC_IRQHandler(void)
|
|||
|
|
{
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>Ͱ<EFBFBD><CDB0><EFBFBD><EFBFBD>ж<EFBFBD>
|
|||
|
|
if(GPIO_GET_INT_FLAG(PB, BIT9))
|
|||
|
|
{
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
Key_Handler();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>绽<EFBFBD><E7BBBD><EFBFBD>ж<EFBFBD>
|
|||
|
|
if(!GPIO_GET_INT_FLAG(PC, BIT6))
|
|||
|
|
return;
|
|||
|
|
|
|||
|
|
if(CLK->WK_INTSTS & CLK_WK_INTSTS_PD_WK_IS_Msk)
|
|||
|
|
{
|
|||
|
|
// <20><><EFBFBD>´<C2B4><F2BFAABF>Ź<EFBFBD><C5B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
WDT->CTL |= WDT_CTL_WTE_Msk;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>жϱ<D0B6>־
|
|||
|
|
GPIO_CLR_INT_FLAG(PC, BIT6);
|
|||
|
|
|
|||
|
|
if(CLK->WK_INTSTS & CLK_WK_INTSTS_PD_WK_IS_Msk)
|
|||
|
|
{
|
|||
|
|
delay_ms(10);
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>жϻ<D0B6><CFBB>ѱ<EFBFBD>־
|
|||
|
|
// CLK->WK_INTSTS |= CLK_WK_INTSTS_PD_WK_IS_Msk;
|
|||
|
|
|
|||
|
|
Vcc_Enable();
|
|||
|
|
delay_ms(40);
|
|||
|
|
Wakeup_Sleeping = 0;
|
|||
|
|
|
|||
|
|
// <20>ж<EFBFBD><D0B6>Ƿ<EFBFBD>Ϊ<EFBFBD>͵<EFBFBD>ƽ<EFBFBD><C6BD><EFBFBD>ų<EFBFBD><C5B3><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
if(!VCC_POWER_STATUS())
|
|||
|
|
{
|
|||
|
|
printf("\nWakeup by DISTURB ...\n");
|
|||
|
|
|
|||
|
|
// <20>ɸ<EFBFBD><C9B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>İ<EFBFBD><C4B0><EFBFBD><EFBFBD>жϣ<D0B6><CFA3><EFBFBD><EFBFBD>ź<EFBFBD><C5BA><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
printf("\n\nWake up by POWER.\n\n");
|
|||
|
|
|
|||
|
|
// <20>ɼ<EFBFBD><C9BC><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
if(!Sample_Busy())
|
|||
|
|
NVIC_SetPendingIRQ(TMR1_IRQn);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void Wakeup_Init()
|
|||
|
|
{
|
|||
|
|
CLK->PWRCTL |= CLK_PWRCTL_PD_WK_IE_Msk;
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD>ⲿ32768Hz<48><7A><EFBFBD><EFBFBD>ʱ<EFBFBD>ӣ<EFBFBD><D3A3>ڵ<EFBFBD><DAB5><EFBFBD>ģʽ<C4A3><CABD>Ȼ<EFBFBD><C8BB>Ч<EFBFBD><D0A7>
|
|||
|
|
CLK_EnableModuleClock(RTC_MODULE);
|
|||
|
|
|
|||
|
|
/* Set GPC as VCC_POWER_STATUS */
|
|||
|
|
SYS->PC_L_MFP &= ~SYS_PC_L_MFP_PC6_MFP_Msk;
|
|||
|
|
SYS->PC_L_MFP |= SYS_PC_L_MFP_PC6_MFP_GPC6;
|
|||
|
|
GPIO_SetMode(PC, 1 << 6, GPIO_PMD_INPUT);
|
|||
|
|
|
|||
|
|
/* Interrupt Type: Rising Edge */
|
|||
|
|
GPIO_EnableInt(PC, 6, GPIO_INT_RISING);
|
|||
|
|
|
|||
|
|
/* Enable interrupt de-bounce function and select de-bounce sampling cycle time is 512 clocks of LIRC clock */
|
|||
|
|
GPIO_SET_DEBOUNCE_TIME(GPIO_DBCLKSRC_IRC10K, GPIO_DBCLKSEL_256);
|
|||
|
|
// GPIO_ENABLE_DEBOUNCE(PC, BIT6);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void Wakeup_InitializeTime(S_RTC_TIME_DATA_T *pRTC)
|
|||
|
|
{
|
|||
|
|
pRTC->u32Year = INITIAL_YEAR;
|
|||
|
|
pRTC->u32Month = 1;
|
|||
|
|
pRTC->u32Day = 1;
|
|||
|
|
pRTC->u32DayOfWeek = RTC_FRIDAY;
|
|||
|
|
pRTC->u32Hour = 0;
|
|||
|
|
pRTC->u32Minute = 0;
|
|||
|
|
pRTC->u32Second = 0;
|
|||
|
|
pRTC->u32TimeScale = RTC_CLOCK_24;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void Wakeup_Open()
|
|||
|
|
{
|
|||
|
|
S_RTC_TIME_DATA_T sRTC;
|
|||
|
|
|
|||
|
|
NVIC_SetPriority (RTC_IRQn, 3); /* set Priority for RTC Interrupt */
|
|||
|
|
NVIC_EnableIRQ(RTC_IRQn);
|
|||
|
|
|
|||
|
|
RTC->INIR = RTC_INIT_KEY;
|
|||
|
|
while(RTC->INIR != RTC_INIR_ACTIVE_Msk);
|
|||
|
|
|
|||
|
|
/* Set RTC date and time */
|
|||
|
|
Wakeup_InitializeTime(&sRTC);
|
|||
|
|
RTC_SetDateAndTime(&sRTC);
|
|||
|
|
|
|||
|
|
/* Waiting for RTC settings stable */
|
|||
|
|
while((RTC->AER & RTC_AER_ENF_Msk) == RTC_AER_ENF_Msk);
|
|||
|
|
|
|||
|
|
printf("\n*******************************\n");
|
|||
|
|
printf("system reseted ...\n");
|
|||
|
|
|
|||
|
|
// <20>趨<EFBFBD>´<EFBFBD><C2B4><EFBFBD><EFBFBD>ӻ<EFBFBD><D3BB><EFBFBD>ʱ<EFBFBD><CAB1>
|
|||
|
|
Wakeup_SetAlarm(dcBuff.sampleData.charging);
|
|||
|
|
|
|||
|
|
/* Enable RTC alarm interrupt and wake-up function will be enabled also */
|
|||
|
|
RTC_EnableInt(RTC_RIER_AIER_Msk);
|
|||
|
|
|
|||
|
|
/* Enable GPIO IRQ */
|
|||
|
|
NVIC_SetPriority (GPABC_IRQn, 3);
|
|||
|
|
NVIC_EnableIRQ(GPABC_IRQn);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
|
|||
|
|
void Wakeup_Powerdown()
|
|||
|
|
{
|
|||
|
|
uint32_t curr_time;
|
|||
|
|
S_RTC_TIME_DATA_T sRTC;
|
|||
|
|
static uint8_t last_PowerStatus = 0;
|
|||
|
|
uint8_t powerStatus;
|
|||
|
|
|
|||
|
|
#if 0
|
|||
|
|
// ģ<><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷŵ磺ÿ<E7A3BA><C3BF>10<31><30><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD>һ<EFBFBD><D2BB>
|
|||
|
|
if(dcBuff.configDisplay.op_SEND_GPS_DATA)
|
|||
|
|
{
|
|||
|
|
RTC_GetDateAndTime(&sRTC);
|
|||
|
|
curr_time = Calc_SecondsFromYear(INITIAL_YEAR, sRTC.u32Year, sRTC.u32Month, sRTC.u32Day,
|
|||
|
|
sRTC.u32Hour, sRTC.u32Minute, sRTC.u32Second);
|
|||
|
|
powerStatus = (curr_time / 600 % 2 == 0);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
#endif
|
|||
|
|
{
|
|||
|
|
// <20><>¼<EFBFBD>ص<EFBFBD><D8B5><EFBFBD>ʱ<EFBFBD><CAB1>
|
|||
|
|
powerStatus = VCC_POWER_STATUS();
|
|||
|
|
}
|
|||
|
|
if(last_PowerStatus != powerStatus && !powerStatus)
|
|||
|
|
{
|
|||
|
|
RTC_GetDateAndTime(&sRTC);
|
|||
|
|
Poweroff_time = Calc_SecondsFromYear(INITIAL_YEAR, sRTC.u32Year, sRTC.u32Month, sRTC.u32Day,
|
|||
|
|
sRTC.u32Hour, sRTC.u32Minute, sRTC.u32Second);
|
|||
|
|
}
|
|||
|
|
last_PowerStatus = powerStatus;
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>Dz۳<C7B2><DBB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ⲿ<EFBFBD><E2B2BF><EFBFBD>磬<EFBFBD><E7A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
if(powerStatus && dcBuff.configDisplay.op_SEND_GPS_DATA)
|
|||
|
|
return;
|
|||
|
|
// <20><>λδ<CEBB>ɹ<EFBFBD><C9B9><EFBFBD>δ<EFBFBD><CEB4>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
if(!dcBuff.configDisplay.op_SEND_GPS_DATA && Wakeup_GetWorkMode() == WORK_MODE_NORMAL && GPS_Waiting)
|
|||
|
|
return;
|
|||
|
|
// <20>ɼ<EFBFBD><C9BC><EFBFBD><EFBFBD><EFBFBD>δ<EFBFBD><CEB4><EFBFBD>ɣ<EFBFBD><C9A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
if(Sample_Busy())
|
|||
|
|
return;
|
|||
|
|
|
|||
|
|
// <20>۳<EFBFBD><DBB3><EFBFBD><EFBFBD>նϵ<D5B6><CFB5><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>ӳ<EFBFBD>20<32><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
if(dcBuff.configDisplay.op_SEND_GPS_DATA)
|
|||
|
|
{
|
|||
|
|
// <20><><EFBFBD><EFBFBD>ģʽ<C4A3><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>£<EFBFBD><C2A3>ӳ<EFBFBD><D3B3><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
if(!powerStatus && Wakeup_GetWorkMode() == WORK_MODE_NORMAL)
|
|||
|
|
{
|
|||
|
|
// <20><>ȡ<EFBFBD><C8A1>ǰʱ<C7B0><CAB1>
|
|||
|
|
RTC_GetDateAndTime(&sRTC);
|
|||
|
|
curr_time = Calc_SecondsFromYear(INITIAL_YEAR, sRTC.u32Year, sRTC.u32Month, sRTC.u32Day,
|
|||
|
|
sRTC.u32Hour, sRTC.u32Minute, sRTC.u32Second);
|
|||
|
|
if(Poweroff_time > 0 && curr_time >= Poweroff_time && curr_time < Poweroff_time + 1200)
|
|||
|
|
// if(Poweroff_time > 0 && curr_time >= Poweroff_time && curr_time < Poweroff_time + 60)
|
|||
|
|
{
|
|||
|
|
if((curr_time - Poweroff_time) % 10 == 0)
|
|||
|
|
printf("\ndelay sleep time: %d\n", curr_time - Poweroff_time);
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20>ر<EFBFBD>DTU
|
|||
|
|
if(Wakeup_GetWorkMode() != WORK_MODE_NORMAL || LCD_Disabled)
|
|||
|
|
DTU_PowerOff();
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
if(Wakeup_GetWorkMode() == WORK_MODE_NORMAL && !LCD_Disabled)
|
|||
|
|
return;
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD>ⲿ<EFBFBD><E2B2BF><EFBFBD>磬<EFBFBD><E7A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
if(powerStatus)
|
|||
|
|
return;
|
|||
|
|
|
|||
|
|
// <20><>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
NVIC_DisableIRQ(GPDEF_IRQn);
|
|||
|
|
|
|||
|
|
// <20>ǹ<EFBFBD><C7B9><EFBFBD><EFBFBD>棬<EFBFBD><E6A3AC>ֹ<EFBFBD><D6B9><EFBFBD>ٶ<EFBFBD><D9B6>ж<EFBFBD>
|
|||
|
|
if(!dcBuff.configDisplay.op_BOX_VER)
|
|||
|
|
GPIO_DisableInt(PF, 5);
|
|||
|
|
|
|||
|
|
printf("\nEnter to Power-Down ......\n");
|
|||
|
|
// printf("\nTickCount: %d\n", GetTickCount());
|
|||
|
|
/* To check if all the debug messages are finished */
|
|||
|
|
while(IsDebugFifoEmpty() == 0);
|
|||
|
|
|
|||
|
|
// <20><>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>⣨<EFBFBD><E2A3A8>ι<EFBFBD><CEB9><EFBFBD><EFBFBD>
|
|||
|
|
WDT_RESET_COUNTER();
|
|||
|
|
Wakeup_Sleeping = 1;
|
|||
|
|
|
|||
|
|
VCC_SFLASH_FRAM_OFF();
|
|||
|
|
Vcc_Disable();
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѣ<EFBFBD><D1A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>жϱ<D0B6>־
|
|||
|
|
PE->ISRC = PE->ISRC;
|
|||
|
|
NVIC_EnableIRQ(GPDEF_IRQn);
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD>Ź<EFBFBD>ʱ<EFBFBD><CAB1>ΪLIRC<52><43><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD><D2B2>Ч<EFBFBD><D0A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֹͣ<CDA3><D6B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
WDT->CTL &= ~WDT_CTL_WTE_Msk;
|
|||
|
|
do
|
|||
|
|
{
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
|
|||
|
|
CLK_PowerDown();
|
|||
|
|
}while(!(CLK->WK_INTSTS & CLK_WK_INTSTS_PD_WK_IS_Msk));
|
|||
|
|
CLK->WK_INTSTS |= CLK_WK_INTSTS_PD_WK_IS_Msk;
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>۳<EFBFBD><DBB3>棬<EFBFBD><E6A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٶ<EFBFBD><D9B6>ж<EFBFBD>
|
|||
|
|
if(dcBuff.configDisplay.op_BOX_VER || dcBuff.configDisplay.op_SEND_GPS_DATA)
|
|||
|
|
{
|
|||
|
|
// <20><>ȡ<EFBFBD>˶<EFBFBD>״̬
|
|||
|
|
Accelero_ReadStatus(PF5);
|
|||
|
|
GPIO_EnableInt(PF, 5, GPIO_INT_BOTH_EDGE);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
delay_ms(20);
|
|||
|
|
SFlash_UnlockBPR();
|
|||
|
|
}
|
|||
|
|
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>ģʽ
|
|||
|
|
uint8_t Wakeup_GetWorkMode()
|
|||
|
|
{
|
|||
|
|
return Wakeup_workMode;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD>ù<EFBFBD><C3B9><EFBFBD>ģʽ
|
|||
|
|
void Wakeup_SetWorkMode()
|
|||
|
|
{
|
|||
|
|
// Ĭ<><C4AC>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
|
|||
|
|
uint8_t mode = WORK_MODE_NORMAL;
|
|||
|
|
|
|||
|
|
// <20>ŵ<EFBFBD>״̬
|
|||
|
|
if(!VCC_POWER_STATUS())
|
|||
|
|
{
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD><D8B1><EFBFBD>ģʽ
|
|||
|
|
if(bat_veryLow)
|
|||
|
|
mode = WORK_MODE_PROTECT;
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾģʽ
|
|||
|
|
else if(dcBuff.dtuData.batLow)
|
|||
|
|
mode = WORK_MODE_CHARGE;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
printf("\nchange work mode from %d to %d\n", Wakeup_workMode, mode);
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD>ر<EFBFBD><D8B1><EFBFBD>ģʽ
|
|||
|
|
if(mode == WORK_MODE_PROTECT)
|
|||
|
|
{
|
|||
|
|
// <20>ر<EFBFBD>DTU<54>ʹ<EFBFBD><CDB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
DTU_PowerOff();
|
|||
|
|
// VCC_SENSOR_24V_OFF();
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD>ģʽ
|
|||
|
|
Wakeup_workMode = mode;
|
|||
|
|
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD><D8B1><EFBFBD>ģʽ
|
|||
|
|
if(Wakeup_workMode == WORK_MODE_PROTECT)
|
|||
|
|
{
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾģʽ
|
|||
|
|
if(mode == WORK_MODE_CHARGE)
|
|||
|
|
{
|
|||
|
|
// <20>ر<EFBFBD>DTU<54>ʹ<EFBFBD><CDB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
DTU_PowerOff();
|
|||
|
|
// VCC_SENSOR_24V_OFF();
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
// VCC_SENSOR_24V_ON();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD>ģʽ
|
|||
|
|
Wakeup_workMode = mode;
|
|||
|
|
}
|