diff --git a/src/DisplayApp/DisplayApp.cpp b/src/DisplayApp/DisplayApp.cpp index 208437a1..f6138ec7 100644 --- a/src/DisplayApp/DisplayApp.cpp +++ b/src/DisplayApp/DisplayApp.cpp @@ -95,14 +95,10 @@ void DisplayApp::Refresh() { vTaskDelay(100); } lcd.DisplayOff(); - lcd.Sleep(); - touchPanel.Sleep(); + systemTask.PushMessage(System::SystemTask::Messages::OnDisplayTaskSleeping); state = States::Idle; break; case Messages::GoToRunning: - lcd.Wakeup(); - touchPanel.Wakeup(); - lcd.DisplayOn(); brightnessController.Restore(); state = States::Running; @@ -173,7 +169,7 @@ void DisplayApp::Refresh() { } } - if(touchMode == TouchModes::Polling) { + if(state != States::Idle && touchMode == TouchModes::Polling) { auto info = touchPanel.GetTouchInfo(); if(info.action == 2) {// 2 = contact if(!currentScreen->OnTouchEvent(info.x, info.y)) { diff --git a/src/SystemTask/SystemTask.cpp b/src/SystemTask/SystemTask.cpp index 1ee8e918..b515d1ab 100644 --- a/src/SystemTask/SystemTask.cpp +++ b/src/SystemTask/SystemTask.cpp @@ -145,6 +145,14 @@ void SystemTask::Work() { case Messages::OnButtonEvent: ReloadIdleTimer(); break; + case Messages::OnDisplayTaskSleeping: + spiNorFlash.Sleep(); + lcd.Sleep(); + touchPanel.Sleep(); + + spi.Sleep(); + twiMaster.Sleep(); + break; default: break; } } @@ -185,6 +193,13 @@ void SystemTask::OnButtonPushed() { void SystemTask::GoToRunning() { PushMessage(Messages::GoToRunning); + spi.Wakeup(); + twiMaster.Wakeup(); + + spiNorFlash.Wakeup(); + lcd.Wakeup(); + touchPanel.Wakeup(); + displayApp->PushMessage(Applications::DisplayApp::Messages::GoToRunning); displayApp->PushMessage(Applications::DisplayApp::Messages::UpdateBatteryLevel); } diff --git a/src/SystemTask/SystemTask.h b/src/SystemTask/SystemTask.h index 3e53baed..3812ea91 100644 --- a/src/SystemTask/SystemTask.h +++ b/src/SystemTask/SystemTask.h @@ -18,7 +18,7 @@ namespace Pinetime { class SystemTask { public: enum class Messages {GoToSleep, GoToRunning, OnNewTime, OnNewNotification, BleConnected, - BleFirmwareUpdateStarted, BleFirmwareUpdateFinished, OnTouchEvent, OnButtonEvent + BleFirmwareUpdateStarted, BleFirmwareUpdateFinished, OnTouchEvent, OnButtonEvent, OnDisplayTaskSleeping }; SystemTask(Drivers::SpiMaster &spi, Drivers::St7789 &lcd, diff --git a/src/drivers/Cst816s.cpp b/src/drivers/Cst816s.cpp index 6afbf734..f6816545 100644 --- a/src/drivers/Cst816s.cpp +++ b/src/drivers/Cst816s.cpp @@ -2,7 +2,6 @@ #include #include #include - #include "Cst816s.h" using namespace Pinetime::Drivers; @@ -96,12 +95,16 @@ Cst816S::TouchInfos Cst816S::GetTouchInfo() { } void Cst816S::Sleep() { - // TODO re enable sleep mode - //twiMaster.Sleep(); - nrf_gpio_cfg_default(6); - nrf_gpio_cfg_default(7); + nrf_gpio_pin_clear(pinReset); + vTaskDelay(5); + nrf_gpio_pin_set(pinReset); + vTaskDelay(50); + static constexpr uint8_t sleepValue = 0x03; + twiMaster.Write(twiAddress, 0xA5, &sleepValue, 1); + NRF_LOG_INFO("[TOUCHPANEL] Sleep"); } void Cst816S::Wakeup() { Init(); + NRF_LOG_INFO("[TOUCHPANEL] Wakeup"); } \ No newline at end of file diff --git a/src/drivers/Spi.cpp b/src/drivers/Spi.cpp index bf08178d..2d8aa3b8 100644 --- a/src/drivers/Spi.cpp +++ b/src/drivers/Spi.cpp @@ -1,4 +1,5 @@ #include +#include #include "Spi.h" using namespace Pinetime::Drivers; @@ -18,8 +19,12 @@ bool Spi::Read(uint8_t* cmd, size_t cmdSize, uint8_t *data, size_t dataSize) { } void Spi::Sleep() { - // TODO sleep spi nrf_gpio_cfg_default(pinCsn); + NRF_LOG_INFO("[SPI] Sleep") +} + +bool Spi::WriteCmdAndBuffer(const uint8_t *cmd, size_t cmdSize, const uint8_t *data, size_t dataSize) { + return spiMaster.WriteCmdAndBuffer(pinCsn, cmd, cmdSize, data, dataSize); } bool Spi::Init() { @@ -27,8 +32,10 @@ bool Spi::Init() { return true; } -bool Spi::WriteCmdAndBuffer(const uint8_t *cmd, size_t cmdSize, const uint8_t *data, size_t dataSize) { - return spiMaster.WriteCmdAndBuffer(pinCsn, cmd, cmdSize, data, dataSize); +void Spi::Wakeup() { + nrf_gpio_cfg_output(pinCsn); + nrf_gpio_pin_set(pinCsn); + NRF_LOG_INFO("[SPI] Wakeup") } diff --git a/src/drivers/SpiMaster.cpp b/src/drivers/SpiMaster.cpp index 8d36b9cd..2e5852a5 100644 --- a/src/drivers/SpiMaster.cpp +++ b/src/drivers/SpiMaster.cpp @@ -4,6 +4,7 @@ #include "SpiMaster.h" #include #include +#include using namespace Pinetime::Drivers; @@ -231,10 +232,13 @@ void SpiMaster::Sleep() { nrf_gpio_cfg_default(params.pinSCK); nrf_gpio_cfg_default(params.pinMOSI); nrf_gpio_cfg_default(params.pinMISO); + + NRF_LOG_INFO("[SPIMASTER] sleep") } void SpiMaster::Wakeup() { Init(); + NRF_LOG_INFO("[SPIMASTER] Wakeup"); } bool SpiMaster::WriteCmdAndBuffer(uint8_t pinCsn, const uint8_t *cmd, size_t cmdSize, const uint8_t *data, size_t dataSize) { diff --git a/src/drivers/SpiNorFlash.cpp b/src/drivers/SpiNorFlash.cpp index 7e4da1ca..351a9dfc 100644 --- a/src/drivers/SpiNorFlash.cpp +++ b/src/drivers/SpiNorFlash.cpp @@ -11,8 +11,8 @@ SpiNorFlash::SpiNorFlash(Spi& spi) : spi{spi} { } void SpiNorFlash::Init() { - auto id = ReadIdentificaion(); - NRF_LOG_INFO("[SPI FLASH] Manufacturer : %d, Memory type : %d, memory density : %d", id.manufacturer, id.type, id.density); + device_id = ReadIdentificaion(); + NRF_LOG_INFO("[SPI FLASH] Manufacturer : %d, Memory type : %d, memory density : %d", device_id.manufacturer, device_id.type, device_id.density); } void SpiNorFlash::Uninit() { @@ -20,11 +20,25 @@ void SpiNorFlash::Uninit() { } void SpiNorFlash::Sleep() { - + auto cmd = static_cast(Commands::DeepPowerDown); + spi.Write(&cmd, sizeof(uint8_t)); + NRF_LOG_INFO("[FLASH] Sleep") } void SpiNorFlash::Wakeup() { - + // send Commands::ReleaseFromDeepPowerDown then 3 dummy bytes before reading Device ID + static constexpr uint8_t cmdSize = 4; + uint8_t cmd[cmdSize] = {static_cast(Commands::ReleaseFromDeepPowerDown), 0x01, 0x02, 0x03}; + uint8_t id = 0; + spi.Read(reinterpret_cast(&cmd), cmdSize, &id, 1); + auto devId = device_id = ReadIdentificaion(); + if(devId.type != device_id.type) { + NRF_LOG_INFO("[SpiNorFlash] ID on Wakeup: Failed"); + } + else { + NRF_LOG_INFO("[SpiNorFlash] ID on Wakeup: %d", id); + } + NRF_LOG_INFO("[FLASH] Wakeup") } SpiNorFlash::Identification SpiNorFlash::ReadIdentificaion() { diff --git a/src/drivers/SpiNorFlash.h b/src/drivers/SpiNorFlash.h index 98267c09..10c25a01 100644 --- a/src/drivers/SpiNorFlash.h +++ b/src/drivers/SpiNorFlash.h @@ -48,11 +48,13 @@ namespace Pinetime { SectorErase = 0x20, ReadSecurityRegister = 0x2B, ReadIdentification = 0x9F, + ReleaseFromDeepPowerDown = 0xAB, + DeepPowerDown = 0xB9 }; static constexpr uint16_t pageSize = 256; Spi& spi; - + Identification device_id; }; } } diff --git a/src/drivers/St7789.cpp b/src/drivers/St7789.cpp index 09269afd..ed28c82c 100644 --- a/src/drivers/St7789.cpp +++ b/src/drivers/St7789.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "St7789.h" #include "Spi.h" @@ -174,12 +175,10 @@ void St7789::HardwareReset() { void St7789::Sleep() { SleepIn(); nrf_gpio_cfg_default(pinDataCommand); -// spi.Sleep(); // TODO sleep SPI + NRF_LOG_INFO("[LCD] Sleep"); } void St7789::Wakeup() { -// spi.Wakeup(); // TODO wake up SPI - nrf_gpio_cfg_output(pinDataCommand); // TODO why do we need to reset the controller? HardwareReset(); @@ -193,4 +192,5 @@ void St7789::Wakeup() { NormalModeOn(); VerticalScrollStartAddress(verticalScrollingStartAddress); DisplayOn(); + NRF_LOG_INFO("[LCD] Wakeup") } diff --git a/src/drivers/TwiMaster.cpp b/src/drivers/TwiMaster.cpp index 4a0c536d..14d12f9d 100644 --- a/src/drivers/TwiMaster.cpp +++ b/src/drivers/TwiMaster.cpp @@ -137,4 +137,16 @@ void TwiMaster::Write(uint8_t deviceAddress, const uint8_t *data, size_t size, b uint32_t error = twiBaseAddress->ERRORSRC; twiBaseAddress->ERRORSRC = error; } -} \ No newline at end of file +} + +void TwiMaster::Sleep() { + nrf_gpio_cfg_default(6); + nrf_gpio_cfg_default(7); + twiBaseAddress->ENABLE = 0; + NRF_LOG_INFO("[TWIMASTER] Sleep"); +} + +void TwiMaster::Wakeup() { + Init(); + NRF_LOG_INFO("[TWIMASTER] Wakeup"); +} diff --git a/src/drivers/TwiMaster.h b/src/drivers/TwiMaster.h index 3b7555f5..9b6b5070 100644 --- a/src/drivers/TwiMaster.h +++ b/src/drivers/TwiMaster.h @@ -22,6 +22,9 @@ namespace Pinetime { void Read(uint8_t deviceAddress, uint8_t registerAddress, uint8_t* buffer, size_t size); void Write(uint8_t deviceAddress, uint8_t registerAddress, const uint8_t* data, size_t size); + void Sleep(); + void Wakeup(); + private: void Read(uint8_t deviceAddress, uint8_t* buffer, size_t size, bool stop); void Write(uint8_t deviceAddress, const uint8_t* data, size_t size, bool stop);