Skip to content

Commit 54c281d

Browse files
authored
Merge pull request #18 from facchinm/master
Expose some additional useful APIs
2 parents 5f80582 + aa09d6e commit 54c281d

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

src/RTCZero.cpp

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,7 @@ void RTCZero::begin(bool resetTime)
5252
}
5353

5454
// Setup clock GCLK2 with OSC32K divided by 32
55-
GCLK->GENDIV.reg = GCLK_GENDIV_ID(2)|GCLK_GENDIV_DIV(4);
56-
while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY)
57-
;
58-
GCLK->GENCTRL.reg = (GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_XOSC32K | GCLK_GENCTRL_ID(2) | GCLK_GENCTRL_DIVSEL );
59-
while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY)
60-
;
61-
GCLK->CLKCTRL.reg = (uint32_t)((GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK2 | (RTC_GCLK_ID << GCLK_CLKCTRL_ID_Pos)));
62-
while (GCLK->STATUS.bit.SYNCBUSY)
63-
;
55+
configureClock();
6456

6557
RTCdisable();
6658

@@ -390,6 +382,21 @@ uint32_t RTCZero::getY2kEpoch()
390382
return (getEpoch() - EPOCH_TIME_OFF);
391383
}
392384

385+
void RTCZero::setAlarmEpoch(uint32_t ts)
386+
{
387+
if (_configured) {
388+
if (ts < EPOCH_TIME_OFF) {
389+
ts = EPOCH_TIME_OFF;
390+
}
391+
392+
time_t t = ts;
393+
struct tm* tmp = gmtime(&t);
394+
395+
setAlarmDate(tmp->tm_year - EPOCH_TIME_YEAR_OFF, tmp->tm_mon + 1, tmp->tm_mday);
396+
setAlarmTime(tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
397+
}
398+
}
399+
393400
void RTCZero::setEpoch(uint32_t ts)
394401
{
395402
if (_configured) {
@@ -419,6 +426,19 @@ void RTCZero::setY2kEpoch(uint32_t ts)
419426
}
420427
}
421428

429+
/* Attach peripheral clock to 32k oscillator */
430+
void RTCZero::configureClock() {
431+
GCLK->GENDIV.reg = GCLK_GENDIV_ID(2)|GCLK_GENDIV_DIV(4);
432+
while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY)
433+
;
434+
GCLK->GENCTRL.reg = (GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_XOSC32K | GCLK_GENCTRL_ID(2) | GCLK_GENCTRL_DIVSEL );
435+
while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY)
436+
;
437+
GCLK->CLKCTRL.reg = (uint32_t)((GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK2 | (RTC_GCLK_ID << GCLK_CLKCTRL_ID_Pos)));
438+
while (GCLK->STATUS.bit.SYNCBUSY)
439+
;
440+
}
441+
422442
/*
423443
* Private Utility Functions
424444
*/

src/RTCZero.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,19 @@ class RTCZero {
9595
uint32_t getY2kEpoch();
9696
void setEpoch(uint32_t ts);
9797
void setY2kEpoch(uint32_t ts);
98+
void setAlarmEpoch(uint32_t ts);
99+
100+
bool isConfigured() {
101+
_configured = RTC->MODE2.CTRL.reg & RTC_MODE2_CTRL_ENABLE;
102+
configureClock();
103+
return _configured;
104+
}
98105

99106
private:
100107
bool _configured;
101108

102109
void config32kOSC(void);
110+
void configureClock(void);
103111
void RTCreadRequest();
104112
bool RTCisSyncing(void);
105113
void RTCdisable();

0 commit comments

Comments
 (0)