|
30 | 30 | #include <hal_init.h> |
31 | 31 | #include <hpl_gclk_base.h> |
32 | 32 | #include <hpl_pm_base.h> |
33 | | -#include <hal_calendar.h> |
34 | 33 |
|
35 | 34 | #include "py/obj.h" |
36 | 35 | #include "py/runtime.h" |
37 | 36 | #include "lib/timeutils/timeutils.h" |
38 | 37 | #include "shared-bindings/rtc/__init__.h" |
| 38 | +#include "supervisor/port.h" |
39 | 39 | #include "supervisor/shared/translate.h" |
40 | 40 |
|
41 | | -static struct calendar_descriptor calendar; |
42 | | - |
43 | | -void rtc_init(void) { |
44 | | -#ifdef SAMD21 |
45 | | - _gclk_enable_channel(RTC_GCLK_ID, CONF_GCLK_RTC_SRC); |
46 | | -#endif |
47 | | -#ifdef SAMD51 |
48 | | - hri_mclk_set_APBAMASK_RTC_bit(MCLK); |
49 | | -#endif |
50 | | - calendar_init(&calendar, RTC); |
51 | | - calendar_set_baseyear(&calendar, 2000); |
52 | | - calendar_enable(&calendar); |
53 | | -} |
| 41 | +// This is the time in seconds since 2000 that the RTC was started. |
| 42 | +static uint32_t rtc_offset = 0; |
54 | 43 |
|
55 | 44 | void common_hal_rtc_get_time(timeutils_struct_time_t *tm) { |
56 | | - struct calendar_date_time datetime; |
57 | | - calendar_get_date_time(&calendar, &datetime); |
58 | | - |
59 | | - tm->tm_year = datetime.date.year; |
60 | | - tm->tm_mon = datetime.date.month; |
61 | | - tm->tm_mday = datetime.date.day; |
62 | | - tm->tm_hour = datetime.time.hour; |
63 | | - tm->tm_min = datetime.time.min; |
64 | | - tm->tm_sec = datetime.time.sec; |
| 45 | + uint64_t ticks_s = port_get_raw_ticks(NULL) / 1024; |
| 46 | + timeutils_seconds_since_2000_to_struct_time(rtc_offset + ticks_s, tm); |
65 | 47 | } |
66 | 48 |
|
67 | 49 | void common_hal_rtc_set_time(timeutils_struct_time_t *tm) { |
68 | | - // Reset prescaler to increase initial precision. Otherwise we can be up to 1 second off already. |
69 | | - uint32_t freqcorr = hri_rtcmode0_read_FREQCORR_reg(calendar.device.hw); |
70 | | - calendar_deinit(&calendar); |
71 | | - rtc_init(); |
72 | | - hri_rtcmode0_write_FREQCORR_reg(calendar.device.hw, freqcorr); |
73 | | - |
74 | | - struct calendar_date date = { |
75 | | - .year = tm->tm_year, |
76 | | - .month = tm->tm_mon, |
77 | | - .day = tm->tm_mday, |
78 | | - }; |
79 | | - calendar_set_date(&calendar, &date); |
80 | | - |
81 | | - struct calendar_time time = { |
82 | | - .hour = tm->tm_hour, |
83 | | - .min = tm->tm_min, |
84 | | - .sec = tm->tm_sec, |
85 | | - }; |
86 | | - calendar_set_time(&calendar, &time); |
| 50 | + uint64_t ticks_s = port_get_raw_ticks(NULL) / 1024; |
| 51 | + uint32_t epoch_s = timeutils_seconds_since_2000( |
| 52 | + tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec |
| 53 | + ); |
| 54 | + rtc_offset = epoch_s - ticks_s; |
87 | 55 | } |
88 | 56 |
|
89 | 57 | // A positive value speeds up the clock by removing clock cycles. |
90 | 58 | int common_hal_rtc_get_calibration(void) { |
91 | | - int calibration = hri_rtcmode0_read_FREQCORR_VALUE_bf(calendar.device.hw); |
| 59 | + int calibration = hri_rtcmode0_read_FREQCORR_VALUE_bf(RTC); |
92 | 60 |
|
93 | | - if (!hri_rtcmode0_get_FREQCORR_SIGN_bit(calendar.device.hw)) |
| 61 | + if (!hri_rtcmode0_get_FREQCORR_SIGN_bit(RTC)) { |
94 | 62 | calibration = -calibration; |
| 63 | + } |
95 | 64 |
|
96 | 65 | return calibration; |
97 | 66 | } |
98 | 67 |
|
99 | 68 | void common_hal_rtc_set_calibration(int calibration) { |
100 | | - if (calibration > 127 || calibration < -127) |
| 69 | + if (calibration > 127 || calibration < -127) { |
101 | 70 | mp_raise_ValueError(translate("calibration value out of range +/-127")); |
| 71 | + } |
102 | 72 |
|
103 | | - hri_rtcmode0_write_FREQCORR_SIGN_bit(calendar.device.hw, calibration < 0 ? 0 : 1); |
104 | | - hri_rtcmode0_write_FREQCORR_VALUE_bf(calendar.device.hw, abs(calibration)); |
| 73 | + hri_rtcmode0_write_FREQCORR_SIGN_bit(RTC, calibration < 0 ? 0 : 1); |
| 74 | + hri_rtcmode0_write_FREQCORR_VALUE_bf(RTC, abs(calibration)); |
105 | 75 | } |
0 commit comments