Skip to content

Commit 1ac4f91

Browse files
committed
Persist time and steps across reboots
1 parent 7dea64a commit 1ac4f91

File tree

6 files changed

+63
-11
lines changed

6 files changed

+63
-11
lines changed

src/components/motion/MotionController.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,15 @@ namespace {
3838
void MotionController::AdvanceDay() {
3939
--nbSteps; // Higher index = further in the past
4040
SetSteps(Days::Today, 0);
41+
carrySteps = 0;
4142
if (service != nullptr) {
4243
service->OnNewStepCountValue(NbSteps(Days::Today));
4344
}
4445
}
4546

4647
void MotionController::Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps) {
48+
// Offset the sensor value by whatever we are carrying forward
49+
nbSteps += carrySteps;
4750
uint32_t oldSteps = NbSteps(Days::Today);
4851
if (oldSteps != nbSteps && service != nullptr) {
4952
service->OnNewStepCountValue(nbSteps);
@@ -160,3 +163,9 @@ void MotionController::Init(Pinetime::Drivers::Bma421::DeviceTypes types) {
160163
break;
161164
}
162165
}
166+
167+
void MotionController::Restore(uint32_t carrySteps, uint32_t carryTripSteps) {
168+
this->carrySteps = carrySteps;
169+
SetSteps(Days::Today, carrySteps);
170+
currentTripSteps = carryTripSteps;
171+
}

src/components/motion/MotionController.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ namespace Pinetime {
6666

6767
void Init(Pinetime::Drivers::Bma421::DeviceTypes types);
6868

69+
void Restore(uint32_t carrySteps, uint32_t carryTripSteps);
70+
6971
void SetService(Pinetime::Controllers::MotionService* service) {
7072
this->service = service;
7173
}
@@ -76,6 +78,7 @@ namespace Pinetime {
7678

7779
private:
7880
Utility::CircularBuffer<uint32_t, stepHistorySize> nbSteps = {0};
81+
uint32_t carrySteps = 0;
7982
uint32_t currentTripSteps = 0;
8083

8184
void SetSteps(Days day, uint32_t steps) {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#pragma once
2+
3+
#include <cstdint>
4+
5+
namespace Pinetime {
6+
namespace Components {
7+
class RebootPersist {
8+
public:
9+
[[nodiscard]] bool IsValid() const volatile {
10+
return canary == magic;
11+
}
12+
13+
void SetValid() volatile {
14+
canary = magic;
15+
}
16+
17+
// layout of time point is implementation defined
18+
// instead store milliseconds since epoch
19+
uint64_t timeMillis;
20+
uint32_t steps;
21+
uint32_t tripSteps;
22+
23+
private:
24+
// The canary determines whether memory has been kept or not after a reset,
25+
// since the NRF52832 doesn't guarantee RAM retention
26+
// See https://docs.nordicsemi.com/bundle/ps_nrf52832/page/power.html#d935e523
27+
// If the magic value is still in the canary when booting,
28+
// we assume that memory is intact from the previous boot
29+
30+
// Increment magic upon changing members of this class
31+
// Otherwise garbage values will be loaded after DFU
32+
static constexpr uint32_t magic = 0xDEAD0001;
33+
uint32_t canary;
34+
};
35+
}
36+
}

src/main.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "components/datetime/DateTimeController.h"
3737
#include "components/heartrate/HeartRateController.h"
3838
#include "components/stopwatch/StopWatchController.h"
39+
#include "components/persistence/RebootPersist.h"
3940
#include "components/fs/FS.h"
4041
#include "drivers/Spi.h"
4142
#include "drivers/SpiMaster.h"
@@ -161,14 +162,10 @@ void vApplicationStackOverflowHook(TaskHandle_t /*xTask*/, char* /*pcTaskName*/)
161162
stackOverflowCount++;
162163
}
163164
}
164-
/* Variable Declarations for variables in noinit SRAM
165-
Increment NoInit_MagicValue upon adding variables to this area
166-
*/
165+
// Variable Declarations for variables in noinit SRAM
167166
extern uint32_t __start_noinit_data;
168167
extern uint32_t __stop_noinit_data;
169-
static constexpr uint32_t NoInit_MagicValue = 0xDEAD0000;
170-
uint32_t NoInit_MagicWord __attribute__((section(".noinit")));
171-
std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> NoInit_BackUpTime __attribute__((section(".noinit")));
168+
volatile Pinetime::Components::RebootPersist NoInit_Persistence __attribute__((section(".noinit")));
172169

173170
void nrfx_gpiote_evt_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action) {
174171
if (pin == Pinetime::PinMap::Cst816sIrq) {
@@ -352,12 +349,14 @@ int main() {
352349
// retrieve version stored by bootloader
353350
Pinetime::BootloaderVersion::SetVersion(NRF_TIMER2->CC[0]);
354351

355-
if (NoInit_MagicWord == NoInit_MagicValue) {
356-
dateTimeController.SetCurrentTime(NoInit_BackUpTime);
352+
if (NoInit_Persistence.IsValid()) {
353+
dateTimeController.SetCurrentTime(
354+
std::chrono::time_point<std::chrono::system_clock>(std::chrono::milliseconds(NoInit_Persistence.timeMillis)));
355+
motionController.Restore(NoInit_Persistence.steps, NoInit_Persistence.tripSteps);
357356
} else {
358357
// Clear Memory to known state
359358
memset(&__start_noinit_data, 0, (uintptr_t) &__stop_noinit_data - (uintptr_t) &__start_noinit_data);
360-
NoInit_MagicWord = NoInit_MagicValue;
359+
NoInit_Persistence.SetValid();
361360
}
362361

363362
systemTask.Start();

src/systemtask/SystemTask.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,11 @@ void SystemTask::Work() {
391391
}
392392
}
393393
monitor.Process();
394-
NoInit_BackUpTime = dateTimeController.CurrentDateTime();
394+
395+
NoInit_Persistence.steps = motionController.NbSteps();
396+
NoInit_Persistence.tripSteps = motionController.GetTripSteps();
397+
NoInit_Persistence.timeMillis =
398+
std::chrono::duration_cast<std::chrono::milliseconds>(dateTimeController.CurrentDateTime().time_since_epoch()).count();
395399
if (nrf_gpio_pin_read(PinMap::Button) == 0) {
396400
watchdog.Reload();
397401
}

src/systemtask/SystemTask.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "components/stopwatch/StopWatchController.h"
1919
#include "components/alarm/AlarmController.h"
2020
#include "components/fs/FS.h"
21+
#include "components/persistence/RebootPersist.h"
2122
#include "touchhandler/TouchHandler.h"
2223
#include "buttonhandler/ButtonHandler.h"
2324
#include "buttonhandler/ButtonActions.h"
@@ -32,7 +33,7 @@
3233
#include "drivers/Watchdog.h"
3334
#include "systemtask/Messages.h"
3435

35-
extern std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> NoInit_BackUpTime;
36+
extern volatile Pinetime::Components::RebootPersist NoInit_Persistence;
3637

3738
namespace Pinetime {
3839
namespace Drivers {

0 commit comments

Comments
 (0)