Skip to content

Commit f4e4599

Browse files
robert-hhdpgeorge
authored andcommitted
ports: Fix machine.RTC.init() method so argument order matches the docs.
This commit makes the argument ordering of `machine.RTC.init()` the same for all the ports that implement arguments to this method: cc3200, esp32, mimxrt and samd. The cc3200 argument ordering is used, which matches the documentation. Also document the availability and the differing semantics for the stm32 and renesas-ra port. Signed-off-by: robert-hh <[email protected]>
1 parent 0302cd6 commit f4e4599

File tree

4 files changed

+31
-17
lines changed

4 files changed

+31
-17
lines changed

docs/library/machine.RTC.rst

+8-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,14 @@ Methods
4242

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

45-
``(year, month, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]])``
45+
``(year, month, day, hour, minute, second, microsecond, tzinfo)``
46+
47+
All eight arguments must be present. The ``microsecond`` and ``tzinfo``
48+
values are currently ignored but might be used in the future.
49+
50+
Availability: CC3200, ESP32, MIMXRT, SAMD. The rtc.init() method on
51+
the stm32 and renesas-ra ports just (re-)starts the RTC and does not
52+
accept arguments.
4653

4754
.. method:: RTC.now()
4855

ports/esp32/machine_rtc.c

+11-4
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ static mp_obj_t machine_rtc_make_new(const mp_obj_type_t *type, size_t n_args, s
101101
return (mp_obj_t)&machine_rtc_obj;
102102
}
103103

104-
static mp_obj_t machine_rtc_datetime_helper(mp_uint_t n_args, const mp_obj_t *args) {
104+
static mp_obj_t machine_rtc_datetime_helper(mp_uint_t n_args, const mp_obj_t *args, int hour_index) {
105105
if (n_args == 1) {
106106
// Get time
107107

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

133133
struct timeval tv = {0};
134-
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]));
134+
tv.tv_sec = timeutils_seconds_since_epoch(
135+
mp_obj_get_int(items[0]),
136+
mp_obj_get_int(items[1]),
137+
mp_obj_get_int(items[2]),
138+
mp_obj_get_int(items[hour_index]),
139+
mp_obj_get_int(items[hour_index + 1]),
140+
mp_obj_get_int(items[hour_index + 2])
141+
);
135142
tv.tv_usec = mp_obj_get_int(items[7]);
136143
settimeofday(&tv, NULL);
137144

138145
return mp_const_none;
139146
}
140147
}
141148
static mp_obj_t machine_rtc_datetime(size_t n_args, const mp_obj_t *args) {
142-
return machine_rtc_datetime_helper(n_args, args);
149+
return machine_rtc_datetime_helper(n_args, args, 4);
143150
}
144151
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_rtc_datetime_obj, 1, 2, machine_rtc_datetime);
145152

146153
static mp_obj_t machine_rtc_init(mp_obj_t self_in, mp_obj_t date) {
147154
mp_obj_t args[2] = {self_in, date};
148-
machine_rtc_datetime_helper(2, args);
155+
machine_rtc_datetime_helper(2, args, 3);
149156

150157
return mp_const_none;
151158
}

ports/mimxrt/machine_rtc.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ static mp_obj_t machine_rtc_make_new(const mp_obj_type_t *type, size_t n_args, s
185185
return (mp_obj_t)&machine_rtc_obj;
186186
}
187187

