Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions ports/analog/common-hal/rtc/RTC.c
Original file line number Diff line number Diff line change
@@ -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 <stdlib.h>

#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);
}
11 changes: 11 additions & 0 deletions ports/analog/common-hal/rtc/RTC.h
Original file line number Diff line number Diff line change
@@ -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);
6 changes: 6 additions & 0 deletions ports/analog/common-hal/rtc/__init__.c
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion ports/analog/mpconfigport.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down