8000 ports/RTC: Attempt to reduce the inconsistencies between the port's RTC implementation. by robert-hh · Pull Request #10607 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

ports/RTC: Attempt to reduce the inconsistencies between the port's RTC implementation. #10607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 19, 2024
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
4 changes: 3 additions & 1 deletion docs/esp32/quickref.rst
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,9 @@ See :ref:`machine.RTC <machine.RTC>` ::
from machine import RTC

rtc = RTC()
rtc.datetime((2017, 8, 23, 1, 12, 48, 0, 0)) # set a specific date and time
rtc.datetime((2017, 8, 23, 0, 1, 12, 48, 0)) # set a specific date and
# time, eg. 2017/8/23 1:12:48
# the day-of-week value is ignored
rtc.datetime() # get date and time

WDT (Watchdog timer)
Expand Down
4 changes: 3 additions & 1 deletion docs/esp8266/quickref.rst
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,9 @@ See :ref:`machine.RTC <machine.RTC>` ::
from machine import RTC

rtc = RTC()
rtc.datetime((2017, 8, 23, 1, 12, 48, 0, 0)) # set a specific date and time
rtc.datetime((2017, 8, 23, 0, 1, 12, 48, 0)) # set a specific date and
# time, eg. 2017/8/23 1:12:48
# the day-of-week value is ignored
rtc.datetime() # get date and time

# synchronize with ntp
Expand Down
11 changes: 10 additions & 1 deletion docs/library/machine.RTC.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,21 @@ Methods

Initialise the RTC. Datetime is a tuple of the form:

``(year, month, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]])``
``(year, month, day, hour, minute, second, microsecond, tzinfo)``

All eight arguments must be present. The ``microsecond`` and ``tzinfo``
values are currently ignored but might be used in the future.

Availability: CC3200, ESP32, MIMXRT, SAMD. The rtc.init() method on
the stm32 and renesas-ra ports just (re-)starts the RTC and does not
accept arguments.

.. method:: RTC.now()

Get get the current datetime tuple.

Availability: WiPy.

.. method:: RTC.deinit()

Resets the RTC to the time of January 1, 2015 and starts running it again.
Expand Down
4 changes: 3 additions & 1 deletion docs/mimxrt/quickref.rst
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,9 @@ See :ref:`machine.RTC <machine.RTC>`::
from machine import RTC

rtc = RTC()
rtc.datetime((2017, 8, 23, 1, 12, 48, 0, 0)) # set a specific date and time
rtc.datetime((2017, 8, 23, 0, 1, 12, 48, 0)) # set a specific date and
# time, eg. 2017/8/23 1:12:48
# the day-of-week value is ignored
rtc.datetime() # get date and time
rtc.now() # return date and time in CPython format.

Expand Down
4 changes: 3 additions & 1 deletion docs/pyboard/quickref.rst
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ See :ref:`pyb.RTC <pyb.RTC>` ::
from pyb import RTC

rtc = RTC()
rtc.datetime((2017, 8, 23, 1, 12, 48, 0, 0)) # set a specific date and time
rtc.datetime((2017, 8, 23, 0, 1, 12, 48, 0)) # set a specific date and
# time, eg. 2017/8/23 1:12:48
# the day-of-week value is ignored
rtc.datetime() # get date and time

PWM (pulse width modulation)
Expand Down
5 changes: 3 additions & 2 deletions docs/renesas-ra/quickref.rst
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,9 @@ See :ref:`machine.RTC <machine.RTC>` ::
from machine import RTC

rtc = RTC()
rtc.datetime((2017, 8, 23, 1, 12, 48, 0, 0)) # set a specific date and time
# time, eg 2017/8/23 1:12:48
rtc.datetime((2017, 8, 23, 0, 1, 12, 48, 0)) # set a specific date and
# time, eg. 2017/8/23 1:12:48
# the day-of-week value is ignored
rtc.datetime() # get date and time

Following functions are not supported at the present::
Expand Down
3 changes: 2 additions & 1 deletion docs/rp2/quickref.rst
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,9 @@ See :ref:`machine.RTC <machine.RTC>` ::
from machine import RTC

rtc = RTC()
rtc.datetime((2017, 8, 23, 2, 12, 48, 0, 0)) # set a specific date and
rtc.datetime((2017, 8, 23, 0, 1, 12, 48, 0)) # set a specific date and
# time, eg. 2017/8/23 1:12:48
# the day-of-week value is ignored
rtc.datetime() # get date and time

