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(); } }
// 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); }
/** * @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); RCC_BackupResetCmd(ENABLE); RCC_BackupResetCmd(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); 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(); }
void RTC_Configuration() { 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); RCC_LSEConfig(RCC_LSE_ON); }
/** * @brief Configures the RTC peripheral and select the clock source. * @param None * @retval None */ static void RTC_Config(void) { RTC_InitTypeDef RTC_InitStructure; RTC_TimeTypeDef RTC_TimeStruct; /* Enable the PWR clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* Allow access to RTC */ PWR_BackupAccessCmd(ENABLE); /* Reset RTC Domain */ RCC_BackupResetCmd(ENABLE); RCC_BackupResetCmd(DISABLE); /* 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); /* Configure the RTC data register and RTC prescaler */ /* ck_spre(1Hz) = RTCCLK(LSI) /(AsynchPrediv + 1)*(SynchPrediv + 1)*/ RTC_InitStructure.RTC_AsynchPrediv = 0x7F; RTC_InitStructure.RTC_SynchPrediv = 0xFF; RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; RTC_Init(&RTC_InitStructure); /* Set the time to 00h 00mn 00s AM */ RTC_TimeStruct.RTC_H12 = RTC_H12_AM; RTC_TimeStruct.RTC_Hours = 0x00; RTC_TimeStruct.RTC_Minutes = 0x00; RTC_TimeStruct.RTC_Seconds = 0x00; RTC_SetTime(RTC_Format_BCD, &RTC_TimeStruct); }
/******************************************************************************* * Function Name : BKP_DeInit * Description : Deinitializes the BKP peripheral registers to their default * reset values. * Input : None * Output : None * Return : None *******************************************************************************/ void BKP_DeInit(void) { RCC_BackupResetCmd(ENABLE); RCC_BackupResetCmd(DISABLE); }
/** * @brief Main program. * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f30x.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f30x.c file */ /* LEDs Init */ STM_EVAL_LEDInit(LED3); STM_EVAL_LEDInit(LED4); STM_EVAL_LEDInit(LED5); STM_EVAL_LEDInit(LED6); STM_EVAL_LEDInit(LED7); STM_EVAL_LEDInit(LED8); STM_EVAL_LEDInit(LED9); STM_EVAL_LEDInit(LED10); /* Enable PWR APB1 Clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* Allow access to Backup */ PWR_BackupAccessCmd(ENABLE); /* Reset RTC Domain */ RCC_BackupResetCmd(ENABLE); RCC_BackupResetCmd(DISABLE); /* Allow access to RTC */ PWR_BackupAccessCmd(ENABLE); /* 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); /* Enable the RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* Wait for RTC APB registers synchronisation */ RTC_WaitForSynchro(); /* RTC prescaler configuration */ RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; RTC_InitStructure.RTC_AsynchPrediv = 88; RTC_InitStructure.RTC_SynchPrediv = 470; RTC_Init(&RTC_InitStructure); /* Set the alarm 01h:00min:04s */ RTC_AlarmStructure.RTC_AlarmTime.RTC_H12 = RTC_H12_AM; RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours = 0x01; RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes = 0x00; RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds = 0x04; RTC_AlarmStructure.RTC_AlarmDateWeekDay = 0x31; RTC_AlarmStructure.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date; /* Alarm mask hour, min and second:default Alarm generation each 1s */ RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_All; RTC_SetAlarm(RTC_Format_BCD, RTC_Alarm_A, &RTC_AlarmStructure); /* Enable RTC Alarm A Interrupt */ RTC_ITConfig(RTC_IT_ALRA, ENABLE); /* Enable the alarm */ RTC_AlarmCmd(RTC_Alarm_A, ENABLE); /* Set the date: Wednesday August 15th 2012 */ RTC_DateStructure.RTC_Year = 12; RTC_DateStructure.RTC_Month = RTC_Month_September; RTC_DateStructure.RTC_Date = 11; RTC_DateStructure.RTC_WeekDay = RTC_Weekday_Tuesday; RTC_SetDate(RTC_Format_BCD, &RTC_DateStructure); /* Set the time to 01h 00mn 00s AM */ RTC_TimeStructure.RTC_H12 = RTC_H12_AM; RTC_TimeStructure.RTC_Hours = 0x01; RTC_TimeStructure.RTC_Minutes = 0x00; RTC_TimeStructure.RTC_Seconds = 0x00; RTC_SetTime(RTC_Format_BCD, &RTC_TimeStructure); RTC_ClearFlag(RTC_FLAG_ALRAF); /* RTC Alarm A Interrupt Configuration */ /* 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); /* Infinite loop */ while (1) { /* Get the RTC current Time */ RTC_GetTime(RTC_Format_BCD, &RTC_TimeStructure); /* Get the RTC current Date */ RTC_GetDate(RTC_Format_BCD, &RTC_DateStructure); /* Display time Format : hh:mm:ss */ // sprintf((char*)showtime,"%0.2d:%0.2d:%0.2d",RTC_TimeStructure.RTC_Hours, RTC_TimeStructure.RTC_Minutes, RTC_TimeStructure.RTC_Seconds); /* Display date Format : mm-dd-yy */ //sprintf((char*)showdate,"%0.2d-%0.2d-%0.2d",RTC_DateStructure.RTC_Month, RTC_DateStructure.RTC_Date, 2000 + RTC_DateStructure.RTC_Year); } }
/** * @brief Configures the RTC. * @param None * @retval None */ static void RTC_Config(void) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* Allow access to BKP Domain */ PWR_BackupAccessCmd(ENABLE); /* Clear Wakeup flag */ PWR_ClearFlag(PWR_FLAG_WU); /* Check if the StandBy flag is set */ if (PWR_GetFlagStatus(PWR_FLAG_SB) != RESET) { /* Clear StandBy flag */ PWR_ClearFlag(PWR_FLAG_SB); /* Wait for RTC APB registers synchronisation (needed after start-up from Reset)*/ RTC_WaitForSynchro(); /* No need to configure the RTC as the RTC config(clock source, enable, prescaler,...) are kept after wake-up from STANDBY */ } else { /* RTC Configuration ******************************************************/ /* Reset Backup Domain */ RCC_BackupResetCmd(ENABLE); RCC_BackupResetCmd(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); SynchPrediv = 0xFF; AsynchPrediv = 0x7F; #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 (needed after start-up from Reset)*/ RTC_WaitForSynchro(); /* Set the RTC time base to 1s */ RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; RTC_InitStructure.RTC_AsynchPrediv = 0x7F; RTC_InitStructure.RTC_SynchPrediv = 0x00FF; if (RTC_Init(&RTC_InitStructure) == ERROR) { /* Turn on LED2 */ STM_EVAL_LEDOn(LED2); /* User can add here some code to deal with this error */ while(1); } /* Set the time to 01h 00mn 00s AM */ RTC_TimeStructure.RTC_H12 = RTC_H12_AM; RTC_TimeStructure.RTC_Hours = 0x01; RTC_TimeStructure.RTC_Minutes = 0x00; RTC_TimeStructure.RTC_Seconds = 0x00; if(RTC_SetTime(RTC_Format_BCD, &RTC_TimeStructure) == ERROR) { /* Turn on LED2 */ STM_EVAL_LEDOn(LED2); /* User can add here some code to deal with this error */ while(1); } } /* Clear RTC Alarm Flag */ RTC_ClearFlag(RTC_FLAG_ALRAF); }
/** * @brief Configures TIM14 to measure the LSI oscillator frequency. * @param None * @retval None */ void TIM14_ConfigForLSI(void) { NVIC_InitTypeDef NVIC_InitStructure; TIM_ICInitTypeDef TIM_ICInitStructure; /* Enable peripheral clocks ------------------------------------------------*/ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* Allow access to the RTC */ PWR_BackupAccessCmd(ENABLE); /* Reset RTC Domain */ RCC_BackupResetCmd(ENABLE); RCC_BackupResetCmd(DISABLE); /*!< LSI Enable */ 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 the RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* Wait for RTC APB registers synchronisation */ RTC_WaitForSynchro(); /* Enable TIM14 clocks */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14, ENABLE); /* Enable the TIM14 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = TIM14_IRQn; NVIC_InitStructure.NVIC_IRQChannelPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Configure TIM14 prescaler */ TIM_PrescalerConfig(TIM14, 0, TIM_PSCReloadMode_Immediate); /* Connect internally the TM14_CH1 Input Capture to the LSI clock output */ TIM_RemapConfig(TIM14, TIM14_RTC_CLK); /* TIM14 configuration: Input Capture mode --------------------- The LSI oscillator is connected to TIM14 CH1 The Rising edge is used as active edge, The TIM14 CCR1 is used to compute the frequency value ------------------------------------------------------------ */ TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV8; TIM_ICInitStructure.TIM_ICFilter = 0; TIM_ICInit(TIM14, &TIM_ICInitStructure); /* TIM14 Counter Enable */ TIM_Cmd(TIM14, ENABLE); /* Reset the flags */ TIM14->SR = 0; /* Enable the CC1 Interrupt Request */ TIM_ITConfig(TIM14, TIM_IT_CC1, ENABLE); }
/** * @brief Main program. * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f30x.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f30x.c file */ /* Configure USER Button */ STM_EVAL_PBInit(BUTTON_USER,BUTTON_MODE_EXTI); KeyPressed = 0; /* Loop while USER button is maintained pressed */ while(KeyPressed == 0) { } /* Loop while User button is maintained pressed */ while(STM_EVAL_PBGetState(BUTTON_USER) != RESET) { } /* Enable PWR APB1 Clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* Allow access to Backup */ PWR_BackupAccessCmd(ENABLE); /* Reset RTC Domain */ RCC_BackupResetCmd(ENABLE); RCC_BackupResetCmd(DISABLE); #if defined (SLEEP_MODE) /* Sleep Mode Entry - System Running at PLL (72MHz) - Flash 1 wait state - Prefetch and Cache enabled - Code running from Internal FLASH - All peripherals disabled. - Wakeup using EXTI Line (USER Button PA.0) */ SleepMode_Measure(); #elif defined (STOP_MODE) /* STOP Mode Entry - RTC Clocked by LSI - Regulator in LP mode - HSI, HSE OFF and LSI OFF if not used as RTC Clock source - No IWDG - FLASH in deep power down mode - Automatic Wakeup using RTC clocked by LSI */ /* When using the small packages (48 and 64 pin packages), the GPIO pins which are not present on these packages, must not be configured in analog mode.*/ StopMode_Measure(); #elif defined (STANDBY_MODE) /* STANDBY Mode Entry - RTC OFF - IWDG and LSI OFF - Wakeup using WakeUp Pin (PA.0) */ StandbyMode_Measure(); #elif defined (STANDBY_RTC_MODE) /* STANDBY Mode with RTC on LSI Entry - RTC Clocked by LSI - IWDG OFF and LSI OFF if not used as RTC Clock source - Automatic Wakeup using RTC clocked by LSI */ StandbyRTCMode_Measure(); #else /* Initialize LED3 on STM32373C-EVAL board */ STM_EVAL_LEDInit(LED3); /* Infinite loop */ while (1) { /* Toggle The LED3 */ STM_EVAL_LEDToggle(LED3); /* Inserted Delay */ for(i = 0; i < 0x7FFF; i++); } #endif }
/** * @brief Main program * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup files (startup_stm32f40_41xxx.s/startup_stm32f427_437xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f4xx.c file */ /* Enable PWR APB1 Clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* Allow access to Backup */ PWR_BackupAccessCmd(ENABLE); /* Reset RTC Domain */ RCC_BackupResetCmd(ENABLE); RCC_BackupResetCmd(DISABLE); /* Check that the system was resumed from StandBy mode */ if(PWR_GetFlagStatus(PWR_FLAG_SB) != RESET) { /* Clear SB Flag */ PWR_ClearFlag(PWR_FLAG_SB); /* Initialize LED1 on STM324xG-EVAL board */ STM_EVAL_LEDInit(LED1); /* Infinite loop */ while (1) { /* Toggle The LED1 */ STM_EVAL_LEDToggle(LED1); /* Inserted Delay */ for(uwCounter = 0; uwCounter < 0x5FFFF; uwCounter++); } } /* Configure Key Button */ STM_EVAL_PBInit(BUTTON_KEY,BUTTON_MODE_GPIO); /* Wait until Key button is pressed to enter the Low Power mode */ while(STM_EVAL_PBGetState(BUTTON_KEY) != RESET) { } /* Loop while Key button is maintained pressed */ while(STM_EVAL_PBGetState(BUTTON_KEY) == RESET) { } #if defined (SLEEP_MODE) /* Sleep Mode Entry - System Running at PLL (168MHz) - Flash 5 wait state - Instruction and Data caches ON - Prefetch ON - Code running from Internal FLASH - All peripherals disabled. - Wakeup using EXTI Line (Key Button PG.15) */ SleepMode_Measure(); #elif defined (STOP_MODE) /* STOP Mode Entry - RTC Clocked by LSE/LSI - Regulator in LP mode - HSI, HSE OFF and LSI OFF if not used as RTC Clock source - No IWDG - FLASH in deep power down mode - Automatic Wakeup using RTC clocked by LSE/LSI (after ~20s) */ StopMode_Measure(); #elif defined (STANDBY_MODE) /* STANDBY Mode Entry - Backup SRAM and RTC OFF - IWDG and LSI OFF - Wakeup using WakeUp Pin (PA.00) */ StandbyMode_Measure(); #elif defined (STANDBY_RTC_MODE) /* STANDBY Mode with RTC on LSE/LSI Entry - RTC Clocked by LSE or LSI - IWDG OFF and LSI OFF if not used as RTC Clock source - Backup SRAM OFF - Automatic Wakeup using RTC clocked by LSE/LSI (after ~20s) */ StandbyRTCMode_Measure(); #elif defined (STANDBY_RTC_BKPSRAM_MODE) /* STANDBY Mode with RTC on LSE/LSI Entry - RTC Clocked by LSE/LSI - Backup SRAM ON - IWDG OFF - Automatic Wakeup using RTC clocked by LSE/LSI (after ~20s) */ StandbyRTCBKPSRAMMode_Measure(); #else /* Initialize LED1 on STM324xG-EVAL board */ STM_EVAL_LEDInit(LED1); /* Infinite loop */ while (1) { /* Toggle The LED1 */ STM_EVAL_LEDToggle(LED1); /* Inserted Delay */ for(Counter = 0; Counter < 0x5FF; Counter++); } #endif }
/** * @brief Configures the RTC clock source. * @param None * @retval None */ void RTC_Config(void) { RTC_InitTypeDef RTC_InitStructure; RTC_AlarmTypeDef RTC_AlarmStructure; RTC_TimeTypeDef RTC_TimeStructure; /* Enable the PWR clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* Allow access to Backup Domain */ PWR_BackupAccessCmd(ENABLE); /* Check if the StandBy flag is set */ if (PWR_GetFlagStatus(PWR_FLAG_SB) != RESET) { /* Clear StandBy flag */ PWR_ClearFlag(PWR_FLAG_SB); /* Check if the StandBy flag is cleared */ if (PWR_GetFlagStatus(PWR_FLAG_SB) != RESET) { while(1); } RTC_WaitForSynchro(); /* No need to configure the RTC as the RTC config(clock source, enable, prescaler,...) are kept after wake-up from STANDBY */ } else { /* RTC Configuration ******************************************************/ /* Reset Backup Domain */ RCC_BackupResetCmd(ENABLE); RCC_BackupResetCmd(DISABLE); /* 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 the RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* Wait for RTC APB registers synchronisation */ RTC_WaitForSynchro(); RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; RTC_InitStructure.RTC_AsynchPrediv = 0x7F; RTC_InitStructure.RTC_SynchPrediv = 0x0138; RTC_Init(&RTC_InitStructure); /* Set the alarm X+5s */ RTC_AlarmStructure.RTC_AlarmTime.RTC_H12 = RTC_H12_AM; RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours = 0x01; RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes = 0x00; RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds = 0x3; RTC_AlarmStructure.RTC_AlarmDateWeekDay = 0x31; RTC_AlarmStructure.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date; RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_DateWeekDay; RTC_SetAlarm(RTC_Format_BCD, RTC_Alarm_A, &RTC_AlarmStructure); /* Enable RTC Alarm A Interrupt */ RTC_ITConfig(RTC_IT_ALRA, ENABLE); /* Enable the alarm */ RTC_AlarmCmd(RTC_Alarm_A, ENABLE); } /* Set the time to 01h 00mn 00s AM */ RTC_TimeStructure.RTC_H12 = RTC_H12_AM; RTC_TimeStructure.RTC_Hours = 0x01; RTC_TimeStructure.RTC_Minutes = 0x00; RTC_TimeStructure.RTC_Seconds = 0x00; RTC_SetTime(RTC_Format_BCD, &RTC_TimeStructure); /* Clear Wakeup flag */ PWR_ClearFlag(PWR_FLAG_WU); 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 clock source. * @param None * @retval None */ static void RTC_Config(void) { RTC_InitTypeDef RTC_InitStructure; RTC_TimeTypeDef RTC_TimeStructure; uint32_t LSIFreq = 0; /* Enable the PWR clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* Allow access to Backup Domain */ PWR_BackupAccessCmd(ENABLE); /* Clear Wakeup flag */ PWR_ClearFlag(PWR_FLAG_WU); /* Enable the LSI OSC */ RCC_LSICmd(ENABLE); /* Wait till LSI is ready */ while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) {} /* Check if the StandBy flag is set */ if (PWR_GetFlagStatus(PWR_FLAG_SB) != RESET) { /* Clear StandBy flag */ PWR_ClearFlag(PWR_FLAG_SB); /* Check if the StandBy flag is cleared */ if (PWR_GetFlagStatus(PWR_FLAG_SB) != RESET) { while(1); } RTC_WaitForSynchro(); /* No need to configure the RTC as the RTC config(clock source, enable, prescaler,...) are kept after wake-up from STANDBY */ } else { /* RTC Configuration ******************************************************/ /* Reset Backup Domain */ RCC_BackupResetCmd(ENABLE); RCC_BackupResetCmd(DISABLE); /* Select the RTC Clock Source */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); /* Enable the RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* Wait for RTC APB registers synchronisation */ RTC_WaitForSynchro(); /* Get the LSI frequency: TIM14 is used to measure the LSI frequency */ LSIFreq = GetLSIFrequency(); RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; RTC_InitStructure.RTC_AsynchPrediv = 99; RTC_InitStructure.RTC_SynchPrediv = (LSIFreq/100) - 1; RTC_Init(&RTC_InitStructure); /* Set the time to 01h 00mn 00s AM */ RTC_TimeStructure.RTC_H12 = RTC_H12_AM; RTC_TimeStructure.RTC_Hours = 0x01; RTC_TimeStructure.RTC_Minutes = 0x00; RTC_TimeStructure.RTC_Seconds = 0x00; RTC_SetTime(RTC_Format_BIN, &RTC_TimeStructure); } }
/** * @brief RTC Tamper Configuration.. * @param None * @retval None */ static void RTC_Config(void) { EXTI_InitTypeDef EXTI_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* Enable the PWR clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* Allow access to RTC */ PWR_BackupAccessCmd(ENABLE); /* Reset Backup Domain */ RCC_BackupResetCmd(ENABLE); RCC_BackupResetCmd(DISABLE); /* 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); RTC_DeInit(); /* Configure the RTC data register and RTC prescaler */ RTC_InitStructure.RTC_AsynchPrediv = 0x7F; RTC_InitStructure.RTC_SynchPrediv = 0xFF; RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; RTC_Init(&RTC_InitStructure); /* Set the time to 00h 00mn 00s AM */ RTC_TimeStruct.RTC_H12 = RTC_H12_AM; RTC_TimeStruct.RTC_Hours = 0x00; RTC_TimeStruct.RTC_Minutes = 0x00; RTC_TimeStruct.RTC_Seconds = 0x00; RTC_SetTime(RTC_Format_BCD, &RTC_TimeStruct); /* Enable the RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* Wait for RTC APB registers synchronisation */ RTC_WaitForSynchro(); /* EXTI configuration *******************************************************/ EXTI_ClearITPendingBit(EXTI_Line19); EXTI_InitStructure.EXTI_Line = EXTI_Line19; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); /* Enable RTC_IRQn */ NVIC_InitStructure.NVIC_IRQChannel = TAMPER_STAMP_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* determines the number of active pulse for the specific level */ RTC_TamperFilterConfig(RTC_TamperFilter_2Sample); /* Determines the frequency at which each of the tamper inputs are sampled */ RTC_TamperSamplingFreqConfig(RTC_TamperSamplingFreq_RTCCLK_Div32768); RTC_TamperPullUpCmd(DISABLE); /* Select the tamper 21 with High level */ RTC_TamperTriggerConfig(RTC_Tamper_1, RTC_TamperTrigger_HighLevel ); /* Clear tamper 1 falg */ RTC_ClearFlag(RTC_FLAG_TAMP1F); }
/** * @brief Main program * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f4xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f4xx.c file */ /* Enable PWR APB1 Clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* Allow access to Backup */ PWR_BackupAccessCmd(ENABLE); /* Reset RTC Domain */ RCC_BackupResetCmd(ENABLE); RCC_BackupResetCmd(DISABLE); /* Configure User Button */ STM_EVAL_PBInit(BUTTON_USER,BUTTON_MODE_EXTI); /* Wait until User button is pressed to enter the Low Power mode */ while(UserButtonStatus == 0x00) { } /* Loop while User button is maintained pressed */ while(STM_EVAL_PBGetState(BUTTON_USER) != RESET) { } #if defined (SLEEP_MODE) /* Sleep Mode Entry - System Running at PLL (168MHz) - Flash 3 wait state - Prefetch and Cache enabled - Code running from Internal FLASH - All peripherals disabled. - Wakeup using EXTI Line (User Button PA.00) */ SleepMode_Measure(); #elif defined (STOP_MODE) /* STOP Mode Entry - RTC Clocked by LSI - Regulator in LP mode - HSI, HSE OFF and LSI OFF if not used as RTC Clock source - No IWDG - FLASH in deep power down mode - Automatic Wakeup using RTC clocked by LSI (after ~20s) */ StopMode_Measure(); #elif defined (STANDBY_MODE) /* STANDBY Mode Entry - Backup SRAM and RTC OFF - IWDG and LSI OFF - Wakeup using WakeUp Pin (PA.00) */ StandbyMode_Measure(); #elif defined (STANDBY_RTC_MODE) /* STANDBY Mode with RTC on LSI Entry - RTC Clocked by LSI - IWDG OFF and LSI OFF if not used as RTC Clock source - Backup SRAM OFF - Automatic Wakeup using RTC clocked by LSI (after ~20s) */ StandbyRTCMode_Measure(); #elif defined (STANDBY_RTC_BKPSRAM_MODE) /* STANDBY Mode with RTC on LSI Entry - RTC Clocked by LSI - Backup SRAM ON - IWDG OFF - Automatic Wakeup using RTC clocked by LSI (after ~20s) */ StandbyRTCBKPSRAMMode_Measure(); #else /* Initialize LED4 on STM32F4-Discovery board */ STM_EVAL_LEDInit(LED4); /* Infinite loop */ while (1) { /* Toggle The LED4 */ STM_EVAL_LEDToggle(LED4); /* Inserted Delay */ for(i = 0; i < 0x5FF; i++); } #endif }
/** * @brief Configure the RTC peripheral by selecting the clock source. * @param None * @retval None */ void RTC_Config(void) { NVIC_InitTypeDef NVIC_InitStructure; EXTI_InitTypeDef EXTI_InitStructure; /* Enable the PWR clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* Allow access to RTC */ PWR_BackupAccessCmd(ENABLE); /* Reset BKP Domain */ RCC_BackupResetCmd(ENABLE); RCC_BackupResetCmd(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 */ /* 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 external line21 interrupt */ EXTI_ClearITPendingBit(EXTI_Line21); EXTI_InitStructure.EXTI_Line = EXTI_Line21; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); /* Enable TAMPER IRQChannel */ NVIC_InitStructure.NVIC_IRQChannel = TAMP_STAMP_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Disable the Tamper 1 detection */ RTC_TamperCmd(RTC_Tamper_1, DISABLE); /* Clear Tamper 1 pin Event(TAMP1F) pending flag */ RTC_ClearFlag(RTC_FLAG_TAMP1F); /* Configure the Tamper 1 Trigger */ RTC_TamperTriggerConfig(RTC_Tamper_1, RTC_TamperTrigger_FallingEdge); /* Enable the Tamper interrupt */ RTC_ITConfig(RTC_IT_TAMP, ENABLE); /* Clear Tamper 1 pin interrupt pending bit */ RTC_ClearITPendingBit(RTC_IT_TAMP1); /* Enable the Tamper 1 detection */ RTC_TamperCmd(RTC_Tamper_1, ENABLE); }
/** * @brief Configure the RTC peripheral by selecting the clock source. * @param None * @retval None */ static void RTC_Config(void) { RTC_InitTypeDef RTC_InitStructure; /* Enable the PWR clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* Allow access to RTC */ PWR_BackupAccessCmd(ENABLE); RCC_BackupResetCmd(ENABLE); RCC_BackupResetCmd(DISABLE); #if 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); /* ck_spre(1Hz) = RTCCLK(LSE) /(AsynchPrediv + 1)*(SynchPrediv + 1)*/ SynchPrediv = 0xFF; AsynchPrediv = 0x7F; #elif 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); /* ck_spre(1Hz) = RTCCLK(LSI) /(AsynchPrediv + 1)*(SynchPrediv + 1)*/ SynchPrediv = 0xFF; AsynchPrediv = 0x7F; #else #error Please select the RTC Clock source inside the main.c file #endif /* RTC_CLOCK_SOURCE_LSE */ /* Configure the RTC data register and RTC prescaler */ RTC_InitStructure.RTC_AsynchPrediv = AsynchPrediv; RTC_InitStructure.RTC_SynchPrediv = SynchPrediv; RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; /* Check on RTC init */ if (RTC_Init(&RTC_InitStructure) == ERROR) { /* Set the Back Color */ LCD_SetBackColor(LCD_COLOR_WHITE); /* Set the Text Color */ LCD_SetTextColor(LCD_COLOR_RED); LCD_SetFont(&Font12x12); LCD_DisplayStringLine(LCD_LINE_3,(uint8_t *) " RTC Prescaler Conf failed " ); LCD_SetFont(&Font16x24); } /* Enable the RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* Wait for RTC APB registers synchronisation */ RTC_WaitForSynchro(); /* Enable The TimeStamp */ RTC_TimeStampCmd(RTC_TimeStampEdge_Falling, ENABLE); }