/** * @brief Initialize the watchdog timer for a specified timeout * * It is important to note that this function returns the achieved timeout * for this hardware. For hardware independence this should be checked when * scheduling updates. Other hardware dependent details may need to be * considered such as a window time which sets a minimum update time, * and this function should return a recommended delay for clearing. * * For the STM32 nominal clock rate is 32 khz, but for the maximum clock rate of * 60 khz and a prescaler of 4 yields a clock rate of 15 khz. The delay that is * set in the watchdog assumes the nominal clock rate, but the delay for FreeRTOS * to use is 75% of the minimal delay. * * @returns Maximum recommended delay between updates based on PIOS_WATCHDOG_TIMEOUT constant */ uint16_t PIOS_WDG_Init() { uint16_t delay = ((uint32_t) PIOS_WATCHDOG_TIMEOUT * 60) / 16; if (delay > 0x0fff) delay = 0x0fff; #if defined(PIOS_INCLUDE_WDG) DBGMCU_Config(DBGMCU_IWDG_STOP, ENABLE); // make the watchdog stop counting in debug mode IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable); IWDG_SetPrescaler(IWDG_Prescaler_16); IWDG_SetReload(delay); IWDG_ReloadCounter(); IWDG_Enable(); // watchdog flags now stored in backup registers PWR_BackupAccessCmd(ENABLE); wdg_configuration.bootup_flags = RTC_ReadBackupRegister(PIOS_WDG_REGISTER); /* * Start from an empty set of registered flags so previous boots * can't influence the current one */ RTC_WriteBackupRegister(PIOS_WDG_REGISTER, 0); #endif return delay; }
void rtc_init() { RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); if(BKP->DR1 != BKP_MAGIC) { PWR_BackupAccessCmd(ENABLE); /* Allow write access to BKP Domain */ RCC_BackupResetCmd(ENABLE); /* Reset Backup Domain */ RCC_BackupResetCmd(DISABLE); RCC_LSEConfig(RCC_LSE_ON); /* Enable LSE */ while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET); /* Wait till LSE is ready */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); /* Select LSE as RTC Clock Source */ RCC_RTCCLKCmd(ENABLE); /* Enable RTC Clock */ RTC_WaitForSynchro(); /* Wait for RTC registers synchronization */ RTC_WaitForLastTask(); RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */ RTC_WaitForLastTask(); BKP->DR1 = BKP_MAGIC; PWR_BackupAccessCmd(DISABLE); /* Protect backup registers */ rtc_is_valid = 0; } else { /* Wait for RTC registers synchronization */ RTC_WaitForSynchro(); } }
unsigned int RTC_cfg(void) { unsigned int ret; RTC_DateTypeDef RTC_DateStruct; RTC_TimeTypeDef RTC_TimeStruct; ret = 0; if (RTC_ReadBackupRegister(RTC_BKP_DR1) != 0xA5A5) { ret = 1; /* Allow access to BKP Domain */ PWR_BackupAccessCmd(ENABLE); /* Reset Backup Domain */ RTC_DeInit(); /* Enable LSE */ RCC_LSEConfig(RCC_LSE_ON); /* Wait till LSE is ready */ while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) {} /* Select LSE as RTC Clock Source */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); /* Enable RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* Wait for RTC registers synchronization */ RTC_WaitForSynchro(); /* Adjust time */ RTC_DateStruct.RTC_Year = 13; RTC_DateStruct.RTC_Month = 05; RTC_DateStruct.RTC_Date = 29; RTC_DateStruct.RTC_WeekDay = 3; RTC_TimeStruct.RTC_Hours = 11; RTC_TimeStruct.RTC_Minutes = 0; RTC_TimeStruct.RTC_Seconds = 0; RTC_SetTime(RTC_Format_BIN, &RTC_TimeStruct); RTC_SetDate(RTC_Format_BIN, &RTC_DateStruct); RTC_WriteBackupRegister(RTC_BKP_DR1, 0xA5A5); } else { /* Allow access to BKP Domain */ PWR_BackupAccessCmd(ENABLE); /* Wait for RTC registers synchronization */ RTC_WaitForSynchro(); while (RTC_GetFlagStatus(RTC_FLAG_RSF) == RESET); } return ret; }
void PVD_IRQHandler(void) { unsigned poweroff_cnt; unsigned long time; EXTI->PR = EXTI_Line16; //EXTI_ClearITPendingBit(EXTI_Line16); /* 系统上电前500ms, 不认为是掉电; 若已处于系统掉电状态, 将不再处理 */ if ((rt_tick_get() < 50) || (NULL == rtc_dev) || is_system_powerdown) { return; } /* lcd_bl_led_off(); buzzer_off(buzzer_gpio, buzzer_pin); */ is_system_powerdown = 1; sys_powerdown_delay4confirm = rt_tick_get(); PWR_BackupAccessCmd(ENABLE); poweroff_cnt = BKP_ReadBackupRegister(RX_POWEROFF_CNT_BKP16BITS); BKP_WriteBackupRegister(RX_POWEROFF_CNT_BKP16BITS, ++poweroff_cnt); rt_device_control(rtc_dev, RT_DEVICE_CTRL_RTC_GET_TIME, &time); BKP_WriteBackupRegister(RX_POWEROFF_N_BKP16BITS_H, (time>>16) & 0xffff); BKP_WriteBackupRegister(RX_POWEROFF_N_BKP16BITS_L, (time) & 0xffff); PWR_BackupAccessCmd(DISABLE); //rt_kprintf("%s", __FUNCTION__); return; }
void rtc_set(uint32_t val) { PWR_BackupAccessCmd(ENABLE); RTC_SetCounter(val); RTC_WaitForLastTask(); PWR_BackupAccessCmd(DISABLE); }
void setRtcTime(time_t current) { PWR_BackupAccessCmd(ENABLE); RTC_WaitForLastTask(); RTC_SetCounter(current); RTC_WaitForLastTask(); PWR_BackupAccessCmd(DISABLE); RTC_WaitForLastTask(); gSecondFlag = 1; }
void backupSave() { PWR_BackupAccessCmd(ENABLE); BKP_ClearFlag(); BKP_WriteBackupRegister(BKP_DR1, BACKUP_MARKER); BKP_WriteBackupRegister(BKP_DR2, (gUtcOffset >> 16) & 0xFFFF); BKP_WriteBackupRegister(BKP_DR3, gUtcOffset & 0xFFFF); BKP_WriteBackupRegister(BKP_DR4, gGridOc.TIM_Pulse); PWR_BackupAccessCmd(DISABLE); }
void btn_enter_pressed_in_set_chk_interval(){ /* Allow access to BKP Domain */ PWR_BackupAccessCmd(ENABLE); BKP_WriteBackupRegister(CHECK_INTERVAL_BKP, chk_int); PWR_BackupAccessCmd(DISABLE); regim = DISPLAY_REGIM_MENU; //выходим в меню lcd_set_state(LCD_ENABLE, CURSOR_DISABLE); }
void Backup_SRAM_Write_Reg(void *backup_reg, void *source_reg,uint8_t reg_size) { uint8_t i=0; PWR_BackupAccessCmd(ENABLE); // set PWR->CR.dbp = 1; PWR_BackupRegulatorCmd(ENABLE); // set PWR->CSR.bre = 1; for(i=0;i<reg_size;i++) { *((uint8_t*)backup_reg+i)=*((uint8_t*)source_reg+i); } PWR_BackupAccessCmd(DISABLE); // reset PWR->CR.dbp = 0; }
void SWRB_TimeSettingsChangeConfirmProc(void) { gSwrbDialogSelectFlag = SWRB_DIALOG_SELECT_NONE; gSwrbTestSetSelectFlag = SWRB_TEST_SET_SELECT_NONE; TimeStr_Comb(hWin_SWRB_TIMESET, ID_TIMESET_EDIT_SETVALUE); PWR_BackupAccessCmd(ENABLE); while(!(RTC_SetDate(RTC_Format_BIN, &rtcDate))); while(!(RTC_SetTime(RTC_Format_BIN, &rtcTime))); while(!RTC_WaitForSynchro()); PWR_BackupAccessCmd(DISABLE); }
static rt_err_t rt_rtc_control(rt_device_t dev, rt_uint8_t cmd, void *args) { //rt_time_t *time; RT_ASSERT(dev != RT_NULL); switch (cmd) { case RT_DEVICE_CTRL_RTC_GET_TIME: // time = (rt_time_t *)args; /* read device */ RTC_GetTime(RTC_Format_BIN, &RTC_TimeStructure); break; case RT_DEVICE_CTRL_RTC_SET_TIME: { // time = (rt_time_t *)args; /* Enable PWR and BKP clocks */ //RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* Allow access to BKP Domain */ //PWR_BackupAccessCmd(ENABLE); PWR_BackupAccessCmd(ENABLE); /* Wait until last write operation on RTC registers has finished */ //RTC_WaitForLastTask(); RTC_SetTime(RTC_Format_BIN, &RTC_TimeStructure); RTC_WaitForSynchro(); /* Change the current time */ //RTC_SetCounter(*time); /* Wait until last write operation on RTC registers has finished */ //RTC_WaitForLastTask(); } break; case RT_DEVICE_CTRL_RTC_SET_DATE: { RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); PWR_BackupAccessCmd(ENABLE); RTC_SetDate(RTC_Format_BIN, &RTC_DateStructure); RTC_WaitForSynchro(); } break; } return RT_EOK; }
/******************************************************************************* * Function Name : rtc_settime * Description : sets HW-RTC with values from time-struct, takes DST into * account, HW-RTC always running in non-DST time * Input : None * Output : None * Return : not used *******************************************************************************/ void rtc_settime (const RTC_t *rtc) { uint32_t cnt; volatile uint16_t i; RTC_t ts; cnt = struct_to_counter( rtc ); // non-DST counter-value counter_to_struct( cnt, &ts ); // normalize struct (for weekday) if ( isDST( &ts ) ) { cnt -= 60*60; // Subtract one hour } PWR_BackupAccessCmd(ENABLE); my_RTC_SetCounter( cnt ); PWR_BackupAccessCmd(DISABLE); }
/* ********************************************************************************************************* * 函 数 名: RTC_Config * 功能说明: 配置RTC用于跑表 * 形 参:无 * 返 回 值: 无 ********************************************************************************************************* */ static void RTC_Config(void) { /* 使能PWR时钟 */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* 允许访问RTC */ PWR_BackupAccessCmd(ENABLE); // /* 复位备份域 */ // RCC_BackupResetCmd(ENABLE); // RCC_BackupResetCmd(DISABLE); /* 选择RTC时钟源 LSI或者LSE */ #if defined (RTC_CLOCK_SOURCE_LSI) RCC_LSICmd(ENABLE); while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) { } RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); #elif defined (RTC_CLOCK_SOURCE_LSE) RCC_LSEConfig(RCC_LSE_ON); while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) { } RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); #else #error Please select the RTC Clock source inside the main.c file #endif }
void RTC_SetUp(void) { /* Allow access to RTC */ PWR_BackupAccessCmd(ENABLE); RCC_LSICmd(ENABLE); // LSI is used as RTC clock source while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) ; // Wait till LSI is ready RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); // Select RTC clock source // Enable RTC clock RCC_RTCCLKCmd(ENABLE); RTC_WaitForSynchro(); // Wait until the RTC Time and Date registers (RTC_TR and RTC_DR) are synchronized with RTC APB clock. // Set RTC calendar clock to 1 HZ (1 second) RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; RTC_InitStructure.RTC_AsynchPrediv = 88; RTC_InitStructure.RTC_SynchPrediv = 470; if (RTC_Init(&RTC_InitStructure) == ERROR) { while (1) ; } }
void RTC_Configuration(void) { /* Enable PWR and BKP clocks */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); /* Allow access to BKP Domain */ PWR_BackupAccessCmd(ENABLE); /* Reset Backup Domain */ BKP_DeInit(); // Select HSE/128 as RTC Clock Source RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div128); /* Enable RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* Wait for RTC registers synchronization */ RTC_WaitForSynchro(); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Enable the RTC Second */ // RTC_ITConfig(RTC_IT_SEC, ENABLE); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Set RTC prescaler: set RTC period to 1sec 1ms */ RTC_SetPrescaler(62); /*62499 RTC period = RTCCLK/RTC_PR = 8M / 128 = 62.5kHz ) -> (62499+1) */ /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); }
//RTC设置成内部或者外部时钟 void RTC_Config(rtc_clk rtcclk) { RTC_InitTypeDef RTC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); PWR_BackupAccessCmd(ENABLE); if(rtcclk == extrnal) { /* Enable the LSE OSC */ RCC_LSEConfig(RCC_LSE_ON); /* Wait till LSE is ready */ while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET); /* Select the RTC Clock Source */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); /* Enable the RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* Wait for RTC APB registers synchronisation */ RTC_WaitForSynchro(); } if(rtcclk == inner) { RCC_LSICmd(ENABLE); //开启内部低速 while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET); RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); RCC_RTCCLKCmd(ENABLE); RTC_WaitForSynchro(); } RTC_InitStructure.RTC_AsynchPrediv = 0x7F; RTC_InitStructure.RTC_SynchPrediv = 0xFF; RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; RTC_Init(&RTC_InitStructure); }
/** * @brief Configures the RTC. * @param None * @retval : None */ void RTC_Configuration(void) { /* Enable PWR and BKP clocks */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); /* Allow access to BKP Domain */ PWR_BackupAccessCmd(ENABLE); /* Reset Backup Domain */ BKP_DeInit(); /* Enable LSE */ RCC_LSEConfig(RCC_LSE_ON); /* Wait till LSE is ready */ while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) {} /* Select LSE as RTC Clock Source */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); /* Enable RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* Wait for RTC registers synchronization */ RTC_WaitForSynchro(); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Enable the RTC Second */ RTC_ITConfig(RTC_IT_SEC, ENABLE); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Set RTC prescaler: set RTC period to 1sec */ RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */ /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); }
/** * @brief Configures RTC clock source and prescaler. * @param None * @retval None */ void RTC_Configuration(void) { /* RTC clock source configuration ----------------------------------------*/ /* Allow access to BKP Domain */ PWR_BackupAccessCmd(ENABLE); /* Reset Backup Domain */ BKP_DeInit(); /* Enable LSE OSC */ RCC_LSICmd(ENABLE); /* Wait till LSE is ready */ while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) { } /* Select the RTC Clock Source */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); /* Enable the RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* RTC configuration -----------------------------------------------------*/ /* Wait for RTC APB registers synchronisation */ RTC_WaitForSynchro(); /* Set the RTC time base to 1s */ RTC_SetPrescaler(32767); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); RTC_ITConfig(RTC_IT_ALR, ENABLE); RTC_WaitForLastTask(); }
void test(void) { //Reads data from the specified RTC Backup data Register. //入侵检测 if (RTC_ReadBackupRegister(RTC_BKP_DR0) != 0x32F2) { /* RTC configuration */ RTC_Config(); /* Configure the time&date register */ RTC_TimeRegulate(); } else { /* Enable the PWR clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* Allow access to RTC */ PWR_BackupAccessCmd(ENABLE); /* Wait for RTC APB registers synchronisation */ RTC_WaitForSynchro(); /* Clear the RTC Alarm Flag */ RTC_ClearFlag(RTC_FLAG_ALRAF); /* Clear the EXTI Line 17 Pending bit (Connected internally to RTC Alarm) */ EXTI_ClearITPendingBit(EXTI_Line17); } while (1); }
/** * @file RTC_Configuration * @brief Configures the RTC. * @param 无 * @retval 无 */ static void RTC_Configuration(void) { //启用PWR和BKP的时钟(from APB1) RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); //后备域解锁 PWR_BackupAccessCmd(ENABLE); //备份寄存器模块复位 BKP_DeInit(); //外部32.768K时钟使能 RCC_LSEConfig(RCC_LSE_ON); //等待稳定 while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) {} //RTC时钟源配置成LSE(外部32.768K) RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); //RTC开启 RCC_RTCCLKCmd(ENABLE); //开启后需要等待APB1时钟与RTC时钟同步,才能读写寄存器 RTC_WaitForSynchro(); //读写寄存器前,要确定上一个操作已经结束 RTC_WaitForLastTask(); //使能秒中断 RTC_ITConfig(RTC_IT_SEC, ENABLE); //等待寄存器写入完成 RTC_WaitForLastTask(); //设置RTC分频器,使RTC时钟为1Hz //RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) RTC_SetPrescaler(32767); //等待写入完成 RTC_WaitForLastTask(); }
u8 RTC_Set(u16 syear,u8 smon,u8 sday,u8 hour,u8 min,u8 sec) { u16 t; u32 seccount=0; if(syear<1970||syear>2099)return 1; for(t=1970;t<syear;t++) //把所有年份的秒钟相加 { if(Is_Leap_Year(t))seccount+=31622400;//闰年的秒钟数 else seccount+=31536000; //平年的秒钟数 } smon-=1; for(t=0;t<smon;t++) //把前面月份的秒钟数相加 { seccount+=(u32)mon_table[t]*86400;//月份秒钟数相加 if(Is_Leap_Year(syear)&&t==1)seccount+=86400;//闰年2月份增加一天的秒钟数 } seccount+=(u32)(sday-1)*86400;//把前面日期的秒钟数相加 seccount+=(u32)hour*3600;//小时秒钟数 seccount+=(u32)min*60; //分钟秒钟数 seccount+=sec;//最后的秒钟加上去 RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); //使能PWR和BKP外设时钟 PWR_BackupAccessCmd(ENABLE); //使能RTC和后备寄存器访问 RTC_SetCounter(seccount); //设置RTC计数器的值 RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成 return 0; }
/******************************************************************************* * Function Name : RTC_Configuration * Description : Configures the RTC. * Input : None * Output : None * Return : 0 reday,-1 error. *******************************************************************************/ int RTC_Config(void) { u32 count=0x250000; /* Enable the PWR clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* Allow access to RTC */ PWR_BackupAccessCmd(ENABLE); RCC_LSEConfig(RCC_LSE_ON); /* Wait till LSE is ready */ while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET && (--count) ); if ( count == 0 ) { return -1; } /* Select the RTC Clock Source */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); SynchPrediv = 0xFF; AsynchPrediv = 0x7F; /* Enable the RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* Wait for RTC APB registers synchronisation */ RTC_WaitForSynchro(); return 0; }
void rtc_init(){ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); PWR_BackupAccessCmd(ENABLE); //needed? /* use LSE-clock X3=32768 KHz, C RCC_LSEConfig(RCC_LSE_ON); while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET){}; RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); RCC_RTCCLKCmd(ENABLE); RTC_WaitForSynchro(); RTC_InitTypeDef rtc_init; rtc_init.RTC_AsynchPrediv = 0xFF; rtc_init.RTC_SynchPrediv = 0x7F; rtc_init.RTC_HourFormat = RTC_HourFormat_24; */ RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div32); RCC_RTCCLKCmd(ENABLE); RTC_WaitForSynchro(); RTC_InitTypeDef rtc_init; rtc_init.RTC_SynchPrediv = 0x7a0; rtc_init.RTC_AsynchPrediv = 0x7F; rtc_init.RTC_HourFormat = RTC_HourFormat_24; RTC_Init(&rtc_init); }
/** * @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 LD3, LD4 and USER Button mounted on STM32VLDISCOVERY board */ STM32vldiscovery_LEDInit(LED3); STM32vldiscovery_LEDInit(LED4); STM32vldiscovery_PBInit(BUTTON_USER, BUTTON_MODE_EXTI); /* Turn on LD3 */ STM32vldiscovery_LEDOn(LED3); /* Enable PWR and BKP clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); /* Allow access to BKP Domain */ PWR_BackupAccessCmd(ENABLE); /* Configure RTC clock source and prescaler */ RTC_Configuration(); /* Configure the SysTick to generate an interrupt each 250 ms */ SysTick_Configuration(); while (1) { } }
/** * @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(); }
/******************************************************************************* * Function Name : main * Description : Main program. * Input : None * Output : None * Return : None *******************************************************************************/ int main(void) { #ifdef DEBUG debug(); #endif /* System Clocks Configuration */ RCC_Configuration(); /* NVIC configuration */ NVIC_Configuration(); /* GPIO configuration */ GPIO_Configuration(); /* Enable PWR and BKP clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); /* Enable write access to Backup domain */ PWR_BackupAccessCmd(ENABLE); /* Clear Tamper pin Event(TE) pending flag */ BKP_ClearFlag(); /* Check if the Power On Reset flag is set */ if(RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET) { /* Clear reset flags */ RCC_ClearFlag(); /* Turn on led connected to GPIO_LED Pin8 */ GPIO_SetBits(GPIO_LED, GPIO_Pin_8); /* Check if Backup data registers are programmed */ if(CheckBackupReg(0x3210) == 0x00) { /* Backup data registers values are correct */ /* Turn on led connected to GPIO_LED Pin6 */ GPIO_SetBits(GPIO_LED, GPIO_Pin_6); } else { /* Backup data registers values are not correct or they are not yet programmed (when the first time the program is executed) */ /* Write data to Backup data registers */ WriteToBackupReg(0x3210); /* Turn on led connected to GPIO_LED Pin7 */ GPIO_SetBits(GPIO_LED, GPIO_Pin_7); } } /* Turn on led connected to GPIO_LED Pin9 */ GPIO_SetBits(GPIO_LED, GPIO_Pin_9); while (1) { } }
/******************************************************************************* * Function Name : RCC_Configuration * Description : Configures the different system clocks. * Input : None * Output : None * Return : None *******************************************************************************/ void RCC_Configuration(void) { ErrorStatus HSEStartUpStatus; /* RCC system reset(for debug purpose) */ RCC_DeInit(); /* Enable HSE */ RCC_HSEConfig(RCC_HSE_ON); /* Wait till HSE is ready */ HSEStartUpStatus = RCC_WaitForHSEStartUp(); if(HSEStartUpStatus == SUCCESS) { /* Enable Prefetch Buffer */ FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); /* Flash 2 wait state */ FLASH_SetLatency(FLASH_Latency_2); /* HCLK = SYSCLK */ RCC_HCLKConfig(RCC_SYSCLK_Div1); /* PCLK2 = HCLK */ RCC_PCLK2Config(RCC_HCLK_Div1); /* PCLK1 = HCLK/2 */ RCC_PCLK1Config(RCC_HCLK_Div2); /* PLLCLK = 8MHz * 9 = 72 MHz */ RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); /* Enable PLL */ RCC_PLLCmd(ENABLE); /* Wait till PLL is ready */ while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) { } /* Select PLL as system clock source */ RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); /* Wait till PLL is used as system clock source */ while(RCC_GetSYSCLKSource() != 0x08) { } } /* Enable peripheral clocks --------------------------------------------------*/ /* Enable USART1 and GPIOB clocks */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOB, ENABLE); /* Enable USART3 clocks */ RCC_APB1PeriphClockCmd ( RCC_APB1Periph_USART3 | RCC_APB1Periph_TIM2, ENABLE); PWR_BackupAccessCmd(ENABLE); }
static void sys_clk_init(void) { ErrorStatus HSEStartUpStatus; // set clocks registers back to defaults (for debugging, st_demo) RCC_DeInit(); // enable the high speed external osc (HSE) and spin for it to stabilise RCC_HSEConfig(RCC_HSE_ON); HSEStartUpStatus = RCC_WaitForHSEStartUp(); if (HSEStartUpStatus != SUCCESS) { ///@todo handle error better while (1) {} } // Enable Prefetch Buffer FLASH_PrefetchBufferCmd(ENABLE); // Flash 1 wait state FLASH_SetLatency(FLASH_Latency_5); // Setup the PLLCLK source and pre scaler for the following clocks // PLLCLK = 8MHz * 336[N] / (8[M] * 2[P]) = 168MHz // PLLCLK = 8MHz * 336[N] / (8[M] * 7[Q]) = 48MHz /* Setup the PLLCLK source and pre scaler */ RCC_PLLConfig(RCC_PLLSource_HSE, 8, 336, 2, 7); // AHB prescaler set to div 1, HCLK = SYSCLK (168MHz [max]) RCC_HCLKConfig(RCC_SYSCLK_Div1); // APB2 (high speed) prescaler set to div 2, APB2 = HCLK / 2 = 84MHz [max] RCC_PCLK2Config(RCC_HCLK_Div2); // APB1 (low speed) prescaler set to div 4, APB1 = HCLK / 4 = 42MHz [max] RCC_PCLK1Config(RCC_HCLK_Div4); // Enable the PLL and spin for it to be ready RCC_PLLCmd(ENABLE); while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) {} // Switch the system clock over to the PLL output and spin until it is ready RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); while(RCC_GetSYSCLKSource() != 0x08) ; // enable backup register domain clocks incase they are not enabled RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); PWR_BackupAccessCmd(ENABLE); RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div31); RCC_RTCCLKCmd(ENABLE); #ifdef DEBUG_MCO /* this is a handy debugging option to output the sys_clk/4 to the MCO1 pin * it must be sys_clk/4 as gpio can only output 50MHz max, so 168MHz/4 = 42MHz */ RCC_MCO1Config(RCC_MCO1Source_PLLCLK, RCC_MCO1Div_4); #endif }
// 配置RTC硬件。 void RTC_Configure(void) { /* Enable PWR and BKP clocks */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); /* Allow access to BKP Domain */ PWR_BackupAccessCmd(ENABLE); RCC_RTCCLKCmd(ENABLE); /* Reset Backup Domain */ BKP_DeInit(); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); #ifdef RTCClockSource_LSI /* Enable LSI */ RCC_LSICmd(ENABLE); /* Wait till LSI is ready */ while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET); /* Select LSI as RTC Clock Source */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); #elif defined RTCClockSource_LSE /* Enable LSE */ RCC_LSEConfig(RCC_LSE_ON); /* Wait till LSE is ready */ while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET); /* Select LSE as RTC Clock Source */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); #endif // 上电后需要校准RTC时钟,也即准确计算出RTC的周期时长。 RTC_Calibrate(); #ifdef RTCClockOutput_Enable /* Disable the Tamper Pin */ BKP_TamperPinCmd(DISABLE); /* To output RTCCLK/64 on Tamper pin, the tamper functionality must be disabled */ /* Enable RTC Clock Output on Tamper Pin */ BKP_RTCCalibrationClockOutputCmd(ENABLE); #endif /*允许RTC报警中断*/ RTC_ITConfig(RTC_IT_ALR, ENABLE); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); }
/** * @file RTC_Init * @brief RTC Initialization * @param 无 * @retval 无 */ void RTC_Init(void) { 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("RTC not yet configured....\r\n"); /* RTC Configuration */ RTC_Configuration(); Time_Regulate(); /* Adjust time by values entred by the user on the hyperterminal */ printf("RTC configured....\r\n"); BKP_WriteBackupRegister(BKP_DR1, 0xA5A5); } else { /* Check if the Power On Reset flag is set */ if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET) { printf("Power On Reset occurred....\r\n"); } /* Check if the Pin Reset flag is set */ else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET) { printf("External Reset occurred....\r\n"); } printf("No need to configure RTC....\r\n"); /* 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(); } /* NVIC configuration */ NVIC_Configuration(); #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(); return; }