void MX_RTC_BACKUP_Init(void){ RTC_TimeTypeDef sTime; RTC_DateTypeDef sDate; if(HAL_RTCEx_BKUPRead(&hrtc,RTC_BKP_DR1) != TSTWORD){ HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR1, TSTWORD); sTime.Hours = 22; sTime.Minutes = 02; sTime.Seconds = 00; sTime. HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN); sDate.WeekDay = RTC_WEEKDAY_FRIDAY; sDate.Month = RTC_MONTH_APRIL; sDate.Date = 23; sDate.Year = 16; HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN); HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR2,sDate.Month); HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR3,sDate.Date); HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR4,sDate.Year); } else { sDate.Month = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR2); sDate.Date = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR3); sDate.Year = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR4); HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN); } }
/** * @brief Configures the current time and date. * @param None * @retval None */ static void RTC_TimeStampConfig(void) { RTC_DateTypeDef sdatestructure; RTC_TimeTypeDef stimestructure; /*##-3- Configure the Date #################################################*/ /* Set Date: Tuesday February 18th 2014 */ sdatestructure.Year = 0x14; sdatestructure.Month = RTC_MONTH_FEBRUARY; sdatestructure.Date = 0x18; sdatestructure.WeekDay = RTC_WEEKDAY_TUESDAY; if(HAL_RTC_SetDate(&RtcHandle,&sdatestructure,FORMAT_BCD) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-4- Configure the Time #################################################*/ /* Set Time: 08:10:00 */ stimestructure.Hours = 0x08; stimestructure.Minutes = 0x10; stimestructure.Seconds = 0x00; stimestructure.TimeFormat = RTC_HOURFORMAT12_AM; stimestructure.DayLightSaving = RTC_DAYLIGHTSAVING_NONE ; stimestructure.StoreOperation = RTC_STOREOPERATION_RESET; if(HAL_RTC_SetTime(&RtcHandle,&stimestructure,FORMAT_BCD) != HAL_OK) { /* Initialization Error */ Error_Handler(); } }
/** * @brief RTC Initialization function */ void BSP_RTC_Init(void) { RTC_TimeTypeDef sTime; RTC_DateTypeDef sDate; /**Initialize RTC and set the Time and Date */ hrtc_bsp.Instance = RTC; hrtc_bsp.Init.HourFormat = RTC_HOURFORMAT_24; hrtc_bsp.Init.AsynchPrediv = 127; //LSI clock AsyncPrediv 127 HSE clock AsyncPrediv 124 hrtc_bsp.Init.SynchPrediv = 255; //LSI clock SyncPrediv 255 HSE clock SyncPrediv 7999 hrtc_bsp.Init.OutPut = RTC_OUTPUT_DISABLE; hrtc_bsp.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; hrtc_bsp.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; BSP_RTC_MspInit(&hrtc_bsp); HAL_RTC_Init(&hrtc_bsp); sTime.Hours = 0; sTime.Minutes = 0; sTime.Seconds = 0; sTime.SubSeconds = 0; sTime.TimeFormat = RTC_HOURFORMAT12_AM; sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; sTime.StoreOperation = RTC_STOREOPERATION_RESET; HAL_RTC_SetTime(&hrtc_bsp, &sTime, FORMAT_BIN); sDate.WeekDay = RTC_WEEKDAY_MONDAY; sDate.Month = RTC_MONTH_JANUARY; sDate.Date = 1; sDate.Year = 00; HAL_RTC_SetDate(&hrtc_bsp, &sDate, FORMAT_BIN); }
void rtc_write(time_t t) { RTC_DateTypeDef dateStruct; RTC_TimeTypeDef timeStruct; RtcHandle.Instance = RTC; // Convert the time into a tm struct tm *timeinfo = localtime(&t); // Fill RTC structures dateStruct.WeekDay = timeinfo->tm_wday; dateStruct.Month = timeinfo->tm_mon + 1; dateStruct.Date = timeinfo->tm_mday; dateStruct.Year = timeinfo->tm_year - 100; timeStruct.Hours = timeinfo->tm_hour; timeStruct.Minutes = timeinfo->tm_min; timeStruct.Seconds = timeinfo->tm_sec; timeStruct.TimeFormat = RTC_HOURFORMAT12_PM; timeStruct.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; timeStruct.StoreOperation = RTC_STOREOPERATION_RESET; // Change the RTC current date/time HAL_RTC_SetDate(&RtcHandle, &dateStruct, FORMAT_BIN); HAL_RTC_SetTime(&RtcHandle, &timeStruct, FORMAT_BIN); }
/* RTC init function */ void MX_RTC_Init(void) { RTC_TimeTypeDef sTime; RTC_DateTypeDef DateToUpdate; /**Initialize RTC and set the Time and Date */ hrtc.Instance = RTC; hrtc.Init.AsynchPrediv = RTC_AUTO_1_SECOND; hrtc.Init.OutPut = RTC_OUTPUTSOURCE_NONE; HAL_RTC_Init(&hrtc); sTime.Hours = 0x10; sTime.Minutes = 0x6; sTime.Seconds = 0x24; HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD); DateToUpdate.WeekDay = RTC_WEEKDAY_MONDAY; DateToUpdate.Month = RTC_MONTH_AUGUST; DateToUpdate.Date = 0x15; DateToUpdate.Year = 0x16; HAL_RTC_SetDate(&hrtc, &DateToUpdate, RTC_FORMAT_BCD); }
/** * @brief Configure the current time and date. * @param None * @retval None */ static void RTC_CalendarConfig(void) { RTC_DateTypeDef sdatestructure; RTC_TimeTypeDef stimestructure; /*##-1- Configure the Date #################################################*/ /* Set Date: Tuesday April 14th 2015 */ sdatestructure.Year = 0x14; sdatestructure.Month = RTC_MONTH_JANUARY; sdatestructure.Date = 0x01; sdatestructure.WeekDay = RTC_WEEKDAY_TUESDAY; if(HAL_RTC_SetDate(&RTCHandle,&sdatestructure,RTC_FORMAT_BCD) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-2- Configure the Time #################################################*/ /* Set Time: 02:00:00 */ stimestructure.Hours = 0x00; stimestructure.Minutes = 0x00; stimestructure.Seconds = 0x00; stimestructure.TimeFormat = RTC_HOURFORMAT12_AM; stimestructure.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; stimestructure.StoreOperation = RTC_STOREOPERATION_RESET; if(HAL_RTC_SetTime(&RTCHandle,&stimestructure,RTC_FORMAT_BCD) != HAL_OK) { /* Initialization Error */ Error_Handler(); } }
void Clocks_Init(void) { RCC_PeriphCLKInitTypeDef PeriphClkInitStruct; PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; //TODO: на реальном приборе кварц не 8 а 25 ћ√ц. исправить делитель PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_HSE_DIV8; // RTCCLK = 1MHz HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct); __HAL_RCC_RTC_ENABLE(); hrtc.Instance = RTC; hrtc.Init.HourFormat = RTC_HOURFORMAT_24; hrtc.Init.AsynchPrediv = 124; hrtc.Init.SynchPrediv = 7999; hrtc.Init.OutPut = RTC_OUTPUT_DISABLE; hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; HAL_RTC_Init(&hrtc); sTime.Hours = 1; sTime.Minutes = 1; sTime.Seconds = 1; sTime.SubSeconds = 0; sTime.TimeFormat = RTC_HOURFORMAT_24; sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; sTime.StoreOperation = RTC_STOREOPERATION_RESET; HAL_RTC_SetTime(&hrtc, &sTime, FORMAT_BCD); sDate.WeekDay = RTC_WEEKDAY_MONDAY; sDate.Month = RTC_MONTH_JANUARY; sDate.Date = 1; sDate.Year = 0; HAL_RTC_SetDate(&hrtc, &sDate, FORMAT_BCD); }
STATIC void RTC_CalendarConfig(void) { // set the date to 1st Jan 2015 RTC_DateTypeDef date; date.Year = 15; date.Month = 1; date.Date = 1; date.WeekDay = RTC_WEEKDAY_THURSDAY; if(HAL_RTC_SetDate(&RTCHandle, &date, FORMAT_BIN) != HAL_OK) { // init error return; } // set the time to 00:00:00 RTC_TimeTypeDef time; time.Hours = 0; time.Minutes = 0; time.Seconds = 0; time.TimeFormat = RTC_HOURFORMAT12_AM; time.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; time.StoreOperation = RTC_STOREOPERATION_RESET; if (HAL_RTC_SetTime(&RTCHandle, &time, FORMAT_BIN) != HAL_OK) { // init error return; } }
/* RTC init function */ void MX_RTC_Init(void) { RTC_TimeTypeDef sTime; RTC_DateTypeDef sDate; RTC_AlarmTypeDef sAlarm; /**Initialize RTC and set the Time and Date */ hrtc.Instance = RTC; hrtc.Init.HourFormat = RTC_HOURFORMAT_24; hrtc.Init.AsynchPrediv = 127; hrtc.Init.SynchPrediv = 255; hrtc.Init.OutPut = RTC_OUTPUT_DISABLE; hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; HAL_RTC_Init(&hrtc); sTime.Hours = 0; sTime.Minutes = 0; sTime.Seconds = 0; sTime.SubSeconds = 0; sTime.TimeFormat = RTC_HOURFORMAT12_AM; sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; sTime.StoreOperation = RTC_STOREOPERATION_RESET; HAL_RTC_SetTime(&hrtc, &sTime, FORMAT_BIN); sDate.WeekDay = RTC_WEEKDAY_MONDAY; sDate.Month = RTC_MONTH_JANUARY; sDate.Date = 1; sDate.Year = 0; HAL_RTC_SetDate(&hrtc, &sDate, FORMAT_BIN); /**Enable the Alarm A */ sAlarm.AlarmTime.Hours = 0; sAlarm.AlarmTime.Minutes = 0; sAlarm.AlarmTime.Seconds = 0; sAlarm.AlarmTime.SubSeconds = 0; sAlarm.AlarmTime.TimeFormat = RTC_HOURFORMAT12_AM; sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET; sAlarm.AlarmMask = RTC_ALARMMASK_NONE; sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL; sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE; sAlarm.AlarmDateWeekDay = 1; sAlarm.Alarm = RTC_ALARM_A; HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, FORMAT_BIN); /**Enable the Alarm B */ sAlarm.Alarm = RTC_ALARM_B; HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, FORMAT_BIN); /**Enable the WakeUp */ HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 196, RTC_WAKEUPCLOCK_RTCCLK_DIV2); }
/** * @brief Configures the current time and date. * @param None * @retval None */ static void RTC_TimeStampConfig(void) { RTC_DateTypeDef sdatestructure; RTC_TimeTypeDef stimestructure; RTC_TamperTypeDef stamperstructure; /*##-1- Configure the Tamper ###############################################*/ /* Configure Tamper registers */ /* RTC Tamper configured as follow: - Pamper = Tamper1 - Pin selection = PC13 - Trigger = Falling Edge - TimeStamp On Tamper Detection = Enable */ stamperstructure.Filter = RTC_TAMPERFILTER_DISABLE; stamperstructure.Tamper = RTC_TAMPER_1; stamperstructure.PinSelection = RTC_TAMPERPIN_PC13; stamperstructure.Trigger = RTC_TAMPERTRIGGER_FALLINGEDGE; stamperstructure.SamplingFrequency = RTC_TAMPERSAMPLINGFREQ_RTCCLK_DIV4096; stamperstructure.PrechargeDuration = RTC_TAMPERPRECHARGEDURATION_1RTCCLK; stamperstructure.TamperPullUp = RTC_TAMPER_PULLUP_DISABLE; stamperstructure.TimeStampOnTamperDetection = RTC_TIMESTAMPONTAMPERDETECTION_ENABLE; HAL_RTCEx_SetTamper(&RtcHandle, &stamperstructure); /*##-2- Configure the Time Stamp peripheral ################################*/ /* RTC TimeStamp flag Generation: TimeStamp Rising Edge on PC13 Pin */ HAL_RTCEx_SetTimeStamp_IT(&RtcHandle, RTC_TIMESTAMPEDGE_RISING, RTC_TIMESTAMPPIN_PC13); /* Clear the TIMESTAMP interrupt pending bit */ __HAL_RTC_TIMESTAMP_CLEAR_FLAG(&RtcHandle,RTC_FLAG_TSF); /*##-3- Configure the Date #################################################*/ /* Set Date: Tuesday February 18th 2014 */ sdatestructure.Year = 0x14; sdatestructure.Month = RTC_MONTH_FEBRUARY; sdatestructure.Date = 0x18; sdatestructure.WeekDay = RTC_WEEKDAY_TUESDAY; if(HAL_RTC_SetDate(&RtcHandle,&sdatestructure,RTC_FORMAT_BCD) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-4- Configure the Time #################################################*/ /* Set Time: 08:10:00 */ stimestructure.Hours = 0x08; stimestructure.Minutes = 0x10; stimestructure.Seconds = 0x00; stimestructure.TimeFormat = RTC_HOURFORMAT12_AM; stimestructure.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; stimestructure.StoreOperation = RTC_STOREOPERATION_RESET; if(HAL_RTC_SetTime(&RtcHandle,&stimestructure,RTC_FORMAT_BCD) != HAL_OK) { /* Initialization Error */ Error_Handler(); } }
/* RTC init function */ void MX_RTC_Init(void) { RTC_TimeTypeDef sTime; RTC_DateTypeDef sDate; RTC_AlarmTypeDef sAlarm; /**Initialize RTC and set the Time and Date */ hrtc.Instance = RTC; hrtc.Init.HourFormat = RTC_HOURFORMAT_24; hrtc.Init.AsynchPrediv = 127; hrtc.Init.SynchPrediv = 255; hrtc.Init.OutPut = RTC_OUTPUT_DISABLE; hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; HAL_RTC_Init(&hrtc); /* USER CODE BEGIN*/ rtc_backup_init(); if(rtc_check()==0) { sTime.Hours = 16; sTime.Minutes = 52; sTime.Seconds = 50; sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; sTime.StoreOperation = RTC_STOREOPERATION_RESET; HAL_RTC_SetTime(&hrtc, &sTime, FORMAT_BIN); sDate.WeekDay = RTC_WEEKDAY_WEDNESDAY; sDate.Month = RTC_MONTH_APRIL; sDate.Date = 20; sDate.Year = 16; HAL_RTC_SetDate(&hrtc, &sDate, FORMAT_BIN); /**Enable the Alarm A */ sAlarm.AlarmTime.Hours = 16; sAlarm.AlarmTime.Minutes = 53; sAlarm.AlarmTime.Seconds = 1; sAlarm.AlarmTime.SubSeconds = 0; sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET; sAlarm.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY; sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL; sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE; sAlarm.AlarmDateWeekDay = 1; sAlarm.Alarm = RTC_ALARM_A; HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, FORMAT_BIN); } /* USER CODE END*/ /**Enable the WakeUp */ HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 10240, RTC_WAKEUPCLOCK_RTCCLK_DIV16); }
//------------------------------------------------------------------ void RTC_Set_datetime(uint8_t * data) { uint8_t temp[3]; RTC_DateTypeDef sdatestructure; RTC_TimeTypeDef stimestructure; if(data!=NULL) { osMutexWait(rtc_mutex, osWaitForever); sdatestructure.Year =data[0]; sdatestructure.Month = data[1]; sdatestructure.Date = data[2]; sdatestructure.WeekDay= CaculateWeekDay(data[0],data[1],data[2]); if(HAL_RTC_SetDate(&hrtc,&sdatestructure,RTC_FORMAT_BIN) != HAL_OK) { /* Initialization Error */ Error_Handler("set time error"); } stimestructure.Hours = data[3]; stimestructure.Minutes = data[4]; stimestructure.Seconds = data[5]; stimestructure.TimeFormat = RTC_HOURFORMAT12_AM; stimestructure.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; stimestructure.StoreOperation = RTC_STOREOPERATION_RESET; if(HAL_RTC_SetTime(&hrtc,&stimestructure,RTC_FORMAT_BIN) != HAL_OK) { /* Initialization Error */ Error_Handler("set date error"); } #if 0 //flash lcd HAL_RTC_GetTime(&hrtc, &stimestructure, RTC_FORMAT_BIN); temp[0]=stimestructure.Hours; temp[1]=stimestructure.Minutes; temp[2]=stimestructure.Seconds; memcpy(¤t_datetime[3],temp,3); /* Get the RTC current Date */ HAL_RTC_GetDate(&hrtc, &sdatestructure, RTC_FORMAT_BIN); data[0]=sdatestructure.Year; data[1]=sdatestructure.Month; data[2]=sdatestructure.Date; current_datetime[6]=sdatestructure.WeekDay; memcpy(¤t_datetime[0],data,3); #endif if(disp_sort==0) send_message(4); osMutexRelease(rtc_mutex); }//data=null }
/** * @brief Configure the current time and date. * @param None * @retval None */ static void RTC_AlarmConfig(void) { RTC_DateTypeDef sdatestructure; RTC_TimeTypeDef stimestructure; RTC_AlarmTypeDef salarmstructure; /*##-1- Configure the Date #################################################*/ /* Set Date: Tuesday February 18th 2014 */ sdatestructure.Year = 0x14; sdatestructure.Month = RTC_MONTH_FEBRUARY; sdatestructure.Date = 0x18; sdatestructure.WeekDay = RTC_WEEKDAY_TUESDAY; if(HAL_RTC_SetDate(&RtcHandle,&sdatestructure,RTC_FORMAT_BCD) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-2- Configure the Time #################################################*/ /* Set Time: 02:20:00 */ stimestructure.Hours = 0x02; stimestructure.Minutes = 0x20; stimestructure.Seconds = 0x00; stimestructure.TimeFormat = RTC_HOURFORMAT12_AM; stimestructure.DayLightSaving = RTC_DAYLIGHTSAVING_NONE ; stimestructure.StoreOperation = RTC_STOREOPERATION_RESET; if(HAL_RTC_SetTime(&RtcHandle,&stimestructure,RTC_FORMAT_BCD) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-3- Configure the RTC Alarm peripheral #################################*/ /* Set Alarm to 02:20:30 RTC Alarm Generation: Alarm on Hours, Minutes and Seconds */ salarmstructure.Alarm = RTC_ALARM_A; salarmstructure.AlarmDateWeekDay = RTC_WEEKDAY_MONDAY; salarmstructure.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE; salarmstructure.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY; salarmstructure.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_NONE; salarmstructure.AlarmTime.TimeFormat = RTC_HOURFORMAT12_AM; salarmstructure.AlarmTime.Hours = 0x02; salarmstructure.AlarmTime.Minutes = 0x20; salarmstructure.AlarmTime.Seconds = 0x30; salarmstructure.AlarmTime.SubSeconds = 0x56; if(HAL_RTC_SetAlarm_IT(&RtcHandle,&salarmstructure,RTC_FORMAT_BCD) != HAL_OK) { /* Initialization Error */ Error_Handler(); } }
static void RtcSetConfig( void ) { RTC_TimeTypeDef RTC_TimeStruct; RTC_DateTypeDef RTC_DateStruct; //RTC_AlarmTypeDef salarmstructure; /* Enable Ultra low power mode */ HAL_PWREx_EnableUltraLowPower(); /* Enable the fast wake up from Ultra low power mode */ HAL_PWREx_EnableFastWakeUp(); /* Select HSI as system clock source after Wake Up from Stop mode */ __HAL_RCC_WAKEUPSTOP_CLK_CONFIG(RCC_StopWakeUpClock_HSI); /*##-1- Configure the RTC peripheral #######################################*/ /* Configure RTC prescaler and RTC data registers */ /* RTC configured as follow: - Hour Format = Format 24 - Asynch Prediv = Value according to source clock - Synch Prediv = Value according to source clock - OutPut = Output Disable - OutPutPolarity = High Polarity - OutPutType = Open Drain */ RTCHandle.Instance = RTC; RTCHandle.Init.HourFormat = RTC_HOURFORMAT_24; RTCHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV; RTCHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV; RTCHandle.Init.OutPut = RTC_OUTPUT_DISABLE; RTCHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; RTCHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; HAL_RTC_Init(&RTCHandle); //Initiate data and time RTC_TimeStruct.TimeFormat = RTC_HOURFORMAT12_AM; RTC_TimeStruct.Hours = 0x00; RTC_TimeStruct.Minutes = 0x00; RTC_TimeStruct.Seconds = 0x00; RTC_TimeStruct.DayLightSaving = RTC_DAYLIGHTSAVING_NONE ; RTC_TimeStruct.StoreOperation = RTC_STOREOPERATION_RESET; HAL_RTC_SetTime(&RTCHandle,&RTC_TimeStruct,FORMAT_BIN); // RTC_TimeStructInit( &RTC_TimeStruct ); RTC_DateStruct.WeekDay = RTC_WEEKDAY_SATURDAY; RTC_DateStruct.Date = 0x01; RTC_DateStruct.Month = RTC_MONTH_JANUARY; RTC_DateStruct.Year = 0x01; HAL_RTC_SetDate(&RTCHandle,&RTC_DateStruct,FORMAT_BIN); }
/* RTC init function */ void MX_RTC_Init(void) { RTC_TimeTypeDef sTime; RTC_DateTypeDef sDate; /**Initialize RTC Only */ hrtc.Instance = RTC; hrtc.Init.HourFormat = RTC_HOURFORMAT_24; hrtc.Init.AsynchPrediv = 127; hrtc.Init.SynchPrediv = 255; hrtc.Init.OutPut = RTC_OUTPUT_DISABLE; hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE; hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; if (HAL_RTC_Init(&hrtc) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); } /**Initialize RTC and set the Time and Date */ if(HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR0) != 0x32F2){ sTime.Hours = 0x0; sTime.Minutes = 0x0; sTime.Seconds = 0x0; sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; sTime.StoreOperation = RTC_STOREOPERATION_RESET; if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); } sDate.WeekDay = RTC_WEEKDAY_MONDAY; sDate.Month = RTC_MONTH_JANUARY; sDate.Date = 0x1; sDate.Year = 0x0; if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); } HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR0,0x32F2); } /**Enable the WakeUp */ if (HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 5, RTC_WAKEUPCLOCK_CK_SPRE_16BITS) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); } }
/* RTC init function */ void MX_RTC_Init(void) { RTC_TimeTypeDef sTime; RTC_DateTypeDef sDate; /**Initialize RTC and set the Time and Date */ hrtc.Instance = RTC; hrtc.Init.HourFormat = RTC_HOURFORMAT_24; hrtc.Init.AsynchPrediv = 127; hrtc.Init.SynchPrediv = 255; hrtc.Init.OutPut = RTC_OUTPUT_DISABLE; hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; HAL_RTC_Init(&hrtc); sTime.Hours = 0x17; sTime.Minutes = 0x3B; sTime.Seconds = 0x0; sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; sTime.StoreOperation = RTC_STOREOPERATION_RESET; HAL_RTC_SetTime(&hrtc, &sTime, FORMAT_BIN); sDate.WeekDay = RTC_WEEKDAY_MONDAY; sDate.Month = RTC_MONTH_JANUARY; sDate.Date = 0x1; sDate.Year = 0x0; HAL_RTC_SetDate(&hrtc, &sDate, FORMAT_BIN); /**Enable the Alarm A */ sAlarm.AlarmTime.Hours = 0x17; sAlarm.AlarmTime.Minutes = 0x3B; sAlarm.AlarmTime.Seconds = 0x9; sAlarm.AlarmTime.SubSeconds = 0x0; sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET; sAlarm.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY; sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL; sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE; sAlarm.AlarmDateWeekDay = 0x1; sAlarm.Alarm = RTC_ALARM_A; // HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, FORMAT_BIN); //__HAL_RCC_RTC_ENABLE(); /*##-4- Configure the NVIC for RTC Alarm ###################################*/ }
void rtcSetDateTime(RTC_DateTypeDef *date, RTC_TimeTypeDef *time) { if(HAL_RTC_SetDate(&RtcHandle,date,FORMAT_BIN) != HAL_OK) { /* Initialization Error */ Error_Handler(); } if(HAL_RTC_SetTime(&RtcHandle,time,FORMAT_BIN) != HAL_OK) { /* Error */ Error_Handler(); } }
/** * @brief Configure the current time and date. * @param None * @retval None */ static void RTC_AlarmConfig(void) { RTC_DateTypeDef sdatestructure; RTC_TimeTypeDef stimestructure; RTC_AlarmTypeDef salarmstructure; /*##-1- Configure the Date #################################################*/ /* Set Date: Tuesday February 18th 2014 */ sdatestructure.Year = 0x14; sdatestructure.Month = RTC_MONTH_FEBRUARY; sdatestructure.Date = 0x18; sdatestructure.WeekDay = RTC_WEEKDAY_TUESDAY; if(HAL_RTC_SetDate(&RtcHandle,&sdatestructure,RTC_FORMAT_BCD) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-2- Configure the Time #################################################*/ /* Set Time: 02:20:00 */ stimestructure.Hours = 0x02; stimestructure.Minutes = 0x20; stimestructure.Seconds = 0x00; if(HAL_RTC_SetTime(&RtcHandle,&stimestructure,RTC_FORMAT_BCD) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-3- Configure the RTC Alarm peripheral #################################*/ /* Set Alarm to 02:20:30 RTC Alarm Generation: Alarm on Hours, Minutes and Seconds */ salarmstructure.Alarm = RTC_ALARM_A; salarmstructure.AlarmTime.Hours = 0x02; salarmstructure.AlarmTime.Minutes = 0x20; salarmstructure.AlarmTime.Seconds = 0x30; if(HAL_RTC_SetAlarm_IT(&RtcHandle,&salarmstructure,RTC_FORMAT_BCD) != HAL_OK) { /* Initialization Error */ Error_Handler(); } }
mp_obj_t pyb_rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) { rtc_init_finalise(); if (n_args == 1) { // get date and time // note: need to call get time then get date to correctly access the registers RTC_DateTypeDef date; RTC_TimeTypeDef time; HAL_RTC_GetTime(&RTCHandle, &time, FORMAT_BIN); HAL_RTC_GetDate(&RTCHandle, &date, FORMAT_BIN); mp_obj_t tuple[8] = { mp_obj_new_int(2000 + date.Year), mp_obj_new_int(date.Month), mp_obj_new_int(date.Date), mp_obj_new_int(date.WeekDay), mp_obj_new_int(time.Hours), mp_obj_new_int(time.Minutes), mp_obj_new_int(time.Seconds), mp_obj_new_int(rtc_subsec_to_us(time.SubSeconds)), }; return mp_obj_new_tuple(8, tuple); } else { // set date and time mp_obj_t *items; mp_obj_get_array_fixed_n(args[1], 8, &items); RTC_DateTypeDef date; date.Year = mp_obj_get_int(items[0]) - 2000; date.Month = mp_obj_get_int(items[1]); date.Date = mp_obj_get_int(items[2]); date.WeekDay = mp_obj_get_int(items[3]); HAL_RTC_SetDate(&RTCHandle, &date, FORMAT_BIN); RTC_TimeTypeDef time; time.Hours = mp_obj_get_int(items[4]); time.Minutes = mp_obj_get_int(items[5]); time.Seconds = mp_obj_get_int(items[6]); time.SubSeconds = rtc_us_to_subsec(mp_obj_get_int(items[7])); time.TimeFormat = RTC_HOURFORMAT12_AM; time.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; time.StoreOperation = RTC_STOREOPERATION_SET; HAL_RTC_SetTime(&RTCHandle, &time, FORMAT_BIN); return mp_const_none; } }
void RtcInit( void ) { RtcCalendar_t rtcInit; if( RtcInitialized == false ) { __HAL_RCC_RTC_ENABLE( ); RtcHandle.Instance = RTC; RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24; RtcHandle.Init.AsynchPrediv = PREDIV_A; // RTC_ASYNCH_PREDIV; RtcHandle.Init.SynchPrediv = PREDIV_S; // RTC_SYNCH_PREDIV; RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE; RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; HAL_RTC_Init( &RtcHandle ); // Set Date: Friday 1st of January 2000 rtcInit.CalendarDate.Year = 0; rtcInit.CalendarDate.Month = RTC_MONTH_JANUARY; rtcInit.CalendarDate.Date = 1; rtcInit.CalendarDate.WeekDay = RTC_WEEKDAY_SATURDAY; HAL_RTC_SetDate( &RtcHandle, &rtcInit.CalendarDate, RTC_FORMAT_BIN ); // Set Time: 00:00:00 rtcInit.CalendarTime.Hours = 0; rtcInit.CalendarTime.Minutes = 0; rtcInit.CalendarTime.Seconds = 0; rtcInit.CalendarTime.SecondFraction = 0; rtcInit.CalendarTime.TimeFormat = RTC_HOURFORMAT12_AM; rtcInit.CalendarTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; rtcInit.CalendarTime.StoreOperation = RTC_STOREOPERATION_RESET; HAL_RTC_SetTime( &RtcHandle, &rtcInit.CalendarTime, RTC_FORMAT_BIN ); // Enable Direct Read of the calendar registers (not through Shadow registers) HAL_RTCEx_EnableBypassShadow( &RtcHandle ); HAL_NVIC_SetPriority( RTC_Alarm_IRQn, 1, 0 ); HAL_NVIC_EnableIRQ( RTC_Alarm_IRQn ); RtcInitialized = true; } }
/** * @brief Configure the current time and date. * @param None * @retval None */ static void RTC_CalendarConfig(void) { RTC_DateTypeDef sdatestructure; RTC_TimeTypeDef stimestructure; /*##-1- Configure the Date #################################################*/ /* Set Date: Friday January 1st 2016 */ sdatestructure.Year = 0x16; sdatestructure.Month = RTC_MONTH_JANUARY; sdatestructure.Date = 0x01; sdatestructure.WeekDay = RTC_WEEKDAY_FRIDAY; if(HAL_RTC_SetDate(&RtcHandle,&sdatestructure,RTC_FORMAT_BCD) != HAL_OK) { /* Initialization Error */ //Error_Handler(); while(1) {} } /*##-2- Configure the Time #################################################*/ /* Set Time: 00:00:00 */ stimestructure.Hours = 0x00; stimestructure.Minutes = 0x00; stimestructure.Seconds = 0x00; stimestructure.TimeFormat = RTC_HOURFORMAT12_AM; stimestructure.DayLightSaving = RTC_DAYLIGHTSAVING_NONE ; stimestructure.StoreOperation = RTC_STOREOPERATION_RESET; if (HAL_RTC_SetTime(&RtcHandle, &stimestructure, RTC_FORMAT_BCD) != HAL_OK) { /* Initialization Error */ //Error_Handler(); while(1) {} } /*##-3- Writes a data in a RTC Backup data Register1 #######################*/ HAL_RTCEx_BKUPWrite(&RtcHandle, RTC_BKP_DR1, 0x32F2); }
/* RTC init function */ void MX_RTC_Init(void) { RTC_TimeTypeDef sTime; RTC_DateTypeDef sDate; /**Initialize RTC and set the Time and Date */ hrtc.Instance = RTC; hrtc.Init.HourFormat = RTC_HOURFORMAT_24; hrtc.Init.AsynchPrediv = 127; hrtc.Init.SynchPrediv = 255; hrtc.Init.OutPut = RTC_OUTPUT_DISABLE; hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; HAL_RTC_Init(&hrtc); sTime.Hours = 0x0; sTime.Minutes = 0x0; sTime.Seconds = 0x0; sTime.TimeFormat = RTC_HOURFORMAT12_AM; sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; sTime.StoreOperation = RTC_STOREOPERATION_RESET; HAL_RTC_SetTime(&hrtc, &sTime, FORMAT_BCD); sDate.WeekDay = RTC_WEEKDAY_MONDAY; sDate.Month = RTC_MONTH_JANUARY; sDate.Date = 0x1; sDate.Year = 0x0; HAL_RTC_SetDate(&hrtc, &sDate, FORMAT_BCD); /**Enable Calibrartion */ HAL_RTCEx_SetCalibrationOutPut(&hrtc, RTC_CALIBOUTPUT_1HZ); /**Enable the reference Clock input */ HAL_RTCEx_SetRefClock(&hrtc); }
void RtcInit( void ) { RtcCalendar_t rtcInit; if( RtcInitalized == false ) { __HAL_RCC_RTC_ENABLE( ); RtcHandle.Instance = RTC; RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24; RtcHandle.Init.AsynchPrediv = 3; RtcHandle.Init.SynchPrediv = 3; RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE; RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; HAL_RTC_Init( &RtcHandle ); // Set Date: Friday 1st of January 2000 rtcInit.CalendarDate.Year = 0; rtcInit.CalendarDate.Month = 1; rtcInit.CalendarDate.Date = 1; rtcInit.CalendarDate.WeekDay = RTC_WEEKDAY_SATURDAY; HAL_RTC_SetDate( &RtcHandle, &rtcInit.CalendarDate, RTC_FORMAT_BIN ); // Set Time: 00:00:00 rtcInit.CalendarTime.Hours = 0; rtcInit.CalendarTime.Minutes = 0; rtcInit.CalendarTime.Seconds = 0; rtcInit.CalendarTime.TimeFormat = RTC_HOURFORMAT12_AM; rtcInit.CalendarTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; rtcInit.CalendarTime.StoreOperation = RTC_STOREOPERATION_RESET; HAL_RTC_SetTime( &RtcHandle, &rtcInit.CalendarTime, RTC_FORMAT_BIN ); HAL_NVIC_SetPriority( RTC_Alarm_IRQn, 4, 0 ); HAL_NVIC_EnableIRQ( RTC_Alarm_IRQn ); RtcInitalized = true; } }
/* RTC init function */ static void MX_RTC_Init(void) { //RTC_TimeTypeDef sTime; //RTC_DateTypeDef DateToUpdate; /**Initialize RTC and set the Time and Date */ hrtc.Instance = RTC; hrtc.Init.AsynchPrediv = RTC_AUTO_1_SECOND; hrtc.Init.OutPut = RTC_OUTPUTSOURCE_NONE; if (HAL_RTC_Init(&hrtc) != HAL_OK) { Error_Handler(); } #if 0 sTime.Hours = 0x1; sTime.Minutes = 0x0; sTime.Seconds = 0x0; if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK) { Error_Handler(); } DateToUpdate.WeekDay = RTC_WEEKDAY_MONDAY; DateToUpdate.Month = RTC_MONTH_JANUARY; DateToUpdate.Date = 0x1; DateToUpdate.Year = 0x0; if (HAL_RTC_SetDate(&hrtc, &DateToUpdate, RTC_FORMAT_BCD) != HAL_OK) { Error_Handler(); } #endif return; }
void mo_Set_UnixTime_hal(time_t unix_time) { struct tm *tmTemp = gmtime( &unix_time );//localtime( &unix_time); RTC_DateTypeDef sdatestructure; RTC_TimeTypeDef stimestructure; /*##-1- Configure the Date #################################################*/ sdatestructure.Year = tmTemp->tm_year + 1904; sdatestructure.Month = tmTemp->tm_mon + 1; sdatestructure.Date = tmTemp->tm_mday; if(HAL_RTC_SetDate(&RtcHandle,&sdatestructure,RTC_FORMAT_BCD) != HAL_OK) { /* Initialization Error */ //Error_Handler(); while(1) {} } /*##-2- Configure the Time #################################################*/ stimestructure.Hours = tmTemp->tm_hour; stimestructure.Minutes = tmTemp->tm_min; stimestructure.Seconds = tmTemp->tm_sec; stimestructure.TimeFormat = RTC_HOURFORMAT12_AM; stimestructure.DayLightSaving = RTC_DAYLIGHTSAVING_NONE ; stimestructure.StoreOperation = RTC_STOREOPERATION_RESET; if (HAL_RTC_SetTime(&RtcHandle, &stimestructure, RTC_FORMAT_BCD) != HAL_OK) { /* Initialization Error */ //Error_Handler(); while(1) {} } /*##-3- Writes a data in a RTC Backup data Register1 #######################*/ HAL_RTCEx_BKUPWrite(&RtcHandle, RTC_BKP_DR1, 0x32F2); }
void RTC_CalendarConfig(time_t unixtime) { DateTime date; RTC_DateTypeDef sdatestructure; RTC_TimeTypeDef stimestructure; convertUnixTimeToDate(unixtime, &date); /*##-1- Configure the Date #################################################*/ /* Set Date: Tuesday February 18th 2014 */ sdatestructure.Year = (uint8_t)(date.year - 2000); sdatestructure.Month = date.month; sdatestructure.Date = date.day; sdatestructure.WeekDay = date.dayOfWeek; if(HAL_RTC_SetDate(&RtcHandle,&sdatestructure,FORMAT_BIN) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-2- Configure the Time #################################################*/ /* Set Time: 02:00:00 */ stimestructure.Hours = date.hours; stimestructure.Minutes = date.minutes; stimestructure.Seconds = date.seconds; stimestructure.TimeFormat = RTC_HOURFORMAT12_AM; stimestructure.DayLightSaving = RTC_DAYLIGHTSAVING_NONE ; stimestructure.StoreOperation = RTC_STOREOPERATION_RESET; if (HAL_RTC_SetTime(&RtcHandle, &stimestructure, FORMAT_BIN) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-3- Writes a data in a RTC Backup data Register0 #######################*/ HAL_RTCEx_BKUPWrite(&RtcHandle, RTC_BKP_DR0, 0x32F2); }
/** * @brief Configure the current time and date and activate timestamp. * @param None * @retval None */ static void RTC_TimeStampConfig(void) { RTC_DateTypeDef sdatestructure; RTC_TimeTypeDef stimestructure; /*##-1- Configure the Time Stamp peripheral ################################*/ /* RTC TimeStamp generation: TimeStamp Rising Edge on PC.13 Pin */ HAL_RTCEx_SetTimeStamp_IT(&RtcHandle, RTC_TIMESTAMPEDGE_RISING, RTC_TIMESTAMPPIN_PC13); /*##-2- Configure the Date #################################################*/ /* Set Date: Monday April 14th 2014 */ sdatestructure.Year = 0x14; sdatestructure.Month = RTC_MONTH_APRIL; sdatestructure.Date = 0x14; sdatestructure.WeekDay = RTC_WEEKDAY_MONDAY; if(HAL_RTC_SetDate(&RtcHandle,&sdatestructure, RTC_FORMAT_BCD) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-3- Configure the Time #################################################*/ /* Set Time: 08:10:00 */ stimestructure.Hours = 0x08; stimestructure.Minutes = 0x10; stimestructure.Seconds = 0x00; stimestructure.SubSeconds = 0x00; stimestructure.TimeFormat = RTC_HOURFORMAT12_AM; stimestructure.DayLightSaving = RTC_DAYLIGHTSAVING_NONE ; stimestructure.StoreOperation = RTC_STOREOPERATION_RESET; if(HAL_RTC_SetTime(&RtcHandle,&stimestructure, RTC_FORMAT_BCD) != HAL_OK) { /* Initialization Error */ Error_Handler(); } }
/*! * @brief Configures the RTC timer * @note The timer is based on the RTC * @param none * @retval none */ static void HW_RTC_SetConfig( void ) { RTC_TimeTypeDef RTC_TimeStruct; RTC_DateTypeDef RTC_DateStruct; RtcHandle.Instance = RTC; RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24; RtcHandle.Init.AsynchPrediv = PREDIV_A; /* RTC_ASYNCH_PREDIV; */ RtcHandle.Init.SynchPrediv = PREDIV_S; /* RTC_SYNCH_PREDIV; */ RtcHandle.Init.OutPut = RTC_OUTPUT; RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; HAL_RTC_Init( &RtcHandle ); /*Monday 1st January 2016*/ RTC_DateStruct.Year = 0; RTC_DateStruct.Month = RTC_MONTH_JANUARY; RTC_DateStruct.Date = 1; RTC_DateStruct.WeekDay = RTC_WEEKDAY_MONDAY; HAL_RTC_SetDate(&RtcHandle , &RTC_DateStruct, RTC_FORMAT_BIN); /*at 0:0:0*/ RTC_TimeStruct.Hours = 0; RTC_TimeStruct.Minutes = 0; RTC_TimeStruct.Seconds = 0; RTC_TimeStruct.TimeFormat = 0; RTC_TimeStruct.SubSeconds = 0; RTC_TimeStruct.StoreOperation = RTC_DAYLIGHTSAVING_NONE; RTC_TimeStruct.DayLightSaving = RTC_STOREOPERATION_RESET; HAL_RTC_SetTime(&RtcHandle , &RTC_TimeStruct, RTC_FORMAT_BIN); /*Enable Direct Read of the calendar registers (not through Shadow) */ HAL_RTCEx_EnableBypassShadow(&RtcHandle); }
static void rtc_configure_time_and_date() { RTC_TimeTypeDef mTime; RTC_DateTypeDef mDate; mDate.WeekDay = 1; mDate.Month = 1; mDate.Date = 1; mDate.Year = 2; if (HAL_RTC_SetDate(&RtcHandle, &mDate, RTC_FORMAT_BIN) != HAL_OK) { error("Date set failed\n"); } mTime.Hours = 0; mTime.Minutes = 0; mTime.Seconds = 0; mTime.TimeFormat = RTC_HOURFORMAT_24; mTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; mTime.StoreOperation = RTC_STOREOPERATION_RESET; if (HAL_RTC_SetTime(&RtcHandle, &mTime, RTC_FORMAT_BIN) != HAL_OK) { error("Time set failed\n"); } }
void rtc_write(time_t t) { RTC_DateTypeDef dateStruct; RTC_TimeTypeDef timeStruct; RtcHandle.Instance = RTC; // Convert the time into a tm struct tm *timeinfo = localtime(&t); // Fill RTC structures dateStruct.WeekDay = timeinfo->tm_wday; dateStruct.Month = timeinfo->tm_mon + 1; dateStruct.Date = timeinfo->tm_mday; dateStruct.Year = timeinfo->tm_year - 100; timeStruct.Hours = timeinfo->tm_hour; timeStruct.Minutes = timeinfo->tm_min; timeStruct.Seconds = timeinfo->tm_sec; // Change the RTC current date/time HAL_RTC_SetDate(&RtcHandle, &dateStruct, FORMAT_BIN); HAL_RTC_SetTime(&RtcHandle, &timeStruct, FORMAT_BIN); }