📄 rtc_functions.c
字号:
/************************************************************************************
* rtc_functions.c : contains low level function for the RTC *
* *
* *
* Author: Laurent Sollier *
* *
* version: 1.0 *
* *
* Date: 03/20/01 *
* (C) Copyright 2000 by Texas Instruments Incorporated, All Rights Reserved *
************************************************************************************/
#include "rtc/rtc_messages_i.h"
#include "rtc/rtc_api.h"
#include "rtc/rtc_i.h"
#include <time.h>
#include <windows.h>
#include "rvm/rvm_use_id_list.h"
#pragma warning (disable:4244) /*'=' : conversion from 'int ' to 'unsigned
char ', possible loss of data */
/* ----- Global variables ----- */
static T_RV_RETURN rtc_return_path = {0, 0};
static T_RTC_DATE_TIME rtc_date_time_alarm = {0};
static BOOL rtc_Mode12Hour = FALSE;
static BOOL rtc_AlarmSet = FALSE;
static T_RTC_ALARM* rtc_msg_alarm_event = NULL;
extern T_RTC_ENV_CTRL_BLK* rtc_env_ctrl_blk;
/*******************************************************************************
*
* format_date_available
*
******************************************************************************/
BOOL format_date_available(T_RTC_DATE_TIME date_time)
{
UINT8 m;
if (date_time.second < 0 || date_time.second > 59)
return FALSE;
if (date_time.minute < 0 || date_time.minute > 59)
return FALSE;
if (date_time.mode_12_hour == FALSE)
{
if (date_time.hour < 0 || date_time.hour > 23)
return FALSE;
}
else
if (date_time.hour < 1 || date_time.hour > 12)
return FALSE;
if (date_time.month < 1 || date_time.month > 12)
return FALSE;
if (date_time.year < 0 || date_time.year > 99)
return FALSE;
if (date_time.wday < 0 || date_time.wday > 6)
return FALSE;
m = date_time.month;
if (m == 1||m == 3||m == 5||m == 7||m == 8||m == 10||m == 12)
{
if (date_time.day < 1 || date_time.day > 31)
return FALSE;
}
else
{
if (m == 4||m == 6||m == 9||m == 11)
{
if (date_time.day < 1 || date_time.day > 30)
return FALSE;
}
else
{
if (date_time.year%4)
{
if (date_time.day < 1 || date_time.day > 29)
return FALSE;
}
else
{
if (date_time.day < 1 || date_time.day > 28)
return FALSE;
}
}
}
return TRUE;
}
/*******************************************************************************
*
* RTC_Initialize
*
******************************************************************************/
T_RVF_RET RTC_Initialize(void)
{
T_RVF_MB_STATUS mb_status;
/* Reserve memory for alarm event */
mb_status = rvf_get_buf (rtc_env_ctrl_blk->prim_id, sizeof (T_RTC_ALARM ), (void **) &rtc_msg_alarm_event);
if ((mb_status == RVF_GREEN) || (mb_status == RVF_YELLOW)) /* Memory allocation success */
{
rtc_msg_alarm_event->os_hdr.msg_id = RTC_ALARM_EVT;
}
else
{
rtc_msg_alarm_event = NULL;
}
rtc_env_ctrl_blk->msg_alarm_event = rtc_msg_alarm_event;
return RVF_OK;
}
/*******************************************************************************
*
* RTC_RtcReset
*
******************************************************************************/
BOOL RTC_RtcReset(void)
{
static UINT8 i = 0;
BOOL ret;
if (!i)
ret = TRUE;
else
ret = FALSE;
i++;
return ret;
}
/*******************************************************************************
*
* RTC_GetDateTime
*
******************************************************************************/
T_RVF_RET RTC_GetDateTime(T_RTC_DATE_TIME* date_time)
{
struct tm system_time;
_getsystime(&system_time);
date_time->day = system_time.tm_mday;
date_time->month = system_time.tm_mon+1;
date_time->year = system_time.tm_year-100;
date_time->wday = system_time.tm_wday;
date_time->second = system_time.tm_sec;
date_time->minute = system_time.tm_min;
date_time->hour = system_time.tm_hour;
if (rtc_Mode12Hour == TRUE)
{
date_time->mode_12_hour = TRUE;
if (date_time->hour > 12)
{
date_time->hour -= 12;
date_time->PM_flag = TRUE;
}
else if (date_time->hour == 0)
{
date_time->hour = 12;
date_time->PM_flag = TRUE;
}
else
date_time->PM_flag = FALSE;
}
else
date_time->mode_12_hour = FALSE;
return RVF_OK;
}
/*******************************************************************************
*
* RTC_SetDateTime
*
******************************************************************************/
T_RVF_RET RTC_SetDateTime(T_RTC_DATE_TIME date_time)
{
LUID luid_time;
HANDLE TokenHandle;
TOKEN_PRIVILEGES global_privileges;
LUID_AND_ATTRIBUTES time_privilege;
SYSTEMTIME new_date = {0};
if (!format_date_available(date_time))
return RVF_INVALID_PARAMETER;
LookupPrivilegeValue(NULL, SE_SYSTEMTIME_NAME, &luid_time);
time_privilege.Luid = luid_time;
time_privilege.Attributes = SE_PRIVILEGE_ENABLED;
global_privileges.PrivilegeCount = 1;
global_privileges.Privileges[0] = time_privilege;
/* Modification of privilege for setting date and time */
OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES,
&TokenHandle );
AdjustTokenPrivileges(TokenHandle,
FALSE,
&global_privileges,
0,
NULL,
NULL);
new_date.wSecond = date_time.second;
new_date.wMinute = date_time.minute;
new_date.wDay = date_time.day;
new_date.wMonth = date_time.month;
new_date.wYear = date_time.year+2000;
if ( (date_time.mode_12_hour == TRUE) && (date_time.PM_flag == TRUE) )
if (date_time.hour == 12)
date_time.hour = 0;
else
date_time.hour += 12;
new_date.wHour = date_time.hour;
/* Change mode to be compliant with board RTC driver */
if (date_time.mode_12_hour == TRUE)
rtc_Mode12Hour = TRUE;
else
rtc_Mode12Hour = FALSE;
SetLocalTime(&new_date);
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;
}
DWORD WINAPI ThreadProc(LPVOID lpParameter )
{
UINT32 time_sleep = *(UINT32*)lpParameter;
Sleep(time_sleep*1000);
if (rtc_AlarmSet)
RTC_ItAlarmHandle();
return RVF_OK;
}
/*******************************************************************************
*
* RTC_SetAlarm
*
******************************************************************************/
T_RVF_RET RTC_SetAlarm(T_RTC_DATE_TIME date_time, T_RV_RETURN return_path)
{
DWORD ThreadId;
UINT32 wait_time;
struct tm time_alarm;
time_t current_time;
if (!format_date_available(date_time))
return RVF_INVALID_PARAMETER;
/* Save alarm date - time */
rtc_date_time_alarm.second = date_time.second;
rtc_date_time_alarm.minute = date_time.minute;
rtc_date_time_alarm.hour = date_time.hour;
rtc_date_time_alarm.day = date_time.day;
rtc_date_time_alarm.month = date_time.month;
rtc_date_time_alarm.year = date_time.year;
rtc_date_time_alarm.wday = date_time.wday;
rtc_date_time_alarm.mode_12_hour = date_time.mode_12_hour;
rtc_date_time_alarm.PM_flag = date_time.PM_flag;
/* Save callback alarm function */
rtc_return_path.callback_func = return_path.callback_func;
rtc_return_path.addr_id = return_path.addr_id;
/* Calculate time (in seconds) before activating alarm */
time_alarm.tm_mday = rtc_date_time_alarm.day;
time_alarm.tm_mon = rtc_date_time_alarm.month-1;
time_alarm.tm_year = rtc_date_time_alarm.year+100;
time_alarm.tm_wday = rtc_date_time_alarm.wday;
time_alarm.tm_sec = rtc_date_time_alarm.second;
time_alarm.tm_min = rtc_date_time_alarm.minute;
time_alarm.tm_hour = rtc_date_time_alarm.hour;
if ( (rtc_date_time_alarm.mode_12_hour == TRUE) && (rtc_date_time_alarm.PM_flag == TRUE) )
if (time_alarm.tm_hour == 12)
time_alarm.tm_hour = 0;
else
time_alarm.tm_hour += 12;
wait_time = mktime(&time_alarm) - time(¤t_time);
/* Create thread for simulating alarm interrupt */
CreateThread(NULL, 0, ThreadProc, &wait_time, 0, &ThreadId);
rtc_AlarmSet = TRUE;
return RVF_OK;
}
/*******************************************************************************
*
* RTC_UnsetAlarm
*
******************************************************************************/
T_RVF_RET RTC_UnsetAlarm(void)
{
rtc_AlarmSet = FALSE;
return RVF_OK;
}
/*******************************************************************************
*
* RTC_Rounding30s
*
******************************************************************************/
void RTC_Rounding30s(void)
{
T_RTC_DATE_TIME date_time;
struct tm rounding_time;
struct tm* good_rounding_time;
time_t rounding_time_sec;
/* Get date */
RTC_GetDateTime(&date_time);
/* Round seconds */
if (date_time.second > 30)
date_time.minute ++;
date_time.second = 0;
rounding_time.tm_mday = date_time.day;
rounding_time.tm_mon = date_time.month;
rounding_time.tm_year = date_time.year+100;
rounding_time.tm_wday = date_time.wday;
rounding_time.tm_sec = date_time.second;
rounding_time.tm_min = date_time.minute;
rounding_time.tm_hour = date_time.hour;
rounding_time_sec = mktime(&rounding_time);
good_rounding_time = localtime(&rounding_time_sec);
date_time.day = good_rounding_time->tm_mday;
date_time.month = good_rounding_time->tm_mon;
date_time.year = good_rounding_time->tm_year-100;
date_time.wday = good_rounding_time->tm_wday;
date_time.second = good_rounding_time->tm_sec;
date_time.minute = good_rounding_time->tm_min;
date_time.hour = good_rounding_time->tm_hour;
/* Set date */
RTC_SetDateTime(date_time);
}
/*******************************************************************************
*
* RTC_Set12HourMode
*
******************************************************************************/
void RTC_Set12HourMode(BOOL Mode12Hour)
{
rtc_Mode12Hour = Mode12Hour;
}
/*******************************************************************************
*
* RTC_Is12HourMode
*
******************************************************************************/
BOOL RTC_Is12HourMode(void)
{
return rtc_Mode12Hour;
}
/*******************************************************************************
*
* RTC_ItTimerHandle
*
******************************************************************************/
void RTC_ItTimerHandle(void)
{
}
/*******************************************************************************
*
* RTC_ItAlarmHandle
*
******************************************************************************/
void RTC_ItAlarmHandle(void)
{
/* Post alarm event in RTC mailbox */
if (rtc_msg_alarm_event)
rvf_send_msg(rtc_env_ctrl_blk->addr_id, rtc_msg_alarm_event);
}
/*******************************************************************************
*
* RTC_ProcessAlarmEvent
*
******************************************************************************/
void RTC_ProcessAlarmEvent(void)
{
T_RVF_MB_STATUS mb_status;
T_RTC_ALARM* msg_alarm;
rtc_AlarmSet = FALSE;
/* 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 );
}
}
}
/*---------------------------------------------------------------------------------------*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -