Move C time API glue from driver to user space
This commit is contained in:
parent
b2065b987a
commit
e9d3a95239
@ -1 +1 @@
|
|||||||
Subproject commit 9303fbf5b59bd934ec0c91d5540b851366da92e1
|
Subproject commit 04af54e7c8029f05a999f85afa0bcc194f654fed
|
@ -83,17 +83,6 @@
|
|||||||
</custom>
|
</custom>
|
||||||
<AAFDebugger>
|
<AAFDebugger>
|
||||||
<AAFDebugFiles>
|
<AAFDebugFiles>
|
||||||
<DebugFile>
|
|
||||||
<path>\Debug\ds3231.lss</path>
|
|
||||||
<AAFSetting>
|
|
||||||
<Label>Lss Files</Label>
|
|
||||||
<Extention>.lss</Extention>
|
|
||||||
<Regex>^\s*(?<address>[a-f0-9]*):\s*.*$</Regex>
|
|
||||||
<DebugEnabled>true</DebugEnabled>
|
|
||||||
<RegexGroups>address</RegexGroups>
|
|
||||||
<DebuggerExpression>$pc</DebuggerExpression>
|
|
||||||
</AAFSetting>
|
|
||||||
</DebugFile>
|
|
||||||
</AAFDebugFiles>
|
</AAFDebugFiles>
|
||||||
</AAFDebugger>
|
</AAFDebugger>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
@ -224,12 +213,6 @@
|
|||||||
<Compile Include="ds3231\rtc.h">
|
<Compile Include="ds3231\rtc.h">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="ds3231\systime.cpp">
|
|
||||||
<SubType>compile</SubType>
|
|
||||||
</Compile>
|
|
||||||
<Compile Include="ds3231\systime.h">
|
|
||||||
<SubType>compile</SubType>
|
|
||||||
</Compile>
|
|
||||||
<Compile Include="ds3231\twi-lowlevel.cpp">
|
<Compile Include="ds3231\twi-lowlevel.cpp">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
@ -251,6 +234,12 @@
|
|||||||
<Compile Include="main.cpp">
|
<Compile Include="main.cpp">
|
||||||
<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="type\type.hpp">
|
<Compile Include="type\type.hpp">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
#include "ds3231/systime.h"
|
#include "systime.h"
|
||||||
#include "uart/uart.hpp"
|
#include "uart/uart.hpp"
|
||||||
|
|
||||||
using uart_t = uart::Uart0<>;
|
using uart_t = uart::Uart0<>;
|
||||||
|
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 "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
|
Loading…
Reference in New Issue
Block a user