Skip to content

Conversation

PaggieZ
Copy link

@PaggieZ PaggieZ commented Aug 27, 2025

Summary

I added a port-specific module, RTC to ports/analog. Now the analog port can handle rtc.set_time_source(), rtc.RTC() and r.datetime(). Note that the RTC calibration functions (common_hal_rtc_get_calibration() and common_hal_rtc_set_calibration()) are not implemented and are planned for the 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 once per 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)

The expected output is:

struct_time(tm_year=2000, tm_mon=1, tm_mday=1, tm_hour=1, tm_min=1, tm_sec=1, tm_wday=5, tm_yday=1, tm_isdst=-1)
struct_time(tm_year=2000, tm_mon=1, tm_mday=1, tm_hour=1, tm_min=1, tm_sec=2, tm_wday=5, tm_yday=1, tm_isdst=-1)
struct_time(tm_year=2000, tm_mon=1, tm_mday=1, tm_hour=1, tm_min=1, tm_sec=3, tm_wday=5, tm_yday=1, tm_isdst=-1)
struct_time(tm_year=2000, tm_mon=1, tm_mday=1, tm_hour=1, tm_min=1, tm_sec=4, tm_wday=5, tm_yday=1, tm_isdst=-1)
struct_time(tm_year=2000, tm_mon=1, tm_mday=1, tm_hour=1, tm_min=1, tm_sec=5, tm_wday=5, tm_yday=1, tm_isdst=-1)
struct_time(tm_year=2000, tm_mon=1, tm_mday=1, tm_hour=1, tm_min=1, tm_sec=6, tm_wday=5, tm_yday=1, tm_isdst=-1)
struct_time(tm_year=2000, tm_mon=1, tm_mday=1, tm_hour=1, tm_min=1, tm_sec=7, tm_wday=5, tm_yday=1, tm_isdst=-1)

The following example call functions from the time module that rely on the RTC module. Successful execution confirms that the newly integrated RTC module enables these related functions in the time module.

import time
import rtc

r = rtc.RTC()
r.datetime = time.struct_time((2000, 1, 1, 1, 1, 1, 0, -1, -1))
rtc.set_time_source(r)

time.time() // should return the current time in seconds since Jan 1, 1970
time.local(time) // should return the current time

An example of the expected output is:

946688527
struct_time(tm_year=2000, tm_mon=1, tm_mday=1, tm_hour=1, tm_min=2, tm_sec=27, tm_wday=5, tm_yday=1, tm_isdst=-1)

Testing

After I finished implementing the module, I built and flashed the project in the ports/analog directory using OpenOCD with a J-Link device. After flashing firmware.elf to an AD-APARD32690-SL, I ran the code snippets in both code.py and REPL and received expected results.

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
@PaggieZ PaggieZ marked this pull request as ready for review August 28, 2025 16:34
Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@tannewt tannewt merged commit f09b42c into adafruit:main Aug 28, 2025
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants