/** * @brief 设置RTC补偿寄存器 * @param compensationInterval Configures the compensation interval in seconds from 1 to 256 to control * how frequently the TCR should adjust the number of 32.768 kHz cycles in * each second. The value written should be one less than the number of * seconds (for example, write zero to configure for a compensation interval * of one second). This register is double buffered and writes do not take * affect until the end of the current compensation interval. * @param timeCompensation Configures the number of 32.768 kHz clock cycles in each second. This * register is double buffered and writes do not take affect until the end * of the current compensation interval.\n * \n * 80h Time prescaler register overflows every 32896 clock cycles.\n * ... ...\n * FFh Time prescaler register overflows every 32769 clock cycles.\n * 00h Time prescaler register overflows every 32768 clock cycles.\n * 01h Time prescaler register overflows every 32767 clock cycles.\n * ... ...\n * 7Fh Time prescaler register overflows every 32641 clock cycles.\n * @retval None */ void RTC_SetCompensation(uint32_t compensationInterval, uint32_t timeCompensation) { RTC->TCR &= ~RTC_TCR_CIR_MASK; RTC->TCR &= ~RTC_TCR_TCR_MASK; RTC->TCR |= RTC_TCR_CIR(compensationInterval); RTC->TCR |= RTC_TCR_TCR(timeCompensation); }
void rtc_init(uint32_t seconds, uint32_t alarm, uint8_t c_interval, uint8_t c_value, uint8_t interrupt) { int i; /*enable the clock to SRTC module register space*/ SIM_SCGC6 |= SIM_SCGC6_RTC_MASK; /*Only VBAT_POR has an effect on the SRTC, RESET to the part does not, so you must manually reset the SRTC to make sure everything is in a known state*/ /*clear the software reset bit*/ RTC_CR = RTC_CR_SWR_MASK; RTC_CR &= ~RTC_CR_SWR_MASK; /*Enable the interrupt*/ if(interrupt) enable_irq(66); /*Enable the oscillator*/ RTC_CR |= RTC_CR_OSCE_MASK; /*Wait to all the 32 kHz to stabilize, refer to the crystal startup time in the crystal datasheet*/ for(i=0;i<0x600000;i++); /*Set time compensation parameters*/ RTC_TCR = RTC_TCR_CIR(c_interval) | RTC_TCR_TCR(c_value); /*Configure the timer seconds and alarm registers*/ RTC_TSR = seconds; RTC_TAR = alarm; /*Enable the counter*/ RTC_SR |= RTC_SR_TCE_MASK; }
void RTC_Init(RTC_Type *base, const rtc_config_t *config) { assert(config); uint32_t reg; #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) CLOCK_EnableClock(kCLOCK_Rtc0); #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */ /* Issue a software reset if timer is invalid */ if (RTC_GetStatusFlags(RTC) & kRTC_TimeInvalidFlag) { RTC_Reset(RTC); } reg = base->CR; /* Setup the update mode and supervisor access mode */ reg &= ~(RTC_CR_UM_MASK | RTC_CR_SUP_MASK); reg |= RTC_CR_UM(config->updateMode) | RTC_CR_SUP(config->supervisorAccess); #if defined(FSL_FEATURE_RTC_HAS_WAKEUP_PIN_SELECTION) && FSL_FEATURE_RTC_HAS_WAKEUP_PIN_SELECTION /* Setup the wakeup pin select */ reg &= ~(RTC_CR_WPS_MASK); reg |= RTC_CR_WPS(config->wakeupSelect); #endif /* FSL_FEATURE_RTC_HAS_WAKEUP_PIN */ base->CR = reg; /* Configure the RTC time compensation register */ base->TCR = (RTC_TCR_CIR(config->compensationInterval) | RTC_TCR_TCR(config->compensationTime)); }
//=============================================================== //函数名称:hw_rtc_init //函数参数:SecendTimes:定时器秒寄存器的初始值 // AlarmTimes:定时器报警寄存器的时间间隔 //函数返回:无 //功能概要:RTC驱动初始化 //=============================================================== void hw_rtc_init(uint32 SecendTimes,uint32 AlarmTimes) { int i; SIM_SCGC6 |= SIM_SCGC6_RTC_MASK;//使能RTC时钟门控制 RTC_CR = RTC_CR_SWR_MASK;//软件复位RTC寄存器 RTC_CR &= ~RTC_CR_SWR_MASK;//软件复位之后清SWR位 RTC_CR |= RTC_CR_OSCE_MASK;//使能32.768kHz振荡器 for(i=0;i<0x600000;i++);//在使能32.768kHz振荡器之后,使能计数器 //之前等待一段时间,来稳定32.768kHz时钟。 RTC_TCR = RTC_TCR_CIR(0) | RTC_TCR_TCR(0);//配置定时器补偿寄存器的时间间隔与时钟周期数 RTC_TSR = SecendTimes;//初始化定时器秒寄存器 RTC_TAR = AlarmTimes;//初始化定时器报警寄存器 RTC_TPR = 0; //复位RTC定时器预分频器寄存器 }
/*! * @brief RTC初始化 * @since v5.0 * Sample usage: rtc_init(); //RTC初始化 */ void rtc_init(void) { volatile uint32 delay; SIM_SCGC6 |= SIM_SCGC6_RTC_MASK; //开启 RTC 时钟 RTC_CR = RTC_CR_SWR_MASK; //复位 RTC 寄存器(除 SWR, RTC_WAR , RTC_RAR ) RTC_CR &= ~RTC_CR_SWR_MASK; //清空复位标志位 RTC_CR = (0 | RTC_CR_OSCE_MASK //32.768 kHz 晶振 使能 //| RTC_CR_SC2P_MASK //加入 2p 电容 //| RTC_CR_SC4P_MASK //加入 4p 电容 //| RTC_CR_SC8P_MASK //加入 8p 电容 | RTC_CR_SC16P_MASK //加入 16p 电容 | RTC_CR_CLKO_MASK //RTC_CLKOUT 输出 32k 使能 (0表示输出,1表示禁止) ); delay = 0x600000; while(delay--); //等待 32K 晶振稳定(起振时间需要看晶振手册) //设置时间补偿 RTC_TCR = (0 | RTC_TCR_CIR(0) //补偿间隔(可以从1秒(0X0)到256(0xFF)的范围内),8bit | RTC_TCR_TCR(0) //补偿值的范围从 32*1024Hz -127 的周期到 32*1024Hz +128 周期,即 TCR 范围为 (int8)-127 ~ (int8)128 ); RTC_SR &= RTC_SR_TCE_MASK; //禁用RTC 计数器,便于后续设置寄存器 TSR 和 TPR //时间和闹钟设置 RTC_TSR = 0; //当前时间 RTC_TAR = 0; //闹钟时间 //中断配置 RTC_IER = (0 //| RTC_IER_TAIE_MASK //闹钟中断使能(0表示禁止,1表示使能) //| RTC_IER_TOIE_MASK //溢出中断使能(0表示禁止,1表示使能) //| RTC_IER_TIIE_MASK //无效时间中断使能(0表示禁止,1表示使能) ); RTC_SR |= RTC_SR_TCE_MASK; //使能RTC 计数器 }
/* ===================================================================*/ LDD_TDeviceData * RTC1_Init(LDD_TUserData *UserDataPtr, bool SoftInit) { RTC1_TDeviceData *DevDataPtr; /* Allocate RTC device structure */ /* {Default RTOS Adapter} Driver memory allocation: Dynamic allocation is simulated by a pointer to the static object */ DevDataPtr = &DevDataPtr__DEFAULT_RTOS_ALLOC; DevDataPtr->UserDataPtr = UserDataPtr; /* Store the user data */ /* Allocate interrupt vector(s) */ /* {Default RTOS Adapter} Set interrupt vector: IVT is static, ISR parameter is passed by the global variable */ INT_RTC__DEFAULT_RTOS_ISRPARAM = DevDataPtr; /* Registration of the device structure */ PE_LDD_RegisterDeviceStructure(PE_LDD_COMPONENT_RTC1_ID,DevDataPtr); /* Enable module clock */ /* SIM_SCGC6: RTC=1 */ SIM_SCGC6 |= SIM_SCGC6_RTC_MASK; if (!SoftInit) { RTC_SR = 0x00U; /* Disable counter */ RTC_TPR = RTC_TPR_TPR(0x00); /* Set prescaler register */ RTC_TSR = RTC_TSR_TSR(0x02); /* Set second register - 2000-01-01 0:0:1 */ RTC_TAR = RTC_TAR_TAR(0x00); /* Set alarm register - 2000-01-01 0:0:0 */ RTC_TCR = RTC_TCR_CIC(0x00) | RTC_TCR_TCV(0x00) | RTC_TCR_CIR(0x00) | RTC_TCR_TCR(0x00); /* Set compensation */ RTC_SR = RTC_SR_TCE_MASK; /* Enable counter */ } /* Enable interrupt */ /* RTC_IER: TAIE=1,TOIE=0,TIIE=0 */ RTC_IER = RTC_IER_TAIE_MASK; /* Enable interrupts */ /* NVICIP46: PRI46=0x70 */ NVICIP46 = NVIC_IP_PRI46(0x70); /* NVICISER1: SETENA|=0x4000 */ NVICISER1 |= NVIC_ISER_SETENA(0x4000); return DevDataPtr; }
void rtc_init(uint32 seconds, uint32 alarm, uint8 c_interval, uint8 c_value, uint8 interrupt) { int i; /*enable the clock to SRTC module register space*/ SIM_SCGC6 |= SIM_SCGC6_RTC_MASK; SIM_SOPT1 = SIM_SOPT1_OSC32KSEL(0); /*Only VBAT_POR has an effect on the SRTC, RESET to the part does not, so you must manually reset the SRTC to make sure everything is in a known state*/ /*clear the software reset bit*/ // printf("Generating SoftWare reset to SRTC\n"); disable_irq(interrupt); disable_irq(interrupt+1); RTC_CR = RTC_CR_SWR_MASK; RTC_CR &= ~RTC_CR_SWR_MASK; if (RTC_SR & RTC_SR_TIF_MASK) { RTC_TSR = 0x00000000; // this action clears the TIF // printf("RTC Invalid flag was set - Write to TSR done to clears RTC_SR = %#02X \n", (RTC_SR) ) ; } /*Set time compensation parameters*/ RTC_TCR = RTC_TCR_CIR(c_interval) | RTC_TCR_TCR(c_value); /*Enable the counter*/ if (seconds >0) { /*Enable the interrupt*/ if(interrupt >1) { enable_irq(interrupt+1); } RTC_IER |= RTC_IER_TSIE_MASK; RTC_SR |= RTC_SR_TCE_MASK; /*Configure the timer seconds and alarm registers*/ RTC_TSR = seconds; } else { RTC_IER &= ~RTC_IER_TSIE_MASK; } if (alarm >0) { RTC_IER |= RTC_IER_TAIE_MASK; RTC_SR |= RTC_SR_TCE_MASK; /*Configure the timer seconds and alarm registers*/ RTC_TAR = alarm; /*Enable the interrupt*/ if(interrupt >1) { enable_irq(interrupt); } } else { RTC_IER &= ~RTC_IER_TAIE_MASK; } /*Enable the oscillator*/ RTC_CR |= RTC_CR_OSCE_MASK|RTC_CR_SC16P_MASK; /*Wait to all the 32 kHz to stabilize, refer to the crystal startup time in the crystal datasheet*/ for(i=0;i<0x600000;i++); RTC_SR |= RTC_SR_TCE_MASK; //rtc_reg_report(); }