Skip to content

Commit 996a1f9

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

File tree

4 files changed

+26
-104
lines changed

4 files changed

+26
-104
lines changed

ports/stm32/Makefile

-1
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,6 @@ SRC_C += \
337337
modmachine.c \
338338
modpyb.c \
339339
modstm.c \
340-
modutime.c \
341340
network_lan.c \
342341
extint.c \
343342
usrsw.c \

ports/stm32/modutime.c

+23-98
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2013, 2014 Damien P. George
6+
* Copyright (c) 2013-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,77 +24,34 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#include <stdio.h>
28-
#include <string.h>
29-
30-
#include "py/runtime.h"
31-
#include "py/smallint.h"
3227
#include "py/obj.h"
3328
#include "shared/timeutils/timeutils.h"
34-
#include "extmod/modutime.h"
35-
#include "systick.h"
36-
#include "portmodules.h"
3729
#include "rtc.h"
3830

39-
/// \module time - time related functions
40-
///
41-
/// The `time` module provides functions for getting the current time and date,
42-
/// and for sleeping.
43-
44-
/// \function localtime([secs])
45-
/// Convert a time expressed in seconds since Jan 1, 2000 into an 8-tuple which
46-
/// contains: (year, month, mday, hour, minute, second, weekday, yearday)
47-
/// If secs is not provided or None, then the current time from the RTC is used.
48-
/// year includes the century (for example 2014)
49-
/// month is 1-12
50-
/// mday is 1-31
51-
/// hour is 0-23
52-
/// minute is 0-59
53-
/// second is 0-59
54-
/// weekday is 0-6 for Mon-Sun.
55-
/// yearday is 1-366
56-
STATIC mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) {
57-
if (n_args == 0 || args[0] == mp_const_none) {
58-
// get current date and time
59-
// note: need to call get time then get date to correctly access the registers
60-
rtc_init_finalise();
61-
RTC_DateTypeDef date;
62-
RTC_TimeTypeDef time;
63-
HAL_RTC_GetTime(&RTCHandle, &time, RTC_FORMAT_BIN);
64-
HAL_RTC_GetDate(&RTCHandle, &date, RTC_FORMAT_BIN);
65-
mp_obj_t tuple[8] = {
66-
mp_obj_new_int(2000 + date.Year),
67-
mp_obj_new_int(date.Month),
68-
mp_obj_new_int(date.Date),
69-
mp_obj_new_int(time.Hours),
70-
mp_obj_new_int(time.Minutes),
71-
mp_obj_new_int(time.Seconds),
72-
mp_obj_new_int(date.WeekDay - 1),
73-
mp_obj_new_int(timeutils_year_day(2000 + date.Year, date.Month, date.Date)),
74-
};
75-
return mp_obj_new_tuple(8, tuple);
76-
} else {
77-
mp_int_t seconds = mp_obj_get_int(args[0]);
78-
timeutils_struct_time_t tm;
79-
timeutils_seconds_since_epoch_to_struct_time(seconds, &tm);
80-
mp_obj_t tuple[8] = {
81-
tuple[0] = mp_obj_new_int(tm.tm_year),
82-
tuple[1] = mp_obj_new_int(tm.tm_mon),
83-
tuple[2] = mp_obj_new_int(tm.tm_mday),
84-
tuple[3] = mp_obj_new_int(tm.tm_hour),
85-
tuple[4] = mp_obj_new_int(tm.tm_min),
86-
tuple[5] = mp_obj_new_int(tm.tm_sec),
87-
tuple[6] = mp_obj_new_int(tm.tm_wday),
88-
tuple[7] = mp_obj_new_int(tm.tm_yday),
89-
};
90-
return mp_obj_new_tuple(8, tuple);
91-
}
31+
// Return the localtime as an 8-tuple.
32+
STATIC mp_obj_t mp_utime_localtime_get(void) {
33+
// get current date and time
34+
// note: need to call get time then get date to correctly access the registers
35+
rtc_init_finalise();
36+
RTC_DateTypeDef date;
37+
RTC_TimeTypeDef time;
38+
HAL_RTC_GetTime(&RTCHandle, &time, RTC_FORMAT_BIN);
39+
HAL_RTC_GetDate(&RTCHandle, &date, RTC_FORMAT_BIN);
40+
mp_obj_t tuple[8] = {
41+
mp_obj_new_int(2000 + date.Year),
42+
mp_obj_new_int(date.Month),
43+
mp_obj_new_int(date.Date),
44+
mp_obj_new_int(time.Hours),
45+
mp_obj_new_int(time.Minutes),
46+
mp_obj_new_int(time.Seconds),
47+
mp_obj_new_int(date.WeekDay - 1),
48+
mp_obj_new_int(timeutils_year_day(2000 + date.Year, date.Month, date.Date)),
49+
};
50+
return mp_obj_new_tuple(8, tuple);
9251
}
93-
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(time_localtime_obj, 0, 1, time_localtime);
9452

95-
/// \function time()
96-
/// Returns the number of seconds, as an integer, since 1/1/2000.
97-
STATIC mp_obj_t time_time(void) {
53+
// Returns the number of seconds, as an integer, since 1/1/2000.
54+
STATIC mp_obj_t mp_utime_time_get(void) {
9855
// get date and time
9956
// note: need to call get time then get date to correctly access the registers
10057
rtc_init_finalise();
@@ -104,35 +61,3 @@ STATIC mp_obj_t time_time(void) {
10461
HAL_RTC_GetDate(&RTCHandle, &date, RTC_FORMAT_BIN);
10562
return mp_obj_new_int(timeutils_seconds_since_epoch(2000 + date.Year, date.Month, date.Date, time.Hours, time.Minutes, time.Seconds));
10663
}
107-
MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time);
108-
109-
#if MICROPY_PY_UTIME
110-
111-
STATIC const mp_rom_map_elem_t time_module_globals_table[] = {
112-
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_utime) },
113-
114-
{ MP_ROM_QSTR(MP_QSTR_gmtime), MP_ROM_PTR(&time_localtime_obj) },
115-
{ MP_ROM_QSTR(MP_QSTR_localtime), MP_ROM_PTR(&time_localtime_obj) },
116-
{ MP_ROM_QSTR(MP_QSTR_mktime), MP_ROM_PTR(&mp_utime_mktime_obj) },
117-
{ MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&time_time_obj) },
118-
{ MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&mp_utime_sleep_obj) },
119-
{ MP_ROM_QSTR(MP_QSTR_sleep_ms), MP_ROM_PTR(&mp_utime_sleep_ms_obj) },
120-
{ MP_ROM_QSTR(MP_QSTR_sleep_us), MP_ROM_PTR(&mp_utime_sleep_us_obj) },
121-
{ MP_ROM_QSTR(MP_QSTR_ticks_ms), MP_ROM_PTR(&mp_utime_ticks_ms_obj) },
122-
{ MP_ROM_QSTR(MP_QSTR_ticks_us), MP_ROM_PTR(&mp_utime_ticks_us_obj) },
123-
{ MP_ROM_QSTR(MP_QSTR_ticks_cpu), MP_ROM_PTR(&mp_utime_ticks_cpu_obj) },
124-
{ MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_utime_ticks_add_obj) },
125-
{ MP_ROM_QSTR(MP_QSTR_ticks_diff), MP_ROM_PTR(&mp_utime_ticks_diff_obj) },
126-
{ MP_ROM_QSTR(MP_QSTR_time_ns), MP_ROM_PTR(&mp_utime_time_ns_obj) },
127-
};
128-
129-
STATIC MP_DEFINE_CONST_DICT(time_module_globals, time_module_globals_table);
130-
131-
const mp_obj_module_t mp_module_utime = {
132-
.base = { &mp_type_module },
133-
.globals = (mp_obj_dict_t *)&time_module_globals,
134-
};
135-
136-
MP_REGISTER_MODULE(MP_QSTR_utime, mp_module_utime);
137-
138-
#endif // MICROPY_PY_UTIME

