Skip to content

Commit 8edc3aa

Browse files
robert-hhdpgeorge
authored andcommitted
mimxrt/modutime: Extend the time module.
Methods added: - time.time() - time.time_ns() - time.gmtime() - time.localtime() - time.mktime() The rp2 port was uses as the template for the change.
1 parent 7417c6a commit 8edc3aa

File tree

5 files changed

+92
-5
lines changed

5 files changed

+92
-5
lines changed

ports/mimxrt/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ SRC_C = \
150150
hal/flexspi_nor_flash.c \
151151
lib/mp-readline/readline.c \
152152
lib/libc/string0.c \
153+
lib/timeutils/timeutils.c \
153154
lib/utils/gchelper_native.c \
154155
lib/utils/mpirq.c \
155156
lib/utils/printf.c \

ports/mimxrt/machine_rtc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ uint32_t us_offset = 0;
4242
// Calculate the weekday from the date.
4343
// The result is zero based with 0 = Monday.
4444
// by Michael Keith and Tom Craver, 1990.
45-
static int calc_weekday(int y, int m, int d) {
45+
int calc_weekday(int y, int m, int d) {
4646
return ((d += m < 3 ? y-- : y - 2, 23 * m / 9 + d + 4 + y / 4 - y / 100 + y / 400) + 6) % 7;
4747
}
4848

ports/mimxrt/modutime.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,92 @@
2525
* THE SOFTWARE.
2626
*/
2727

28+
#include "py/runtime.h"
29+
#include "lib/timeutils/timeutils.h"
2830
#include "extmod/utime_mphal.h"
31+
#include "fsl_snvs_lp.h"
32+
33+
extern int calc_weekday(int y, int m, int d);
34+
35+
// localtime([secs])
36+
// Convert a time expressed in seconds since the Epoch into an 8-tuple which
37+
// contains: (year, month, mday, hour, minute, second, weekday, yearday)
38+
// If secs is not provided or None, then the current time from the RTC is used.
39+
STATIC mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) {
40+
if (n_args == 0 || args[0] == mp_const_none) {
41+
// Get current date and time.
42+
snvs_lp_srtc_datetime_t t;
43+
SNVS_LP_SRTC_GetDatetime(SNVS, &t);
44+
mp_obj_t tuple[8] = {
45+
mp_obj_new_int(t.year),
46+
mp_obj_new_int(t.month),
47+
mp_obj_new_int(t.day),
48+
mp_obj_new_int(t.hour),
49+
mp_obj_new_int(t.minute),
50+
mp_obj_new_int(t.second),
51+
mp_obj_new_int(calc_weekday(t.year, t.month, t.day)),
52+
mp_obj_new_int(timeutils_year_day(t.year, t.month, t.day)),
53+
};
54+
return mp_obj_new_tuple(8, tuple);
55+
} else {
56+
// Convert given seconds to tuple.
57+
mp_int_t seconds = mp_obj_get_int(args[0]);
58+
timeutils_struct_time_t tm;
59+
timeutils_seconds_since_epoch_to_struct_time(seconds, &tm);
60+
mp_obj_t tuple[8] = {
61+
tuple[0] = mp_obj_new_int(tm.tm_year),
62+
tuple[1] = mp_obj_new_int(tm.tm_mon),
63+
tuple[2] = mp_obj_new_int(tm.tm_mday),
64+
tuple[3] = mp_obj_new_int(tm.tm_hour),
65+
tuple[4] = mp_obj_new_int(tm.tm_min),
66+
tuple[5] = mp_obj_new_int(tm.tm_sec),
67+
tuple[6] = mp_obj_new_int(tm.tm_wday),
68+
tuple[7] = mp_obj_new_int(tm.tm_yday),
69+
};
70+
return mp_obj_new_tuple(8, tuple);
71+
}
72+
}
73+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(time_localtime_obj, 0, 1, time_localtime);
74+
75+
// mktime()
76+
// This is inverse function of localtime. It's argument is a full 8-tuple
77+
// which expresses a time as per localtime. It returns an integer which is
78+
// the number of seconds since the Epoch.
79+
STATIC mp_obj_t time_mktime(mp_obj_t tuple) {
80+
size_t len;
81+
mp_obj_t *elem;
82+
mp_obj_get_array(tuple, &len, &elem);
83+
84+
// localtime generates a tuple of len 8. CPython uses 9, so we accept both.
85+
if (len < 8 || len > 9) {
86+
mp_raise_TypeError(MP_ERROR_TEXT("mktime needs a tuple of length 8 or 9"));
87+
}
88+
89+
return mp_obj_new_int_from_uint(timeutils_mktime(mp_obj_get_int(elem[0]),
90+
mp_obj_get_int(elem[1]), mp_obj_get_int(elem[2]), mp_obj_get_int(elem[3]),
91+
mp_obj_get_int(elem[4]), mp_obj_get_int(elem[5])));
92+
}
93+
MP_DEFINE_CONST_FUN_OBJ_1(time_mktime_obj, time_mktime);
94+
95+
// time()
96+
// Return the number of seconds since the Epoch.
97+
STATIC mp_obj_t time_time(void) {
98+
snvs_lp_srtc_datetime_t t;
99+
SNVS_LP_SRTC_GetDatetime(SNVS, &t);
100+
return mp_obj_new_int_from_ull(timeutils_seconds_since_epoch(t.year, t.month, t.day, t.hour, t.minute, t.second));
101+
}
102+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time);
29103

