From 55f8908769aab970413b249cb26acdaf22c79ada Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Tue, 17 Aug 2021 23:53:57 +0000 Subject: [PATCH 1/7] Make Clock Persistant. --- gcc_nrf52.ld | 11 ++++++++++- src/components/datetime/DateTimeController.cpp | 7 ++++++- src/components/datetime/DateTimeController.h | 2 ++ src/main.cpp | 11 ++++++++++- 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/gcc_nrf52.ld b/gcc_nrf52.ld index 98e133aa..b40dc06b 100644 --- a/gcc_nrf52.ld +++ b/gcc_nrf52.ld @@ -3,14 +3,23 @@ SEARCH_DIR(.) GROUP(-lgcc -lc -lnosys) +NO_INIT_SIZE = 0x100; +RAM_MAX = 64K; MEMORY { FLASH (rx) : ORIGIN = 0x00000, LENGTH = 0x78000 - RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 0x10000 + RAM (rwx) : ORIGIN = 0x20000000, LENGTH = RAM_MAX - NO_INIT_SIZE + NOINIT (rwx): ORIGIN = ORIGIN(RAM) + LENGTH(RAM), LENGTH = NO_INIT_SIZE } SECTIONS { + noinit (NOLOAD): + { + PROVIDE(__start_noinit_data = .); + KEEP(*(.noinit)) + PROVIDE(__stop_noinit_data = .); + } >NOINIT } SECTIONS diff --git a/src/components/datetime/DateTimeController.cpp b/src/components/datetime/DateTimeController.cpp index d6aa83c8..77c5be4e 100644 --- a/src/components/datetime/DateTimeController.cpp +++ b/src/components/datetime/DateTimeController.cpp @@ -5,6 +5,10 @@ using namespace Pinetime::Controllers; +void DateTime::SetCurrentTime(std::chrono::time_point t) { + this->currentDateTime = t; +} + void DateTime::SetTime( uint16_t year, uint8_t month, uint8_t day, uint8_t dayOfWeek, uint8_t hour, uint8_t minute, uint8_t second, uint32_t systickCounter) { std::tm tm = { @@ -67,11 +71,12 @@ void DateTime::UpdateTime(uint32_t systickCounter) { // Notify new day to SystemTask if (hour == 0 and not isMidnightAlreadyNotified) { isMidnightAlreadyNotified = true; - if(systemTask != nullptr) + if (systemTask != nullptr) systemTask->PushMessage(System::Messages::OnNewDay); } else if (hour != 0) { isMidnightAlreadyNotified = false; } + BackUpTime = currentDateTime; } const char* DateTime::MonthShortToString() { diff --git a/src/components/datetime/DateTimeController.h b/src/components/datetime/DateTimeController.h index 265d6e9d..ed7b0861 100644 --- a/src/components/datetime/DateTimeController.h +++ b/src/components/datetime/DateTimeController.h @@ -2,6 +2,7 @@ #include #include +extern std::chrono::time_point BackUpTime; namespace Pinetime { namespace System { @@ -74,6 +75,7 @@ namespace Pinetime { } void Register(System::SystemTask* systemTask); + void SetCurrentTime(std::chrono::time_point t); private: uint16_t year = 0; diff --git a/src/main.cpp b/src/main.cpp index d301be67..9c384a8b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -124,7 +124,6 @@ Pinetime::Controllers::FS fs {spiNorFlash}; Pinetime::Controllers::Settings settingsController {fs}; Pinetime::Controllers::MotorController motorController {settingsController}; - Pinetime::Applications::DisplayApp displayApp(lcd, lvgl, touchPanel, @@ -161,6 +160,9 @@ Pinetime::System::SystemTask systemTask(spi, heartRateApp, fs); +uint32_t MAGIC_RAM __attribute__((section(".noinit"))); +std::chrono::time_point BackUpTime __attribute__((section(".noinit"))); + void nrfx_gpiote_evt_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action) { if (pin == pinTouchIrq) { systemTask.OnTouchEvent(); @@ -321,6 +323,13 @@ int main(void) { // retrieve version stored by bootloader Pinetime::BootloaderVersion::SetVersion(NRF_TIMER2->CC[0]); + // Check Magic Ram and reset lost variables + if (MAGIC_RAM == 0xDEADBEEF) { + dateTimeController.SetCurrentTime(BackUpTime); + } else { + MAGIC_RAM = 0xDEADBEEF; + } + lvgl.Init(); systemTask.Start(); From 16502b788f19458b472de74173d92f01aba30b5f Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Thu, 19 Aug 2021 00:14:42 +0000 Subject: [PATCH 2/7] Add MCUBOOT linker changes use better linker constant names --- gcc_nrf52-mcuboot.ld | 12 +++++++++++- gcc_nrf52.ld | 9 +++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/gcc_nrf52-mcuboot.ld b/gcc_nrf52-mcuboot.ld index 0746f491..b1413f16 100644 --- a/gcc_nrf52-mcuboot.ld +++ b/gcc_nrf52-mcuboot.ld @@ -3,14 +3,24 @@ SEARCH_DIR(.) GROUP(-lgcc -lc -lnosys) +NOINIT_SIZE = 0x100; +RAM_SIZE = 64K; + MEMORY { FLASH (rx) : ORIGIN = 0x08020, LENGTH = 0x78000 - RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 0x10000 + RAM (rwx) : ORIGIN = 0x20000000, LENGTH = RAM_SIZE - NOINIT_SIZE + NOINIT (rwx): ORIGIN = ORIGIN(RAM) + LENGTH(RAM), LENGTH = NOINIT_SIZE } SECTIONS { + noinit (NOLOAD): + { + PROVIDE(__start_noinit_data = .); + KEEP(*(.noinit)) + PROVIDE(__stop_noinit_data = .); + } >NOINIT } SECTIONS diff --git a/gcc_nrf52.ld b/gcc_nrf52.ld index b40dc06b..705c3d3b 100644 --- a/gcc_nrf52.ld +++ b/gcc_nrf52.ld @@ -3,13 +3,14 @@ SEARCH_DIR(.) GROUP(-lgcc -lc -lnosys) -NO_INIT_SIZE = 0x100; -RAM_MAX = 64K; +NOINIT_SIZE = 0x100; +RAM_SIZE = 64K; + MEMORY { FLASH (rx) : ORIGIN = 0x00000, LENGTH = 0x78000 - RAM (rwx) : ORIGIN = 0x20000000, LENGTH = RAM_MAX - NO_INIT_SIZE - NOINIT (rwx): ORIGIN = ORIGIN(RAM) + LENGTH(RAM), LENGTH = NO_INIT_SIZE + RAM (rwx) : ORIGIN = 0x20000000, LENGTH = RAM_SIZE - NOINIT_SIZE + NOINIT (rwx): ORIGIN = ORIGIN(RAM) + LENGTH(RAM), LENGTH = NOINIT_SIZE } SECTIONS From 2dd7b8ba2c0c8f756eddd9c86a40497f1b7f7537 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Thu, 19 Aug 2021 00:44:22 +0000 Subject: [PATCH 3/7] Add clearing of noinit segment on bad word Code readability cleanup --- src/components/datetime/DateTimeController.cpp | 2 +- src/components/datetime/DateTimeController.h | 2 +- src/main.cpp | 15 ++++++++++----- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/components/datetime/DateTimeController.cpp b/src/components/datetime/DateTimeController.cpp index 77c5be4e..49953388 100644 --- a/src/components/datetime/DateTimeController.cpp +++ b/src/components/datetime/DateTimeController.cpp @@ -76,7 +76,7 @@ void DateTime::UpdateTime(uint32_t systickCounter) { } else if (hour != 0) { isMidnightAlreadyNotified = false; } - BackUpTime = currentDateTime; + NoInit_BackUpTime = currentDateTime; } const char* DateTime::MonthShortToString() { diff --git a/src/components/datetime/DateTimeController.h b/src/components/datetime/DateTimeController.h index ed7b0861..a20f71c1 100644 --- a/src/components/datetime/DateTimeController.h +++ b/src/components/datetime/DateTimeController.h @@ -2,7 +2,7 @@ #include #include -extern std::chrono::time_point BackUpTime; +extern std::chrono::time_point NoInit_BackUpTime; namespace Pinetime { namespace System { diff --git a/src/main.cpp b/src/main.cpp index 9c384a8b..d5da4283 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -160,8 +160,12 @@ Pinetime::System::SystemTask systemTask(spi, heartRateApp, fs); -uint32_t MAGIC_RAM __attribute__((section(".noinit"))); -std::chrono::time_point BackUpTime __attribute__((section(".noinit"))); +extern uint32_t __start_noinit_data; +extern uint32_t __stop_noinit_data; +static constexpr uint32_t NoInit_MagicValue = 0xDEADBEEF; +uint32_t NoInit_MagicWord __attribute__((section(".noinit"))); +std::chrono::time_point NoInit_BackUpTime __attribute__((section(".noinit"))); + void nrfx_gpiote_evt_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action) { if (pin == pinTouchIrq) { @@ -324,10 +328,11 @@ int main(void) { Pinetime::BootloaderVersion::SetVersion(NRF_TIMER2->CC[0]); // Check Magic Ram and reset lost variables - if (MAGIC_RAM == 0xDEADBEEF) { - dateTimeController.SetCurrentTime(BackUpTime); + if (NoInit_MagicWord == NoInit_MagicValue) { + dateTimeController.SetCurrentTime(NoInit_BackUpTime); } else { - MAGIC_RAM = 0xDEADBEEF; + memset(&__start_noinit_data,0,(uintptr_t)&__stop_noinit_data-(uintptr_t)&__start_noinit_data); + NoInit_MagicWord = NoInit_MagicValue; } lvgl.Init(); From 4f6d7e2c6386ed584a54a513c811cb77b5d320e1 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Thu, 19 Aug 2021 00:51:12 +0000 Subject: [PATCH 4/7] Move Backup to the system task. --- src/components/datetime/DateTimeController.cpp | 1 - src/components/datetime/DateTimeController.h | 1 - src/systemtask/SystemTask.cpp | 1 + src/systemtask/SystemTask.h | 1 + 4 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/datetime/DateTimeController.cpp b/src/components/datetime/DateTimeController.cpp index 49953388..6e426825 100644 --- a/src/components/datetime/DateTimeController.cpp +++ b/src/components/datetime/DateTimeController.cpp @@ -76,7 +76,6 @@ void DateTime::UpdateTime(uint32_t systickCounter) { } else if (hour != 0) { isMidnightAlreadyNotified = false; } - NoInit_BackUpTime = currentDateTime; } const char* DateTime::MonthShortToString() { diff --git a/src/components/datetime/DateTimeController.h b/src/components/datetime/DateTimeController.h index a20f71c1..061c303f 100644 --- a/src/components/datetime/DateTimeController.h +++ b/src/components/datetime/DateTimeController.h @@ -2,7 +2,6 @@ #include #include -extern std::chrono::time_point NoInit_BackUpTime; namespace Pinetime { namespace System { diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index 3553f449..add591e7 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -353,6 +353,7 @@ void SystemTask::Work() { monitor.Process(); uint32_t systick_counter = nrf_rtc_counter_get(portNRF_RTC_REG); dateTimeController.UpdateTime(systick_counter); + NoInit_BackUpTime = dateTimeController.CurrentDateTime(); if (!nrf_gpio_pin_read(pinButton)) watchdog.Kick(); } diff --git a/src/systemtask/SystemTask.h b/src/systemtask/SystemTask.h index ba434298..9293fd78 100644 --- a/src/systemtask/SystemTask.h +++ b/src/systemtask/SystemTask.h @@ -30,6 +30,7 @@ #include "drivers/Watchdog.h" #include "Messages.h" +extern std::chrono::time_point NoInit_BackUpTime; namespace Pinetime { namespace Drivers { class Cst816S; From c9dedfd402c67078ce53ad661889be841633f468 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Thu, 19 Aug 2021 00:51:42 +0000 Subject: [PATCH 5/7] Add some VSCODE stuff to gitignore... Allow settings to stop naggin me that it has added assosiation types --- .gitignore | 6 ++++- .vscode/settings.json | 52 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 6de76a9e..39fb672b 100644 --- a/.gitignore +++ b/.gitignore @@ -38,4 +38,8 @@ Testing/Temporary/ **/.DS_Store # Windows -**/thumbs.db \ No newline at end of file +**/thumbs.db + +#VSCODE +.vscode/.cortex-debug.registers.state.json +.vscode/.cortex-debug.peripherals.state.json diff --git a/.vscode/settings.json b/.vscode/settings.json index 8f0e63f4..f1cc3a81 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -5,5 +5,55 @@ "-DNRF5_SDK_PATH=${env:NRF5_SDK_PATH}", ], "cmake.generator": "Unix Makefiles", - "clang-tidy.buildPath": "build/compile_commands.json" + "clang-tidy.buildPath": "build/compile_commands.json", + "files.associations": { + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "bitset": "cpp", + "cctype": "cpp", + "chrono": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "optional": "cpp", + "random": "cpp", + "ratio": "cpp", + "string": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "fstream": "cpp", + "initializer_list": "cpp", + "iosfwd": "cpp", + "istream": "cpp", + "limits": "cpp", + "new": "cpp", + "ostream": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "cinttypes": "cpp", + "typeinfo": "cpp" + } } From 110c82c225c22685a3f0b7d1d75566a929516637 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Mon, 23 Aug 2021 20:13:26 +0000 Subject: [PATCH 6/7] Move noinit to after BSS segment, Now dynamically allocates noinit area size stores it after the bss before the heap and stack. --- gcc_nrf52-mcuboot.ld | 14 +++++--------- gcc_nrf52.ld | 15 ++++++--------- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/gcc_nrf52-mcuboot.ld b/gcc_nrf52-mcuboot.ld index b1413f16..81b318c5 100644 --- a/gcc_nrf52-mcuboot.ld +++ b/gcc_nrf52-mcuboot.ld @@ -3,25 +3,21 @@ SEARCH_DIR(.) GROUP(-lgcc -lc -lnosys) -NOINIT_SIZE = 0x100; -RAM_SIZE = 64K; - MEMORY { FLASH (rx) : ORIGIN = 0x08020, LENGTH = 0x78000 - RAM (rwx) : ORIGIN = 0x20000000, LENGTH = RAM_SIZE - NOINIT_SIZE - NOINIT (rwx): ORIGIN = ORIGIN(RAM) + LENGTH(RAM), LENGTH = NOINIT_SIZE + RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 64K } SECTIONS { - noinit (NOLOAD): + .noinit(NOLOAD): { PROVIDE(__start_noinit_data = .); - KEEP(*(.noinit)) + *(.noinit) PROVIDE(__stop_noinit_data = .); - } >NOINIT -} + } > RAM +} INSERT AFTER .bss SECTIONS { diff --git a/gcc_nrf52.ld b/gcc_nrf52.ld index 705c3d3b..f9bc5b68 100644 --- a/gcc_nrf52.ld +++ b/gcc_nrf52.ld @@ -3,25 +3,21 @@ SEARCH_DIR(.) GROUP(-lgcc -lc -lnosys) -NOINIT_SIZE = 0x100; -RAM_SIZE = 64K; - MEMORY { FLASH (rx) : ORIGIN = 0x00000, LENGTH = 0x78000 - RAM (rwx) : ORIGIN = 0x20000000, LENGTH = RAM_SIZE - NOINIT_SIZE - NOINIT (rwx): ORIGIN = ORIGIN(RAM) + LENGTH(RAM), LENGTH = NOINIT_SIZE + RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 64K } SECTIONS { - noinit (NOLOAD): + .noinit(NOLOAD): { PROVIDE(__start_noinit_data = .); - KEEP(*(.noinit)) + *(.noinit) PROVIDE(__stop_noinit_data = .); - } >NOINIT -} + } > RAM +} INSERT AFTER .bss SECTIONS { @@ -54,6 +50,7 @@ SECTIONS PROVIDE(__stop_log_filter_data = .); } > RAM + } INSERT AFTER .data; SECTIONS From 28fdc376b23ee2cd87a3ed8925b44a2f6e614ebf Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Thu, 2 Sep 2021 23:01:12 +0000 Subject: [PATCH 7/7] Add some comments --- src/main.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index d5da4283..5d304bc4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -160,9 +160,12 @@ Pinetime::System::SystemTask systemTask(spi, heartRateApp, fs); +/* Variable Declarations for variables in noinit SRAM + Increment NoInit_MagicValue upon adding variables to this area +*/ extern uint32_t __start_noinit_data; extern uint32_t __stop_noinit_data; -static constexpr uint32_t NoInit_MagicValue = 0xDEADBEEF; +static constexpr uint32_t NoInit_MagicValue = 0xDEAD0000; uint32_t NoInit_MagicWord __attribute__((section(".noinit"))); std::chrono::time_point NoInit_BackUpTime __attribute__((section(".noinit"))); @@ -327,10 +330,11 @@ int main(void) { // retrieve version stored by bootloader Pinetime::BootloaderVersion::SetVersion(NRF_TIMER2->CC[0]); - // Check Magic Ram and reset lost variables + if (NoInit_MagicWord == NoInit_MagicValue) { dateTimeController.SetCurrentTime(NoInit_BackUpTime); } else { + //Clear Memory to known state memset(&__start_noinit_data,0,(uintptr_t)&__stop_noinit_data-(uintptr_t)&__start_noinit_data); NoInit_MagicWord = NoInit_MagicValue; }