|
| 1 | +// This file is part of the CircuitPython project: https://circuitpython.org |
| 2 | +// |
| 3 | +// SPDX-FileCopyrightText: Copyright (c) 2019 Nick Moore for Adafruit Industries |
| 4 | +// SPDX-FileCopyrightText: Copyright (c) 2019 Artur Pacholec |
| 5 | +// SPDX-FileCopyrightText: Copyright (c) 2025 Peggy Zhu, Analog Devices, Inc. |
| 6 | +// |
| 7 | +// SPDX-License-Identifier: MIT |
| 8 | + |
| 9 | +#include <stdlib.h> |
| 10 | + |
| 11 | +#include "py/obj.h" |
| 12 | +#include "py/runtime.h" |
| 13 | +#include "shared/timeutils/timeutils.h" |
| 14 | +#include "supervisor/port.h" |
| 15 | + |
| 16 | +// This is the time in seconds since 2000 that the RTC was started. |
| 17 | +// TODO: Change the offset to ticks so that it can be a subsecond adjustment. |
| 18 | +static uint32_t rtc_offset = 0; |
| 19 | + |
| 20 | +void common_hal_rtc_get_time(timeutils_struct_time_t *tm) { |
| 21 | + uint64_t ticks_s = port_get_raw_ticks(NULL) / 1024; |
| 22 | + timeutils_seconds_since_2000_to_struct_time(rtc_offset + ticks_s, tm); |
| 23 | +} |
| 24 | + |
| 25 | +void common_hal_rtc_set_time(timeutils_struct_time_t *tm) { |
| 26 | + uint64_t ticks_s = port_get_raw_ticks(NULL) / 1024; |
| 27 | + uint32_t epoch_s = timeutils_seconds_since_2000( |
| 28 | + tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec |
| 29 | + ); |
| 30 | + rtc_offset = epoch_s - ticks_s; |
| 31 | +} |
| 32 | + |
| 33 | +// the calibration function will be implemented in near future |
| 34 | +// the RTC oscillator on my MAX32690-APARD is only off by 0.001 second |
| 35 | +// the inaccuracy is still tolerable |
| 36 | +int common_hal_rtc_get_calibration(void) { |
| 37 | + return 0; |
| 38 | +} |
| 39 | + |
| 40 | +void common_hal_rtc_set_calibration(int calibration) { |
| 41 | + mp_raise_NotImplementedError_varg(MP_ERROR_TEXT("%q"), MP_QSTR_calibration); |
| 42 | +} |
0 commit comments