⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mx21_rtc.c

📁 MX21_InitCodeLib.rar freescale mx21系列ARM芯片9328的WINCE5.0下初始化代码
💻 C
字号:
/**********************************************************************
*
*         (C) COPYRIGHT 2004 FREESCALE, INC.
*         ALL RIGHTS RESERVED
*
*     THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF FREESCALE, INC.
*     The copyright notice above does not evidence any actual or
*     intended publication of such source code
*
*     Freescale Confidential Proprietary
*
*
*     Group/Division: WMSG/MMDO
*
*     Description:
*
*     Related Specifications:
*
*     Errata:
*
*     File Name: MX21_RTC.c
*     Revision Number: 0.1
*     Author(s): Sharad Kumar
*     Date created:
*     Revision History:
*        Date      Rev     Description
*        ----      ---     -----------
*        30Apr2004  0.1     First Draft
*
**********************************************************************/

#include "MX21_RTC.h"

#if DEMO
	extern volatile int gFastInt;
	extern volatile int gNormInt;
	extern void gpt3_WaitTime(uint32_t);
#endif

//---------------------------------------------------
// issue a soft reset to the RTC by writing the 
// SWR bit of the RTC control register to 1
//---------------------------------------------------
void 
Rtc_SoftReset(void)
{
	RTC_RCCTL.bits.SWR = RTC_SWR_SET;
	return;
}

//---------------------------------------------------
// enable the RTC by writing a 1 to the EN 
// bit of the RTC control register
//---------------------------------------------------
void 
Rtc_Enable(void)
{
	RTC_RCCTL.bits.EN = RTC_ENABLE;
	return;
}

//---------------------------------------------------
// disable the RTC by writing a 0 to the EN 
// bit of the RTC control register
//---------------------------------------------------
void Rtc_Disable(void)
{
	RTC_RCCTL.bits.EN = RTC_DISABLE;
	return;
}

//---------------------------------------------------
// set time of day by specifying
// days, hours, mins, and seconds
//---------------------------------------------------
void 
Rtc_SetTime(uint32_t days,
            uint32_t hours,
            uint32_t mins,
            uint32_t secs) 
{
	         
    // set the day for curent time
	RTC_DAYR.bits.DAYS = days;
	
	// set the hours, minutes for curent time
	RTC_HOURMIN.bits.HOURS = hours;
	RTC_HOURMIN.bits.MINUTES = mins;
	
	// set the seconds for curent time
	RTC_SECONDS.bits.SECONDS = secs;
	
}

//---------------------------------------------------
// set the RTC stop watch counter
//---------------------------------------------------
void
Rtc_SetStpwch(uint32_t value) {
	RTC_STPWCH.bits.CNT = value;
}

//---------------------------------------------------
// select the proper crystal frequency by 
// writing to the XTL bits. Specify:
// 0x0, 0x60 for 32.768 Khz, 
// 0x40 for 32 Khz
// 0x60 for 32.768 Khz
//---------------------------------------------------
void 
Rtc_SetXTLBits(xtl_val) {
	RTC_RCCTL.bits.XTL = xtl_val;
}

//---------------------------------------------------
// set the alarm by specifying the 
// time of alarm in days, hours,mins, seconds
//---------------------------------------------------
void 
Rtc_SetAlarm(uint32_t days, 
             uint32_t hours,
             uint32_t mins,
             uint32_t secs) {
	

    // set the day for curent time
	RTC_DAYALARM.bits.DAYSAL = days;
	
	// set the hours, minutes for curent time
	RTC_ALRM_HM.bits.HOURS = hours;
	RTC_ALRM_HM.bits.MINUTES = mins;
	
	// set the seconds for curent time
	RTC_ALRM_SEC.bits.SECONDS = secs;
	
}

//---------------------------------------------------
// enable a selected interrupt type
//---------------------------------------------------
void
Rtc_EnableInterrupts (rtc_int_type  rtc_int,
                      uint32_t      value) {


	// bit or bit field value
	uint32_t _value = 1;
	
	switch(rtc_int) {
	
		case RTC_SAM7:	 
			RTC_RTCIENR.bits.SAM7    = _value;
			break;
		case RTC_SAM6:	 
			RTC_RTCIENR.bits.SAM6    = _value;
			break;
		case RTC_SAM5:	 
			RTC_RTCIENR.bits.SAM5    = _value;
			break;
		case RTC_SAM4:	 
			RTC_RTCIENR.bits.SAM4    = _value;
			break;
		case RTC_SAM3:	 
			RTC_RTCIENR.bits.SAM3    = _value;
			break;		
		case RTC_SAM2:	 
			RTC_RTCIENR.bits.SAM2    = _value;
			break;		
		case RTC_SAM1:
			RTC_RTCIENR.bits.SAM1    = _value;
			break;		
		case RTC_SAM0:
			RTC_RTCIENR.bits.SAM0    = _value;
			break;		
		case RTC_2HZ:
			RTC_RTCIENR.bits.TWO_HZ  = _value;
			break;		
		case RTC_HR:
			RTC_RTCIENR.bits.HR      = _value;
			break;		
		case RTC_1HZ:
			RTC_RTCIENR.bits.ONE_HZ  = _value;
			break;		
		case RTC_DAY:
			RTC_RTCIENR.bits.DAY     = _value;
			break;		
		case RTC_ALM:
			RTC_RTCIENR.bits.ALM     = _value;
			break;		
		case RTC_MIN:
			RTC_RTCIENR.bits.MIN     = _value;
			break;		
		case RTC_SW:
			RTC_RTCIENR.bits.SW      = _value;
			break;		
		case RTC_VEC:
		default:
			_value = value;
			RTC_RTCIENR.all          = _value;
			break;
	}
	return;
} 


