//------------------------------------------------------------------- //read date time void RTC_Read_datetime(uint8_t * data,uint8_t flag) { uint8_t temp[3]; //first read tiem ,then read date, or not time is not run; RTC_DateTypeDef sdatestructureget; RTC_TimeTypeDef stimestructureget; HAL_RTCStateTypeDef status; if(data!=NULL) { osMutexWait(rtc_mutex, osWaitForever); /* Get the RTC current Time */ HAL_RTC_GetTime(&hrtc, &stimestructureget, RTC_FORMAT_BIN); temp[0]=stimestructureget.Hours; temp[1]=stimestructureget.Minutes; temp[2]=stimestructureget.Seconds; memcpy(¤t_datetime[3],temp,3); /* Get the RTC current Date */ HAL_RTC_GetDate(&hrtc, &sdatestructureget, RTC_FORMAT_BIN); data[0]=sdatestructureget.Year; data[1]=sdatestructureget.Month; data[2]=sdatestructureget.Date; current_datetime[6]=sdatestructureget.WeekDay; memcpy(¤t_datetime[0],data,3); if(flag==1) { memcpy(data,temp,3); } osMutexRelease(rtc_mutex); } }
/** * @brief Displays the current time and date. * @param None * @retval None */ static void RTC_CalendarShow(void) { RTC_DateTypeDef sdatestructureget; RTC_TimeTypeDef stimestructureget; #ifdef USE_LCD /* Get the RTC current Time */ HAL_RTC_GetTime(&RtcHandle, &stimestructureget, RTC_FORMAT_BCD); /* Get the RTC current Date */ HAL_RTC_GetDate(&RtcHandle, &sdatestructureget, RTC_FORMAT_BCD); /* Set the Back Color */ BSP_LCD_SetBackColor(LCD_COLOR_WHITE); /* Set the Text Color */ BSP_LCD_SetTextColor(LCD_COLOR_BLUE); BSP_LCD_DisplayStringAtLine(6, (uint8_t *)"Current Time Display"); RTC_Time_display(7, LCD_COLOR_BLACK , RTC_Get_Time(&stimestructureget)); #else /* Get the RTC current Time */ HAL_RTC_GetTime(&RtcHandle, &stimestructureget, RTC_FORMAT_BIN); /* Get the RTC current Date */ HAL_RTC_GetDate(&RtcHandle, &sdatestructureget, RTC_FORMAT_BIN); /* Display time Format : hh:mm:ss */ sprintf((char*)aShowTime,"%0.2d:%0.2d:%0.2d", stimestructureget.Hours, stimestructureget.Minutes, stimestructureget.Seconds); /* Display date Format : mm-dd-yy */ sprintf((char*)aShowDate,"%0.2d-%0.2d-%0.2d", sdatestructureget.Month, sdatestructureget.Date, 2000 + sdatestructureget.Year); #endif /* USE_LCD */ }
/* StartTaskTFT function */ void StartTaskTFT(void const * argument) { /* USER CODE BEGIN StartTaskTFT */ RTC_TimeTypeDef sTime; RTC_DateTypeDef sDate; char buf[100]; sprintf(buf, "tft run\r\n"); HAL_UART_Transmit(&huart1,(uint8_t*)buf,strlen(buf),100); SSD1289_Init(); LCD_Clear(yellow); memset(buf,0,100); LCD_WriteString_5x7(20, 240 - 30, "Hello world.", red, yellow, 0, 1); LCD_WriteString_5x7(20, 240 - 30 - 20, "STM32F103VET6", green, yellow, 0, 1); /* Infinite loop */ for(;;) { HAL_RTC_GetDate(&hrtc, &sDate, FORMAT_BIN); HAL_RTC_GetTime(&hrtc, &sTime, FORMAT_BIN); memset(buf,0,100); sprintf(buf, "Date: %02d/%02d/%02d", sDate.Date, sDate.Month, sDate.Year); LCD_WriteString_5x7(50,100, buf, magneta, yellow,0, 2); memset(buf,0,100); sprintf(buf, "Time: %02d:%02d:%02d", sTime.Hours, sTime.Minutes, sTime.Seconds); LCD_WriteString_5x7(50, 160, buf, magneta, yellow,0, 2); LCD_WriteString_5x7(50, 50, gbuf, green, yellow,0, 2); osDelay(500); } /* USER CODE END StartTaskTFT */ }
/* RTC Registers RTC_WeekDay 1=monday, 2=tuesday, ..., 7=sunday RTC_Month 1=january, 2=february, ..., 12=december RTC_Date day of the month 1-31 RTC_Year year 0-99 struct tm tm_sec seconds after the minute 0-61 tm_min minutes after the hour 0-59 tm_hour hours since midnight 0-23 tm_mday day of the month 1-31 tm_mon months since January 0-11 tm_year years since 1900 tm_wday days since Sunday 0-6 tm_yday days since January 1 0-365 tm_isdst Daylight Saving Time flag */ time_t rtc_read(void) { RTC_DateTypeDef dateStruct; RTC_TimeTypeDef timeStruct; struct tm timeinfo; RtcHandle.Instance = RTC; // Read actual date and time // Warning: the time must be read first! HAL_RTC_GetTime(&RtcHandle, &timeStruct, FORMAT_BIN); HAL_RTC_GetDate(&RtcHandle, &dateStruct, FORMAT_BIN); // Setup a tm structure based on the RTC timeinfo.tm_wday = dateStruct.WeekDay; timeinfo.tm_mon = dateStruct.Month - 1; timeinfo.tm_mday = dateStruct.Date; timeinfo.tm_year = dateStruct.Year + 100; timeinfo.tm_hour = timeStruct.Hours; timeinfo.tm_min = timeStruct.Minutes; timeinfo.tm_sec = timeStruct.Seconds; // Convert to timestamp time_t t = mktime(&timeinfo); return t; }
uint32_t RTC_CalendarShow(uint8_t* showtime) { uint32_t current_time; RTC_DateTypeDef sdatestructureget; RTC_TimeTypeDef stimestructureget; /* Get the RTC current Time */ HAL_RTC_GetTime(&RTCHandle, &stimestructureget, RTC_FORMAT_BIN); /* Get the RTC current Date */ HAL_RTC_GetDate(&RTCHandle, &sdatestructureget, RTC_FORMAT_BIN); #ifdef RTC_LSI current_time = ( (1000 - (stimestructureget.SubSeconds)*1000/1032) + (stimestructureget.Seconds )*1000+ (stimestructureget.Minutes )*1000*60 ); #endif #ifdef RTC_LSE current_time = ( (1000 - (stimestructureget.SubSeconds)*1000/1023) + (stimestructureget.Seconds )*1000+ (stimestructureget.Minutes )*1000*60 ); #endif current_time &= 0x00FFFFFF; return current_time; }
/* StartDefaultTask function */ void StartDefaultTask(void const * argument) { /* init code for FATFS */ MX_FATFS_Init(); /* USER CODE BEGIN 5 */ uint8_t sec = 0; RTC_TimeTypeDef sTime; RTC_DateTypeDef sDate; char buf[50]; sprintf(buf, "idle run\r\n"); HAL_UART_Transmit(&huart1,(uint8_t*)buf,strlen(buf),100); /* Infinite loop */ for(;;) { HAL_RTC_GetDate(&hrtc, &sDate, FORMAT_BIN); HAL_RTC_GetTime(&hrtc, &sTime, FORMAT_BIN); //memset(buf,0,255); if(sec != sTime.Seconds){ sec = sTime.Seconds; memset(buf,0,50); sprintf(buf, "Date: %02d/%02d/%02d Time: %02d:%02d:%02d\r\n\0", sDate.Date, sDate.Month, sDate.Year, sTime.Hours, sTime.Minutes, sTime.Seconds); HAL_UART_Transmit(&huart1,(uint8_t*)buf,strlen(buf),100); } osDelay(1000); } /* USER CODE END 5 */ }
void rtcGetDateTime(RTC_DateTypeDef *date, RTC_TimeTypeDef *time) { /* Get the RTC current Time */ HAL_RTC_GetTime(&RtcHandle, time, FORMAT_BIN); /* Get the RTC current Date */ HAL_RTC_GetDate(&RtcHandle, date, FORMAT_BIN); }
/** * @brief EXTI line detection callbacks * @param GPIO_Pin: Specifies the pins connected EXTI line * @retval None */ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { if(GPIO_Pin == KEY_BUTTON_PIN) { HAL_RTC_GetTime(&RTCHandle, &RTC_TimeStructure, RTC_FORMAT_BIN); HAL_RTC_GetDate(&RTCHandle, &RTC_DateStructure, RTC_FORMAT_BIN); /* Set the alarm to current time + 5s */ RTC_AlarmStructure.Alarm = RTC_ALARM_A; RTC_AlarmStructure.AlarmTime.TimeFormat = RTC_TimeStructure.TimeFormat; RTC_AlarmStructure.AlarmTime.Hours = RTC_TimeStructure.Hours; RTC_AlarmStructure.AlarmTime.Minutes = RTC_TimeStructure.Minutes; RTC_AlarmStructure.AlarmTime.Seconds = (RTC_TimeStructure.Seconds + 0x05) % 60; RTC_AlarmStructure.AlarmDateWeekDay = 0x31; RTC_AlarmStructure.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE; RTC_AlarmStructure.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY | RTC_ALARMMASK_HOURS | RTC_ALARMMASK_MINUTES; RTC_AlarmStructure.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_NONE; /* The Following Wakeup sequence is highly recommended prior to each Standby mode entry mainly when using more than one wakeup source this is to not miss any wakeup event: - Disable all used wakeup sources, - Clear all related wakeup flags, - Re-enable all used wakeup sources, - Enter the Standby mode. */ /*## Disable all used wakeup sources #####################################*/ /* Disable Wake-up timer */ HAL_PWR_DisableWakeUpPin(PWR_WAKEUP_PIN1); /* Disable RTC Alarm */ HAL_RTC_DeactivateAlarm(&RTCHandle, RTC_ALARM_A); /*## Clear all related wakeup flags ######################################*/ /* Clear PWR wake up Flag */ __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU); /* Clear the Alarm Flag */ __HAL_RTC_ALARM_CLEAR_FLAG(&RTCHandle, RTC_FLAG_ALRAF); /*## Re-enable all used wakeup sources ###################################*/ /* Set RTC alarm */ if(HAL_RTC_SetAlarm_IT(&RTCHandle, &RTC_AlarmStructure, RTC_FORMAT_BIN) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /* Enable WKUP pin */ HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN1); /* Turn LED1 off */ BSP_LED_Off(LED1); /*## Enter Standby Mode ##################################################*/ HAL_PWR_EnterSTANDBYMode(); } }
DWORD get_fattime(void) { rtc_init_finalise(); RTC_TimeTypeDef time; RTC_DateTypeDef date; HAL_RTC_GetTime(&RTCHandle, &time, FORMAT_BIN); HAL_RTC_GetDate(&RTCHandle, &date, FORMAT_BIN); return ((2000 + date.Year - 1980) << 25) | ((date.Month) << 21) | ((date.Date) << 16) | ((time.Hours) << 11) | ((time.Minutes) << 5) | (time.Seconds / 2); }
static RtcCalendar_t RtcGetCalendar( void ) { RtcCalendar_t calendar; HAL_RTC_GetTime( &RtcHandle, &calendar.CalendarTime, RTC_FORMAT_BIN ); HAL_RTC_GetDate( &RtcHandle, &calendar.CalendarDate, RTC_FORMAT_BIN ); calendar.CalendarCentury = Century; RtcCheckCalendarRollOver( calendar.CalendarDate.Year ); return calendar; }
/// \function time() /// Returns the number of seconds, as an integer, since 1/1/2000. STATIC mp_obj_t time_time(void) { // 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); return mp_obj_new_int(timeutils_seconds_since_2000(2000 + date.Year, date.Month, date.Date, time.Hours, time.Minutes, time.Seconds)); }
//------------------------------------------------------------------ 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 }
/* Conditions: 0 Exit points: 1 M = 0 - 1 + 2 = 3 Cyclomatic complexity 1 */ uint32_t fp_get_rtc_time(void) { RTC_TimeTypeDef sTime; uint32_t curTime; HAL_RTC_GetTime(&rtc, &sTime, FORMAT_BIN); curTime = (sTime.Hours * 3600) + (sTime.Minutes * 60) + (sTime.Seconds); return curTime; }
/** * @brief Display the current time. * @param showtime : pointer to buffer * @retval None */ static void RTC_TimeShow(uint8_t* showtime) { RTC_DateTypeDef sdatestructureget; RTC_TimeTypeDef stimestructureget; /* Get the RTC current Time */ HAL_RTC_GetTime(&RtcHandle, &stimestructureget, RTC_FORMAT_BIN); /* Get the RTC current Date */ HAL_RTC_GetDate(&RtcHandle, &sdatestructureget, RTC_FORMAT_BIN); /* Display time Format : hh:mm:ss */ sprintf((char*)showtime,"%02d:%02d:%02d",stimestructureget.Hours, stimestructureget.Minutes, stimestructureget.Seconds); }
int main(void) { /* USER CODE BEGIN 1 */ trace_printf("Hello\n"); RTC_TimeTypeDef Tim; RTC_DateTypeDef Dat; /* USER CODE END 1 */ /* MCU Configuration----------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* Configure the system clock */ SystemClock_Config(); /* Initialize all configured peripherals */ MX_GPIO_Init(); /* USER CODE BEGIN 2 */ /* Initialize LED 4 */ BSP_LED_Init(LED4); /* Initialize RTC */ BSP_RTC_Init(); BSP_RTC_Alarm_Init(AlarmA,0,0,5,RTC_ALARMDATEWEEKDAYSEL_DATE); //Generate alarm interrupt /* Initialize UART with 115200 baud rate */ BSP_UART_Init(115200); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ __HAL_RTC_ALARM_CLEAR_FLAG(&hrtc_bsp,RTC_ISR_ALRAF); __HAL_RTC_ALARM_CLEAR_FLAG(&hrtc_bsp,RTC_ISR_ALRBF); HAL_RTC_GetTime(&hrtc_bsp,&Tim,FORMAT_BIN); HAL_RTC_GetDate(&hrtc_bsp,&Dat,FORMAT_BIN); Tim.SubSeconds = 1000 - ((hrtc_bsp.Instance->SSR*1000)/hrtc_bsp.Init.SynchPrediv); // Count msec uprintf("\x1b[3;1H %d02 : %d02 : %d02 : %d04",Tim.Hours,Tim.Minutes,Tim.Seconds,Tim.SubSeconds); // uprintf("\x1b[4;1H %d02 / %d02 / %d04", Dat.Date,Dat.Month,Dat.Year); } /* USER CODE END 3 */ }
/** * @brief EXTI line detection callbacks * @param GPIO_Pin: Specifies the pins connected EXTI line * @retval None */ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { if (GPIO_Pin == USER_BUTTON_PIN) { /* Wait that user release the User push-button */ while(BSP_PB_GetState(BUTTON_USER) == GPIO_PIN_SET){} HAL_RTC_GetTime(&RTCHandle, &RTC_TimeStructure, RTC_FORMAT_BIN); HAL_RTC_GetDate(&RTCHandle, &RTC_DateStructure, RTC_FORMAT_BIN); /* Set the alarm to current time + 5s */ RTC_AlarmStructure.Alarm = RTC_ALARM_A; RTC_AlarmStructure.AlarmTime.TimeFormat = RTC_TimeStructure.TimeFormat; RTC_AlarmStructure.AlarmTime.Hours = RTC_TimeStructure.Hours; RTC_AlarmStructure.AlarmTime.Minutes = RTC_TimeStructure.Minutes; RTC_AlarmStructure.AlarmTime.Seconds = (RTC_TimeStructure.Seconds + 5) % 60; RTC_AlarmStructure.AlarmDateWeekDay = 31; RTC_AlarmStructure.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE; RTC_AlarmStructure.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY | RTC_ALARMMASK_HOURS | RTC_ALARMMASK_MINUTES; RTC_AlarmStructure.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_NONE; if (HAL_RTC_SetAlarm_IT(&RTCHandle, &RTC_AlarmStructure, RTC_FORMAT_BIN) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /* The Following Wakeup sequence is highly recommended prior to each Standby mode entry mainly when using more than one wakeup source this is to not miss any wakeup event. - Disable all used wakeup sources, - Clear all related wakeup flags, - Re-enable all used wakeup sources, - Enter the Standby mode. */ /*#### Disable all used wakeup sources: WKUP pin ###########################*/ HAL_PWR_DisableWakeUpPin(PWR_WAKEUP_PIN1); /*#### Clear all related wakeup flags ######################################*/ /* Clear PWR wake up Flag */ __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU); /* Enable WKUP pin */ HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN1); /* Turn LED3 off */ BSP_LED_Off(LED3); /* Request to enter STANDBY mode */ HAL_PWR_EnterSTANDBYMode(); } }
MP_WEAK DWORD get_fattime(void) { #if MICROPY_HW_ENABLE_RTC rtc_init_finalise(); RTC_TimeTypeDef time; RTC_DateTypeDef date; HAL_RTC_GetTime(&RTCHandle, &time, RTC_FORMAT_BIN); HAL_RTC_GetDate(&RTCHandle, &date, RTC_FORMAT_BIN); return ((2000 + date.Year - 1980) << 25) | ((date.Month) << 21) | ((date.Date) << 16) | ((time.Hours) << 11) | ((time.Minutes) << 5) | (time.Seconds / 2); #else // Jan 1st, 2018 at midnight. Not sure what timezone. return ((2018 - 1980) << 25) | ((1) << 21) | ((1) << 16) | ((0) << 11) | ((0) << 5) | (0 / 2); #endif }
static RtcCalendar_t RtcGetCalendar( void ) { uint32_t first_read = 0; uint32_t second_read = 0; RtcCalendar_t now; // Get Time and Date HAL_RTC_GetTime( &RtcHandle, &now.CalendarTime, RTC_FORMAT_BIN ); first_read = now.CalendarTime.SubSeconds; HAL_RTC_GetTime( &RtcHandle, &now.CalendarTime, RTC_FORMAT_BIN ); second_read = now.CalendarTime.SubSeconds; // make sure it is correct due to asynchronous nature of RTC while( first_read != second_read ) { first_read = second_read; HAL_RTC_GetTime( &RtcHandle, &now.CalendarTime, RTC_FORMAT_BIN ); second_read = now.CalendarTime.SubSeconds; } HAL_RTC_GetDate( &RtcHandle, &now.CalendarDate, RTC_FORMAT_BIN ); return( now ); }
TM_RTC_Result_t TM_RTC_GetDateTime(TM_RTC_t* data, TM_RTC_Format_t format) { uint32_t unix; /* Get time */ if (format == TM_RTC_Format_BIN) { HAL_RTC_GetTime(&hRTC, &RTC_TimeStruct, RTC_FORMAT_BIN); } else { HAL_RTC_GetTime(&hRTC, &RTC_TimeStruct, RTC_FORMAT_BCD); } /* Format hours */ data->Hours = RTC_TimeStruct.Hours; data->Minutes = RTC_TimeStruct.Minutes; data->Seconds = RTC_TimeStruct.Seconds; /* Get subseconds */ data->Subseconds = RTC->SSR; /* Get date */ if (format == TM_RTC_Format_BIN) { HAL_RTC_GetDate(&hRTC, &RTC_DateStruct, RTC_FORMAT_BIN); } else { HAL_RTC_GetDate(&hRTC, &RTC_DateStruct, RTC_FORMAT_BCD); } /* Format date */ data->Year = RTC_DateStruct.Year; data->Month = RTC_DateStruct.Month; data->Day = RTC_DateStruct.Date; data->WeekDay = RTC_DateStruct.WeekDay; /* Calculate unix offset */ unix = TM_RTC_GetUnixTimeStamp(data); data->Unix = unix; /* Return OK */ return TM_RTC_Result_Ok; }
/** * @brief Display the current time and date. * @param showtime : pointer to buffer * @param showdate : pointer to buffer * @retval None */ static void RTC_CalendarShow(uint8_t *showtime, uint8_t *showdate) { RTC_DateTypeDef sdatestructureget; RTC_TimeTypeDef stimestructureget; /* Get the RTC current Time */ HAL_RTC_GetTime(&RtcHandle, &stimestructureget, RTC_FORMAT_BIN); /* Get the RTC current Date */ HAL_RTC_GetDate(&RtcHandle, &sdatestructureget, RTC_FORMAT_BIN); /* Display time Format : hh:mm:ss */ sprintf((char *)showtime, "%2d:%2d:%2d", stimestructureget.Hours, stimestructureget.Minutes, stimestructureget.Seconds); /* Display date Format : mm-dd-yy */ sprintf((char *)showdate, "%2d-%2d-%2d", sdatestructureget.Month, sdatestructureget.Date, 2000 + sdatestructureget.Year); }
/** * @brief This function handles RTC alarms A and B interrupt through EXTI line 17. */ void RTC_Alarm_IRQHandler(void) { /* USER CODE BEGIN RTC_Alarm_IRQn 0 */ /* Get time and date from RTC registers */ HAL_RTC_GetTime(&hrtc, &time_now, RTC_FORMAT_BIN); HAL_RTC_GetDate(&hrtc, &date_now, RTC_FORMAT_BIN); /* USER CODE END RTC_Alarm_IRQn 0 */ HAL_RTC_AlarmIRQHandler(&hrtc); /* USER CODE BEGIN RTC_Alarm_IRQn 1 */ /* USER CODE END RTC_Alarm_IRQn 1 */ }
/* USER CODE BEGIN 0 */ void HAL_RTCEx_RTCEventCallback(RTC_HandleTypeDef *hrtc){ RTC_TimeTypeDef sTime; RTC_DateTypeDef sDate; static int day = 0; HAL_RTC_GetDate(hrtc, &sDate, FORMAT_BIN); HAL_RTC_GetTime(hrtc, &sTime, FORMAT_BIN); if(day != sDate.Date){ day = sDate.Date; 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); } };
static uint8_t rtc_check() { uint8_t hour,min; RTC_AlarmTypeDef sAlarm; RTC_DateTypeDef sdatestructureget; RTC_TimeTypeDef stimestructureget; if (HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR1) != 0x32F2) { HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR1, 0x32F2); return 0; } else { //read time HAL_RTC_GetTime(&hrtc, &stimestructureget, RTC_FORMAT_BIN); HAL_RTC_GetDate(&hrtc, &sdatestructureget, RTC_FORMAT_BIN); //set alarm /**Enable the Alarm A */ hour=stimestructureget.Hours; min=stimestructureget.Minutes+1; if(min>59) { min-=60; if(hour==23) hour=0; else hour++; } sAlarm.AlarmTime.Hours = hour; sAlarm.AlarmTime.Minutes = min; 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); return 1; } }
int main(void) { /* USER CODE BEGIN 1 */ trace_printf("Hello\n"); RTC_TimeTypeDef Tim; RTC_DateTypeDef Dat; /* USER CODE END 1 */ /* MCU Configuration----------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* Configure the system clock */ SystemClock_Config(); /* Initialize all configured peripherals */ MX_GPIO_Init(); /* USER CODE BEGIN 2 */ /* Initialize RTC */ BSP_RTC_Init(); /* Initialize UART with 115200 baud rate */ BSP_UART_Init(115200); uprintf("Set Time: "); BSP_RTC_Change_Time(); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ HAL_RTC_GetTime(&hrtc_bsp,&Tim,FORMAT_BIN); HAL_RTC_GetDate(&hrtc_bsp,&Dat,FORMAT_BIN); Tim.SubSeconds = 1000 - ((hrtc_bsp.Instance->SSR*1000)/hrtc_bsp.Init.SynchPrediv); // Count msec uprintf("\x1b[3;1H %d02 : %d02 : %d02 : %d04",Tim.Hours,Tim.Minutes,Tim.Seconds,Tim.SubSeconds); } /* USER CODE END 3 */ }
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; } }
/** * @brief EXTI line detection callbacks * @param GPIO_Pin: Specifies the pins connected EXTI line * @retval None */ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { if (GPIO_Pin == TAMPER_BUTTON_PIN) { HAL_RTC_GetTime(&RTCHandle, &RTC_TimeStructure, RTC_FORMAT_BIN); HAL_RTC_GetDate(&RTCHandle, &RTC_DateStructure, RTC_FORMAT_BIN); /* Set the alarm to current time + 5s */ RTC_AlarmStructure.Alarm = RTC_ALARM_A; RTC_AlarmStructure.AlarmTime.TimeFormat = RTC_TimeStructure.TimeFormat; RTC_AlarmStructure.AlarmTime.Hours = RTC_TimeStructure.Hours; RTC_AlarmStructure.AlarmTime.Minutes = RTC_TimeStructure.Minutes; RTC_AlarmStructure.AlarmTime.Seconds = (RTC_TimeStructure.Seconds + 5) % 60; RTC_AlarmStructure.AlarmDateWeekDay = 31; RTC_AlarmStructure.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE; RTC_AlarmStructure.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY | RTC_ALARMMASK_HOURS | RTC_ALARMMASK_MINUTES; RTC_AlarmStructure.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_NONE; if (HAL_RTC_SetAlarm_IT(&RTCHandle, &RTC_AlarmStructure, RTC_FORMAT_BIN) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /* Wait that user release the Tamper push-button */ while(BSP_PB_GetState(BUTTON_TAMPER) == GPIO_PIN_SET){} /* Disable all used wakeup sources: WKUP pin */ HAL_PWR_DisableWakeUpPin(PWR_WAKEUP_PIN4); /* Clear all related wakeup flags */ /* Clear PWR wake up Flag */ __HAL_PWR_CLEAR_WAKEUP_FLAG(PWR_WAKEUP_PIN_FLAG4); /* Enable WKUP pin */ HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN4); /* Turn on LED1 */ BSP_LED_Off(LED1); /* Request to enter STANDBY mode */ HAL_PWR_EnterSTANDBYMode(); } }
double current_time() { RTC_TimeTypeDef time; RTC_DateTypeDef date; uint32_t subsec; /* must get time and date here due to STM32 HW bug */ HAL_RTC_GetTime(&hrtc, &time, FORMAT_BIN); HAL_RTC_GetDate(&hrtc, &date, FORMAT_BIN); subsec = (255 - time.SubSeconds) * 1000 / 255; (void)date; /* return seconds.milliseconds */ return ((double)time.Hours * 24) + ((double)time.Minutes * 60) + (double)time.Seconds + ((double)subsec/1000); }
void vOutputTime (void){ extern RTC_HandleTypeDef hrtc; RTC_TimeTypeDef sTime; RTC_DateTypeDef sDate; // if( xSemaphoreTake( xDEBUGbusMutex, cmdMAX_MUTEX_WAIT ) == pdPASS ) // ждем семафор cmdMAX_MUTEX_WAIT иначе уходим // { HAL_RTC_GetTime(&hrtc, &sTime, FORMAT_BIN); // Читаем время HAL_RTC_GetDate(&hrtc, &sDate, FORMAT_BIN); // читаем дату char* WriteBuffer = pvPortMalloc(30); sprintf( WriteBuffer, "[%d.%d.%d.%u] ",sTime.Hours,sTime.Minutes,sTime.Seconds,(uint16_t)(sTime.SubSeconds/2) ); USART_0TRACE(WriteBuffer); vPortFree(WriteBuffer); // xSemaphoreGive( xDEBUGbusMutex ); // вернём семафор // } }
time_t mo_Get_UnixTime_hal(void) { RTC_DateTypeDef sdatestructureget; RTC_TimeTypeDef stimestructureget; struct tm tmstruct; memset(&tmstruct, 0, sizeof(tm)); /* Get the RTC current Time */ HAL_RTC_GetTime(&RtcHandle, &stimestructureget, RTC_FORMAT_BIN); /* Get the RTC current Date */ HAL_RTC_GetDate(&RtcHandle, &sdatestructureget, RTC_FORMAT_BIN); tmstruct.tm_year = 2000 + sdatestructureget.Year - 1900; tmstruct.tm_mon = sdatestructureget.Month - 1; tmstruct.tm_mday = sdatestructureget.Date; tmstruct.tm_hour = stimestructureget.Hours; tmstruct.tm_min = stimestructureget.Minutes; tmstruct.tm_sec = stimestructureget.Seconds; time_t t = mktime( &tmstruct ); return t; }
int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration----------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* Configure the system clock */ SystemClock_Config(); /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_RTC_Init(); //if(!(*(volatile uint32_t *) (BDCR_RTCEN_BB)))__HAL_RCC_RTC_ENABLE(); MX_TIM2_Init(); MX_TIM3_Init(); MX_USART1_UART_Init(); MX_USART2_UART_Init(); /* USER CODE BEGIN 2 */ /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ HAL_RTC_GetTime(&hrtc, &myTime, FORMAT_BIN); HAL_RTC_GetDate(&hrtc, &myDate, FORMAT_BIN); } /* USER CODE END 3 */ }