📄 hw_rtc.c
字号:
/******************************************************************/
/* Copyright (C) 2007 ROCK-CHIPS FUZHOU . All Rights Reserved. */
/*******************************************************************
File : rtc.c
Desc : rtc接口函数的实现
Author : huangxinyu
Date : 2007-05-31
Notes :
$Log: hw_rtc.c,v $
Revision 1.2 2008/06/19 04:43:33 Administrator
代码整理!
Revision 1.1.1.1 2008/05/07 04:15:08 Administrator
no message
Revision 1.1.1.1 2008/03/06 13:29:07 Lingzhaojun
no message
Revision 1.3 2007/11/02 03:11:16 Huangzufang
增加RTC
Revision 1.2 2007/10/08 02:38:46 Lingzhaojun
添加版本自动注释脚本
* huangxinyu 2007-06-01 修改RTC接口名称
*********************************************************************/
#include "hw_include.h"
#include "hw_rtc.h"
#include "hwapi_interrupt.h"
#include "hw_serial.h"
#define pRTCReg ((pRTCReg_t)RTC_REG_BASE)
//static pRTCReg_t pRTCReg = (pRTCReg_t)RTC_REG_BASE;
#define RTC_CTRL_SET_FLAG(x) SetRegBits32(&(pRTCReg->RTC_CTRL),(x));
#define RTC_CTRL_CLR_FLAG(x) ClrRegBits32(&(pRTCReg->RTC_CTRL),(x));
#define RTC_SET_DIVIDER(x) MaskRegBits32(&(pRTCReg->RTC_CTRL), ~0xf8000000, (x));
#define RTC_GET_DIVIDER() ClrRegBits32(&(pRTCReg->RTC_CTRL), ~ BITMASK(27));
#define RTC_BSOS_EN() RTC_CTRL_SET_FLAG( RTC_CTRL_BSOS )
#define RTC_BSOS_DIS() RTC_CTRL_CLR_FLAG( RTC_CTRL_BSOS )
#define RTC_INTR_LEVEL() RTC_CTRL_SET_FLAG( RTC_CTRL_IT )
#define RTC_INTR_EDGE() RTC_CTRL_CLR_FLAG( RTC_CTRL_IT )
#define RTC_INTR_EN() RTC_CTRL_SET_FLAG( RTC_CTRL_INTE )
#define RTC_INTR_DIS() RTC_CTRL_CLR_FLAG( RTC_CTRL_INTE )
#define RTC_ALRM_EN() RTC_CTRL_SET_FLAG( RTC_CTRL_ALRM )
#define RTC_ALRM_DIS() RTC_CTRL_CLR_FLAG( RTC_CTRL_ALRM )
#define RTC_EN() RTC_CTRL_SET_FLAG( RTC_CTRL_EN )
#define RTC_DIS() RTC_CTRL_CLR_FLAG( RTC_CTRL_EN )
#define RTC_RESET() SetRegBits32(&(pRTCReg->RTC_CTRL),0x10000000);
/**************************************************************************
* 函数描述: 将time结构数据转化为time寄存器中32位数
* 入口参数: *tm -- time结构数据
* 出口参数: temp -- time寄存器中32位数
* 返回值: 无
***************************************************************************/
static UINT32 rtc_time_to_u32(const rtc_time_t *tm)
{
UINT32 temp = 0;
temp |= (tm->dow << RTC_TIME_DOW);
temp |= (tm->ten_hr << RTC_TIME_TH);
temp |= (tm->hr << RTC_TIME_H);
temp |= (tm->ten_min << RTC_TIME_TM);
temp |= (tm->min << RTC_TIME_M);
temp |= (tm->ten_sec << RTC_TIME_TS);
temp |= (tm->sec << RTC_TIME_S);
temp |= (tm->sos << RTC_TIME_SOS);
return temp;
}
/**************************************************************************
* 函数描述: 将date结构数据转化为date寄存器中32位数
* 入口参数: *dt -- date结构数据
* 出口参数: temp -- date寄存器中32位数
* 返回值: 无
***************************************************************************/
static UINT32 rtc_date_to_u32(const rtc_date_t *dt)
{
UINT32 temp = 0;
temp |= (dt->day << RTC_DATE_D);
temp |= (dt->ten_day << RTC_DATE_TD);
temp |= (dt->mth << RTC_DATE_M);
temp |= (dt->ten_mth << RTC_DATE_TM);
temp |= (dt->yr << RTC_DATE_Y);
temp |= (dt->ten_yr << RTC_DATE_TY);
temp |= (dt->cent << RTC_DATE_C);
temp |= (dt->ten_cent << RTC_DATE_TC);
return temp;
}
/**************************************************************************
* 函数描述: 将time寄存器中32位数转化为time结构数据
* 入口参数: temp -- time寄存器中32位数
* 出口参数: *tm -- time结构数据
* 返回值: 无
***************************************************************************/
static void rtc_u32_to_time(rtc_time_t *tm, const UINT32 temp)
{
tm->sos = (temp >> RTC_TIME_SOS) & BITMASK(4);
tm->sec = (temp >> RTC_TIME_S) & BITMASK(4);
tm->ten_sec = (temp >> RTC_TIME_TS) & BITMASK(3);
tm->min = (temp >> RTC_TIME_M) & BITMASK(4);
tm->ten_min = (temp >> RTC_TIME_TM) & BITMASK(3);
tm->hr = (temp >> RTC_TIME_H) & BITMASK(4);
tm->ten_hr = (temp >> RTC_TIME_TH) & BITMASK(2);
tm->dow = (temp >> RTC_TIME_DOW) & BITMASK(3);
}
/**************************************************************************
* 函数描述: 将date寄存器中32位数转化为date结构数据
* 入口参数: temp -- date寄存器中32位数
* 出口参数: *dt -- date结构数据
* 返回值: 无
***************************************************************************/
static void rtc_u32_to_date(rtc_date_t *dt, const data_t temp)
{
dt->day = (temp >> RTC_DATE_D) & BITMASK(4);
dt->ten_day = (temp >> RTC_DATE_TD) & BITMASK(2);
dt->mth = (temp >> RTC_DATE_M) & BITMASK(4);
dt->ten_mth = (temp >> RTC_DATE_TM) & BITMASK(1);
dt->yr = (temp >> RTC_DATE_Y) & BITMASK(4);
dt->ten_yr = (temp >> RTC_DATE_TY) & BITMASK(4);
dt->cent = (temp >> RTC_DATE_C) & BITMASK(4);
dt->ten_cent = (temp >> RTC_DATE_TC) & BITMASK(4);
}
/**************************************************************************
* 函数描述: 初始化RTC
* 入口参数: 无
* 出口参数: 无
* 返回值: TRUE -- 初始化成功
* FALSE -- 初始化失败
***************************************************************************/
BOOL RTC_PowerOnInit(void)
{
RTC_RESET();
RTC_SET_DIVIDER(RTC_DIVIDER);
RTC_EN(); /* add by huangzf */
return TRUE;
}
/**************************************************************************
* 函数描述: 设置RTC时间
* 入口参数: RTC的时间结构(/时/分/秒/星期)
* 出口参数: 无
* 返回值: 无
***************************************************************************/
void rtc_set_time(const rtc_time_t *tm)
{
UINT32 temp = rtc_time_to_u32(tm);
WriteReg32(&(pRTCReg->RTC_TIME), temp);
}
/**************************************************************************
* 函数描述: 设置RTC日期
* 入口参数: RTC的日期结构(年/月/日)
* 出口参数: 无
* 返回值: 无
***************************************************************************/
void rtc_set_date(const rtc_date_t *dt)
{
UINT32 temp = rtc_date_to_u32(dt);
WriteReg32(&(pRTCReg->RTC_DATE), temp);
}
/**************************************************************************
* 函数描述: 得到RTC时间
* 入口参数: 无
* 出口参数: RTC的时间结构数据(/时/分/秒/星期)
* 返回值: 无
***************************************************************************/
void rtc_get_time(rtc_time_t *tm)
{
UINT32 temp = ReadReg32(&(pRTCReg->RTC_TIME));
rtc_u32_to_time(tm, temp);
}
/**************************************************************************
* 函数描述: 得到RTC日期
* 入口参数: 无
* 出口参数: RTC的日期结构数据(年/月/日)
* 返回值: 无
***************************************************************************/
void rtc_get_date(rtc_date_t *dt)
{
UINT32 temp = ReadReg32(&(pRTCReg->RTC_DATE));
rtc_u32_to_date(dt, temp);
}
/**************************************************************************
* 函数描述: 设置RTC值
* 入口参数: RTC的年/月/日/时/分/秒/星期 结构数据
* 出口参数: 无
* 返回值: 0 -- 设置成功
* 非0 -- 设置失败
***************************************************************************/
int RTC_Set(rtc_date_time_t *rtc)
{
rtc_date_t dt;
rtc_time_t tm;
dt.ten_cent = (rtc->year) / 1000;
dt.cent = ((rtc->year) / 100) - (dt.ten_cent * 10) ;
dt.ten_yr = ((rtc->year) / 10) - (dt.ten_cent * 100) - (dt.cent * 10);
dt.yr = (rtc->year) - (dt.ten_cent * 1000) - (dt.cent * 100) - (dt.ten_yr * 10);
dt.ten_mth = (rtc->mon) / 10 ;
dt.mth = (rtc->mon) - (dt.ten_mth * 10);
dt.ten_day = (rtc->day) / 10;
dt.day = (rtc->day) - (dt.ten_day * 10) ;
tm.ten_hr = (rtc->hour) / 10;
tm.hr = (rtc->hour) - (tm.ten_hr * 10);
tm.ten_min = (rtc->minute) / 10;
tm.min = (rtc->minute) - (tm.ten_min * 10);
tm.ten_sec = (rtc->sec) / 10;
tm.sec = (rtc->sec) - (tm.ten_sec * 10);
tm.sos = 0 ;
tm.dow = rtc->week;
rtc_set_date(&dt);
rtc_set_time(&tm);
RTC_EN();
return 0;
}
/**************************************************************************
* 函数描述: 读取RTC值
* 入口参数: 无
* 出口参数: RTC的年/月/日/时/分/秒/星期 结构数据
* 返回值: 0 -- 读取成功
* 非0 -- 读取失败
***************************************************************************/
int RTC_Read(rtc_date_time_t *rtc)
{
rtc_date_t dt;
rtc_time_t tm;
rtc_get_date(&dt);
rtc_get_time(&tm);
rtc->year = dt.ten_cent * 1000 + dt.cent * 100 + dt.ten_yr * 10 + dt.yr;
rtc->mon = dt.ten_mth * 10 + dt.mth;
rtc->day = dt.ten_day * 10 + dt.day;
rtc->hour = tm.ten_hr * 10 + tm.hr;
rtc->minute = tm.ten_min * 10 + tm.min;
rtc->sec = tm.ten_sec * 10 + tm.sec;
rtc->week = tm.dow;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -