Skip to content

Commit e2a5f0e

Browse files
committed
Fix/Updated systime.c/h.
Removed DeltaTime global variable definition. The value is stored on MCU backup registers. Added SysTimeGetMcuTime API. Returns a SysTime_t value which corresponds to the elapsed time since the MCU started.
1 parent 4b3e43e commit e2a5f0e

File tree

3 files changed

+46
-52
lines changed

3 files changed

+46
-52
lines changed

src/boards/SAML21/rtc-board.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ Gpio_t DbgRtcPin0;
7575
Gpio_t DbgRtcPin1;
7676
#endif
7777

78+
/*!
79+
* Used to store the Seconds and SubSeconds.
80+
*
81+
* WARNING: Temporary fix fix. Should use MCU NVM internal
82+
* registers
83+
*/
84+
uint32_t RtcBkupRegisters[] = { 0, 0 };
85+
7886
/*!
7987
* \brief Callback for the hw_timer when alarm expired
8088
*/
@@ -220,12 +228,18 @@ uint32_t RtcGetCalendarTime( uint16_t *milliseconds )
220228

221229
void RtcBkupWrite( uint32_t data0, uint32_t data1 )
222230
{
223-
// Not implemented
231+
CRITICAL_SECTION_BEGIN( );
232+
RtcBkupRegisters[0] = data0;
233+
RtcBkupRegisters[1] = data1;
234+
CRITICAL_SECTION_END( );
224235
}
225236

226237
void RtcBkupRead( uint32_t* data0, uint32_t* data1 )
227238
{
228-
// Not implemented
239+
CRITICAL_SECTION_BEGIN( );
240+
*data0 = RtcBkupRegisters[0];
241+
*data1 = RtcBkupRegisters[1];
242+
CRITICAL_SECTION_END( );
229243
}
230244

231245
void RtcProcess( void )

src/system/systime.c

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#define END_OF_FEBRUARY_NORM 59 //31+28
3333
#define END_OF_JULY_NORM 212 //31+28+...
3434

35-
#define UNIX_YEAR 68 //68 is leap year
35+
#define UNIX_YEAR 68 //1968 is leap year
3636

3737
//UNIX time 0 = start at 01:00:00, 01/01/1970
3838
#define UNIX_HOUR_OFFSET ( ( TM_DAYS_IN_LEAP_YEAR + TM_DAYS_IN_YEAR ) * TM_SECONDS_IN_1DAY )
@@ -44,8 +44,8 @@
4444
#define DAYS_IN_MONTH_CORRECTION_LEAP ( (uint32_t )0x445550 )
4545

4646

47-
/* 1461 = 366 + 365 + 365 + 365 */
48-
#define DIV_1461( X ) ( ( ( X ) * 91867 + 22750 ) >> 25 )
47+
/* 365.25 = (366 + 365 + 365 + 365)/4 */
48+
#define DIV_365_25( X ) ( ( ( X ) * 91867 + 22750 ) >> 25 )
4949

5050
#define DIV_APPROX_86400( X ) ( ( ( X ) >> 18 ) + ( ( X ) >> 17 ) )
5151

@@ -71,15 +71,6 @@ static void CalendarDiv86400( uint32_t in, uint32_t* out, uint32_t* remainder );
7171
static uint32_t CalendarDiv61( uint32_t in );
7272
static void CalendarDiv60( uint32_t in, uint32_t* out, uint32_t* remainder );
7373

