From ff5bdd8432fdbc2104a65d19e022665d14bbcb92 Mon Sep 17 00:00:00 2001 From: mark9064 <30447455+mark9064@users.noreply.github.com> Date: Fri, 27 Oct 2023 00:26:57 +0100 Subject: [PATCH 1/4] Use FreeRTOS delay instead of spinning the CPU --- src/drivers/St7789.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/drivers/St7789.cpp b/src/drivers/St7789.cpp index e583aac84b..ffd8f4d09c 100644 --- a/src/drivers/St7789.cpp +++ b/src/drivers/St7789.cpp @@ -1,6 +1,5 @@ #include "drivers/St7789.h" #include -#include #include #include "drivers/Spi.h" @@ -45,7 +44,7 @@ void St7789::WriteSpi(const uint8_t* data, size_t size) { void St7789::SoftwareReset() { WriteCommand(static_cast(Commands::SoftwareReset)); - nrf_delay_ms(150); + vTaskDelay(pdMS_TO_TICKS(150)); } void St7789::SleepOut() { @@ -59,7 +58,7 @@ void St7789::SleepIn() { void St7789::ColMod() { WriteCommand(static_cast(Commands::ColMod)); WriteData(0x55); - nrf_delay_ms(10); + vTaskDelay(pdMS_TO_TICKS(10)); } void St7789::MemoryDataAccessControl() { @@ -96,12 +95,12 @@ void St7789::RowAddressSet() { void St7789::DisplayInversionOn() { WriteCommand(static_cast(Commands::DisplayInversionOn)); - nrf_delay_ms(10); + vTaskDelay(pdMS_TO_TICKS(10)); } void St7789::NormalModeOn() { WriteCommand(static_cast(Commands::NormalModeOn)); - nrf_delay_ms(10); + vTaskDelay(pdMS_TO_TICKS(10)); } void St7789::DisplayOn() { @@ -137,7 +136,7 @@ void St7789::SetVdv() { void St7789::DisplayOff() { WriteCommand(static_cast(Commands::DisplayOff)); - nrf_delay_ms(500); + vTaskDelay(pdMS_TO_TICKS(500)); } void St7789::VerticalScrollStartAddress(uint16_t line) { @@ -158,7 +157,7 @@ void St7789::DrawBuffer(uint16_t x, uint16_t y, uint16_t width, uint16_t height, void St7789::HardwareReset() { nrf_gpio_pin_clear(pinReset); - nrf_delay_ms(10); + vTaskDelay(pdMS_TO_TICKS(10)); nrf_gpio_pin_set(pinReset); } From 86e7664e68c5ccb5e1c094e4f29fe7571c573d44 Mon Sep 17 00:00:00 2001 From: mark9064 <30447455+mark9064@users.noreply.github.com> Date: Tue, 30 Jan 2024 23:29:16 +0000 Subject: [PATCH 2/4] Apply display driver datasheet delays --- src/drivers/St7789.cpp | 47 ++++++++++++++++++++++++++++++++++-------- src/drivers/St7789.h | 7 +++++++ 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/src/drivers/St7789.cpp b/src/drivers/St7789.cpp index ffd8f4d09c..8da7466ca8 100644 --- a/src/drivers/St7789.cpp +++ b/src/drivers/St7789.cpp @@ -1,7 +1,4 @@ #include "drivers/St7789.h" -#include -#include -#include "drivers/Spi.h" using namespace Pinetime::Drivers; @@ -43,22 +40,52 @@ void St7789::WriteSpi(const uint8_t* data, size_t size) { } void St7789::SoftwareReset() { + EnsureSleepOutPostDelay(); WriteCommand(static_cast(Commands::SoftwareReset)); - vTaskDelay(pdMS_TO_TICKS(150)); + // If sleep in: must wait 120ms before sleep out can sent (see driver datasheet) + // Unconditionally wait as software reset doesn't need to be performant + sleepIn = true; + lastSleepExit = xTaskGetTickCount(); + vTaskDelay(pdMS_TO_TICKS(125)); } void St7789::SleepOut() { + if (!sleepIn) { + return; + } WriteCommand(static_cast(Commands::SleepOut)); + // Wait 5ms for clocks to stabilise + // pdMS rounds down => 6 used here + vTaskDelay(pdMS_TO_TICKS(6)); + // Cannot send sleep in or software reset for 120ms + lastSleepExit = xTaskGetTickCount(); + sleepIn = false; +} + +void St7789::EnsureSleepOutPostDelay() { + TickType_t delta = xTaskGetTickCount() - lastSleepExit; + // Due to timer wraparound, there is a chance of delaying when not necessary + // It is very low (pdMS_TO_TICKS(125)/2^32) and waiting an extra 125ms isn't too bad + if (delta < pdMS_TO_TICKS(125)) { + vTaskDelay(pdMS_TO_TICKS(125) - delta); + } } void St7789::SleepIn() { + if (sleepIn) { + return; + } + EnsureSleepOutPostDelay(); WriteCommand(static_cast(Commands::SleepIn)); + // Wait 5ms for clocks to stabilise + // pdMS rounds down => 6 used here + vTaskDelay(pdMS_TO_TICKS(6)); + sleepIn = true; } void St7789::ColMod() { WriteCommand(static_cast(Commands::ColMod)); WriteData(0x55); - vTaskDelay(pdMS_TO_TICKS(10)); } void St7789::MemoryDataAccessControl() { @@ -95,12 +122,10 @@ void St7789::RowAddressSet() { void St7789::DisplayInversionOn() { WriteCommand(static_cast(Commands::DisplayInversionOn)); - vTaskDelay(pdMS_TO_TICKS(10)); } void St7789::NormalModeOn() { WriteCommand(static_cast(Commands::NormalModeOn)); - vTaskDelay(pdMS_TO_TICKS(10)); } void St7789::DisplayOn() { @@ -136,7 +161,6 @@ void St7789::SetVdv() { void St7789::DisplayOff() { WriteCommand(static_cast(Commands::DisplayOff)); - vTaskDelay(pdMS_TO_TICKS(500)); } void St7789::VerticalScrollStartAddress(uint16_t line) { @@ -157,8 +181,13 @@ void St7789::DrawBuffer(uint16_t x, uint16_t y, uint16_t width, uint16_t height, void St7789::HardwareReset() { nrf_gpio_pin_clear(pinReset); - vTaskDelay(pdMS_TO_TICKS(10)); + vTaskDelay(pdMS_TO_TICKS(1)); nrf_gpio_pin_set(pinReset); + // If hardware reset started while sleep out, reset time may be up to 120ms + // Unconditionally wait as hardware reset doesn't need to be performant + sleepIn = true; + lastSleepExit = xTaskGetTickCount(); + vTaskDelay(pdMS_TO_TICKS(125)); } void St7789::Sleep() { diff --git a/src/drivers/St7789.h b/src/drivers/St7789.h index b00bee03bd..f3cbdc1d5a 100644 --- a/src/drivers/St7789.h +++ b/src/drivers/St7789.h @@ -2,6 +2,10 @@ #include #include +#include +#include +#include "drivers/Spi.h" + namespace Pinetime { namespace Drivers { class Spi; @@ -29,10 +33,13 @@ namespace Pinetime { uint8_t pinDataCommand; uint8_t pinReset; uint8_t verticalScrollingStartAddress = 0; + bool sleepIn; + TickType_t lastSleepExit; void HardwareReset(); void SoftwareReset(); void SleepOut(); + void EnsureSleepOutPostDelay(); void SleepIn(); void ColMod(); void MemoryDataAccessControl(); From c0c5c661dc6990823483fefc037af6911447de7c Mon Sep 17 00:00:00 2001 From: mark9064 <30447455+mark9064@users.noreply.github.com> Date: Mon, 25 Mar 2024 23:05:01 +0000 Subject: [PATCH 3/4] Move includes back --- src/drivers/St7789.cpp | 3 +++ src/drivers/St7789.h | 4 +--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/drivers/St7789.cpp b/src/drivers/St7789.cpp index 8da7466ca8..3abdc9ce99 100644 --- a/src/drivers/St7789.cpp +++ b/src/drivers/St7789.cpp @@ -1,4 +1,7 @@ #include "drivers/St7789.h" +#include +#include +#include "drivers/Spi.h" using namespace Pinetime::Drivers; diff --git a/src/drivers/St7789.h b/src/drivers/St7789.h index f3cbdc1d5a..2e98860009 100644 --- a/src/drivers/St7789.h +++ b/src/drivers/St7789.h @@ -2,9 +2,7 @@ #include #include -#include -#include -#include "drivers/Spi.h" +#include namespace Pinetime { namespace Drivers { From c33847e8458cd972bb76bb3bc81cc469a60f5a1d Mon Sep 17 00:00:00 2001 From: mark9064 <30447455+mark9064@users.noreply.github.com> Date: Fri, 29 Mar 2024 16:29:07 +0000 Subject: [PATCH 4/4] Include task header (Fixes sim) --- src/drivers/St7789.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/drivers/St7789.cpp b/src/drivers/St7789.cpp index 3abdc9ce99..ef4533e6bd 100644 --- a/src/drivers/St7789.cpp +++ b/src/drivers/St7789.cpp @@ -2,6 +2,7 @@ #include #include #include "drivers/Spi.h" +#include "task.h" using namespace Pinetime::Drivers;