Skip to content

Commit 6871cb5

Browse files
committed
ports/nrf: Updates for review comments.
Added missing check for rtc_offset prefix/suffix. Changed millisecond divisor from 999 to 1000. Added 64 bit ticks counter to increase time before rollover. Made time.sleep and time.ticks_cpu methods to the time library on all builds (unconditionally). Removed unnecessary working comments. Signed-off-by: RetiredWizard <github@retiredwizard.com>
1 parent fdf0736 commit 6871cb5

File tree

5 files changed

+56
-39
lines changed

5 files changed

+56
-39
lines changed

ports/nrf/modules/machine/rtc.c

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
7-
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
86
* Copyright (c) 2019 Nick Moore for Adafruit Industries
9-
* Copyright (c) 2021 Junji Sakai
107
* Copyright (c) 2021 "Krzysztof Adamski" <k@japko.eu>
118
*
129
* Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -30,30 +27,41 @@
3027

3128
#include "py/runtime.h"
3229
#include "modmachine.h"
30+
#include "rtc.h"
3331

3432
#if MICROPY_PY_MACHINE_RTC
3533

3634
#include "py/mphal.h"
3735
#include "shared/timeutils/timeutils.h"
3836

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];
4039

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.
4148
#define RTC_OFFSET_CHECK_PREFIX 0x25ea7e2a
4249
#define RTC_OFFSET_CHECK_SUFFIX 0x2b80b69e
4350

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+
}
4757

4858
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;
5160
timeutils_seconds_since_2000_to_struct_time(rtc_offset[1] + ticks_s, tm);
5261
}
5362

5463
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;
5765
uint32_t epoch_s = timeutils_seconds_since_2000(
5866
tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec
5967
);
@@ -66,8 +74,6 @@ void rtc_set_time(timeutils_struct_time_t *tm) {
6674
rtc_offset[2] = RTC_OFFSET_CHECK_SUFFIX;
6775
}
6876

69-
// end of Circuitpython
70-
7177
typedef struct _machine_rtc_obj_t {
7278
mp_obj_base_t base;
7379
} machine_rtc_obj_t;
@@ -78,29 +84,15 @@ STATIC const machine_rtc_obj_t machine_rtc_obj = {{&machine_rtc_type}};
7884
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) {
7985
// check arguments
8086
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+
9088
// return constant object
9189
return (mp_obj_t)&machine_rtc_obj;
9290
}
9391

9492
STATIC mp_obj_t machine_rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) {
9593
if (n_args == 1) {
96-
// bool ret;
97-
// datetime_t t;
9894
timeutils_struct_time_t t;
9995

100-
// ret = rtc_get_datetime(&t);
101-
// if (!ret) {
102-
// mp_raise_OSError(MP_EIO);
103-
// }
10496
rtc_get_time(&t);
10597

10698
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) {
131123
// Deliberately ignore the weekday argument and compute the proper value
132124
t.tm_wday = timeutils_calc_weekday(t.tm_year, t.tm_mon, t.tm_mday);
133125

134-
// if (!rtc_set_time(&t)) {
135-
// mp_raise_OSError(MP_EINVAL);
136-
// }
137126
rtc_set_time(&t);
138127

139128
}

ports/nrf/modules/machine/rtc.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
26+
extern uint64_t ticks_ms_64(void);

ports/nrf/modules/utime/modutime.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ void rtc_get_time(timeutils_struct_time_t *time);
4747
// If secs is not provided or None, then the current time from is used.
4848
STATIC mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) {
4949
if (n_args == 0 || args[0] == mp_const_none) {
50-
// Get current date and time.
51-
// datetime_t t;
5250
timeutils_struct_time_t t;
5351
rtc_get_time(&t);
5452
mp_obj_t tuple[8] = {
@@ -125,17 +123,14 @@ STATIC const mp_rom_map_elem_t time_module_globals_table[] = {
125123

126124
{ MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&time_time_obj) },
127125
{ MP_ROM_QSTR(MP_QSTR_time_ns), MP_ROM_PTR(&mp_utime_time_ns_obj) },
128-
129-
{ MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&mp_utime_sleep_obj) },
130126
#endif
127+
{ MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&mp_utime_sleep_obj) },
131128
{ MP_ROM_QSTR(MP_QSTR_sleep_ms), MP_ROM_PTR(&mp_utime_sleep_ms_obj) },
132129
{ MP_ROM_QSTR(MP_QSTR_sleep_us), MP_ROM_PTR(&mp_utime_sleep_us_obj) },
133130

134131
{ MP_ROM_QSTR(MP_QSTR_ticks_ms), MP_ROM_PTR(&mp_utime_ticks_ms_obj) },
135132
{ MP_ROM_QSTR(MP_QSTR_ticks_us), MP_ROM_PTR(&mp_utime_ticks_us_obj) },
136-
#if MICROPY_PY_MACHINE_RTC
137133
{ MP_ROM_QSTR(MP_QSTR_ticks_cpu), MP_ROM_PTR(&mp_utime_ticks_cpu_obj) },
138-
#endif
139134
{ MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_utime_ticks_add_obj) },
140135
{ MP_ROM_QSTR(MP_QSTR_ticks_diff), MP_ROM_PTR(&mp_utime_ticks_diff_obj) },
141136
};

ports/nrf/mphalport.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ void rtc1_init_time_ticks(void) {
101101
// Start the low-frequency clock (if it hasn't been started already)
102102
if (!nrf_clock_lf_is_running(NRF_CLOCK)) {
103103
nrf_clock_task_trigger(NRF_CLOCK, NRF_CLOCK_TASK_LFCLKSTART);
104+
rtc_offset_check();
104105
}
105106
// Uninitialize first, then set overflow IRQ and first CC event
106107
nrfx_rtc_uninit(&rtc1);
@@ -110,7 +111,8 @@ void rtc1_init_time_ticks(void) {
110111
nrfx_rtc_enable(&rtc1);
111112
}
112113

113-
mp_uint_t mp_hal_ticks_ms(void) {
114+
115+
uint64_t ticks_ms_64(void) {
114116
// Compute: (rtc_overflows << 24 + COUNTER) * 1000 / 32768
115117
//
116118
// Note that COUNTER * 1000 / 32768 would overflow during calculation, so use
@@ -123,7 +125,11 @@ mp_uint_t mp_hal_ticks_ms(void) {
123125
uint32_t counter;
124126
// guard against overflow irq
125127
RTC1_GET_TICKS_ATOMIC(rtc1, overflows, counter)
126-
return (overflows << 9) * 1000 + (counter * 125 / 4096);
128+
return ((uint64_t)overflows << 9) * 1000 + (counter * 125 / 4096);
129+
}
130+
131+
mp_uint_t mp_hal_ticks_ms(void) {
132+
return ticks_ms_64();
127133
}
128134

129135
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
@@ -72,6 +72,7 @@ void rtc1_init_time_ticks();
7272
mp_uint_t mp_hal_ticks_ms(void);
7373
#define mp_hal_ticks_us() (0)
7474
#endif
75+
extern void rtc_offset_check(void);
7576

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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy