8000 ports/nrf: Force push rebase attempt. · micropython/micropython@19789ab · GitHub
[go: up one dir, main page]

Skip to content

Commit 19789ab

Browse files
committed
ports/nrf: Force push rebase attempt.
Signed-off-by: RetiredWizard <github@retiredwizard.com>
1 parent 35e8d18 commit 19789ab

File tree

9 files changed

+237
-4
lines changed

9 files changed

+237
-4
lines changed

ports/nrf/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ DRIVERS_SRC_C += $(addprefix modules/,\
267267
machine/pin.c \
268268
machine/timer.c \
269269
machine/rtcounter.c \
270+
machine/rtc.c \
270271
machine/temp.c \
271272
os/microbitfs.c \
272273
board/modboard.c \

ports/nrf/modtime.c

+51Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2013-2023 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "py/obj.h"
28+
#include "shared/timeutils/timeutils.h"
29+
#include "ports/nrf/modules/machine/rtc.h"
30+
31+
// Return the localtime as an 8-tuple.
32+
static mp_obj_t mp_time_localtime_get(void) {
33+
mp_int_t seconds = ticks_ms_64() / 1000;
34+
timeutils_struct_time_t tm;
35+
timeutils_seconds_since_epoch_to_struct_time(seconds, &tm);
36+
mp_obj_t tuple[8] = {
37+
tuple[0] = mp_obj_new_int(tm.tm_year),
38+
tuple[1] = mp_obj_new_int(tm.tm_mon),
39+
tuple[2] = mp_obj_new_int(tm.tm_mday),
40+
tuple[3] = mp_obj_new_int(tm.tm_hour),
41+
tuple[4] = mp_obj_new_int(tm.tm_min),
42+
tuple[5] = mp_obj_new_int(tm.tm_sec),
43+
tuple[6] = mp_obj_new_int(tm.tm_wday),
44+
tuple[7] = mp_obj_new_int(tm.tm_yday),
45+
};
46+
return mp_obj_new_tuple(8, tuple);
47+
}
48+
49+
static mp_obj_t mp_time_time_get(void) {
50+
return mp_obj_new_int((ticks_ms_64() + rtc_offset[1]) / 1000);
51+
}

ports/nrf/modules/machine/modmachine.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@
6565
#define MICROPY_PY_MACHINE_RTCOUNTER_ENTRY
6666
#endif
6767

68+
#if MICROPY_PY_MACHINE_RTC
69+
#define MICROPY_PY_MACHINE_RTC_ENTRY { MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&machine_rtc_type) },
70+
#else
71+
#define MICROPY_PY_MACHINE_RTC_ENTRY
72+
#endif
73+
6874
#if MICROPY_PY_MACHINE_TIMER_NRF
6975
#define MICROPY_PY_MACHINE_TIMER_ENTRY { MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&machine_timer_type) },
7076
#else

ports/nrf/modules/machine/modmachine.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030

3131
#include "py/obj.h"
3232

33+
#if MICROPY_PY_MACHINE_RTC
34+
extern const mp_obj_type_t machine_rtc_type;
35+
#endif
36+
3337
void machine_init(void);
3438

3539
#endif // __MICROPY_INCLUDED_NRF5_MODMACHINE_H__

ports/nrf/modules/machine/rtc.c

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2019 Nick Moore for Adafruit Industries
7+
* Copyright (c) 2021 "Krzysztof Adamski" <k@japko.eu>
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#include "py/runtime.h"
29+
#include "modmachine.h"
30+
31+
#if MICROPY_PY_MACHINE_RTC
32+
33+
#include "py/mphal.h"
34+
#include "shared/timeutils/timeutils.h"
35+
#include "rtc.h"
36+
37+
38+
// These values are placed before and after the current RTC count. They are
39+
// used to determine if the RTC count is valid. These randomly-generated values
40+
// will be set when the RTC value is set in order to mark the RTC as valid. If
41+
// the system crashes or reboots, these values will remain undisturbed and the
42+
// RTC offset will remain valid.
43+
//
44+
// If MicroPython is updated or these symbols shift around, the prefix and
45+
// suffix will no longer match, and the time will no longer be valid.
46+
#define RTC_OFFSET_CHECK_PREFIX 0x25ea7e2a
47+
#define RTC_OFFSET_CHECK_SUFFIX 0x2b80b69e
48+
49+
void rtc_offset_check(void) {
50+
// If the prefix and suffix are not valid, zero-initialize the RTC offset.
51+
if ((rtc_offset[0] != RTC_OFFSET_CHECK_PREFIX) || (rtc_offset[2] != RTC_OFFSET_CHECK_SUFFIX)) {
52+
rtc_offset[1] = 0;
53+
}
54+
}
55+
56+
void rtc_get_time(timeutils_struct_time_t *tm) {
57+
uint64_t ticks_s = ticks_ms_64() / 1000;
58+
timeutils_seconds_since_2000_to_struct_time(rtc_offset[1] + ticks_s, tm);
59+
}
60+
61+
void rtc_set_time(timeutils_struct_time_t *tm) {
62+
uint64_t ticks_s = ticks_ms_64() / 1000;
63+
uint32_t epoch_s = timeutils_seconds_since_2000(
64+
tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec
65+
);
66+
rtc_offset[1] = epoch_s - ticks_s;
67+
68+
// Set the prefix and suffix in order to indicate the time is valid. This
69+
// must be done after the offset is updated, in case there is a crash or
70+
// power failure.
71+
rtc_offset[0] = RTC_OFFSET_CHECK_PREFIX;
72+
rtc_offset[2] = RTC_OFFSET_CHECK_SUFFIX;
73+
}
74+
75+
typedef struct _machine_rtc_obj_t {
76+
mp_obj_base_t base;
77+
} machine_rtc_obj_t;
78+
79+
// singleton RTC object
80+
static const machine_rtc_obj_t machine_rtc_obj = {{&machine_rtc_type}};
81+
82+
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) {
83+
// check arguments
84+
mp_arg_check_num(n_args, n_kw, 0, 0, false);
85+
86+
// return constant object
87+
return (mp_obj_t)&machine_rtc_obj;
88+
}
89+
90+
static mp_obj_t machine_rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) {
91+
if (n_args == 1) {
92+
timeutils_struct_time_t t;
93+
94+
rtc_get_time(&t);
95+
96+
mp_obj_t tuple[8] = {
97+
mp_obj_new_int(t.tm_year),
98+
mp_obj_new_int(t.tm_mon),
99+
mp_obj_new_int(t.tm_mday),
100+
mp_obj_new_int(t.tm_wday),
101+
mp_obj_new_int(t.tm_hour),
102+
mp_obj_new_int(t.tm_min),
103+
mp_obj_new_int(t.tm_sec),
104+
mp_obj_new_int(0)
105+
};
106+
107+
return mp_obj_new_tuple(8, tuple);
108+
} else {
109+
mp_obj_t *items;
110+
111+
mp_obj_get_array_fixed_n(args[1], 8, &items);
112+
113+
timeutils_struct_time_t t = {
114+
.tm_year = mp_obj_get_int(items[0]),
115+
.tm_mon = mp_obj_get_int(items[1]),
116+
.tm_mday = mp_obj_get_int(items[2]),
117+
.tm_hour = mp_obj_get_int(items[4]),
118+
.tm_min = mp_obj_get_int(items[5]),
119+
.tm_sec = mp_obj_get_int(items[6]),
120+
};
121+
// Deliberately ignore the weekday argument and compute the proper value
122+
t.tm_wday = timeutils_calc_weekday(t.tm_year, t.tm_mon, t.tm_mday);
123+
124+
rtc_set_time(&t);
125+
126+
}
127+
return mp_const_none;
128+
}
129+
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_rtc_datetime_obj, 1, 2, machine_rtc_datetime);
130+
131+
static const mp_rom_map_elem_t machine_rtc_locals_dict_table[] = {
132+
{ MP_ROM_QSTR(MP_QSTR_datetime), MP_ROM_PTR(&machine_rtc_datetime_obj) },
133+
};
134+
static MP_DEFINE_CONST_DICT(machine_rtc_locals_dict, machine_rtc_locals_dict_table);
135+
136+
MP_DEFINE_CONST_OBJ_TYPE(
137+
machine_rtc_type,
138+
MP_QSTR_RTC,
139+
MP_TYPE_FLAG_NONE,
140+
make_new, machine_rtc_make_new,
141+
locals_dict, &machine_rtc_locals_dict
142+
);
143+
144+
#endif // MICROPY_PY_MACHINE_RTC

