diff --git a/ds3231/ds3231.cppproj b/ds3231/ds3231.cppproj
index 25a4d45..15ee949 100644
--- a/ds3231/ds3231.cppproj
+++ b/ds3231/ds3231.cppproj
@@ -85,117 +85,117 @@
- -mmcu=atmega328p -B "%24(PackRepoDir)\Atmel\ATmega_DFP\1.2.193\gcc\dev\atmega328p"
- True
- True
- True
- True
- False
- True
- True
-
-
- NDEBUG
-
-
-
-
- %24(PackRepoDir)\Atmel\ATmega_DFP\1.2.193\include
-
-
- Optimize for size (-Os)
- True
- True
- True
- True
- True
-
-
- NDEBUG
-
-
-
-
- %24(PackRepoDir)\Atmel\ATmega_DFP\1.2.193\include
-
-
- Optimize for size (-Os)
- True
- True
- True
- True
- -Wextra -std=c++14
-
-
- libm
-
-
-
-
- %24(PackRepoDir)\Atmel\ATmega_DFP\1.2.193\include
-
-
-
+ -mmcu=atmega328p -B "%24(PackRepoDir)\Atmel\ATmega_DFP\1.2.193\gcc\dev\atmega328p"
+ True
+ True
+ True
+ True
+ False
+ True
+ True
+
+
+ NDEBUG
+
+
+
+
+ %24(PackRepoDir)\Atmel\ATmega_DFP\1.2.193\include
+
+
+ Optimize for size (-Os)
+ True
+ True
+ True
+ True
+ True
+
+
+ NDEBUG
+
+
+
+
+ %24(PackRepoDir)\Atmel\ATmega_DFP\1.2.193\include
+
+
+ Optimize for size (-Os)
+ True
+ True
+ True
+ True
+ -Wextra -std=c++14
+
+
+ libm
+
+
+
+
+ %24(PackRepoDir)\Atmel\ATmega_DFP\1.2.193\include
+
+
+
echo "C:\bin\avrdude-6.3\avrdude.exe" -v -p$(avrdevice) %%* -Uflash:w:"$(OutputDirectory)\$(Name).hex":i > "$(MSBuildProjectDirectory)\avrdude.bat"
- -mmcu=atmega328p -B "%24(PackRepoDir)\Atmel\ATmega_DFP\1.2.193\gcc\dev\atmega328p"
- True
- True
- True
- True
- False
- True
- True
-
-
- DEBUG
-
-
-
-
- %24(PackRepoDir)\Atmel\ATmega_DFP\1.2.193\include
-
-
- Optimize (-O1)
- True
- True
- Default (-g2)
- True
- True
- True
-
-
- DEBUG
-
-
-
-
- %24(PackRepoDir)\Atmel\ATmega_DFP\1.2.193\include
-
-
- Optimize (-O1)
- True
- True
- Default (-g2)
- True
- True
- -Wextra -std=c++14
-
-
- libm
-
-
-
-
- %24(PackRepoDir)\Atmel\ATmega_DFP\1.2.193\include
-
-
- Default (-Wa,-g)
-
+ -mmcu=atmega328p -B "%24(PackRepoDir)\Atmel\ATmega_DFP\1.2.193\gcc\dev\atmega328p"
+ True
+ True
+ True
+ True
+ False
+ True
+ True
+
+
+ DEBUG
+
+
+
+
+ %24(PackRepoDir)\Atmel\ATmega_DFP\1.2.193\include
+
+
+ Optimize (-O1)
+ True
+ True
+ Default (-g2)
+ True
+ True
+ True
+
+
+ DEBUG
+
+
+
+
+ %24(PackRepoDir)\Atmel\ATmega_DFP\1.2.193\include
+
+
+ Optimize (-O1)
+ True
+ True
+ Default (-g2)
+ True
+ True
+ -Wextra -std=c++14
+
+
+ libm
+
+
+
+
+ %24(PackRepoDir)\Atmel\ATmega_DFP\1.2.193\include
+
+
+ Default (-Wa,-g)
+
echo "C:\bin\avrdude-6.3\avrdude.exe" -v -p$(avrdevice) %%* -Uflash:w:"$(OutputDirectory)\$(Name).hex":i > "$(MSBuildProjectDirectory)\avrdude.bat"
@@ -215,6 +215,12 @@
compile
+
+ compile
+
+
+ compile
+
compile
diff --git a/ds3231/main.cpp b/ds3231/main.cpp
index 0a65b3c..cd24039 100644
--- a/ds3231/main.cpp
+++ b/ds3231/main.cpp
@@ -1,7 +1,7 @@
/*
* Copyright (c) by BlackMark 2017
-* Date 16/12/2017
-* Version 1.1
+* Date 17/12/2017
+* Version 1.2
*/
#include
@@ -12,63 +12,13 @@
#include "usart/usart.h"
#include "clock.h"
#include "bootloader.h"
-#include "twi.h"
-#include "rtc.h"
-
-// DST magic by Edgar Bonet
-int eu_dst( const time_t *timer, int32_t *z )
-{
- static_cast( z );
-
- uint32_t t = *timer;
-
- if( static_cast( t >> 24 ) >= 194 )
- t -= 3029443200U;
-
- t = ( t + 655513200 ) / 604800 * 28;
-
- if( static_cast( t % 1461 ) < 856 )
- return ONE_HOUR;
- else
- return 0;
-}
-
-tm 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 timeNow = mk_gmtime( &sTime );
- sTime.tm_isdst = eu_dst( &timeNow, nullptr );
-
- mktime( &sTime );
-
- return sTime;
-}
-
-void setSystemTime()
-{
- tm sTime = getTime();
- set_system_time( mktime( &sTime ) );
-}
+#include "systime.h"
void setup()
{
USART0 &cSerial = USART0::inst();
- twi_init_master();
- rtc_init();
-
- if( rtc_is_ds3231() )
+ if( SysTime::init() )
{
cSerial << "DS3231 detected!" << "\r\n";
}
@@ -76,18 +26,13 @@ void setup()
{
cSerial << "ERROR - No RTC detected!" << "\r\n";
}
-
- set_zone( 1 * ONE_HOUR );
- set_dst( eu_dst );
-
- setSystemTime();
}
void setTime()
{
USART0 &cSerial = USART0::inst();
- rtc_tm* tmTime = rtc_get_time();
+ tm sTime;
constexpr auto BUFFER_SIZE = 32;
char szBuffer[BUFFER_SIZE];
@@ -95,36 +40,36 @@ void setTime()
cSerial << "Set time:" << "\r\n" << "Year: ";
cSerial.receiveLine( szBuffer, BUFFER_SIZE, "\r" );
cSerial << szBuffer << "\r\n";
- tmTime->year = atoi( szBuffer );
+ sTime.tm_year = atoi( szBuffer ) - 1900;
cSerial << "Set time:" << "\r\n" << "Month: ";
cSerial.receiveLine( szBuffer, BUFFER_SIZE, "\r" );
cSerial << szBuffer << "\r\n";
- tmTime->mon = atoi( szBuffer );
+ sTime.tm_mon = atoi( szBuffer ) - 1;
cSerial << "Set time:" << "\r\n" << "Day: ";
cSerial.receiveLine( szBuffer, BUFFER_SIZE, "\r" );
cSerial << szBuffer << "\r\n";
- tmTime->mday = atoi( szBuffer );
+ sTime.tm_mday = atoi( szBuffer );
cSerial << "Set time:" << "\r\n" << "Hour: ";
cSerial.receiveLine( szBuffer, BUFFER_SIZE, "\r" );
cSerial << szBuffer << "\r\n";
- tmTime->hour = atoi( szBuffer );
+ sTime.tm_hour = atoi( szBuffer );
cSerial << "Minute: ";
cSerial.receiveLine( szBuffer, BUFFER_SIZE, "\r" );
cSerial << szBuffer << "\r\n";
- tmTime->min = atoi( szBuffer );
+ sTime.tm_min = atoi( szBuffer );
cSerial << "Second: ";
cSerial.receiveLine( szBuffer, BUFFER_SIZE, "\r" );
cSerial << szBuffer << "\r\n";
- tmTime->sec = atoi( szBuffer );
+ sTime.tm_sec = atoi( szBuffer );
- rtc_set_time( tmTime );
+ mktime( &sTime );
- setSystemTime();
+ SysTime::setTime( sTime );
}
void setAlarm()
@@ -154,16 +99,6 @@ void setAlarm()
rtc_set_alarm( tmAlarm );
}
-bool timeDiff( const tm &sTime1, const tm &sTime2 )
-{
- if( sTime1.tm_sec != sTime2.tm_sec || sTime1.tm_min != sTime2.tm_min || sTime1.tm_hour != sTime2.tm_hour )
- return true;
- if( sTime1.tm_mday != sTime2.tm_mday || sTime1.tm_mon != sTime2.tm_mon || sTime1.tm_year != sTime2.tm_year )
- return true;
-
- return false;
-}
-
int main()
{
uint8_t ui8MCUSR = handleReset();
@@ -180,18 +115,14 @@ int main()
setup();
- tm sTime;
-
constexpr auto BUFFER_SIZE = 32;
char szBuffer[BUFFER_SIZE];
while( true )
{
- tm sTimeNow = getTime();
-
- if( timeDiff( sTime, sTimeNow ) )
+ if( !SysTime::checkSync() )
{
- system_tick();
+ SysTime::syncSysTime();
time_t timeNow = time( nullptr );
tm *psLocalTime = localtime( &timeNow );
@@ -206,8 +137,6 @@ int main()
}
}
- sTime = sTimeNow;
-
uint8_t ui8Cmd;
if( cSerial.receiveByte( ui8Cmd ) )
@@ -242,8 +171,6 @@ int main()
cSerial.transmitByte( ui8Cmd );
cSerial << "'" << "\r\n";
}
-
- setSystemTime();
}
}
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..3e2803d
--- /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 "twi.h"
+#include "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