Changed RTC clock to UTC

This commit is contained in:
BlackMark 2017-12-17 10:45:02 +01:00
parent 9c9164c447
commit 20e7392201
4 changed files with 262 additions and 193 deletions

View File

@ -135,7 +135,7 @@
<Value>%24(PackRepoDir)\Atmel\ATmega_DFP\1.2.193\include</Value> <Value>%24(PackRepoDir)\Atmel\ATmega_DFP\1.2.193\include</Value>
</ListValues> </ListValues>
</avrgcccpp.assembler.general.IncludePaths> </avrgcccpp.assembler.general.IncludePaths>
</AvrGccCpp> </AvrGccCpp>
</ToolchainSettings> </ToolchainSettings>
<PreBuildEvent>echo "C:\bin\avrdude-6.3\avrdude.exe" -v -p$(avrdevice) %%* -Uflash:w:"$(OutputDirectory)\$(Name).hex":i &gt; "$(MSBuildProjectDirectory)\avrdude.bat"</PreBuildEvent> <PreBuildEvent>echo "C:\bin\avrdude-6.3\avrdude.exe" -v -p$(avrdevice) %%* -Uflash:w:"$(OutputDirectory)\$(Name).hex":i &gt; "$(MSBuildProjectDirectory)\avrdude.bat"</PreBuildEvent>
</PropertyGroup> </PropertyGroup>
@ -195,7 +195,7 @@
</ListValues> </ListValues>
</avrgcccpp.assembler.general.IncludePaths> </avrgcccpp.assembler.general.IncludePaths>
<avrgcccpp.assembler.debugging.DebugLevel>Default (-Wa,-g)</avrgcccpp.assembler.debugging.DebugLevel> <avrgcccpp.assembler.debugging.DebugLevel>Default (-Wa,-g)</avrgcccpp.assembler.debugging.DebugLevel>
</AvrGccCpp> </AvrGccCpp>
</ToolchainSettings> </ToolchainSettings>
<PreBuildEvent>echo "C:\bin\avrdude-6.3\avrdude.exe" -v -p$(avrdevice) %%* -Uflash:w:"$(OutputDirectory)\$(Name).hex":i &gt; "$(MSBuildProjectDirectory)\avrdude.bat"</PreBuildEvent> <PreBuildEvent>echo "C:\bin\avrdude-6.3\avrdude.exe" -v -p$(avrdevice) %%* -Uflash:w:"$(OutputDirectory)\$(Name).hex":i &gt; "$(MSBuildProjectDirectory)\avrdude.bat"</PreBuildEvent>
</PropertyGroup> </PropertyGroup>
@ -215,6 +215,12 @@
<Compile Include="rtc.h"> <Compile Include="rtc.h">
<SubType>compile</SubType> <SubType>compile</SubType>
</Compile> </Compile>
<Compile Include="systime.cpp">
<SubType>compile</SubType>
</Compile>
<Compile Include="systime.h">
<SubType>compile</SubType>
</Compile>
<Compile Include="twi-lowlevel.cpp"> <Compile Include="twi-lowlevel.cpp">
<SubType>compile</SubType> <SubType>compile</SubType>
</Compile> </Compile>

View File

