Changed RTC clock to UTC
This commit is contained in:
parent
9c9164c447
commit
20e7392201
@ -85,117 +85,117 @@
|
||||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
|
||||
<ToolchainSettings>
|
||||
<AvrGccCpp>
|
||||
<avrgcc.common.Device>-mmcu=atmega328p -B "%24(PackRepoDir)\Atmel\ATmega_DFP\1.2.193\gcc\dev\atmega328p"</avrgcc.common.Device>
|
||||
<avrgcc.common.outputfiles.hex>True</avrgcc.common.outputfiles.hex>
|
||||
<avrgcc.common.outputfiles.lss>True</avrgcc.common.outputfiles.lss>
|
||||
<avrgcc.common.outputfiles.eep>True</avrgcc.common.outputfiles.eep>
|
||||
<avrgcc.common.outputfiles.srec>True</avrgcc.common.outputfiles.srec>
|
||||
<avrgcc.common.outputfiles.usersignatures>False</avrgcc.common.outputfiles.usersignatures>
|
||||
<avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned>True</avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned>
|
||||
<avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned>True</avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned>
|
||||
<avrgcc.compiler.symbols.DefSymbols>
|
||||
<ListValues>
|
||||
<Value>NDEBUG</Value>
|
||||
</ListValues>
|
||||
</avrgcc.compiler.symbols.DefSymbols>
|
||||
<avrgcc.compiler.directories.IncludePaths>
|
||||
<ListValues>
|
||||
<Value>%24(PackRepoDir)\Atmel\ATmega_DFP\1.2.193\include</Value>
|
||||
</ListValues>
|
||||
</avrgcc.compiler.directories.IncludePaths>
|
||||
<avrgcc.compiler.optimization.level>Optimize for size (-Os)</avrgcc.compiler.optimization.level>
|
||||
<avrgcc.compiler.optimization.PackStructureMembers>True</avrgcc.compiler.optimization.PackStructureMembers>
|
||||
<avrgcc.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcc.compiler.optimization.AllocateBytesNeededForEnum>
|
||||
<avrgcc.compiler.warnings.AllWarnings>True</avrgcc.compiler.warnings.AllWarnings>
|
||||
<avrgcccpp.compiler.general.ChangeDefaultCharTypeUnsigned>True</avrgcccpp.compiler.general.ChangeDefaultCharTypeUnsigned>
|
||||
<avrgcccpp.compiler.general.ChangeDefaultBitFieldUnsigned>True</avrgcccpp.compiler.general.ChangeDefaultBitFieldUnsigned>
|
||||
<avrgcccpp.compiler.symbols.DefSymbols>
|
||||
<ListValues>
|
||||
<Value>NDEBUG</Value>
|
||||
</ListValues>
|
||||
</avrgcccpp.compiler.symbols.DefSymbols>
|
||||
<avrgcccpp.compiler.directories.IncludePaths>
|
||||
<ListValues>
|
||||
<Value>%24(PackRepoDir)\Atmel\ATmega_DFP\1.2.193\include</Value>
|
||||
</ListValues>
|
||||
</avrgcccpp.compiler.directories.IncludePaths>
|
||||
<avrgcccpp.compiler.optimization.level>Optimize for size (-Os)</avrgcccpp.compiler.optimization.level>
|
||||
<avrgcccpp.compiler.optimization.PackStructureMembers>True</avrgcccpp.compiler.optimization.PackStructureMembers>
|
||||
<avrgcccpp.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcccpp.compiler.optimization.AllocateBytesNeededForEnum>
|
||||
<avrgcccpp.compiler.warnings.AllWarnings>True</avrgcccpp.compiler.warnings.AllWarnings>
|
||||
<avrgcccpp.compiler.warnings.Pedantic>True</avrgcccpp.compiler.warnings.Pedantic>
|
||||
<avrgcccpp.compiler.miscellaneous.OtherFlags>-Wextra -std=c++14</avrgcccpp.compiler.miscellaneous.OtherFlags>
|
||||
<avrgcccpp.linker.libraries.Libraries>
|
||||
<ListValues>
|
||||
<Value>libm</Value>
|
||||
</ListValues>
|
||||
</avrgcccpp.linker.libraries.Libraries>
|
||||
<avrgcccpp.assembler.general.IncludePaths>
|
||||
<ListValues>
|
||||
<Value>%24(PackRepoDir)\Atmel\ATmega_DFP\1.2.193\include</Value>
|
||||
</ListValues>
|
||||
</avrgcccpp.assembler.general.IncludePaths>
|
||||
</AvrGccCpp>
|
||||
<avrgcc.common.Device>-mmcu=atmega328p -B "%24(PackRepoDir)\Atmel\ATmega_DFP\1.2.193\gcc\dev\atmega328p"</avrgcc.common.Device>
|
||||
<avrgcc.common.outputfiles.hex>True</avrgcc.common.outputfiles.hex>
|
||||
<avrgcc.common.outputfiles.lss>True</avrgcc.common.outputfiles.lss>
|
||||
<avrgcc.common.outputfiles.eep>True</avrgcc.common.outputfiles.eep>
|
||||
<avrgcc.common.outputfiles.srec>True</avrgcc.common.outputfiles.srec>
|
||||
<avrgcc.common.outputfiles.usersignatures>False</avrgcc.common.outputfiles.usersignatures>
|
||||
<avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned>True</avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned>
|
||||
<avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned>True</avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned>
|
||||
<avrgcc.compiler.symbols.DefSymbols>
|
||||
<ListValues>
|
||||
<Value>NDEBUG</Value>
|
||||
</ListValues>
|
||||
</avrgcc.compiler.symbols.DefSymbols>
|
||||
<avrgcc.compiler.directories.IncludePaths>
|
||||
<ListValues>
|
||||
<Value>%24(PackRepoDir)\Atmel\ATmega_DFP\1.2.193\include</Value>
|
||||
</ListValues>
|
||||
</avrgcc.compiler.directories.IncludePaths>
|
||||
<avrgcc.compiler.optimization.level>Optimize for size (-Os)</avrgcc.compiler.optimization.level>
|
||||
<avrgcc.compiler.optimization.PackStructureMembers>True</avrgcc.compiler.optimization.PackStructureMembers>
|
||||
<avrgcc.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcc.compiler.optimization.AllocateBytesNeededForEnum>
|
||||
<avrgcc.compiler.warnings.AllWarnings>True</avrgcc.compiler.warnings.AllWarnings>
|
||||
<avrgcccpp.compiler.general.ChangeDefaultCharTypeUnsigned>True</avrgcccpp.compiler.general.ChangeDefaultCharTypeUnsigned>
|
||||
<avrgcccpp.compiler.general.ChangeDefaultBitFieldUnsigned>True</avrgcccpp.compiler.general.ChangeDefaultBitFieldUnsigned>
|
||||
<avrgcccpp.compiler.symbols.DefSymbols>
|
||||
<ListValues>
|
||||
<Value>NDEBUG</Value>
|
||||
</ListValues>
|
||||
</avrgcccpp.compiler.symbols.DefSymbols>
|
||||
<avrgcccpp.compiler.directories.IncludePaths>
|
||||
<ListValues>
|
||||
<Value>%24(PackRepoDir)\Atmel\ATmega_DFP\1.2.193\include</Value>
|
||||
</ListValues>
|
||||
</avrgcccpp.compiler.directories.IncludePaths>
|
||||
<avrgcccpp.compiler.optimization.level>Optimize for size (-Os)</avrgcccpp.compiler.optimization.level>
|
||||
<avrgcccpp.compiler.optimization.PackStructureMembers>True</avrgcccpp.compiler.optimization.PackStructureMembers>
|
||||
<avrgcccpp.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcccpp.compiler.optimization.AllocateBytesNeededForEnum>
|
||||
<avrgcccpp.compiler.warnings.AllWarnings>True</avrgcccpp.compiler.warnings.AllWarnings>
|
||||
<avrgcccpp.compiler.warnings.Pedantic>True</avrgcccpp.compiler.warnings.Pedantic>
|
||||
<avrgcccpp.compiler.miscellaneous.OtherFlags>-Wextra -std=c++14</avrgcccpp.compiler.miscellaneous.OtherFlags>
|
||||
<avrgcccpp.linker.libraries.Libraries>
|
||||
<ListValues>
|
||||
<Value>libm</Value>
|
||||
</ListValues>
|
||||
</avrgcccpp.linker.libraries.Libraries>
|
||||
<avrgcccpp.assembler.general.IncludePaths>
|
||||
<ListValues>
|
||||
<Value>%24(PackRepoDir)\Atmel\ATmega_DFP\1.2.193\include</Value>
|
||||
</ListValues>
|
||||
</avrgcccpp.assembler.general.IncludePaths>
|
||||
</AvrGccCpp>
|
||||
</ToolchainSettings>
|
||||
<PreBuildEvent>echo "C:\bin\avrdude-6.3\avrdude.exe" -v -p$(avrdevice) %%* -Uflash:w:"$(OutputDirectory)\$(Name).hex":i > "$(MSBuildProjectDirectory)\avrdude.bat"</PreBuildEvent>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
|
||||
<ToolchainSettings>
|
||||
<AvrGccCpp>
|
||||
<avrgcc.common.Device>-mmcu=atmega328p -B "%24(PackRepoDir)\Atmel\ATmega_DFP\1.2.193\gcc\dev\atmega328p"</avrgcc.common.Device>
|
||||
<avrgcc.common.outputfiles.hex>True</avrgcc.common.outputfiles.hex>
|
||||
<avrgcc.common.outputfiles.lss>True</avrgcc.common.outputfiles.lss>
|
||||
<avrgcc.common.outputfiles.eep>True</avrgcc.common.outputfiles.eep>
|
||||
<avrgcc.common.outputfiles.srec>True</avrgcc.common.outputfiles.srec>
|
||||
<avrgcc.common.outputfiles.usersignatures>False</avrgcc.common.outputfiles.usersignatures>
|
||||
<avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned>True</avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned>
|
||||
<avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned>True</avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned>
|
||||
<avrgcc.compiler.symbols.DefSymbols>
|
||||
<ListValues>
|
||||
<Value>DEBUG</Value>
|
||||
</ListValues>
|
||||
</avrgcc.compiler.symbols.DefSymbols>
|
||||
<avrgcc.compiler.directories.IncludePaths>
|
||||
<ListValues>
|
||||
<Value>%24(PackRepoDir)\Atmel\ATmega_DFP\1.2.193\include</Value>
|
||||
</ListValues>
|
||||
</avrgcc.compiler.directories.IncludePaths>
|
||||
<avrgcc.compiler.optimization.level>Optimize (-O1)</avrgcc.compiler.optimization.level>
|
||||
<avrgcc.compiler.optimization.PackStructureMembers>True</avrgcc.compiler.optimization.PackStructureMembers>
|
||||
<avrgcc.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcc.compiler.optimization.AllocateBytesNeededForEnum>
|
||||
<avrgcc.compiler.optimization.DebugLevel>Default (-g2)</avrgcc.compiler.optimization.DebugLevel>
|
||||
<avrgcc.compiler.warnings.AllWarnings>True</avrgcc.compiler.warnings.AllWarnings>
|
||||
<avrgcccpp.compiler.general.ChangeDefaultCharTypeUnsigned>True</avrgcccpp.compiler.general.ChangeDefaultCharTypeUnsigned>
|
||||
<avrgcccpp.compiler.general.ChangeDefaultBitFieldUnsigned>True</avrgcccpp.compiler.general.ChangeDefaultBitFieldUnsigned>
|
||||
<avrgcccpp.compiler.symbols.DefSymbols>
|
||||
<ListValues>
|
||||
<Value>DEBUG</Value>
|
||||
</ListValues>
|
||||
</avrgcccpp.compiler.symbols.DefSymbols>
|
||||
<avrgcccpp.compiler.directories.IncludePaths>
|
||||
<ListValues>
|
||||
<Value>%24(PackRepoDir)\Atmel\ATmega_DFP\1.2.193\include</Value>
|
||||
</ListValues>
|
||||
</avrgcccpp.compiler.directories.IncludePaths>
|
||||
<avrgcccpp.compiler.optimization.level>Optimize (-O1)</avrgcccpp.compiler.optimization.level>
|
||||
<avrgcccpp.compiler.optimization.PackStructureMembers>True</avrgcccpp.compiler.optimization.PackStructureMembers>
|
||||
<avrgcccpp.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcccpp.compiler.optimization.AllocateBytesNeededForEnum>
|
||||
<avrgcccpp.compiler.optimization.DebugLevel>Default (-g2)</avrgcccpp.compiler.optimization.DebugLevel>
|
||||
<avrgcccpp.compiler.warnings.AllWarnings>True</avrgcccpp.compiler.warnings.AllWarnings>
|
||||
<avrgcccpp.compiler.warnings.Pedantic>True</avrgcccpp.compiler.warnings.Pedantic>
|
||||
<avrgcccpp.compiler.miscellaneous.OtherFlags>-Wextra -std=c++14</avrgcccpp.compiler.miscellaneous.OtherFlags>
|
||||
<avrgcccpp.linker.libraries.Libraries>
|
||||
<ListValues>
|
||||
<Value>libm</Value>
|
||||
</ListValues>
|
||||
</avrgcccpp.linker.libraries.Libraries>
|
||||
<avrgcccpp.assembler.general.IncludePaths>
|
||||
<ListValues>
|
||||
<Value>%24(PackRepoDir)\Atmel\ATmega_DFP\1.2.193\include</Value>
|
||||
</ListValues>
|
||||
</avrgcccpp.assembler.general.IncludePaths>
|
||||
<avrgcccpp.assembler.debugging.DebugLevel>Default (-Wa,-g)</avrgcccpp.assembler.debugging.DebugLevel>
|
||||
</AvrGccCpp>
|
||||
<avrgcc.common.Device>-mmcu=atmega328p -B "%24(PackRepoDir)\Atmel\ATmega_DFP\1.2.193\gcc\dev\atmega328p"</avrgcc.common.Device>
|
||||
<avrgcc.common.outputfiles.hex>True</avrgcc.common.outputfiles.hex>
|
||||
<avrgcc.common.outputfiles.lss>True</avrgcc.common.outputfiles.lss>
|
||||
<avrgcc.common.outputfiles.eep>True</avrgcc.common.outputfiles.eep>
|
||||
<avrgcc.common.outputfiles.srec>True</avrgcc.common.outputfiles.srec>
|
||||
<avrgcc.common.outputfiles.usersignatures>False</avrgcc.common.outputfiles.usersignatures>
|
||||
<avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned>True</avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned>
|
||||
<avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned>True</avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned>
|
||||
<avrgcc.compiler.symbols.DefSymbols>
|
||||
<ListValues>
|
||||
<Value>DEBUG</Value>
|
||||
</ListValues>
|
||||
</avrgcc.compiler.symbols.DefSymbols>
|
||||
<avrgcc.compiler.directories.IncludePaths>
|
||||
<ListValues>
|
||||
<Value>%24(PackRepoDir)\Atmel\ATmega_DFP\1.2.193\include</Value>
|
||||
</ListValues>
|
||||
</avrgcc.compiler.directories.IncludePaths>
|
||||
<avrgcc.compiler.optimization.level>Optimize (-O1)</avrgcc.compiler.optimization.level>
|
||||
<avrgcc.compiler.optimization.PackStructureMembers>True</avrgcc.compiler.optimization.PackStructureMembers>
|
||||
<avrgcc.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcc.compiler.optimization.AllocateBytesNeededForEnum>
|
||||
<avrgcc.compiler.optimization.DebugLevel>Default (-g2)</avrgcc.compiler.optimization.DebugLevel>
|
||||
<avrgcc.compiler.warnings.AllWarnings>True</avrgcc.compiler.warnings.AllWarnings>
|
||||
<avrgcccpp.compiler.general.ChangeDefaultCharTypeUnsigned>True</avrgcccpp.compiler.general.ChangeDefaultCharTypeUnsigned>
|
||||
<avrgcccpp.compiler.general.ChangeDefaultBitFieldUnsigned>True</avrgcccpp.compiler.general.ChangeDefaultBitFieldUnsigned>
|
||||
<avrgcccpp.compiler.symbols.DefSymbols>
|
||||
<ListValues>
|
||||
<Value>DEBUG</Value>
|
||||
</ListValues>
|
||||
</avrgcccpp.compiler.symbols.DefSymbols>
|
||||
<avrgcccpp.compiler.directories.IncludePaths>
|
||||
<ListValues>
|
||||
<Value>%24(PackRepoDir)\Atmel\ATmega_DFP\1.2.193\include</Value>
|
||||
</ListValues>
|
||||
</avrgcccpp.compiler.directories.IncludePaths>
|
||||
<avrgcccpp.compiler.optimization.level>Optimize (-O1)</avrgcccpp.compiler.optimization.level>
|
||||
<avrgcccpp.compiler.optimization.PackStructureMembers>True</avrgcccpp.compiler.optimization.PackStructureMembers>
|
||||
<avrgcccpp.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcccpp.compiler.optimization.AllocateBytesNeededForEnum>
|
||||
<avrgcccpp.compiler.optimization.DebugLevel>Default (-g2)</avrgcccpp.compiler.optimization.DebugLevel>
|
||||
<avrgcccpp.compiler.warnings.AllWarnings>True</avrgcccpp.compiler.warnings.AllWarnings>
|
||||
<avrgcccpp.compiler.warnings.Pedantic>True</avrgcccpp.compiler.warnings.Pedantic>
|
||||
<avrgcccpp.compiler.miscellaneous.OtherFlags>-Wextra -std=c++14</avrgcccpp.compiler.miscellaneous.OtherFlags>
|
||||
<avrgcccpp.linker.libraries.Libraries>
|
||||
<ListValues>
|
||||
<Value>libm</Value>
|
||||
</ListValues>
|
||||
</avrgcccpp.linker.libraries.Libraries>
|
||||
<avrgcccpp.assembler.general.IncludePaths>
|
||||
<ListValues>
|
||||
<Value>%24(PackRepoDir)\Atmel\ATmega_DFP\1.2.193\include</Value>
|
||||
</ListValues>
|
||||
</avrgcccpp.assembler.general.IncludePaths>
|
||||
<avrgcccpp.assembler.debugging.DebugLevel>Default (-Wa,-g)</avrgcccpp.assembler.debugging.DebugLevel>
|
||||
</AvrGccCpp>
|
||||
</ToolchainSettings>
|
||||
<PreBuildEvent>echo "C:\bin\avrdude-6.3\avrdude.exe" -v -p$(avrdevice) %%* -Uflash:w:"$(OutputDirectory)\$(Name).hex":i > "$(MSBuildProjectDirectory)\avrdude.bat"</PreBuildEvent>
|
||||
</PropertyGroup>
|
||||
@ -215,6 +215,12 @@
|
||||
<Compile Include="rtc.h">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="systime.cpp">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="systime.h">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="twi-lowlevel.cpp">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
|
103
ds3231/main.cpp
103
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 <stdio.h>
|
||||
@ -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<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 ) );
|
||||
}
|
||||
#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();
|
||||
}
|
||||
}
|
||||
|
||||
|
107
ds3231/systime.cpp
Normal file
107
ds3231/systime.cpp
Normal 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
29
ds3231/systime.h
Normal 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
|
Loading…
Reference in New Issue
Block a user