ports/stm32/mpconfigport.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,9 @@
105105
#define MICROPY_PY_UOS_UNAME (1)
106106
#define MICROPY_PY_UOS_URANDOM (MICROPY_HW_ENABLE_RNG)
107107
#define MICROPY_PY_URANDOM_SEED_INIT_FUNC (rng_get())
108-
#ifndef MICROPY_PY_UTIME
109-
#define MICROPY_PY_UTIME (1)
110-
#endif
111-
#define MICROPY_PY_UTIME_MP_HAL (MICROPY_PY_UTIME)
108+
#define MICROPY_PY_UTIME_GMTIME_LOCALTIME_MKTIME (1)
109+
#define MICROPY_PY_UTIME_TIME_TIME_NS (1)
110+
#define MICROPY_PY_UTIME_INCLUDEFILE "ports/stm32/modutime.c"
112111
#ifndef MICROPY_PY_UTIMEQ
113112
#define MICROPY_PY_UTIMEQ (1)
114113
#endif

ports/stm32/portmodules.h

-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828

2929
extern const mp_obj_module_t pyb_module;
3030
extern const mp_obj_module_t stm_module;
31-
extern const mp_obj_module_t mp_module_utime;
3231
extern const mp_obj_module_t mp_module_usocket;
3332

3433
// additional helper functions exported by the modules

0 commit comments

Comments
 (0)