void StandBy(void) { PushREG(REGA,REG_Shut,0x00,0x00); RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); NVIC_SystemLPConfig(NVIC_LP_SEVONPEND,ENABLE); PWR_WakeUpPinCmd(ENABLE); PWR_ClearFlag(PWR_FLAG_WU); PWR_ClearFlag(PWR_FLAG_SB); PWR_EnterSTANDBYMode(); return; }
/******************************************************************************* * Function Name : RTCAlarm_IRQHandler * Description : This function handles RTC Alarm interrupt request. * Input : None * Output : None * Return : None *******************************************************************************/ void RTCAlarm_IRQHandler(void) { if(RTC_GetITStatus(RTC_IT_ALR) != RESET) { SPARK_WLAN_SLEEP = 0; /* Clear EXTI line17 pending bit */ EXTI_ClearITPendingBit(EXTI_Line17); /* Check if the Wake-Up flag is set */ if(PWR_GetFlagStatus(PWR_FLAG_WU) != RESET) { /* Clear Wake Up flag */ PWR_ClearFlag(PWR_FLAG_WU); } /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Clear RTC Alarm interrupt pending bit */ RTC_ClearITPendingBit(RTC_IT_ALR); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); } }
/** * @brief This function configures the system to enter Standby mode for * current consumption measurement purpose. * STANDBY Mode * ============ * - IWDG and LSI OFF * - Current Consumption ~0.3uA * - Wakeup using WakeUp Pin 1 (PA.00) * @param None * @retval None */ void StandbyMode_Measure(void) { /* Configure Key Button*/ STM_EVAL_PBInit(BUTTON_KEY,BUTTON_MODE_GPIO); /* Wait Until Key button pressed */ while(STM_EVAL_PBGetState(BUTTON_KEY) == RESET) { } /* Wait Until Key button pressed */ while(STM_EVAL_PBGetState(BUTTON_KEY) != RESET) { } /* Enable Ultra low power mode */ PWR_UltraLowPowerCmd(ENABLE); /* Clear PWR WakeUp flag */ PWR_ClearFlag(PWR_FLAG_WU); /* Enable WKUP pin 1 */ PWR_WakeUpPinCmd(PWR_WakeUpPin_1, ENABLE); /* Request to enter STANDBY mode */ PWR_EnterSTANDBYMode(); /* Infinite loop */ while (1) { } }
/** * @brief Enters MCU in STANDBY mode. The wake-up from STANDBY mode is performed * when a rising edge is detected on WakeUp pin. * @param None * @retval None */ void LowPower_EnterSTANDBYMode_WAKEUP_1(void) { // LCD_Clear(LCD_COLOR_WHITE); /* Set the LCD Back Color */ // LCD_SetBackColor(LCD_COLOR_BLUE); /* Set the LCD Text Color */ // LCD_SetTextColor(LCD_COLOR_WHITE); // LCD_DisplayStringLine(LCD_LINE_7, " MCU in STANDBY Mode"); // LCD_DisplayStringLine(LCD_LINE_8, " To exit press SEL "); /* Check if the StandBy flag is set */ if (PWR_GetFlagStatus(PWR_FLAG_SB) != RESET) { /* Clear StandBy flag */ PWR_ClearFlag(PWR_FLAG_SB); RTC_WaitForSynchro(); } RTC_AlarmCmd(RTC_Alarm_A, DISABLE); /* Enable WakeUp pin */ PWR_WakeUpPinCmd(PWR_WakeUpPin_1, ENABLE); /* Request to enter STANDBY mode (Wake Up flag is cleared in PWR_EnterSTANDBYMode function) */ PWR_EnterSTANDBYMode(); }
/** * @brief This function configures the system to enter Standby mode for * current consumption measurement purpose. * STANDBY Mode * ============ * - RTC OFF * - IWDG and LSI OFF * - Wakeup using WakeUp Pin2 (PC.13) * @param None * @retval None */ void StandbyMode_Measure(void) { /* Disable wake-up source(Wake up pin) to guarantee free access to WUT level-OR input */ PWR_WakeUpPinCmd(PWR_WakeUpPin_2, DISABLE); /* Clear Wake-up flag */ PWR_ClearFlag(PWR_FLAG_WU); PWR_ClearFlag(PWR_FLAG_SB); /* Enable WKUP pin 1 */ PWR_WakeUpPinCmd(PWR_WakeUpPin_2,ENABLE); /* Request to enter STANDBY mode (Wake Up flag is cleared in PWR_EnterSTANDBYMode function) */ PWR_EnterSTANDBYMode(); /* Infinite loop */ while (1) { } }
void PWR_StandbyMode( void ) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); PWR_WakeUpPinCmd(ENABLE); PWR_BackupAccessCmd(ENABLE); PWR_ClearFlag(PWR_FLAG_SB); PWR_EnterSTANDBYMode(); }
/**************************************************************************** * 名 称:void clock_ini(void) * 功 能:时钟初始化函数 * 入口参数:无 * 出口参数:无 * 说 明: * 调用方法: ****************************************************************************/ void clock_ini(void) { #if defined (STOP_Mode) if(BKP_CheckLOCK_RTC() != BKP_RTC_Flag){ RTC_Configuration(); RTC_InitStructure.RTC_AsynchPrediv = AsynchPrediv; RTC_InitStructure.RTC_SynchPrediv = SynchPrediv; RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; while(RTC_Init(&RTC_InitStructure) == ERROR) {} Set_Time(time); BKP_LOCK_RTC(); }else{ while(RCC_GetFlagStatus(RCC_FLAG_PORRST) == RESET) {} while(RCC_GetFlagStatus(RCC_FLAG_PINRST) == RESET) {} RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); PWR_RTCAccessCmd(ENABLE); RTC_WaitForSynchro(); } RTC_Alarm_Exit(); RTC_NVIC_Configuration(); RTC_ClearFlag(RTC_FLAG_ALRAF); PWR_ClearFlag(PWR_FLAG_WU); #elif defined (TANDBY_Mode) RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); PWR_RTCAccessCmd(ENABLE); PWR_ClearFlag(PWR_FLAG_WU); if(PWR_GetFlagStatus(PWR_FLAG_SB) != RESET) { PWR_ClearFlag(PWR_FLAG_SB); RTC_WaitForSynchro(); }else { RTC_Configuration(); RTC_InitStructure.RTC_AsynchPrediv = AsynchPrediv; RTC_InitStructure.RTC_SynchPrediv = SynchPrediv; RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; while(RTC_Init(&RTC_InitStructure) == ERROR) {} Set_Time(time); RTC_ClearFlag(RTC_FLAG_ALRAF); } #endif }
//进入待机模式 void StandbyMode(void) { //关闭不需要的外设 RCC_AHB1PeriphResetCmd(0X01FF,ENABLE); //复位所有 IO 口 RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);//使能 PWR 时钟 PWR_ClearFlag(PWR_FLAG_WU);//清除 Wake-up 标志 PWR_WakeUpPinCmd(ENABLE);//设置 WKUP 用于唤醒 PWR_EnterSTANDBYMode();//进入待机模式 }
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; }
/** * @brief Enters MCU in STANDBY mode. The wake-up from STANDBY mode is performed * by an RTC Alarm event. * @param None * @retval None */ void LowPower_EnterSTANDBYMode_RTCAlarm(void) { // LCD_Clear(LCD_COLOR_WHITE); /* Set the LCD Back Color */ // LCD_SetBackColor(LCD_COLOR_BLUE); /* Set the LCD Text Color */ // LCD_SetTextColor(LCD_COLOR_WHITE); /* External Interrupt Disable */ //Demo_IntExtOnOffCmd(DISABLE); /* Enable WakeUp pin */ PWR_WakeUpPinCmd(PWR_WakeUpPin_1, ENABLE); /* Check if the StandBy flag is set */ if (PWR_GetFlagStatus(PWR_FLAG_SB) != RESET) { /* Clear StandBy flag */ PWR_ClearFlag(PWR_FLAG_SB); RTC_WaitForSynchro(); } if (RTC_ReadBackupRegister(RTC_BKP_DR0) != 0x5AA5) { // LCD_DisplayStringLine(LCD_LINE_1, "Time and Date are "); // LCD_DisplayStringLine(LCD_LINE_2, "not configured, "); // LCD_DisplayStringLine(LCD_LINE_3, "please go to the "); // LCD_DisplayStringLine(LCD_LINE_4, "calendar menu and "); // LCD_DisplayStringLine(LCD_LINE_5, "set the time and "); // LCD_DisplayStringLine(LCD_LINE_6, "date parameters. "); // LCD_DisplayStringLine(LCD_LINE_7, "Press JoyStick to "); // LCD_DisplayStringLine(LCD_LINE_8, "continue... "); /* External Interrupt Enable */ //Demo_IntExtOnOffCmd(ENABLE); return; } Calendar_AlarmPreAdjust_A(); // LCD_DisplayStringLine(LCD_LINE_7, " MCU in STANDBY Mode"); // LCD_DisplayStringLine(LCD_LINE_8, " Wait For RTC Alarm "); /* Request to enter STANDBY mode (Wake Up flag is cleared in PWR_EnterSTANDBYMode function) */ PWR_EnterSTANDBYMode(); }
/** * @brief This function configures the system to enter Standby mode with RTC * clocked by LSI for current consumption measurement purpose. * STANDBY Mode with RTC clocked by LSI * ======================================== * - RTC Clocked by LSI * - IWDG OFF * - Automatic Wakeup using RTC * @param None * @retval None */ void StandbyRTCMode_Measure(void) { /* 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 Wakeup Interrupt Generation: Clock Source: RTCCLK_Div16, Wakeup Time Base: ~5s RTC Clock Source LSI ~32KHz Wakeup Time Base = (16 / (LSI)) * WakeUpCounter */ RTC_WakeUpClockConfig(RTC_WakeUpClock_RTCCLK_Div16); RTC_SetWakeUpCounter(0x2800-1); /* Enable the Wakeup Interrupt */ RTC_ITConfig(RTC_IT_WUT, ENABLE); /* Enable Wakeup Counter */ RTC_WakeUpCmd(ENABLE); /* Clear Wakeup flag */ PWR_ClearFlag(PWR_FLAG_WU); /* Request to enter STANDBY mode (Wake Up flag is cleared in PWR_EnterSTANDBYMode function) */ PWR_EnterSTANDBYMode(); /* Infinite loop */ while (1) { } }
/** * @brief This function configures the system to enter Standby mode for * current consumption measurement purpose. * STANDBY Mode * ============ * - Backup SRAM and RTC OFF * - IWDG and LSI OFF * - Wakeup using WakeUp Pin (PA.00) * @param None * @retval None */ void StandbyMode_Measure(void) { /* Enable WKUP pin 1 */ PWR_WakeUpPinCmd(ENABLE); /* Clear Power WakeUp (CWUF) pending flag */ PWR_ClearFlag(PWR_FLAG_WU); /* Request to enter STANDBY mode (Wake Up flag is cleared in PWR_EnterSTANDBYMode function) */ PWR_EnterSTANDBYMode(); /* Infinite loop */ while (1) { } }
/** * @brief Configures RTC clock source and prescaler. * @param None * @retval None */ void RTC_Configuration(void) { /* Check if the StandBy flag is set */ if(PWR_GetFlagStatus(PWR_FLAG_SB) != RESET) {/* System resumed from STANDBY mode */ /* Turn on LD4 */ STM32vldiscovery_LEDOn(LED4); /* Clear StandBy flag */ PWR_ClearFlag(PWR_FLAG_SB); /* Wait for RTC APB registers synchronisation */ RTC_WaitForSynchro(); /* No need to configure the RTC as the RTC configuration(clock source, enable, prescaler,...) is kept after wake-up from STANDBY */ } else {/* StandBy flag is not set */ /* RTC clock source configuration ----------------------------------------*/ /* Reset Backup Domain */ BKP_DeInit(); /* Enable 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(); /* Set the RTC time base to 1s */ RTC_SetPrescaler(32767); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); } }
void Set_Alarm_Time(uint8_t Sec) { uint32_t sec_time; RTC_AlarmCmd(RTC_Alarm_A, DISABLE); sec_time = Get_Time().Hour*3600 + Get_Time().Min*60 + Get_Time().Sec + Sec; RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours = (sec_time / 3600) % 24; RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes = (sec_time % 3600) / 60; RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds = sec_time % 60; RTC_AlarmStructure.RTC_AlarmDateWeekDay = Get_Time().Day + (sec_time > 86400 ? 1 : 0); RTC_AlarmStructure.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date; RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_None; RTC_SetAlarm(RTC_Format_BIN, RTC_Alarm_A, &RTC_AlarmStructure); RTC_ITConfig(RTC_IT_ALRA, ENABLE); RTC_AlarmCmd(RTC_Alarm_A, ENABLE); RTC_ClearFlag(RTC_FLAG_ALRAF); PWR_ClearFlag(PWR_FLAG_WU); }
void Platform_Init(void) { /*STM32 wakeup by watchdog in standby mode, re-enter standby mode in this situation*/ PlatformWDGReload(); if ( (PWR_GetFlagStatus(PWR_FLAG_SB) != RESET) && RCC_GetFlagStatus(RCC_FLAG_IWDGRST) != RESET) { RCC_ClearFlag(); Platform_Enter_STANDBY(); } PWR_ClearFlag(PWR_FLAG_SB); mico_rtos_init_mutex(&printf_mutex); Platform_Button_EL_Init(); Platform_Button_STANDBY_Init(); Platform_LED_SYS_Init(); Platform_LED_RF_Init(); Platform_Debug_UART_Init(); }
/** * @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(); } }
/** * @brief This function configures the system to enter Standby mode for * current consumption measurement purpose. * STANDBY Mode * ============ * - RTC OFF * - IWDG and LSI OFF * - Wakeup using WakeUp Pin1 (PA.00) * @param None * @retval None */ void StandbyMode_Measure(void) { /* Check if the StandBy flag is set */ if (PWR_GetFlagStatus(PWR_FLAG_SB) != RESET) { /* Clear StandBy flag */ PWR_ClearFlag(PWR_FLAG_SB); } /* Enable WKUP pin 1 */ PWR_WakeUpPinCmd(PWR_WakeUpPin_1,ENABLE); /* Request to enter STANDBY mode (Wake Up flag is cleared in PWR_EnterSTANDBYMode function) */ PWR_EnterSTANDBYMode(); /* Infinite loop */ while (1) { } }
void EXTI9_5_IRQHandler(void) #endif { if(EXTI_GetITStatus(SEL_BUTTON_EXTI_LINE) != RESET) { /* Clear the SEL Button EXTI line pending bit */ EXTI_ClearITPendingBit(SEL_BUTTON_EXTI_LINE); /* Turn on LED1 */ STM_EVAL_LEDOn(LED1); /* Disable the Alarm A */ RTC_AlarmCmd(RTC_Alarm_A, DISABLE); RTC_GetTime(RTC_Format_BCD, &RTC_TimeStructure); /* Set the alarm X+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_SetAlarm(RTC_Format_BCD, RTC_Alarm_A, &RTC_AlarmStructure); /* Enable RTC Alarm A Interrupt */ RTC_ITConfig(RTC_IT_ALRA, ENABLE); /* Enable the Alarm A */ RTC_AlarmCmd(RTC_Alarm_A, ENABLE); /* Clear RTC Alarm A flag */ RTC_ClearFlag(RTC_FLAG_ALRAF); /* Clear WakeUp flag */ PWR_ClearFlag(PWR_FLAG_WU); /* Request to enter STANDBY mode (Wake Up flag is cleared in PWR_EnterSTANDBYMode function) */ PWR_EnterSTANDBYMode(); } }
void RTC_Alarm_irq(void) { if(RTC_GetITStatus(RTC_IT_ALRA) != RESET) { HAL_RTCAlarm_Handler(); /* Clear EXTI line17 pending bit */ EXTI_ClearITPendingBit(EXTI_Line17); /* Check if the Wake-Up flag is set */ if(PWR_GetFlagStatus(PWR_FLAG_WU) != RESET) { /* Clear Wake Up flag */ PWR_ClearFlag(PWR_FLAG_WU); } /* Clear RTC Alarm interrupt pending bit */ RTC_ClearITPendingBit(RTC_IT_ALRA); } }
/** * @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 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 }
int main(void) { RCC_APB1PeriphClockCmd( RCC_APB1Periph_PWR, ENABLE);//!!!! // тактирование ядра низкоскоростным внутренним генератором 4мгц RCC->ICSCR &= ~RCC_ICSCR_MSIRANGE;//!!!! RCC->ICSCR |= RCC_ICSCR_MSIRANGE_6;//!!!! //RTC_Initilithahion();//!!!! //////////////////////////////////////////////////////////////////// delay_ms(1000);//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! curent_cmd = 0; PCount_Start = 0; /////////////////// настройка пробуждения RCC_HSICmd(DISABLE); PWR_PVDCmd(DISABLE); PWR_UltraLowPowerCmd(ENABLE); PWR_WakeUpPinCmd(PWR_WakeUpPin_1,ENABLE); PWR_UltraLowPowerCmd(ENABLE); ////////////// настройка прирывания для будильника в неспящем режиме EXTI_InitTypeDef exti; NVIC_InitTypeDef NVIC_InitStructure; EXTI_ClearITPendingBit(EXTI_Line17); exti.EXTI_Line = EXTI_Line17; exti.EXTI_Mode = EXTI_Mode_Interrupt; exti.EXTI_Trigger = EXTI_Trigger_Rising; exti.EXTI_LineCmd = ENABLE; EXTI_Init(&exti); 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); ////////////////// if(PWR_GetFlagStatus(PWR_FLAG_SB)!=RESET)//Если МК вышел из режима standby { WkupFlag=1; curent_cmd = 1; if(RTC_GetFlagStatus(RTC_FLAG_ALRAF)!=RESET)//Если МК вышел из режима ALARM { //RTC_Initilithahion();//!!!! //PWR_RTCAccessCmd(ENABLE); //RTC_ClearFlag(RTC_FLAG_ALRAF); //PWR_RTCAccessCmd(DISABLE); //f_WakeupToAlarm=1; curent_cmd = 5; } //RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR,ENABLE); PWR_ClearFlag(PWR_FLAG_SB); //Сброс флага, который указывает на то, что МК вышел из режима standby PWR_RTCAccessCmd(ENABLE); // Доступ в RTC //RTC_ClearITPendingBit(RTC_IT_WUT); //RTC_ClearFlag(RTC_FLAG_WUTF); RTC_ClearFlag(RTC_FLAG_ALRAF); PWR_RTCAccessCmd(DISABLE); } else { WkupFlag=0; RTC_TimeTypeDef alarmTime; alarmTime.RTC_H12 = RTC_H12_PM;//!!!!; alarmTime.RTC_Hours = 20; alarmTime.RTC_Minutes = 0; alarmTime.RTC_Seconds = 20; RTC_Initilithahion();//!!!! //RTC_Set_Alarm(alarmTime);/// Магическая последовательность, 1. RTC_Set_Alarm, 2. RTC_Set_Time иначе не работает будильник RTC_TimeTypeDef RTC_Time; RTC_DateTypeDef RTC_Date; RTC_Time.RTC_H12 = RTC_H12_PM;//!!!!; RTC_Time.RTC_Hours = 20; RTC_Time.RTC_Minutes = 00; RTC_Time.RTC_Seconds = 00; RTC_Date.RTC_WeekDay = RTC_Weekday_Sunday; RTC_Date.RTC_Year = 14; RTC_Date.RTC_Month = 8; RTC_Date.RTC_Date = 17; RTC_Set_TimeDate(RTC_Time, RTC_Date);/// } //else curent_cmd = 0;//f_WakeupToAlarm=0; //RTC_Initilithahion();//!!!! //RTC_Set_Time(); OLED_Init(); OLED_Clear(); char i,j; /* RCC_GetClocksFreq(&Frequency); f=Frequency.SYSCLK_Frequency/1000; IntToStr(a,f); OLED_DrawString_fast(30,30,a,10); */ OLED_LcdCache_Clear(); //if(0==WkupFlag) //Если МК до этого не находился в режиме STANDBY!!!!!!!!!!!!!!!!! //PWR_EnterSTANDBYMode(); //Перейди в режим STANDBY!!!!!!!!!!!!!!!!!!! //OLED_DrawBitmap_fast(3,12,menu1_sprait,123,40); //delay_ms(10000); WaitToSleep = 0; TACT_Config(); BackTimeAndData.hour=0; BackTimeAndData.minute=0; BackTimeAndData.second=0; globa_menu = 1; f_aktive_menu = 0; //curent_cmd = 0; PWatch_Setup_Step = 0; key_up = 0; key_down = 0; key_ok = 0; PCount_Start = 0; while(1) { key = 0; key_ok = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0); key_down = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_2); key_up = GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12); /* OLED_DrawString_fast(0,0," ",10); IntToStr(bbb,key_ok); OLED_DrawString_fast(0,0,bbb,10); IntToStr(bbb,key_down); OLED_DrawString_fast(30,0,bbb,10); IntToStr(bbb,key_up); OLED_DrawString_fast(60,0,bbb,10); */ if (key_ok!=0) { key_ok = 0; key = 1; //OLED_DrawString_fast(0,0,"000000000",10); //f_aktive_menu = 1; /* if (f_aktive_menu == 1) { //f_work_comand = 1; f_aktive_menu = 0; f_clearScrin = 1; curent_cmd = menu_cmd[globa_menu-1]; } else curent_cmd = 255 ; */ } if (key_down==0) { key_down = 0; key = 2; /* if ((globa_menu>1)&&(f_work_comand == 0)) globa_menu--; else globa_menu = 1; //IntToStr(bbb,globa_menu); //OLED_DrawString_fast(0,0,bbb,10); */ } if (key_up==0) { key_up = 0; key = 12; /* if ((globa_menu<4)&&(f_work_comand == 0)) globa_menu++; else globa_menu = 4; //IntToStr(bbb,globa_menu); //OLED_DrawString_fast(0,0,bbb,10); */ } if (key!=0) { GPIO_SetBits(GPIOA, GPIO_Pin_1); delay_ms(30); GPIO_ResetBits(GPIOA, GPIO_Pin_1); } /* if (f_clearScrin==1) { OLED_Clear(); f_clearScrin=0; } */ //if (f_aktive_menu==0) //{ switch(curent_cmd) { case 0: //нет команды PWatch_Menu(); break; case 1: //Программа показа часов (главная страница) //OLED_Clear(); //f_work_comand = 1; PWatch_Anim(); break; case 2: //Программа настройки даты/времени и др. //OLED_Clear(); OLED_DrawString_fast(0,0,"Календарь",10); curent_cmd = 0; break; case 3: //OLED_Clear(); PWatch_Set_Alarm(); break; case 4: //OLED_Clear(); //OLED_DrawString_fast(0,0,"Настройки",10); //f_work_comand = 1; PWatch_Setup(); break; case 5: PWatch_Vibration(); break; case 254: OLED_Clear(); break; /*case 255: OLED_Clear(); OLED_DrawBitmap_fast(3,12,menu1_sprait,123,40); f_aktive_menu = 1; //f_work_comand = 0; //key=0; break; */ default: //выполнить, если ни один вариант не подошел break; } //} //else //{ //PWatch_Menu(); //} /* if(WainToAlarm == 1) { OLED_DrawString_fast(0,0,"alarm1",10); WainToAlarm =0; } */ //if(WaitToSleep>=5) //{ //WaitToSleep=0; //PWR_ClearFlag(PWR_FLAG_SB); //Сброс флага, который указывает на то, что МК вышел из режима standby /* for (i=255; i>0; i--) { OLED_SetContrast(i); IntToStr(a,i); OLED_DrawString_fast(0,0,a,10); //delay_ms(500); }*/ //OLED_DrawBitmap_fast(32,10,Fallout_Bitmaps,58,60); //delay_ms(1000); //OLED_Display_Off();!!!!!!!!!!!!!!!!!!!!!!!!1 //RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, DISABLE); //походу стабилитрон кушает 0.1 мА, общее потребление 0.15мА //PWR_EnterSTANDBYMode();!!!!!!!!!!!!!!!!!!!!! //} //WaitToSleep++; /* RCC_GetClocksFreq(&Frequency); f=Frequency.SYSCLK_Frequency/1000; IntToStr(a,f); OLED_DrawString_fast(0,0,a,10); */ delay_ms(250); } }
/** * @brief This function configures the system to enter Standby mode with RTC * clocked by LSE or LSI and with Backup SRAM ON for current consumption * measurement purpose. * STANDBY Mode with RTC clocked by LSE/LSI and BKPSRAM * ==================================================== * - RTC Clocked by LSE or LSI * - Backup SRAM ON * - IWDG OFF * - Automatic Wakeup using RTC clocked by LSE/LSI (after ~20s) * @param None * @retval None */ void StandbyRTCBKPSRAMMode_Measure(void) { /* 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); #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 RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* Wait for RTC APB registers synchronisation */ RTC_WaitForSynchro(); /* Backup SRAM ***************************************************************/ /* Enable BKPRAM Clock */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_BKPSRAM, ENABLE); /* Enable the Backup SRAM low power Regulator */ PWR_BackupRegulatorCmd(ENABLE); /* Wait until the Backup SRAM low power Regulator is ready */ while(PWR_GetFlagStatus(PWR_FLAG_BRR) == RESET) { } /* RTC Wakeup Interrupt Generation: Clock Source: RTCCLK_Div16, Wakeup Time Base: ~20s RTC Clock Source LSE 32.768KHz or LSI ~32KHz Wakeup Time Base = (16 / (LSE or LSI)) * WakeUpCounter */ RTC_WakeUpClockConfig(RTC_WakeUpClock_RTCCLK_Div16); RTC_SetWakeUpCounter(0xA000-1); /* Disable the Wakeup Interrupt */ RTC_ITConfig(RTC_IT_WUT, DISABLE); /* Clear Power WakeUp (CWUF) pending flag */ PWR_ClearFlag(PWR_FLAG_WU); /* Enable the Wakeup Interrupt */ RTC_ITConfig(RTC_IT_WUT, ENABLE); /* Enable Wakeup Counter */ RTC_WakeUpCmd(ENABLE); /* Clear WakeUp (WUTF) pending flag */ RTC_ClearFlag(RTC_FLAG_WUTF); /* Request to enter STANDBY mode (Wake Up flag is cleared in PWR_EnterSTANDBYMode function) */ PWR_EnterSTANDBYMode(); /* Infinite loop */ while (1) { } }
int main(void) { bool StanbyWakeUp ; float Current_STBY; /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32l1xx_md.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32l1xx.c file */ Int_CurrentSTBY = Current_Measurement(); /* Check if the StandBy flag is set */ if (PWR_GetFlagStatus(PWR_FLAG_SB) != RESET) { /* System resumed from STANDBY mode */ /* Clear StandBy flag */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR,ENABLE); PWR_ClearFlag(PWR_FLAG_SB); StanbyWakeUp = TRUE; } else { StanbyWakeUp = FALSE; } PWR_PVDCmd(DISABLE); RCC_Configuration(); PWR_VoltageScalingConfig(PWR_VoltageScaling_Range1); /* Wait Until the Voltage Regulator is ready */ while (PWR_GetFlagStatus(PWR_FLAG_VOS) != RESET) ; /* Init I/O ports */ Init_GPIOs (); /* Initializes ADC */ ADC_Icc_Init(); enableInterrupts(); /* Warning ! in TSL Init the sysTick interrupt is setted to: SysTick_Config(RCC_Clocks.HCLK_Frequency / 2000 ---> 500 µs*/ /* Init Touch Sensing configuration */ TSL_Init(); sMCKeyInfo[0].Setting.b.IMPLEMENTED = 1; sMCKeyInfo[0].Setting.b.ENABLED = 1; sMCKeyInfo[0].DxSGroup = 0x00; /* Initializes the LCD glass */ LCD_GLASS_Init(); while (1) { switch( State ) { case 0: //LED3 off and LED4 off blink = 0; GPIOB_ODR_value = 0x00000000; ptr_PORTB->GPIOx_ODR = GPIOB_ODR_value; break; case 1: //LED3 on and LED4 off blink = 0; GPIOB_ODR_value = 0x00000080; ptr_PORTB->GPIOx_ODR = GPIOB_ODR_value; break; case 2: //LED3 off and LED4 on blink = 0; GPIOB_ODR_value = 0x00000040; ptr_PORTB->GPIOx_ODR = GPIOB_ODR_value; break; case 3: //LED3 and LED4 blink with 2 second period blink = 1; break; } if(blink == 1) { GPIOB_ODR_value = 0x000000C0; ptr_PORTB->GPIOx_ODR = GPIOB_ODR_value; Delay(100); GPIOB_ODR_value = 0x00000000; ptr_PORTB->GPIOx_ODR = GPIOB_ODR_value; Delay(100); } } return(0); }
int main(void) { bool StanbyWakeUp ; float Current_STBY; /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32l1xx_md.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32l1xx.c file */ Int_CurrentSTBY = Current_Measurement(); /* Check if the StandBy flag is set */ if (PWR_GetFlagStatus(PWR_FLAG_SB) != RESET) { /* System resumed from STANDBY mode */ /* Clear StandBy flag */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR,ENABLE); PWR_ClearFlag(PWR_FLAG_SB); StanbyWakeUp = TRUE; } else { StanbyWakeUp = FALSE; } PWR_PVDCmd(DISABLE); RCC_Configuration(); PWR_VoltageScalingConfig(PWR_VoltageScaling_Range1); /* Wait Until the Voltage Regulator is ready */ while (PWR_GetFlagStatus(PWR_FLAG_VOS) != RESET) ; /* Init I/O ports */ Init_GPIOs (); /* Initializes ADC */ ADC_Icc_Init(); enableInterrupts(); /* Warning ! in TSL Init the sysTick interrupt is setted to: SysTick_Config(RCC_Clocks.HCLK_Frequency / 2000 ---> 500 µs*/ /* Init Touch Sensing configuration */ TSL_Init(); sMCKeyInfo[0].Setting.b.IMPLEMENTED = 1; sMCKeyInfo[0].Setting.b.ENABLED = 1; sMCKeyInfo[0].DxSGroup = 0x00; /* Initializes the LCD glass */ LCD_GLASS_Init(); // EECE 337 Code -- Start int N; int f; char str[8]; N = 10; // Call to original factorial algorithm in C - for debugging //f = factorial_orig( N ); factorial(&f, N); // Copy result to f sprintf (str, "%X", f); //printf( "factorial of %i is %i\n", N, f); LCD_GLASS_DisplayString(str); // EECE 337 Code -- End return(0); }
void RTC_Wakeup_init(void) { NVIC_InitTypeDef NVIC_InitStructure; EXTI_InitTypeDef EXTI_InitStructure; RTC_InitTypeDef RTC_InitStruct; 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 ); /* Enable the PWR clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* RTC clock source configuration ------------------------------------------*/ /* Allow access to BKP Domain */ PWR_BackupAccessCmd(ENABLE); #ifdef USE_RTC_BKP 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(); RTC_WakeUpCmd( DISABLE ); EXTI_ClearITPendingBit( RTC_INTERRUPT_EXTI_LINE ); PWR_ClearFlag(PWR_FLAG_WU); RTC_ClearFlag(RTC_FLAG_WUTF); RTC_WakeUpClockConfig(RTC_WakeUpClock_RTCCLK_Div2); EXTI_ClearITPendingBit( RTC_INTERRUPT_EXTI_LINE ); EXTI_InitStructure.EXTI_Line = RTC_INTERRUPT_EXTI_LINE; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); 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_ITConfig(RTC_IT_WUT, DISABLE); /* Prepare Stop-Mode but leave disabled */ PWR_ClearFlag(PWR_FLAG_WU); PWR->CR |= PWR_CR_LPDS; PWR->CR &= (unsigned long)(~(PWR_CR_PDDS)); SCB->SCR |= ((unsigned long)SCB_SCR_SLEEPDEEP_Msk); #ifdef USE_RTC_BKP if (RTC_ReadBackupRegister(RTC_BKP_DR0) != USE_RTC_BKP) { /* set it to 12:20:30 08/04/2013 monday */ MicoRtcSetTime(&mico_default_time); RTC_WriteBackupRegister(RTC_BKP_DR0, USE_RTC_BKP); } #else //#ifdef RTC_ENABLED /* application must have wiced_application_default_time structure declared somewhere, otherwise it wont compile */ /* write default application time inside rtc */ MicoRtcSetTime(&mico_default_time); //#endif /* RTC_ENABLED */ #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(); }
static unsigned long stop_mode_power_down_hook( unsigned long sleep_ms ) { unsigned long retval; unsigned long wut_ticks_passed; unsigned long scale_factor = 0; UNUSED_PARAMETER(sleep_ms); UNUSED_PARAMETER(rtc_timeout_start_time); UNUSED_PARAMETER(scale_factor); if ( ( ( SCB->SCR & (unsigned long)SCB_SCR_SLEEPDEEP_Msk) != 0) && sleep_ms < 5 ){ SCB->SCR &= (~((unsigned long)SCB_SCR_SLEEPDEEP_Msk)); __asm("wfi"); SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk; /* Note: We return 0 ticks passed because system tick is still going when wfi instruction gets executed */ ENABLE_INTERRUPTS; return 0; } if ( ( ( SCB->SCR & (unsigned long)SCB_SCR_SLEEPDEEP_Msk) ) != 0 ) { /* pick up the appropriate prescaler for a requested delay */ select_wut_prescaler_calculate_wakeup_time(&rtc_timeout_start_time, sleep_ms, &scale_factor ); DISABLE_INTERRUPTS; SysTick->CTRL &= (~(SysTick_CTRL_TICKINT_Msk|SysTick_CTRL_ENABLE_Msk)); /* systick IRQ off */ RTC_ITConfig(RTC_IT_WUT, ENABLE); EXTI_ClearITPendingBit( RTC_INTERRUPT_EXTI_LINE ); PWR_ClearFlag(PWR_FLAG_WU); RTC_ClearFlag(RTC_FLAG_WUTF); RTC_SetWakeUpCounter( rtc_timeout_start_time ); RTC_WakeUpCmd( ENABLE ); platform_rtc_enter_powersave(); DBGMCU->CR |= 0x03; /* Enable debug in stop mode */ /* This code will be running with BASEPRI register value set to 0, the main intention behind that is that */ /* all interrupts must be allowed to wake the CPU from the power-down mode */ /* the PRIMASK is set to 1( see DISABLE_INTERRUPTS), thus we disable all interrupts before entering the power-down mode */ /* This may sound contradictory, however according to the ARM CM3 documentation power-management unit */ /* takes into account only the contents of the BASEPRI register and it is an external from the CPU core unit */ /* PRIMASK register value doesn't affect its operation. */ /* So, if the interrupt has been triggered just before the wfi instruction */ /* it remains pending and wfi instruction will be treated as a nop */ __asm("wfi"); /* After CPU exits powerdown mode, the processer will not execute the interrupt handler(PRIMASK is set to 1) */ /* Disable rtc for now */ RTC_WakeUpCmd( DISABLE ); RTC_ITConfig(RTC_IT_WUT, DISABLE); /* Initialise the clocks again */ init_clocks( ); /* Enable CPU ticks */ SysTick->CTRL |= (SysTick_CTRL_TICKINT_Msk|SysTick_CTRL_ENABLE_Msk); /* Get the time of how long the sleep lasted */ wut_ticks_passed = rtc_timeout_start_time - RTC_GetWakeUpCounter(); UNUSED_VARIABLE(wut_ticks_passed); platform_rtc_exit_powersave( sleep_ms, (uint32_t *)&retval ); /* as soon as interrupts are enabled, we will go and execute the interrupt handler */ /* which triggered a wake up event */ ENABLE_INTERRUPTS; wake_up_interrupt_triggered = false; UNUSED_VARIABLE(wake_up_interrupt_triggered); return retval; } else { UNUSED_PARAMETER(wut_ticks_passed); ENABLE_INTERRUPTS; __asm("wfi"); /* Note: We return 0 ticks passed because system tick is still going when wfi instruction gets executed */ return 0; } }
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_stm32l1xx_md.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32l1xx.c file */ /* Configure Clocks for Application need */ RCC_Configuration(); /* Configure RTC Clocks */ RTC_Configuration(); /* Set internal voltage regulator to 1.8V */ PWR_VoltageScalingConfig(PWR_VoltageScaling_Range1); /* Wait Until the Voltage Regulator is ready */ while (PWR_GetFlagStatus(PWR_FLAG_VOS) != RESET) ; /* Enable debug features in low power modes (Sleep, STOP and STANDBY) */ #ifdef DEBUG_SWD_PIN DBGMCU_Config(DBGMCU_SLEEP | DBGMCU_STOP | DBGMCU_STANDBY, ENABLE); #endif /* Configure SysTick IRQ and SysTick Timer to generate interrupts */ RCC_GetClocksFreq(&RCC_Clocks); SysTick_Config(RCC_Clocks.HCLK_Frequency / 500); /* Init I/O ports */ Init_GPIOs(); /* Initializes the LCD glass */ LCD_GLASS_Configure_GPIO(); LCD_GLASS_Init(); /* Display Welcome message */ LCD_GLASS_ScrollSentence(" ** TEMPERATURE SENSOR EXAMPLE ** ",1,SCROLL_SPEED); /* Disable SysTick IRQ and SysTick Timer */ SysTick->CTRL &= ~ ( SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk ); /* Test user or factory temperature sensor calibration value */ if ( testFactoryCalibData() == SUCCESS ) getFactoryTSCalibData(&calibdata); else if ( testUserCalibData() == SUCCESS ) calibdata = *USER_CALIB_DATA; else { /* User calibration or factory calibration TS data are not available */ calibdata.TS_CAL_1 = DEFAULT_COLD_VAL; calibdata.TS_CAL_2 = DEFAULT_HOT_VAL; writeCalibData(&calibdata); calibdata = *USER_CALIB_DATA; } /* Configure Wakeup from sleep using RTC event*/ configureWakeup(); /* Configure direct memory access for ADC usage*/ configureDMA(); /* Configure ADC for temperature sensor value conversion */ configureADC_Temp(); while(1) { /* Re-enable DMA and ADC conf and start Temperature Data acquisition */ acquireTemperatureData(); /* Stay in SLEEP mode untill the data are acquired by ADC */ __WFI(); /* for DEBUG purpose uncomment the following line and comment the __WFI call to do not enter STOP mode */ // while (!flag_ADCDMA_TransferComplete); /* Disable ADC, DMA and clock*/ powerDownADC_Temper(); /* Process mesured Temperature data - calculate average temperature value in °C */ processTempData(); if (flag_UserButton == TRUE) { clearUserButtonFlag(); if (CurrentlyDisplayed == Display_TemperatureDegC) CurrentlyDisplayed = Display_ADCval; else CurrentlyDisplayed = Display_TemperatureDegC; } if (CurrentlyDisplayed == Display_TemperatureDegC) { /* print average temperature value in °C */ sprintf(strDisp, "%d °C", temperature_C ); } else { /* print result of ADC conversion */ sprintf(strDisp, "> %d", tempAVG ); } LCD_GLASS_Clear(); LCD_GLASS_DisplayString( (unsigned char *) strDisp ); /* Enable RTC Wakeup */ RTC_WakeUpCmd(ENABLE); /* Clear WakeUp flag */ PWR_ClearFlag(PWR_FLAG_WU); /* Enter in wait for interrupt stop mode*/ PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI); RCC_Configuration(); // reinitialize clock /* After Wake up : Disable Wake up from RTC*/ RTC_WakeUpCmd(DISABLE); } }
/** * @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); }