WDT (Watchdog timer)
Expand Down
15 changes: 11 additions & 4 deletions ports/esp32/machine_rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ static mp_obj_t machine_rtc_make_new(const mp_obj_type_t *type, size_t n_args, s
return (mp_obj_t)&machine_rtc_obj;
}

static mp_obj_t machine_rtc_datetime_helper(mp_uint_t n_args, const mp_obj_t *args) {
static mp_obj_t machine_rtc_datetime_helper(mp_uint_t n_args, const mp_obj_t *args, int hour_index) {
if (n_args == 1) {
// Get time

Expand Down Expand Up @@ -131,21 +131,28 @@ static mp_obj_t machine_rtc_datetime_helper(mp_uint_t n_args, const mp_obj_t *ar
mp_obj_get_array_fixed_n(args[1], 8, &items);

struct timeval tv = {0};
tv.tv_sec = timeutils_seconds_since_epoch(mp_obj_get_int(items[0]), mp_obj_get_int(items[1]), mp_obj_get_int(items[2]), mp_obj_get_int(items[4]), mp_obj_get_int(items[5]), mp_obj_get_int(items[6]));
tv.tv_sec = timeutils_seconds_since_epoch(
mp_obj_get_int(items[0]),
mp_obj_get_int(items[1]),
mp_obj_get_int(items[2]),
mp_obj_get_int(items[hour_index]),
mp_obj_get_int(items[hour_index + 1]),
mp_obj_get_int(items[hour_index + 2])
);
tv.tv_usec = mp_obj_get_int(items[7]);
settimeofday(&tv, NULL);

return mp_const_none;
}
}
static mp_obj_t machine_rtc_datetime(size_t n_args, const mp_obj_t *args) {
return machine_rtc_datetime_helper(n_args, args);
return machine_rtc_datetime_helper(n_args, args, 4);
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_rtc_datetime_obj, 1, 2, machine_rtc_datetime);

static mp_obj_t machine_rtc_init(mp_obj_t self_in, mp_obj_t date) {
mp_obj_t args[2] = {self_in, date};
machine_rtc_datetime_helper(2, args);
machine_rtc_datetime_helper(2, args, 3);

return mp_const_none;
}
Expand Down
32 changes: 6 additions & 26 deletions ports/mimxrt/machine_rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ static mp_obj_t machine_rtc_make_new(const mp_obj_type_t *type, size_t n_args, s
return (mp_obj_t)&machine_rtc_obj;
}

static mp_obj_t machine_rtc_datetime_helper(size_t n_args, const mp_obj_t *args) {
static mp_obj_t machine_rtc_datetime_helper(size_t n_args, const mp_obj_t *args, int hour_index) {
if (n_args == 1) {
// Get date and time.
snvs_lp_srtc_datetime_t srtc_date;
Expand Down Expand Up @@ -214,9 +214,9 @@ static mp_obj_t machine_rtc_datetime_helper(size_t n_args, const mp_obj_t *args)
srtc_date.month = mp_obj_get_int(items[1]);
srtc_date.day = mp_obj_get_int(items[2]);
// Ignore weekday at items[3]
srtc_date.hour = mp_obj_get_int(items[4]);
srtc_date.minute = mp_obj_get_int(items[5]);
srtc_date.second = mp_obj_get_int(items[6]);
srtc_date.hour = mp_obj_get_int(items[hour_index]);
srtc_date.minute = mp_obj_get_int(items[hour_index + 1]);
srtc_date.second = mp_obj_get_int(items[hour_index + 2]);
if (SNVS_LP_SRTC_SetDatetime(SNVS, &srtc_date) != kStatus_Success) {
mp_raise_ValueError(NULL);
}
Expand All @@ -226,32 +226,13 @@ static mp_obj_t machine_rtc_datetime_helper(size_t n_args, const mp_obj_t *args)
}

static mp_obj_t machine_rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) {
return machine_rtc_datetime_helper(n_args, args);
return machine_rtc_datetime_helper(n_args, args, 4);
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_rtc_datetime_obj, 1, 2, machine_rtc_datetime);

static mp_obj_t machine_rtc_now(mp_obj_t self_in) {
// Get date and time in CPython order.
snvs_lp_srtc_datetime_t srtc_date;
SNVS_LP_SRTC_GetDatetime(SNVS, &srtc_date);

mp_obj_t tuple[8] = {
mp_obj_new_int(srtc_date.year),
mp_obj_new_int(srtc_date.month),
mp_obj_new_int(srtc_date.day),
mp_obj_new_int(srtc_date.hour),
mp_obj_new_int(srtc_date.minute),
mp_obj_new_int(srtc_date.second),
mp_obj_new_int(0),
mp_const_none,
};
return mp_obj_new_tuple(8, tuple);
}
static MP_DEFINE_CONST_FUN_OBJ_1(machine_rtc_now_obj, machine_rtc_now);

static mp_obj_t machine_rtc_init(mp_obj_t self_in, mp_obj_t date) {
mp_obj_t args[2] = {self_in, date};
machine_rtc_datetime_helper(2, args);
machine_rtc_datetime_helper(2, args, 3);
return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_2(machine_rtc_init_obj, machine_rtc_init);
Expand Down Expand Up @@ -389,7 +370,6 @@ static MP_DEFINE_CONST_FUN_OBJ_KW(machine_rtc_irq_obj, 1, machine_rtc_irq);
static const mp_rom_map_elem_t machine_rtc_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_rtc_init_obj) },
{ MP_ROM_QSTR(MP_QSTR_datetime), MP_ROM_PTR(&machine_rtc_datetime_obj) },
{ MP_ROM_QSTR(MP_QSTR_now), MP_ROM_PTR(&machine_rtc_now_obj) },
{ MP_ROM_QSTR(MP_QSTR_calibration), MP_ROM_PTR(&machine_rtc_calibration_obj) },
{ MP_ROM_QSTR(MP_QSTR_alarm), MP_ROM_PTR(&machine_rtc_alarm_obj) },
{ MP_ROM_QSTR(MP_QSTR_alarm_left), MP_ROM_PTR(&machine_rtc_alarm_left_obj) },
Expand Down
12 changes: 6 additions & 6 deletions ports/samd/machine_rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ static mp_obj_t machine_rtc_make_new(const mp_obj_type_t *type, size_t n_args, s
return (mp_obj_t)&machine_rtc_obj;
}

static mp_obj_t machine_rtc_datetime_helper(size_t n_args, const mp_obj_t *args) {
static mp_obj_t machine_rtc_datetime_helper(size_t n_args, const mp_obj_t *args, int hour_index) {
// Rtc *rtc = RTC;
if (n_args == 1) {
// Get date and time.
Expand All @@ -120,9 +120,9 @@ static mp_obj_t machine_rtc_datetime_helper(size_t n_args, const mp_obj_t *args)
RTC_MODE2_CLOCK_YEAR(mp_obj_get_int(items[0]) % 100) |
RTC_MODE2_CLOCK_MONTH(mp_obj_get_int(items[1])) |
RTC_MODE2_CLOCK_DAY(mp_obj_get_int(items[2])) |
RTC_MODE2_CLOCK_HOUR(mp_obj_get_int(items[4])) |
RTC_MODE2_CLOCK_MINUTE(mp_obj_get_int(items[5])) |
RTC_MODE2_CLOCK_SECOND(mp_obj_get_int(items[6]));
RTC_MODE2_CLOCK_HOUR(mp_obj_get_int(items[hour_index])) |
RTC_MODE2_CLOCK_MINUTE(mp_obj_get_int(items[hour_index + 1])) |
RTC_MODE2_CLOCK_SECOND(mp_obj_get_int(items[hour_index + 2]));

RTC->MODE2.CLOCK.reg = date;
#if defined(MCU_SAMD21)
Expand All @@ -138,13 +138,13 @@ static mp_obj_t machine_rtc_datetime_helper(size_t n_args, const mp_obj_t *args)
}

static mp_obj_t machine_rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) {
return machine_rtc_datetime_helper(n_args, args);
return machine_rtc_datetime_helper(n_args, args, 4);
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_rtc_datetime_obj, 1, 2, machine_rtc_datetime);

static mp_obj_t machine_rtc_init(mp_obj_t self_in, mp_obj_t date) {
mp_obj_t args[2] = {self_in, date};
machine_rtc_datetime_helper(2, args);
machine_rtc_datetime_helper(2, args, 3);
return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_2(machine_rtc_init_obj, machine_rtc_init);
Expand Down
Loading
0