ports/nrf/modules/machine/rtc.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
extern uint64_t ticks_ms_64(void);
2+
3+
// This is the time in seconds since 2000 that the RTC was started.
4+
__attribute__((section(".uninitialized"))) static uint32_t rtc_offset[3];

ports/nrf/mpconfigport.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,26 @@
234234
#define MICROPY_PY_MACHINE_RTCOUNTER (0)
235235
#endif
236236

237+
#ifndef MICROPY_PY_MACHINE_RTC
238+
#define MICROPY_PY_MACHINE_RTC (1)
239+
#endif
240+
237241
#ifndef MICROPY_PY_TIME_TICKS
238242
#define MICROPY_PY_TIME_TICKS (1)
239243
#endif
240244

245+
#ifndef MICROPY_PY_TIME_GMTIME_LOCALTIME_MKTIME
246+
#define MICROPY_PY_TIME_GMTIME_LOCALTIME_MKTIME (1)
247+
#endif
248+
249+
#ifndef MICROPY_PY_TIME_TIME_TIME_NS
250+
#define MICROPY_PY_TIME_TIME_TIME_NS (1)
251+
#endif
252+
253+
#ifndef MICROPY_PY_TIME_INCLUDEFILE
254+
#define MICROPY_PY_TIME_INCLUDEFILE "ports/nrf/modtime.c"
255+
#endif
256+
241257
#define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1)
242258
#define MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE (0)
243259

