/********************************* **函数名:SetRTCTime **功能:设置时间,除了把Real_Time的值改变外,还要把时分秒转换为RTC计数值,年月日存到后备寄存器上 **注意事项:函数内会自动根据年月日计算星期,并且返回到*time上 **********************************/ void SetRTCTime(T_STRUCT* time) { u32 count; RTC_ITConfig(RTC_IT_SEC, DISABLE); //关闭秒中断 RTC_WaitForLastTask(); //付时间值到Real_Time上 Real_Time.year=time->year; Real_Time.month=time->month; Real_Time.day=time->day; Real_Time.hour=time->hour; Real_Time.minute=time->minute; Real_Time.sec=time->sec; //计算星期 time->date=Real_Time.date=GetDate(time); //把新的年月日存到掉电寄存器上 BKP_WriteBackupRegister(BKP_TIME_DATE,Real_Time.date); // RTC_WaitForLastTask(); BKP_WriteBackupRegister(BKP_TIME_DAY,Real_Time.day); // RTC_WaitForLastTask(); BKP_WriteBackupRegister(BKP_TIME_MONTH,Real_Time.month); // RTC_WaitForLastTask(); BKP_WriteBackupRegister(BKP_TIME_YEAR,Real_Time.year); // RTC_WaitForLastTask(); //计算新的RTC count值 count=Real_Time.hour*3600+Real_Time.minute*60+Real_Time.sec; RTC_WaitForLastTask(); RTC_SetCounter(count); RTC_WaitForLastTask(); RTC_ITConfig(RTC_IT_SEC, ENABLE); //打开秒中断 }
/*************************************************************************** Declaration : void sleep(char wdt_prescaler) Description : Sleep until WDT interrupt controlled by wdt_prescaler ***************************************************************************/ void sleep(int data_ms, char mode) { /* Enable the RTC Alarm interrupt */ RTC_ITConfig(RTC_IT_ALR, ENABLE); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Alarm in data_ms */ // RTC_SetAlarm(RTC_GetCounter()+ data_ms<<5); RTC_SetAlarm(RTC_GetCounter()+ data_ms); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Request to enter STOP mode with regulator ON */ PWR_EnterSTOPMode(PWR_Regulator_ON, PWR_STOPEntry_WFI); /* At this stage the system has resumed from STOP mode -------------------*/ /* Configures system clock after wake-up from STOP: enable HSE, PLL and select PLL as system clock source (HSE and PLL are disabled in STOP mode) */ /* Enable the RTC Alarm interrupt */ SYSCLKConfig_STOP(); RTC_ITConfig(RTC_IT_ALR, DISABLE); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); }
void radiotimer_schedule(uint16_t offset) { RTC_ITConfig(RTC_IT_ALR, DISABLE); //need to disable radio also in case that a radio interrupt is happening // Set the RTC alarm(RTC timer will alarm at next state of slot) RTC_SetAlarm(offset); RTC_WaitForLastTask(); //set radiotimer irpstatus radiotimer_vars.overflowORcompare = RADIOTIMER_COMPARE; RTC_ClearFlag(RTC_IT_ALR); RTC_ITConfig(RTC_IT_ALR, ENABLE); }
void radiotimer_cancel() { RTC_ITConfig(RTC_IT_ALR, DISABLE); //need to disable radio also in case that a radio interrupt is happening // set RTC alarm (slotlength) RTC_SetAlarm(radiotimer_vars.currentSlotPeriod); RTC_WaitForLastTask(); //set radiotimer irpstatus radiotimer_vars.overflowORcompare = RADIOTIMER_OVERFLOW; RTC_ClearFlag(RTC_IT_ALR); RTC_ITConfig(RTC_IT_ALR, ENABLE); }
/** * @brief Configures the RTC. * @param None * @retval : None */ void RTC_Configuration(void) { /* Enable PWR and BKP clocks */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); /* Allow access to BKP Domain */ PWR_BackupAccessCmd(ENABLE); /* Reset Backup Domain */ BKP_DeInit(); /* Enable LSE */ RCC_LSEConfig(RCC_LSE_ON); /* Wait till LSE is ready */ while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) {} /* Select LSE as RTC Clock Source */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); /* Enable RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* Wait for RTC registers synchronization */ RTC_WaitForSynchro(); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Enable the RTC Second */ RTC_ITConfig(RTC_IT_SEC, ENABLE); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Set RTC prescaler: set RTC period to 1sec */ RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */ /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); }
void HAL_RTC_Set_UnixAlarm(time_t value) { RTC_AlarmTypeDef RTC_AlarmStructure; time_t alarm_time = HAL_RTC_Get_UnixTime() + value; struct tm *alarm_time_tm; alarm_time_tm = localtime(&alarm_time); /* Disable the Alarm A */ RTC_AlarmCmd(RTC_Alarm_A, DISABLE); RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours = alarm_time_tm->tm_hour; RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes = alarm_time_tm->tm_min; RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds = alarm_time_tm->tm_sec; RTC_AlarmStructure.RTC_AlarmDateWeekDay = alarm_time_tm->tm_mday; RTC_AlarmStructure.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date; RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_None; /* Configure the RTC Alarm A register */ RTC_SetAlarm(RTC_Format_BIN, RTC_Alarm_A, &RTC_AlarmStructure); /* Enable the RTC Alarm A Interrupt */ RTC_ITConfig(RTC_IT_ALRA, ENABLE); /* Enable the Alarm A */ RTC_AlarmCmd(RTC_Alarm_A, ENABLE); /* Clear RTC Alarm Flag */ RTC_ClearFlag(RTC_FLAG_ALRAF); }
/** * @file RTC_Configuration * @brief Configures the RTC. * @param 无 * @retval 无 */ static void RTC_Configuration(void) { //启用PWR和BKP的时钟(from APB1) RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); //后备域解锁 PWR_BackupAccessCmd(ENABLE); //备份寄存器模块复位 BKP_DeInit(); //外部32.768K时钟使能 RCC_LSEConfig(RCC_LSE_ON); //等待稳定 while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) {} //RTC时钟源配置成LSE(外部32.768K) RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); //RTC开启 RCC_RTCCLKCmd(ENABLE); //开启后需要等待APB1时钟与RTC时钟同步,才能读写寄存器 RTC_WaitForSynchro(); //读写寄存器前,要确定上一个操作已经结束 RTC_WaitForLastTask(); //使能秒中断 RTC_ITConfig(RTC_IT_SEC, ENABLE); //等待寄存器写入完成 RTC_WaitForLastTask(); //设置RTC分频器,使RTC时钟为1Hz //RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) RTC_SetPrescaler(32767); //等待写入完成 RTC_WaitForLastTask(); }
/** * @brief Configures RTC clock source and prescaler. * @param None * @retval None */ void RTC_Configuration(void) { /* RTC clock source configuration ----------------------------------------*/ /* Allow access to BKP Domain */ PWR_BackupAccessCmd(ENABLE); /* Reset Backup Domain */ BKP_DeInit(); /* Enable LSE OSC */ RCC_LSICmd(ENABLE); /* Wait till LSE is ready */ while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) { } /* Select the RTC Clock Source */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); /* Enable the RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* RTC configuration -----------------------------------------------------*/ /* Wait for RTC APB registers synchronisation */ RTC_WaitForSynchro(); /* Set the RTC time base to 1s */ RTC_SetPrescaler(32767); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); RTC_ITConfig(RTC_IT_ALR, ENABLE); RTC_WaitForLastTask(); }
void rtc_InitWakeUpInterrupt(){ NVIC_InitTypeDef NVIC_InitStructure; EXTI_InitTypeDef EXTI_InitStructure; // NVIC init NVIC_InitStructure.NVIC_IRQChannel = RTC_WKUP_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); // ext Interrupt 22 einstellen (fuer WakeUp) EXTI_ClearITPendingBit(EXTI_Line22); EXTI_InitStructure.EXTI_Line = EXTI_Line22; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); // zum einstellen muss Wakup disabled sein RTC_WakeUpCmd(DISABLE); // Teiler 16 => 32,768kHz:16 => 2048 Hz RTC_WakeUpClockConfig(RTC_WakeUpClock_RTCCLK_Div16); // WakeUp Counter einstellen RTC_SetWakeUpCounter(16); //set to 16 -> 128 interrupts per second (32768Hz / Div16 = 2048; 2948 / 128 = 16 // enable Interrupt RTC_ITConfig(RTC_IT_WUT, ENABLE); // enable Wakeup RTC_WakeUpCmd(ENABLE); }
/*设置系统时间*/ void SysSetTime(FUN_BLOCK **funBlock) { DIS_SING *sing_temp; GetSubFunBlock(&pfunSubBlock); sing_temp = (DIS_SING *)(pfunSubBlock->current_display); pfunSubBlock->data_type = ZINUM;//显示属性 RTC_ITConfig(RTC_IT_SEC, DISABLE); //关RTC中断,防止systmtime被修改 strcpy(sing_temp->y2 , "请输入时间"); /*取得VIRTUAL_NUM_LENGTH位数的箱门数*/ if(CANCEL == KeyDisplay(pfunSubBlock , 10)) { FreeMem(); return; } /*读取时间值*/ systmtime.tm_year = 2000 + (sing_temp->y3[0]-'0')*10 + sing_temp->y3[1]-'0'; systmtime.tm_mon = (sing_temp->y3[2]-'0')*10 + sing_temp->y3[3]-'0'; systmtime.tm_mday = (sing_temp->y3[4]-'0')*10 + sing_temp->y3[5]-'0'; systmtime.tm_hour = (sing_temp->y3[6]-'0')*10 + sing_temp->y3[7]-'0'; systmtime.tm_min = (sing_temp->y3[8]-'0')*10 + sing_temp->y3[9]-'0'; memset(sing_temp->y3 , 0 , 10); /*写入时间*/ RTC_Configuration(); Time_Adjust(&systmtime);//写入系统时间 BKP_WriteBackupRegister(BKP_DR1, 0xA5A5); strcpy(sing_temp->y2 , "系统时间修改成功"); DisplaySubBlock(pfunSubBlock); FreeMem(); }
/** * @brief Enters STANDBY mode, RTC Alarm within 3 second or an external RESET * will wake-up the system from STANDBY * @param None * @retval None */ static void EnterSTANDBYMode(void) { RTC_AlarmTypeDef RTC_AlarmStructure; RTC_TimeTypeDef RTC_TimeStructure; /* Disable the Alarm A */ RTC_AlarmCmd(RTC_Alarm_A, DISABLE); /* Get the current time */ RTC_GetTime(RTC_Format_BIN, &RTC_TimeStructure); /* Set the alarm to current time + 3s */ RTC_AlarmStructure.RTC_AlarmTime.RTC_H12 = RTC_TimeStructure.RTC_H12; RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours = RTC_TimeStructure.RTC_Hours; RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes = RTC_TimeStructure.RTC_Minutes; RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds = (RTC_TimeStructure.RTC_Seconds + 0x3) % 60; RTC_AlarmStructure.RTC_AlarmDateWeekDay = 31; RTC_AlarmStructure.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date; RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_DateWeekDay | RTC_AlarmMask_Hours | RTC_AlarmMask_Minutes; RTC_SetAlarm(RTC_Format_BIN, RTC_Alarm_A, &RTC_AlarmStructure); /* Enable RTC Alarm A Interrupt: this Interrupt will wake-up the system from STANDBY mode (RTC Alarm IT not enabled in NVIC) */ RTC_ITConfig(RTC_IT_ALRA, ENABLE); /* Enable the Alarm A */ RTC_AlarmCmd(RTC_Alarm_A, ENABLE); /* Clear RTC Alarm Flag */ RTC_ClearFlag(RTC_FLAG_ALRAF); /* Request to enter STANDBY mode (Wake Up flag is cleared in PWR_EnterSTANDBYMode function) */ PWR_EnterSTANDBYMode(); }
/** * @brief Configures the RTC Alarm. * @param None * @retval None */ void RTC_AlarmConfig(void) { EXTI_InitTypeDef EXTI_InitStructure; RTC_AlarmTypeDef RTC_AlarmStructure; NVIC_InitTypeDef NVIC_InitStructure; /* EXTI configuration */ EXTI_ClearITPendingBit(EXTI_Line17); EXTI_InitStructure.EXTI_Line = EXTI_Line17; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); /* Enable the RTC Alarm Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = RTC_Alarm_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Set the alarmA Masks */ RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_All; RTC_SetAlarm(RTC_Format_BIN, RTC_Alarm_A, &RTC_AlarmStructure); /* Set AlarmA subseconds and enable SubSec Alarm : generate 8 interripts per Second */ RTC_AlarmSubSecondConfig(RTC_Alarm_A, 0xFF, RTC_AlarmSubSecondMask_SS14_5); /* Enable AlarmA interrupt */ RTC_ITConfig(RTC_IT_ALRA, ENABLE); /* Enable the alarmA */ RTC_AlarmCmd(RTC_Alarm_A, DISABLE); }
static void RtcSetAlarmConfig( void ) { EXTI_InitTypeDef EXTI_InitStructure; RTC_AlarmTypeDef RTC_AlarmStructure; NVIC_InitTypeDef NVIC_InitStructure; /* EXTI configuration */ EXTI_ClearITPendingBit( EXTI_Line17 ); EXTI_InitStructure.EXTI_Line = EXTI_Line17; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init( &EXTI_InitStructure ); /* Enable the RTC Alarm Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = RTC_Alarm_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init( &NVIC_InitStructure ); /* Set the alarmA Masks */ RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_DateWeekDay; RTC_SetAlarm( RTC_Format_BIN, RTC_Alarm_A, &RTC_AlarmStructure ); /* Enable AlarmA interrupt */ RTC_ITConfig( RTC_IT_ALRA, DISABLE ); /* Enable the alarmA */ RTC_AlarmCmd( RTC_Alarm_A, DISABLE ); }
void TM_RTC_DisableAlarm(TM_RTC_Alarm_t Alarm) { switch (Alarm) { case TM_RTC_Alarm_A: /* Disable Alarm A */ RTC_AlarmCmd(RTC_Alarm_A, DISABLE); /* Disable Alarm A interrupt */ RTC_ITConfig(RTC_IT_ALRA, DISABLE); /* Clear Alarm A pending bit */ RTC_ClearFlag(RTC_IT_ALRA); break; case TM_RTC_Alarm_B: /* Disable Alarm B */ RTC_AlarmCmd(RTC_Alarm_B, DISABLE); /* Disable Alarm B interrupt */ RTC_ITConfig(RTC_IT_ALRB, DISABLE); /* Clear Alarm B pending bit */ RTC_ClearFlag(RTC_IT_ALRB); break; default: break; } /* Clear RTC Alarm pending bit */ EXTI_ClearITPendingBit(EXTI_Line17); /* Configure EXTI 17 as interrupt */ EXTI_InitStruct.EXTI_Line = EXTI_Line17; EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStruct.EXTI_LineCmd = ENABLE; /* Initialite Alarm EXTI interrupt */ EXTI_Init(&EXTI_InitStruct); /* Configure the RTC Alarm Interrupt */ NVIC_InitStruct.NVIC_IRQChannel = RTC_Alarm_IRQn; NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = RTC_PRIORITY; NVIC_InitStruct.NVIC_IRQChannelSubPriority = RTC_ALARM_SUBPRIORITY; NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; /* Initialize RTC Alarm Interrupt */ NVIC_Init(&NVIC_InitStruct); }
// 配置RTC硬件。 void RTC_Configure(void) { /* Enable PWR and BKP clocks */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); /* Allow access to BKP Domain */ PWR_BackupAccessCmd(ENABLE); RCC_RTCCLKCmd(ENABLE); /* Reset Backup Domain */ BKP_DeInit(); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); #ifdef RTCClockSource_LSI /* Enable LSI */ RCC_LSICmd(ENABLE); /* Wait till LSI is ready */ while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET); /* Select LSI as RTC Clock Source */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); #elif defined RTCClockSource_LSE /* Enable LSE */ RCC_LSEConfig(RCC_LSE_ON); /* Wait till LSE is ready */ while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET); /* Select LSE as RTC Clock Source */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); #endif // 上电后需要校准RTC时钟,也即准确计算出RTC的周期时长。 RTC_Calibrate(); #ifdef RTCClockOutput_Enable /* Disable the Tamper Pin */ BKP_TamperPinCmd(DISABLE); /* To output RTCCLK/64 on Tamper pin, the tamper functionality must be disabled */ /* Enable RTC Clock Output on Tamper Pin */ BKP_RTCCalibrationClockOutputCmd(ENABLE); #endif /*允许RTC报警中断*/ RTC_ITConfig(RTC_IT_ALR, ENABLE); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); }
/** * @file RTC_Init * @brief RTC Initialization * @param 无 * @retval 无 */ void RTC_Init(void) { if (BKP_ReadBackupRegister(BKP_DR1)!= 0xA5A5) { /* Backup data register value is not correct or not yet programmed (when the first time the program is executed) */ printf("RTC not yet configured....\r\n"); /* RTC Configuration */ RTC_Configuration(); Time_Regulate(); /* Adjust time by values entred by the user on the hyperterminal */ printf("RTC configured....\r\n"); BKP_WriteBackupRegister(BKP_DR1, 0xA5A5); } else { /* Check if the Power On Reset flag is set */ if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET) { printf("Power On Reset occurred....\r\n"); } /* Check if the Pin Reset flag is set */ else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET) { printf("External Reset occurred....\r\n"); } printf("No need to configure RTC....\r\n"); /* Wait for RTC registers synchronization */ RTC_WaitForSynchro(); /* Enable the RTC Second */ RTC_ITConfig(RTC_IT_SEC, ENABLE); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); } /* NVIC configuration */ NVIC_Configuration(); #ifdef RTCClockOutput_Enable /* Enable PWR and BKP clocks */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); /* Allow access to BKP Domain */ PWR_BackupAccessCmd(ENABLE); /* Disable the Tamper Pin */ BKP_TamperPinCmd(DISABLE); /* To output RTCCLK/64 on Tamper pin, the tamper functionality must be disabled */ /* Enable RTC Clock Output on Tamper Pin */ BKP_RTCOutputConfig(BKP_RTCOutputSource_CalibClock); #endif /* Clear reset flags */ RCC_ClearFlag(); return; }
void radiotimer_setPeriod(uint16_t period) { RTC_ITConfig(RTC_IT_ALR, DISABLE); //need to disable radio also in case that a radio interrupt is happening when set Alarm value //Reset RTC Counter to begin a new slot RTC_SetAlarm(period); RTC_WaitForLastTask(); radiotimer_vars.currentSlotPeriod = period; //set radiotimer irpstatus radiotimer_vars.overflowORcompare = RADIOTIMER_OVERFLOW; RTC_ClearFlag(RTC_IT_ALR); RTC_ITConfig(RTC_IT_ALR, ENABLE); }
uint08 RTC_Init(void) { //检查是不是第一次配置时钟 uint08 temp=0; if ( BKP_ReadBackupRegister(BKP_DR1) != 0x5050 ) //从指定的后备寄存器中读出数据:读出了与写入的指定数据不相乎 { RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); //使能PWR和BKP外设时钟 PWR_BackupAccessCmd(ENABLE); //使能后备寄存器访问 BKP_DeInit(); //复位备份区域 RCC_LSEConfig(RCC_LSE_ON); //设置外部低速晶振(LSE),使用外设低速晶振 while ( RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET && temp< 252) //检查指定的RCC标志位设置与否,等待低速晶振就绪 { temp++; //delay_ms(10); //OSTimeDlyHMSM(0, 0, 0, 10); } if ( temp>=250 ) { return 1;//初始化时钟失败,晶振有问题 } RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); //设置RTC时钟(RTCCLK),选择LSE作为RTC时钟 RCC_RTCCLKCmd(ENABLE); //使能RTC时钟 RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成 RTC_WaitForSynchro(); //等待RTC寄存器同步 RTC_ITConfig(RTC_IT_SEC, ENABLE); //使能RTC秒中断 RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成 RTC_EnterConfigMode();/// 允许配置 RTC_SetPrescaler(32767); //设置RTC预分频的值 RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成 RTC_Set(2009,12,2,10,0,55); //设置时间 RTC_ExitConfigMode(); //退出配置模式 BKP_WriteBackupRegister(BKP_DR1, 0X5050); //向指定的后备寄存器中写入用户程序数据 } else//系统继续计时 { RTC_WaitForSynchro(); //等待最近一次对RTC寄存器的写操作完成 RTC_ITConfig(RTC_IT_SEC, ENABLE); //使能RTC秒中断 RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成 } RTC_NVIC_Config();//RCT中断分组设置 RTC_Get();//更新时间 return 0; //ok }
uint8_t RTC_AutoTest(void) { uint8_t i = 0; /* Enable PWR and BKP clocks */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); /* Allow access to BKP Domain */ PWR_BackupAccessCmd(ENABLE); /* Reset Backup Domain */ BKP_DeInit(); /* Enable LSE */ RCC_LSEConfig(RCC_LSE_ON); /* Wait till LSE is ready */ while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) { Delay_us(50000); i++; if(i>100) //5秒都还没起振,坏了 { return 0; } } /* Select LSE as RTC Clock Source */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); /* Enable RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* Wait for RTC registers synchronization * 因为RTC时钟是低速的,内环时钟是高速的,所以要同步 */ RTC_WaitForSynchro(); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Enable the RTC Second */ RTC_ITConfig(RTC_IT_SEC, ENABLE); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Set RTC prescaler: set RTC period to 1sec */ RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) = 1HZ */ /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); return 1; }
//------------------------------------------ void TM_RTC_Interrupts(uint32_t int_value) { /* Clear pending bit */ EXTI->PR = 0x00400000; /* Disable wakeup interrupt */ RTC_WakeUpCmd(DISABLE); /* Disable RTC interrupt flag */ RTC_ITConfig(RTC_IT_WUT, DISABLE); /* NVIC init for RTC */ NVIC_InitStruct.NVIC_IRQChannel = RTC_WKUP_IRQn; NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = RTC_PRIORITY; NVIC_InitStruct.NVIC_IRQChannelSubPriority = RTC_WAKEUP_SUBPRIORITY; NVIC_InitStruct.NVIC_IRQChannelCmd = DISABLE; NVIC_Init(&NVIC_InitStruct); /* RTC connected to EXTI_Line22 */ EXTI_InitStruct.EXTI_Line = EXTI_Line22; EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStruct.EXTI_LineCmd = DISABLE; EXTI_Init(&EXTI_InitStruct); if (int_value != TM_RTC_Int_Disable) { /* Enable NVIC */ NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStruct); /* Enable EXT1 interrupt */ EXTI_InitStruct.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStruct); /* First disable wake up command */ RTC_WakeUpCmd(DISABLE); /* Clock divided by 8, 32768 / 8 = 4096 */ /* 4096 ticks for 1second interrupt */ RTC_WakeUpClockConfig(RTC_WakeUpClock_RTCCLK_Div8); /* Set RTC wakeup counter */ RTC_SetWakeUpCounter(0x0FFF * int_value); /* Enable wakeup interrupt */ RTC_ITConfig(RTC_IT_WUT, ENABLE); /* Enable wakeup command */ RTC_WakeUpCmd(ENABLE); } }
void rtc_timer_start(u32 alarmValue) { //enable BKP and PWR, Clock RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP|RCC_APB1Periph_PWR , ENABLE); // RTC clock source configuration PWR_BackupAccessCmd(ENABLE); //Allow access to BKP Domain RCC_LSEConfig(RCC_LSE_ON); //Enable LSE OSC while(RCC_GetFlagStatus(RCC_FLAG_LSERDY)==RESET); //Wait till LSE is ready RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); //Select the RTC Clock Source RCC_RTCCLKCmd(ENABLE); //enable RTC // RTC configuration // Wait for RTC APB registers synchronisation RTC_WaitForSynchro(); RTC_SetPrescaler(0); //Set the RTC time base to 30.5us RTC_WaitForLastTask(); //Wait until last write operation on RTC registers has finished //Set the RTC time counter to 0 RTC_SetCounter(0); RTC_WaitForLastTask(); // Set the RTC time alarm(the length of slot) RTC_SetAlarm(alarmValue); RTC_WaitForLastTask(); //interrupt when reach alarm value RTC_ClearFlag(RTC_IT_ALR); RTC_ITConfig(RTC_IT_ALR, ENABLE); //Configures EXTI line 17 to generate an interrupt on rising edge(alarm interrupt to wakeup board) EXTI_ClearITPendingBit(EXTI_Line17); EXTI_InitTypeDef EXTI_InitStructure; EXTI_InitStructure.EXTI_Line = EXTI_Line17; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); //Configure RTC global interrupt: //Configure NVIC: Preemption Priority = 1 and Sub Priority = 1 NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); //Configure RTC Alarm interrupt: //Configure NVIC: Preemption Priority = 0 and Sub Priority = 1 NVIC_InitStructure.NVIC_IRQChannel = RTCAlarm_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }
void SetRtcTime(void) { /* Enable PWR and BKP clocks */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); /* Allow access to BKP Domain */ PWR_BackupAccessCmd(ENABLE); /* Reset Backup Domain */ BKP_DeInit(); #ifdef RTC_LSE /* Enable LSE */ RCC_LSEConfig(RCC_LSE_ON); /* Wait till LSE is ready */ while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) {} /* Select LSE as RTC Clock Source */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); #else /* Enable the LSI OSC */ RCC_LSICmd(ENABLE); /* Wait till LSI is ready */ while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) {} /* Select the RTC Clock Source */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); #endif /* Enable RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* Wait for RTC registers synchronization */ RTC_WaitForSynchro(); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Enable the RTC Second */ RTC_ITConfig(RTC_IT_SEC, ENABLE); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); printf("\r\n RTC configured...."); /* Adjust time by values entred by the user on the hyperterminal */ // Time_Adjust(); Time_SetCalendarTime(time_set); BKP_WriteBackupRegister(BKP_DR1, 0xA5A5); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); }
/** * @brief RTC Tamper Configuration. * @param None * @retval None */ void Tamper_Init(void) { RTC_TamperFilterConfig((RTC_TamperFilter_TypeDef)RTC_TampFilter[TampFilterIndex]); RTC_TamperSamplingFreqConfig(RTC_TamperSamplingFreq_RTCCLK_Div32768); RTC_TamperPinsPrechargeDuration(RTC_TamperPrechargeDuration_None); RTC_TamperLevelConfig(RTC_Tamper_1, RTC_TamperLevel_Low ); RTC_TamperCmd(RTC_Tamper_1, ENABLE); RTC_ITConfig(RTC_IT_TAMP, ENABLE); }
/** * @brief This function handles External lines 9 to 5 interrupt request. * @param None * @retval None */ void EXTI15_10_IRQHandler(void) { if(EXTI_GetITStatus(KEY_BUTTON_EXTI_LINE) != RESET) { /* Clear the SEL Button EXTI line pending bit */ EXTI_ClearITPendingBit(KEY_BUTTON_EXTI_LINE); /* Disable the Alarm A */ RTC_AlarmCmd(RTC_Alarm_A, DISABLE); /* Disable RTC Alarm A Interrupt */ RTC_ITConfig(RTC_IT_ALRA, DISABLE); /* Clear Power WakeUp (CWUF) pending flag */ PWR_ClearFlag(PWR_FLAG_WU); RTC_GetTime(RTC_Format_BIN, &RTC_TimeStructure); /* Set the alarm to current time + 5s */ RTC_AlarmStructure.RTC_AlarmTime.RTC_H12 = RTC_TimeStructure.RTC_H12; RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours = RTC_TimeStructure.RTC_Hours; RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes = RTC_TimeStructure.RTC_Minutes; RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds = (RTC_TimeStructure.RTC_Seconds + 0x5) % 60; RTC_AlarmStructure.RTC_AlarmDateWeekDay = 0x31; RTC_AlarmStructure.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date; RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_DateWeekDay | RTC_AlarmMask_Hours | RTC_AlarmMask_Minutes; RTC_SetAlarm(RTC_Format_BIN, RTC_Alarm_A, &RTC_AlarmStructure); /* Enable RTC Alarm A Interrupt: this Interrupt will wake-up the system from STANDBY mode (RTC Alarm IT not enabled in NVIC) */ RTC_ITConfig(RTC_IT_ALRA, ENABLE); /* Enable the Alarm A */ RTC_AlarmCmd(RTC_Alarm_A, ENABLE); /* Clear RTC Alarm Flag */ RTC_ClearFlag(RTC_FLAG_ALRAF); /* Request to enter STANDBY mode (Wake Up flag is cleared in PWR_EnterSTANDBYMode function) */ PWR_EnterSTANDBYMode(); } }
void sleep_mode(FunctionalState sleep) { if(Settings.Sleep_time>0 && !Power.USB_active) { Power.Pump_deny=ENABLE; if(Power.Pump_active)Pump_now(DISABLE); set_msi(); if(sleep) { RTC_ITConfig(RTC_IT_WUT, DISABLE); Power.led_sleep_time=0; GPIO_SetBits(GPIOC,GPIO_Pin_13);// Выключаем подсветку display_off(); // выключить дисплей GPIO_ResetBits(GPIOA,GPIO_Pin_7);// Фиксируем режим 1.8 вольта, с низким потреблением ножки delay_ms(1000); // подождать установки напряжения DataUpdate.Need_batt_voltage_update=ENABLE; // разрешить работу АЦП adc_check_event(); // запустить преобразование delay_ms(100); // подождать установки напряжения PWR_FastWakeUpCmd(DISABLE); PWR_UltraLowPowerCmd(ENABLE); PWR_PVDCmd(DISABLE); RTC_ITConfig(RTC_IT_WUT, ENABLE); } else { RTC_ITConfig(RTC_IT_WUT, DISABLE); GPIO_SetBits(GPIOA,GPIO_Pin_7);// Переключаем в режим 3 вольта delay_ms(400); // подождать установки напряжения display_on(); // включить дисплей DataUpdate.Need_batt_voltage_update=ENABLE; // разрешить работу АЦП DataUpdate.Need_display_update=ENABLE; adc_check_event(); // запустить преобразование RTC_ITConfig(RTC_IT_WUT, ENABLE); sound_deactivate(); } Power.Pump_deny=DISABLE; } }
void platform_mcu_enter_standby(uint32_t secondsToWakeup) { platform_rtc_time_t time; uint32_t currentSecond; RTC_AlarmTypeDef RTC_AlarmStructure; PWR_WakeUpPinCmd(ENABLE); if(secondsToWakeup == MICO_WAIT_FOREVER) PWR_EnterSTANDBYMode(); platform_log("Wake up in %d seconds", secondsToWakeup); platform_rtc_get_time(&time); currentSecond = time.hr*3600 + time.min*60 + time.sec; currentSecond += secondsToWakeup; RTC_AlarmStructure.RTC_AlarmTime.RTC_H12 = RTC_HourFormat_24; RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours = currentSecond/3600%24; RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes = currentSecond/60%60; RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds = currentSecond%60; RTC_AlarmStructure.RTC_AlarmDateWeekDay = 0x31; RTC_AlarmStructure.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date; RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_DateWeekDay ; RTC_AlarmCmd(RTC_Alarm_A, DISABLE); /* Disable the Alarm A */ RTC_ITConfig(RTC_IT_ALRA, DISABLE); /* Clear RTC Alarm Flag */ RTC_ClearFlag(RTC_FLAG_ALRAF); RTC_SetAlarm(RTC_Format_BIN, RTC_Alarm_A, &RTC_AlarmStructure); /* Enable RTC Alarm A Interrupt: this Interrupt will wake-up the system from STANDBY mode (RTC Alarm IT not enabled in NVIC) */ RTC_ITConfig(RTC_IT_ALRA, ENABLE); /* Enable the Alarm A */ RTC_AlarmCmd(RTC_Alarm_A, ENABLE); PWR_EnterSTANDBYMode(); }
static void RtcClearStatus( void ) { /* Clear RTC Alarm Flag */ RTC_ClearFlag(RTC_FLAG_ALRAF); /* Enable RTC Alarm A Interrupt */ RTC_ITConfig( RTC_IT_ALRA, DISABLE ); /* Enable the Alarm A */ RTC_AlarmCmd( RTC_Alarm_A, DISABLE ); }
void RTC_Config(void) { /*后备寄存器1中,存了一个特殊字符0xA5A5 第一次上电或后备电源掉电后,该寄存器数据丢失, 表明RTC数据丢失,需要重新配置 */ if(BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5) //检查是否第一次上电或后备电池已经掉电, { Write_Log("Backup VBAT PowerDown or First time PowerUp,Initialize RTC\r\n"); RTC_Configuration(); BKP_WriteBackupRegister(BKP_DR1, 0xA5A5); time_now.tm_year = 2011; time_now.tm_mon = 10; //月份表示为0~11 time_now.tm_mday = 13; time_now.tm_hour = 13; time_now.tm_min = 16; time_now.tm_sec = 38; Time_SetCalendarTime(time_now);//设置初始时间 } else //若后备寄存器没有掉电,则无需重新配置RTC { Write_Log("Backup VBAT Keep, Don't RTC Configuralation\r\n"); //等待RTC与APB同步 RTC_WaitForSynchro(); RTC_WaitForLastTask(); //使能秒中断 RTC_ITConfig(RTC_IT_SEC, ENABLE); RTC_WaitForLastTask(); } //这里我们可以利用RCC_GetFlagStatus()函数查看本次复位类型 if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET) { por_rst_flag = 1; Write_Log("PowerUp Reset\r\n"); } else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET) { pin_rst_flag = 1; Write_Log("pin Reset\r\n"); } else if(PWR_GetFlagStatus(PWR_FLAG_WU)!= RESET) //wakeup唤醒 { Write_Log("WakeUp...\r\n"); } if(PWR_GetFlagStatus(PWR_FLAG_SB) != RESET) //检查是否由待机模式下唤醒,如是则不需要配置RTC /* System resumed from STANDBY mode */ /* Clear StandBy flag */ PWR_ClearFlag(PWR_FLAG_SB); //清除RCC中复位标志 RCC_ClearFlag(); return; }
static void rtcConfig(void) { /* Enable PWR and BKP clocks */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); /* Allow access to BKP Domain */ PWR_BackupAccessCmd(ENABLE); /* Reset Backup Domain */ BKP_DeInit(); /* Enable the LSI OSC */ RCC_LSICmd(ENABLE); /* Wait till LSI is ready */ while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET); /* Select the RTC Clock Source */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); /* Enable RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* Wait for RTC registers synchronization */ RTC_WaitForSynchro(); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Enable the RTC Second */ RTC_ITConfig(RTC_IT_SEC, ENABLE); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); #ifndef PRECISION_IN_MS /* Set RTC prescaler: set RTC period to 1 sec */ RTC_SetPrescaler(40000); //LSI default is 40k HZ #else /* Set RTC prescaler: set RTC period to 1ms */ RTC_SetPrescaler(40); //LSI default is 40k HZ #endif /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* To output second signal on Tamper pin, the tamper functionality * must be disabled (by default this functionality is disabled) */ //BKP_TamperPinCmd(DISABLE); /* Enable the RTC Second Output on Tamper Pin */ //BKP_RTCOutputConfig(BKP_RTCOutputSource_Second); }
static int rtc_standbyUntil( lua_State* L ) { RTC_AlarmTypeDef RTC_AlarmStructure; mico_rtos_suspend_all_thread(); RTC_AlarmStructure.RTC_AlarmTime.RTC_H12 = RTC_HourFormat_24; RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours = luaL_checkinteger( L, 1 ); RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes = luaL_checkinteger( L, 2 ); RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds = luaL_checkinteger( L, 3 ); RTC_AlarmStructure.RTC_AlarmDateWeekDay = 0x31; RTC_AlarmStructure.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date; RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_DateWeekDay ; RTC_AlarmCmd(RTC_Alarm_A, DISABLE); /* Disable the Alarm A */ RTC_ITConfig(RTC_IT_ALRA, DISABLE); /* Clear RTC Alarm Flag */ RTC_ClearFlag(RTC_FLAG_ALRAF); RTC_SetAlarm(RTC_Format_BIN, RTC_Alarm_A, &RTC_AlarmStructure); /* Enable RTC Alarm A Interrupt: this Interrupt will wake-up the system from STANDBY mode (RTC Alarm IT not enabled in NVIC) */ RTC_ITConfig(RTC_IT_ALRA, ENABLE); /* Enable the Alarm A */ RTC_AlarmCmd(RTC_Alarm_A, ENABLE); char buff[32]; sprintf(buff,"Wake up at %02d:%02d:%02d\r\n", RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours, RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes, RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds); l_message(NULL,buff); PWR_EnterSTANDBYMode(); mico_rtos_resume_all_thread(); lua_pushstring(L, "RETURN FROM STANDBY"); return 1; }