@ -1,7 +1,7 @@
/* /*
* Copyright (c) by BlackMark 2017 * Copyright (c) by BlackMark 2017
* Date 16/12/2017 * Date 17/12/2017
* Version 1.1 * Version 1.2
*/ */
#include <stdio.h> #include <stdio.h>
@ -12,63 +12,13 @@
#include "usart/usart.h" #include "usart/usart.h"
#include "clock.h" #include "clock.h"
#include "bootloader.h" #include "bootloader.h"
#include "twi.h" #include "systime.h"
#include "rtc.h"
// DST magic by Edgar Bonet
int eu_dst( const time_t *timer, int32_t *z )
{
static_cast<void>( z );
uint32_t t = *timer;
if( static_cast<uint8_t>( t >> 24 ) >= 194 )
t -= 3029443200U;
t = ( t + 655513200 ) / 604800 * 28;
if( static_cast<uint16_t>( 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 ) );
}
void setup() void setup()
{ {
USART0 &cSerial = USART0::inst(); USART0 &cSerial = USART0::inst();
twi_init_master(); if( SysTime::init() )
rtc_init();
if( rtc_is_ds3231() )
{ {
cSerial << "DS3231 detected!" << "\r\n"; cSerial << "DS3231 detected!" << "\r\n";
} }
@ -76,18 +26,13 @@ void setup()
{ {
cSerial << "ERROR - No RTC detected!" << "\r\n"; cSerial << "ERROR - No RTC detected!" << "\r\n";
} }
set_zone( 1 * ONE_HOUR );
set_dst( eu_dst );
setSystemTime();
} }
void setTime() void setTime()
{ {
USART0 &cSerial = USART0::inst(); USART0 &cSerial = USART0::inst();
rtc_tm* tmTime = rtc_get_time(); tm sTime;
constexpr auto BUFFER_SIZE = 32; constexpr auto BUFFER_SIZE = 32;
char szBuffer[BUFFER_SIZE]; char szBuffer[BUFFER_SIZE];
@ -95,36 +40,36 @@ void setTime()
cSerial << "Set time:" << "\r\n" << "Year: "; cSerial << "Set time:" << "\r\n" << "Year: ";
cSerial.receiveLine( szBuffer, BUFFER_SIZE, "\r" ); cSerial.receiveLine( szBuffer, BUFFER_SIZE, "\r" );
cSerial << szBuffer << "\r\n"; cSerial << szBuffer << "\r\n";
tmTime->year = atoi( szBuffer ); sTime.tm_year = atoi( szBuffer ) - 1900;
cSerial << "Set time:" << "\r\n" << "Month: "; cSerial << "Set time:" << "\r\n" << "Month: ";
cSerial.receiveLine( szBuffer, BUFFER_SIZE, "\r" ); cSerial.receiveLine( szBuffer, BUFFER_SIZE, "\r" );
cSerial << szBuffer << "\r\n"; cSerial << szBuffer << "\r\n";
tmTime->mon = atoi( szBuffer ); sTime.tm_mon = atoi( szBuffer ) - 1;
cSerial << "Set time:" << "\r\n" << "Day: "; cSerial << "Set time:" << "\r\n" << "Day: ";
cSerial.receiveLine( szBuffer, BUFFER_SIZE, "\r" ); cSerial.receiveLine( szBuffer, BUFFER_SIZE, "\r" );
cSerial << szBuffer << "\r\n"; cSerial << szBuffer << "\r\n";
tmTime->mday = atoi( szBuffer ); sTime.tm_mday = atoi( szBuffer );
cSerial << "Set time:" << "\r\n" << "Hour: "; cSerial << "Set time:" << "\r\n" << "Hour: ";
cSerial.receiveLine( szBuffer, BUFFER_SIZE, "\r" ); cSerial.receiveLine( szBuffer, BUFFER_SIZE, "\r" );
cSerial << szBuffer << "\r\n"; cSerial << szBuffer << "\r\n";
tmTime->hour = atoi( szBuffer ); sTime.tm_hour = atoi( szBuffer );
cSerial << "Minute: "; cSerial << "Minute: ";
cSerial.receiveLine( szBuffer, BUFFER_SIZE, "\r" ); cSerial.receiveLine( szBuffer, BUFFER_SIZE, "\r" );
cSerial << szBuffer << "\r\n"; cSerial << szBuffer << "\r\n";
tmTime->min = atoi( szBuffer ); sTime.tm_min = atoi( szBuffer );
cSerial << "Second: "; cSerial << "Second: ";
cSerial.receiveLine( szBuffer, BUFFER_SIZE, "\r" ); cSerial.receiveLine( szBuffer, BUFFER_SIZE, "\r" );
cSerial << szBuffer << "\r\n"; 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() void setAlarm()
@ -154,16 +99,6 @@ void setAlarm()
rtc_set_alarm( tmAlarm ); 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() int main()
{ {
uint8_t ui8MCUSR = handleReset(); uint8_t ui8MCUSR = handleReset();
@ -180,18 +115,14 @@ int main()
setup(); setup();
tm sTime;
constexpr auto BUFFER_SIZE = 32; constexpr auto BUFFER_SIZE = 32;
char szBuffer[BUFFER_SIZE]; char szBuffer[BUFFER_SIZE];
while( true ) while( true )
{ {
tm sTimeNow = getTime(); if( !SysTime::checkSync() )
if( timeDiff( sTime, sTimeNow ) )
{ {
system_tick(); SysTime::syncSysTime();
time_t timeNow = time( nullptr ); time_t timeNow = time( nullptr );
tm *psLocalTime = localtime( &timeNow ); tm *psLocalTime = localtime( &timeNow );
@ -206,8 +137,6 @@ int main()
} }
} }
sTime = sTimeNow;
uint8_t ui8Cmd; uint8_t ui8Cmd;
if( cSerial.receiveByte( ui8Cmd ) ) if( cSerial.receiveByte( ui8Cmd ) )
@ -242,8 +171,6 @@ int main()
cSerial.transmitByte( ui8Cmd ); cSerial.transmitByte( ui8Cmd );
cSerial << "'" << "\r\n"; cSerial << "'" << "\r\n";
} }
setSystemTime();
} }
} }

107
ds3231/systime.cpp Normal file
View File

@ -0,0 +1,107 @@
#include "systime.h"
//////////////////////////////////////////////////////////////////////////
// DST magic by Edgar Bonet
int SysTime::euDST( const time_t *pTime, int32_t *pZ )
{
static_cast<void>( pZ );
uint32_t t = *pTime;
if( static_cast<uint8_t>( t >> 24 ) >= 194 )
t -= 3029443200U;
t = ( t + 655513200 ) / 604800 * 28;
if( static_cast<uint16_t>( 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();
}

29
ds3231/systime.h Normal file
View File

@ -0,0 +1,29 @@
/*
* Copyright (c) by BlackMark 2017
* Date 17/12/2016
* Version 1.0
*/
#ifndef SYSTIME_H
#define SYSTIME_H
#include <time.h>
#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