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
3676STATIC 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-
4988MP_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+
51112STATIC 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
57120STATIC const mp_obj_dict_t time_module_globals = {
0 commit comments