Compare commits

...

6 Commits

Author SHA1 Message Date
04af54e7c8 Remove C time API glue from driver 2020-05-15 10:20:56 +02:00
9303fbf5b5 Fix clock path 2020-05-15 09:40:08 +02:00
727a974504 Fix warnings 2020-05-15 09:25:48 +02:00
b3364f0b88 Add gitignore, clang-format and license file 2020-05-15 09:20:23 +02:00
22a74d0b77 Imported changes from example branch 2017-12-17 10:55:25 +01:00
8e2e18128b Exported code as submodule 2017-12-17 10:48:59 +01:00
18 changed files with 48 additions and 664 deletions

13
.clang-format Normal file
View File

@@ -0,0 +1,13 @@
---
BasedOnStyle: LLVM
ColumnLimit: 120
IndentWidth: 4
TabWidth: 4
UseTab: ForIndentation
AlignEscapedNewlines: DontAlign
AllowShortFunctionsOnASingleLine: Empty
AlwaysBreakTemplateDeclarations: true
BreakBeforeBraces: Custom
BraceWrapping:
AfterFunction: true
...

8
.gitignore vendored
View File

@@ -2,4 +2,10 @@
Release
Debug
*.componentinfo.xml
avrdude.bat
*.elf
*.o
*.hex
*.srec
*.eeprom
*.lss
*.map

3
.gitmodules vendored
View File

@@ -1,3 +0,0 @@
[submodule "ds3231/usart"]
path = ds3231/usart
url = git@blackmark.me:usart.git

