/** * @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 HAL_Core_Enter_Bootloader(bool persist) { if (persist) { RTC_WriteBackupRegister(RTC_BKP_DR10, 0xFFFF); system_flags.FLASH_OTA_Update_SysFlag = 0xFFFF; Save_SystemFlags(); } else { RTC_WriteBackupRegister(RTC_BKP_DR1, ENTER_DFU_APP_REQUEST); } HAL_Core_System_Reset(); }
void TM_RTC_Config(TM_RTC_ClockSource_t source) { if (source == TM_RTC_ClockSource_Internal) { /* 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); } else if (source == TM_RTC_ClockSource_External) { /* 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 register synchronization */ RTC_WaitForSynchro(); /* Write status */ RTC_WriteBackupRegister(RTC_STATUS_REG, RTC_STATUS_INIT_OK); }
void TM_RTC_SetDateTime(TM_RTC_Time_t* data, TM_RTC_Format_t format) { /* Fill time */ RTC_TimeStruct.RTC_Hours = data->hours; RTC_TimeStruct.RTC_Minutes = data->minutes; RTC_TimeStruct.RTC_Seconds = data->seconds; /* Fill date */ RTC_DateStruct.RTC_Date = data->date; RTC_DateStruct.RTC_Month = data->month; RTC_DateStruct.RTC_Year = data->year; RTC_DateStruct.RTC_WeekDay = data->day; /* Set the RTC time base to 1s and hours format to 24h */ RTC_InitStruct.RTC_HourFormat = RTC_HourFormat_24; RTC_InitStruct.RTC_AsynchPrediv = uwAsynchPrediv; RTC_InitStruct.RTC_SynchPrediv = uwSynchPrediv; RTC_Init(&RTC_InitStruct); if (format == TM_RTC_Format_BCD) { RTC_SetTime(RTC_Format_BCD, &RTC_TimeStruct); } else { RTC_SetTime(RTC_Format_BIN, &RTC_TimeStruct); } if (format == TM_RTC_Format_BCD) { RTC_SetDate(RTC_Format_BCD, &RTC_DateStruct); } else { RTC_SetDate(RTC_Format_BIN, &RTC_DateStruct); } if (TM_RTC_Status != RTC_STATUS_ZERO) { /* Write backup registers */ RTC_WriteBackupRegister(RTC_STATUS_REG, RTC_STATUS_TIME_OK); } }
//设置时间 // RTC_H12_AM ((uint8_t)0x00) // RTC_H12_PM ((uint8_t)0x40) void set_time( unsigned char year, unsigned char mounth, unsigned char day, unsigned char hour, unsigned char min, unsigned char sec, unsigned char week, unsigned char ampm) { RTC_TimeTypeDef RTC_TimeStructure; RTC_DateTypeDef RTC_DateStructure; RTC_DateStructure.RTC_Year = year; RTC_DateStructure.RTC_Month = mounth; RTC_DateStructure.RTC_Date = day; RTC_DateStructure.RTC_WeekDay = week; //设置时间 RTC_SetDate(RTC_Format_BCD, &RTC_DateStructure); RTC_TimeStructure.RTC_H12 = ampm; RTC_TimeStructure.RTC_Hours = hour; RTC_TimeStructure.RTC_Minutes = min; RTC_TimeStructure.RTC_Seconds = sec; RTC_SetTime(RTC_Format_BCD, &RTC_TimeStructure); RTC_WriteBackupRegister(RTC_BKP_DR0, 0x32F1); }
/** * @brief Save parameters in the backup memory * @param mem_base: parameters memory address in the backup sram * @param cfg: configuration parameters structure * @retval None */ void MOD_SetParam(uint16_t mem_base , uint32_t *cfg) { if ( RTC_Error == 0) { RTC_WriteBackupRegister(mem_base, *cfg); } }
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; }
/* ********************************************************************************************************* * 函 数 名: WriteToRTC_BKP_DR * 功能说明: 写数据到RTC的备份数据寄存器 * 形 参:无FirstRTCBackupData 写入的数据 * 返 回 值: 无 ********************************************************************************************************* */ void WriteToRTC_BKP_DR(uint8_t RTCBackupIndex, uint32_t RTCBackupData) { if (RTCBackupIndex < RTC_BKP_DR_NUMBER) { /* 写数据备份数据寄存器 */ RTC_WriteBackupRegister(aRTC_BKP_DR[RTCBackupIndex], RTCBackupData); } }
/** * @brief Function called by modules to indicate they are still running * * This function will set this flag in the active flags register (which is * a backup regsiter) and if all the registered flags are set will clear * the watchdog and set only this flag in the backup register * * @param[in] flag the flag to set * @return true if the watchdog cleared, false if flags are pending */ bool PIOS_WDG_UpdateFlag(uint16_t flag) { // we can probably avoid using a semaphore here which will be good for // efficiency and not blocking critical tasks. race condition could // overwrite their flag update, but unlikely to block _all_ of them // for the timeout window uint16_t cur_flags = RTC_ReadBackupRegister(PIOS_WDG_REGISTER); if ((cur_flags | flag) == wdg_configuration.used_flags) { PIOS_WDG_Clear(); RTC_WriteBackupRegister(PIOS_WDG_REGISTER, flag); return true; } else { RTC_WriteBackupRegister(PIOS_WDG_REGISTER, cur_flags | flag); return false; } }
void RTC_Config(void) { RTC_InitTypeDef RTC_InitStructure; RTC_TimeTypeDef RTC_TimeStructure; RTC_DateTypeDef RTC_DateStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR,ENABLE); PWR_BackupAccessCmd(ENABLE);//使能备份寄存器操作 RCC_LSICmd(ENABLE); while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET); RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); RCC_RTCCLKCmd(ENABLE); RTC_WaitForSynchro(); if(RTC_ReadBackupRegister(RTC_BKP_DR0) != 0x9741) //一个变量,看看RTC初始化没 { RTC_WriteProtectionCmd(DISABLE); RTC_EnterInitMode(); RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; RTC_InitStructure.RTC_AsynchPrediv = 0x7D-1; RTC_InitStructure.RTC_SynchPrediv = 0xFF-1; RTC_Init(&RTC_InitStructure); RTC_TimeStructure.RTC_Seconds = 0x00; RTC_TimeStructure.RTC_Minutes = 0x01; RTC_TimeStructure.RTC_Hours = 0x01; RTC_TimeStructure.RTC_H12 = RTC_H12_AM; RTC_SetTime(RTC_Format_BCD,&RTC_TimeStructure); RTC_DateStructure.RTC_Date = 30; RTC_DateStructure.RTC_Month = 5; RTC_DateStructure.RTC_WeekDay= RTC_Weekday_Thursday; RTC_DateStructure.RTC_Year = 12; RTC_SetDate(RTC_Format_BCD,&RTC_DateStructure); RTC_ExitInitMode(); RTC_WriteBackupRegister(RTC_BKP_DR0,0X9741); RTC_WriteProtectionCmd(ENABLE); RTC_WriteBackupRegister(RTC_BKP_DR0,0x9741); //初始化完成,设置标志 } PWR_BackupAccessCmd(DISABLE); }
void rtc_write(void) { if(!rtc_ok) { // Enable the PWR clock RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); // Allow access to RTC PWR_BackupAccessCmd(ENABLE); // 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 The TimeStamp RTC_TimeStampCmd(RTC_TimeStampEdge_Falling, ENABLE); // TODO: can return ERROR RTC_InitTypeDef RTC_InitStructure; RTC_InitStructure.RTC_AsynchPrediv = 0x7F; RTC_InitStructure.RTC_SynchPrediv = 0xFF; RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; RTC_Init(&RTC_InitStructure); } RTC_TimeTypeDef TimeRTC; RTC_DateTypeDef DateRTC; TimeRTC.RTC_H12 = RTC_H12_AM; // Always AM for 24H TimeRTC.RTC_Hours = rtc_hour; TimeRTC.RTC_Minutes = rtc_minute; TimeRTC.RTC_Seconds = rtc_second; RTC_SetTime(RTC_Format_BIN, &TimeRTC); (void)RTC->DR; DateRTC.RTC_WeekDay = rtc_day; DateRTC.RTC_Date = rtc_date; DateRTC.RTC_Month = rtc_month; DateRTC.RTC_Year = rtc_year; RTC_SetDate(RTC_Format_BIN, &DateRTC); // TODO: can return ERROR RTC_WriteBackupRegister(RTC_BKP_DR0, 0xCA7E); rtc_ok = true; }
OSStatus platform_rtc_init(void) { #ifdef MICO_ENABLE_MCU_RTC RTC_InitTypeDef RTC_InitStruct; RTC_DeInit( ); 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 ); #ifdef USE_RTC_BKP /* Enable the PWR clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* Allow access to BKP Domain */ PWR_BackupAccessCmd(ENABLE); 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(); #ifdef USE_RTC_BKP if (RTC_ReadBackupRegister(RTC_BKP_DR0) != USE_RTC_BKP) { /* set it to 12:20:30 08/04/2013 monday */ platform_rtc_set_time(&default_rtc_time); RTC_WriteBackupRegister(RTC_BKP_DR0, USE_RTC_BKP); } #else //#ifdef RTC_ENABLED /* application must have mico_application_default_time structure declared somewhere, otherwise it wont compile */ /* write default application time inside rtc */ platform_rtc_set_time(&mico_default_time); //#endif /* RTC_ENABLED */ #endif return kNoErr; #else return kUnsupportedErr; #endif }
/** * @brief Writes data RTC Backup DRx registers. * @param FirstRTCBackupData: data to be written to RTC Backup data registers. * @retval None */ static void WriteToRTC_BKP_DR(uint32_t FirstRTCBackupData) { uint32_t index = 0; for (index = 0; index < RTC_BKP_DR_NUMBER; index++) { /* write To bkp data register */ RTC_WriteBackupRegister(aRTC_BKP_DR[index], FirstRTCBackupData + (index * 0x5A)); } }
/** * @brief Writes data to all Backup data registers. * @param FirstBackupData: data to write to first backup data register. * @retval None */ void WriteToBackupReg(uint16_t FirstBackupData) { uint32_t index = 0; for (index = 0; index < RTC_BKP_DR_NUMBER; index++) { RTC_WriteBackupRegister(BKPDataReg[index], FirstBackupData + (index * 0x5A)); } }
//-------------------------------------------------------------- // RTC setzen // vor dem Aufruf muss die aktuelle Uhrzeit und das Datum // in der Struktur "UB_RTC" gespeichert werden // format : [RTC_DEC, RTC_HEX] //-------------------------------------------------------------- void UB_RTC_SetClock(RTC_FORMAT_t format) { // Check auf Min+Max if(UB_RTC.std>23) UB_RTC.std=23; if(UB_RTC.min>59) UB_RTC.min=59; if(UB_RTC.sek>59) UB_RTC.sek=59; if(UB_RTC.tag<1) UB_RTC.tag=1; if(UB_RTC.tag>31) UB_RTC.tag=31; if(UB_RTC.monat<1) UB_RTC.monat=1; if(UB_RTC.monat>12) UB_RTC.monat=12; if(UB_RTC.jahr>99) UB_RTC.jahr=99; if(UB_RTC.wotag<1) UB_RTC.wotag=1; if(UB_RTC.wotag>7) UB_RTC.wotag=7; // Zeit einstellen RTC_TimeStructure.RTC_Hours = UB_RTC.std; RTC_TimeStructure.RTC_Minutes = UB_RTC.min; RTC_TimeStructure.RTC_Seconds = UB_RTC.sek; if(format==RTC_DEC) { RTC_SetTime(RTC_Format_BIN, &RTC_TimeStructure); } else { RTC_SetTime(RTC_Format_BCD, &RTC_TimeStructure); } // Datum einstellen RTC_DateStructure.RTC_Date = UB_RTC.tag; RTC_DateStructure.RTC_Month = UB_RTC.monat; RTC_DateStructure.RTC_Year = UB_RTC.jahr; RTC_DateStructure.RTC_WeekDay = UB_RTC.wotag; if(format==RTC_DEC) { RTC_SetDate(RTC_Format_BIN, &RTC_DateStructure); } else { RTC_SetDate(RTC_Format_BCD, &RTC_DateStructure); } // Sonstige Settings RTC_InitStructure.RTC_AsynchPrediv = 0x7F; RTC_InitStructure.RTC_SynchPrediv = 0xFF; RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; RTC_Init(&RTC_InitStructure); // Status-Register beschreiben // falls Settings vom User gemacht wurden if(UB_RTC.status!=RTC_UNDEFINED) { RTC_WriteBackupRegister(RTC_STATUS_REG, RTC_STATUS_TIME_OK); } }
void RTC_Clock_Init(void) { RTC_TimeTypeDef RTC_TimeStruct; RTC_DateTypeDef RTC_DateStruct; RTC_InitTypeDef RTC_InitStruct; /* Enable the PWR clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* Allow access to RTC */ //RTC_WriteProtectionCmd(DISABLE); PWR_BackupAccessCmd(ENABLE); 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; RCC_RTCCLKCmd(ENABLE); RTC_WaitForSynchro(); RTC_WriteBackupRegister(RTC_BKP_DR0, FIRST_DATA); RTC_StructInit(&RTC_InitStruct); RTC_TimeStruct.RTC_Hours = (1<<4)|0; RTC_TimeStruct.RTC_Minutes = (2<<4)|3; RTC_TimeStruct.RTC_Seconds = 0; RTC_SetTime(RTC_Format_BCD,&RTC_TimeStruct); RTC_DateStruct.RTC_WeekDay = RTC_Weekday_Thursday; RTC_DateStruct.RTC_Date = 14; RTC_DateStruct.RTC_Month = RTC_Month_March; RTC_DateStruct.RTC_Year = 13; RTC_SetDate(RTC_Format_BCD,&RTC_DateStruct); RTC_InitStruct.RTC_AsynchPrediv = AsynchPrediv; RTC_InitStruct.RTC_SynchPrediv = SynchPrediv; RTC_InitStruct.RTC_HourFormat = RTC_HourFormat_24; RTC_Init(&RTC_InitStruct); xTaskCreate(RTC_Task,(signed char*)"RTC",128,NULL, tskIDLE_PRIORITY + 1, NULL); }
void platform_rtc_init(void) { 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; /* Enable the PWR clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* RTC clock source configuration ------------------------------------------*/ /* Allow access to BKP Domain */ PWR_BackupAccessCmd(ENABLE); RTC_Init( &RTC_InitStruct ); /* 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(); #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 /* write default application time inside rtc */ MicoRtcSetTime(&mico_default_time); #endif }
/** * Configure the RTC peripheral by selecting the clock source. * ck_spre(1Hz) = RTCCLK(LSE) /(asynchPrediv + 1)*(synchPrediv + 1) * * @param none * @retval none */ void RTC_Config(uint32_t synch_prediv, uint32_t asynch_prediv) { #ifdef DEBUG printf("Configuring real time clock.\n\r"); #endif /* Allocate the init structure */ RTC_InitTypeDef RTC_InitStructure; /* Enable the PWR clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* Allow access to RTC */ PWR_BackupAccessCmd(ENABLE); /* Enable the LSE OSC */ RCC_LSEConfig(RCC_LSE_ON ); /* Wait till LSE is ready */ while (RCC_GetFlagStatus(RCC_FLAG_LSERDY ) == RESET) { /* Do nothing */ } /* Select the RTC Clock Source */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE ); /* Configure the RTC data register and RTC prescaler */ RTC_InitStructure.RTC_AsynchPrediv = synch_prediv; RTC_InitStructure.RTC_SynchPrediv = asynch_prediv; RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; /* Check on RTC init */ if (RTC_Init(&RTC_InitStructure) == ERROR) { #ifdef DEBUG printf("[Tekdaqc RTC] RTC Prescaler Config failed.\n\r"); #endif } /* Enable the RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* Wait for RTC APB registers synchronization */ RTC_WaitForSynchro(); uint32_t reg = RTC_ReadBackupRegister(RTC_CONFIGURED_REG ); reg |= RTC_CONFIGURED; RTC_WriteBackupRegister(RTC_CONFIGURED_REG, reg); }
/*---------------------设置时间------------------------------------------*/ static void RTC_TimeRegulate(void) { /* Set Time hh:mm:ss */ RTC_TimeStructure.RTC_H12 = RTC_H12_AM; RTC_TimeStructure.RTC_Hours = 0x08; RTC_TimeStructure.RTC_Minutes = 0x10; RTC_TimeStructure.RTC_Seconds = 0x00; RTC_SetTime(RTC_Format_BCD, &RTC_TimeStructure); /* Set Date Week/Date/Month/Year */ RTC_DateStructure.RTC_WeekDay = 01; RTC_DateStructure.RTC_Date = 0x31; RTC_DateStructure.RTC_Month = 0x12; RTC_DateStructure.RTC_Year = 0x12; RTC_SetDate(RTC_Format_BCD, &RTC_DateStructure); //20 个备份寄存器( 80 字节)。发生入侵检测事件时,将复位备份寄存器。 RTC_WriteBackupRegister(RTC_BKP_DR0, 0x32F2);//向备份寄存器写入0x32F2 }
/** * @brief Returns the time entered by user, using Hyperterminal. * @param None * @retval None */ void RTC_TimeRegulate(int h, int m, int s) { RTC_TimeStructure.RTC_H12 = RTC_H12_AM; RTC_TimeStructure.RTC_Hours = h; RTC_TimeStructure.RTC_Minutes = m; RTC_TimeStructure.RTC_Seconds = s; /* Configure the RTC time register */ if(RTC_SetTime(RTC_Format_BIN, &RTC_TimeStructure) == ERROR) { MessageBox_Show("Set Time failed", "RTC Error", true, false); } else { /* Indicator for the RTC configuration */ RTC_WriteBackupRegister(RTC_BKP_DR0, 0x32F2); } }
//-------------------------------------------------------------- // interne Funktion // RTC Settings einstellen //-------------------------------------------------------------- void P_RTC_Config(void) { // Clock auf LSE einstellen RCC_LSEConfig(RCC_LSE_ON); // warten bis Clock eingestellt while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET); // Clock enable fuer LSE RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); // enable RTC RCC_RTCCLKCmd(ENABLE); // warte bis synconisiert RTC_WaitForSynchro(); // Status-Register beschreiben RTC_WriteBackupRegister(RTC_STATUS_REG, RTC_STATUS_INIT_OK); }
/** * @brief Returns the time entered by user, using Hyperterminal. * @param None * @retval None */ static void RTC_TimeRegulate(void) { RTC_TimeTypeDef RTC_TimeStructure; RTC_DateTypeDef RTC_DateStructure; /* Set Time hh:mm:ss */ RTC_TimeStructure.RTC_H12 = RTC_H12_AM; RTC_TimeStructure.RTC_Hours = 0x08; RTC_TimeStructure.RTC_Minutes = 0x10; RTC_TimeStructure.RTC_Seconds = 0x00; RTC_SetTime(RTC_Format_BCD, &RTC_TimeStructure); /* Set Date Week/Date/Month/Year */ RTC_DateStructure.RTC_WeekDay = 01; RTC_DateStructure.RTC_Date = 0x31; RTC_DateStructure.RTC_Month = 0x12; RTC_DateStructure.RTC_Year = 0x12; RTC_SetDate(RTC_Format_BCD, &RTC_DateStructure); /* Write BkUp DR0 */ RTC_WriteBackupRegister(RTC_BKP_DR0, 0x32F2); }
void PIOS_IAP_SetRequest2(void) { RTC_WriteBackupRegister( MAGIC_REG_2, IAP_MAGIC_WORD_2); }
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 Returns the time entered by user, using Hyperterminal. * @param None * @retval None */ void RTC_TimeRegulate(void) { uint32_t tmp_hh = 0xFF, tmp_mm = 0xFF, tmp_ss = 0xFF; printf("\n\r==============Time Settings=====================================\n\r"); RTC_TimeStructure.RTC_H12 = RTC_H12_AM; printf(" Please Set Hours\n\r"); while (tmp_hh == 0xFF) { tmp_hh = USART_Scanf(0, 23); RTC_TimeStructure.RTC_Hours = tmp_hh; } printf(": %0.2d\n\r", tmp_hh); printf(" Please Set Minutes\n\r"); while (tmp_mm == 0xFF) { tmp_mm = USART_Scanf(0, 59); RTC_TimeStructure.RTC_Minutes = tmp_mm; } printf(": %0.2d\n\r", tmp_mm); printf(" Please Set Seconds\n\r"); while (tmp_ss == 0xFF) { tmp_ss = USART_Scanf(0, 59); RTC_TimeStructure.RTC_Seconds = tmp_ss; } printf(": %0.2d\n\r", tmp_ss); /* Configure the RTC time register */ if(RTC_SetTime(RTC_Format_BIN, &RTC_TimeStructure) == ERROR) { printf("\n\r>> !! RTC Set Time failed. !! <<\n\r"); } else { printf("\n\r>> !! RTC Set Time success. !! <<\n\r"); RTC_TimeShow(); /* Indicator for the RTC configuration */ RTC_WriteBackupRegister(RTC_BKP_DR0, 0x32F2); } tmp_hh = 0xFF; tmp_mm = 0xFF; tmp_ss = 0xFF; printf("\n\r==============Date Settings=====================================\n\r"); printf(" Please Set WeekDay (01-07)\n\r"); while (tmp_hh == 0xFF) { tmp_hh = USART_Scanf(1, 7); RTC_DateStructure.RTC_WeekDay = tmp_hh; } printf(": %0.2d\n\r", tmp_hh); tmp_hh = 0xFF; printf(" Please Set Date (01-31)\n\r"); while (tmp_hh == 0xFF) { tmp_hh = USART_Scanf(1, 31); RTC_DateStructure.RTC_Date = tmp_hh; } printf(": %0.2d\n\r", tmp_hh); printf(" Please Set Month (01-12)\n\r"); while (tmp_mm == 0xFF) { tmp_mm = USART_Scanf(1, 12); RTC_DateStructure.RTC_Month = tmp_mm; } printf(": %0.2d\n\r", tmp_mm); printf(" Please Set Year (00-99)\n\r"); while (tmp_ss == 0xFF) { tmp_ss = USART_Scanf(0, 99); RTC_DateStructure.RTC_Year = tmp_ss; } printf(": %0.2d\n\r", tmp_ss); /* Configure the RTC date register */ if(RTC_SetDate(RTC_Format_BIN, &RTC_DateStructure) == ERROR) { printf("\n\r>> !! RTC Set Date failed. !! <<\n\r"); } else { printf("\n\r>> !! RTC Set Date success. !! <<\n\r"); RTC_DateShow(); /* Indicator for the RTC configuration */ RTC_WriteBackupRegister(RTC_BKP_DR0, 0x32F2); } }
u8 RT_Total_Config(void) { u8 Resualt=0; //表明RTC数据丢失,需要重新配置 if (RTC_ReadBackupRegister(RTC_BKP_DR0) != RTC_First) { rt_kprintf("rtc is not configured\n"); //重新配置RTC RTC_Config(); // Check resualt if ( RTC_Config() != 0) { RTC_Config(); rt_kprintf("rtc configure fail...Reconfig once\r\n"); return 0 ; } else { /* Configure the RTC data register and RTC prescaler */ RTC_InitStructure.RTC_AsynchPrediv = AsynchPrediv; RTC_InitStructure.RTC_SynchPrediv = SynchPrediv; RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; /* Check on RTC init */ if (RTC_Init(&RTC_InitStructure) == ERROR) { rt_kprintf("\n\r /!\\***** RTC Prescaler Config failed ********/!\\ \n\r"); } } //配置完成后,向后备寄存器中写特殊字符0xA5A5 RTC_WriteBackupRegister(RTC_BKP_DR0, RTC_First); Resualt=1; } else { /* Check if the Power On Reset flag is set */ if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != 0) { ;// rt_kprintf("\r\n Power On Reset occurred....\n\r"); } /* Check if the Pin Reset flag is set */ else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != 0) { ;//rt_kprintf("\r\n External Reset occurred....\n\r"); } // rt_kprintf("\r\n No need to configure RTC....\n\r"); /* 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); /* Wait for RTC registers synchronization */ RTC_WaitForSynchro(); Resualt=0; } return Resualt; }
/** * @brief This function handles External lines 15 to 10 interrupt request. * @param None * @retval None */ void EXTI15_10_IRQHandler(void) { if((EXTI_GetITStatus(TAMPER_BUTTON_EXTI_LINE) != RESET) && (ubStartevent !=0) && (uwBackupindex < 11)) { uint16_t Colorx; uint8_t index = 0; if (uwBackupindex < 10) { /* Increment counter */ ubRTCCount++; /* Set LCD background color*/ if((uint8_t)(ubRTCCount % 2) != 0) { LCD_SetBackColor(Blue2); Colorx = White; } else { LCD_SetBackColor(Cyan); Colorx = Black; } /* Get the Current sub seconds and time */ ubSSecondfraction = 1000 - ((uint32_t)((uint32_t)RTC_GetSubSecond() * 1000) / (uint32_t)0x3FF); RTC_GetTime(RTC_Format_BCD, &RTC_StampTimeStruct); LCD_SetFont(&Font12x12); /* Display result on the LCD */ RTC_Time_display(LINE(7 + uwBackupindex), Colorx, RTC_Get_Time(ubSSecondfraction , &RTC_StampTimeStruct)); /* Save time register to Backup register (the first 10 registers are reserved for time) */ RTC_WriteBackupRegister(aBKPDataReg[uwBackupindex],(uint32_t)RTC->TR); /* Save sub second time stamp register (the latest 10 registers are reserved for sub second) */ RTC_WriteBackupRegister(aBKPDataReg[uwBackupindex + 10], ubSSecondfraction); } else { /* Set the LCD Background Color */ LCD_SetBackColor(White); LCD_SetFont(&Font12x12); /* Clear all LCD lines from 7 to 19 */ for (index = 0; index < 13; index++) { LCD_ClearLine(LINE(7 + index)); } /* Reset Counters */ ubRTCCount = 0; uwBackupindex = 0 ; /* Enter to idle */ ubStartevent =0; /* Set the time to 00h 00mn 00s AM */ RTC_TimeStructure.RTC_H12 = RTC_H12_AM; RTC_TimeStructure.RTC_Hours = 0; RTC_TimeStructure.RTC_Minutes = 0; RTC_TimeStructure.RTC_Seconds = 0; RTC_SetTime(RTC_Format_BCD, &RTC_TimeStructure); /* Disable the RTC Clock */ RCC_RTCCLKCmd(DISABLE); LCD_SetFont(&Font16x24); /* Set the LCD Text Color */ LCD_SetTextColor(Blue); /* Display message to the LCD */ LCD_DisplayStringLine(LINE(5), (uint8_t *)MESSAGE1); /* Clear EXTI line 21 */ EXTI_ClearITPendingBit(EXTI_Line21); /* Clear Tamper pin interrupt pending bit */ RTC_ClearITPendingBit(RTC_IT_TAMP1); } uwBackupindex++; } /* Clear the TAMPER EXTI pending bit */ EXTI_ClearITPendingBit(TAMPER_BUTTON_EXTI_LINE); }
void PIOS_IAP_WriteBootCount (uint16_t boot_count) { RTC_WriteBackupRegister ( IAP_BOOTCOUNT, boot_count ); }
void PIOS_IAP_ClearRequest(void) { RTC_WriteBackupRegister( MAGIC_REG_1, 0); RTC_WriteBackupRegister( MAGIC_REG_2, 0); }
/*! * \brief Sets the 1st word of the request sequence. * \param n/a * \return n/a * \retval */ void PIOS_IAP_SetRequest1(void) { RTC_WriteBackupRegister( MAGIC_REG_1, IAP_MAGIC_WORD_1); }