Skip to content

Commit 10e8e3b

Browse files
committed
Add method for compensating RTC drift
1 parent d7d8348 commit 10e8e3b

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

docs/api.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,3 +411,17 @@ rtc.standbyMode()
411411
#### Parameters
412412

413413
None
414+
415+
### `setFrequencyCorrection()`
416+
417+
Set the RTC frequency correction value. A positive value reduces the frequency, a negative value will increase the frequency.
418+
419+
#### Syntax
420+
421+
``` arduino
422+
rtc.setFrequencyCorrection(int8_t correction)
423+
```
424+
425+
#### Parameters
426+
427+
- correction: the number of counts to be increased or decreasd periodically by the RTC frequency correction module.

keywords.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ disableAlarm KEYWORD2
5454

5555
standbyMode KEYWORD2
5656

57+
setFrequencyCorrection KEYWORD2
58+
5759
#######################################
5860
# Constants (LITERAL1)
5961
#######################################

src/RTCZero.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,34 @@ void RTCZero::configureClock() {
464464
;
465465
}
466466

467+
/*
468+
* From: SAM D21 Family Datasheet - Chapter 19.6.9.2
469+
* ============================================================================
470+
* Correction[ppm] = (FREQCORR.VALUE / 4096 * 240) * 10^6 ppm
471+
*
472+
* FREQCORR.VALUE = (correction[ppm] * 4096 * 240) / 10^6 = correction * 4096 * 240
473+
*
474+
* Example:
475+
* Correction = 1s / 24h = 1s / 86400s = 1/86400
476+
* FREQCORR.VALUE = 1/86400 * 4096 * 240 = 11
477+
*/
478+
479+
void RTCZero::setFrequencyCorrection(int8_t correction)
480+
{
481+
if (correction == 0 || correction == -128) {
482+
return;
483+
}
484+
485+
uint8_t sign = (correction & 0x80);
486+
487+
if (correction < 0) {
488+
correction *= -1;
489+
}
490+
491+
while(RTC->MODE2.STATUS.bit.SYNCBUSY);
492+
RTC->MODE2.FREQCORR.reg = sign | correction;
493+
}
494+
467495
/*
468496
* Private Utility Functions
469497
*/

src/RTCZero.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
#include "Arduino.h"
2424

25+
#define RTCZERO_FREQCORR(_CORR_) (_CORR_ * 240 * 4096)
26+
2527
typedef void(*voidFuncPtr)(void);
2628

2729
class RTCZero {
@@ -88,6 +90,8 @@ class RTCZero {
8890
void setAlarmMonth(uint8_t month);
8991
void setAlarmYear(uint8_t year);
9092
void setAlarmDate(uint8_t day, uint8_t month, uint8_t year);
93+
94+
void setFrequencyCorrection(int8_t correction);
9195

9296
/* Epoch Functions */
9397

0 commit comments

Comments
 (0)