3
3
*
4
4
* The MIT License (MIT)
5
5
*
6
- * Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
7
- * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
8
6
* Copyright (c) 2019 Nick Moore for Adafruit Industries
9
- * Copyright (c) 2021 Junji Sakai
10
7
* Copyright (c) 2021 "Krzysztof Adamski" <k@japko.eu>
11
8
*
12
9
* Permission is hereby granted, free of charge, to any person obtaining a copy
36
33
#include "py/mphal.h"
37
34
#include "shared/timeutils/timeutils.h"
38
35
39
- // Circuitpython RTC.C
40
-
41
- #define RTC_OFFSET_CHECK_PREFIX 0x25ea7e2a
42
- #define RTC_OFFSET_CHECK_SUFFIX 0x2b80b69e
36
+ extern uint64_t ticks_ms_64 (void );
43
37
44
38
// This is the time in seconds since 2000 that the RTC was started.
45
39
__attribute__((section (".uninitialized" ))) static uint32_t rtc_offset [3 ];
46
40
41
+ // These values are placed before and after the current RTC count. They are
42
+ // used to determine if the RTC count is valid. These randomly-generated values
43
+ // will be set when the RTC value is set in order to mark the RTC as valid. If
44
+ // the system crashes or reboots, these values will remain undisturbed and the
45
+ // RTC offset will remain valid.
46
+ //
47
+ // If MicroPython is updated or these symbols shift around, the prefix and
48
+ // suffix will no longer match, and the time will no longer be valid.
49
+ #define RTC_OFFSET_CHECK_PREFIX 0x25ea7e2a
50
+ #define RTC_OFFSET_CHECK_SUFFIX 0x2b80b69e
51
+
52
+ void rtc_offset_check (void ) {
53
+ // If the prefix and suffix are not valid, zero-initialize the RTC offset.
54
+ if ((rtc_offset [0 ] != RTC_OFFSET_CHECK_PREFIX ) || (rtc_offset [2 ] != RTC_OFFSET_CHECK_SUFFIX )) {
55
+ rtc_offset [1 ] = 0 ;
56
+ }
57
+ }
47
58
48
59
void rtc_get_time (timeutils_struct_time_t * tm ) {
49
- // uint64_t ticks_s = port_get_raw_ticks(NULL) / 1024;
50
- uint64_t ticks_s = mp_hal_ticks_ms () / 999 ;
60
+ uint64_t ticks_s = ticks_ms_64 () / 1000 ;
51
61
timeutils_seconds_since_2000_to_struct_time (rtc_offset [1 ] + ticks_s , tm );
52
62
}
53
63
54
64
void rtc_set_time (timeutils_struct_time_t * tm ) {
55
- // uint64_t ticks_s = port_get_raw_ticks(NULL) / 1024;
56
- uint64_t ticks_s = mp_hal_ticks_ms () / 999 ;
65
+ uint64_t ticks_s = ticks_ms_64 () / 1000 ;
57
66
uint32_t epoch_s = timeutils_seconds_since_2000 (
58
67
tm -> tm_year , tm -> tm_mon , tm -> tm_mday , tm -> tm_hour , tm -> tm_min , tm -> tm_sec
59
68
);
@@ -66,8 +75,6 @@ void rtc_set_time(timeutils_struct_time_t *tm) {
66
75
rtc_offset [2 ] = RTC_OFFSET_CHECK_SUFFIX ;
67
76
}
68
77
69
- // end of Circuitpython
70
-
71
78
typedef struct _machine_rtc_obj_t {
72
79
mp_obj_base_t base ;
73
80
} machine_rtc_obj_t ;
@@ -78,29 +85,15 @@ STATIC const machine_rtc_obj_t machine_rtc_obj = {{&machine_rtc_type}};
78
85
STATIC 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 ) {
79
86
// check arguments
80
87
mp_arg_check_num (n_args , n_kw , 0 , 0 , false);
81
- // bool r = rtc_running();
82
-
83
- // if (!r) {
84
- // This shouldn't happen as rtc_init() is already called in main so
85
- // it's here just in case
86
- // rtc_init();
87
- // datetime_t t = { .month = 1, .day = 1 };
88
- // rtc_set_datetime(&t);
89
- // }
88
+
90
89
// return constant object
91
90
return (mp_obj_t )& machine_rtc_obj ;
92
91
}
93
92
94
93
STATIC mp_obj_t machine_rtc_datetime (mp_uint_t n_args , const mp_obj_t * args ) {
95
94
if (n_args == 1 ) {
96
- // bool ret;
97
- // datetime_t t;
98
95
timeutils_struct_time_t t ;
99
96
100
- // ret = rtc_get_datetime(&t);
101
- // if (!ret) {
102
- // mp_raise_OSError(MP_EIO);
103
- // }
104
97
rtc_get_time (& t );
105
98
106
99
mp_obj_t tuple [8 ] = {
@@ -131,9 +124,6 @@ STATIC mp_obj_t machine_rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) {
131
124
// Deliberately ignore the weekday argument and compute the proper value
132
125
t .tm_wday = timeutils_calc_weekday (t .tm_year , t .tm_mon , t .tm_mday );
133
126
134
- // if (!rtc_set_time(&t)) {
135
- // mp_raise_OSError(MP_EINVAL);
136
- // }
137
127
rtc_set_time (& t );
138
128
139
129
}
0 commit comments