void RTC_Alarm_IRQHandler() { if(RTC_GetITStatus(RTC_IT_ALRA) != RESET) { //RTC_AlarmCmd(RTC_Alarm_A, ENABLE); PWR_RTCAccessCmd(ENABLE); // Доступ в RTC RTC_ClearITPendingBit(RTC_IT_ALRA); RTC_ClearFlag(RTC_FLAG_ALRAF); PWR_RTCAccessCmd(DISABLE); EXTI_ClearITPendingBit(EXTI_Line17); //OLED_DrawString_fast(0,0,"alarm",10); } /////////////////////////????????????????????????????????????????????????????????????????????????????????????? if(RTC_GetFlagStatus(RTC_FLAG_ALRAF) != RESET) { PWR_RTCAccessCmd(ENABLE); RTC_ClearFlag(RTC_FLAG_ALRAF); PWR_RTCAccessCmd(DISABLE); //f_WakeupToAlarm=1; curent_cmd = 5; } ///////////////////////// }
/** * @brief This function handles RTC Tamper and Time Stamp interrupts requests. * @param None * @retval None */ void TAMP_STAMP_IRQHandler(void) { if(RTC_GetFlagStatus(RTC_FLAG_TAMP1F) != RESET) { /* Tamper 1 detection event occurred */ /* Check if RTC Backup Data registers are cleared */ if(IsBackupRegReset() == 0) { /* OK, RTC Backup Data registers are reset as expected */ /* Toggle LED2 */ STM_EVAL_LEDToggle(LED2); } else { /* RTC Backup Data registers are not reset */ /* Toggle LED4 */ STM_EVAL_LEDToggle(LED4); } /* Clear Tamper 1 pin Event pending flag */ RTC_ClearFlag(RTC_FLAG_TAMP1F); /* Disable Tamper pin 1 */ RTC_TamperCmd(RTC_Tamper_1, DISABLE); /* Enable Tamper pin */ RTC_TamperCmd(RTC_Tamper_1, ENABLE); } }
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 Main program. * @param None * @retval None */ int main(void) { /* Setup the microcontroller system. Initialize the Embedded Flash Interface, initialize the PLL and update the SystemFrequency variable. */ SystemInit(); /* Initialize LEDs and Key Button mounted on STM3210X-EVAL board */ STM_EVAL_LEDInit(LED1); STM_EVAL_LEDInit(LED2); STM_EVAL_LEDInit(LED3); STM_EVAL_PBInit(Button_KEY, Mode_EXTI); /* Enable PWR and BKP clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); /* Configure EXTI Line to generate an interrupt on falling edge */ EXTI_Configuration(); /* Configure RTC clock source and prescaler */ RTC_Configuration(); /* NVIC configuration */ NVIC_Configuration(); /* Configure the SysTick to generate an interrupt each 1 millisecond */ SysTick_Configuration(); /* Turn on LED1 */ STM_EVAL_LEDOn(LED1); while (1) { /* Insert 1.5 second delay */ Delay(1500); /* Wait till RTC Second event occurs */ RTC_ClearFlag(RTC_FLAG_SEC); while(RTC_GetFlagStatus(RTC_FLAG_SEC) == RESET); /* Alarm in 3 second */ RTC_SetAlarm(RTC_GetCounter()+ 3); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Turn off LED1 */ STM_EVAL_LEDOff(LED1); /* Request to enter STOP mode with regulator in low power mode*/ PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI); /* At this stage the system has resumed from STOP mode -------------------*/ /* Turn on LED1 */ STM_EVAL_LEDOn(LED1); /* Configures system clock after wake-up from STOP: enable HSE, PLL and select PLL as system clock source (HSE and PLL are disabled in STOP mode) */ SYSCLKConfig_STOP(); } }
/******************************************************************************* * Function Name : main * Description : Main program. * Input : None * Output : None * Return : None *******************************************************************************/ int main(void) { #ifdef DEBUG debug(); #endif /* Clock configuration */ RCC_Configuration(); /* Enable PWR and BKP clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); /* GPIO configuration */ GPIO_Configuration(); /* Configure EXTI Line to generate an interrupt on falling edge */ EXTI_Configuration(); /* Configure RTC clock source and prescaler */ RTC_Configuration(); /* NVIC configuration */ NVIC_Configuration(); /* Configure the SysTick to generate an interrupt each 1 millisecond */ SysTick_Configuration(); /* Turn on led connected to GPIO_LED Pin6 */ GPIO_SetBits(GPIO_LED, GPIO_Pin_6); while (1) { /* Insert 1.5 second delay */ Delay(1500); /* Wait till RTC Second event occurs */ RTC_ClearFlag(RTC_FLAG_SEC); while(RTC_GetFlagStatus(RTC_FLAG_SEC) == RESET); /* Alarm in 3 second */ RTC_SetAlarm(RTC_GetCounter()+ 3); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Turn off led connected to GPIO_LED Pin6 */ GPIO_ResetBits(GPIO_LED, GPIO_Pin_6); /* Request to enter STOP mode with regulator in low power mode*/ PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI); /* At this stage the system has resumed from STOP mode -------------------*/ /* Turn on led connected to GPIO_LED Pin6 */ GPIO_SetBits(GPIO_LED, GPIO_Pin_6); /* Configures system clock after wake-up from STOP: enable HSE, PLL and select PLL as system clock source (HSE and PLL are disabled in STOP mode) */ SYSCLKConfig_STOP(); } }
/******************************************************************************* * Function Name : RTC_IRQHandler * Description : This function handles RTC global interrupt request. * Input : None * Output : None * Return : None *******************************************************************************/ void RTC_IRQHandler(void) { if(RTC_GetFlagStatus(RTC_IT_ALR) != RESET) { RTC_ClearITPendingBit(RTC_IT_ALR); //Clear RTC Alarm interrupt pending bit RTC_WaitForLastTask(); //Wait until last write operation on RTC registers has finished } }
void RTC_STOP_WakeUp(void) { /* Enable PWR and BKP clocks */ /* PWR时钟(电源控制)与BKP时钟(RTC后备寄存器)使能 */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); /* Allow access to BKP Domain */ PWR_BackupAccessCmd(ENABLE); /* 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(); /* Enable the RTC Alarm interrupt */ RTC_ITConfig(RTC_IT_ALR, ENABLE); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); RTC_ClearFlag(RTC_FLAG_SEC); while(RTC_GetFlagStatus(RTC_FLAG_SEC) == RESET); /* Alarm in 5 second */ RTC_SetAlarm(RTC_GetCounter()+ 5); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); }
/** * @} */ void RTC_IRQHandler(void) { static int delay_cnt; struct rtgui_event_command ecmd; /* enter interrupt */ rt_interrupt_enter(); ++rtc_1s_int_cnt; #if 1 == LCD_TEST_MALOOEI rt_kprintf("rtc:%d\n", rtc_1s_int_cnt); #endif if (delay_cnt < 1) { ++delay_cnt; goto ret_entry; } if (NULL == wb_thread) goto ret_entry; if (RTC_GetITStatus(RTC_IT_SEC) != RESET) { RTGUI_EVENT_COMMAND_INIT(&ecmd); ecmd.command_id = UPDATE_SYS_TIME; if (RT_EOK != rtgui_thread_send(wb_thread, &ecmd.parent, sizeof(struct rtgui_event_command))) rt_kprintf("[RTC_IRQHandler]send enent cmd fail\n"); } #if 0 if(RTC_GetFlagStatus(RTC_FLAG_ALR) != RESET) { } else { } #endif ret_entry: RTC_ClearITPendingBit(RTC_IT_ALR | RTC_IT_SEC); /* leave interrupt */ rt_interrupt_leave(); return; }
/** * @brief This function handles External lines 9 to 5 interrupt request. * @param None * @retval None */ void EXTI9_5_IRQHandler(void) { if(EXTI_GetITStatus(KEY_BUTTON_EXTI_LINE) != RESET) { /* Clear the Key Button EXTI line pending bit */ EXTI_ClearITPendingBit(KEY_BUTTON_EXTI_LINE); /* Turn on LED1 */ STM_EVAL_LEDOn(LED1); /* Wait till RTC Second event occurs */ RTC_ClearFlag(RTC_FLAG_SEC); while(RTC_GetFlagStatus(RTC_FLAG_SEC) == RESET); /* Set the RTC Alarm after 3s */ RTC_SetAlarm(RTC_GetCounter()+ 3); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Request to enter STANDBY mode (Wake Up flag is cleared in PWR_EnterSTANDBYMode function) */ PWR_EnterSTANDBYMode(); } }
void TIM2_IRQHandler(void) { if ( TIM_GetITStatus(TIM2 , TIM_IT_Update) != RESET ) { TIM_ClearITPendingBit(TIM2 , TIM_FLAG_Update); ///////////////////////////////////////////////////////////////// // Power OFF GPS and GSM before go into standby mode ///////////////////////////////////////////////////////////////// GSM_TurnOnOff_delay(); #ifdef USE_STM32_GPS_BOARD_VB GPSPowerOff(); GSM_PowerOff(); #endif /* Wait till RTC Second event occurs */ RTC_ClearFlag(RTC_FLAG_SEC); while(RTC_GetFlagStatus(RTC_FLAG_SEC) == RESET); // normal working state if(BKP_TRUE == BKP_ReadBackupRegister(BKP_DR1)) { /* Set the RTC Alarm after xx s */ DEBUG("in timer2 bkptrue %d\n", (BKP_ReadBackupRegister(BKP_DR4) + BKP_ReadBackupRegister(BKP_DR4) <<16)); RTC_SetAlarm(RTC_GetCounter()+ ((BKP_ReadBackupRegister(BKP_DR4) + BKP_ReadBackupRegister(BKP_DR4) <<16)/SLEEP_TIM2_RATIO)); } else { DEBUG("in timer2 bkpfalse\n"); RTC_SetAlarm(RTC_GetCounter()+ (SLEEP_NORMAL_SEC/SLEEP_TIM2_RATIO)); } /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Request to enter STANDBY mode (Wake Up flag is cleared in PWR_EnterSTANDBYMode function) */ PWR_EnterSTANDBYMode(); } }
/** * @brief This function handles Tamper interrupt request. * @param None * @retval None */ void TAMPER_STAMP_IRQHandler(void) { if (RTC_GetFlagStatus(RTC_FLAG_TAMP1F) != RESET) { /* Tamper 1 detection event occurred */ /* Check if RTC Backup Data registers are cleared */ if (IsBackupRegReset() == 0) { /* OK, RTC Backup Data registers are reset as expected */ /* Turn on GREEN and BLUE */ GPIO_SetBits(GPIOB, LD_GREEN_GPIO_PIN | LD_BLUE_GPIO_PIN); } else { /* RTC Backup Data registers are not reset */ /* Turn off GREEN and BLUE */ GPIO_ResetBits(GPIOB, LD_GREEN_GPIO_PIN | LD_BLUE_GPIO_PIN); } /* Clear Tamper 1 pin Event(TAMP1F) pending flag */ RTC_ClearFlag(RTC_FLAG_TAMP1F); /* Disable Tamper pin 1 */ RTC_TamperCmd(RTC_Tamper_1, DISABLE); /* Enable Tamper pin */ RTC_TamperCmd(RTC_Tamper_1, ENABLE); } }
/* Private functions ---------------------------------------------------------------------------------------*/ void RTC_IRQHandler(void) { u8 bFlags ; bFlags = RTC_GetFlagStatus(); if((bFlags & 0x2) != 0x0) /* Match flag */ { /* Reset RTC init time when Time is 23:59:59 */ RTC_SetCompare(86400) ; PWRCU_WriteBackupRegister(PWRCU_BAKREG_1, 0); } /* Toggle LED3 each 1s */ if(led_on) { GPIO_SetOutBits(GPIOA, GPIO_PIN_5); led_on = 0; } else { GPIO_ClearOutBits(GPIOA, GPIO_PIN_5); led_on = 1; } /* Enable time update */ gwTimeDisplay = 1; }
// 设定闹钟时间 void SetAlarmTime(void) { // 23:59:59 struct tm time_current; uint32_t DayTotalSeconds; // 一天累积总秒数 uint32_t CurrentTotalSeconds; // 当前时间累积总秒数 uint32_t AlarmValue; // 秒差 DayTotalSeconds = 23*3600 + 59*60 +59; time_current = Time_GetCalendarTime(); CurrentTotalSeconds = time_current.tm_hour*3600 + time_current.tm_min*60 + time_current.tm_sec; AlarmValue = DayTotalSeconds - CurrentTotalSeconds; /* Wait till RTC Second event occurs */ RTC_ClearFlag(RTC_FLAG_SEC); while(RTC_GetFlagStatus(RTC_FLAG_SEC) == RESET); /* Set the RTC Alarm after 3s */ RTC_SetAlarm(RTC_GetCounter()+AlarmValue); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); }
// Get elapsed number of seconds static int rtc_status( lua_State *L ) { lua_pushinteger(L, (lua_Integer)RTC_GetFlagStatus()); return 1; }
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 Enter the MCU selected low power modes. * @param lpmode: selected MCU low power modes. This parameter can be one of the * following values: * @arg STM32L_RUN: Run mode at 32MHz. * @arg STM32L_RUN_1M: Run mode at 1MHz. * @arg STM32L_RUN_LP: Low power Run mode at 32KHz. * @arg STM32L_SLEEP: Sleep mode at 16MHz. * @arg STM32L_SLEEP_LP: Low power Sleep mode at 32KHz. * @arg STM32L_STOP: Stop mode with or without RTC. * @arg STM32L_STANDBY: Standby mode with or without RTC. * @param RTCState: RTC peripheral state during low power modes. This parameter * is valid only for STM32L_RUN_LP, STM32L_SLEEP_LP, STM32L_STOP and * STM32L_STANDBY. This parameter can be one of the following values: * @arg RTC_STATE_ON: RTC peripheral is ON during low power modes. * @arg RTC_STATE_OFF: RTC peripheral is OFF during low power modes. * @param CalibrationState: Bias Calibration mode selection state during low * power modes. * This parameter can be one of the following values: * @arg BIAS_CALIB_OFF: Bias Calibration mode is selected during * low power modes. * @arg BIAS_CALIB_ON: Bias Calibration mode isn't selected during * low power modes. * @retval None */ void IDD_Measurement(uint32_t lpmode, uint8_t RTCState, uint8_t CalibrationState) { GPIO_InitTypeDef GPIO_InitStructure; uint16_t mode = STM32L_MODE_LP, adcdata, i; /* Disable the Wakeup Interrupt */ RTC_ITConfig(RTC_IT_WUT, DISABLE); /* Disable the JoyStick interrupts */ Demo_IntExtOnOffConfig(DISABLE); /* Disable Leds toggling */ Demo_LedShow(DISABLE); /* Save the RCC configuration */ RCC_AHBENR = RCC->AHBENR; RCC_APB2ENR = RCC->APB2ENR; RCC_APB1ENR = RCC->APB1ENR; /* Disable PVD */ PWR_PVDCmd(DISABLE); /* Wait until JoyStick is pressed */ while (Menu_ReadKey() != NOKEY) {} /* Save the GPIO pins current configuration then put all GPIO pins in Analog Input mode ...*/ IDD_Measurement_SaveContext(); /* Clear Wake Up flag */ PWR_ClearFlag(PWR_FLAG_WU); RCC->AHBENR = 0x05; RCC->APB2ENR = 0x00; RCC->APB1ENR = 0x10000000; /* PWR APB1 Clock enable */ switch(lpmode) { /*=========================================================================* * RUN MODE 32MHz (HSE + PLL) * *========================================================================*/ case STM32L_RUN: { mode = STM32L_MODE_RUN; /* Needed delay to have a correct value on capacitor C25. Running NOP during waiting loop will decrease the current consumption. */ for (i = 0;i < 0xFFFF; i++) { __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); } } break; /*=========================================================================* * RUN MODE MSI 1MHz * *========================================================================*/ case STM32L_RUN_1M: { mode = STM32L_MODE_RUN; /* Reconfigure the System Clock at MSI 1 MHz */ SetHCLKToMSI_1MHz(); /* Needed delay to have a correct value on capacitor C25. Running NOP during waiting loop will decrease the current consumption. */ for (i = 0;i < 0x3FFF; i++) { __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); } } break; /*=========================================================================* * RUN LOW POWER MODE MSI 32KHz * *========================================================================*/ case STM32L_RUN_LP: { if(!RTCState) { RCC_LSEConfig(RCC_LSE_OFF); } else { if (RTC_GetFlagStatus(RTC_FLAG_INITS) == RESET) { /* RTC Configuration ************************************************/ /* Reset RTC Domain */ RCC_RTCResetCmd(ENABLE); RCC_RTCResetCmd(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); /* Enable the RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* Wait for RTC APB registers synchronisation */ RTC_WaitForSynchro(); } } /* Configure the System Clock at MSI 32 KHz */ SetHCLKToMSI_64KHz(); RCC_HCLKConfig(RCC_SYSCLK_Div2); /* Clear IDD_CNT_EN pin */ GPIO_ResetBits(IDD_CNT_EN_GPIO_PORT, IDD_CNT_EN_PIN); /* Enter low power run mode */ PWR_EnterLowPowerRunMode(ENABLE); /* Waiting wake-up interrupt */ /* Needed delay to have a correct value on capacitor C25. Running NOP during waiting loop will decrease the current consumption. */ do { __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); }while(LowPowerStatus == 0x00); /* Exit low power run mode before setting the clock to 32MHz */ PWR_EnterLowPowerRunMode(DISABLE); while(PWR_GetFlagStatus(PWR_FLAG_REGLP) != RESET) { } } break; /*=========================================================================* * SLEEP MODE HSI 16MHz * *========================================================================*/ case STM32L_SLEEP: { mode = STM32L_MODE_RUN; /* Enable Ultra low power mode */ PWR_UltraLowPowerCmd(ENABLE); /* Diable FLASH during SLeep LP */ FLASH_SLEEPPowerDownCmd(ENABLE); RCC_APB2PeriphClockLPModeCmd(RCC_APB2Periph_CLOCK, ENABLE); RCC_APB1PeriphClockLPModeCmd(RCC_APB1Periph_CLOCK, ENABLE); RCC_AHBPeriphClockLPModeCmd(RCC_AHBPeriph_CLOCK, ENABLE); /* Configure the System Clock to 16MHz */ SetHCLKToHSI(); Demo_SysTickConfig(); Demo_Delay(5); /* Request to enter SLEEP mode with regulator on */ PWR_EnterSleepMode(PWR_Regulator_ON, PWR_STOPEntry_WFI); } break; /*=========================================================================* * SLEEP LOW POWER MODE MSI 32KHz * *========================================================================*/ case STM32L_SLEEP_LP: { if(!RTCState) { RCC_LSEConfig(RCC_LSE_OFF); } else { if (RTC_GetFlagStatus(RTC_FLAG_INITS) == RESET) { /* RTC Configuration ************************************************/ /* Reset RTC Domain */ RCC_RTCResetCmd(ENABLE); RCC_RTCResetCmd(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); /* Enable the RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* Wait for RTC APB registers synchronisation */ RTC_WaitForSynchro(); } } /* Enable Ultra low power mode */ PWR_UltraLowPowerCmd(ENABLE); /* Diable FLASH during SLeep LP */ FLASH_SLEEPPowerDownCmd(ENABLE); /* Disable HSI clock before entering Sleep LP mode */ RCC_HSICmd(DISABLE); /* Disable HSE clock */ RCC_HSEConfig(RCC_HSE_OFF); /* Disable LSI clock */ RCC_LSICmd(DISABLE); RCC_APB2PeriphClockLPModeCmd(RCC_APB2Periph_CLOCK, ENABLE); RCC_APB1PeriphClockLPModeCmd(RCC_APB1Periph_CLOCK, ENABLE); RCC_AHBPeriphClockLPModeCmd(RCC_AHBPeriph_CLOCK, ENABLE); /* Clear IDD_CNT_EN pin */ GPIO_ResetBits(IDD_CNT_EN_GPIO_PORT, IDD_CNT_EN_PIN); /* Reconfigure the System Clock at MSI 64 KHz */ SetHCLKToMSI_64KHz(); RCC_HCLKConfig(RCC_SYSCLK_Div2); /* Request to enter SLEEP mode with regulator low power */ PWR_EnterSleepMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI); } break; /*=========================================================================* * STOP LOW POWER MODE * *========================================================================*/ case STM32L_STOP: { /* Enable Ultra low power mode */ PWR_UltraLowPowerCmd(ENABLE); if(!RTCState) { RCC_LSEConfig(RCC_LSE_OFF); } else { if (RTC_GetFlagStatus(RTC_FLAG_INITS) == RESET) { /* RTC Configuration ************************************************/ /* Reset RTC Domain */ RCC_RTCResetCmd(ENABLE); RCC_RTCResetCmd(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); /* Enable the RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* Wait for RTC APB registers synchronisation */ RTC_WaitForSynchro(); } } /* Clear IDD_CNT_EN pin */ GPIO_ResetBits(IDD_CNT_EN_GPIO_PORT, IDD_CNT_EN_PIN); /* Request to enter STOP mode with regulator in low power */ PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI); /* Reset the counter by programming IDD_CNT_EN High in less than 70ms after the wakeup to avoid 1Kohm to be connected later on VDD_MCU */ GPIO_SetBits(IDD_CNT_EN_GPIO_PORT, IDD_CNT_EN_PIN); /* Configures system clock after wake-up from STOP: enable HSE, PLL and select PLL as system clock source (HSE and PLL are disabled in STOP mode) */ IDD_Measurement_SYSCLKConfig_STOP(); } break; /*=========================================================================* * STANDBY LOW POWER MODE * *========================================================================*/ case STM32L_STANDBY: { if (RTC_GetFlagStatus(RTC_FLAG_INITS) == RESET) { /* RTC Configuration **************************************************/ /* Reset RTC Domain */ RCC_RTCResetCmd(ENABLE); RCC_RTCResetCmd(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); /* Enable the RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* Wait for RTC APB registers synchronisation */ RTC_WaitForSynchro(); } RTC_OutputTypeConfig(RTC_OutputType_PushPull); RTC_OutputConfig(RTC_Output_WakeUp, RTC_OutputPolarity_High); /* To configure PC13 WakeUP output */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_400KHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOC, GPIO_PinSource13, GPIO_AF_RTC_AF1); PWR_WakeUpPinCmd(PWR_WakeUpPin_1, ENABLE); PWR_UltraLowPowerCmd(ENABLE); RTC_ClearFlag(RTC_FLAG_WUTF | RTC_FLAG_ALRBF | RTC_FLAG_ALRAF | RTC_FLAG_TAMP1F | RTC_FLAG_TSF); RTC_ITConfig(RTC_IT_WUT, DISABLE); if(!RTCState) { RCC_LSEConfig(RCC_LSE_OFF); } /* Clear Wake Up flag */ PWR_ClearFlag(PWR_FLAG_WU); /* Request to enter STANDBY mode (Wake Up flag is cleared in PWR_EnterSTANDBYMode function) */ PWR_EnterSTANDBYMode(); } break; } /* Configure the System Clock to 32MHz */ SetHCLKTo32(); /* Reset lowpower status variable*/ LowPowerStatus = 0x00; RCC->AHBENR = RCC_AHBENR; RCC->APB2ENR = RCC_APB2ENR; RCC->APB1ENR = RCC_APB1ENR; /* Reset the counter by programming IDD_CNT_EN High in less than 70ms after the wakeup to avoid 1Kohm to be connected later on VDD_MCU */ GPIO_SetBits(IDD_CNT_EN_GPIO_PORT, IDD_CNT_EN_PIN); /* Measure the Voltage using the ADC */ adcdata = IDD_Measurement_ADC_ReadValue(); /* Write the ADC converted value in the DATA EEPROM memory for Bias Measurement */ if(CalibrationState == BIAS_CALIB_ON) { /* Unlock EEPROM write access*/ DATA_EEPROM_Unlock(); /* Store the value in EEPROM for application needs */ DATA_EEPROM_ProgramHalfWord(DATA_EEPROM_BIAS_ADDR, adcdata); /* Lock back EEPROM write access */ DATA_EEPROM_Lock(); } IDD_Measurement_ADC_DisplayValue(adcdata, mode); /* Clear Wake Up flag */ PWR_ClearFlag(PWR_FLAG_WU); /* Enable PVD */ PWR_PVDCmd(ENABLE); /* Restore Demonstration Context. */ IDD_Measurement_RestoreContext(); LCD_SetBackColor(LCD_COLOR_GREEN); LCD_DisplayStringLine(LCD_LINE_6, "STM32L LowPower Mode"); LCD_DisplayStringLine(LCD_LINE_7, Str); LCD_DisplayStringLine(LCD_LINE_8, "Press JoyStick to "); LCD_DisplayStringLine(LCD_LINE_9, "continue. "); /* Wait until Joystick pressed. */ while (Menu_ReadKey() == NOKEY) {} /* Disable ADC1 */ ADC_Cmd(ADC1, DISABLE); LCD_Clear(LCD_COLOR_WHITE); LCD_GLASS_DisplayString(" STM32L "); /* Enable the Wakeup Interrupt */ RTC_ITConfig(RTC_IT_WUT, ENABLE); /* Enable the JoyStick interrupts */ Demo_IntExtOnOffConfig(ENABLE); /* Display the previous menu */ Menu_DisplayMenu(); }
void initUart() { RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_StructInit(&GPIO_InitStructure); // Transmit pin. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9, GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); // Receive pin. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); // Default clock structure works for us. USART_ClockInitTypeDef USART_ClockInitStructure; USART_ClockStructInit(&USART_ClockInitStructure); USART_ClockInit(USART1, &USART_ClockInitStructure); // Default USART structure works for us. USART_InitTypeDef USART_InitStructure; USART_StructInit(&USART_InitStructure); USART_Init(USART1, &USART_InitStructure); USART_Cmd(USART1, ENABLE); // // Disable data pushes from GPS. // usartTx("$PUBX,40,GGA,0,0,0,0*5A\r\n"); // usartTx("$PUBX,40,GGA,0,0,0,0*5A\r\n"); // Repeat first; timing work around. // usartTx("$PUBX,40,GLL,0,0,0,0*5C\r\n"); // usartTx("$PUBX,40,GSA,0,0,0,0*4E\r\n"); // usartTx("$PUBX,40,GSV,0,0,0,0*59\r\n"); // usartTx("$PUBX,40,RMC,0,0,0,0*47\r\n"); // usartTx("$PUBX,40,VTG,0,0,0,0*5E\r\n"); // // Keep just the recommended minimum flowing, slowly. // usartTx("$PUBX,40,GGA,0,15,0,0*6E\r\n"); // usartTx("$PUBX,40,GGA,0,15,0,0*6E\r\n"); // Repeat first; timing work around. // usartTx("$PUBX,40,GLL,0,15,0,0*68\r\n"); // usartTx("$PUBX,40,GSA,0,15,0,0*7A\r\n"); // usartTx("$PUBX,40,GSV,0,15,0,0*6D\r\n"); // usartTx("$PUBX,40,RMC,0,15,0,0*73\r\n"); // usartTx("$PUBX,40,VTG,0,15,0,0*6A\r\n"); // The first character is sometimes (always?) dropped, so send a few // buffer characters that can be safely discarded. gpsTxChar('\r'); gpsTxChar('\n'); gpsTxChar('\r'); gpsTxChar('\n'); // Disable all data pushes from GPS. gpsSetPushFreq("DTM", 0); gpsSetPushFreq("GBS", 0); gpsSetPushFreq("GGA", 0); gpsSetPushFreq("GLL", 0); gpsSetPushFreq("GPQ", 0); gpsSetPushFreq("GRS", 0); gpsSetPushFreq("GSA", 0); gpsSetPushFreq("GST", 0); gpsSetPushFreq("GSV", 0); gpsSetPushFreq("RMC", 0); gpsSetPushFreq("THS", 0); gpsSetPushFreq("VTG", 0); // Except the one which tells us what time it is. gpsSetPushFreq("ZDA", 2); // Now that it won't be noisy, enable the RX interrupt. NVIC_InitTypeDef NVIC_InitStructure = { .NVIC_IRQChannel = USART1_IRQn, .NVIC_IRQChannelPreemptionPriority = 1, .NVIC_IRQChannelSubPriority = 1, .NVIC_IRQChannelCmd = ENABLE, }; NVIC_Init(&NVIC_InitStructure); USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); } // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // void RTC_IRQHandler(void) { if (RTC_GetFlagStatus(RTC_FLAG_SEC) != RESET) { RTC_ClearFlag(RTC_FLAG_SEC); gSecondFlag = 1; gSeconds = RTC_GetCounter(); } } void SysTick_Handler(void) { gBlinkTick++; if (gBlinkTick > 125) { gBlinkTick = 0; gBlinkStatus = !gBlinkStatus; } uint8_t btnPressed = 0; uint16_t btns = GPIO_ReadInputData(BTN_PORT) & BTN_ALL_PINS; if ((btns & BTN_MAPLE_PIN) == BTN_MAPLE_PIN) btnPressed = BTN_MAPLE; else if ((btns & BTN_DIM_PIN) == 0) btnPressed = BTN_DIM; else if ((btns & BTN_DN_PIN) == 0) btnPressed = BTN_DOWN; else if ((btns & BTN_UP_PIN) == 0) btnPressed = BTN_UP; else if ((btns & BTN_SET_PIN) == 0) btnPressed = BTN_SET; if (btnPressed && gButtonPending == btnPressed) { if (gButtonDebounce > 10) { // Button already activated, still held, do nothing. } else if (gButtonDebounce == 10) { // Transition! Set pressed button. gButtonPressed = btnPressed; gButtonDebounce++; } else { gButtonDebounce++; } } else { gButtonDebounce = 0; gButtonPending = btnPressed; } if (gDpTick > 0) { gDpTick--; if (gDpTick == 0) { GPIO_WriteBit(DP_PORT, DP_PIN, RESET); } } }
/** * @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_stm32f10x_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f10x.c file */ /* Initialize LEDs and Key Button mounted on STM3210X-EVAL board */ STM_EVAL_LEDInit(LED1); STM_EVAL_LEDInit(LED2); STM_EVAL_LEDInit(LED3); STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_EXTI); /* Enable PWR and BKP clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); /* Configure EXTI Line to generate an interrupt on falling edge */ EXTI_Configuration(); /* Configure RTC clock source and prescaler */ RTC_Configuration(); /* NVIC configuration */ NVIC_Configuration(); /* Configure the SysTick to generate an interrupt each 1 millisecond */ SysTick_Configuration(); /* Turn on LED1 */ STM_EVAL_LEDOn(LED1); while (1) { /* Insert 1.5 second delay */ Delay(1500); /* Wait till RTC Second event occurs */ RTC_ClearFlag(RTC_FLAG_SEC); while(RTC_GetFlagStatus(RTC_FLAG_SEC) == RESET); /* Alarm in 3 second */ RTC_SetAlarm(RTC_GetCounter()+ 3); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Turn off LED1 */ STM_EVAL_LEDOff(LED1); /* Request to enter STOP mode with regulator in low power mode*/ PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI); /* At this stage the system has resumed from STOP mode -------------------*/ /* Turn on LED1 */ STM_EVAL_LEDOn(LED1); /* Configures system clock after wake-up from STOP: enable HSE, PLL and select PLL as system clock source (HSE and PLL are disabled in STOP mode) */ SYSCLKConfig_STOP(); } }