ports/nrf/mphalport.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,10 @@ static void rtc_irq_time(nrfx_rtc_int_type_t event) {
129129

130130
void rtc1_init_time_ticks(void) {
131131
// Start the low-frequency clock (if it hasn't been started already)
132-
mp_nrf_start_lfclk();
132+
if (!nrf_clock_lf_is_running(NRF_CLOCK)) {
133+
nrf_clock_task_trigger(NRF_CLOCK, NRF_CLOCK_TASK_LFCLKSTART);
134+
rtc_offset_check();
135+
}
133136
// Uninitialize first, then set overflow IRQ and first CC event
134137
nrfx_rtc_uninit(&rtc1);
135138
nrfx_rtc_init(&rtc1, &rtc_config_time_ticks, rtc_irq_time);
@@ -138,8 +141,7 @@ void rtc1_init_time_ticks(void) {
138141
nrfx_rtc_enable(&rtc1);
139142
}
140143

141-
mp_uint_t mp_hal_ticks_ms(void) {
142-
// Compute: (rtc_overflows << 24 + COUNTER) * 1000 / 32768
144+
uint64_t ticks_ms_64(void) { // Compute: (rtc_overflows << 24 + COUNTER) * 1000 / 32768
143145
//
144146
// Note that COUNTER * 1000 / 32768 would overflow during calculation, so use
145147
// the less obvious * 125 / 4096 calculation (overflow secure).
@@ -151,7 +153,11 @@ mp_uint_t mp_hal_ticks_ms(void) {
151153
uint32_t counter;
152154
// guard against overflow irq
153155
RTC1_GET_TICKS_ATOMIC(rtc1, overflows, counter)
154-
return (overflows << 9) * 1000 + (counter * 125 / 4096);
156+
return ((uint64_t)overflows << 9) * 1000 + (counter * 125 / 4096);
157+
}
158+
159+
mp_uint_t mp_hal_ticks_ms(void) {
160+
return ticks_ms_64();
155161
}
156162

157163
mp_uint_t mp_hal_ticks_us(void) {

ports/nrf/mphalport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ void rtc1_init_time_ticks();
7474
mp_uint_t mp_hal_ticks_ms(void);
7575
#define mp_hal_ticks_us() (0)
7676
#endif
77+
extern void rtc_offset_check(void);
7778

7879
// TODO: empty implementation for now. Used by machine_spi.c:69
7980
#define mp_hal_delay_us_fast(p)

0 commit comments

Comments
 (0)
0