//---------------------------------------------------
// disble a selected interrupt type
//---------------------------------------------------
void
Rtc_DisInterrupts (rtc_int_type  rtc_int,
                   uint32_t      value) {

	
	// bit or bit field value
	uint32_t _value = 0;
	
	switch(rtc_int) {
	
		case RTC_SAM7:	 
			RTC_RTCIENR.bits.SAM7   = _value;
			break;
		case RTC_SAM6:	 
			RTC_RTCIENR.bits.SAM6   = _value;
			break;
		case RTC_SAM5:	 
			RTC_RTCIENR.bits.SAM5   = _value;
			break;
		case RTC_SAM4:	 
			RTC_RTCIENR.bits.SAM4   = _value;
			break;
		case RTC_SAM3:	 
			RTC_RTCIENR.bits.SAM3   = _value;
			break;		
		case RTC_SAM2:	 
			RTC_RTCIENR.bits.SAM2   = _value;
			break;		
		case RTC_SAM1:
			RTC_RTCIENR.bits.SAM1   = _value;
			break;		
		case RTC_SAM0:
			RTC_RTCIENR.bits.SAM0   = _value;
			break;		
		case RTC_2HZ:
			RTC_RTCIENR.bits.TWO_HZ = _value;
			break;		
		case RTC_HR:
			RTC_RTCIENR.bits.HR     = _value;
			break;		
		case RTC_1HZ:
			RTC_RTCIENR.bits.ONE_HZ = _value;
			break;		
		case RTC_DAY:
			RTC_RTCIENR.bits.DAY    = _value;
			break;		
		case RTC_ALM:
			RTC_RTCIENR.bits.ALM    = _value;
			break;		
		case RTC_MIN:
			RTC_RTCIENR.bits.MIN    = _value;
			break;		
		case RTC_SW:
			RTC_RTCIENR.bits.SW     = _value;
			break;		
		case RTC_VEC:
		default:
			_value = value;
			RTC_RTCIENR.all         = _value;
			break;
	}
	return;
} 


//---------------------------------------------------
// check status of selected interrupt                
//---------------------------------------------------                          
uint32_t
Rtc_ChkIntStatus(rtc_int_type rtc_int) {
	
	
	uint32_t _regval;
    
	// read a single interrupt 
	// status bit
	switch(rtc_int) {

		case RTC_SAM7:	 
			_regval = RTC_RTCISR.bits.SAM7;
			break;
		case RTC_SAM6:	 
			_regval = RTC_RTCISR.bits.SAM6;
			break;
		case RTC_SAM5:	 
			_regval = RTC_RTCISR.bits.SAM5;
			break;
		case RTC_SAM4:	 
			_regval = RTC_RTCISR.bits.SAM4;
			break;
		case RTC_SAM3:	 
			_regval = RTC_RTCISR.bits.SAM3;
			break;		
		case RTC_SAM2:	 
			_regval = RTC_RTCISR.bits.SAM2;
			break;		
		case RTC_SAM1:
			_regval = RTC_RTCISR.bits.SAM1;
			break;		
		case RTC_SAM0:
			_regval = RTC_RTCISR.bits.SAM0;
			break;		
		case RTC_2HZ:
			_regval = RTC_RTCISR.bits.TWO_HZ;
			break;		
		case RTC_HR:
			_regval = RTC_RTCISR.bits.HR;
			break;		
		case RTC_1HZ:
			_regval = RTC_RTCISR.bits.ONE_HZ;
			break;		
		case RTC_DAY:
			_regval = RTC_RTCISR.bits.DAY;
			break;		
		case RTC_ALM:
			_regval = RTC_RTCISR.bits.ALM;
			break;		
		case RTC_MIN:
			_regval = RTC_RTCISR.bits.MIN;
			break;		
		case RTC_SW:
			_regval = RTC_RTCISR.bits.SW;
			break;		
	
		// entire register read	 
		case RTC_VEC:			
		default:
			 _regval = RTC_RTCISR.all;
			 break;	
	}
	return _regval;
}