21
LICENSE Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2020 BlackMark
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice (including the next
paragraph) shall be included in all copies or substantial portions of the
Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -1,22 +0,0 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Atmel Studio Solution File, Format Version 11.00
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{E66E83B9-2572-4076-B26E-6BE79FF3018A}") = "ds3231", "ds3231\ds3231.cppproj", "{DCE6C7E3-EE26-4D79-826B-08594B9AD897}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|AVR = Debug|AVR
Release|AVR = Release|AVR
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Debug|AVR.ActiveCfg = Debug|AVR
{DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Debug|AVR.Build.0 = Debug|AVR
{DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Release|AVR.ActiveCfg = Release|AVR
{DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Release|AVR.Build.0 = Release|AVR
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

View File

@@ -1,56 +0,0 @@
/*
* Copyright (c) by BlackMark 2016
* Date 09/09/2016
* Version 1.4
*/
#ifndef BOOTLOADER_H
#define BOOTLOADER_H
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
typedef void (*flash)() __attribute__ ((noreturn));
flash boot = reinterpret_cast<flash>( 0x0000 );
flash bootloader = reinterpret_cast<flash>( 0x7E00 / 2 );
inline bool checkBootloader()
{
if( pgm_read_byte( reinterpret_cast<uint16_t>( bootloader ) * 2 ) == 0xF8 )
{
return true;
}
return false;
}
inline void callBootloader()
{
if( checkBootloader() )
{
bootloader();
}
else
{
boot();
}
}
inline uint8_t handleReset()
{
wdt_reset();
uint8_t ui8MCUSR = MCUSR;
MCUSR &= ~( 1 << WDRF );
wdt_disable();
return ui8MCUSR;
}
inline void reset()
{
wdt_enable( WDTO_15MS );
while( true );
}
#endif

View File

@@ -1,14 +0,0 @@
/*
* Copyright (c) by BlackMark 2015
* Date 24/11/2015
* Version 1.1
*/
#ifndef CLOCK_H
#define CLOCK_H
#define F_CPU 8000000
#include <util/delay.h>
#endif

View File

@@ -1,247 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="14.0">
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
<ProjectVersion>7.0</ProjectVersion>
<ToolchainName>com.Atmel.AVRGCC8.CPP</ToolchainName>
<ProjectGuid>dce6c7e3-ee26-4d79-826b-08594b9ad897</ProjectGuid>
<avrdevice>ATmega328P</avrdevice>
<avrdeviceseries>none</avrdeviceseries>
<OutputType>Executable</OutputType>
<Language>CPP</Language>
<OutputFileName>$(MSBuildProjectName)</OutputFileName>
<OutputFileExtension>.elf</OutputFileExtension>
<OutputDirectory>$(MSBuildProjectDirectory)\$(Configuration)</OutputDirectory>
<AssemblyName>ds3231</AssemblyName>
<Name>ds3231</Name>
<RootNamespace>ds3231</RootNamespace>
<ToolchainFlavour>Native</ToolchainFlavour>
<KeepTimersRunning>true</KeepTimersRunning>
<OverrideVtor>false</OverrideVtor>
<CacheFlash>true</CacheFlash>
<ProgFlashFromRam>true</ProgFlashFromRam>
<RamSnippetAddress>0x20000000</RamSnippetAddress>
<UncachedRange />
<preserveEEPROM>true</preserveEEPROM>
<OverrideVtorValue>exception_table</OverrideVtorValue>
<BootSegment>2</BootSegment>
<eraseonlaunchrule>0</eraseonlaunchrule>
<AsfFrameworkConfig>
<framework-data xmlns="">
<options />
<configurations />
<files />
<documentation help="" />
<offline-documentation help="" />
<dependencies>
<content-extension eid="atmel.asf" uuidref="Atmel.ASF" version="3.34.2" />
</dependencies>
</framework-data>
</AsfFrameworkConfig>
<avrtool>com.atmel.avrdbg.tool.stk500</avrtool>
<avrtoolserialnumber />
<avrdeviceexpectedsignature>0x1E950F</avrdeviceexpectedsignature>
<custom>
<ToolOptions>
<InterfaceProperties>
</InterfaceProperties>
<InterfaceName>
</InterfaceName>
</ToolOptions>
<ToolType>custom</ToolType>
<ToolNumber>
</ToolNumber>
<ToolName>Custom Programming Tool</ToolName>
</custom>
<avrtoolinterface>ISP</avrtoolinterface>
<com_atmel_avrdbg_tool_simulator>
<ToolOptions xmlns="">
<InterfaceProperties>
</InterfaceProperties>
<InterfaceName>
</InterfaceName>
</ToolOptions>
<ToolType xmlns="">com.atmel.avrdbg.tool.simulator</ToolType>
<ToolNumber xmlns="">
</ToolNumber>
<ToolName xmlns="">Simulator</ToolName>
</com_atmel_avrdbg_tool_simulator>
<ResetRule>0</ResetRule>
<EraseKey />
<com_atmel_avrdbg_tool_stk500>
<ToolOptions>
<InterfaceProperties>
<IspClock>125000</IspClock>
</InterfaceProperties>
<InterfaceName>ISP</InterfaceName>
</ToolOptions>
<ToolType>com.atmel.avrdbg.tool.stk500</ToolType>
<ToolNumber>
</ToolNumber>
<ToolName>STK500</ToolName>
</com_atmel_avrdbg_tool_stk500>
<avrtoolinterfaceclock>125000</avrtoolinterfaceclock>
</PropertyGroup>
<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>
</ToolchainSettings>
<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 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>
</ToolchainSettings>
<PreBuildEvent>echo "C:\bin\avrdude-6.3\avrdude.exe" -v -p$(avrdevice) %%* -Uflash:w:"$(OutputDirectory)\$(Name).hex":i &gt; "$(MSBuildProjectDirectory)\avrdude.bat"</PreBuildEvent>
</PropertyGroup>
<ItemGroup>
<Compile Include="bootloader.h">
<SubType>compile</SubType>
</Compile>
<Compile Include="clock.h">
<SubType>compile</SubType>
</Compile>
<Compile Include="main.cpp">
<SubType>compile</SubType>
</Compile>
<Compile Include="rtc.cpp">
<SubType>compile</SubType>
</Compile>
<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>
<Compile Include="twi-lowlevel.h">
<SubType>compile</SubType>
</Compile>
<Compile Include="twi.cpp">
<SubType>compile</SubType>
</Compile>
<Compile Include="twi.h">
<SubType>compile</SubType>
</Compile>
<Compile Include="usart\usart.cpp">
<SubType>compile</SubType>
</Compile>
<Compile Include="usart\usart.h">
<SubType>compile</SubType>
</Compile>
</ItemGroup>
<ItemGroup>
<Folder Include="usart" />
</ItemGroup>
<Import Project="$(AVRSTUDIO_EXE_PATH)\\Vs\\Compiler.targets" />
</Project>

View File

@@ -1,178 +0,0 @@
/*
* Copyright (c) by BlackMark 2017
* Date 17/12/2017
* Version 1.2
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <avr/interrupt.h>
#include "usart/usart.h"
#include "clock.h"
#include "bootloader.h"
#include "systime.h"
void setup()
{
USART0 &cSerial = USART0::inst();
if( SysTime::init() )
{
cSerial << "DS3231 detected!" << "\r\n";
}
else
{
cSerial << "ERROR - No RTC detected!" << "\r\n";
}
}
void setTime()
{
USART0 &cSerial = USART0::inst();
tm sTime;
constexpr auto BUFFER_SIZE = 32;
char szBuffer[BUFFER_SIZE];
cSerial << "Set time:" << "\r\n" << "Year: ";
cSerial.receiveLine( szBuffer, BUFFER_SIZE, "\r" );
cSerial << szBuffer << "\r\n";
sTime.tm_year = atoi( szBuffer ) - 1900;
cSerial << "Set time:" << "\r\n" << "Month: ";
cSerial.receiveLine( szBuffer, BUFFER_SIZE, "\r" );
cSerial << szBuffer << "\r\n";
sTime.tm_mon = atoi( szBuffer ) - 1;
cSerial << "Set time:" << "\r\n" << "Day: ";
cSerial.receiveLine( szBuffer, BUFFER_SIZE, "\r" );
cSerial << szBuffer << "\r\n";
sTime.tm_mday = atoi( szBuffer );
cSerial << "Set time:" << "\r\n" << "Hour: ";
cSerial.receiveLine( szBuffer, BUFFER_SIZE, "\r" );
cSerial << szBuffer << "\r\n";
sTime.tm_hour = atoi( szBuffer );
cSerial << "Minute: ";
cSerial.receiveLine( szBuffer, BUFFER_SIZE, "\r" );
cSerial << szBuffer << "\r\n";
sTime.tm_min = atoi( szBuffer );
cSerial << "Second: ";
cSerial.receiveLine( szBuffer, BUFFER_SIZE, "\r" );
cSerial << szBuffer << "\r\n";
sTime.tm_sec = atoi( szBuffer );
mktime( &sTime );
SysTime::setTime( sTime );
}
void setAlarm()
{
USART0 &cSerial = USART0::inst();
rtc_tm* tmAlarm = rtc_get_alarm();
constexpr auto BUFFER_SIZE = 32;
char szBuffer[BUFFER_SIZE];
cSerial << "Set alarm:" << "\r\n" << "Hour: ";
cSerial.receiveLine( szBuffer, BUFFER_SIZE, "\r" );
cSerial << szBuffer << "\r\n";
tmAlarm->hour = atoi( szBuffer );
cSerial << "Minute: ";
cSerial.receiveLine( szBuffer, BUFFER_SIZE, "\r" );
cSerial << szBuffer << "\r\n";
tmAlarm->min = atoi( szBuffer );
cSerial << "Second: ";
cSerial.receiveLine( szBuffer, BUFFER_SIZE, "\r" );
cSerial << szBuffer << "\r\n";
tmAlarm->sec = atoi( szBuffer );
rtc_set_alarm( tmAlarm );
}
int main()
{
uint8_t ui8MCUSR = handleReset();
if( ui8MCUSR & ( 1 << WDRF ) )
{
callBootloader();
}
USART0 &cSerial = USART0::inst();
cSerial.init( 9600 );
sei();
setup();
constexpr auto BUFFER_SIZE = 32;
char szBuffer[BUFFER_SIZE];
while( true )
{
if( !SysTime::checkSync() )
{
SysTime::syncSysTime();
time_t timeNow = time( nullptr );
tm *psLocalTime = localtime( &timeNow );
strftime( szBuffer, BUFFER_SIZE, "%F %T %z", psLocalTime );
cSerial << "Time: " << szBuffer << "\r\n";
if( rtc_check_alarm() )
{
cSerial << "ALARM!" << "\r\n";
}
}
uint8_t ui8Cmd;
if( cSerial.receiveByte( ui8Cmd ) )
{
if( ui8Cmd == 'b' )
{
cSerial << "Bootloader . . . " << "\r\n";
_delay_ms( 1000 );
reset();
}
else if( ui8Cmd == 's' )
{
setTime();
}
else if( ui8Cmd == 'a' )
{
setAlarm();
}
else if( ui8Cmd == 'p' )
{
uint8_t ui8Hour;
uint8_t ui8Minute;
uint8_t ui8Second;
rtc_get_alarm_s( &ui8Hour, &ui8Minute, &ui8Second );
sprintf( szBuffer, "Alarm set to: %02d:%02d:%02d", ui8Hour, ui8Minute, ui8Second );
cSerial << szBuffer << "\r\n";
}
else
{
cSerial << "Invalid char: '";
cSerial.transmitByte( ui8Cmd );
cSerial << "'" << "\r\n";
}
}
}
return 0;
}

View File

@@ -1,107 +0,0 @@
#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();
}

View File

@@ -1,29 +0,0 @@
/*
* 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

Submodule ds3231/usart deleted from b2a3b03867

View File

@@ -271,7 +271,7 @@ void rtc_run_clock(bool run)
else
b |= _BV(CH_BIT); // set bit
rtc_write_byte(b, 0x0);
rtc_write_byte(b, 0x0);
}
// DS1307 only

View File

View File

@@ -79,7 +79,7 @@ void twi_init(void)
// initialize twi prescaler and bit rate
cbi(TWSR, TWPS0);
cbi(TWSR, TWPS1);
TWBR = ((CPU_FREQ / TWI_FREQ) - 16) / 2;
TWBR = ((F_CPU / TWI_FREQ) - 16) / 2;
/* twi bit rate formula from atmega128 manual pg 204
SCL Frequency = CPU Clock Frequency / (16 + (2 * TWBR))
@@ -369,6 +369,7 @@ SIGNAL(TWI_vect)
case TW_MR_DATA_ACK: // data received, ack sent
// put byte into buffer
twi_masterBuffer[twi_masterBufferIndex++] = TWDR;
[[fallthrough]];
case TW_MR_SLA_ACK: // address sent, ack received
// ack if more bytes are expected, otherwise nack
if(twi_masterBufferIndex < twi_masterBufferLength){
@@ -380,6 +381,7 @@ SIGNAL(TWI_vect)
case TW_MR_DATA_NACK: // data received, nack sent
// put final byte into buffer
twi_masterBuffer[twi_masterBufferIndex++] = TWDR;
[[fallthrough]];
case TW_MR_SLA_NACK: // address sent, nack received
twi_stop();
break;
@@ -446,6 +448,7 @@ SIGNAL(TWI_vect)
twi_txBuffer[0] = 0x00;
}
// transmit first byte from buffer, fall
[[fallthrough]];
case TW_ST_DATA_ACK: // byte sent, ack returned
// copy data to output register
TWDR = twi_txBuffer[twi_txBufferIndex++];

View File

@@ -22,11 +22,9 @@
#include <inttypes.h>
//#define ATMEGA8
#include "../clock.hpp"
#ifndef CPU_FREQ
#define CPU_FREQ 16000000L
#endif
//#define ATMEGA8
#ifndef TWI_FREQ
#define TWI_FREQ 100000L

View File