diff --git a/ds3231/ds3231 b/ds3231/ds3231 index 9303fbf..04af54e 160000 --- a/ds3231/ds3231 +++ b/ds3231/ds3231 @@ -1 +1 @@ -Subproject commit 9303fbf5b59bd934ec0c91d5540b851366da92e1 +Subproject commit 04af54e7c8029f05a999f85afa0bcc194f654fed diff --git a/ds3231/ds3231.cppproj b/ds3231/ds3231.cppproj index 949d764..0b055a6 100644 --- a/ds3231/ds3231.cppproj +++ b/ds3231/ds3231.cppproj @@ -83,17 +83,6 @@ - - \Debug\ds3231.lss - - - .lss - ^\s*(?<address>[a-f0-9]*):\s*.*$ - true - address - $pc - - @@ -224,12 +213,6 @@ compile - - compile - - - compile - compile @@ -251,6 +234,12 @@ compile + + compile + + + compile + compile diff --git a/ds3231/main.cpp b/ds3231/main.cpp index 2d335ee..b2e7da5 100644 --- a/ds3231/main.cpp +++ b/ds3231/main.cpp @@ -6,7 +6,7 @@ #include #include -#include "ds3231/systime.h" +#include "systime.h" #include "uart/uart.hpp" using uart_t = uart::Uart0<>; diff --git a/ds3231/systime.cpp b/ds3231/systime.cpp new file mode 100644 index 0000000..315f255 --- /dev/null +++ b/ds3231/systime.cpp @@ -0,0 +1,107 @@ +#include "systime.h" + +////////////////////////////////////////////////////////////////////////// +// DST magic by Edgar Bonet +int SysTime::euDST( const time_t *pTime, int32_t *pZ ) +{ + static_cast( pZ ); + + uint32_t t = *pTime; + + if( static_cast( t >> 24 ) >= 194 ) + t -= 3029443200U; + + t = ( t + 655513200 ) / 604800 * 28; + + if( static_cast( t % 1461 ) < 856 ) + return ONE_HOUR; + + return 0; +} + +////////////////////////////////////////////////////////////////////////// +bool SysTime::init() +{ + twi_init_master(); + rtc_init(); + + if( !rtc_is_ds3231() ) + return false; + + set_zone( 1 * ONE_HOUR ); + set_dst( euDST ); + + syncSysTime(); + + return true; +} + +////////////////////////////////////////////////////////////////////////// +void SysTime::syncSysTime() +{ + tm sTime = getTime(); + set_system_time( mk_gmtime( &sTime ) ); +} + +////////////////////////////////////////////////////////////////////////// +bool SysTime::checkSync() +{ + time_t timeNow = time( nullptr ); + tm *ptmUTC = gmtime( &timeNow ); + + rtc_tm *ptmRtcTime = rtc_get_time(); + + if( ptmUTC->tm_sec != ptmRtcTime->sec || ptmUTC->tm_min != ptmRtcTime->min || ptmUTC->tm_hour != ptmRtcTime->hour ) + return false; + + if( ptmUTC->tm_mday != ptmRtcTime->mday || ( ptmUTC->tm_mon + 1 ) != ptmRtcTime->mon || ( ptmUTC->tm_year + 1900 ) != ptmRtcTime->year ) + return false; + + return true; +} + +////////////////////////////////////////////////////////////////////////// +void SysTime::tick() +{ + system_tick(); +} + +////////////////////////////////////////////////////////////////////////// +tm SysTime::getTime() +{ + tm sTime; + rtc_tm *ptmTime = rtc_get_time(); + + sTime.tm_sec = ptmTime->sec; + sTime.tm_min = ptmTime->min; + sTime.tm_hour = ptmTime->hour; + sTime.tm_mday = ptmTime->mday; + sTime.tm_mon = ptmTime->mon - 1; + sTime.tm_year = ptmTime->year - 1900; + sTime.tm_isdst = 0; + + time_t timeUTC = mk_gmtime( &sTime ); + sTime = *( gmtime( &timeUTC ) ); + + return sTime; +} + +////////////////////////////////////////////////////////////////////////// +void SysTime::setTime( const tm &sTime ) +{ + rtc_tm *pRtcTime = rtc_get_time(); + + pRtcTime->sec = sTime.tm_sec; + pRtcTime->min = sTime.tm_min; + pRtcTime->hour = sTime.tm_hour; + pRtcTime->mday = sTime.tm_mday; + pRtcTime->mon = sTime.tm_mon + 1; + pRtcTime->year = sTime.tm_year + 1900; + pRtcTime->wday = sTime.tm_wday + 1; + pRtcTime->am = ( sTime.tm_hour < 12 ) ? true : false; + pRtcTime->twelveHour = ( sTime.tm_hour == 0 ) ? 12 : ( ( sTime.tm_hour > 12 ) ? ( sTime.tm_hour - 12 ) : sTime.tm_hour ); + + rtc_set_time( pRtcTime ); + + syncSysTime(); +} \ No newline at end of file diff --git a/ds3231/systime.h b/ds3231/systime.h new file mode 100644 index 0000000..af8e0bc --- /dev/null +++ b/ds3231/systime.h @@ -0,0 +1,29 @@ +/* +* Copyright (c) by BlackMark 2017 +* Date 17/12/2016 +* Version 1.0 +*/ + +#ifndef SYSTIME_H +#define SYSTIME_H + +#include + +#include "ds3231/twi.h" +#include "ds3231/rtc.h" + +class SysTime +{ +private: + static int euDST( const time_t *pTime, int32_t *pZ ); + +public: + static bool init(); + static void syncSysTime(); + static bool checkSync(); + static void tick(); + static tm getTime(); + static void setTime( const tm &sTime ); +}; + +#endif \ No newline at end of file