-
-
Notifications
You must be signed in to change notification settings - Fork 984
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Continuous time update - Alternative implementation to #2041 #2054
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,6 +1,7 @@ | ||||||
#include "components/datetime/DateTimeController.h" | ||||||
#include <libraries/log/nrf_log.h> | ||||||
#include <systemtask/SystemTask.h> | ||||||
#include <nrf_rtc.h> | ||||||
|
||||||
using namespace Pinetime::Controllers; | ||||||
|
||||||
|
@@ -12,11 +13,16 @@ namespace { | |||||
} | ||||||
|
||||||
DateTime::DateTime(Controllers::Settings& settingsController) : settingsController {settingsController} { | ||||||
mutex = xSemaphoreCreateMutex(); | ||||||
ASSERT(mutex != nullptr); | ||||||
xSemaphoreGive(mutex); | ||||||
} | ||||||
|
||||||
void DateTime::SetCurrentTime(std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> t) { | ||||||
xSemaphoreTake(mutex, portMAX_DELAY); | ||||||
this->currentDateTime = t; | ||||||
UpdateTime(previousSystickCounter); // Update internal state without updating the time | ||||||
xSemaphoreGive(mutex); | ||||||
} | ||||||
|
||||||
void DateTime::SetTime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second) { | ||||||
|
@@ -35,7 +41,9 @@ void DateTime::SetTime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, | |||||
NRF_LOG_INFO("%d %d %d ", day, month, year); | ||||||
NRF_LOG_INFO("%d %d %d ", hour, minute, second); | ||||||
|
||||||
xSemaphoreTake(mutex, portMAX_DELAY); | ||||||
UpdateTime(previousSystickCounter); | ||||||
xSemaphoreGive(mutex); | ||||||
|
||||||
systemTask->PushMessage(System::Messages::OnNewTime); | ||||||
} | ||||||
|
@@ -45,29 +53,23 @@ void DateTime::SetTimeZone(int8_t timezone, int8_t dst) { | |||||
dstOffset = dst; | ||||||
} | ||||||
|
||||||
void DateTime::UpdateTime(uint32_t systickCounter) { | ||||||
uint32_t DateTime::GetTickFromPreviousSystickCounter(uint32_t systickCounter) const { | ||||||
// Handle systick counter overflow | ||||||
uint32_t systickDelta = 0; | ||||||
if (systickCounter < previousSystickCounter) { | ||||||
systickDelta = 0xffffff - previousSystickCounter; | ||||||
systickDelta = static_cast<uint32_t>(portNRF_RTC_MAXTICKS) - previousSystickCounter; | ||||||
systickDelta += systickCounter + 1; | ||||||
} else { | ||||||
systickDelta = systickCounter - previousSystickCounter; | ||||||
} | ||||||
return systickDelta; | ||||||
} | ||||||
|
||||||
/* | ||||||
* 1000 ms = 1024 ticks | ||||||
*/ | ||||||
auto correctedDelta = systickDelta / 1024; | ||||||
auto rest = systickDelta % 1024; | ||||||
if (systickCounter >= rest) { | ||||||
previousSystickCounter = systickCounter - rest; | ||||||
} else { | ||||||
previousSystickCounter = 0xffffff - (rest - systickCounter); | ||||||
} | ||||||
|
||||||
currentDateTime += std::chrono::seconds(correctedDelta); | ||||||
uptime += std::chrono::seconds(correctedDelta); | ||||||
void DateTime::UpdateTime() { | ||||||
xSemaphoreTake(mutex, portMAX_DELAY); | ||||||
uint32_t systick_counter = nrf_rtc_counter_get(portNRF_RTC_REG); | ||||||
UpdateTime(systick_counter); | ||||||
xSemaphoreGive(mutex); | ||||||
|
||||||
std::time_t currentTime = std::chrono::system_clock::to_time_t(currentDateTime); | ||||||
localTime = *std::localtime(¤tTime); | ||||||
|
@@ -103,6 +105,23 @@ void DateTime::UpdateTime(uint32_t systickCounter) { | |||||
} | ||||||
} | ||||||
|
||||||
void DateTime::UpdateTime(uint32_t systickCounter) { | ||||||
auto systickDelta = GetTickFromPreviousSystickCounter(systickCounter); | ||||||
auto correctedDelta = systickDelta / configTICK_RATE_HZ; | ||||||
|
||||||
/* | ||||||
* 1000 ms = 1024 ticks | ||||||
*/ | ||||||
auto rest = systickDelta % configTICK_RATE_HZ; | ||||||
if (systickCounter >= rest) { | ||||||
previousSystickCounter = systickCounter - rest; | ||||||
} else { | ||||||
previousSystickCounter = static_cast<uint32_t>(portNRF_RTC_MAXTICKS) - (rest - systickCounter); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
previousSystickCounter should semantically be |
||||||
} | ||||||
currentDateTime += std::chrono::seconds(correctedDelta); | ||||||
uptime += std::chrono::seconds(correctedDelta); | ||||||
} | ||||||
|
||||||
const char* DateTime::MonthShortToString() const { | ||||||
return MonthsString[static_cast<uint8_t>(Month())]; | ||||||
} | ||||||
|
@@ -146,3 +165,18 @@ std::string DateTime::FormattedTime() { | |||||
} | ||||||
return std::string(buff); | ||||||
} | ||||||
|
||||||
std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> DateTime::CurrentDateTime() const { | ||||||
xSemaphoreTake(mutex, portMAX_DELAY); | ||||||
uint32_t systick_counter = nrf_rtc_counter_get(portNRF_RTC_REG); | ||||||
auto correctedDelta = GetTickFromPreviousSystickCounter(systick_counter) / configTICK_RATE_HZ; | ||||||
auto result = currentDateTime + std::chrono::seconds(correctedDelta); | ||||||
; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Formatting typo?
Suggested change
|
||||||
xSemaphoreGive(mutex); | ||||||
|
||||||
return result; | ||||||
} | ||||||
|
||||||
std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> DateTime::UTCDateTime() const { | ||||||
return CurrentDateTime() - std::chrono::seconds((tzOffset + dstOffset) * 15 * 60); | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should probably extend the critical section to include updating
localTime