74-
/*!
75-
* \brief This is the time difference between the calendar time and UNIX time.
76-
*/
77-
static SysTime_t DeltaTime =
78-
{
79-
.Seconds = 0,
80-
.SubSeconds = 0
81-
};
82-
8374
const char *WeekDayString[]={ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
8475

8576
SysTime_t SysTimeAdd( SysTime_t a, SysTime_t b )
@@ -112,33 +103,47 @@ SysTime_t SysTimeSub( SysTime_t a, SysTime_t b )
112103

113104
void SysTimeSet( SysTime_t sysTime )
114105
{
106+
SysTime_t deltaTime;
107+
115108
SysTime_t calendarTime = { .Seconds = 0, .SubSeconds = 0 };
116109

117110
calendarTime.Seconds = RtcGetCalendarTime( ( uint16_t* )&calendarTime.SubSeconds );
118111

119112
// sysTime is epoch
120-
DeltaTime = SysTimeSub( sysTime, calendarTime );
113+
deltaTime = SysTimeSub( sysTime, calendarTime );
121114

122-
RtcBkupWrite( DeltaTime.Seconds, ( uint32_t )DeltaTime.SubSeconds );
115+
RtcBkupWrite( deltaTime.Seconds, ( uint32_t )deltaTime.SubSeconds );
123116
}
124117

125118
SysTime_t SysTimeGet( void )
126119
{
127120
SysTime_t calendarTime = { .Seconds = 0, .SubSeconds = 0 };
128121
SysTime_t sysTime = { .Seconds = 0, .SubSeconds = 0 };
122+
SysTime_t deltaTime;
129123

130124
calendarTime.Seconds = RtcGetCalendarTime( ( uint16_t* )&calendarTime.SubSeconds );
131125

132-
RtcBkupRead( &DeltaTime.Seconds, ( uint32_t* )&DeltaTime.SubSeconds );
126+
RtcBkupRead( &deltaTime.Seconds, ( uint32_t* )&deltaTime.SubSeconds );
133127

134-
sysTime = SysTimeAdd( DeltaTime, calendarTime );
128+
sysTime = SysTimeAdd( deltaTime, calendarTime );
135129

136130
return sysTime;
137131
}
138132

133+
SysTime_t SysTimeGetMcuTime( void )
134+
{
135+
SysTime_t calendarTime = { .Seconds = 0, .SubSeconds = 0 };
136+
137+
calendarTime.Seconds = RtcGetCalendarTime( ( uint16_t* )&calendarTime.SubSeconds );
138+
139+
return calendarTime;
140+
}
141+
139142
uint32_t SysTime2Ms( SysTime_t sysTime )
140143
{
141-
SysTime_t calendarTime = SysTimeSub( sysTime, DeltaTime );
144+
SysTime_t deltaTime;
145+
RtcBkupRead( &deltaTime.Seconds, ( uint32_t* )&deltaTime.SubSeconds );
146+
SysTime_t calendarTime = SysTimeSub( sysTime, deltaTime );
142147
return calendarTime.Seconds * 1000 + calendarTime.SubSeconds;
143148
}
144149

@@ -201,7 +206,7 @@ void SysTimeLocalTime( const uint32_t timestamp, struct tm *localtime )
201206
localtime->tm_hour = ( uint8_t )divOut;
202207

203208
// Calculates year
204-
localtime->tm_year = DIV_1461( days );
209+
localtime->tm_year = DIV_365_25( days );
205210
days-= DIVC_BY_4( ( TM_DAYS_IN_YEAR * 3 + TM_DAYS_IN_LEAP_YEAR ) * localtime->tm_year );
206211

207212
localtime->tm_yday = days;
@@ -225,26 +230,6 @@ void SysTimeLocalTime( const uint32_t timestamp, struct tm *localtime )
225230
localtime->tm_isdst = -1;
226231
}
227232

228-
void SysTimeGetStr( char dateTimeString[DATE_TIME_STR_LEN] )
229-
{
230-
SysTime_t sysTime;
231-
struct tm dateTime;
232-
233-
sysTime = SysTimeGet( );
234-
SysTimeLocalTime( sysTime.Seconds, &dateTime );
235-
236-
snprintf( dateTimeString,
237-
DATE_TIME_STR_LEN,
238-
"it's %02dh%02dm%02d on %s %02d/%02d/%04d",
239-
dateTime.tm_hour,
240-
dateTime.tm_min,
241-
dateTime.tm_sec,
242-
WeekDayString[dateTime.tm_wday],
243-
dateTime.tm_mday,
244-
dateTime.tm_mon,
245-
dateTime.tm_year + 1900 );
246-
}
247-
248233
static uint32_t CalendarGetMonth( uint32_t days, uint32_t year )
249234
{
250235
uint32_t month;

src/system/systime.h

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,6 @@ extern "C"
7676
*/
7777
#define UNIX_GPS_EPOCH_OFFSET 315964800
7878

79-
/*!
80-
* \brief Size of the string returned by SysTimeGetStr
81-
*/
82-
#define DATE_TIME_STR_LEN 32
83-
8479
/*!
8580
* \brief Structure holding the system time in seconds and milliseconds.
8681
*/
@@ -124,6 +119,13 @@ void SysTimeSet( SysTime_t sysTime );
124119
*/
125120
SysTime_t SysTimeGet( void );
126121

122+
/*!
123+
* \brief Gets current MCU system time
124+
*
125+
* \retval sysTime Current seconds/sub-seconds since Mcu started
126+
*/
127+
SysTime_t SysTimeGetMcuTime( void );
128+
127129
/*!
128130
* Converts the given SysTime to the equivalent RTC value in milliseconds
129131
*
@@ -150,13 +152,6 @@ uint32_t SysTimeMkTime( const struct tm* localtime );
150152
*/
151153
void SysTimeLocalTime( const uint32_t timestamp, struct tm *localtime );
152154

153-
/*!
154-
* \brief Decodes the strFormat and posts it to the circular queue for printing
155-
*
156-
* \param [OUT]: string String buffer pointer.
157-
*/
158-
void SysTimeGetStr( char dateTimeSring[DATE_TIME_STR_LEN] );
159-
160155
#ifdef __cplusplus
161156
}
162157
#endif

0 commit comments

Comments
 (0)