Index: RTC.c =================================================================== diff -u -r37aa2cb57a556ed608f2512850300340ce2b5369 -rf5f58ba4dd4247da495c8f6656b0cc61390df596 --- RTC.c (.../RTC.c) (revision 37aa2cb57a556ed608f2512850300340ce2b5369) +++ RTC.c (.../RTC.c) (revision f5f58ba4dd4247da495c8f6656b0cc61390df596) @@ -14,7 +14,7 @@ * @date (original) 11-Jan-2020 * ***************************************************************************/ -#include // For calculating epoch +#include // For epoch conversion functions mktime and gmtime #include "Timers.h" #include "RTC.h" @@ -207,6 +207,7 @@ static U08 convertBCD2Decimal( U08 bcd ); static U08 convertDecimal2BCD( U08 decimal ); static U32 convertTime2Epoch( void ); +static void convertEpoch2Time( U32 epoch ); static void updateReadTimestampStruct( void ); static BOOL setMibSPIBufferLength( U16 length ); static void prepBufferForReadCommand( U08 length ); // Puts the read command @@ -240,7 +241,7 @@ RTCServiceState = RTC_SEND_COMMAND; } - /*********************************************************************//** +/*********************************************************************//** * @brief * The setRTCTimestamp gets the timestamp values from caller, converts them * into BCD format and inserts them into the txBuffer to be written into the RTC. @@ -299,6 +300,29 @@ } /*********************************************************************//** +* @brief +* The setRTCEpoch sets the clock from a given epoch. +* @details Inputs: none +* @details Outputs: RTCNewTimestampStruct +* @param epoch date/time stamp epoch +* @return TRUE if clock set, FALSE if not. +*************************************************************************/ +BOOL setRTCEpoch( U32 epoch ) +{ + BOOL result; + + convertEpoch2Time( epoch ); + result = setRTCTimestamp( RTCNewTimestampStruct.seconds, + RTCNewTimestampStruct.minutes, + RTCNewTimestampStruct.hours, + RTCNewTimestampStruct.days, + RTCNewTimestampStruct.months, + RTCNewTimestampStruct.years); + + return result; +} + +/*********************************************************************//** * @brief * The execRTCSelfTest runs the RTC POST during the self-test. * @details Inputs: RTCSelfTestState @@ -819,6 +843,32 @@ /*********************************************************************//** * @brief + * The convertEpoch2Time converts a given epoch date/time to a date/time + * structure that can be used to set the clock. + * @details Inputs: none + * @details Outputs: RTCNewTimestampStruct + * @param epoch epoch time stamp (seconds since 1/1/1970) + * @return none + *************************************************************************/ +static void convertEpoch2Time( U32 epoch ) +{ + struct tm *ptm; + + // Convert epoch to C library equivalent + epoch -= YEAR_1900_TO_1970_SECONDS_DIFF; + // Convert epoch to date/time structure + ptm = gmtime( &epoch ); + // Copy date/time stamp to our structure + RTCNewTimestampStruct.years = ptm->tm_year; + RTCNewTimestampStruct.months = ptm->tm_mon; + RTCNewTimestampStruct.days = ptm->tm_mday; + RTCNewTimestampStruct.hours = ptm->tm_hour; + RTCNewTimestampStruct.minutes = ptm->tm_min; + RTCNewTimestampStruct.seconds = ptm->tm_sec; +} + +/*********************************************************************//** + * @brief * The updateReadTimestampStruct function updates the time structure * after every read. * @details Inputs: RTCTimestampStruct Index: RTC.h =================================================================== diff -u -r7ff7e715f7a15da5f4055b33e186d58cf96fe909 -rf5f58ba4dd4247da495c8f6656b0cc61390df596 --- RTC.h (.../RTC.h) (revision 7ff7e715f7a15da5f4055b33e186d58cf96fe909) +++ RTC.h (.../RTC.h) (revision f5f58ba4dd4247da495c8f6656b0cc61390df596) @@ -54,6 +54,7 @@ void execRTC( void ); BOOL setRTCTimestamp( U08 secs, U08 mins, U08 hours, U08 days, U08 months, U32 years ); +BOOL setRTCEpoch( U32 epoch ); U32 getRTCTimestamp( void );