30104
STATIC const mp_rom_map_elem_t time_module_globals_table[] = {
31105
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_utime) },
32106

107+
{ MP_ROM_QSTR(MP_QSTR_gmtime), MP_ROM_PTR(&time_localtime_obj) },
108+
{ MP_ROM_QSTR(MP_QSTR_localtime), MP_ROM_PTR(&time_localtime_obj) },
109+
{ MP_ROM_QSTR(MP_QSTR_mktime), MP_ROM_PTR(&time_mktime_obj) },
110+
111+
{ MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&time_time_obj) },
112+
{ MP_ROM_QSTR(MP_QSTR_time_ns), MP_ROM_PTR(&mp_utime_time_ns_obj) },
113+
33114
{ MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&mp_utime_sleep_obj) },
34115
{ MP_ROM_QSTR(MP_QSTR_sleep_ms), MP_ROM_PTR(&mp_utime_sleep_ms_obj) },
35116
{ MP_ROM_QSTR(MP_QSTR_sleep_us), MP_ROM_PTR(&mp_utime_sleep_us_obj) },

ports/mimxrt/mphalport.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@
2828
#include "py/runtime.h"
2929
#include "py/stream.h"
3030
#include "py/mphal.h"
31+
#include "lib/timeutils/timeutils.h"
3132
#include "ticks.h"
3233
#include "tusb.h"
34+
#include "fsl_snvs_lp.h"
3335

3436
#include CPU_HEADER_H
3537

@@ -94,3 +96,10 @@ void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
9496
// USARTx->USART.DATA.bit.DATA = *str++;
9597
// }
9698
}
99+
100+
uint64_t mp_hal_time_ns(void) {
101+
snvs_lp_srtc_datetime_t t;
102+
SNVS_LP_SRTC_GetDatetime(SNVS, &t);
103+
uint64_t s = timeutils_seconds_since_epoch(t.year, t.month, t.day, t.hour, t.minute, t.second);
104+
return s * 1000000000ULL;
105+
}

ports/mimxrt/mphalport.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,5 @@ static inline mp_uint_t mp_hal_ticks_cpu(void) {
7676
return 0;
7777
}
7878

79-
static inline uint64_t mp_hal_time_ns(void) {
80-
// TODO: Implement this function.
81-
return 0UL;
82-
}
8379

8480
#endif // MICROPY_INCLUDED_MIMXRT_MPHALPORT_H

0 commit comments

Comments
 (0)