3030#include <time.h>
3131#include <sys/time.h>
3232
33+ #include "pico/aon_timer.h"
34+
3335#include "py/nlr.h"
3436#include "py/obj.h"
3537#include "py/runtime.h"
3638#include "py/mphal.h"
3739#include "py/mperrno.h"
3840#include "extmod/modmachine.h"
3941#include "shared/timeutils/timeutils.h"
40- #include "hardware/rtc.h"
41- #include "pico/util/datetime.h"
4242
4343typedef struct _machine_rtc_obj_t {
4444 mp_obj_base_t base ;
@@ -50,14 +50,12 @@ static const machine_rtc_obj_t machine_rtc_obj = {{&machine_rtc_type}};
5050static mp_obj_t machine_rtc_make_new (const mp_obj_type_t * type , size_t n_args , size_t n_kw , const mp_obj_t * args ) {
5151 // check arguments
5252 mp_arg_check_num (n_args , n_kw , 0 , 0 , false);
53- bool r = rtc_running ();
53+ bool r = aon_timer_is_running ();
5454
5555 if (!r ) {
56- // This shouldn't happen as rtc_init() is already called in main so
57- // it's here just in case
58- rtc_init ();
59- datetime_t t = { .month = 1 , .day = 1 };
60- rtc_set_datetime (& t );
56+ // This shouldn't happen. it's here just in case
57+ struct timespec ts = { 0 , 0 };
58+ aon_timer_start (& ts );
6159 mp_hal_time_ns_set_from_rtc ();
6260 }
6361 // return constant object
@@ -66,47 +64,36 @@ static mp_obj_t machine_rtc_make_new(const mp_obj_type_t *type, size_t n_args, s
6664
6765static mp_obj_t machine_rtc_datetime (mp_uint_t n_args , const mp_obj_t * args ) {
6866 if (n_args == 1 ) {
69- bool ret ;
70- datetime_t t ;
71-
72- ret = rtc_get_datetime (& t );
73- if (!ret ) {
74- mp_raise_OSError (MP_EIO );
75- }
76-
67+ struct timespec ts ;
68+ timeutils_struct_time_t tm ;
69+ aon_timer_get_time (& ts );
70+ timeutils_seconds_since_epoch_to_struct_time (ts .tv_sec , & tm );
7771 mp_obj_t tuple [8 ] = {
78- mp_obj_new_int (t . year ),
79- mp_obj_new_int (t . month ),
80- mp_obj_new_int (t . day ),
81- mp_obj_new_int (t . dotw ),
82- mp_obj_new_int (t . hour ),
83- mp_obj_new_int (t . min ),
84- mp_obj_new_int (t . sec ),
85- mp_obj_new_int (0 )
72+ mp_obj_new_int (tm . tm_year ),
73+ mp_obj_new_int (tm . tm_mon ),
74+ mp_obj_new_int (tm . tm_mday ),
75+ mp_obj_new_int (tm . tm_wday ),
76+ mp_obj_new_int (tm . tm_hour ),
77+ mp_obj_new_int (tm . tm_min ),
78+ mp_obj_new_int (tm . tm_sec ),
79+ mp_obj_new_int (0 ),
8680 };
87-
8881 return mp_obj_new_tuple (8 , tuple );
8982 } else {
9083 mp_obj_t * items ;
91-
9284 mp_obj_get_array_fixed_n (args [1 ], 8 , & items );
93-
94- datetime_t t = {
95- .year = mp_obj_get_int (items [0 ]),
96- .month = mp_obj_get_int (items [1 ]),
97- .day = mp_obj_get_int (items [2 ]),
98- .hour = mp_obj_get_int (items [4 ]),
99- .min = mp_obj_get_int (items [5 ]),
100- .sec = mp_obj_get_int (items [6 ]),
85+ timeutils_struct_time_t tm = {
86+ .tm_year = mp_obj_get_int (items [0 ]),
87+ .tm_mon = mp_obj_get_int (items [1 ]),
88+ .tm_mday = mp_obj_get_int (items [2 ]),
89+ .tm_hour = mp_obj_get_int (items [4 ]),
90+ .tm_min = mp_obj_get_int (items [5 ]),
91+ .tm_sec = mp_obj_get_int (items [6 ]),
10192 };
102- // Deliberately ignore the weekday argument and compute the proper value
103- t .dotw = timeutils_calc_weekday (t .year , t .month , t .day );
104-
105- if (!rtc_set_datetime (& t )) {
106- mp_raise_OSError (MP_EINVAL );
107- }
93+ struct timespec ts = { 0 , 0 };
94+ ts .tv_sec = timeutils_seconds_since_epoch (tm .tm_year , tm .tm_mon , tm .tm_mday , tm .tm_hour , tm .tm_min , tm .tm_sec );
95+ aon_timer_set_time (& ts );
10896 mp_hal_time_ns_set_from_rtc ();
109-
11097 }
11198 return mp_const_none ;
11299}
0 commit comments