Skip to content

Commit d7cb53c

Browse files
committed
samd/modutime: Use extmod version of time module.
No API or functional change. Signed-off-by: Damien George <[email protected]>
1 parent 67e9176 commit d7cb53c

File tree

4 files changed

+18
-48
lines changed

4 files changed

+18
-48
lines changed

ports/samd/Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ SRC_C += \
103103
machine_uart.c \
104104
machine_wdt.c \
105105
main.c \
106-
modutime.c \
107106
modmachine.c \
108107
modsamd.c \
109108
mphalport.c \

ports/samd/mcu/samd21/mpconfigmcu.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
#define MICROPY_PY_BUILTINS_COMPLEX (0)
1515
#endif
1616

17+
#ifndef MICROPY_PY_UTIME
18+
#define MICROPY_PY_UTIME (1)
19+
#endif
20+
1721
#ifndef MICROPY_PY_MATH
1822
#define MICROPY_PY_MATH (1)
1923
#endif

ports/samd/modutime.c

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2019 Damien P. George
6+
* Copyright (c) 2019-2023 Damien P. George
77
*
88
* Permission is hereby granted, free of charge, to any person obtaining a copy
99
* of this software and associated documentation files (the "Software"), to deal
@@ -24,68 +24,33 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#include "py/runtime.h"
28-
#include "extmod/modutime.h"
27+
#include "py/obj.h"
2928
#include "shared/timeutils/timeutils.h"
3029
#include "modmachine.h"
3130

32-
// localtime([secs])
33-
STATIC mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) {
31+
// Return the localtime as an 8-tuple.
32+
STATIC mp_obj_t mp_utime_localtime_get(void) {
3433
timeutils_struct_time_t tm;
35-
mp_int_t seconds;
36-
37-
if (n_args == 0 || args[0] == mp_const_none) {
38-
rtc_gettime(&tm);
39-
} else {
40-
seconds = mp_obj_get_int(args[0]);
41-
timeutils_seconds_since_epoch_to_struct_time(seconds, &tm);
42-
}
43-
34+
rtc_gettime(&tm);
35+
tm.tm_wday = timeutils_calc_weekday(tm.tm_year, tm.tm_mon, tm.tm_mday);
36+
tm.tm_yday = timeutils_year_day(tm.tm_year, tm.tm_mon, tm.tm_mday);
4437
mp_obj_t tuple[8] = {
4538
tuple[0] = mp_obj_new_int(tm.tm_year),
4639
tuple[1] = mp_obj_new_int(tm.tm_mon),
4740
tuple[2] = mp_obj_new_int(tm.tm_mday),
4841
tuple[3] = mp_obj_new_int(tm.tm_hour),
4942
tuple[4] = mp_obj_new_int(tm.tm_min),
5043
tuple[5] = mp_obj_new_int(tm.tm_sec),
51-
tuple[6] = mp_obj_new_int(timeutils_calc_weekday(tm.tm_year, tm.tm_mon, tm.tm_mday)),
52-
tuple[7] = mp_obj_new_int(timeutils_year_day(tm.tm_year, tm.tm_mon, tm.tm_mday)),
44+
tuple[6] = mp_obj_new_int(tm.tm_wday),
45+
tuple[7] = mp_obj_new_int(tm.tm_yday),
5346
};
5447
return mp_obj_new_tuple(8, tuple);
5548
}
56-
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(time_localtime_obj, 0, 1, time_localtime);
5749

58-
// time()
59-
STATIC mp_obj_t time_time(void) {
50+
// Returns the number of seconds, as an integer, since the Epoch.
51+
STATIC mp_obj_t mp_utime_time_get(void) {
6052
timeutils_struct_time_t tm;
6153
rtc_gettime(&tm);
6254
return mp_obj_new_int_from_uint(timeutils_mktime(
6355
tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec));
6456
}
65-
STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time);
66-
67-
STATIC const mp_rom_map_elem_t time_module_globals_table[] = {
68-
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_utime) },
69-
70-
{ MP_ROM_QSTR(MP_QSTR_gmtime), MP_ROM_PTR(&time_localtime_obj) },
71-
{ MP_ROM_QSTR(MP_QSTR_localtime), MP_ROM_PTR(&time_localtime_obj) },
72-
{ MP_ROM_QSTR(MP_QSTR_mktime), MP_ROM_PTR(&mp_utime_mktime_obj) },
73-
{ MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&mp_utime_sleep_obj) },
74-
{ MP_ROM_QSTR(MP_QSTR_sleep_ms), MP_ROM_PTR(&mp_utime_sleep_ms_obj) },
75-
{ MP_ROM_QSTR(MP_QSTR_sleep_us), MP_ROM_PTR(&mp_utime_sleep_us_obj) },
76-
{ MP_ROM_QSTR(MP_QSTR_ticks_ms), MP_ROM_PTR(&mp_utime_ticks_ms_obj) },
77-
{ MP_ROM_QSTR(MP_QSTR_ticks_us), MP_ROM_PTR(&mp_utime_ticks_us_obj) },
78-
{ MP_ROM_QSTR(MP_QSTR_ticks_cpu), MP_ROM_PTR(&mp_utime_ticks_cpu_obj) },
79-
{ MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_utime_ticks_add_obj) },
80-
{ MP_ROM_QSTR(MP_QSTR_ticks_diff), MP_ROM_PTR(&mp_utime_ticks_diff_obj) },
81-
{ MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&time_time_obj) },
82-
};
83-
84-
STATIC MP_DEFINE_CONST_DICT(time_module_globals, time_module_globals_table);
85-
86-
const mp_obj_module_t mp_module_utime = {
87-
.base = { &mp_type_module },
88-
.globals = (mp_obj_dict_t *)&time_module_globals,
89-
};
90-
91-
MP_REGISTER_MODULE(MP_QSTR_utime, mp_module_utime);

ports/samd/mpconfigport.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@
7878
#define MICROPY_PY_IO_IOBASE (1)
7979

8080
// Extended modules
81-
#define MICROPY_PY_UTIME_MP_HAL (1)
81+
#define MICROPY_PY_UTIME_GMTIME_LOCALTIME_MKTIME (1)
82+
#define MICROPY_PY_UTIME_TIME_TIME_NS (1)
83+
#define MICROPY_PY_UTIME_INCLUDEFILE "ports/samd/modutime.c"
8284
#define MICROPY_PY_MACHINE (1)
8385
#define MICROPY_PY_UOS (1)
8486
#define MICROPY_PY_UOS_INCLUDEFILE "ports/samd/moduos.c"

0 commit comments

Comments
 (0)