/** * @brief Apllication Initialisation Routine * @param None * @retval : None */ void ApplicationInit(void) { /* System Clocks Configuration */ RCC_Configuration(); /*Enables the clock to Backup and power interface peripherals */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP | RCC_APB1Periph_PWR,ENABLE); /* SysTick Configuration*/ SysTickConfig(); /*Initialisation of TFT LCD */ STM3210B_LCD_Init(); /* Unlock the Flash Program Erase controller */ FLASH_Unlock(); /*RTC_NVIC Configuration */ RTC_NVIC_Configuration(); /* RTC Configuration*/ RTC_Configuration(); BKP_RTCOutputConfig(BKP_RTCOutputSource_None); /* General Purpose I/O Configuration */ GPIO_Configuration(); /* Battery Removal Emulation */ GPIO_SetBits(GPIOC, GPIO_Pin_8); while(!(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_13))) { TamperEvent = 1; } /* Joystick NVIC Configuration */ NVIC_JoyStickConfig(); /* Tamper pin NVIC Configuration */ Tamper_NVIC_Configuration(); /* Configure PVD Supervisor to disable the Tamper Interrupt when voltage drops below 2.5 volts*/ PWR_PVDCmd(ENABLE); PWR_PVDLevelConfig(PWR_PVDLevel_2V5); PWR_BackupAccessCmd(ENABLE); /* Only JoyStick Sel Interrupt is enabled on startup */ SelIntExtOnOffConfig(ENABLE); /* Tamper FeatureRTC - Enable Tamper Interrupt and configure for Low level */ BKP_ITConfig(ENABLE); /* Enable Tamper Pin for Active low level: Tamper level detected for low level*/ BKP_TamperPinLevelConfig(BKP_TamperPinLevel_Low); /* Enable tamper Pin Interrupt */ BKP_TamperPinCmd(ENABLE); /* Menu Initialisation */ MenuInit(); }
/**************************************************************************** * 名 称: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 }
/**************************************************************/ //程 序 名: RTC_Init //开 发 者: MingH //入口参数: 无 //功能说明: RTC 相关寄存器配置初始化, 外部调用 //**************************************************************/ void RTC_Init(void) { RTC_NVIC_Configuration(); // RTC 中断配置 if (BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5) { /* Backup data register value is not correct or not yet programmed (when the first time the program is executed) */ printf("\r\n\n RTC not yet configured...."); /* RTC Configuration */ RTC_Configuration(); printf("\r\n RTC configured...."); /* Adjust time by values entered by the user on the hyperterminal */ Time_Adjust(); BKP_WriteBackupRegister(BKP_DR1, 0xA5A5); } else { /* Check if the Power On Reset flag is set */ if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET) { printf("\r\n\n Power On Reset occurred...."); } /* Check if the Pin Reset flag is set */ else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET) { printf("\r\n\n External Reset occurred...."); } printf("\r\n No need to configure RTC...."); /* Wait for RTC registers synchronization */ RTC_WaitForSynchro(); /* Enable the RTC Second */ RTC_ITConfig(RTC_IT_SEC, ENABLE); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); } #ifdef RTCClockOutput_Enable /* Enable PWR and BKP clocks */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); /* Allow access to BKP Domain */ PWR_BackupAccessCmd(ENABLE); /* Disable the Tamper Pin */ BKP_TamperPinCmd(DISABLE); /* To output RTCCLK/64 on Tamper pin, the tamper functionality must be disabled */ /* Enable RTC Clock Output on Tamper Pin */ BKP_RTCOutputConfig(BKP_RTCOutputSource_CalibClock); #endif /* Clear reset flags */ RCC_ClearFlag(); }
// init RTC void rtc_init(void) { uint16_t WaitForOscSource; /*RTC_NVIC Configuration */ RTC_NVIC_Configuration(); /*Enables the clock to Backup and power interface peripherals */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP | RCC_APB1Periph_PWR,ENABLE); /*Allow access to Backup Registers*/ PWR_BackupAccessCmd(ENABLE); if(BKP_ReadBackupRegister(BKP_DR1)== (uint16_t)(~CONFIGURATION_DONE)) { /*Enables the clock to Backup and power interface peripherals */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP | RCC_APB1Periph_PWR,ENABLE); /* Backup Domain Reset */ BKP_DeInit(); systime.Month=DEFAULT_MONTH; systime.Day=DEFAULT_DAY; systime.Year=DEFAULT_YEAR; BKP_ModifyBackupRegister(BKP_DR3, (systime.Month << 8) | systime.Day); BKP_ModifyBackupRegister(BKP_DR5, systime.Year); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* enter config mode */ RTC_EnterConfigMode(); /*Enable 32.768 kHz external oscillator */ RCC_LSEConfig(RCC_LSE_ON); /* select clock source for RTC */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); /* RTC Enabled */ RCC_RTCCLKCmd(ENABLE); /*Wait for RTC registers synchronisation */ RTC_WaitForSynchro(); RTC_WaitForLastTask(); /* Setting RTC Interrupts-Seconds interrupt enabled */ /* Enable the RTC Second */ RTC_ITConfig(RTC_IT_SEC , ENABLE); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* init prescale value*/ RTC_SetPrescaler(32767); /* exit config mode */ RTC_ExitConfigMode(); /* Set default system time to 09 : 24 : 00 */ SetTime(DEFAULT_HOURS,DEFAULT_MINUTES); BKP_ModifyBackupRegister(BKP_DR1, CONFIGURATION_DONE); } else { /* PWR and BKP clocks selection */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); for(WaitForOscSource=0;WaitForOscSource<5000;WaitForOscSource++); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Enable the RTC Second */ RTC_ITConfig(RTC_IT_SEC, ENABLE); RTC_WaitForLastTask(); } /* Check if how many days are elapsed in power down/Low Power Mode- Updates Date that many Times*/ CheckForDaysElapsed(); systime.Month = (uint8_t)(BKP_ReadBackupRegister(BKP_DR3) >> 8); systime.Day = (uint8_t)BKP_ReadBackupRegister(BKP_DR3); systime.Year = (uint8_t)BKP_ReadBackupRegister(BKP_DR5); BKP_RTCOutputConfig(BKP_RTCOutputSource_None); }