Skip to content

Commit 652f85b

Browse files
committed
Moved __time24 into the class.
Also removed 1 second delay in begin(). It was unnecessary after all.
1 parent 50d9b8b commit 652f85b

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

src/RTCZero.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,19 @@
2424

2525
static const uint8_t daysInMonth[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
2626

27-
static bool __time24 = false;
28-
2927
voidFuncPtr RTC_callBack = NULL;
3028

29+
RTCZero::RTCZero()
30+
{
31+
_time24 = false;
32+
}
33+
3134
void RTCZero::begin(bool timeRep)
3235
{
3336
uint16_t tmp_reg = 0;
3437

3538
if (timeRep)
36-
__time24 = true; // 24h representation chosen
39+
_time24 = true; // 24h representation chosen
3740

3841
PM->APBAMASK.reg |= PM_APBAMASK_RTC; // turn on digital interface clock
3942
config32kOSC();
@@ -58,7 +61,7 @@ void RTCZero::begin(bool timeRep)
5861
tmp_reg &= ~RTC_MODE2_CTRL_MATCHCLR; // disable clear on match
5962

6063
//According to the datasheet RTC_MODE2_CTRL_CLKREP = 0 for 24h
61-
if (timeRep)
64+
if (_time24)
6265
tmp_reg &= ~RTC_MODE2_CTRL_CLKREP; // 24h time representation
6366
else
6467
tmp_reg |= RTC_MODE2_CTRL_CLKREP; // 12h time representation
@@ -80,9 +83,6 @@ void RTCZero::begin(bool timeRep)
8083

8184
RTCenable();
8285
RTCresetRemove();
83-
84-
// The clock does not seem to sync until the first tick
85-
delay(1000);
8686
}
8787

8888
void RTC_Handler(void)
@@ -136,7 +136,7 @@ uint8_t RTCZero::getHours()
136136
{
137137
uint8_t hours = RTC->MODE2.CLOCK.bit.HOUR;
138138

139-
if (!__time24) {
139+
if (!_time24) {
140140
hours &= ~RTC_MODE2_CLOCK_HOUR_PM_Val;
141141
}
142142

@@ -147,7 +147,7 @@ uint8_t RTCZero::getAM_PM()
147147
{
148148
uint8_t result = RTC_AM_PM::RTC_AM;
149149

150-
if (__time24) {
150+
if (_time24) {
151151
if (RTC->MODE2.CLOCK.bit.HOUR > 11) {
152152
result = RTC_AM_PM::RTC_PM;
153153
}
@@ -190,7 +190,7 @@ uint8_t RTCZero::getAlarmHours()
190190
{
191191
uint8_t hours = RTC->MODE2.Mode2Alarm[0].ALARM.bit.HOUR;
192192

193-
if (!__time24) {
193+
if (!_time24) {
194194
hours &= ~RTC_MODE2_CLOCK_HOUR_PM_Val;
195195
}
196196

@@ -201,7 +201,7 @@ uint8_t RTCZero::getAlarmAM_PM()
201201
{
202202
uint8_t result = RTC_AM_PM::RTC_AM;
203203

204-
if (__time24) {
204+
if (_time24) {
205205
if (RTC->MODE2.Mode2Alarm[0].ALARM.bit.HOUR > 11) {
206206
result = RTC_AM_PM::RTC_PM;
207207
}
@@ -250,7 +250,7 @@ void RTCZero::setMinutes(uint8_t minutes)
250250

251251
void RTCZero::setHours(uint8_t hours, uint8_t am_pm)
252252
{
253-
if (!__time24)
253+
if (!_time24)
254254
{
255255
if (hours > 12) {
256256
hours -= 12;
@@ -317,7 +317,7 @@ void RTCZero::setAlarmMinutes(uint8_t minutes)
317317

318318
void RTCZero::setAlarmHours(uint8_t hours, uint8_t am_pm)
319319
{
320-
if (!__time24)
320+
if (!_time24)
321321
{
322322
if (hours > 12) {
323323
hours -= 12;
@@ -376,6 +376,7 @@ uint32_t RTCZero::getEpoch()
376376
uint32_t RTCZero::getY2kEpoch()
377377
{
378378
uint16_t days = RTC->MODE2.CLOCK.bit.DAY;
379+
days = days > 0 ? days : 1;
379380
uint8_t months = RTC->MODE2.CLOCK.bit.MONTH;
380381
uint16_t years = RTC->MODE2.CLOCK.bit.YEAR;
381382

@@ -386,12 +387,11 @@ uint32_t RTCZero::getY2kEpoch()
386387
if ((months > 2) && (years % 4 == 0)) {
387388
++days;
388389
}
389-
390390
days += 365 * years + (years + 3) / 4 - 1;
391-
391+
392392
uint8_t hours = RTC->MODE2.CLOCK.bit.HOUR;
393393

394-
if (!__time24) {
394+
if (!_time24) {
395395
uint8_t pm = hours & RTC_MODE2_CLOCK_HOUR_PM_Val;
396396
hours &= ~RTC_MODE2_CLOCK_HOUR_PM_Val;
397397

src/RTCZero.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class RTCZero {
4747
MATCH_YYMMDDHHMMSS = RTC_MODE2_MASK_SEL_YYMMDDHHMMSS_Val // Once, on a specific date and a specific time
4848
};
4949

50-
RTCZero() {};
50+
RTCZero();
5151
void begin(bool timeRep);
5252

5353
void enableAlarm(Alarm_Match match);
@@ -106,6 +106,8 @@ class RTCZero {
106106
void setY2kEpoch(uint32_t ts);
107107

108108
private:
109+
bool _time24;
110+
109111
void config32kOSC(void);
110112
bool RTCisSyncing(void);
111113
void RTCdisable();

0 commit comments

Comments
 (0)