Skip to content

Commit 62d7a80

Browse files
committed
atmel-samd: RTC: Improve precision when setting datetime
The prescaler is not reset when setting the counter so we can be off by as much as 1023/1024 seconds. Fix this by resetting the RTC module.
1 parent 83e3907 commit 62d7a80

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

  • ports/atmel-samd/common-hal/rtc

ports/atmel-samd/common-hal/rtc/RTC.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ void common_hal_rtc_get_time(timeutils_struct_time_t *tm) {
6464
}
6565

6666
void common_hal_rtc_set_time(timeutils_struct_time_t *tm) {
67+
// Reset prescaler to increase initial precision. Otherwise we can be up to 1 second off already.
68+
uint32_t freqcorr = hri_rtcmode0_read_FREQCORR_reg(calendar.device.hw);
69+
calendar_deinit(&calendar);
70+
rtc_init();
71+
hri_rtcmode0_write_FREQCORR_reg(calendar.device.hw, freqcorr);
72+
6773
struct calendar_date date = {
6874
.year = tm->tm_year,
6975
.month = tm->tm_mon,

0 commit comments

Comments
 (0)