📄 drvrtc.c
字号:
////////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2006-2007 MStar Semiconductor, Inc.
// All rights reserved.
//
// Unless otherwise stipulated in writing, any and all information contained
// herein regardless in any format shall remain the sole proprietary of
// MStar Semiconductor Inc. and be kept in strict confidence
// (¨MStar Confidential Information〃) by the recipient.
// Any unauthorized act including without limitation unauthorized disclosure,
// copying, use, reproduction, sale, distribution, modification, disassembling,
// reverse engineering and compiling of the contents of MStar Confidential
// Information is unlawful and strictly prohibited. MStar hereby reserves the
// rights to any and all damages, losses, costs and expenses resulting therefrom.
//
////////////////////////////////////////////////////////////////////////////////
#include "hwreg.h"
#include "drvRTC.h"
#define RTC_CLOCK FREQ_14P318MHZ // XXX need to be changed to real clock
////////////////////////////////////////////////////////////////////////////////
// How to use RTC:
// 1. set freq_cw according to the frequency of XTAL
// 2. set cnt_en to HIGH to enable the divided clock output
// 3. program load_val for using as the initial value of RTC counter
// 4. set load_en to HIGH (this is a write&clear register), then load_val will
// be loaded into RTC counter (32-bit)
// Then RTC will continue counting base on the divided clk rate
//
// How to read RTC value:
// 1. set read_en to HIGH, then the value of RTC counter will be double
// buffered into rtc_cnt
// 2. SW then can read the value from rtc_cnt
//
// Additional controls:
// 1. match_value: when RTC counter is matched to this value, an interrupt
// will be issued (can be used as an alarm, or other application)
// 2. wrap_en: when this flag is enabled, once RTC counter is matched to
// match_value, the counter will be reset to zero and count from zero.
////////////////////////////////////////////////////////////////////////////////
#include "drvGlobal.h"
// register definition from register table
#define REG_RTC_CTRL RTC_REG_BASE + 0x00
#define RTC_CTRL_NOT_RSTZ (1 << 0)
#define RTC_CTRL_CNT_EN (1 << 1)
#define RTC_CTRL_WRAP_EN (1 << 2)
#define RTC_CTRL_LOAD_EN (1 << 3)
#define RTC_CTRL_READ_EN (1 << 4)
#define RTC_CTRL_INT_MASK (1 << 5)
#define RTC_CTRL_INT_FORCE (1 << 6)
#define RTC_CTRL_INT_CLEAR (1 << 7)
#define REG_RTC_FREQ_CW (RTC_REG_BASE + 0x01 * 2)
#define REG_RTC_LOAD_VAL (RTC_REG_BASE + 0x03 * 2)
#define REG_RTC_MATCH_VAL (RTC_REG_BASE + 0x05 * 2)
#define REG_RTC_INTERRUPT (RTC_REG_BASE + 0x07 * 2)
#define RTC_INT_RAW_STATUS (1 << 0)
#define RTC_INT_STATUS (1 << 1)
#define REG_RTC_CNT (RTC_REG_BASE + 0x08 * 2)
//-------------------------------------------------------------------------------------------------
/// RTC Reset Function
/// @param
/// @return
///
//-------------------------------------------------------------------------------------------------
void MDrv_RTC_Reset( void )
{
MDrv_WriteRegBit( REG_RTC_CTRL, 0, _BIT0 );
MDrv_WriteRegBit( REG_RTC_CTRL, 1, _BIT0 );
}
//-------------------------------------------------------------------------------------------------
/// RTC Set Control Word Function
/// @param
/// @return
///
//-------------------------------------------------------------------------------------------------
void MDrv_RTC_SetCW( U32 u32CtrlWord )
{
MDrv_Write4Byte( REG_RTC_FREQ_CW, u32CtrlWord );
}
//-------------------------------------------------------------------------------------------------
/// RTC Count Enable Function
/// @param
/// @return
///
//-------------------------------------------------------------------------------------------------
void MDrv_RTC_CountEnable( void )
{
MDrv_WriteRegBit( REG_RTC_CTRL, 1, RTC_CTRL_CNT_EN );
}
//-------------------------------------------------------------------------------------------------
/// RTC Initialization Function
/// @param
/// @return
///
//-------------------------------------------------------------------------------------------------
void MDrv_RTC_Init( U32 u32CtrlWord )
{
MDrv_WriteByte( REG_RTC_CTRL, 0 );
MDrv_WriteByte( REG_RTC_CTRL, RTC_CTRL_NOT_RSTZ | RTC_CTRL_INT_MASK );
MDrv_Write4Byte( REG_RTC_FREQ_CW, u32CtrlWord );
MDrv_WriteByte( REG_RTC_CTRL, RTC_CTRL_NOT_RSTZ | RTC_CTRL_CNT_EN | RTC_CTRL_INT_MASK | RTC_CTRL_INT_CLEAR );
}
//-------------------------------------------------------------------------------------------------
/// RTC Set Counter Function
/// @param
/// @return
///
//-------------------------------------------------------------------------------------------------
void MDrv_RTC_SetCounter( U32 u32Counter )
{
MDrv_Write4Byte( REG_RTC_LOAD_VAL, u32Counter );
MDrv_WriteRegBit( REG_RTC_CTRL, 1, RTC_CTRL_LOAD_EN );
}
//-------------------------------------------------------------------------------------------------
/// RTC Get Counter Function
/// @param
/// @return
///
//-------------------------------------------------------------------------------------------------
U32 MDrv_RTC_GetCounter( void )
{
MDrv_WriteRegBit( REG_RTC_CTRL, 1, RTC_CTRL_READ_EN );
return MDrv_Read4Byte( REG_RTC_CNT );
}
//-------------------------------------------------------------------------------------------------
/// RTC Alarm Function
/// @param
/// @return
///
//-------------------------------------------------------------------------------------------------
void MDrv_RTC_Alarm( U32 u32AlarmCounter )
{
MDrv_WriteRegBit( REG_RTC_CTRL, 1, RTC_CTRL_INT_MASK | RTC_CTRL_INT_CLEAR );
if ( u32AlarmCounter )
{
MDrv_Write4Byte( REG_RTC_MATCH_VAL, u32AlarmCounter );
MDrv_WriteRegBit( REG_RTC_CTRL, 0, RTC_CTRL_INT_MASK );
}
}
//-------------------------------------------------------------------------------------------------
/// RTC Clear Interrupt Function
/// @param
/// @return
///
//-------------------------------------------------------------------------------------------------
void MDrv_RTC_ClearInterrupt( void )
{
MDrv_WriteRegBit( REG_RTC_CTRL, 1, RTC_CTRL_INT_CLEAR );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -