Skip to content

Commit d6cbbc5

Browse files
committed
stmhal: Add time.time() and time.localtime().
time.time: returns seconds since 1/1/2000, as an integer. time.localtime: Returns 8-tuple: (year, month, date, hour, minute, second, weekday, yearday).
1 parent 62b5f42 commit d6cbbc5

2 files changed

Lines changed: 66 additions & 2 deletions

File tree

stmhal/modtime.c

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,54 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#include <stm32f4xx_hal.h>
27+
#include "stm32f4xx_hal.h"
2828

2929
#include "mpconfig.h"
3030
#include "nlr.h"
3131
#include "misc.h"
3232
#include "qstr.h"
3333
#include "obj.h"
3434
#include "portmodules.h"
35+
#include "rtc.h"
36+
37+
STATIC const uint days_since_jan1[]= { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
38+
39+
STATIC bool is_leap_year(uint year) {
40+
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
41+
}
42+
43+
// compute the day of the year, between 1 and 366
44+
// month should be between 1 and 12, date should start at 1
45+
STATIC uint year_day(uint year, uint month, uint date) {
46+
uint yday = days_since_jan1[month - 1] + date;
47+
if (month >= 3 && is_leap_year(year)) {
48+
yday += 1;
49+
}
50+
return yday;
51+
}
52+
53+
// returns time stored in RTC as: (year, month, date, hour, minute, second, weekday)
54+
// weekday is 0-6 for Mon-Sun
55+
STATIC mp_obj_t time_localtime(void) {
56+
// get date and time
57+
// note: need to call get time then get date to correctly access the registers
58+
RTC_DateTypeDef date;
59+
RTC_TimeTypeDef time;
60+
HAL_RTC_GetTime(&RTCHandle, &time, FORMAT_BIN);
61+
HAL_RTC_GetDate(&RTCHandle, &date, FORMAT_BIN);
62+
mp_obj_t tuple[8] = {
63+
mp_obj_new_int(2000 + date.Year),
64+
mp_obj_new_int(date.Month),
65+
mp_obj_new_int(date.Date),
66+
mp_obj_new_int(time.Hours),
67+
mp_obj_new_int(time.Minutes),
68+
mp_obj_new_int(time.Seconds),
69+
mp_obj_new_int(date.WeekDay - 1),
70+
mp_obj_new_int(year_day(2000 + date.Year, date.Month, date.Date)),
71+
};
72+
return mp_obj_new_tuple(8, tuple);
73+
}
74+
MP_DEFINE_CONST_FUN_OBJ_0(time_localtime_obj, time_localtime);
3575

3676
STATIC mp_obj_t time_sleep(mp_obj_t seconds_o) {
3777
#if MICROPY_ENABLE_FLOAT
@@ -45,13 +85,36 @@ STATIC mp_obj_t time_sleep(mp_obj_t seconds_o) {
4585
#endif
4686
return mp_const_none;
4787
}
48-
4988
MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_obj, time_sleep);
5089

90+
// returns the number of seconds, as an integer, since 1/1/2000
91+
STATIC mp_obj_t time_time(void) {
92+
// get date and time
93+
// note: need to call get time then get date to correctly access the registers
94+
RTC_DateTypeDef date;
95+
RTC_TimeTypeDef time;
96+
HAL_RTC_GetTime(&RTCHandle, &time, FORMAT_BIN);
97+
HAL_RTC_GetDate(&RTCHandle, &date, FORMAT_BIN);
98+
return mp_obj_new_int(
99+
time.Seconds
100+
+ time.Minutes * 60
101+
+ time.Hours * 3600
102+
+ (year_day(2000 + date.Year, date.Month, date.Date) - 1
103+
+ ((date.Year + 3) / 4) // add a day each 4 years starting with 2001
104+
- ((date.Year + 99) / 100) // subtract a day each 100 years starting with 2001
105+
+ ((date.Year + 399) / 400) // add a day each 400 years starting with 2001
106+
) * 86400
107+
+ date.Year * 31536000
108+
);
109+
}
110+
MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time);
111+
51112
STATIC const mp_map_elem_t time_module_globals_table[] = {
52113
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_time) },
53114

115+
{ MP_OBJ_NEW_QSTR(MP_QSTR_localtime), (mp_obj_t)&time_localtime_obj },
54116
{ MP_OBJ_NEW_QSTR(MP_QSTR_sleep), (mp_obj_t)&time_sleep_obj },
117+
{ MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&time_time_obj },
55118
};
56119

57120
STATIC const mp_obj_dict_t time_module_globals = {

stmhal/qstrdefsport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ Q(urandom)
236236

237237
// for time module
238238
Q(time)
239+
Q(localtime)
239240
Q(sleep)
240241

241242
// for input

0 commit comments

Comments
 (0)