/** * @file node_rtc.h * @author * @brief Header file for the RTC driver * @version 1.0 * @date 2025-10-21 * @ref Alienteck RTC Driver * @copyright Copyright (c) 2024 * */#pragma once#ifdef __cplusplusextern"C"{#endif#include<sys/time.h>#include"freertos/FreeRTOS.h"#include"freertos/task.h"/* Time structure, including year, month, day, week, hour, minute, and second */typedefstruct{uint8_thour;/* Hour */uint8_tmin;/* Minute */uint8_tsec;/* Second *//* Gregorian year, month, day, and week */uint16_tyear;/* Year */uint8_tmonth;/* Month */uint8_tdate;/* Day */uint8_tweek;/* Weekday */}_calendar_obj;extern_calendar_objcalendar;/* Time structure *//* Function declarations *//** * @brief Set the RTC time * @param year : Year * @param mon : Month * @param mday : Day * @param hour : Hour * @param min : Minute * @param sec : Second * @retval None */voidrtc_set_time(intyear,intmon,intmday,inthour,intmin,intsec);/* Set time *//** * @brief Get the current time * @param None * @retval None */voidrtc_get_time(void);/* Get time *//** * @brief Convert year, month, and day to the day of the week * @note Calculates the weekday based on the Gregorian calendar. * Utilizes the Kim Larson formula for calculation. * For more details, refer to: * https://www.cnblogs.com/fengbohello/p/3264300.html * @param year : Year * @param month: Month * @param day : Day * @retval 0: Sunday; 1 ~ 6: Monday ~ Saturday */uint8_trtc_get_week(uint16_tyear,uint8_tmonth,uint8_tday);/* Get the weekday */#ifdef __cplusplus}#endif
/** * @file node_rtc.c * @author * @brief This file contains the implementation of the RTC driver * @version 1.0 * @date 2025-10-21 * @ref Alienteck RTC Driver * */#include"node_rtc.h"#ifdef __cplusplusextern"C"{#endif_calendar_objcalendar;/* Time structure *//** * @brief Set the RTC time * @param year : Year * @param mon : Month * @param mday : Day * @param hour : Hour * @param min : Minute * @param sec : Second * @retval None */voidrtc_set_time(intyear,intmon,intmday,inthour,intmin,intsec){structtmdatetime;/* Set time */datetime.tm_year=year-1900;datetime.tm_mon=mon-1;datetime.tm_mday=mday;datetime.tm_hour=hour;datetime.tm_min=min;datetime.tm_sec=sec;datetime.tm_isdst=-1;/* Get total seconds since 1970-01-01 */time_tsecond=mktime(&datetime);structtimevalval={.tv_sec=second,.tv_usec=0};/* Set current time */settimeofday(&val,NULL);}/** * @brief Get the current time * @param None * @retval None */voidrtc_get_time(void){structtm*datetime;time_tsecond;/* Get the time elapsed since (1970-01-01 00:00:00 UTC) in seconds */time(&second);datetime=localtime(&second);calendar.hour=datetime->tm_hour;/* Hour */calendar.min=datetime->tm_min;/* Minute */calendar.sec=datetime->tm_sec;/* Second *//* Gregorian year, month, day, and week */calendar.year=datetime->tm_year+1900;/* Year */calendar.month=datetime->tm_mon+1;/* Month */calendar.date=datetime->tm_mday;/* Day *//* Weekday */calendar.week=rtc_get_week(calendar.year,calendar.month,calendar.date);}/** * @brief Convert year, month, and day to the day of the week * @note Calculates the weekday based on the Gregorian calendar. * Utilizes the Kim Larson formula for calculation. * For more details, refer to: * https://www.cnblogs.com/fengbohello/p/3264300.html * @param year : Year * @param month: Month * @param day : Day * @retval 0: Sunday; 1 ~ 6: Monday ~ Saturday */uint8_trtc_get_week(uint16_tyear,uint8_tmonth,uint8_tday){uint8_tweek=0;if(month<3){month+=12;--year;}week=(day+1+2*month+3*(month+1)/5+year+(year>>2)-year/100+year/400)%7;returnweek;}#ifdef __cplusplus}#endif