📄 rtc.c
字号:
DRV_WriteReg(RTC_POWERKEY2,RTC_POWERKEY2_KEY);
}
/*
* FUNCTION
* RTC_init
*
* DESCRIPTION
* RTC initial function
*
* CALLS
* This function is called to initialize the RTC module
*
* PARAMETERS
* RTC_TCCallback : callback function, when TC interrupt is coming
* RTC_ALCallback : callback function, when Alarm interrupt is coming
*
* RETURNS
* None
*
* GLOBALS AFFECTED
* external_global
*/
void RTC_TCintr(void)
{
#ifndef L4_NOT_PRESENT
ilm_struct *RTC_ilm;
RTC_Primitive *RTC_Prim;
#ifdef RTC_CAL_DEBUG
kal_uint32 frame_tick;
frame_tick=L1I_GetTimeStamp();
kal_prompt_trace(MOD_RTC_HISR, "1 min ticks=%d\r\n", frame_tick);
#endif /* RTC_CAL_DEBUG */
//RTC_ilm = allocate_ilm(MOD_RTC_HISR);
RTC_ilm = allocate_ilm(MOD_DRV_HISR);
RTC_Prim = (RTC_Primitive*)
construct_local_para(sizeof(RTC_Primitive), TD_CTRL);
RTC_Prim->rtc_ind = RTC_TC_IND;
//RTC_ilm->src_mod_id = MOD_RTC_HISR;
RTC_ilm->src_mod_id = MOD_DRV_HISR;
RTC_ilm->dest_mod_id = MOD_UEM;
RTC_ilm->sap_id = DRIVER_PS_SAP;
RTC_ilm->msg_id = MSG_ID_DRVUEM_RTC_PERIOD_IND;
RTC_ilm->local_para_ptr = (local_para_struct *)RTC_Prim;
RTC_ilm->peer_buff_ptr = NULL;
msg_send_ext_queue(RTC_ilm);
#endif /*L4_NOT_PRESENT*/
}
void RTC_ALintr(void)
{
kal_uint16 BBPU;
#ifndef L4_NOT_PRESENT
ilm_struct *RTC_ilm;
RTC_Primitive *RTC_Prim;
#endif /*L4_NOT_PRESENT*/
BBPU = DRV_Reg(RTC_BBPU);
BBPU &=~(RTC_BBPU_ALARM_SW);/*0227 TY adds*/
DRV_WriteReg(RTC_BBPU,(BBPU|RTC_BBPU_KEY));/*0227 TY adds*/
#ifndef L4_NOT_PRESENT
//RTC_ilm = allocate_ilm(MOD_RTC_HISR);
RTC_ilm = allocate_ilm(MOD_DRV_HISR);
RTC_Prim = (RTC_Primitive*)
construct_local_para(sizeof(RTC_Primitive), TD_CTRL);
RTC_Prim->rtc_ind = RTC_AL_IND;
//RTC_ilm->src_mod_id = MOD_RTC_HISR;
RTC_ilm->src_mod_id = MOD_DRV_HISR;
RTC_ilm->dest_mod_id = MOD_UEM;
RTC_ilm->sap_id = DRIVER_PS_SAP;
RTC_ilm->msg_id = MSG_ID_DRVUEM_RTC_PERIOD_IND;
RTC_ilm->local_para_ptr = (local_para_struct *)RTC_Prim;
RTC_ilm->peer_buff_ptr = NULL;
msg_send_ext_queue(RTC_ilm);
#endif /*L4_NOT_PRESENT*/
}
/*
* FUNCTION
* rtc_update_time
*
* DESCRIPTION
* update time according to time stamp
*
* CALLS
* This function is to update time according to time stamp
*
* PARAMETERS
* None
*
* RETURNS
* None
*
* GLOBALS AFFECTED
* external_global
*/
void rtc_update_time(void)
{
kal_uint32 current_frame_tick;
kal_uint32 sec=0;
kal_uint32 max_day=0;
#ifdef RTC_CAL_DEBUG
kal_uint32 frame_tick;
#endif /* RTC_CAL_DEBUG */
#if (!defined(__L1_STANDALONE__))
current_frame_tick=L1I_GetTimeStamp();
#endif
if(current_frame_tick>=next_frame_tick)
diff_frame_tick=(current_frame_tick-next_frame_tick)+tick_left;
else
diff_frame_tick=(0xffffffff+current_frame_tick-next_frame_tick+1)+tick_left;
sec=(diff_frame_tick/rtc_tick_update_interval);
sec=sec*rtc_sec_update_interval;
/* the 1/3 tick for each second. */
one_third_tick_count += sec;
tick_left=(diff_frame_tick%rtc_tick_update_interval) + one_third_tick_count/3;
/* record the left 1/3 tick count. */
one_third_tick_count %=3;
/*update time*/
/*sec*/
next_current_time.rtc_sec=next_current_time.rtc_sec+sec;
if(next_current_time.rtc_sec>59)
{
next_current_time.rtc_sec=next_current_time.rtc_sec-60;
/*min*/
next_current_time.rtc_min++;
if(next_current_time.rtc_min>59)
{
next_current_time.rtc_min=0;
/*hour*/
next_current_time.rtc_hour++;
if(next_current_time.rtc_hour>23)
{
next_current_time.rtc_hour=0;
/*day of week*/
next_current_time.rtc_wday++;
if(next_current_time.rtc_wday>7)
next_current_time.rtc_wday=1;
/*day of month*/
next_current_time.rtc_day++;
if(next_current_time.rtc_mon==1||next_current_time.rtc_mon==3||
next_current_time.rtc_mon==5||next_current_time.rtc_mon==7||
next_current_time.rtc_mon==8||next_current_time.rtc_mon==10||
next_current_time.rtc_mon==12)
max_day=31;
else if(next_current_time.rtc_mon==4||next_current_time.rtc_mon==6||
next_current_time.rtc_mon==9||next_current_time.rtc_mon==11)
max_day=30;
else
{
kal_uint32 remender=0;
remender=next_current_time.rtc_year%4;
if(remender==0)
max_day=29;
else
max_day=28;
}
if(next_current_time.rtc_day>max_day)
{
next_current_time.rtc_day=1;
/*month of year*/
next_current_time.rtc_mon++;
if(next_current_time.rtc_mon>12)
{
next_current_time.rtc_mon=1;
next_current_time.rtc_year++;
}
}
}
}
}
#ifdef RTC_CAL_DEBUG
frame_tick=L1I_GetTimeStamp();
kal_prompt_trace(MOD_MMI, "(update %d:%d:%d,):%d \r\n"
,next_current_time.rtc_hour,next_current_time.rtc_min,next_current_time.rtc_sec,frame_tick);
#endif /* RTC_CAL_DEBUG */
//if(sec!=0)
RTC_Check_Alarm(next_current_time);
RTC_Cali_Time(&next_current_time);
#ifdef RTC_CAL_DEBUG
kal_prompt_trace(MOD_RTC_HISR, "(update %d:%d:%d,):%d \r\n"
,next_current_time.rtc_hour,next_current_time.rtc_min,next_current_time.rtc_sec,current_frame_tick);
#endif /* RTC_CAL_DEBUG */
/*next frame tick and time*/
//#if (!defined(__L1_STANDALONE__))
//next_frame_tick=current_frame_tick;
//#endif
next_frame_tick=rtc_cali_tick_value+current_frame_tick;
rtc_get_next_time();
}
/*
* FUNCTION
* rtc_get_next_time
*
* DESCRIPTION
* calculate nexe time out time
*
* CALLS
* This function is to calculate nexe time out time
*
* PARAMETERS
* None
*
* RETURNS
* None
*
* GLOBALS AFFECTED
* external_global
*/
void rtc_get_next_time(void)
{
kal_uint32 max_day=0;
/*next time*/
next_current_time.rtc_hour=next_current_time.rtc_hour+rtc_cali_period;
if(next_current_time.rtc_hour>=24)/*hour*/
{
/*hour of day*/
next_current_time.rtc_hour=next_current_time.rtc_hour-24;
/*day of week*/
next_current_time.rtc_wday++;
if(next_current_time.rtc_wday>7)
next_current_time.rtc_wday=1;
/*day of month*/
next_current_time.rtc_day++;
if(next_current_time.rtc_mon==1||next_current_time.rtc_mon==3||
next_current_time.rtc_mon==5||next_current_time.rtc_mon==7||
next_current_time.rtc_mon==8||next_current_time.rtc_mon==10||
next_current_time.rtc_mon==12)
max_day=31;
else if(next_current_time.rtc_mon==4||next_current_time.rtc_mon==6||
next_current_time.rtc_mon==9||next_current_time.rtc_mon==11)
max_day=30;
else
{
kal_uint32 remender=0;
remender=next_current_time.rtc_year%4;
if(remender==0)
max_day=29;
else
max_day=28;
}
if(next_current_time.rtc_day>max_day)
{
next_current_time.rtc_day=1;
/*month of year*/
next_current_time.rtc_mon++;
if(next_current_time.rtc_mon>12)
{
next_current_time.rtc_mon=1;
next_current_time.rtc_year++;
}
}
}
}
/*
* FUNCTION
* rtc_cali_init
*
* DESCRIPTION
* Initialize rtc calibration
*
* CALLS
* This function is called at power on or user configures time.
*
* PARAMETERS
* None
*
* RETURNS
* None
*
* GLOBALS AFFECTED
* external_global
*/
void rtc_cali_init(void *timer_param)
{
kal_uint32 frame_tick;
kal_set_timer(rtc_timer_id, (kal_timer_func_ptr)rtc_timer, NULL, rtc_cali_tick_value+650,0);
#if (!defined(__L1_STANDALONE__))
frame_tick=L1I_GetTimeStamp();
#endif
RTC_GetTime(&next_current_time);
#ifdef RTC_CAL_DEBUG
kal_prompt_trace(MOD_RTC_HISR, "(%d:%d:%d): ",next_current_time.rtc_hour,next_current_time.rtc_min,next_current_time.rtc_sec);
kal_prompt_trace(MOD_RTC_HISR, "ini tick=%d\r\n",frame_tick);
#endif /* RTC_CAL_DEBUG */
tick_left=0;
one_third_tick_count = 0;
next_frame_tick=frame_tick+rtc_cali_tick_value;
rtc_get_next_time();
}
/*
* FUNCTION
* rtc_timer
*
* DESCRIPTION
* system timer callback function
*
* CALLS
* This function is called when calibration timer time out
*
* PARAMETERS
* timer_param
*
* RETURNS
* None
*
* GLOBALS AFFECTED
* external_global
*/
void rtc_timer(void *timer_param)
{
// kal_uint32 savedMask;
kal_uint8 rtc_sec;
#ifdef RTC_CAL_DEBUG
kal_uint32 frame_tick;
t_rtc rtctime;
#endif /* RTC_CAL_DEBUG */
rtc_sec = DRV_Reg(RTC_TC_SEC);
if (rtc_sec < 5 || rtc_sec > 55)
{
#ifdef RTC_CAL_DEBUG
kal_prompt_trace(MOD_RTC_HISR, "rtc in minute boundary :(%d): delay 12 secs. ", rtc_sec);
#endif /* RTC_CAL_DEBUG */
/* To skip the miniute boundary, delay about 12 secs to do the calibration. */
kal_set_timer(rtc_timer_id, (kal_timer_func_ptr)rtc_timer, NULL, 2600 ,0);
}
else
{
/* Set the timer priod as 1hr plus 3 sec to guarantee it will be longer than 1 hr. */
kal_set_timer(rtc_timer_id, (kal_timer_func_ptr)rtc_timer, NULL, rtc_cali_tick_value+650,0);
#ifdef RTC_CAL_DEBUG
frame_tick=L1I_GetTimeStamp();
RTC_GetTime(&rtctime);
kal_prompt_trace(MOD_RTC_HISR, "cal time out(%d:%d:%d): ",rtctime.rtc_hour,rtctime.rtc_min,rtctime.rtc_sec);
kal_prompt_trace(MOD_RTC_HISR, "cal time out tick=%d\r\n", frame_tick);
#endif /* RTC_CAL_DEBUG */
// savedMask = SaveAndSetIRQMask();
rtc_update_time();
// RestoreIRQMask(savedMask);
}
}
/*
* FUNCTION
* RTC_init
*
* DESCRIPTION
* RTC initialization function
*
* CALLS
* This function is initialize RTC software
*
* PARAMETERS
* RTC_TCCallback: time update callback function
* RTC_ALCallback: alarm callback function
*
* RETURNS
* None
*
* GLOBALS AFFECTED
* external_global
*/
void RTC_init(void (*RTC_TCCallback)(void),void (*RTC_ALCallback)(void)) //validate
{
/*status = DRV_Reg(RTC_IRQ_STATUS);*/
RTC_CALLBAC.rtc_tcfunc = RTC_TCCallback;
RTC_CALLBAC.rtc_alfunc = RTC_ALCallback;
/**(volatile kal_uint16 *)RTC_IRQ_EN = RTC_IRQ_EN_ONESHOT;*/
/*DRV_WriteReg(RTC_IRQ_EN,RTC_IRQ_EN_ALLOFF);*/
if (RTCConfig.hisr == NULL)
{
//RTCConfig.hisr = kal_create_hisr("RTC_HISR",2,512,RTC_HISR,NULL);
RTCConfig.hisr=(void*)0x1234;
DRV_Register_HISR(DRV_RTC_HISR_ID, RTC_HISR);
}
#if (!defined(__L1_STANDALONE__))
rtc_timer_id=kal_create_timer("RTCcal_Timer");
#if 1/*enable RTC calibration*/
kal_set_timer(rtc_timer_id, (kal_timer_func_ptr)rtc_cali_init, NULL, 2, 0);
//rtc_cali_init();
#endif
#endif
/*RTC IRQ configuration*/
#ifdef __MULTI_BOOT__
if (INT_BootMode() == MTK_NORMAL_MODE)
{
IRQ_Register_LISR(IRQ_RTC_CODE, RTC_LISR,"RTC handler");
IRQSensitivity(IRQ_RTC_CODE,LEVEL_SENSITIVE);
IRQUnmask(IRQ_RTC_CODE);
}
#else /*__MULTI_BOOT__*/
IRQ_Register_LISR(IRQ_RTC_CODE, RTC_LISR,"RTC handler");
IRQSensitivity(IRQ_RTC_CODE,LEVEL_SENSITIVE);
IRQUnmask(IRQ_RTC_CODE);
#endif /*__MULTI_BOOT__*/
/*config RTC driving current*/
#if defined(MT6228)|| defined(MT6229)|| defined(MT6230)
DRV_WriteReg(RTC_XOSCCAL,0x8);
#endif
}
/*
* FUNCTION
* RTC_is_config_valid
*
* DESCRIPTION
* Check if RTC time and Alarm are valid. If these
* settings are not correct, we treat it as first power on and
* reconfigure RTC.
*
* CALLS
* This function is to check if RTC time and Alarm are valid.
*
* PARAMETERS
* valid or not
*
* RETURNS
* None
*
* GLOBALS AFFECTED
* external_global
*/
kal_bool RTC_is_config_valid(void)
{
t_rtc time, alarm;
RTC_GetTime(&time);
RTC_GetALTime(&alarm);
if((time.rtc_sec>59) ||(alarm.rtc_sec>59)||
(time.rtc_min>59) ||(alarm.rtc_min>59)||
(time.rtc_hour>23)||(alarm.rtc_hour>23) ||
(time.rtc_day>31) ||(alarm.rtc_day>31)||
(time.rtc_wday>7) ||(alarm.rtc_wday>7)||
(time.rtc_mon>12) ||(alarm.rtc_mon>12) )
{
alarm.rtc_sec=0;
alarm.rtc_min=0;
alarm.rtc_hour=0;
alarm.rtc_day=0;
alarm.rtc_wday=0;
alarm.rtc_mon=0;
RTC_SetAlarm(&alarm);
return KAL_FALSE;
}
return KAL_TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -