Skip to content

Commit db55ee0

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 db55ee0

File tree

4 files changed

+31
-38
lines changed

4 files changed

+31
-38
lines changed

ports/nrf/modules/machine/rtc.c

Lines changed: 21 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
@@ -36,24 +33,36 @@
3633
#include "py/mphal.h"
3734
#include "shared/timeutils/timeutils.h"
3835

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);
4337

4438
// This is the time in seconds since 2000 that the RTC was started.
4539
__attribute__((section(".uninitialized"))) static uint32_t rtc_offset[3];
4640

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

4859
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;
5161
timeutils_seconds_since_2000_to_struct_time(rtc_offset[1] + ticks_s, tm);
5262
}
5363

5464
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;
5766
uint32_t epoch_s = timeutils_seconds_since_2000(
5867
tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec
5968
);
@@ -66,8 +75,6 @@ void rtc_set_time(timeutils_struct_time_t *tm) {
6675
rtc_offset[2] = RTC_OFFSET_CHECK_SUFFIX;
6776
}
6877

69-
// end of Circuitpython
70-
7178
typedef struct _machine_rtc_obj_t {
7279
mp_obj_base_t base;
7380
} machine_rtc_obj_t;
@@ -78,29 +85,15 @@ STATIC const machine_rtc_obj_t machine_rtc_obj = {{&machine_rtc_type}};
7885
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) {
7986
// check arguments
8087
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+
9089
// return constant object
9190
return (mp_obj_t)&machine_rtc_obj;
9291
}
9392

9493
STATIC mp_obj_t machine_rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) {
9594
if (n_args == 1) {
96-
// bool ret;
97-
// datetime_t t;
9895
timeutils_struct_time_t t;
9996

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

10699
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) {
131124
// Deliberately ignore the weekday argument and compute the proper value
132125
t.tm_wday = timeutils_calc_weekday(t.tm_year, t.tm_mon, t.tm_mday);
133126

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

139129
}

ports/nrf/modules/utime/modutime.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ void rtc_get_time(timeutils_struct_time_t *time);
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) {
5050
// Get current date and time.
51-
// datetime_t t;
5251
timeutils_struct_time_t t;
5352
rtc_get_time(&t);
5453
mp_obj_t tuple[8] = {
@@ -125,17 +124,14 @@ STATIC const mp_rom_map_elem_t time_module_globals_table[] = {
125124

126125
{ MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&time_time_obj) },
127126
{ 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) },
130127
#endif
128+
{ MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&mp_utime_sleep_obj) },
131129
{ MP_ROM_QSTR(MP_QSTR_sleep_ms), MP_ROM_PTR(&mp_utime_sleep_ms_obj) },
132130
{ MP_ROM_QSTR(MP_QSTR_sleep_us), MP_ROM_PTR(&mp_utime_sleep_us_obj) },
133131

134132
{ MP_ROM_QSTR(MP_QSTR_ticks_ms), MP_ROM_PTR(&mp_utime_ticks_ms_obj) },
135133
{ MP_ROM_QSTR(MP_QSTR_ticks_us), MP_ROM_PTR(&mp_utime_ticks_us_obj) },
136-
#if MICROPY_PY_MACHINE_RTC
137134
{ MP_ROM_QSTR(MP_QSTR_ticks_cpu), MP_ROM_PTR(&mp_utime_ticks_cpu_obj) },
138-
#endif
139135
{ MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_utime_ticks_add_obj) },
140136
{ MP_ROM_QSTR(MP_QSTR_ticks_diff), MP_ROM_PTR(&mp_utime_ticks_diff_obj) },
141137
};

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