Skip to content

Commit a4c8ef9

Browse files
committed
esp8266: Reset "virtual RTC" on power on.
Initialize RTC period coefficients, etc. if RTC RAM doesn't contain valid values. time.time() then will return number of seconds since power-on, unless set to different timebase. This reuses MEM_MAGIC for the purpose beyond its initial purpose (but the whole modpybrtc.c need to be eventually reworked completely anyway).
1 parent 5788499 commit a4c8ef9

3 files changed

Lines changed: 16 additions & 0 deletions

File tree

esp8266/esp_mphal.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ extern void ets_delay_us();
3737

3838
void mp_hal_init(void) {
3939
ets_wdt_disable(); // it's a pain while developing
40+
mp_hal_rtc_init();
4041
uart_init(UART_BIT_RATE_115200, UART_BIT_RATE_115200);
4142
}
4243

esp8266/esp_mphal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
void ets_isr_mask(unsigned);
3232

3333
void mp_hal_init(void);
34+
void mp_hal_rtc_init(void);
3435
void mp_hal_feed_watchdog(void);
3536

3637
uint32_t mp_hal_ticks_us(void);

esp8266/modpybrtc.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ typedef struct _pyb_rtc_obj_t {
4949
// singleton RTC object
5050
STATIC const pyb_rtc_obj_t pyb_rtc_obj = {{&pyb_rtc_type}};
5151

52+
void mp_hal_rtc_init(void) {
53+
uint32_t magic;
54+
55+
system_rtc_mem_read(MEM_USER_MAGIC_ADDR, &magic, sizeof(magic));
56+
if (magic != MEM_MAGIC) {
57+
magic = MEM_MAGIC;
58+
system_rtc_mem_write(MEM_USER_MAGIC_ADDR, &magic, sizeof(magic));
59+
uint32_t cal = system_rtc_clock_cali_proc();
60+
int64_t delta = 0;
61+
system_rtc_mem_write(MEM_CAL_ADDR, &cal, sizeof(cal));
62+
system_rtc_mem_write(MEM_DELTA_ADDR, &delta, sizeof(delta));
63+
}
64+
}
65+
5266
STATIC mp_obj_t pyb_rtc_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
5367
// check arguments
5468
mp_arg_check_num(n_args, n_kw, 0, 0, false);

0 commit comments

Comments
 (0)