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
30
27
31
28
#include "py/runtime.h"
32
29
#include "modmachine.h"
30
+ #include "rtc.h"
33
31
34
32
#if MICROPY_PY_MACHINE_RTC
35
33
36
34
#include "py/mphal.h"
37
35
#include "shared/timeutils/timeutils.h"
38
36
39
- // Circuitpython RTC.C
37
+ // This is the time in seconds since 2000 that the RTC was started.
38
+ __attribute__((section (".uninitialized" ))) static uint32_t rtc_offset [3 ];
40
39
40
+ // These values are placed before and after the current RTC count. They are
41
+ // used to determine if the RTC count is valid. These randomly-generated values
42
+ // will be set when the RTC value is set in order to mark the RTC as valid. If
43
+ // the system crashes or reboots, these values will remain undisturbed and the
44
+ // RTC offset will remain valid.
45
+ //
46
+ // If MicroPython is updated or these symbols shift around, the prefix and
47
+ // suffix will no longer match, and the time will no longer be valid.
41
48
#define RTC_OFFSET_CHECK_PREFIX 0x25ea7e2a
42
49
#define RTC_OFFSET_CHECK_SUFFIX 0x2b80b69e
43
50
44
- // This is the time in seconds since 2000 that the RTC was started.
45
- __attribute__((section (".uninitialized" ))) static uint32_t rtc_offset [3 ];
46
-
51
+ void rtc_offset_check (void ) {
52
+ // If the prefix and suffix are not valid, zero-initialize the RTC offset.
53
+ if ((rtc_offset [0 ] != RTC_OFFSET_CHECK_PREFIX ) || (rtc_offset [2 ] != RTC_OFFSET_CHECK_SUFFIX )) {
54
+ rtc_offset [1 ] = 0 ;
55
+ }
56
+ }
47
57
48
58
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 ;
59
+ uint64_t ticks_s = ticks_ms_64 () / 1000 ;
51
60
timeutils_seconds_since_2000_to_struct_time (rtc_offset [1 ] + ticks_s , tm );
52
61
}
53
62
54
63
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 ;
64
+ uint64_t ticks_s = ticks_ms_64 () / 1000 ;
57
65
uint32_t epoch_s = timeutils_seconds_since_2000 (
58
66
tm -> tm_year , tm -> tm_mon , tm -> tm_mday , tm -> tm_hour , tm -> tm_min , tm -> tm_sec
59
67
);
@@ -66,8 +74,6 @@ void rtc_set_time(timeutils_struct_time_t *tm) {
66
74
rtc_offset [2 ] = RTC_OFFSET_CHECK_SUFFIX ;
67
75
}
68
76
69
- // end of Circuitpython
70
-
71
77
typedef struct _machine_rtc_obj_t {
72
78
mp_obj_base_t base ;
73
79
} machine_rtc_obj_t ;
@@ -78,29 +84,15 @@ STATIC const machine_rtc_obj_t machine_rtc_obj = {{&machine_rtc_type}};
78
84
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
85
// check arguments
80
86
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
- // }
87
+
90
88
// return constant object
91
89
return (mp_obj_t )& machine_rtc_obj ;
92
90
}
93
91
94
92
STATIC mp_obj_t machine_rtc_datetime (mp_uint_t n_args , const mp_obj_t * args ) {
95
93
if (n_args == 1 ) {
96
- // bool ret;
97
- // datetime_t t;
98
94
timeutils_struct_time_t t ;
99
95
100
- // ret = rtc_get_datetime(&t);
101
- // if (!ret) {
102
- // mp_raise_OSError(MP_EIO);
103
- // }
104
96
rtc_get_time (& t );
105
97
106
98
mp_obj_t tuple [8 ] = {
@@ -131,9 +123,6 @@ STATIC mp_obj_t machine_rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) {
131
123
// Deliberately ignore the weekday argument and compute the proper value
132
124
t .tm_wday = timeutils_calc_weekday (t .tm_year , t .tm_mon , t .tm_mday );
133
125
134
- // if (!rtc_set_time(&t)) {
135
- // mp_raise_OSError(MP_EINVAL);
136
- // }
137
126
rtc_set_time (& t );
138
127
139
128
}
0 commit comments