From 57b25b5f3abf39e7ebbab97f4cc0cdbfa73c4d53 Mon Sep 17 00:00:00 2001 From: Peggy Date: Wed, 27 Aug 2025 14:07:34 -0700 Subject: [PATCH] Add RTC module to ports/analog for MAX32690_apard I added a port-specific module, RTC to ports/analog. Now the analog port is capable of handling `rtc.set_time_source()`, `rtc.RTC()` and `r.datetime()`. Note that the RTC calibration functions are not implemented and is planned for near future. The implementation in the RTC module also enables two functions in the time module, which are `time.time()` and `time.localtime()`. The following example prints the current time every second: import time import rtc r = rtc.RTC) r.datetime = time.struct_Time((2000, 1, 1, 1, 1, 1, 0, -1, -1)) while(True): current_time = r.datetime print(current_time) time.sleep(1) Files I modified: * ports/analog/common-hal/rtc/RTC.c * ports/analog/common-hal/rtc/RTC.h * ports/analog/common-hal/rtc/__init__.c * ports/analog/mpconfigport.mk --- ports/analog/common-hal/rtc/RTC.c | 42 ++++++++++++++++++++++++++ ports/analog/common-hal/rtc/RTC.h | 11 +++++++ ports/analog/common-hal/rtc/__init__.c | 6 ++++ ports/analog/mpconfigport.mk | 2 +- 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 ports/analog/common-hal/rtc/RTC.c create mode 100644 ports/analog/common-hal/rtc/RTC.h create mode 100644 ports/analog/common-hal/rtc/__init__.c diff --git a/ports/analog/common-hal/rtc/RTC.c b/ports/analog/common-hal/rtc/RTC.c new file mode 100644 index 0000000000000..c9d2258c1ece1 --- /dev/null +++ b/ports/analog/common-hal/rtc/RTC.c @@ -0,0 +1,42 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2019 Nick Moore for Adafruit Industries +// SPDX-FileCopyrightText: Copyright (c) 2019 Artur Pacholec +// SPDX-FileCopyrightText: Copyright (c) 2025 Peggy Zhu, Analog Devices, Inc. +// +// SPDX-License-Identifier: MIT + +#include + +#include "py/obj.h" +#include "py/runtime.h" +#include "shared/timeutils/timeutils.h" +#include "supervisor/port.h" + +// This is the time in seconds since 2000 that the RTC was started. +// TODO: Change the offset to ticks so that it can be a subsecond adjustment. +static uint32_t rtc_offset = 0; + +void common_hal_rtc_get_time(timeutils_struct_time_t *tm) { + uint64_t ticks_s = port_get_raw_ticks(NULL) / 1024; + timeutils_seconds_since_2000_to_struct_time(rtc_offset + ticks_s, tm); +} + +void common_hal_rtc_set_time(timeutils_struct_time_t *tm) { + uint64_t ticks_s = port_get_raw_ticks(NULL) / 1024; + uint32_t epoch_s = timeutils_seconds_since_2000( + tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec + ); + rtc_offset = epoch_s - ticks_s; +} + +// the calibration function will be implemented in near future +// the RTC oscillator on my MAX32690-APARD is only off by 0.001 second +// the inaccuracy is still tolerable +int common_hal_rtc_get_calibration(void) { + return 0; +} + +void common_hal_rtc_set_calibration(int calibration) { + mp_raise_NotImplementedError_varg(MP_ERROR_TEXT("%q"), MP_QSTR_calibration); +} diff --git a/ports/analog/common-hal/rtc/RTC.h b/ports/analog/common-hal/rtc/RTC.h new file mode 100644 index 0000000000000..590bc93ab5231 --- /dev/null +++ b/ports/analog/common-hal/rtc/RTC.h @@ -0,0 +1,11 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2018 Noralf Trønnes +// SPDX-FileCopyrightText: Copyright (c) 2019 Artur Pacholec +// SPDX-FileCopyrightText: Copyright (c) 2025 Peggy Zhu, Analog Devices, Inc. +// +// SPDX-License-Identifier: MIT + +#pragma once + +extern void rtc_reset(void); diff --git a/ports/analog/common-hal/rtc/__init__.c b/ports/analog/common-hal/rtc/__init__.c new file mode 100644 index 0000000000000..ddf788bb0b5ad --- /dev/null +++ b/ports/analog/common-hal/rtc/__init__.c @@ -0,0 +1,6 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2024 Adafruit Industries LLC +// SPDX-FileCopyrightText: Copyright (c) 2025 Peggy Zhu, Analog Devices, Inc. +// +// SPDX-License-Identifier: MIT diff --git a/ports/analog/mpconfigport.mk b/ports/analog/mpconfigport.mk index 67f1f79fb8121..97ead8efdfb0f 100644 --- a/ports/analog/mpconfigport.mk +++ b/ports/analog/mpconfigport.mk @@ -24,7 +24,7 @@ INTERNAL_FLASH_FILESYSTEM = 1 # Plan to implement CIRCUITPY_BUSIO ?= 0 -CIRCUITPY_RTC ?= 0 +CIRCUITPY_RTC ?= 1 # Other modules (may or may not implement): CIRCUITPY_ANALOGIO ?= 0