188-
static mp_obj_t machine_rtc_datetime_helper(size_t n_args, const mp_obj_t *args) {
188+
static mp_obj_t machine_rtc_datetime_helper(size_t n_args, const mp_obj_t *args, int hour_index) {
189189
if (n_args == 1) {
190190
// Get date and time.
191191
snvs_lp_srtc_datetime_t srtc_date;
@@ -214,9 +214,9 @@ static mp_obj_t machine_rtc_datetime_helper(size_t n_args, const mp_obj_t *args)
214214
srtc_date.month = mp_obj_get_int(items[1]);
215215
srtc_date.day = mp_obj_get_int(items[2]);
216216
// Ignore weekday at items[3]
217-
srtc_date.hour = mp_obj_get_int(items[4]);
218-
srtc_date.minute = mp_obj_get_int(items[5]);
219-
srtc_date.second = mp_obj_get_int(items[6]);
217+
srtc_date.hour = mp_obj_get_int(items[hour_index]);
218+
srtc_date.minute = mp_obj_get_int(items[hour_index + 1]);
219+
srtc_date.second = mp_obj_get_int(items[hour_index + 2]);
220220
if (SNVS_LP_SRTC_SetDatetime(SNVS, &srtc_date) != kStatus_Success) {
221221
mp_raise_ValueError(NULL);
222222
}
@@ -226,13 +226,13 @@ static mp_obj_t machine_rtc_datetime_helper(size_t n_args, const mp_obj_t *args)
226226
}
227227

228228
static mp_obj_t machine_rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) {
229-
return machine_rtc_datetime_helper(n_args, args);
229+
return machine_rtc_datetime_helper(n_args, args, 4);
230230
}
231231
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_rtc_datetime_obj, 1, 2, machine_rtc_datetime);
232232

233233
static mp_obj_t machine_rtc_init(mp_obj_t self_in, mp_obj_t date) {
234234
mp_obj_t args[2] = {self_in, date};
235-
machine_rtc_datetime_helper(2, args);
235+
machine_rtc_datetime_helper(2, args, 3);
236236
return mp_const_none;
237237
}
238238
static MP_DEFINE_CONST_FUN_OBJ_2(machine_rtc_init_obj, machine_rtc_init);

ports/samd/machine_rtc.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ static mp_obj_t machine_rtc_make_new(const mp_obj_type_t *type, size_t n_args, s
9393
return (mp_obj_t)&machine_rtc_obj;
9494
}
9595

96-
static mp_obj_t machine_rtc_datetime_helper(size_t n_args, const mp_obj_t *args) {
96+
static mp_obj_t machine_rtc_datetime_helper(size_t n_args, const mp_obj_t *args, int hour_index) {
9797
// Rtc *rtc = RTC;
9898
if (n_args == 1) {
9999
// Get date and time.
@@ -120,9 +120,9 @@ static mp_obj_t machine_rtc_datetime_helper(size_t n_args, const mp_obj_t *args)
120120
RTC_MODE2_CLOCK_YEAR(mp_obj_get_int(items[0]) % 100) |
121121
RTC_MODE2_CLOCK_MONTH(mp_obj_get_int(items[1])) |
122122
RTC_MODE2_CLOCK_DAY(mp_obj_get_int(items[2])) |
123-
RTC_MODE2_CLOCK_HOUR(mp_obj_get_int(items[4])) |
124-
RTC_MODE2_CLOCK_MINUTE(mp_obj_get_int(items[5])) |
125-
RTC_MODE2_CLOCK_SECOND(mp_obj_get_int(items[6]));
123+
RTC_MODE2_CLOCK_HOUR(mp_obj_get_int(items[hour_index])) |
124+
RTC_MODE2_CLOCK_MINUTE(mp_obj_get_int(items[hour_index + 1])) |
125+
RTC_MODE2_CLOCK_SECOND(mp_obj_get_int(items[hour_index + 2]));
126126

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

140140
static mp_obj_t machine_rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) {
141-
return machine_rtc_datetime_helper(n_args, args);
141+
return machine_rtc_datetime_helper(n_args, args, 4);
142142
}
143143
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_rtc_datetime_obj, 1, 2, machine_rtc_datetime);
144144

145145
static mp_obj_t machine_rtc_init(mp_obj_t self_in, mp_obj_t date) {
146146
mp_obj_t args[2] = {self_in, date};
147-
machine_rtc_datetime_helper(2, args);
147+
machine_rtc_datetime_helper(2, args, 3);
148148
return mp_const_none;
149149
}
150150
static MP_DEFINE_CONST_FUN_OBJ_2(machine_rtc_init_obj, machine_rtc_init);

0 commit comments

Comments
 (0)