📄 rtc_functions.c
字号:
date_time->month = conv_bin(month);
date_time->year = conv_bin(year);
date_time->wday = conv_bin(wday);
return RVF_OK;
}
/*******************************************************************************
*
* RTC_SetDateTime
*
******************************************************************************/
T_RVF_RET RTC_SetDateTime(T_RTC_DATE_TIME date_time)
{
UINT8 sec;
UINT8 min;
UINT8 hour;
UINT8 day;
UINT8 month;
UINT8 year;
UINT8 wday;
/* testing parameter range validity */
if (!format_date_available(date_time))
return RVF_INVALID_PARAMETER;
sec = conv_bcd(date_time.second);
min = conv_bcd(date_time.minute);
if (date_time.mode_12_hour == FALSE)
{
* (volatile UINT8 *)RTC_CTRL_REG &= ~RTC_MODE_12_24;
hour = conv_bcd(date_time.hour);
}
else
{
* (volatile UINT8 *)RTC_CTRL_REG |= RTC_MODE_12_24;
hour = conv_bcd(date_time.hour);
if (date_time.PM_flag != FALSE)
hour |= (0x80);
}
day = conv_bcd(date_time.day);
month = conv_bcd(date_time.month);
year = conv_bcd(date_time.year);
wday = conv_bcd(date_time.wday);
/* Disable all IT before reading register */
rvf_disable(25);
if (RTC_TEST_BUSY)
{
rvf_enable();
return RVF_NOT_READY;
}
else
{
* (volatile UINT8 *) RTC_DAYS_REG = day;
* (volatile UINT8 *) RTC_MONTHS_REG = month;
* (volatile UINT8 *) RTC_YEARS_REG = year;
* (volatile UINT8 *) RTC_WEEK_REG = wday;
* (volatile UINT8 *) RTC_SECONDS_REG = sec;
* (volatile UINT8 *) RTC_MINUTES_REG = min;
* (volatile UINT8 *) RTC_HOURS_REG = hour;
rvf_enable();
}
return RVF_OK;
}
/*******************************************************************************
*
* RTC_GetAlarm
*
******************************************************************************/
T_RVF_RET RTC_GetAlarm(T_RTC_DATE_TIME* date_time)
{
date_time->second = rtc_date_time_alarm.second;
date_time->minute = rtc_date_time_alarm.minute;
date_time->hour = rtc_date_time_alarm.hour;
date_time->day = rtc_date_time_alarm.day;
date_time->month = rtc_date_time_alarm.month;
date_time->year = rtc_date_time_alarm.year;
date_time->wday = rtc_date_time_alarm.wday;
date_time->mode_12_hour = rtc_date_time_alarm.mode_12_hour;
date_time->PM_flag = rtc_date_time_alarm.PM_flag;
return RVF_OK;
}
/*******************************************************************************
*
* RTC_SetAlarm
*
******************************************************************************/
T_RVF_RET RTC_SetAlarm(T_RTC_DATE_TIME date_time, T_RV_RETURN return_path)
{
UINT8 sec;
UINT8 min;
UINT8 hour;
UINT8 day;
UINT8 month;
UINT8 year;
UINT8 wday;
/* testing parameter range validity */
if (!format_date_available(date_time))
return RVF_INVALID_PARAMETER;
sec = conv_bcd(date_time.second);
min = conv_bcd(date_time.minute);
if (date_time.mode_12_hour == FALSE)
hour = conv_bcd(date_time.hour);
else
hour = conv_bcd(date_time.hour) | (date_time.PM_flag << 7);
day = conv_bcd(date_time.day);
month = conv_bcd(date_time.month);
year = conv_bcd(date_time.year);
wday = conv_bcd(date_time.wday);
/* Disable all IT before reading register */
rvf_disable(25);
if (RTC_TEST_BUSY)
{
rvf_enable();
return RVF_NOT_READY;
}
else
{
*(volatile UINT8*) RTC_ALARM_DAYS_REG = day;
*(volatile UINT8*) RTC_ALARM_MONTHS_REG = month;
*(volatile UINT8*) RTC_ALARM_YEARS_REG = year;
*(volatile UINT8*) RTC_ALARM_SECONDS_REG = sec;
*(volatile UINT8*) RTC_ALARM_MINUTES_REG = min;
*(volatile UINT8*) RTC_ALARM_HOURS_REG = hour;
/* Enable alarm*/
RTC_ENABLE_IT_ALARM;
rvf_enable();
/* Save callback */
rtc_return_path.callback_func = return_path.callback_func;
rtc_return_path.addr_id = return_path.addr_id;
}
return RVF_OK;
}
/*******************************************************************************
*
* RTC_UnsetAlarm
*
******************************************************************************/
T_RVF_RET RTC_UnsetAlarm(void)
{
/* Disable all IT before reading register */
rvf_disable(25);
if (RTC_TEST_BUSY)
{
rvf_enable();
return RVF_NOT_READY;
}
else
{
/* Disable alarm*/
RTC_DISABLE_IT_ALARM;
rvf_enable();
}
return RVF_OK;
}
/*******************************************************************************
*
* RTC_Rounding30s
*
******************************************************************************/
void RTC_Rounding30s(void)
{
*(UINT8*) RTC_CTRL_REG |= RTC_ROUND_30S;
}
/*******************************************************************************
*
* RTC_Set12HourMode
*
******************************************************************************/
void RTC_Set12HourMode(BOOL Mode12Hour)
{
if ( Mode12Hour == FALSE)
* (volatile UINT8*) RTC_CTRL_REG &= 0xf7;
else
* (volatile UINT8*) RTC_CTRL_REG |= 0x08;
}
/*******************************************************************************
*
* RTC_Is12HourMode
*
******************************************************************************/
BOOL RTC_Is12HourMode(void)
{
if ( (*(volatile UINT8*) RTC_CTRL_REG & RTC_MODE_12_24) )
return TRUE;
else
return FALSE;
}
/*******************************************************************************
*
* RTC_ItTimerHandle
*
******************************************************************************/
void RTC_ItTimerHandle(void)
{
static double compensation = 0;
static UINT8 nb_sample = 0;
double delta;
UINT16 value = 0;
INT16 tmp_value = 0;
UINT8 cmp[15];
/* Evaluate average on one hour max */
if ( nb_sample < 60)
nb_sample++;
/* perform calculation of auto compensation each minute and evaluate an
average on one hour */
/* Number of 32 kHz clock lost in one second */
/* Accurate operation is : delta = CLK_32 - rtc_nb_32khz*CLK_PLL/g_nb_hf */
/* with CLK_32 = 32768 Hz and CLK_PLL depend on chipset */
delta = RTC_CLOCK_32K - rtc_nb_32khz*RTC_CLOCK_HF/rtc_nb_hf;
/* Number of 32 kHz clock lost in one hour */
delta *= 3600.0;
/* Average of the compensation to load */
compensation = (compensation*(nb_sample-1) + delta)/nb_sample;
if (compensation >= 0x7FFF)
tmp_value = 0x7FFE;
else if (compensation <= -0x7FFF)
tmp_value = -0x7FFE;
else
tmp_value = (INT16) compensation;
if (tmp_value > 0) /* if 32 Khz clock is slow */
value = tmp_value;
if (tmp_value < 0) /* if 32 Khz clock is fast */
value = 0xFFFF + tmp_value + 1;
/* Set value in compensation register */
if (!RTC_TEST_BUSY)
{
*(volatile UINT8*) RTC_COMP_MSB_REG = (UINT8) (value >> 8);
*(volatile UINT8*) RTC_COMP_LSB_REG = (UINT8) (value & 0xFF);
}
/*toto = value; tmp*/
/*NU_Activate_HISR(&hisr); tmp*/
}
/*******************************************************************************
*
* RTC_ItAlarmHandle
*
******************************************************************************/
void RTC_ItAlarmHandle(void)
{
/* Sending alarm event */
/* Post alarm event in RTC mailbox */
NU_Activate_HISR(&hisr);
/*if (rtc_msg_alarm_event)
rvf_send_msg(rtc_env_ctrl_blk->task_id, rtc_env_ctrl_blk->mbox_id, rtc_msg_alarm_event);*/
/* Free alarm IT line */
RTC_FREE_AL_ITLINE;
}
/*******************************************************************************
*
* RTC_GaugingHandler
*
******************************************************************************/
void RTC_GaugingHandler(void)
{
/* Number of 32 Khz clock at the end of the gauging */
rtc_nb_32khz = ((*(volatile UINT16 *)ULDP_COUNTER_32_MSB_REG) * 0x10000) +
(*(volatile UINT16 *)ULDP_COUNTER_32_LSB_REG);
/* Number of high frequency clock at the end of the gauging */
/* To convert in nbr of 13 Mhz clocks (5*13=65Mhz) */
rtc_nb_hf = ( ((*(volatile UINT16 *)ULDP_COUNTER_HI_FREQ_MSB_REG) * 0x10000) +
(*(volatile UINT16 *)ULDP_COUNTER_HI_FREQ_LSB_REG) );
}
/*******************************************************************************
*
* RTC_ProcessAlarmEvent
*
******************************************************************************/
void RTC_ProcessAlarmEvent(void)
{
T_RVF_MB_STATUS mb_status;
T_RTC_ALARM* msg_alarm;
/* Call MMI */
if (rtc_return_path.callback_func != NULL)
{
rtc_return_path.callback_func(NULL);
}
else
{
/* Reserve memory for alarm event */
mb_status = rvf_get_buf (rtc_env_ctrl_blk->prim_id, sizeof (T_RTC_ALARM ), (void **) &msg_alarm);
if ((mb_status == RVF_GREEN) || (mb_status == RVF_YELLOW)) /* Memory allocation success */
{
msg_alarm->os_hdr.msg_id = RTC_ALARM_EVT;
/* Send event in the mailbox */
rvf_send_msg(rtc_return_path.addr_id, msg_alarm);
}
else
{
rvf_send_trace("Memory allocation error",23, NULL_PARAM, RV_TRACE_LEVEL_ERROR, RTC_USE_ID );
}
}
}
/* glowing,2004-06-16, import the following from M188 */
/*******************************************************************************
*
* RTC_GetDateTime
*
******************************************************************************/
UINT8 RTC_GetStatus( T_RTC_STATUS* rtc_status )
{
UINT8 ctrl_reg;
UINT8 stat_reg;
UINT8 cmpl_reg;
UINT8 cmpm_reg;
UINT8 int_reg;
/* Disable all IT before reading register */
//rvf_disable(25);
if (RTC_TEST_BUSY)
{
rvf_enable();
return RVF_NOT_READY;
}
else
{
ctrl_reg = * (volatile UINT8 *) RTC_CTRL_REG;
stat_reg = * (volatile UINT8 *) RTC_STATUS_REG;
cmpl_reg = * (volatile UINT8 *) RTC_COMP_LSB_REG;
cmpm_reg = * (volatile UINT8 *) RTC_COMP_MSB_REG;
int_reg = * (volatile UINT8 *) RTC_INTERRUPTS_REG;
rvf_enable();
}
rtc_status->ctrl_reg = conv_bin(ctrl_reg);
rtc_status->stat_reg = conv_bin(stat_reg);
rtc_status->cmpl_reg = conv_bin(cmpl_reg);
rtc_status->cmpm_reg = conv_bin(cmpm_reg);
rtc_status->int_reg = conv_bin(int_reg);
return RVF_OK;
}
UINT8 Rtc_getctrreg( void )
{
/* Disable all IT before reading register */
//rvf_disable(25);
if (RTC_TEST_BUSY)
{
rvf_enable();
return RVF_NOT_READY;
}
else
{
return( * (volatile UINT8 *) RTC_CTRL_REG);
}
}
UINT8 Rtc_getstatusreg( void )
{
/* Disable all IT before reading register */
//rvf_disable(25);
if (RTC_TEST_BUSY)
{
rvf_enable();
return RVF_NOT_READY;
}
else
{
return( * (volatile UINT8 *) RTC_STATUS_REG);
}
}
/* glowing,2004-06-16, end of import */
/*---------------------------------------------------------------------------------------*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -