//RTC设置成内部或者外部时钟 void RTC_Config(rtc_clk rtcclk) { RTC_InitTypeDef RTC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); PWR_BackupAccessCmd(ENABLE); if(rtcclk == extrnal) { /* Enable the LSE OSC */ RCC_LSEConfig(RCC_LSE_ON); /* Wait till LSE is ready */ while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET); /* Select the RTC Clock Source */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); /* Enable the RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* Wait for RTC APB registers synchronisation */ RTC_WaitForSynchro(); } if(rtcclk == inner) { RCC_LSICmd(ENABLE); //开启内部低速 while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET); RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); RCC_RTCCLKCmd(ENABLE); RTC_WaitForSynchro(); } RTC_InitStructure.RTC_AsynchPrediv = 0x7F; RTC_InitStructure.RTC_SynchPrediv = 0xFF; RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; RTC_Init(&RTC_InitStructure); }
/** * @brief This function handles External line 10 to 15 interrupts request. * @param None * @retval None */ void EXTI15_10_IRQHandler(void) { if((EXTI_GetITStatus(KEY_BUTTON_EXTI_LINE) != RESET)) { /* Set the LCD Back Color */ LCD_SetBackColor(White); /* Enable the RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* Wait for RTC APB registers synchronisation */ RTC_WaitForSynchro(); /* Enable the alarmA */ RTC_AlarmCmd(RTC_Alarm_A, ENABLE); /* Clear the LEFT EXTI pending bit */ EXTI_ClearITPendingBit(KEY_BUTTON_EXTI_LINE); } else if((EXTI_GetITStatus(TAMPER_BUTTON_EXTI_LINE) != RESET)) { /* Disable the RTC Clock */ RCC_RTCCLKCmd(DISABLE); /* Wait for RTC APB registers synchronisation */ RTC_WaitForSynchro(); /* Clear the TAMPER EXTI pending bit */ EXTI_ClearITPendingBit(TAMPER_BUTTON_EXTI_LINE); } }
// 通过直接计算校准RTC时钟(使用0.01ms周期的systick测量再修正RTC预分频系数)。 void RTC_Calibrate(void) { unsigned int systick_sta; unsigned int rtctick_lmt; unsigned int prescaler = 0; // 在使用systick测量RTC tick的周期长度之前,初始化systick NVIC_SetPriority(SysTick_IRQn, 0); SysTick_Init(); // 开始rtcalarm计数并使能rtcalarm中断 SysTick_Enable(); // 先设置默认的RTC预分频系数(rtc tike = ~1ms) RTC_SetPrescaler(DEF_RTC_PRESCALER); /* 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(); // 测量RTC单周期长度持续~1秒 rtctick_lmt = systick+1000/SYSTICK_PERIOD; // 记录测量前的systick值 systick_sta = systick; // 测量1000个周期的rtc tick(~1s) while(systick < rtctick_lmt); prescaler = (DEF_RTC_PRESCALER * 1000)/((systick-systick_sta)/100); // printf("prescaler = %d.\r\n", prescaler); // 重新设置RTC预分频系数 RTC_SetPrescaler(prescaler); /* 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(); // 校准完RTC时钟后,停止rtcalarm计数并禁止rtcalarm中断 SysTick_Disable(); // printf("RTC Clock calibrated.\r\n"); }
void rtc_init() { RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); if(BKP->DR1 != BKP_MAGIC) { PWR_BackupAccessCmd(ENABLE); /* Allow write access to BKP Domain */ RCC_BackupResetCmd(ENABLE); /* Reset Backup Domain */ RCC_BackupResetCmd(DISABLE); RCC_LSEConfig(RCC_LSE_ON); /* Enable LSE */ while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET); /* Wait till LSE is ready */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); /* Select LSE as RTC Clock Source */ RCC_RTCCLKCmd(ENABLE); /* Enable RTC Clock */ RTC_WaitForSynchro(); /* Wait for RTC registers synchronization */ RTC_WaitForLastTask(); RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */ RTC_WaitForLastTask(); BKP->DR1 = BKP_MAGIC; PWR_BackupAccessCmd(DISABLE); /* Protect backup registers */ rtc_is_valid = 0; } else { /* Wait for RTC registers synchronization */ RTC_WaitForSynchro(); } }
unsigned int RTC_cfg(void) { unsigned int ret; RTC_DateTypeDef RTC_DateStruct; RTC_TimeTypeDef RTC_TimeStruct; ret = 0; if (RTC_ReadBackupRegister(RTC_BKP_DR1) != 0xA5A5) { ret = 1; /* Allow access to BKP Domain */ PWR_BackupAccessCmd(ENABLE); /* Reset Backup Domain */ RTC_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(); /* Adjust time */ RTC_DateStruct.RTC_Year = 13; RTC_DateStruct.RTC_Month = 05; RTC_DateStruct.RTC_Date = 29; RTC_DateStruct.RTC_WeekDay = 3; RTC_TimeStruct.RTC_Hours = 11; RTC_TimeStruct.RTC_Minutes = 0; RTC_TimeStruct.RTC_Seconds = 0; RTC_SetTime(RTC_Format_BIN, &RTC_TimeStruct); RTC_SetDate(RTC_Format_BIN, &RTC_DateStruct); RTC_WriteBackupRegister(RTC_BKP_DR1, 0xA5A5); } else { /* Allow access to BKP Domain */ PWR_BackupAccessCmd(ENABLE); /* Wait for RTC registers synchronization */ RTC_WaitForSynchro(); while (RTC_GetFlagStatus(RTC_FLAG_RSF) == RESET); } return ret; }
/** * @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_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(); // Select HSE/128 as RTC Clock Source RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div128); /* 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 1ms */ RTC_SetPrescaler(62); /*62499 RTC period = RTCCLK/RTC_PR = 8M / 128 = 62.5kHz ) -> (62499+1) */ /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); }
void rtc_init(void) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); // Enable PWR clock PWR_RTCAccessCmd(ENABLE); // Enable access to RTC // Note: the LSI is used as RTC source clock // The RTC Clock may vary due to LSI frequency dispersion. RCC_LSICmd(ENABLE); // Enable LSI while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) {} // Wait until ready RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); // Select LSI as RTC Clock Source RCC_RTCCLKCmd(ENABLE); // Enable RTC Clock RTC_WaitForSynchro(); // Wait for RTC registers synchronization uint32_t lsi_freq = 40000; // [TODO] To be measured precisely using a timer input capture RTC_InitTypeDef RTC_InitStructure; RTC_InitStructure.RTC_AsynchPrediv = 127; RTC_InitStructure.RTC_SynchPrediv = (lsi_freq / 128) - 1; RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; RTC_Init(&RTC_InitStructure); PWR_RTCAccessCmd(DISABLE); // Disable access to RTC rtc_inited = 1; }
/** * @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(); }
void rtc_init(){ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); PWR_BackupAccessCmd(ENABLE); //needed? /* use LSE-clock X3=32768 KHz, C RCC_LSEConfig(RCC_LSE_ON); while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET){}; RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); RCC_RTCCLKCmd(ENABLE); RTC_WaitForSynchro(); RTC_InitTypeDef rtc_init; rtc_init.RTC_AsynchPrediv = 0xFF; rtc_init.RTC_SynchPrediv = 0x7F; rtc_init.RTC_HourFormat = RTC_HourFormat_24; */ RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div32); RCC_RTCCLKCmd(ENABLE); RTC_WaitForSynchro(); RTC_InitTypeDef rtc_init; rtc_init.RTC_SynchPrediv = 0x7a0; rtc_init.RTC_AsynchPrediv = 0x7F; rtc_init.RTC_HourFormat = RTC_HourFormat_24; RTC_Init(&rtc_init); }
void TM_RTC_Config(TM_RTC_ClockSource_t source) { if (source == TM_RTC_ClockSource_Internal) { /* 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); } else if (source == TM_RTC_ClockSource_External) { /* Enable the LSE OSC */ RCC_LSEConfig(RCC_LSE_ON); /* Wait till LSE is ready */ while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET); /* Select the RTC Clock Source */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); } /* Enable the RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* Wait for register synchronization */ RTC_WaitForSynchro(); /* Write status */ RTC_WriteBackupRegister(RTC_STATUS_REG, RTC_STATUS_INIT_OK); }
void RTC_Configuration(void) { /* 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(); /* 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 RTC_Configuration(void) * 功 能:RTC配置函数 * 入口参数:无 * 出口参数:无 * 说 明: * 调用方法: ****************************************************************************/ static void RTC_Configuration(void) { /* Enable the PWR clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* Allow access to RTC */ PWR_RTCAccessCmd(ENABLE); /*!< Reset RTC Domain */ RCC_RTCResetCmd(ENABLE); RCC_RTCResetCmd(DISABLE); #if defined (RTC_CLOCK_SOURCE_LSI) /* LSI used as RTC source clock*/ /* The RTC Clock may varies due to LSI frequency dispersion. */ /* 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); #elif defined (RTC_CLOCK_SOURCE_LSE) /* LSE used as RTC source clock *///外部32.378K晶振 /* Enable the LSE OSC */ RCC_LSEConfig(RCC_LSE_ON); /* Wait till LSE is ready */ while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) { } /* Select the RTC Clock Source */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); #else #error Please select the RTC Clock source inside the main.c file #endif /* RTC_CLOCK_SOURCE_LSI */ /* Enable the RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* Wait for RTC APB registers synchronisation */ RTC_WaitForSynchro(); }
/******************************************************************************* * Function Name : RTC_Configuration * Description : Configures the RTC. * Input : None * Output : None * Return : 0 reday,-1 error. *******************************************************************************/ int RTC_Config(void) { u32 count=0x250000; /* Enable the PWR clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* Allow access to RTC */ PWR_BackupAccessCmd(ENABLE); RCC_LSEConfig(RCC_LSE_ON); /* Wait till LSE is ready */ while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET && (--count) ); if ( count == 0 ) { return -1; } /* Select the RTC Clock Source */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); SynchPrediv = 0xFF; AsynchPrediv = 0x7F; /* Enable the RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* Wait for RTC APB registers synchronisation */ RTC_WaitForSynchro(); return 0; }
void RTC_SetUp(void) { /* Allow access to RTC */ PWR_BackupAccessCmd(ENABLE); RCC_LSICmd(ENABLE); // LSI is used as RTC clock source while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) ; // Wait till LSI is ready RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); // Select RTC clock source // Enable RTC clock RCC_RTCCLKCmd(ENABLE); RTC_WaitForSynchro(); // Wait until the RTC Time and Date registers (RTC_TR and RTC_DR) are synchronized with RTC APB clock. // Set RTC calendar clock to 1 HZ (1 second) RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; RTC_InitStructure.RTC_AsynchPrediv = 88; RTC_InitStructure.RTC_SynchPrediv = 470; if (RTC_Init(&RTC_InitStructure) == ERROR) { while (1) ; } }
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(); #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 /* 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 */ /* Set RTC prescaler: set RTC period to 1sec */ #ifdef RTCClockSource_LSI RTC_SetPrescaler(31999); /* RTC period = RTCCLK/RTC_PR = (32.000 KHz)/(31999+1) */ #elif defined RTCClockSource_LSE RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */ #endif // 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(); }
// 配置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(); }
static void sys_clk_init(void) { ErrorStatus HSEStartUpStatus; // set clocks registers back to defaults (for debugging, st_demo) RCC_DeInit(); // enable the high speed external osc (HSE) and spin for it to stabilise RCC_HSEConfig(RCC_HSE_ON); HSEStartUpStatus = RCC_WaitForHSEStartUp(); if (HSEStartUpStatus != SUCCESS) { ///@todo handle error better while (1) {} } // Enable Prefetch Buffer FLASH_PrefetchBufferCmd(ENABLE); // Flash 1 wait state FLASH_SetLatency(FLASH_Latency_5); // Setup the PLLCLK source and pre scaler for the following clocks // PLLCLK = 8MHz * 336[N] / (8[M] * 2[P]) = 168MHz // PLLCLK = 8MHz * 336[N] / (8[M] * 7[Q]) = 48MHz /* Setup the PLLCLK source and pre scaler */ RCC_PLLConfig(RCC_PLLSource_HSE, 8, 336, 2, 7); // AHB prescaler set to div 1, HCLK = SYSCLK (168MHz [max]) RCC_HCLKConfig(RCC_SYSCLK_Div1); // APB2 (high speed) prescaler set to div 2, APB2 = HCLK / 2 = 84MHz [max] RCC_PCLK2Config(RCC_HCLK_Div2); // APB1 (low speed) prescaler set to div 4, APB1 = HCLK / 4 = 42MHz [max] RCC_PCLK1Config(RCC_HCLK_Div4); // Enable the PLL and spin for it to be ready RCC_PLLCmd(ENABLE); while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) {} // Switch the system clock over to the PLL output and spin until it is ready RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); while(RCC_GetSYSCLKSource() != 0x08) ; // enable backup register domain clocks incase they are not enabled RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); PWR_BackupAccessCmd(ENABLE); RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div31); RCC_RTCCLKCmd(ENABLE); #ifdef DEBUG_MCO /* this is a handy debugging option to output the sys_clk/4 to the MCO1 pin * it must be sys_clk/4 as gpio can only output 50MHz max, so 168MHz/4 = 42MHz */ RCC_MCO1Config(RCC_MCO1Source_PLLCLK, RCC_MCO1Div_4); #endif }
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 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); }
OSStatus platform_rtc_init(void) { #ifdef MICO_ENABLE_MCU_RTC RTC_InitTypeDef RTC_InitStruct; RTC_DeInit( ); RTC_InitStruct.RTC_HourFormat = RTC_HourFormat_24; /* RTC ticks every second */ RTC_InitStruct.RTC_AsynchPrediv = 0x7F; RTC_InitStruct.RTC_SynchPrediv = 0xFF; RTC_Init( &RTC_InitStruct ); #ifdef USE_RTC_BKP /* Enable the PWR clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* Allow access to BKP Domain */ PWR_BackupAccessCmd(ENABLE); PWR_BackupRegulatorCmd(ENABLE); #endif /* Enable the LSE OSC */ RCC_LSEConfig(RCC_LSE_ON); /* Wait till LSE is ready */ while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) { } /* Select the RTC Clock Source */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); /* Enable the RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* RTC configuration -------------------------------------------------------*/ /* Wait for RTC APB registers synchronisation */ RTC_WaitForSynchro(); #ifdef USE_RTC_BKP if (RTC_ReadBackupRegister(RTC_BKP_DR0) != USE_RTC_BKP) { /* set it to 12:20:30 08/04/2013 monday */ platform_rtc_set_time(&default_rtc_time); RTC_WriteBackupRegister(RTC_BKP_DR0, USE_RTC_BKP); } #else //#ifdef RTC_ENABLED /* application must have mico_application_default_time structure declared somewhere, otherwise it wont compile */ /* write default application time inside rtc */ platform_rtc_set_time(&mico_default_time); //#endif /* RTC_ENABLED */ #endif return kNoErr; #else return kUnsupportedErr; #endif }
void rtc_write(void) { if(!rtc_ok) { // Enable the PWR clock RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); // Allow access to RTC PWR_BackupAccessCmd(ENABLE); // Enable the LSE OSC RCC_LSEConfig(RCC_LSE_ON); // Wait till LSE is ready while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET); // Select the RTC Clock Source RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); // Enable the RTC Clock RCC_RTCCLKCmd(ENABLE); // Wait for RTC APB registers synchronisation RTC_WaitForSynchro(); // Enable The TimeStamp RTC_TimeStampCmd(RTC_TimeStampEdge_Falling, ENABLE); // TODO: can return ERROR RTC_InitTypeDef RTC_InitStructure; RTC_InitStructure.RTC_AsynchPrediv = 0x7F; RTC_InitStructure.RTC_SynchPrediv = 0xFF; RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; RTC_Init(&RTC_InitStructure); } RTC_TimeTypeDef TimeRTC; RTC_DateTypeDef DateRTC; TimeRTC.RTC_H12 = RTC_H12_AM; // Always AM for 24H TimeRTC.RTC_Hours = rtc_hour; TimeRTC.RTC_Minutes = rtc_minute; TimeRTC.RTC_Seconds = rtc_second; RTC_SetTime(RTC_Format_BIN, &TimeRTC); (void)RTC->DR; DateRTC.RTC_WeekDay = rtc_day; DateRTC.RTC_Date = rtc_date; DateRTC.RTC_Month = rtc_month; DateRTC.RTC_Year = rtc_year; RTC_SetDate(RTC_Format_BIN, &DateRTC); // TODO: can return ERROR RTC_WriteBackupRegister(RTC_BKP_DR0, 0xCA7E); rtc_ok = true; }
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(); }
void initClock(void) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_SYSCFG , ENABLE); // divide HCLK / 2 RCC_HCLKConfig(RCC_SYSCLK_Div2); // enable HSI RCC_HSICmd(ENABLE); RCC_PLLCmd(DISABLE); // wait for HSI to get ready while (RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET); // configure PLL - x4 /2 RCC_PLLConfig( RCC_PLLSource_HSI, RCC_PLLMul_8, RCC_PLLDiv_4 ); RCC_PLLCmd(ENABLE); while ( RCC_GetFlagStatus( RCC_FLAG_PLLRDY ) == RESET ); RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); RCC_ClocksTypeDef RCC_Clocks; RCC_GetClocksFreq(&RCC_Clocks); SysTick_Config((RCC_Clocks.SYSCLK_Frequency / 2) / 1000); // Cannot exceed 16,777,215 /* Set SysTick Preemption Priority, it's a system handler rather than a regular interrupt */ NVIC_SetPriority(SysTick_IRQn, 0x04); // setup lse for rtc PWR_RTCAccessCmd(ENABLE); // Reset RTC Backup Domain RCC_RTCResetCmd(ENABLE); RCC_RTCResetCmd(DISABLE); // LSE Enable RCC_LSEConfig(RCC_LSE_ON); // Wait until LSE is ready while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET); // RTC Clock Source Selection RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); // Enable the RTC RCC_RTCCLKCmd(ENABLE); RTC_WaitForSynchro(); }
void back_register_init(void) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); PWR_BackupAccessCmd(ENABLE); RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div31); RCC_RTCCLKCmd(ENABLE); // RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_BKPSRAM, ENABLE);//打开BACKUP_SRAM的时钟. // backup_data[0]++; // rt_kprintf("backup_data[0] = %d\n",backup_data[0]); }
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); }
// Control functions void rtcInit(void) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); PWR_BackupAccessCmd(ENABLE); RCC_BackupResetCmd(ENABLE); RCC_BackupResetCmd(DISABLE); RCC_LSICmd(ENABLE); RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); RCC_RTCCLKCmd(ENABLE); }
/* ************************************************************ * 函数名称: RTC_Init * * 函数功能: RTC初始化 * * 入口参数: 无 * * 返回参数: 无 * * 说明: ************************************************************ */ _Bool RTC_Init(void) { #if(USE_EXT_RCC == 1) unsigned char errCount = 0; #endif RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); //使能PWR和BKP外设时钟 PWR_BackupAccessCmd(ENABLE); //使能后备寄存器访问 BKP_DeInit(); //复位备份区域 #if(USE_EXT_RCC == 1) RCC_LSEConfig(RCC_LSE_ON); //设置外部低速晶振(LSE),使用外设低速晶振 while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET && errCount < 250) //检查指定的RCC标志位设置与否,等待低速晶振就绪 { errCount++; DelayMs(10); } if(errCount >= 250) return 1; //初始化时钟失败,晶振有问题 #endif #if(USE_EXT_RCC == 1) RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); //设置RTC时钟(RTCCLK),选择LSE作为RTC时钟 #else RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div128); //设置RTC时钟(HSE/128),选择HES作为RTC时钟 #endif RCC_RTCCLKCmd(ENABLE); //使能RTC时钟 RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成 RTC_WaitForSynchro(); //等待RTC寄存器同步 RTC_ITConfig(RTC_IT_ALR, ENABLE); //使能RTC闹钟中断 RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成 RTC_EnterConfigMode(); //允许配置 #if(USE_EXT_RCC == 1) RTC_SetPrescaler(32767); //设置RTC预分频的值 #else RTC_SetPrescaler(HSE_VALUE / 128 - 1); //设置RTC预分频的值 #endif RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成 RTC_SetCounter(0); //设置RTC计数器的值 RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成 RTC_ExitConfigMode(); //退出配置模式 RTC_NVIC_Init(); return 0; }
/** * @brief Configure the RTC peripheral by selecting the clock source. * @param None * @retval None */ void RTC_Config(void) { /* Enable the PWR clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* Allow access to RTC */ PWR_BackupAccessCmd(ENABLE); #if defined (RTC_CLOCK_SOURCE_LSI) /* LSI used as RTC source clock*/ /* The RTC Clock may varies due to LSI frequency dispersion. */ /* 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); SynchPrediv = 0x18F; AsynchPrediv = 0x63; #elif defined (RTC_CLOCK_SOURCE_LSE) /* LSE used as RTC source clock */ /* Enable the LSE OSC */ RCC_LSEConfig(RCC_LSE_ON); /* Wait till LSE is ready */ while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) { } /* Select the RTC Clock Source */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); SynchPrediv = 0xFF; AsynchPrediv = 0x7F; #else #error Please select the RTC Clock source inside the main.c file #endif /* RTC_CLOCK_SOURCE_LSI */ /* Enable the RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* Wait for RTC APB registers synchronisation */ RTC_WaitForSynchro(); /* Enable The TimeStamp */ RTC_TimeStampCmd(RTC_TimeStampEdge_Falling, ENABLE); }
/** * @brief Configures the RTC Wakeup. * @param None * @retval None */ void RTC_Config(void) { NVIC_InitTypeDef NVIC_InitStructure; EXTI_InitTypeDef EXTI_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /*!< Allow access to RTC */ PWR_RTCAccessCmd(ENABLE); /*!< Reset RTC Domain */ RCC_RTCResetCmd(ENABLE); RCC_RTCResetCmd(DISABLE); /*!< LSE Enable */ RCC_LSEConfig(RCC_LSE_ON); /*!< Wait till LSE is ready */ while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) {} /*!< LCD Clock Source Selection */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); /*!< Enable the RTC Clock */ RCC_RTCCLKCmd(ENABLE); /*!< Wait for RTC APB registers synchronisation */ RTC_WaitForSynchro(); /* EXTI configuration *******************************************************/ EXTI_ClearITPendingBit(EXTI_Line20); EXTI_InitStructure.EXTI_Line = EXTI_Line20; 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 Wakeup Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = RTC_WKUP_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* RTC Wakeup Interrupt Generation: Clock Source: RTCDiv_16, Wakeup Time Base: 4s */ RTC_WakeUpClockConfig(RTC_WakeUpClock_RTCCLK_Div16); RTC_SetWakeUpCounter(0x1FFF); /* Enable the Wakeup Interrupt */ RTC_ITConfig(RTC_IT_WUT, ENABLE); }