From c1f8823f615855c1a8e5e337e33ba0b36573bc43 Mon Sep 17 00:00:00 2001 From: Daniel Thompson Date: Sat, 9 May 2020 14:18:39 +0100 Subject: [PATCH] Add PNVRAM support to avoid forgetting the time during a reboot. --- TODO.md | 2 +- bootloader | 2 +- micropython | 2 +- wasp/drivers/nrf_rtc.py | 16 ++++++++++++++-- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/TODO.md b/TODO.md index 982a4b9..af655b3 100644 --- a/TODO.md +++ b/TODO.md @@ -47,7 +47,7 @@ applications. ### Bootloader * [X] OTA bootloader update - * [ ] RTC time measurement whilst in bootloader + * [X] RTC time measurement whilst in bootloader ### MicroPython diff --git a/bootloader b/bootloader index 7786926..d8d7d76 160000 --- a/bootloader +++ b/bootloader @@ -1 +1 @@ -Subproject commit 7786926950902cf4ce49a93894f69a205d6fd325 +Subproject commit d8d7d76b17e188420f66705f0b319f6e80a328f2 diff --git a/micropython b/micropython index 5424de7..8ea3211 160000 --- a/micropython +++ b/micropython @@ -1 +1 @@ -Subproject commit 5424de70d7cfb1d53828b54cd1df509c3f0fee74 +Subproject commit 8ea321106823866adcb6c4881b4045939b56cc3a diff --git a/wasp/drivers/nrf_rtc.py b/wasp/drivers/nrf_rtc.py index 63a2a51..0ff2c1f 100644 --- a/wasp/drivers/nrf_rtc.py +++ b/wasp/drivers/nrf_rtc.py @@ -3,6 +3,7 @@ """ Real Time Clock based on the nRF-family low power counter """ +import machine import time #class Stim(object): @@ -20,8 +21,17 @@ class RTC(object): def __init__(self, counter): self.counter = counter - self._uptime = 0 - self.set_localtime((2020, 3, 1, 3, 0, 0, 0, 0)) + + if machine.mem32[0x200039c0] == 0x1abe11ed and \ + machine.mem32[0x200039dc] == 0x10adab1e: + self.lastcount = self.counter.counter() + self.offset = machine.mem32[0x200039c4] + self._uptime = machine.mem32[0x200039c8] // 125 + else: + machine.mem32[0x200039c0] = 0x1abe11ed + machine.mem32[0x200039dc] = 0x10adab1e + self._uptime = 0 + self.set_localtime((2020, 3, 1, 3, 0, 0, 0, 0)) def update(self): newcount = self.counter.counter() @@ -34,6 +44,7 @@ class RTC(object): self.lastcount += split self.lastcount &= (1 << 24) - 1 self._uptime += split + machine.mem32[0x200039c8] = self._uptime * 125 return True @@ -52,6 +63,7 @@ class RTC(object): lt = time.mktime(t) self.offset = lt - (self._uptime >> 3) + machine.mem32[0x200039c4] = self.offset def get_localtime(self): self.update()