2525 */
2626
2727#include "objrtc.h"
28+ #include "rtl8721d_rtc.h"
2829
2930// singleton RTC object
3031STATIC rtc_obj_t rtc_obj = {
@@ -49,13 +50,27 @@ STATIC mp_obj_t rtc_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp_uin
4950}
5051
5152STATIC mp_obj_t rtc_datetime (mp_uint_t n_args , const mp_obj_t * args ) {
53+ static uint32_t seconds_ymd_backup = 0 ;
54+ RTC_TimeTypeDef RTC_TimeStruct ;
55+
5256 if (n_args == 1 ) {
53- // Get time
54- clock_t secs = rtc_read ();
55- // (Since RTL8195A use 1970 for it's start yaer, so I need to add the seconds of 30 years)
56- secs -= SECS_IN_30YEARS ;
57+ // RTL872x RTC can only count up to 512 days
58+ // Get updated time and reset RTC day counter to 0
59+ RTC_GetTime (RTC_Format_BIN , & RTC_TimeStruct );
60+ int days = RTC_TimeStruct .RTC_Days ;
61+ RTC_TimeStruct .RTC_Days = 0 ;
62+ RTC_SetTime (RTC_Format_BIN , & RTC_TimeStruct );
63+ int hours = RTC_TimeStruct .RTC_Hours ;
64+ int mins = RTC_TimeStruct .RTC_Minutes ;
65+ int secs = RTC_TimeStruct .RTC_Seconds ;
66+
67+ clock_t seconds_elapsed = days * NUM_SECS_IN_DAY ;
68+ clock_t total_seconds = seconds_elapsed + seconds_ymd_backup + hours * 3600 + mins * 60 + secs ;
5769 timeutils_struct_time_t tm ;
58- timeutils_seconds_since_2000_to_struct_time (secs , & tm );
70+ timeutils_seconds_since_2000_to_struct_time (total_seconds , & tm );
71+
72+ // Update backup copy of year month date in seconds
73+ seconds_ymd_backup += seconds_elapsed ;
5974
6075 mp_obj_t tuple [8 ] = {
6176 mp_obj_new_int (tm .tm_year ),
@@ -65,7 +80,7 @@ STATIC mp_obj_t rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) {
6580 mp_obj_new_int (tm .tm_hour ),
6681 mp_obj_new_int (tm .tm_min ),
6782 mp_obj_new_int (tm .tm_sec ),
68- mp_obj_new_int (secs )
83+ mp_obj_new_int (total_seconds )
6984 };
7085
7186 return mp_obj_new_tuple (8 , tuple );
@@ -74,19 +89,27 @@ STATIC mp_obj_t rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) {
7489 mp_obj_t * items ;
7590 mp_obj_get_array_fixed_n (args [1 ], 8 , & items );
7691
77- rtc_write (
78- ((clock_t )timeutils_seconds_since_2000 (
79- mp_obj_get_int (items [0 ]),
80- mp_obj_get_int (items [1 ]),
81- mp_obj_get_int (items [2 ]),
82- mp_obj_get_int (items [4 ]),
83- mp_obj_get_int (items [5 ]),
84- mp_obj_get_int (items [6 ]) + SECS_IN_30YEARS
85- )));
92+ uint32_t year = mp_obj_get_int (items [0 ]);
93+ uint32_t month = mp_obj_get_int (items [1 ]);
94+ uint32_t day = mp_obj_get_int (items [2 ]);
95+ uint32_t hour = mp_obj_get_int (items [4 ]);
96+ uint32_t mins = mp_obj_get_int (items [5 ]);
97+ uint32_t secs = mp_obj_get_int (items [6 ]);
98+
99+ seconds_ymd_backup = ((clock_t )timeutils_seconds_since_2000 (year , month , day , 0 , 0 , 0 ));
100+
101+ // RTL872x RTC can only count up to 512 days, so use it to only count day offsets
102+ RTC_TimeStruct .RTC_H12_PMAM = RTC_H12_AM ;
103+ RTC_TimeStruct .RTC_Days = 0 ;
104+ RTC_TimeStruct .RTC_Hours = hour ;
105+ RTC_TimeStruct .RTC_Minutes = mins ;
106+ RTC_TimeStruct .RTC_Seconds = secs ;
107+ RTC_SetTime (RTC_Format_BIN , & RTC_TimeStruct );
86108
87109 return mp_const_none ;
88110 }
89111}
112+
90113STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (rtc_datetime_obj , 1 , 2 , rtc_datetime );
91114
92115STATIC const mp_map_elem_t rtc_locals_dict_table [] = {
0 commit comments