Skip to content

Commit f23ef84

Browse files
committed
Improve date/time accuracy and handle counter overflow.
1 parent 9baf00b commit f23ef84

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

src/DisplayApp/DisplayApp.cpp

+21-13
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,16 @@ void DisplayApp::Refresh() {
146146
state = States::Running;
147147
break;
148148
case Messages::UpdateDateTime:
149+
currentDateTime = {};
150+
currentDateTime += date::years( dateTimeController.Year()-1970);
151+
currentDateTime += date::days( dateTimeController.Day() - 1);
152+
currentDateTime += date::months( (int)dateTimeController.Month() - 1);
153+
154+
currentDateTime += std::chrono::hours(dateTimeController.Hours());
155+
currentDateTime += std::chrono::minutes (dateTimeController.Minutes());
156+
currentDateTime += std::chrono::seconds (dateTimeController.Seconds());
157+
158+
currentDateTime -= std::chrono::hours(3); // TODO WHYYYY?
149159
break;
150160
case Messages::UpdateBleConnection:
151161
bleConnectionUpdated = true;
@@ -163,6 +173,15 @@ void DisplayApp::Refresh() {
163173

164174
void DisplayApp::RunningState() {
165175
uint32_t systick_counter = nrf_rtc_counter_get(portNRF_RTC_REG);
176+
uint32_t systickDelta = 0;
177+
if(systick_counter < previousSystickCounter) {
178+
systickDelta = 0xffffff - previousSystickCounter;
179+
systickDelta += systick_counter + 1;
180+
} else {
181+
systickDelta = systick_counter - previousSystickCounter;
182+
}
183+
184+
previousSystickCounter = systick_counter;
166185

167186
if (batteryLevelUpdated) {
168187
char batteryChar[11];
@@ -181,19 +200,8 @@ void DisplayApp::RunningState() {
181200
gfx->DrawString(10, 0, color, "BLE", &smallFont, false);
182201
}
183202

184-
auto raw = systick_counter / 1000;
185-
auto currentDeltaSeconds = raw - deltaSeconds;
186-
187-
std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> currentDateTime;
188-
currentDateTime += date::years( dateTimeController.Year()-1970);
189-
currentDateTime += date::days( dateTimeController.Day() - 1);
190-
currentDateTime += date::months( (int)dateTimeController.Month() - 1);
191-
192-
currentDateTime += std::chrono::hours(dateTimeController.Hours());
193-
currentDateTime += std::chrono::minutes (dateTimeController.Minutes());
194-
currentDateTime += std::chrono::seconds(dateTimeController.Seconds() + currentDeltaSeconds);
195-
196-
currentDateTime -= std::chrono::hours(3); // TODO WHYYYY?
203+
// TODO date/time management should be done in module DateTimeController
204+
currentDateTime += std::chrono::milliseconds(systickDelta);
197205

198206
auto dp = date::floor<date::days>(currentDateTime);
199207
auto time = date::make_time(currentDateTime-dp);

src/DisplayApp/DisplayApp.h

+2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ namespace Pinetime {
6565

6666
Pinetime::Drivers::Cst816S touchPanel;
6767
void OnTouchEvent();
68+
uint32_t previousSystickCounter = 0;
69+
std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> currentDateTime;
6870
};
6971
}
7072
}

src/FreeRTOSConfig.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
#define configUSE_PREEMPTION 1
5959
#define configUSE_PORT_OPTIMISED_TASK_SELECTION 1
6060
#define configUSE_TICKLESS_IDLE 1
61-
#define configUSE_TICKLESS_IDLE_SIMPLE_DEBUG 1 /* See into vPortSuppressTicksAndSleep source code for explanation */
61+
#define configUSE_TICKLESS_IDLE_SIMPLE_DEBUG 0 /* See into vPortSuppressTicksAndSleep source code for explanation */
6262
#define configCPU_CLOCK_HZ ( SystemCoreClock )
6363
#define configTICK_RATE_HZ 1024
6464
#define configMAX_PRIORITIES ( 3 )

0 commit comments

Comments
 (0)