From 87c6556ad049077ab14398637031ea029b321baf Mon Sep 17 00:00:00 2001 From: JF Date: Fri, 1 May 2020 21:58:31 +0200 Subject: Defer the discovery of services using the system task. --- src/SystemTask/SystemTask.cpp | 16 ++++++++++++++++ src/SystemTask/SystemTask.h | 5 +++-- 2 files changed, 19 insertions(+), 2 deletions(-) (limited to 'src/SystemTask') diff --git a/src/SystemTask/SystemTask.cpp b/src/SystemTask/SystemTask.cpp index 6516f68b..43ac73f9 100644 --- a/src/SystemTask/SystemTask.cpp +++ b/src/SystemTask/SystemTask.cpp @@ -100,9 +100,25 @@ void SystemTask::Work() { case Messages::OnNewNotification: displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::NewNotification); break; + case Messages::BleConnected: + isBleDiscoveryTimerRunning = true; + bleDiscoveryTimer = 5; + break; default: break; } } + + if(isBleDiscoveryTimerRunning) { + if(bleDiscoveryTimer == 0) { + isBleDiscoveryTimerRunning = false; + // Services discovery is deffered from 3 seconds to avoid the conflicts between the host communicating with the + // tharget and vice-versa. I'm not sure if this is the right way to handle this... + nimbleController.StartDiscovery(); + } else { + bleDiscoveryTimer--; + } + } + uint32_t systick_counter = nrf_rtc_counter_get(portNRF_RTC_REG); dateTimeController.UpdateTime(systick_counter); batteryController.Update(); diff --git a/src/SystemTask/SystemTask.h b/src/SystemTask/SystemTask.h index 5eba391b..65cfdd8f 100644 --- a/src/SystemTask/SystemTask.h +++ b/src/SystemTask/SystemTask.h @@ -14,7 +14,7 @@ namespace Pinetime { namespace System { class SystemTask { public: - enum class Messages {GoToSleep, GoToRunning, OnNewTime, OnNewNotification + enum class Messages {GoToSleep, GoToRunning, OnNewTime, OnNewNotification, BleConnected }; SystemTask(Drivers::SpiMaster &spi, Drivers::St7789 &lcd, Drivers::Cst816S &touchPanel, @@ -58,7 +58,8 @@ namespace Pinetime { static void Process(void* instance); void Work(); - + bool isBleDiscoveryTimerRunning = false; + uint8_t bleDiscoveryTimer = 0; }; } -- cgit v1.2.3-70-g09d2 From e20fdfa494ed8b1b6a62b71ca605a6a801e54e4e Mon Sep 17 00:00:00 2001 From: JF Date: Sat, 2 May 2020 14:16:57 +0200 Subject: Add new screen that is displayed during the OTA transfert. --- src/CMakeLists.txt | 2 ++ src/Components/Ble/BleController.cpp | 16 ++++++++++ src/Components/Ble/BleController.h | 12 ++++++++ src/Components/Ble/DfuService.cpp | 13 +++++++- src/Components/Ble/DfuService.h | 9 +++++- src/Components/Ble/NimbleController.cpp | 1 + src/Components/Ble/NimbleController.h | 2 +- src/DisplayApp/DisplayApp.cpp | 12 ++++++++ src/DisplayApp/DisplayApp.h | 2 +- src/DisplayApp/Screens/FirmwareUpdate.cpp | 49 +++++++++++++++++++++++++++++++ src/DisplayApp/Screens/FirmwareUpdate.h | 39 ++++++++++++++++++++++++ src/SystemTask/SystemTask.cpp | 6 ++++ src/SystemTask/SystemTask.h | 3 +- 13 files changed, 161 insertions(+), 5 deletions(-) create mode 100644 src/DisplayApp/Screens/FirmwareUpdate.cpp create mode 100644 src/DisplayApp/Screens/FirmwareUpdate.h (limited to 'src/SystemTask') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 003fec51..2a9614e7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -305,6 +305,7 @@ list(APPEND SOURCE_FILES DisplayApp/Screens/Brightness.cpp DisplayApp/Screens/ScreenList.cpp DisplayApp/Screens/Label.cpp + DisplayApp/Screens/FirmwareUpdate.cpp main.cpp drivers/St7789.cpp drivers/SpiMaster.cpp @@ -354,6 +355,7 @@ set(INCLUDE_FILES DisplayApp/Screens/Brightness.h DisplayApp/Screens/ScreenList.h DisplayApp/Screens/Label.h + DisplayApp/Screens/FirmwareUpdate.h drivers/St7789.h drivers/SpiMaster.h drivers/Watchdog.h diff --git a/src/Components/Ble/BleController.cpp b/src/Components/Ble/BleController.cpp index 5fa51688..2b396e12 100644 --- a/src/Components/Ble/BleController.cpp +++ b/src/Components/Ble/BleController.cpp @@ -12,4 +12,20 @@ void Ble::Disconnect() { isConnected = false; } +void Ble::StartFirmwareUpdate() { + isFirmwareUpdating = true; +} + +void Ble::StopFirmwareUpdate() { + isFirmwareUpdating = false; +} + +void Ble::FirmwareUpdateTotalBytes(uint32_t totalBytes) { + firmwareUpdateTotalBytes = totalBytes; +} + +void Ble::FirmwareUpdateCurrentBytes(uint32_t currentBytes) { + firmwareUpdateCurrentBytes = currentBytes; +} + diff --git a/src/Components/Ble/BleController.h b/src/Components/Ble/BleController.h index f2bd77e0..65a5ef8f 100644 --- a/src/Components/Ble/BleController.h +++ b/src/Components/Ble/BleController.h @@ -12,8 +12,20 @@ namespace Pinetime { bool IsConnected() const {return isConnected;} void Connect(); void Disconnect(); + + void StartFirmwareUpdate(); + void StopFirmwareUpdate(); + void FirmwareUpdateTotalBytes(uint32_t totalBytes); + void FirmwareUpdateCurrentBytes(uint32_t currentBytes); + + bool IsFirmwareUpdating() const { return isFirmwareUpdating; } + uint32_t FirmwareUpdateTotalBytes() const { return firmwareUpdateTotalBytes; } + uint32_t FirmwareUpdateCurrentBytes() const { return firmwareUpdateCurrentBytes; } private: bool isConnected = false; + bool isFirmwareUpdating = false; + uint32_t firmwareUpdateTotalBytes = 0; + uint32_t firmwareUpdateCurrentBytes = 0; }; } diff --git a/src/Components/Ble/DfuService.cpp b/src/Components/Ble/DfuService.cpp index bc96dd9b..2870d7f0 100644 --- a/src/Components/Ble/DfuService.cpp +++ b/src/Components/Ble/DfuService.cpp @@ -1,3 +1,5 @@ +#include +#include #include "DfuService.h" using namespace Pinetime::Controllers; @@ -13,7 +15,9 @@ int DfuServiceCallback(uint16_t conn_handle, uint16_t attr_handle, return dfuService->OnServiceData(conn_handle, attr_handle, ctxt); } -DfuService::DfuService() : +DfuService::DfuService(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::Ble& bleController) : + systemTask{systemTask}, + bleController{bleController}, characteristicDefinition{ { .uuid = (ble_uuid_t *) &packetCharacteristicUuid, @@ -102,6 +106,7 @@ int DfuService::WritePacketHandler(uint16_t connectionHandle, os_mbuf *om) { case States::Data: { nbPacketReceived++; bytesReceived += om->om_len; + bleController.FirmwareUpdateCurrentBytes(bytesReceived); NRF_LOG_INFO("[DFU] -> Bytes received : %d in %d packets", bytesReceived, nbPacketReceived); if((nbPacketReceived % nbPacketsToNotify) == 0) { @@ -139,6 +144,10 @@ int DfuService::ControlPointHandler(uint16_t connectionHandle, os_mbuf *om) { if(imageType == ImageTypes::Application) { NRF_LOG_INFO("[DFU] -> Start DFU, mode = Application"); state = States::Start; + bleController.StartFirmwareUpdate(); + bleController.FirmwareUpdateTotalBytes(175280); + bleController.FirmwareUpdateCurrentBytes(0); + systemTask.PushMessage(Pinetime::System::SystemTask::Messages::BleFirmwareUpdateStarted); return 0; } else { NRF_LOG_INFO("[DFU] -> Start DFU, mode %d not supported!", imageType); @@ -194,6 +203,8 @@ int DfuService::ControlPointHandler(uint16_t connectionHandle, os_mbuf *om) { return 0; } NRF_LOG_INFO("[DFU] -> Activate image and reset!"); + bleController.StopFirmwareUpdate(); + systemTask.PushMessage(Pinetime::System::SystemTask::Messages::BleFirmwareUpdateFinished); return 0; default: return 0; } diff --git a/src/Components/Ble/DfuService.h b/src/Components/Ble/DfuService.h index 1b13c00d..7077bf02 100644 --- a/src/Components/Ble/DfuService.h +++ b/src/Components/Ble/DfuService.h @@ -5,14 +5,21 @@ #include namespace Pinetime { + namespace System { + class SystemTask; + } namespace Controllers { + class Ble; class DfuService { public: - DfuService(); + DfuService(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::Ble& bleController); void Init(); int OnServiceData(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt *context); private: + Pinetime::System::SystemTask& systemTask; + Pinetime::Controllers::Ble& bleController; + static constexpr uint16_t dfuServiceId {0x1530}; static constexpr uint16_t packetCharacteristicId {0x1532}; static constexpr uint16_t controlPointCharacteristicId {0x1531}; diff --git a/src/Components/Ble/NimbleController.cpp b/src/Components/Ble/NimbleController.cpp index f3d271ec..f16d8af0 100644 --- a/src/Components/Ble/NimbleController.cpp +++ b/src/Components/Ble/NimbleController.cpp @@ -29,6 +29,7 @@ NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask, bleController{bleController}, dateTimeController{dateTimeController}, notificationManager{notificationManager}, + dfuService{systemTask, bleController}, currentTimeClient{dateTimeController}, alertNotificationClient{systemTask, notificationManager} { diff --git a/src/Components/Ble/NimbleController.h b/src/Components/Ble/NimbleController.h index 945d3329..c4922bae 100644 --- a/src/Components/Ble/NimbleController.h +++ b/src/Components/Ble/NimbleController.h @@ -34,7 +34,7 @@ namespace Pinetime { Pinetime::Controllers::Ble& bleController; DateTime& dateTimeController; Pinetime::Controllers::NotificationManager& notificationManager; - DfuService dfuService; + Pinetime::Controllers::DfuService dfuService; DeviceInformationService deviceInformationService; CurrentTimeClient currentTimeClient; diff --git a/src/DisplayApp/DisplayApp.cpp b/src/DisplayApp/DisplayApp.cpp index 1b4515e0..c42639c7 100644 --- a/src/DisplayApp/DisplayApp.cpp +++ b/src/DisplayApp/DisplayApp.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include "../SystemTask/SystemTask.h" using namespace Pinetime::Applications; @@ -157,6 +158,17 @@ void DisplayApp::Refresh() { // toggle = true; // } + break; + case Messages::BleFirmwareUpdateStarted: + lvgl.SetFullRefresh(Components::LittleVgl::FullRefreshDirections::Down); + currentScreen.reset(nullptr); + currentScreen.reset(new Screens::FirmwareUpdate(this, bleController)); + + break; + case Messages::BleFirmwareUpdateFinished: + lvgl.SetFullRefresh(Components::LittleVgl::FullRefreshDirections::Down); + currentScreen.reset(nullptr); + currentScreen.reset(new Screens::Clock(this, dateTimeController, batteryController, bleController)); break; } } diff --git a/src/DisplayApp/DisplayApp.h b/src/DisplayApp/DisplayApp.h index 09f0d1cd..b45a0bee 100644 --- a/src/DisplayApp/DisplayApp.h +++ b/src/DisplayApp/DisplayApp.h @@ -30,7 +30,7 @@ namespace Pinetime { public: enum class States {Idle, Running}; enum class Messages : uint8_t {GoToSleep, GoToRunning, UpdateDateTime, UpdateBleConnection, UpdateBatteryLevel, TouchEvent, SwitchScreen,ButtonPushed, - NewNotification + NewNotification, BleFirmwareUpdateStarted, BleFirmwareUpdateFinished }; enum class FullRefreshDirections { None, Up, Down }; diff --git a/src/DisplayApp/Screens/FirmwareUpdate.cpp b/src/DisplayApp/Screens/FirmwareUpdate.cpp new file mode 100644 index 00000000..f3cf42f9 --- /dev/null +++ b/src/DisplayApp/Screens/FirmwareUpdate.cpp @@ -0,0 +1,49 @@ +#include +#include "FirmwareUpdate.h" +#include "../DisplayApp.h" + +using namespace Pinetime::Applications::Screens; +extern lv_font_t jetbrains_mono_extrabold_compressed; +extern lv_font_t jetbrains_mono_bold_20; + + +FirmwareUpdate::FirmwareUpdate(Pinetime::Applications::DisplayApp *app, Pinetime::Controllers::Ble& bleController) : + Screen(app), bleController{bleController} { + + titleLabel = lv_label_create(lv_scr_act(), NULL); + lv_label_set_text(titleLabel, "Firmware update"); + lv_obj_set_auto_realign(titleLabel, true); + lv_obj_align(titleLabel, NULL, LV_ALIGN_IN_TOP_MID, 0, 50); + + bar1 = lv_bar_create(lv_scr_act(), NULL); + lv_obj_set_size(bar1, 200, 30); + lv_obj_align(bar1, NULL, LV_ALIGN_CENTER, 0, 0); + lv_bar_set_anim_time(bar1, 10); + lv_bar_set_range(bar1, 0, 100); + lv_bar_set_value(bar1, 0, LV_ANIM_OFF); + + percentLabel = lv_label_create(lv_scr_act(), NULL); + lv_label_set_text(percentLabel, ""); + lv_obj_set_auto_realign(percentLabel, true); + lv_obj_align(percentLabel, bar1, LV_ALIGN_OUT_TOP_MID, 0, 60); +} + +FirmwareUpdate::~FirmwareUpdate() { + lv_obj_clean(lv_scr_act()); +} + +bool FirmwareUpdate::Refresh() { + float current = bleController.FirmwareUpdateCurrentBytes() / 1024.0f; + float total = bleController.FirmwareUpdateTotalBytes() / 1024.0f; + int16_t pc = (current / total) * 100.0f; + sprintf(percentStr, "%d %%", pc); + lv_label_set_text(percentLabel, percentStr); + + lv_bar_set_value(bar1, pc, LV_ANIM_OFF); + return running; +} + +bool FirmwareUpdate::OnButtonPushed() { + running = false; + return true; +} diff --git a/src/DisplayApp/Screens/FirmwareUpdate.h b/src/DisplayApp/Screens/FirmwareUpdate.h new file mode 100644 index 00000000..a4cbec62 --- /dev/null +++ b/src/DisplayApp/Screens/FirmwareUpdate.h @@ -0,0 +1,39 @@ +#pragma once + +#include +#include +#include +#include "Screen.h" +#include +#include +#include +#include +#include +#include "../Fonts/lcdfont14.h" +#include "../Fonts/lcdfont70.h" +#include "../../Version.h" + +namespace Pinetime { + namespace Applications { + namespace Screens { + + class FirmwareUpdate : public Screen{ + public: + FirmwareUpdate(DisplayApp* app, Pinetime::Controllers::Ble& bleController); + ~FirmwareUpdate() override; + + bool Refresh() override; + bool OnButtonPushed() override; + + private: + Pinetime::Controllers::Ble& bleController; + lv_obj_t * bar1; + lv_obj_t * percentLabel; + lv_obj_t * titleLabel; + char percentStr[10]; + bool running = true; + + }; + } + } +} diff --git a/src/SystemTask/SystemTask.cpp b/src/SystemTask/SystemTask.cpp index 43ac73f9..21298ab6 100644 --- a/src/SystemTask/SystemTask.cpp +++ b/src/SystemTask/SystemTask.cpp @@ -104,6 +104,12 @@ void SystemTask::Work() { isBleDiscoveryTimerRunning = true; bleDiscoveryTimer = 5; break; + case Messages::BleFirmwareUpdateStarted: + displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::BleFirmwareUpdateStarted); + break; + case Messages::BleFirmwareUpdateFinished: + displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::BleFirmwareUpdateFinished); + break; default: break; } } diff --git a/src/SystemTask/SystemTask.h b/src/SystemTask/SystemTask.h index 65cfdd8f..d3498c1f 100644 --- a/src/SystemTask/SystemTask.h +++ b/src/SystemTask/SystemTask.h @@ -14,7 +14,8 @@ namespace Pinetime { namespace System { class SystemTask { public: - enum class Messages {GoToSleep, GoToRunning, OnNewTime, OnNewNotification, BleConnected + enum class Messages {GoToSleep, GoToRunning, OnNewTime, OnNewNotification, BleConnected, + BleFirmwareUpdateStarted, BleFirmwareUpdateFinished }; SystemTask(Drivers::SpiMaster &spi, Drivers::St7789 &lcd, Drivers::Cst816S &touchPanel, -- cgit v1.2.3-70-g09d2 From 0b8e6c3fa20457bce931b1d289f187e46fc68307 Mon Sep 17 00:00:00 2001 From: JF Date: Thu, 7 May 2020 19:53:51 +0200 Subject: Add SPI NOR Flash driver, WIP. --- src/CMakeLists.txt | 4 +++ src/Components/Ble/NimbleController.cpp | 6 ++-- src/SystemTask/SystemTask.cpp | 6 ++-- src/SystemTask/SystemTask.h | 5 +++- src/drivers/Spi.cpp | 29 ++++++++++++++++++ src/drivers/Spi.h | 33 ++++++++++++++++++++ src/drivers/SpiMaster.cpp | 53 ++++++++++++++++++++++++++++----- src/drivers/SpiMaster.h | 5 ++-- src/drivers/SpiNorFlash.cpp | 36 ++++++++++++++++++++++ src/drivers/SpiNorFlash.h | 28 +++++++++++++++++ src/drivers/St7789.cpp | 9 +++--- src/drivers/St7789.h | 6 ++-- src/main.cpp | 16 ++++++---- src/sdk_config.h | 6 ++-- 14 files changed, 212 insertions(+), 30 deletions(-) create mode 100644 src/drivers/Spi.cpp create mode 100644 src/drivers/Spi.h create mode 100644 src/drivers/SpiNorFlash.cpp create mode 100644 src/drivers/SpiNorFlash.h (limited to 'src/SystemTask') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2a9614e7..e8e5726b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -308,7 +308,9 @@ list(APPEND SOURCE_FILES DisplayApp/Screens/FirmwareUpdate.cpp main.cpp drivers/St7789.cpp + drivers/SpiNorFlash.cpp drivers/SpiMaster.cpp + drivers/Spi.cpp drivers/Watchdog.cpp drivers/DebugPins.cpp Components/Battery/BatteryController.cpp @@ -357,7 +359,9 @@ set(INCLUDE_FILES DisplayApp/Screens/Label.h DisplayApp/Screens/FirmwareUpdate.h drivers/St7789.h + drivers/SpiNorFlash.h drivers/SpiMaster.h + drivers/Spi.h drivers/Watchdog.h drivers/DebugPins.h Components/Battery/BatteryController.h diff --git a/src/Components/Ble/NimbleController.cpp b/src/Components/Ble/NimbleController.cpp index 23bd3e2f..1175022e 100644 --- a/src/Components/Ble/NimbleController.cpp +++ b/src/Components/Ble/NimbleController.cpp @@ -122,14 +122,14 @@ void NimbleController::StartAdvertising() { int res; res = ble_gap_adv_set_fields(&fields); - ASSERT(res == 0); +// ASSERT(res == 0); // TODO this one sometimes fails with error 22 (notsync) res = ble_gap_adv_rsp_set_fields(&rsp_fields); - ASSERT(res == 0); +// ASSERT(res == 0); res = ble_gap_adv_start(addrType, NULL, 10000, &adv_params, GAPEventCallback, this); - ASSERT(res == 0); +// ASSERT(res == 0); } int OnAllSvrDisco(uint16_t conn_handle, diff --git a/src/SystemTask/SystemTask.cpp b/src/SystemTask/SystemTask.cpp index 30ea568b..c29a932a 100644 --- a/src/SystemTask/SystemTask.cpp +++ b/src/SystemTask/SystemTask.cpp @@ -14,12 +14,13 @@ using namespace Pinetime::System; -SystemTask::SystemTask(Drivers::SpiMaster &spi, Drivers::St7789 &lcd, Drivers::Cst816S &touchPanel, +SystemTask::SystemTask(Drivers::SpiMaster &spi, Drivers::St7789 &lcd, + Pinetime::Drivers::SpiNorFlash& spiNorFlash, Drivers::Cst816S &touchPanel, Components::LittleVgl &lvgl, Controllers::Battery &batteryController, Controllers::Ble &bleController, Controllers::DateTime &dateTimeController, Pinetime::Controllers::NotificationManager& notificationManager) : - spi{spi}, lcd{lcd}, touchPanel{touchPanel}, lvgl{lvgl}, batteryController{batteryController}, + spi{spi}, lcd{lcd}, spiNorFlash{spiNorFlash}, touchPanel{touchPanel}, lvgl{lvgl}, batteryController{batteryController}, bleController{bleController}, dateTimeController{dateTimeController}, watchdog{}, watchdogView{watchdog}, notificationManager{notificationManager}, nimbleController(*this, bleController,dateTimeController, notificationManager) { @@ -50,6 +51,7 @@ void SystemTask::Work() { spi.Init(); lcd.Init(); + spiNorFlash.Init(); touchPanel.Init(); batteryController.Init(); diff --git a/src/SystemTask/SystemTask.h b/src/SystemTask/SystemTask.h index d3498c1f..53e69fa7 100644 --- a/src/SystemTask/SystemTask.h +++ b/src/SystemTask/SystemTask.h @@ -9,6 +9,7 @@ #include #include #include +#include namespace Pinetime { namespace System { @@ -18,7 +19,8 @@ namespace Pinetime { BleFirmwareUpdateStarted, BleFirmwareUpdateFinished }; - SystemTask(Drivers::SpiMaster &spi, Drivers::St7789 &lcd, Drivers::Cst816S &touchPanel, + SystemTask(Drivers::SpiMaster &spi, Drivers::St7789 &lcd, + Pinetime::Drivers::SpiNorFlash& spiNorFlash, Drivers::Cst816S &touchPanel, Components::LittleVgl &lvgl, Controllers::Battery &batteryController, Controllers::Ble &bleController, Controllers::DateTime &dateTimeController, @@ -35,6 +37,7 @@ namespace Pinetime { Pinetime::Drivers::SpiMaster& spi; Pinetime::Drivers::St7789& lcd; + Pinetime::Drivers::SpiNorFlash& spiNorFlash; Pinetime::Drivers::Cst816S& touchPanel; Pinetime::Components::LittleVgl& lvgl; Pinetime::Controllers::Battery& batteryController; diff --git a/src/drivers/Spi.cpp b/src/drivers/Spi.cpp new file mode 100644 index 00000000..76490aed --- /dev/null +++ b/src/drivers/Spi.cpp @@ -0,0 +1,29 @@ +#include +#include "Spi.h" + +using namespace Pinetime::Drivers; + +Spi::Spi(SpiMaster& spiMaster, uint8_t pinCsn) : + spiMaster{spiMaster}, pinCsn{pinCsn} { + +} + +bool Spi::Write(const uint8_t *data, size_t size) { + return spiMaster.Write(pinCsn, data, size); +} + +bool Spi::Read(uint8_t *data, size_t size) { + return spiMaster.Read(pinCsn, data, size); +} + +void Spi::Sleep() { + // TODO sleep spi + nrf_gpio_cfg_default(pinCsn); +} + +bool Spi::Init() { + nrf_gpio_pin_set(pinCsn); /* disable Set slave select (inactive high) */ + return true; +} + + diff --git a/src/drivers/Spi.h b/src/drivers/Spi.h new file mode 100644 index 00000000..7e155582 --- /dev/null +++ b/src/drivers/Spi.h @@ -0,0 +1,33 @@ +#pragma once +#include +#include +#include +#include +#include +#include + +#include "BufferProvider.h" +#include "SpiMaster.h" + +namespace Pinetime { + namespace Drivers { + class Spi { + public: + Spi(SpiMaster& spiMaster, uint8_t pinCsn); + Spi(const Spi&) = delete; + Spi& operator=(const Spi&) = delete; + Spi(Spi&&) = delete; + Spi& operator=(Spi&&) = delete; + + bool Init(); + bool Write(const uint8_t* data, size_t size); + bool Read(uint8_t* data, size_t size); + void Sleep(); + void Wakeup(); + + private: + SpiMaster& spiMaster; + uint8_t pinCsn; + }; + } +} diff --git a/src/drivers/SpiMaster.cpp b/src/drivers/SpiMaster.cpp index 71986054..7e5bb935 100644 --- a/src/drivers/SpiMaster.cpp +++ b/src/drivers/SpiMaster.cpp @@ -19,8 +19,8 @@ bool SpiMaster::Init() { nrf_gpio_pin_clear(params.pinMOSI); nrf_gpio_cfg_output(params.pinMOSI); nrf_gpio_cfg_input(params.pinMISO, NRF_GPIO_PIN_NOPULL); - nrf_gpio_cfg_output(params.pinCSN); - pinCsn = params.pinCSN; +// nrf_gpio_cfg_output(params.pinCSN); +// pinCsn = params.pinCSN; switch(spi) { case SpiModule::SPI0: spiBaseAddress = NRF_SPIM0; break; @@ -32,7 +32,6 @@ bool SpiMaster::Init() { spiBaseAddress->PSELSCK = params.pinSCK; spiBaseAddress->PSELMOSI = params.pinMOSI; spiBaseAddress->PSELMISO = params.pinMISO; - nrf_gpio_pin_set(pinCsn); /* disable Set slave select (inactive high) */ uint32_t frequency; switch(params.Frequency) { @@ -122,7 +121,7 @@ void SpiMaster::OnEndEvent() { portYIELD_FROM_ISR(xHigherPriorityTaskWoken); } - nrf_gpio_pin_set(pinCsn); + nrf_gpio_pin_set(this->pinCsn); } } @@ -140,20 +139,33 @@ void SpiMaster::PrepareTx(const volatile uint32_t bufferAddress, const volatile spiBaseAddress->EVENTS_END = 0; } -bool SpiMaster::Write(const uint8_t *data, size_t size) { +void SpiMaster::PrepareRx(const volatile uint32_t bufferAddress, const volatile size_t size) { + spiBaseAddress->TXD.PTR = 0; + spiBaseAddress->TXD.MAXCNT = 0; + spiBaseAddress->TXD.LIST = 0; + spiBaseAddress->RXD.PTR = bufferAddress; + spiBaseAddress->RXD.MAXCNT = size; + spiBaseAddress->RXD.LIST = 0; + spiBaseAddress->EVENTS_END = 0; +} + + +bool SpiMaster::Write(uint8_t pinCsn, const uint8_t *data, size_t size) { if(data == nullptr) return false; taskToNotify = xTaskGetCurrentTaskHandle(); while(busy) { asm("nop"); } + this->pinCsn = pinCsn; + if(size == 1) { SetupWorkaroundForFtpan58(spiBaseAddress, 0,0); } else { DisableWorkaroundForFtpan58(spiBaseAddress, 0, 0); } - nrf_gpio_pin_clear(pinCsn); + nrf_gpio_pin_clear(this->pinCsn); currentBufferAddr = (uint32_t)data; currentBufferSize = size; @@ -173,6 +185,33 @@ bool SpiMaster::Write(const uint8_t *data, size_t size) { return true; } +bool SpiMaster::Read(uint8_t pinCsn, uint8_t *data, size_t size) { + while(busy) { + asm("nop"); + } + taskToNotify = nullptr; + + this->pinCsn = pinCsn; + SetupWorkaroundForFtpan58(spiBaseAddress, 0,0); + + nrf_gpio_pin_clear(this->pinCsn); + + currentBufferAddr = 0; + currentBufferSize = 0; + busy = true; + + PrepareRx((uint32_t)data, size); + spiBaseAddress->TASKS_START = 1; + + while (spiBaseAddress->EVENTS_END == 0); + nrf_gpio_pin_set(this->pinCsn); + + busy = false; + + return true; +} + + void SpiMaster::Sleep() { while(spiBaseAddress->ENABLE != 0) { spiBaseAddress->ENABLE = (SPIM_ENABLE_ENABLE_Disabled << SPIM_ENABLE_ENABLE_Pos); @@ -180,7 +219,6 @@ void SpiMaster::Sleep() { nrf_gpio_cfg_default(params.pinSCK); nrf_gpio_cfg_default(params.pinMOSI); nrf_gpio_cfg_default(params.pinMISO); - nrf_gpio_cfg_default(params.pinCSN); } void SpiMaster::Wakeup() { @@ -188,3 +226,4 @@ void SpiMaster::Wakeup() { } + diff --git a/src/drivers/SpiMaster.h b/src/drivers/SpiMaster.h index 362f480c..24b39b97 100644 --- a/src/drivers/SpiMaster.h +++ b/src/drivers/SpiMaster.h @@ -22,7 +22,6 @@ namespace Pinetime { uint8_t pinSCK; uint8_t pinMOSI; uint8_t pinMISO; - uint8_t pinCSN; }; SpiMaster(const SpiModule spi, const Parameters& params); @@ -32,7 +31,8 @@ namespace Pinetime { SpiMaster& operator=(SpiMaster&&) = delete; bool Init(); - bool Write(const uint8_t* data, size_t size); + bool Write(uint8_t pinCsn, const uint8_t* data, size_t size); + bool Read(uint8_t pinCsn, uint8_t* data, size_t size); void OnStartedEvent(); void OnEndEvent(); @@ -44,6 +44,7 @@ namespace Pinetime { void SetupWorkaroundForFtpan58(NRF_SPIM_Type *spim, uint32_t ppi_channel, uint32_t gpiote_channel); void DisableWorkaroundForFtpan58(NRF_SPIM_Type *spim, uint32_t ppi_channel, uint32_t gpiote_channel); void PrepareTx(const volatile uint32_t bufferAddress, const volatile size_t size); + void PrepareRx(const volatile uint32_t bufferAddress, const volatile size_t size); NRF_SPIM_Type * spiBaseAddress; uint8_t pinCsn; diff --git a/src/drivers/SpiNorFlash.cpp b/src/drivers/SpiNorFlash.cpp new file mode 100644 index 00000000..d19548ee --- /dev/null +++ b/src/drivers/SpiNorFlash.cpp @@ -0,0 +1,36 @@ +#include +#include +#include +#include "SpiNorFlash.h" +#include "Spi.h" + +using namespace Pinetime::Drivers; + +SpiNorFlash::SpiNorFlash(Spi& spi) : spi{spi} { + +} + +void SpiNorFlash::Init() { + uint8_t cmd = 0x9F; + spi.Write(&cmd, 1); + + uint8_t data[3]; + data[0] = 0; + data[1] = 0; + data[2] = 0; + spi.Read(data, 3); + + NRF_LOG_INFO("Manufacturer : %d, Device : %d", data[0], (data[1] + (data[2]<<8))); +} + +void SpiNorFlash::Uninit() { + +} + +void SpiNorFlash::Sleep() { + +} + +void SpiNorFlash::Wakeup() { + +} diff --git a/src/drivers/SpiNorFlash.h b/src/drivers/SpiNorFlash.h new file mode 100644 index 00000000..839a1c2a --- /dev/null +++ b/src/drivers/SpiNorFlash.h @@ -0,0 +1,28 @@ +#pragma once +#include + +namespace Pinetime { + namespace Drivers { + class Spi; + class SpiNorFlash { + public: + explicit SpiNorFlash(Spi& spi); + SpiNorFlash(const SpiNorFlash&) = delete; + SpiNorFlash& operator=(const SpiNorFlash&) = delete; + SpiNorFlash(SpiNorFlash&&) = delete; + SpiNorFlash& operator=(SpiNorFlash&&) = delete; + + void Init(); + void Uninit(); + + + void Sleep(); + void Wakeup(); + private: + Spi& spi; + + }; + } +} + + diff --git a/src/drivers/St7789.cpp b/src/drivers/St7789.cpp index db7c27e2..09269afd 100644 --- a/src/drivers/St7789.cpp +++ b/src/drivers/St7789.cpp @@ -1,16 +1,17 @@ #include #include #include "St7789.h" -#include "SpiMaster.h" +#include "Spi.h" using namespace Pinetime::Drivers; -St7789::St7789(SpiMaster &spiMaster, uint8_t pinDataCommand) : spi{spiMaster}, pinDataCommand{pinDataCommand} { +St7789::St7789(Spi &spi, uint8_t pinDataCommand) : spi{spi}, pinDataCommand{pinDataCommand} { } void St7789::Init() { + spi.Init(); nrf_gpio_cfg_output(pinDataCommand); nrf_gpio_cfg_output(26); nrf_gpio_pin_set(26); @@ -173,11 +174,11 @@ void St7789::HardwareReset() { void St7789::Sleep() { SleepIn(); nrf_gpio_cfg_default(pinDataCommand); - spi.Sleep(); +// spi.Sleep(); // TODO sleep SPI } void St7789::Wakeup() { - spi.Wakeup(); +// spi.Wakeup(); // TODO wake up SPI nrf_gpio_cfg_output(pinDataCommand); // TODO why do we need to reset the controller? diff --git a/src/drivers/St7789.h b/src/drivers/St7789.h index 3721b184..0b94cf24 100644 --- a/src/drivers/St7789.h +++ b/src/drivers/St7789.h @@ -3,10 +3,10 @@ namespace Pinetime { namespace Drivers { - class SpiMaster; + class Spi; class St7789 { public: - explicit St7789(SpiMaster& spiMaster, uint8_t pinDataCommand); + explicit St7789(Spi& spi, uint8_t pinDataCommand); St7789(const St7789&) = delete; St7789& operator=(const St7789&) = delete; St7789(St7789&&) = delete; @@ -29,7 +29,7 @@ namespace Pinetime { void Sleep(); void Wakeup(); private: - SpiMaster& spi; + Spi& spi; uint8_t pinDataCommand; uint8_t verticalScrollingStartAddress = 0; diff --git a/src/main.cpp b/src/main.cpp index e0e9b65e..a48395d0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,6 +12,7 @@ #include "Components/Ble/BleController.h" #include #include +#include #include #include #include @@ -38,7 +39,8 @@ Pinetime::Logging::DummyLogger logger; static constexpr uint8_t pinSpiSck = 2; static constexpr uint8_t pinSpiMosi = 3; static constexpr uint8_t pinSpiMiso = 4; -static constexpr uint8_t pinSpiCsn = 25; +static constexpr uint8_t pinSpiFlashCsn = 5; +static constexpr uint8_t pinLcdCsn = 25; static constexpr uint8_t pinLcdDataCommand = 18; Pinetime::Drivers::SpiMaster spi{Pinetime::Drivers::SpiMaster::SpiModule::SPI0, { @@ -47,11 +49,15 @@ Pinetime::Drivers::SpiMaster spi{Pinetime::Drivers::SpiMaster::SpiModule::SPI0, Pinetime::Drivers::SpiMaster::Frequencies::Freq8Mhz, pinSpiSck, pinSpiMosi, - pinSpiMiso, - pinSpiCsn + pinSpiMiso } }; -Pinetime::Drivers::St7789 lcd {spi, pinLcdDataCommand}; + +Pinetime::Drivers::Spi lcdSpi {spi, pinLcdCsn}; +Pinetime::Drivers::St7789 lcd {lcdSpi, pinLcdDataCommand}; + +Pinetime::Drivers::Spi flashSpi {spi, pinSpiFlashCsn}; +Pinetime::Drivers::SpiNorFlash spiNorFlash {flashSpi}; Pinetime::Drivers::Cst816S touchPanel {}; Pinetime::Components::LittleVgl lvgl {lcd, touchPanel}; @@ -207,7 +213,7 @@ int main(void) { debounceTimer = xTimerCreate ("debounceTimer", 200, pdFALSE, (void *) 0, DebounceTimerCallback); - systemTask.reset(new Pinetime::System::SystemTask(spi, lcd, touchPanel, lvgl, batteryController, bleController, + systemTask.reset(new Pinetime::System::SystemTask(spi, lcd, spiNorFlash, touchPanel, lvgl, batteryController, bleController, dateTimeController, notificationManager)); systemTask->Start(); nimble_port_init(); diff --git a/src/sdk_config.h b/src/sdk_config.h index a63eb6fb..244b21bd 100644 --- a/src/sdk_config.h +++ b/src/sdk_config.h @@ -8460,15 +8460,15 @@ // NRF_LOG_ENABLED - nrf_log - Logger //========================================================== #ifndef NRF_LOG_ENABLED -#define NRF_LOG_ENABLED 0 +#define NRF_LOG_ENABLED 1 #endif #ifndef NRF_LOG_BACKEND_RTT_ENABLED -#define NRF_LOG_BACKEND_RTT_ENABLED 0 +#define NRF_LOG_BACKEND_RTT_ENABLED 1 #endif #ifndef NRF_LOG_BACKEND_SERIAL_USES_RTT -#define NRF_LOG_BACKEND_SERIAL_USES_RTT 0 +#define NRF_LOG_BACKEND_SERIAL_USES_RTT 1 #endif // Log message pool - Configuration of log message pool -- cgit v1.2.3-70-g09d2 From ee05577dd62c64d0e6a2e497b75710c7a1351557 Mon Sep 17 00:00:00 2001 From: JF Date: Mon, 11 May 2020 18:50:37 +0200 Subject: Fix race conditions on SPI and integrate the SPI NOR Flash driver into DFUService (WIP) --- src/Components/Ble/DfuService.cpp | 17 ++++- src/Components/Ble/DfuService.h | 7 ++- src/Components/Ble/NimbleController.cpp | 6 +- src/Components/Ble/NimbleController.h | 8 ++- src/DisplayApp/LittleVgl.cpp | 3 + src/SystemTask/SystemTask.cpp | 14 ++--- src/drivers/Spi.cpp | 11 +++- src/drivers/Spi.h | 3 +- src/drivers/SpiMaster.cpp | 85 +++++++++++++++++++------ src/drivers/SpiMaster.h | 11 +++- src/drivers/SpiNorFlash.cpp | 108 +++++++++++++++++++++++++++++--- src/drivers/SpiNorFlash.h | 32 ++++++++++ 12 files changed, 257 insertions(+), 48 deletions(-) (limited to 'src/SystemTask') diff --git a/src/Components/Ble/DfuService.cpp b/src/Components/Ble/DfuService.cpp index 22983a83..0c8d357c 100644 --- a/src/Components/Ble/DfuService.cpp +++ b/src/Components/Ble/DfuService.cpp @@ -15,9 +15,10 @@ int DfuServiceCallback(uint16_t conn_handle, uint16_t attr_handle, return dfuService->OnServiceData(conn_handle, attr_handle, ctxt); } -DfuService::DfuService(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::Ble& bleController) : +DfuService::DfuService(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::Ble& bleController, Pinetime::Drivers::SpiNorFlash& spiNorFlash) : systemTask{systemTask}, bleController{bleController}, + spiNorFlash{spiNorFlash}, characteristicDefinition{ { .uuid = (ble_uuid_t *) &packetCharacteristicUuid, @@ -104,6 +105,15 @@ int DfuService::WritePacketHandler(uint16_t connectionHandle, os_mbuf *om) { applicationSize = om->om_data[8] + (om->om_data[9] << 8) + (om->om_data[10] << 16) + (om->om_data[11] << 24); NRF_LOG_INFO("[DFU] -> Start data received : SD size : %d, BT size : %d, app size : %d", softdeviceSize, bootloaderSize, applicationSize); + for(int erased = 0; erased < applicationSize; erased += 0x1000) { + spiNorFlash.SectorErase(erased); + + auto p = spiNorFlash.ProgramFailed(); + auto e = spiNorFlash.EraseFailed(); + NRF_LOG_INFO("[DFU] Erasing sector %d - %d-%d", erased, p, e); + + } + uint8_t data[] {16, 1, 1}; SendNotification(connectionHandle, data, 3); state = States::Init; @@ -128,10 +138,15 @@ int DfuService::WritePacketHandler(uint16_t connectionHandle, os_mbuf *om) { case States::Data: { nbPacketReceived++; + + spiNorFlash.Write(bytesReceived, om->om_data, om->om_len); + bytesReceived += om->om_len; bleController.FirmwareUpdateCurrentBytes(bytesReceived); NRF_LOG_INFO("[DFU] -> Bytes received : %d in %d packets", bytesReceived, nbPacketReceived); + + if((nbPacketReceived % nbPacketsToNotify) == 0) { uint8_t data[5]{static_cast(Opcodes::PacketReceiptNotification), (uint8_t)(bytesReceived&0x000000FFu),(uint8_t)(bytesReceived>>8u), (uint8_t)(bytesReceived>>16u),(uint8_t)(bytesReceived>>24u) }; diff --git a/src/Components/Ble/DfuService.h b/src/Components/Ble/DfuService.h index f59ba2c9..1406d6ad 100644 --- a/src/Components/Ble/DfuService.h +++ b/src/Components/Ble/DfuService.h @@ -8,17 +8,22 @@ namespace Pinetime { namespace System { class SystemTask; } + namespace Drivers { + class SpiNorFlash; + } namespace Controllers { class Ble; class DfuService { public: - DfuService(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::Ble& bleController); + DfuService(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::Ble& bleController, + Pinetime::Drivers::SpiNorFlash& spiNorFlash); void Init(); int OnServiceData(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt *context); private: Pinetime::System::SystemTask& systemTask; Pinetime::Controllers::Ble& bleController; + Pinetime::Drivers::SpiNorFlash& spiNorFlash; static constexpr uint16_t dfuServiceId {0x1530}; static constexpr uint16_t packetCharacteristicId {0x1532}; diff --git a/src/Components/Ble/NimbleController.cpp b/src/Components/Ble/NimbleController.cpp index 1175022e..d2ee4801 100644 --- a/src/Components/Ble/NimbleController.cpp +++ b/src/Components/Ble/NimbleController.cpp @@ -24,12 +24,14 @@ using namespace Pinetime::Controllers; NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::Ble& bleController, DateTime& dateTimeController, - Pinetime::Controllers::NotificationManager& notificationManager) : + Pinetime::Controllers::NotificationManager& notificationManager, + Pinetime::Drivers::SpiNorFlash& spiNorFlash) : systemTask{systemTask}, bleController{bleController}, dateTimeController{dateTimeController}, notificationManager{notificationManager}, - dfuService{systemTask, bleController}, + spiNorFlash{spiNorFlash}, + dfuService{systemTask, bleController, spiNorFlash}, currentTimeClient{dateTimeController}, alertNotificationClient{systemTask, notificationManager} { diff --git a/src/Components/Ble/NimbleController.h b/src/Components/Ble/NimbleController.h index c4922bae..9f74be2b 100644 --- a/src/Components/Ble/NimbleController.h +++ b/src/Components/Ble/NimbleController.h @@ -8,12 +8,17 @@ #include namespace Pinetime { + namespace Drivers { + class SpiNorFlash; + } namespace Controllers { class DateTime; class NimbleController { public: - NimbleController(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::Ble& bleController, DateTime& dateTimeController, Pinetime::Controllers::NotificationManager& notificationManager); + NimbleController(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::Ble& bleController, + DateTime& dateTimeController, Pinetime::Controllers::NotificationManager& notificationManager, + Pinetime::Drivers::SpiNorFlash& spiNorFlash); void Init(); void StartAdvertising(); int OnGAPEvent(ble_gap_event *event); @@ -34,6 +39,7 @@ namespace Pinetime { Pinetime::Controllers::Ble& bleController; DateTime& dateTimeController; Pinetime::Controllers::NotificationManager& notificationManager; + Pinetime::Drivers::SpiNorFlash& spiNorFlash; Pinetime::Controllers::DfuService dfuService; DeviceInformationService deviceInformationService; diff --git a/src/DisplayApp/LittleVgl.cpp b/src/DisplayApp/LittleVgl.cpp index b1b894f7..7b6cda94 100644 --- a/src/DisplayApp/LittleVgl.cpp +++ b/src/DisplayApp/LittleVgl.cpp @@ -74,6 +74,9 @@ void LittleVgl::SetFullRefresh(FullRefreshDirections direction) { void LittleVgl::FlushDisplay(const lv_area_t *area, lv_color_t *color_p) { ulTaskNotifyTake(pdTRUE, 500); + // NOtification is still needed (even if there is a mutex on SPI) because of the DataCommand pin + // which cannot be set/clear during a transfert. + // TODO refactore and remove duplicated code diff --git a/src/SystemTask/SystemTask.cpp b/src/SystemTask/SystemTask.cpp index c29a932a..022cade4 100644 --- a/src/SystemTask/SystemTask.cpp +++ b/src/SystemTask/SystemTask.cpp @@ -23,7 +23,7 @@ SystemTask::SystemTask(Drivers::SpiMaster &spi, Drivers::St7789 &lcd, spi{spi}, lcd{lcd}, spiNorFlash{spiNorFlash}, touchPanel{touchPanel}, lvgl{lvgl}, batteryController{batteryController}, bleController{bleController}, dateTimeController{dateTimeController}, watchdog{}, watchdogView{watchdog}, notificationManager{notificationManager}, - nimbleController(*this, bleController,dateTimeController, notificationManager) { + nimbleController(*this, bleController,dateTimeController, notificationManager, spiNorFlash) { systemTaksMsgQueue = xQueueCreate(10, 1); } @@ -39,19 +39,17 @@ void SystemTask::Process(void *instance) { } void SystemTask::Work() { - watchdog.Setup(7); - watchdog.Start(); +// watchdog.Setup(7); +// watchdog.Start(); NRF_LOG_INFO("Last reset reason : %s", Pinetime::Drivers::Watchdog::ResetReasonToString(watchdog.ResetReason())); APP_GPIOTE_INIT(2); -/* BLE */ + spi.Init(); + spiNorFlash.Init(); nimbleController.Init(); nimbleController.StartAdvertising(); -/* /BLE*/ - - spi.Init(); lcd.Init(); - spiNorFlash.Init(); + touchPanel.Init(); batteryController.Init(); diff --git a/src/drivers/Spi.cpp b/src/drivers/Spi.cpp index 76490aed..ec3a5e94 100644 --- a/src/drivers/Spi.cpp +++ b/src/drivers/Spi.cpp @@ -5,15 +5,16 @@ using namespace Pinetime::Drivers; Spi::Spi(SpiMaster& spiMaster, uint8_t pinCsn) : spiMaster{spiMaster}, pinCsn{pinCsn} { - + nrf_gpio_cfg_output(pinCsn); + nrf_gpio_pin_set(pinCsn); } bool Spi::Write(const uint8_t *data, size_t size) { return spiMaster.Write(pinCsn, data, size); } -bool Spi::Read(uint8_t *data, size_t size) { - return spiMaster.Read(pinCsn, data, size); +bool Spi::Read(uint8_t* cmd, size_t cmdSize, uint8_t *data, size_t dataSize) { + return spiMaster.Read(pinCsn, cmd, cmdSize, data, dataSize); } void Spi::Sleep() { @@ -26,4 +27,8 @@ bool Spi::Init() { return true; } +bool Spi::WriteCmdAndBuffer(uint8_t *cmd, size_t cmdSize, uint8_t *data, size_t dataSize) { + return spiMaster.WriteCmdAndBuffer(pinCsn, cmd, cmdSize, data, dataSize); +} + diff --git a/src/drivers/Spi.h b/src/drivers/Spi.h index 7e155582..bee39af2 100644 --- a/src/drivers/Spi.h +++ b/src/drivers/Spi.h @@ -21,7 +21,8 @@ namespace Pinetime { bool Init(); bool Write(const uint8_t* data, size_t size); - bool Read(uint8_t* data, size_t size); + bool Read(uint8_t* cmd, size_t cmdSize, uint8_t *data, size_t dataSize); + bool WriteCmdAndBuffer(uint8_t* cmd, size_t cmdSize, uint8_t *data, size_t dataSize); void Sleep(); void Wakeup(); diff --git a/src/drivers/SpiMaster.cpp b/src/drivers/SpiMaster.cpp index 7e5bb935..4d44a435 100644 --- a/src/drivers/SpiMaster.cpp +++ b/src/drivers/SpiMaster.cpp @@ -9,7 +9,8 @@ using namespace Pinetime::Drivers; SpiMaster::SpiMaster(const SpiMaster::SpiModule spi, const SpiMaster::Parameters ¶ms) : spi{spi}, params{params} { - + mutex = xSemaphoreCreateBinary(); + ASSERT(mutex != NULL); } bool SpiMaster::Init() { @@ -67,6 +68,8 @@ bool SpiMaster::Init() { NRFX_IRQ_PRIORITY_SET(SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQn,2); NRFX_IRQ_ENABLE(SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQn); + + xSemaphoreGive(mutex); return true; } @@ -93,13 +96,17 @@ void SpiMaster::DisableWorkaroundForFtpan58(NRF_SPIM_Type *spim, uint32_t ppi_ch NRF_PPI->CH[ppi_channel].EEP = 0; NRF_PPI->CH[ppi_channel].TEP = 0; NRF_PPI->CHENSET = ppi_channel; + spiBaseAddress->EVENTS_END = 0; spim->INTENSET = (1<<6); spim->INTENSET = (1<<1); spim->INTENSET = (1<<19); } void SpiMaster::OnEndEvent() { - if(!busy) return; + if(currentBufferAddr == 0) { + asm("nop"); + return; + } auto s = currentBufferSize; if(s > 0) { @@ -112,7 +119,7 @@ void SpiMaster::OnEndEvent() { } else { uint8_t* buffer = nullptr; size_t size = 0; - busy = false; + if(taskToNotify != nullptr) { @@ -122,11 +129,14 @@ void SpiMaster::OnEndEvent() { } nrf_gpio_pin_set(this->pinCsn); + currentBufferAddr = 0; + BaseType_t xHigherPriorityTaskWoken = pdFALSE; + xSemaphoreGiveFromISR(mutex, &xHigherPriorityTaskWoken); + portYIELD_FROM_ISR(xHigherPriorityTaskWoken); } } void SpiMaster::OnStartedEvent() { - if(!busy) return; } void SpiMaster::PrepareTx(const volatile uint32_t bufferAddress, const volatile size_t size) { @@ -139,7 +149,7 @@ void SpiMaster::PrepareTx(const volatile uint32_t bufferAddress, const volatile spiBaseAddress->EVENTS_END = 0; } -void SpiMaster::PrepareRx(const volatile uint32_t bufferAddress, const volatile size_t size) { +void SpiMaster::PrepareRx(const volatile uint32_t cmdAddress, const volatile size_t cmdSize, const volatile uint32_t bufferAddress, const volatile size_t size) { spiBaseAddress->TXD.PTR = 0; spiBaseAddress->TXD.MAXCNT = 0; spiBaseAddress->TXD.LIST = 0; @@ -152,10 +162,10 @@ void SpiMaster::PrepareRx(const volatile uint32_t bufferAddress, const volatile bool SpiMaster::Write(uint8_t pinCsn, const uint8_t *data, size_t size) { if(data == nullptr) return false; + auto ok = xSemaphoreTake(mutex, portMAX_DELAY); + ASSERT(ok == true); taskToNotify = xTaskGetCurrentTaskHandle(); - while(busy) { - asm("nop"); - } + this->pinCsn = pinCsn; @@ -169,7 +179,6 @@ bool SpiMaster::Write(uint8_t pinCsn, const uint8_t *data, size_t size) { currentBufferAddr = (uint32_t)data; currentBufferSize = size; - busy = true; auto currentSize = std::min((size_t)255, (size_t)currentBufferSize); PrepareTx(currentBufferAddr, currentSize); @@ -179,34 +188,42 @@ bool SpiMaster::Write(uint8_t pinCsn, const uint8_t *data, size_t size) { if(size == 1) { while (spiBaseAddress->EVENTS_END == 0); - busy = false; + nrf_gpio_pin_set(this->pinCsn); + currentBufferAddr = 0; + xSemaphoreGive(mutex); } return true; } -bool SpiMaster::Read(uint8_t pinCsn, uint8_t *data, size_t size) { - while(busy) { - asm("nop"); - } +bool SpiMaster::Read(uint8_t pinCsn, uint8_t* cmd, size_t cmdSize, uint8_t *data, size_t dataSize) { + xSemaphoreTake(mutex, portMAX_DELAY); + taskToNotify = nullptr; this->pinCsn = pinCsn; - SetupWorkaroundForFtpan58(spiBaseAddress, 0,0); + DisableWorkaroundForFtpan58(spiBaseAddress, 0,0); + spiBaseAddress->INTENCLR = (1<<6); + spiBaseAddress->INTENCLR = (1<<1); + spiBaseAddress->INTENCLR = (1<<19); nrf_gpio_pin_clear(this->pinCsn); + currentBufferAddr = 0; currentBufferSize = 0; - busy = true; - PrepareRx((uint32_t)data, size); + PrepareTx((uint32_t)cmd, cmdSize); + spiBaseAddress->TASKS_START = 1; + while (spiBaseAddress->EVENTS_END == 0); + + PrepareRx((uint32_t)cmd, cmdSize, (uint32_t)data, dataSize); spiBaseAddress->TASKS_START = 1; while (spiBaseAddress->EVENTS_END == 0); nrf_gpio_pin_set(this->pinCsn); - busy = false; + xSemaphoreGive(mutex); return true; } @@ -225,5 +242,37 @@ void SpiMaster::Wakeup() { Init(); } +bool SpiMaster::WriteCmdAndBuffer(uint8_t pinCsn, uint8_t *cmd, size_t cmdSize, uint8_t *data, size_t dataSize) { + xSemaphoreTake(mutex, portMAX_DELAY); + + taskToNotify = nullptr; + + this->pinCsn = pinCsn; + DisableWorkaroundForFtpan58(spiBaseAddress, 0,0); + spiBaseAddress->INTENCLR = (1<<6); + spiBaseAddress->INTENCLR = (1<<1); + spiBaseAddress->INTENCLR = (1<<19); + + nrf_gpio_pin_clear(this->pinCsn); + + + currentBufferAddr = 0; + currentBufferSize = 0; + + PrepareTx((uint32_t)cmd, cmdSize); + spiBaseAddress->TASKS_START = 1; + while (spiBaseAddress->EVENTS_END == 0); + + PrepareTx((uint32_t)data, dataSize); + spiBaseAddress->TASKS_START = 1; + + while (spiBaseAddress->EVENTS_END == 0); + nrf_gpio_pin_set(this->pinCsn); + + xSemaphoreGive(mutex); + + return true; +} + diff --git a/src/drivers/SpiMaster.h b/src/drivers/SpiMaster.h index 24b39b97..7b35dfc8 100644 --- a/src/drivers/SpiMaster.h +++ b/src/drivers/SpiMaster.h @@ -5,6 +5,7 @@ #include #include #include +#include #include "BufferProvider.h" namespace Pinetime { @@ -32,7 +33,9 @@ namespace Pinetime { bool Init(); bool Write(uint8_t pinCsn, const uint8_t* data, size_t size); - bool Read(uint8_t pinCsn, uint8_t* data, size_t size); + bool Read(uint8_t pinCsn, uint8_t* cmd, size_t cmdSize, uint8_t *data, size_t dataSize); + + bool WriteCmdAndBuffer(uint8_t pinCsn, uint8_t* cmd, size_t cmdSize, uint8_t *data, size_t dataSize); void OnStartedEvent(); void OnEndEvent(); @@ -44,7 +47,7 @@ namespace Pinetime { void SetupWorkaroundForFtpan58(NRF_SPIM_Type *spim, uint32_t ppi_channel, uint32_t gpiote_channel); void DisableWorkaroundForFtpan58(NRF_SPIM_Type *spim, uint32_t ppi_channel, uint32_t gpiote_channel); void PrepareTx(const volatile uint32_t bufferAddress, const volatile size_t size); - void PrepareRx(const volatile uint32_t bufferAddress, const volatile size_t size); + void PrepareRx(const volatile uint32_t cmdAddress, const volatile size_t cmdSize, const volatile uint32_t bufferAddress, const volatile size_t size); NRF_SPIM_Type * spiBaseAddress; uint8_t pinCsn; @@ -52,10 +55,12 @@ namespace Pinetime { SpiMaster::SpiModule spi; SpiMaster::Parameters params; - volatile bool busy = false; +// volatile bool busy = false; volatile uint32_t currentBufferAddr = 0; volatile size_t currentBufferSize = 0; volatile TaskHandle_t taskToNotify; + + SemaphoreHandle_t mutex; }; } } diff --git a/src/drivers/SpiNorFlash.cpp b/src/drivers/SpiNorFlash.cpp index d19548ee..8fbb53a1 100644 --- a/src/drivers/SpiNorFlash.cpp +++ b/src/drivers/SpiNorFlash.cpp @@ -11,16 +11,8 @@ SpiNorFlash::SpiNorFlash(Spi& spi) : spi{spi} { } void SpiNorFlash::Init() { - uint8_t cmd = 0x9F; - spi.Write(&cmd, 1); - - uint8_t data[3]; - data[0] = 0; - data[1] = 0; - data[2] = 0; - spi.Read(data, 3); - - NRF_LOG_INFO("Manufacturer : %d, Device : %d", data[0], (data[1] + (data[2]<<8))); + auto id = ReadIdentificaion(); + NRF_LOG_INFO("[SPI FLASH] Manufacturer : %d, Memory type : %d, memory density : %d", id.manufacturer, id.type, id.density); } void SpiNorFlash::Uninit() { @@ -34,3 +26,99 @@ void SpiNorFlash::Sleep() { void SpiNorFlash::Wakeup() { } + +SpiNorFlash::Identification SpiNorFlash::ReadIdentificaion() { + auto cmd = static_cast(Commands::ReadIdentification); + Identification identification; + spi.Read(&cmd, 1, reinterpret_cast(&identification), sizeof(Identification)); + return identification; +} + +uint8_t SpiNorFlash::ReadStatusRegister() { + auto cmd = static_cast(Commands::ReadStatusRegister); + uint8_t status; + spi.Read(&cmd, sizeof(cmd), &status, sizeof(uint8_t)); + return status; +} + +bool SpiNorFlash::WriteInProgress() { + return (ReadStatusRegister() & 0x01u) == 0x01u; +} + +bool SpiNorFlash::WriteEnabled() { + return (ReadStatusRegister() & 0x02u) == 0x02u; +} + +uint8_t SpiNorFlash::ReadConfigurationRegister() { + auto cmd = static_cast(Commands::ReadConfigurationRegister); + uint8_t status; + spi.Read(&cmd, sizeof(cmd), &status, sizeof(uint8_t)); + return status; +} + +void SpiNorFlash::Read(uint32_t address, uint8_t *buffer, size_t size) { + static constexpr uint8_t cmdSize = 4; + uint8_t cmd[cmdSize] = { static_cast(Commands::Read), (uint8_t)(address >> 16U), (uint8_t)(address >> 8U), + (uint8_t)address }; + spi.Read(reinterpret_cast(&cmd), cmdSize, buffer, size); +} + +void SpiNorFlash::WriteEnable() { + auto cmd = static_cast(Commands::WriteEnable); + spi.Read(&cmd, sizeof(cmd), nullptr, 0); +} + +void SpiNorFlash::SectorErase(uint32_t sectorAddress) { + static constexpr uint8_t cmdSize = 4; + uint8_t cmd[cmdSize] = { static_cast(Commands::SectorErase), (uint8_t)(sectorAddress >> 16U), (uint8_t)(sectorAddress >> 8U), + (uint8_t)sectorAddress }; + + WriteEnable(); + while(!WriteEnabled()) vTaskDelay(1); + + spi.Read(reinterpret_cast(&cmd), cmdSize, nullptr, 0); + + while(WriteInProgress()) vTaskDelay(1); +} + +uint8_t SpiNorFlash::ReadSecurityRegister() { + auto cmd = static_cast(Commands::ReadSecurityRegister); + uint8_t status; + spi.Read(&cmd, sizeof(cmd), &status, sizeof(uint8_t)); + return status; +} + +bool SpiNorFlash::ProgramFailed() { + return (ReadSecurityRegister() & 0x20u) == 0x20u; +} + +bool SpiNorFlash::EraseFailed() { + return (ReadSecurityRegister() & 0x40u) == 0x40u; +} + +void SpiNorFlash::Write(uint32_t address, uint8_t *buffer, size_t size) { + static constexpr uint8_t cmdSize = 4; + + size_t len = size; + uint32_t addr = address; + uint8_t* b = buffer; + while(len > 0) { + uint32_t pageLimit = (addr & ~(pageSize - 1u)) + pageSize; + uint32_t toWrite = pageLimit - addr > len ? len : pageLimit - addr; + + uint8_t cmd[cmdSize] = { static_cast(Commands::PageProgram), (uint8_t)(addr >> 16U), (uint8_t)(addr >> 8U), + (uint8_t)addr }; + + WriteEnable(); + while(!WriteEnabled()) vTaskDelay(1); + + spi.WriteCmdAndBuffer(cmd, cmdSize, b, toWrite); + + while(WriteInProgress()) vTaskDelay(1); + + addr += toWrite; + b += toWrite; + len -= toWrite; + } + +} diff --git a/src/drivers/SpiNorFlash.h b/src/drivers/SpiNorFlash.h index 839a1c2a..b5f19202 100644 --- a/src/drivers/SpiNorFlash.h +++ b/src/drivers/SpiNorFlash.h @@ -12,6 +12,26 @@ namespace Pinetime { SpiNorFlash(SpiNorFlash&&) = delete; SpiNorFlash& operator=(SpiNorFlash&&) = delete; + typedef struct __attribute__((packed)) { + uint8_t manufacturer = 0; + uint8_t type = 0; + uint8_t density = 0; + } Identification; + + Identification ReadIdentificaion(); + uint8_t ReadStatusRegister(); + bool WriteInProgress(); + bool WriteEnabled(); + uint8_t ReadConfigurationRegister(); + void Read(uint32_t address, uint8_t* buffer, size_t size); + void Write(uint32_t address, uint8_t *buffer, size_t size); + void WriteEnable(); + void SectorErase(uint32_t sectorAddress); + uint8_t ReadSecurityRegister(); + bool ProgramFailed(); + bool EraseFailed(); + + void Init(); void Uninit(); @@ -19,6 +39,18 @@ namespace Pinetime { void Sleep(); void Wakeup(); private: + enum class Commands : uint8_t { + PageProgram = 0x02, + Read = 0x03, + ReadStatusRegister = 0x05, + WriteEnable = 0x06, + ReadConfigurationRegister = 0x15, + SectorErase = 0x20, + ReadSecurityRegister = 0x2B, + ReadIdentification = 0x9F, + }; + static constexpr uint16_t pageSize = 256; + Spi& spi; }; -- cgit v1.2.3-70-g09d2 From 3cc76d7673f6a3b8b5253d04b358b440e550fdb4 Mon Sep 17 00:00:00 2001 From: JF Date: Sun, 24 May 2020 09:15:59 +0200 Subject: Working demo of OTA using MCUBoot https://github.com/lupyuen/pinetime-rust-mynewt/releases/tag/v4.1.4 --- gcc_nrf52.ld | 2 +- src/Components/Ble/DfuService.cpp | 33 ++++++++++++++++++++++++++++----- src/Components/Ble/DfuService.h | 9 ++++++--- src/SystemTask/SystemTask.cpp | 29 +++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 9 deletions(-) (limited to 'src/SystemTask') diff --git a/gcc_nrf52.ld b/gcc_nrf52.ld index b849ea2c..0746f491 100644 --- a/gcc_nrf52.ld +++ b/gcc_nrf52.ld @@ -5,7 +5,7 @@ GROUP(-lgcc -lc -lnosys) MEMORY { - FLASH (rx) : ORIGIN = 0x00000, LENGTH = 0x80000 + FLASH (rx) : ORIGIN = 0x08020, LENGTH = 0x78000 RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 0x10000 } diff --git a/src/Components/Ble/DfuService.cpp b/src/Components/Ble/DfuService.cpp index e7b4c5f4..de525321 100644 --- a/src/Components/Ble/DfuService.cpp +++ b/src/Components/Ble/DfuService.cpp @@ -107,7 +107,7 @@ int DfuService::WritePacketHandler(uint16_t connectionHandle, os_mbuf *om) { bleController.FirmwareUpdateTotalBytes(applicationSize); NRF_LOG_INFO("[DFU] -> Start data received : SD size : %d, BT size : %d, app size : %d", softdeviceSize, bootloaderSize, applicationSize); - for(int erased = 0; erased < applicationSize; erased += 0x1000) { + for(int erased = 0; erased < maxImageSize; erased += 0x1000) { #if 1 spiNorFlash.SectorErase(writeOffset + erased); @@ -159,7 +159,7 @@ int DfuService::WritePacketHandler(uint16_t connectionHandle, os_mbuf *om) { bytesReceived += om->om_len; bleController.FirmwareUpdateCurrentBytes(bytesReceived); - NRF_LOG_INFO("[DFU] -> Bytes received : %d in %d packets", bytesReceived, nbPacketReceived); + //NRF_LOG_INFO("[DFU] -> Bytes received : %d in %d packets", bytesReceived, nbPacketReceived); @@ -170,6 +170,16 @@ int DfuService::WritePacketHandler(uint16_t connectionHandle, os_mbuf *om) { SendNotification(connectionHandle, data, 5); } if(bytesReceived == applicationSize) { + if((nbPacketReceived % nbPacketsToNotify) != 0) { + auto remaningPacket = nbPacketReceived % nbPacketsToNotify; + uint32_t spiOffset = writeOffset + ((nbPacketReceived-remaningPacket)*20); + + spiNorFlash.Write(writeOffset + ((nbPacketReceived-remaningPacket)*20), tempBuffer, remaningPacket * 20); + } + if(applicationSize < maxImageSize) { + WriteMagicNumber(); + } + uint8_t data[3]{static_cast(Opcodes::Response), static_cast(Opcodes::ReceiveFirmwareImage), static_cast(ErrorCodes::NoError)}; @@ -304,14 +314,27 @@ void DfuService::Validate() { while(currentOffset < applicationSize) { uint32_t readSize = (applicationSize - currentOffset) > chunkSize ? chunkSize : (applicationSize - currentOffset); - spiNorFlash.Read(writeOffset + currentOffset, tempBuffer, chunkSize); + spiNorFlash.Read(writeOffset + currentOffset, tempBuffer, readSize); if(first) { - crc = ComputeCrc(tempBuffer, chunkSize, NULL); + crc = ComputeCrc(tempBuffer, readSize, NULL); first = false; } else - crc = ComputeCrc(tempBuffer, chunkSize, &crc); + crc = ComputeCrc(tempBuffer, readSize, &crc); currentOffset += readSize; } + NRF_LOG_INFO("CRC : %u", crc); } + +void DfuService::WriteMagicNumber() { + uint32_t magic[4] = { + 0xf395c277, + 0x7fefd260, + 0x0f505235, + 0x8079b62c, + }; + + uint32_t offset = writeOffset + (maxImageSize - (4 * sizeof(uint32_t))); + spiNorFlash.Write(offset, reinterpret_cast(magic), 4 * sizeof(uint32_t)); +} diff --git a/src/Components/Ble/DfuService.h b/src/Components/Ble/DfuService.h index e22e09f0..c8351ede 100644 --- a/src/Components/Ble/DfuService.h +++ b/src/Components/Ble/DfuService.h @@ -18,6 +18,7 @@ namespace Pinetime { DfuService(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::Ble& bleController, Pinetime::Drivers::SpiNorFlash& spiNorFlash); void Init(); + void Validate(); int OnServiceData(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt *context); private: @@ -89,11 +90,12 @@ namespace Pinetime { uint8_t nbPacketsToNotify = 0; uint32_t nbPacketReceived = 0; uint32_t bytesReceived = 0; - uint32_t writeOffset = 0; //0x40000; + uint32_t writeOffset = 0x40000; uint32_t softdeviceSize = 0; uint32_t bootloaderSize = 0; - uint32_t applicationSize = 115200; + uint32_t applicationSize = 0; + static constexpr uint32_t maxImageSize = 475136; int SendDfuRevision(os_mbuf *om) const; void SendNotification(uint16_t connectionHandle, const uint8_t *data, const size_t size); @@ -103,9 +105,10 @@ namespace Pinetime { uint8_t tempBuffer[200]; uint16_t ComputeCrc(uint8_t const * p_data, uint32_t size, uint16_t const * p_crc); - void Validate(); bool firstCrc = true; uint16_t tempCrc = 0; + + void WriteMagicNumber(); }; } } \ No newline at end of file diff --git a/src/SystemTask/SystemTask.cpp b/src/SystemTask/SystemTask.cpp index 022cade4..021bac3d 100644 --- a/src/SystemTask/SystemTask.cpp +++ b/src/SystemTask/SystemTask.cpp @@ -38,6 +38,27 @@ void SystemTask::Process(void *instance) { app->Work(); } +static inline void nrf52_wait_for_flash_ready(void) +{ + while (NRF_NVMC->READY == NVMC_READY_READY_Busy) {;} +} + +void nrf52_nvmc_write_word(uint32_t address, uint32_t value) { + // Enable write. + NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen; + __ISB(); + __DSB(); + + // Write word + *(uint32_t*)address = value; + nrf52_wait_for_flash_ready(); + + // Disable write + NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren; + __ISB(); + __DSB(); +} + void SystemTask::Work() { // watchdog.Setup(7); // watchdog.Start(); @@ -46,6 +67,14 @@ void SystemTask::Work() { spi.Init(); spiNorFlash.Init(); + + // TODO write magic only if it's not already 1 + nrf52_nvmc_write_word(0x7BFE8, 1); + + uint32_t* magicptr = reinterpret_cast(0x7BFE8); + uint32_t magic = *magicptr; + NRF_LOG_INFO("MAGIC : %d", magic); + nimbleController.Init(); nimbleController.StartAdvertising(); lcd.Init(); -- cgit v1.2.3-70-g09d2 From 331481cd0c6ba804860f7cdbd5110a87ff0160fe Mon Sep 17 00:00:00 2001 From: JF Date: Sun, 24 May 2020 16:36:04 +0200 Subject: Refactor CMake files to allow building 2 binaries : one standalone and one compatible with mcuboot bootloader (the offset in memory is different). Add documentation about mcuboot image generation and OTA. Re-enable watchdog --- CMakeLists.txt | 1 - README.md | 13 +- bootloader/README.md | 50 ++++++ bootloader/mynewt_nosemi.elf_4.1.7.bin | Bin 0 -> 22232 bytes bootloader/mynewt_nosemi_4.1.7.elf | Bin 0 -> 542120 bytes gcc_nrf52-mcuboot.ld | 136 +++++++++++++++ gcc_nrf52.ld | 2 +- src/CMakeLists.txt | 308 +++++++++++++++++++++++++++------ src/SystemTask/SystemTask.cpp | 10 +- 9 files changed, 450 insertions(+), 70 deletions(-) create mode 100644 bootloader/README.md create mode 100644 bootloader/mynewt_nosemi.elf_4.1.7.bin create mode 100644 bootloader/mynewt_nosemi_4.1.7.elf create mode 100644 gcc_nrf52-mcuboot.ld (limited to 'src/SystemTask') diff --git a/CMakeLists.txt b/CMakeLists.txt index 5af971f0..b6a4e932 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -66,5 +66,4 @@ endif() set(VERSION_EDIT_WARNING "// Do not edit this file, it is automatically generated by CMAKE!") configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/Version.h.in ${CMAKE_CURRENT_SOURCE_DIR}/src/Version.h) -include("cmake-nRF5x/CMake_nRF5x.cmake") add_subdirectory(src) diff --git a/README.md b/README.md index 790e3de1..fcd4e537 100644 --- a/README.md +++ b/README.md @@ -34,11 +34,13 @@ I've tested this project on the actual PineTime hardware. * Rich user interface (using [LittleVGL](https://littlevgl.com/)) via display, touchpanel and push button. * Digital watch face and 4 demo applications (spinning meter, analog gauche, push button and message box); * Watchdog (automatic reset in case of firmware crash) and reset support (push and hold the button for 7 - 10s); - * BLE Notification support (still Work-In-Progress, [companion app](https://github.com/JF002/gobbledegook) needed). + * BLE Notification support (still Work-In-Progress, [companion app](https://github.com/JF002/gobbledegook) needed); + * **[EXPERIMENTAL]** Firmware update (OTA) via BLE. ## Documentation * [BLE implementation and API](./doc/ble.md) + * [Bootloader and DFU](./bootloader/README.md) ## Stub using NRF52-DK ![Pinetime stub](./images/pinetimestub1.jpg "PinetimeStub") @@ -116,19 +118,12 @@ $ make -j pinetime-app $ make FLASH_ERASE ``` -* Flash softdevice & application +* Flash application ``` -$ make FLASH_SOFTDEVICE $ make FLASH_pinetime-app ``` -Or, with ```mergehex``` - -``` -$ make FLASH_MERGED_pinetime-app -``` - * For your information : list make targets : ``` diff --git a/bootloader/README.md b/bootloader/README.md new file mode 100644 index 00000000..facc0500 --- /dev/null +++ b/bootloader/README.md @@ -0,0 +1,50 @@ +# Bootloader + +## Bootloader binary +The binary comes from https://github.com/lupyuen/pinetime-rust-mynewt/releases/tag/v4.1.7 + +It must be flash at address **0x00** in the internal flash memory. + +Using OpenOCD: + +` +program mynewt_nosemi.elf_4.1.7.bin 0 +` + +## Application firmware image +Build the binary compatible with the booloader: + +` +make pinetime-mcuboot-app +` + +The binary is located in */src/pinetime-mcuboot-app.bin*. + +It must me converted into a MCUBoot image using *imgtool.py* from [MCUBoot](https://github.com/JuulLabs-OSS/mcuboot/tree/master/scripts). + +` +imgtool.py create --align 4 --version 1.0.0 --header-size 32 --slot-size 475136 --pad-header /src/pinetime-mcuboot-app.bin image.bin +` + +The image must be then flashed at address **0x8000** in the internal flash memory. + +Using OpenOCD: + +` +program image.bin 0x8000 +` + +## OTA and DFU +Pack the image into a .zip file for the NRF DFU protocol: + +` +adafruit-nrfutil dfu genpkg --dev-type 0x0052 --application image.bin dfu.zip +` + +Use NRFConnect or dfu.py to upload the zip file to the device: + +` +sudo dfu.py -z /home/jf/nrf52/bootloader/dfu.zip -a --legacy +` + +**TODO** : dfu.py \ No newline at end of file diff --git a/bootloader/mynewt_nosemi.elf_4.1.7.bin b/bootloader/mynewt_nosemi.elf_4.1.7.bin new file mode 100644 index 00000000..bd00ec9c Binary files /dev/null and b/bootloader/mynewt_nosemi.elf_4.1.7.bin differ diff --git a/bootloader/mynewt_nosemi_4.1.7.elf b/bootloader/mynewt_nosemi_4.1.7.elf new file mode 100644 index 00000000..3d7c231f Binary files /dev/null and b/bootloader/mynewt_nosemi_4.1.7.elf differ diff --git a/gcc_nrf52-mcuboot.ld b/gcc_nrf52-mcuboot.ld new file mode 100644 index 00000000..0746f491 --- /dev/null +++ b/gcc_nrf52-mcuboot.ld @@ -0,0 +1,136 @@ +/* Linker script to configure memory regions. */ + +SEARCH_DIR(.) +GROUP(-lgcc -lc -lnosys) + +MEMORY +{ + FLASH (rx) : ORIGIN = 0x08020, LENGTH = 0x78000 + RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 0x10000 +} + +SECTIONS +{ +} + +SECTIONS +{ + . = ALIGN(4); + .mem_section_dummy_ram : + { + } + .cli_sorted_cmd_ptrs : + { + PROVIDE(__start_cli_sorted_cmd_ptrs = .); + KEEP(*(.cli_sorted_cmd_ptrs)) + PROVIDE(__stop_cli_sorted_cmd_ptrs = .); + } > RAM + .fs_data : + { + PROVIDE(__start_fs_data = .); + KEEP(*(.fs_data)) + PROVIDE(__stop_fs_data = .); + } > RAM + .log_dynamic_data : + { + PROVIDE(__start_log_dynamic_data = .); + KEEP(*(SORT(.log_dynamic_data*))) + PROVIDE(__stop_log_dynamic_data = .); + } > RAM + .log_filter_data : + { + PROVIDE(__start_log_filter_data = .); + KEEP(*(SORT(.log_filter_data*))) + PROVIDE(__stop_log_filter_data = .); + } > RAM + +} INSERT AFTER .data; + +SECTIONS +{ + .mem_section_dummy_rom : + { + } + .sdh_soc_observers : + { + PROVIDE(__start_sdh_soc_observers = .); + KEEP(*(SORT(.sdh_soc_observers*))) + PROVIDE(__stop_sdh_soc_observers = .); + } > FLASH + .sdh_ble_observers : + { + PROVIDE(__start_sdh_ble_observers = .); + KEEP(*(SORT(.sdh_ble_observers*))) + PROVIDE(__stop_sdh_ble_observers = .); + } > FLASH + .sdh_req_observers : + { + PROVIDE(__start_sdh_req_observers = .); + KEEP(*(SORT(.sdh_req_observers*))) + PROVIDE(__stop_sdh_req_observers = .); + } > FLASH + .sdh_state_observers : + { + PROVIDE(__start_sdh_state_observers = .); + KEEP(*(SORT(.sdh_state_observers*))) + PROVIDE(__stop_sdh_state_observers = .); + } > FLASH + .sdh_stack_observers : + { + PROVIDE(__start_sdh_stack_observers = .); + KEEP(*(SORT(.sdh_stack_observers*))) + PROVIDE(__stop_sdh_stack_observers = .); + } > FLASH + .nrf_queue : + { + PROVIDE(__start_nrf_queue = .); + KEEP(*(.nrf_queue)) + PROVIDE(__stop_nrf_queue = .); + } > FLASH + .nrf_balloc : + { + PROVIDE(__start_nrf_balloc = .); + KEEP(*(.nrf_balloc)) + PROVIDE(__stop_nrf_balloc = .); + } > FLASH + .cli_command : + { + PROVIDE(__start_cli_command = .); + KEEP(*(.cli_command)) + PROVIDE(__stop_cli_command = .); + } > FLASH + .crypto_data : + { + PROVIDE(__start_crypto_data = .); + KEEP(*(SORT(.crypto_data*))) + PROVIDE(__stop_crypto_data = .); + } > FLASH + .pwr_mgmt_data : + { + PROVIDE(__start_pwr_mgmt_data = .); + KEEP(*(SORT(.pwr_mgmt_data*))) + PROVIDE(__stop_pwr_mgmt_data = .); + } > FLASH + .log_const_data : + { + PROVIDE(__start_log_const_data = .); + KEEP(*(SORT(.log_const_data*))) + PROVIDE(__stop_log_const_data = .); + } > FLASH + .log_backends : + { + PROVIDE(__start_log_backends = .); + KEEP(*(SORT(.log_backends*))) + PROVIDE(__stop_log_backends = .); + } > FLASH + .nrf_balloc : + { + PROVIDE(__start_nrf_balloc = .); + KEEP(*(.nrf_balloc)) + PROVIDE(__stop_nrf_balloc = .); + } > FLASH + +} INSERT AFTER .text + + +INCLUDE "./nrf_common.ld" diff --git a/gcc_nrf52.ld b/gcc_nrf52.ld index 0746f491..98e133aa 100644 --- a/gcc_nrf52.ld +++ b/gcc_nrf52.ld @@ -5,7 +5,7 @@ GROUP(-lgcc -lc -lnosys) MEMORY { - FLASH (rx) : ORIGIN = 0x08020, LENGTH = 0x78000 + FLASH (rx) : ORIGIN = 0x00000, LENGTH = 0x78000 RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 0x10000 } diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e8e5726b..f82f3091 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,29 +5,86 @@ project(pinetime-app C CXX ASM) # define some variables just for this example to determine file locations set(NRF_PROJECT_NAME pinetime-app) set(NRF_BOARD pca10040) -#set(NRF_SOFTDEVICE s132) - -nRF5x_toolchainSetup() -nRF5x_setup() - -#nRF5x_addAppScheduler() -#nRF5x_addAppFIFO() -#nRF5x_addAppTimer() -#nRF5x_addAppUART() -nRF5x_addAppButton() -nRF5x_addBSP(FALSE FALSE FALSE) -nRF5x_addAppGpiote() -#nRF5x_addBLEGATT() -# -#nRF5x_addBLEService(ble_lbs) -add_definitions(-DCONFIG_GPIO_AS_PINRESET) -add_definitions(-DDEBUG) -add_definitions(-DNIMBLE_CFG_CONTROLLER) -add_definitions(-DOS_CPUTIME_FREQ) - -include_directories(.) -include_directories(libs/) +# check if all the necessary tools paths have been provided. +if (NOT NRF5_SDK_PATH) + message(FATAL_ERROR "The path to the nRF5 SDK (NRF5_SDK_PATH) must be set.") +endif () +if(DEFINED ARM_NONE_EABI_TOOLCHAIN_PATH) + set(ARM_NONE_EABI_TOOLCHAIN_BIN_PATH ${ARM_NONE_EABI_TOOLCHAIN_PATH}/bin) +endif() + +if (NOT NRF_TARGET MATCHES "nrf52") + message(FATAL_ERROR "Only rRF52 boards are supported right now") +endif() + +# Setup toolchain +include(${CMAKE_SOURCE_DIR}/cmake-nRF5x/arm-gcc-toolchain.cmake) + +if(NOT DEFINED ARM_GCC_TOOLCHAIN) + message(FATAL_ERROR "The toolchain must be set up before calling this macro") +endif() +set(CMAKE_OSX_SYSROOT "/") +set(CMAKE_OSX_DEPLOYMENT_TARGET "") + + +set(SDK_SOURCE_FILES + # Startup + "${NRF5_SDK_PATH}/modules/nrfx/mdk/system_nrf52.c" + "${NRF5_SDK_PATH}/modules/nrfx/mdk/gcc_startup_nrf52.S" + + # Base SDK + "${NRF5_SDK_PATH}/components/boards/boards.c" + "${NRF5_SDK_PATH}/integration/nrfx/legacy/nrf_drv_clock.c" + "${NRF5_SDK_PATH}/modules/nrfx/drivers/src/nrfx_clock.c" + "${NRF5_SDK_PATH}/modules/nrfx/drivers/src/nrfx_gpiote.c" + "${NRF5_SDK_PATH}/modules/nrfx/soc/nrfx_atomic.c" + "${NRF5_SDK_PATH}/modules/nrfx/drivers/src/nrfx_saadc.c" + + # FreeRTOS + ${NRF5_SDK_PATH}/external/freertos/source/croutine.c + ${NRF5_SDK_PATH}/external/freertos/source/event_groups.c + ${NRF5_SDK_PATH}/external/freertos/source/portable/MemMang/heap_1.c + ${NRF5_SDK_PATH}/external/freertos/source/list.c + ${NRF5_SDK_PATH}/external/freertos/source/queue.c + ${NRF5_SDK_PATH}/external/freertos/source/stream_buffer.c + ${NRF5_SDK_PATH}/external/freertos/source/tasks.c + ${NRF5_SDK_PATH}/external/freertos/source/timers.c + ${NRF5_SDK_PATH}/components/libraries/timer/app_timer_freertos.c + + # Libs + "${NRF5_SDK_PATH}/components/libraries/atomic/nrf_atomic.c" + "${NRF5_SDK_PATH}/components/libraries/balloc/nrf_balloc.c" + "${NRF5_SDK_PATH}/components/libraries/util/nrf_assert.c" + "${NRF5_SDK_PATH}/components/libraries/util/app_error.c" + "${NRF5_SDK_PATH}/components/libraries/util/app_error_weak.c" + "${NRF5_SDK_PATH}/components/libraries/util/app_error_handler_gcc.c" + "${NRF5_SDK_PATH}/components/libraries/util/app_util_platform.c" + "${NRF5_SDK_PATH}/components/libraries/log/src/nrf_log_backend_rtt.c" + "${NRF5_SDK_PATH}/components/libraries/log/src/nrf_log_backend_serial.c" + "${NRF5_SDK_PATH}/components/libraries/log/src/nrf_log_default_backends.c" + "${NRF5_SDK_PATH}/components/libraries/log/src/nrf_log_frontend.c" + "${NRF5_SDK_PATH}/components/libraries/log/src/nrf_log_str_formatter.c" + "${NRF5_SDK_PATH}/components/libraries/memobj/nrf_memobj.c" + "${NRF5_SDK_PATH}/components/libraries/ringbuf/nrf_ringbuf.c" + "${NRF5_SDK_PATH}/components/libraries/strerror/nrf_strerror.c" + + # Segger RTT + "${NRF5_SDK_PATH}/external/segger_rtt/SEGGER_RTT_Syscalls_GCC.c" + "${NRF5_SDK_PATH}/external/segger_rtt/SEGGER_RTT.c" + "${NRF5_SDK_PATH}/external/segger_rtt/SEGGER_RTT_printf.c" + + # Other + "${NRF5_SDK_PATH}/external/utf_converter/utf.c" + "${NRF5_SDK_PATH}/external/fprintf/nrf_fprintf.c" + "${NRF5_SDK_PATH}/external/fprintf/nrf_fprintf_format.c" + + # TWI + "${NRF5_SDK_PATH}/modules/nrfx/drivers/src/nrfx_twi.c" + + # GPIOTE + "${NRF5_SDK_PATH}/components/libraries/gpiote/app_gpiote.c" +) set(TINYCRYPT_SRC libs/mynewt-nimble/ext/tinycrypt/src/aes_encrypt.c @@ -37,9 +94,6 @@ set(TINYCRYPT_SRC set(NIMBLE_SRC libs/mynewt-nimble/porting/npl/freertos/src/nimble_port_freertos.c libs/mynewt-nimble/porting/npl/freertos/src/npl_os_freertos.c - - - libs/mynewt-nimble/nimble/host/src/ble_hs.c libs/mynewt-nimble/nimble/host/src/ble_hs_hci_evt.c libs/mynewt-nimble/nimble/host/src/ble_l2cap_sig_cmd.c @@ -78,11 +132,7 @@ set(NIMBLE_SRC libs/mynewt-nimble/nimble/host/src/ble_hs_stop.c libs/mynewt-nimble/nimble/host/src/ble_hs_startup.c libs/mynewt-nimble/nimble/host/store/ram/src/ble_store_ram.c - libs/mynewt-nimble/nimble/transport/ram/src/ble_hci_ram.c - - - libs/mynewt-nimble/nimble/controller/src/ble_ll.c libs/mynewt-nimble/nimble/controller/src/ble_ll_rand.c libs/mynewt-nimble/nimble/controller/src/ble_ll_conn.c @@ -97,9 +147,6 @@ set(NIMBLE_SRC libs/mynewt-nimble/nimble/controller/src/ble_ll_supp_cmd.c libs/mynewt-nimble/nimble/controller/src/ble_ll_hci_ev.c libs/mynewt-nimble/nimble/controller/src/ble_ll_rfmgmt.c - - - libs/mynewt-nimble/porting/nimble/src/os_cputime.c libs/mynewt-nimble/porting/nimble/src/os_cputime_pwr2.c libs/mynewt-nimble/porting/nimble/src/os_mbuf.c @@ -108,13 +155,10 @@ set(NIMBLE_SRC libs/mynewt-nimble/porting/nimble/src/mem.c libs/mynewt-nimble/porting/nimble/src/endian.c libs/mynewt-nimble/porting/nimble/src/os_msys_init.c - libs/mynewt-nimble/nimble/drivers/nrf52/src/ble_hw.c libs/mynewt-nimble/nimble/drivers/nrf52/src/ble_phy.c - libs/mynewt-nimble/nimble/host/services/gap/src/ble_svc_gap.c libs/mynewt-nimble/nimble/host/services/gatt/src/ble_svc_gatt.c - libs/mynewt-nimble/nimble/host/util/src/addr.c ) @@ -135,7 +179,6 @@ set(LVGL_SRC libs/lvgl/src/lv_core/lv_refr.h libs/lvgl/src/lv_core/lv_style.c libs/lvgl/src/lv_core/lv_style.h - libs/lvgl/src/lv_misc/lv_anim.c libs/lvgl/src/lv_misc/lv_anim.h libs/lvgl/src/lv_misc/lv_async.h @@ -175,7 +218,6 @@ set(LVGL_SRC libs/lvgl/src/lv_misc/lv_types.h libs/lvgl/src/lv_misc/lv_utils.c libs/lvgl/src/lv_misc/lv_utils.h - libs/lvgl/src/lv_draw/lv_draw.c libs/lvgl/src/lv_draw/lv_draw.h libs/lvgl/src/lv_draw/lv_draw_arc.c @@ -196,7 +238,6 @@ set(LVGL_SRC libs/lvgl/src/lv_draw/lv_img_cache.h libs/lvgl/src/lv_draw/lv_img_decoder.c libs/lvgl/src/lv_draw/lv_img_decoder.h - libs/lvgl/src/lv_hal/lv_hal.h libs/lvgl/src/lv_hal/lv_hal_disp.c libs/lvgl/src/lv_hal/lv_hal_disp.h @@ -204,31 +245,23 @@ set(LVGL_SRC libs/lvgl/src/lv_hal/lv_hal_indev.h libs/lvgl/src/lv_hal/lv_hal_tick.c libs/lvgl/src/lv_hal/lv_hal_tick.h - libs/lvgl/src/lv_font/lv_font.c libs/lvgl/src/lv_font/lv_font.h libs/lvgl/src/lv_font/lv_font_fmt_txt.c libs/lvgl/src/lv_font/lv_font_fmt_txt.h -# libs/lvgl/src/lv_font/lv_font_roboto_16.c libs/lvgl/src/lv_font/lv_symbol_def.h - libs/lvgl/src/lv_themes/lv_theme.c libs/lvgl/src/lv_themes/lv_theme.h - libs/lvgl/src/lv_objx/lv_btn.h libs/lvgl/src/lv_objx/lv_btn.c - libs/lvgl/src/lv_objx/lv_cont.h libs/lvgl/src/lv_objx/lv_cont.c - libs/lvgl/src/lv_objx/lv_label.h libs/lvgl/src/lv_objx/lv_label.c - libs/lvgl/src/lv_themes/lv_theme.c libs/lvgl/src/lv_themes/lv_theme.h libs/lvgl/src/lv_themes/lv_theme_night.h libs/lvgl/src/lv_themes/lv_theme_night.c - libs/lvgl/src/lv_objx/lv_list.c libs/lvgl/src/lv_objx/lv_list.h libs/lvgl/src/lv_objx/lv_tileview.c @@ -247,20 +280,16 @@ set(LVGL_SRC libs/lvgl/src/lv_objx/lv_arc.h libs/lvgl/src/lv_objx/lv_gauge.c libs/lvgl/src/lv_objx/lv_gauge.h - libs/lvgl/src/lv_objx/lv_mbox.c libs/lvgl/src/lv_objx/lv_mbox.h - libs/lvgl/src/lv_objx/lv_bar.c libs/lvgl/src/lv_objx/lv_bar.h libs/lvgl/src/lv_objx/lv_slider.h libs/lvgl/src/lv_objx/lv_slider.c - ) list(APPEND IMAGE_FILES DisplayApp/Icons/battery/os_battery_error.c - DisplayApp/Icons/battery/os_battery_100.c DisplayApp/Icons/battery/os_battery_090.c DisplayApp/Icons/battery/os_battery_080.c @@ -331,6 +360,7 @@ list(APPEND SOURCE_FILES ${NIMBLE_SRC} ${LVGL_SRC} ${IMAGE_FILES} + ${SDK_SOURCE_FILES} DisplayApp/LittleVgl.cpp DisplayApp/Fonts/jetbrains_mono_extrabold_compressed.c @@ -385,13 +415,14 @@ set(INCLUDE_FILES libs/date/includes/date/julian.h libs/date/includes/date/ptz.h libs/date/includes/date/tz_private.h - DisplayApp/LittleVgl.h - SystemTask/SystemTask.h ) include_directories( + . + ../ + libs/ FreeRTOS/ libs/date/includes libs/mynewt-nimble/porting/npl/freertos/include @@ -406,10 +437,179 @@ include_directories( libs/mynewt-nimble/nimble/host/services/gatt/include libs/mynewt-nimble/nimble/host/util/include libs/mynewt-nimble/nimble/host/store/ram/include + + "${NRF5_SDK_PATH}/components/drivers_nrf/nrf_soc_nosd" + "${NRF5_SDK_PATH}/components" + "${NRF5_SDK_PATH}/components/boards" + "${NRF5_SDK_PATH}/components/softdevice/common" + "${NRF5_SDK_PATH}/integration/nrfx" + "${NRF5_SDK_PATH}/integration/nrfx/legacy" + "${NRF5_SDK_PATH}/modules/nrfx" + "${NRF5_SDK_PATH}/modules/nrfx/drivers/include" + "${NRF5_SDK_PATH}/modules/nrfx/hal" + "${NRF5_SDK_PATH}/modules/nrfx/mdk" + ${NRF5_SDK_PATH}/external/freertos/source/include + "${NRF5_SDK_PATH}/components/toolchain/cmsis/include" + "${NRF5_SDK_PATH}/components/libraries/atomic" + "${NRF5_SDK_PATH}/components/libraries/atomic_fifo" + "${NRF5_SDK_PATH}/components/libraries/atomic_flags" + "${NRF5_SDK_PATH}/components/libraries/balloc" + "${NRF5_SDK_PATH}/components/libraries/bootloader/ble_dfu" + "${NRF5_SDK_PATH}/components/libraries/cli" + "${NRF5_SDK_PATH}/components/libraries/crc16" + "${NRF5_SDK_PATH}/components/libraries/crc32" + "${NRF5_SDK_PATH}/components/libraries/crypto" + "${NRF5_SDK_PATH}/components/libraries/csense" + "${NRF5_SDK_PATH}/components/libraries/csense_drv" + "${NRF5_SDK_PATH}/components/libraries/delay" + "${NRF5_SDK_PATH}/components/libraries/ecc" + "${NRF5_SDK_PATH}/components/libraries/experimental_section_vars" + "${NRF5_SDK_PATH}/components/libraries/experimental_task_manager" + "${NRF5_SDK_PATH}/components/libraries/fds" + "${NRF5_SDK_PATH}/components/libraries/fstorage" + "${NRF5_SDK_PATH}/components/libraries/gfx" + "${NRF5_SDK_PATH}/components/libraries/gpiote" + "${NRF5_SDK_PATH}/components/libraries/hardfault" + "${NRF5_SDK_PATH}/components/libraries/hci" + "${NRF5_SDK_PATH}/components/libraries/led_softblink" + "${NRF5_SDK_PATH}/components/libraries/log" + "${NRF5_SDK_PATH}/components/libraries/log/src" + "${NRF5_SDK_PATH}/components/libraries/low_power_pwm" + "${NRF5_SDK_PATH}/components/libraries/mem_manager" + "${NRF5_SDK_PATH}/components/libraries/memobj" + "${NRF5_SDK_PATH}/components/libraries/mpu" + "${NRF5_SDK_PATH}/components/libraries/mutex" + "${NRF5_SDK_PATH}/components/libraries/pwm" + "${NRF5_SDK_PATH}/components/libraries/pwr_mgmt" + "${NRF5_SDK_PATH}/components/libraries/queue" + "${NRF5_SDK_PATH}/components/libraries/ringbuf" + "${NRF5_SDK_PATH}/components/libraries/scheduler" + "${NRF5_SDK_PATH}/components/libraries/sdcard" + "${NRF5_SDK_PATH}/components/libraries/slip" + "${NRF5_SDK_PATH}/components/libraries/sortlist" + "${NRF5_SDK_PATH}/components/libraries/spi_mngr" + "${NRF5_SDK_PATH}/components/libraries/stack_guard" + "${NRF5_SDK_PATH}/components/libraries/strerror" + "${NRF5_SDK_PATH}/components/libraries/svc" + "${NRF5_SDK_PATH}/components/libraries/timer" + "${NRF5_SDK_PATH}/components/libraries/usbd" + "${NRF5_SDK_PATH}/components/libraries/usbd/class/audio" + "${NRF5_SDK_PATH}/components/libraries/usbd/class/cdc" + "${NRF5_SDK_PATH}/components/libraries/usbd/class/cdc/acm" + "${NRF5_SDK_PATH}/components/libraries/usbd/class/hid" + "${NRF5_SDK_PATH}/components/libraries/usbd/class/hid/generic" + "${NRF5_SDK_PATH}/components/libraries/usbd/class/hid/kbd" + "${NRF5_SDK_PATH}/components/libraries/usbd/class/hid/mouse" + "${NRF5_SDK_PATH}/components/libraries/usbd/class/msc" + "${NRF5_SDK_PATH}/components/libraries/util" + "${NRF5_SDK_PATH}/external/segger_rtt/" + "${NRF5_SDK_PATH}/external/fprintf/" + "${NRF5_SDK_PATH}/external/thedotfactory_fonts" + "${NRF5_SDK_PATH}/components/libraries/gpiote" + ) link_directories( - ../ ) -nRF5x_addExecutable(pinetime-app "${SOURCE_FILES}" ${INCLUDE_FILES}) + +set(COMMON_FLAGS -MP -MD -mthumb -mabi=aapcs -Wall -g3 -ffunction-sections -fdata-sections -fno-strict-aliasing -fno-builtin --short-enums -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -Wreturn-type -Werror=return-type) +add_definitions(-DCONFIG_GPIO_AS_PINRESET) +add_definitions(-DDEBUG) +add_definitions(-DNIMBLE_CFG_CONTROLLER) +add_definitions(-DOS_CPUTIME_FREQ) +add_definitions(-DNRF52 -DNRF52832 -DNRF52832_XXAA -DNRF52_PAN_74 -DNRF52_PAN_64 -DNRF52_PAN_12 -DNRF52_PAN_58 -DNRF52_PAN_54 -DNRF52_PAN_31 -DNRF52_PAN_51 -DNRF52_PAN_36 -DNRF52_PAN_15 -DNRF52_PAN_20 -DNRF52_PAN_55 -DBOARD_PCA10040) +add_definitions(-DFREERTOS) +add_definitions(-DDEBUG_NRF_USER) + +# Build autonomous binary (without support for bootloader) +set(EXECUTABLE_NAME "pinetime-app") +set(NRF5_LINKER_SCRIPT "${CMAKE_SOURCE_DIR}/gcc_nrf52.ld") +add_executable(${EXECUTABLE_NAME} ${SOURCE_FILES}) +target_compile_options(${EXECUTABLE_NAME} PUBLIC + $<$,$>: ${COMMON_FLAGS} -O0 -g3> + $<$,$>: ${COMMON_FLAGS} -O3> + $<$,$>: ${COMMON_FLAGS} -O0 -g3> + $<$,$>: ${COMMON_FLAGS} -O3> + $<$: -MP -MD -std=c99 -x assembler-with-cpp> +) + +set_target_properties(${EXECUTABLE_NAME} PROPERTIES + SUFFIX ".out" + LINK_FLAGS "-mthumb -mabi=aapcs -std=gnu++98 -std=c99 -L ${NRF5_SDK_PATH}/modules/nrfx/mdk -T${NRF5_LINKER_SCRIPT} -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -Wl,--gc-sections --specs=nano.specs -lc -lnosys -lm -Wl,-Map=${EXECUTABLE_NAME}.map" + CXX_STANDARD 11 + C_STANDARD 99 + ) + +add_custom_command(TARGET ${EXECUTABLE_NAME} + POST_BUILD + COMMAND ${CMAKE_SIZE_UTIL} ${EXECUTABLE_NAME}.out + COMMAND ${CMAKE_OBJCOPY} -O binary ${EXECUTABLE_NAME}.out "${EXECUTABLE_NAME}.bin" + COMMAND ${CMAKE_OBJCOPY} -O ihex ${EXECUTABLE_NAME}.out "${EXECUTABLE_NAME}.hex" + COMMENT "post build steps for ${EXECUTABLE_NAME}") + + +# Build binary intended to be used by bootloader +set(EXECUTABLE_MCUBOOT_NAME "pinetime-mcuboot-app") +set(EXECUTABLE_MCUBOOT_WITH_BOOTLOADER_NAME "pinetime-mcuboot-app-wth-bootloader") +set(NRF5_LINKER_SCRIPT_MCUBOOT "${CMAKE_SOURCE_DIR}/gcc_nrf52-mcuboot.ld") +add_executable(${EXECUTABLE_MCUBOOT_NAME} ${SOURCE_FILES}) +target_compile_options(${EXECUTABLE_MCUBOOT_NAME} PUBLIC + $<$,$>: ${COMMON_FLAGS} -O0 -g3> + $<$,$>: ${COMMON_FLAGS} -O3> + $<$,$>: ${COMMON_FLAGS} -O0 -g3> + $<$,$>: ${COMMON_FLAGS} -O3> + $<$: -MP -MD -std=c99 -x assembler-with-cpp> + ) + +set_target_properties(${EXECUTABLE_MCUBOOT_NAME} PROPERTIES + SUFFIX ".out" + LINK_FLAGS "-mthumb -mabi=aapcs -std=gnu++98 -std=c99 -L ${NRF5_SDK_PATH}/modules/nrfx/mdk -T${NRF5_LINKER_SCRIPT_MCUBOOT} -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -Wl,--gc-sections --specs=nano.specs -lc -lnosys -lm -Wl,-Map=${EXECUTABLE_MCUBOOT_NAME}.map" + CXX_STANDARD 11 + C_STANDARD 99 + ) + +add_custom_command(TARGET ${EXECUTABLE_MCUBOOT_NAME} + POST_BUILD + COMMAND ${CMAKE_SIZE_UTIL} ${EXECUTABLE_MCUBOOT_NAME}.out + COMMAND ${CMAKE_OBJCOPY} -O binary ${EXECUTABLE_MCUBOOT_NAME}.out "${EXECUTABLE_MCUBOOT_NAME}.bin" + COMMAND ${CMAKE_OBJCOPY} -O ihex ${EXECUTABLE_MCUBOOT_NAME}.out "${EXECUTABLE_MCUBOOT_NAME}.hex" + COMMENT "post build steps for ${EXECUTABLE_MCUBOOT_NAME}" +) + +# FLASH +if(USE_JLINK) + add_custom_target(FLASH_ERASE + COMMAND ${NRFJPROG} --eraseall -f ${NRF_TARGET} + COMMENT "erasing flashing" + ) + add_custom_target("FLASH_${EXECUTABLE_NAME}" + DEPENDS ${EXECUTABLE_NAME} + COMMAND ${NRFJPROG} --program ${EXECUTABLE_NAME}.hex -f ${NRF_TARGET} --sectorerase + COMMAND sleep 0.5s + COMMAND ${NRFJPROG} --reset -f ${NRF_TARGET} + COMMENT "flashing ${EXECUTABLE_NAME}.hex" + ) + +elseif(USE_GDB_CLIENT) + add_custom_target(FLASH_ERASE + COMMAND ${GDB_CLIENT_BIN_PATH} -nx --batch -ex 'target extended-remote ${GDB_CLIENT_TARGET_REMOTE}' -ex 'monitor swdp_scan' -ex 'attach 1' -ex 'mon erase_mass' + COMMENT "erasing flashing" + ) + add_custom_target("FLASH_${EXECUTABLE_NAME}" + DEPENDS ${EXECUTABLE_NAME} + COMMAND ${GDB_CLIENT_BIN_PATH} -nx --batch -ex 'target extended-remote ${GDB_CLIENT_TARGET_REMOTE}' -ex 'monitor swdp_scan' -ex 'attach 1' -ex 'load' -ex 'kill' ${EXECUTABLE_NAME}.hex + COMMENT "flashing ${EXECUTABLE_NAME}.hex" + ) +elseif(USE_OPENOCD) + add_custom_target(FLASH_ERASE + COMMAND ${OPENOCD_BIN_PATH} -f interface/stlink.cfg -c 'transport select hla_swd' -f target/nrf52.cfg -c init -c halt -c 'nrf5 mass_erase' -c reset -c shutdown + COMMENT "erasing flashing" + ) + add_custom_target("FLASH_${EXECUTABLE_NAME}" + DEPENDS ${EXECUTABLE_NAME} + COMMAND ${OPENOCD_BIN_PATH} -c "tcl_port disabled" -c "gdb_port 3333" -c "telnet_port 4444" -f interface/stlink.cfg -c 'transport select hla_swd' -f target/nrf52.cfg -c "program \"${EXECUTABLE_NAME}.hex\"" -c reset -c shutdown + COMMENT "flashing ${EXECUTABLE_NAME}.hex" + ) + +endif() \ No newline at end of file diff --git a/src/SystemTask/SystemTask.cpp b/src/SystemTask/SystemTask.cpp index 021bac3d..84392c7d 100644 --- a/src/SystemTask/SystemTask.cpp +++ b/src/SystemTask/SystemTask.cpp @@ -60,19 +60,19 @@ void nrf52_nvmc_write_word(uint32_t address, uint32_t value) { } void SystemTask::Work() { -// watchdog.Setup(7); -// watchdog.Start(); + watchdog.Setup(7); + watchdog.Start(); NRF_LOG_INFO("Last reset reason : %s", Pinetime::Drivers::Watchdog::ResetReasonToString(watchdog.ResetReason())); APP_GPIOTE_INIT(2); spi.Init(); spiNorFlash.Init(); - // TODO write magic only if it's not already 1 - nrf52_nvmc_write_word(0x7BFE8, 1); - uint32_t* magicptr = reinterpret_cast(0x7BFE8); uint32_t magic = *magicptr; + if(magic != 1) + nrf52_nvmc_write_word(0x7BFE8, 1); + NRF_LOG_INFO("MAGIC : %d", magic); nimbleController.Init(); -- cgit v1.2.3-70-g09d2