//---------------------------------------------------
// clear the selected interrupt
//---------------------------------------------------
void
Rtc_ClrIntStatus(rtc_int_type rtc_int) {
	
		
	// clear bit value
	uint32_t _value = 0;
	

	switch(rtc_int) {
	
		case RTC_SAM7:	 
			RTC_RTCISR.bits.SAM7   = _value;
			break;
		case RTC_SAM6:	 
			RTC_RTCISR.bits.SAM6   = _value;
			break;
		case RTC_SAM5:	 
			RTC_RTCISR.bits.SAM5   = _value;
			break;
		case RTC_SAM4:	 
			RTC_RTCISR.bits.SAM4   = _value;
			break;
		case RTC_SAM3:	 
			RTC_RTCISR.bits.SAM3   = _value;
			break;		
		case RTC_SAM2:	 
			RTC_RTCISR.bits.SAM2   = _value;
			break;		
		case RTC_SAM1:
			RTC_RTCISR.bits.SAM1   = _value;
			break;		
		case RTC_SAM0:
			RTC_RTCISR.bits.SAM0   = _value;
			break;		
		case RTC_2HZ:
			RTC_RTCISR.bits.TWO_HZ = _value;
			break;		
		case RTC_HR:
			RTC_RTCISR.bits.HR     = _value;
			break;		
		case RTC_1HZ:
			RTC_RTCISR.bits.ONE_HZ = _value;
			break;		
		case RTC_DAY:
			RTC_RTCISR.bits.DAY    = _value;
			break;		
		case RTC_ALM:
			RTC_RTCISR.bits.ALM    = _value;
			break;		
		case RTC_MIN:
			RTC_RTCISR.bits.MIN    = _value;
			break;		
		case RTC_SW:
			RTC_RTCISR.bits.SW     = _value;
			break;		
		case RTC_VEC:
		default:
			RTC_RTCISR.all		   = _value;
			break;
	}
		
}

//---------------------------------------------------
// sample MX21 initialization routine 
// to setup the real time clock.
//---------------------------------------------------
void Rtc_setup(uint32_t     days,
               uint32_t     hours,
               uint32_t		mins,
               uint32_t     secs,
               uint32_t     alm_day,
               uint32_t     alm_hrs,
               uint32_t		alm_mins,
               uint32_t     alm_secs,
               uint32_t     stp_wat_time,
               rtc_int_type rtc_int,
               uint32_t     vec_val)
{

    // Is a soft reset required here ?
	
	
	// Disable RTC
	Rtc_Disable();
	
	// for MX21 crystal frequency is
	// 32.768 KHz = 0x60h
	Rtc_SetXTLBits(0x60);

	//clear all the bit of the rtc status register
	Rtc_ClrIntStatus(RTC_VEC);
	
	// setup the time of day
	Rtc_SetTime(days, hours, mins, secs);
	
	// setup alarm
	Rtc_SetAlarm(alm_day, alm_hrs, alm_mins, alm_secs);
	
	// setup stop watch
	Rtc_SetStpwch(stp_wat_time);


	// Enable the interrupts by
	// specifying a 32 bit vector
	Rtc_EnableInterrupts(rtc_int, vec_val);
	
	// Enable the RTC
	Rtc_Enable();
	
	return;    
}

//---------------------------------------------------
// in this example the RTC interrupt type is specified to be 
// of type SAM0. If multiple interrupts need to be simultaneously
// enabled, chose an int of type RTC_VEC for interrupt type, and
// specify the 32 bit enable vector in the vector field.
//---------------------------------------------------
#if DEMO

void
rtc_demo() 
{
	
	uint32_t _delay = 1000000;
	
	gFastInt = 0;
	gNormInt = 0;
				
	Rtc_setup( 0,        // days
               0,        // hours
               0,        // mins
               0,        // secs
               0,        // alm_day
               0,        // alm_hrs,
               0,        // alm_mins
               0,        // alm_secs,
               0,        // stp_wat_time,
               RTC_SAM0, // interrupt type - SAM0
               0);       // vector
	
	// insert a small delay
	// softDelay(_delay);
	
	// insert a small  delay
	// using the GPT3 counter
	//gpt3_WaitCycles(_delay);
	gpt3_WaitTime(_delay);
	
	// note: the RTC ISR will cause the the gNormInt counter to be incremented by 1
	// refer to the ISRs in the MX21_IntHandler.c file.
	if (1 != gNormInt) 
	{	
		PRINTF(" The value of the gNormInt, gFastInt are %d, %d\n", gNormInt, gFastInt);
		printf(" The RTC test case FAILS\n");
		return;
	}
	
    PRINTF(" The value of the gNormInt, gFastInt are %d, %d\n", gNormInt, gFastInt);	
	printf("\n The RTC test case passes\n");		
	
	
	return;
}

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -