Author: JF <jf@codingfield.com>
Re-implement sleep/wakeup for touch panel, display, NOR Flash, SPI and TWI.
src/DisplayApp/DisplayApp.cpp | 8 ++------ src/SystemTask/SystemTask.cpp | 15 +++++++++++++++ src/SystemTask/SystemTask.h | 2 +- src/drivers/Cst816s.cpp | 13 ++++++++----- src/drivers/Spi.cpp | 13 ++++++++++--- src/drivers/SpiMaster.cpp | 4 ++++ src/drivers/SpiNorFlash.cpp | 22 ++++++++++++++++++---- src/drivers/SpiNorFlash.h | 4 +++- src/drivers/St7789.cpp | 6 +++--- src/drivers/TwiMaster.cpp | 14 +++++++++++++- src/drivers/TwiMaster.h | 3 +++
diff --git a/src/DisplayApp/DisplayApp.cpp b/src/DisplayApp/DisplayApp.cpp index 208437a1a75531b9584952c2a23904fbee6eea08..f6138ec756d0adc30ebbf1c82fd1af1c5d26acc2 100644 --- a/src/DisplayApp/DisplayApp.cpp +++ b/src/DisplayApp/DisplayApp.cpp @@ -95,14 +95,10 @@ brightnessController.Lower(); 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 @@ break; } } - 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 1ee8e9184c43f8aff205bd074264b6351daf78cf..b515d1abc00dfc5d0f2181095fde9eea5c70be74 100644 --- a/src/SystemTask/SystemTask.cpp +++ b/src/SystemTask/SystemTask.cpp @@ -145,6 +145,14 @@ break; 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::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 3e53baedafad7689c8050bc12c410c38619571a2..3812ea91082c910e0bdaed6e14b497daaabbdb93 100644 --- a/src/SystemTask/SystemTask.h +++ b/src/SystemTask/SystemTask.h @@ -18,7 +18,7 @@ namespace System { 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 6afbf734ba4864bda06c57ef501d342433d58c0f..f6816545f80d134bd98982c4d9b85dbdc593841e 100644 --- a/src/drivers/Cst816s.cpp +++ b/src/drivers/Cst816s.cpp @@ -2,7 +2,6 @@ #include#include <task.h> #include <nrfx_log.h> #include <legacy/nrf_drv_gpiote.h> - #include "Cst816s.h" using namespace Pinetime::Drivers; @@ -96,12 +95,16 @@ return info; } 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 bf08178d8663f7276dab8c08fd6da26dd90b712d..2d8aa3b8408e7969720814e3ec8319dbec99d327 100644 --- a/src/drivers/Spi.cpp +++ b/src/drivers/Spi.cpp @@ -1,4 +1,5 @@ #include <hal/nrf_gpio.h> +#include <nrfx_log.h> #include "Spi.h" using namespace Pinetime::Drivers; @@ -18,8 +19,12 @@ return spiMaster.Read(pinCsn, cmd, cmdSize, data, 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 @@ nrf_gpio_pin_set(pinCsn); /* disable Set slave select (inactive high) */ 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 8d36b9cdb2d4e1af8c3d521e41210637fae46161..2e5852a5d8436cc15100a2d34eb85c3e735c0e81 100644 --- a/src/drivers/SpiMaster.cpp +++ b/src/drivers/SpiMaster.cpp @@ -4,6 +4,7 @@ #include #include "SpiMaster.h" #include <algorithm> #include <task.h> +#include <nrfx_log.h> using namespace Pinetime::Drivers; @@ -231,10 +232,13 @@ } 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 7e4da1caeb73a4d1fb81bc9ba1a98a5726a15109..351a9dfc6f1d99d48a8873d1d366d1539faba42a 100644 --- a/src/drivers/SpiNorFlash.cpp +++ b/src/drivers/SpiNorFlash.cpp @@ -11,8 +11,8 @@ } 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::Sleep() { - + auto cmd = static_cast<uint8_t>(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<uint8_t>(Commands::ReleaseFromDeepPowerDown), 0x01, 0x02, 0x03}; + uint8_t id = 0; + spi.Read(reinterpret_cast<uint8_t *>(&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 98267c0933f03b75358afa304568f135d39b4413..10c25a0171a1fdfd0f981edf0a523b6564c7fc72 100644 --- a/src/drivers/SpiNorFlash.h +++ b/src/drivers/SpiNorFlash.h @@ -48,11 +48,13 @@ ReadConfigurationRegister = 0x15, 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 09269afd77a5249ade7f5cde52d396e88865068e..ed28c82c31082e03c7e6a82656e37ac632f84790 100644 --- a/src/drivers/St7789.cpp +++ b/src/drivers/St7789.cpp @@ -1,5 +1,6 @@ #include <hal/nrf_gpio.h> #include <libraries/delay/nrf_delay.h> +#include <nrfx_log.h> #include "St7789.h" #include "Spi.h" @@ -174,12 +175,10 @@ 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 @@ DisplayInversionOn(); NormalModeOn(); VerticalScrollStartAddress(verticalScrollingStartAddress); DisplayOn(); + NRF_LOG_INFO("[LCD] Wakeup") } diff --git a/src/drivers/TwiMaster.cpp b/src/drivers/TwiMaster.cpp index 4a0c536d1cf45ed2a6c422ee271b70c00dc739de..14d12f9de07d2071a467710495e60267515ed9c5 100644 --- a/src/drivers/TwiMaster.cpp +++ b/src/drivers/TwiMaster.cpp @@ -137,4 +137,16 @@ twiBaseAddress->EVENTS_ERROR = 0x0UL; 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 3b7555f5377283a66c7a7855d472d36fb5731119..9b6b5070e49ef407c3c84a27da78bdf7b3f94fb9 100644 --- a/src/drivers/TwiMaster.h +++ b/src/drivers/TwiMaster.h @@ -22,6 +22,9 @@ void Init(); 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);