11#include " drivers/St7789.h"
2- #include < hal/nrf_gpio.h>
3- #include < nrfx_log.h>
4- #include " drivers/Spi.h"
52
63using namespace Pinetime ::Drivers;
74
@@ -53,22 +50,52 @@ void St7789::WriteSpi(const uint8_t* data, size_t size, const std::function<void
5350}
5451
5552void St7789::SoftwareReset () {
53+ EnsureSleepOutPostDelay ();
5654 WriteCommand (static_cast <uint8_t >(Commands::SoftwareReset));
57- vTaskDelay (pdMS_TO_TICKS (150 ));
55+ // If sleep in: must wait 120ms before sleep out can sent (see driver datasheet)
56+ // Unconditionally wait as software reset doesn't need to be performant
57+ sleepIn = true ;
58+ lastSleepExit = xTaskGetTickCount ();
59+ vTaskDelay (pdMS_TO_TICKS (125 ));
5860}
5961
6062void St7789::SleepOut () {
63+ if (!sleepIn) {
64+ return ;
65+ }
6166 WriteCommand (static_cast <uint8_t >(Commands::SleepOut));
67+ // Wait 5ms for clocks to stabilise
68+ // pdMS rounds down => 6 used here
69+ vTaskDelay (pdMS_TO_TICKS (6 ));
70+ // Cannot send sleep in or software reset for 120ms
71+ lastSleepExit = xTaskGetTickCount ();
72+ sleepIn = false ;
73+ }
74+
75+ void St7789::EnsureSleepOutPostDelay () {
76+ TickType_t delta = xTaskGetTickCount () - lastSleepExit;
77+ // Due to timer wraparound, there is a chance of delaying when not necessary
78+ // It is very low (pdMS_TO_TICKS(125)/2^32) and waiting an extra 125ms isn't too bad
79+ if (delta < pdMS_TO_TICKS (125 )) {
80+ vTaskDelay (pdMS_TO_TICKS (125 ) - delta);
81+ }
6282}
6383
6484void St7789::SleepIn () {
85+ if (sleepIn) {
86+ return ;
87+ }
88+ EnsureSleepOutPostDelay ();
6589 WriteCommand (static_cast <uint8_t >(Commands::SleepIn));
90+ // Wait 5ms for clocks to stabilise
91+ // pdMS rounds down => 6 used here
92+ vTaskDelay (pdMS_TO_TICKS (6 ));
93+ sleepIn = true ;
6694}
6795
6896void St7789::ColMod () {
6997 WriteCommand (static_cast <uint8_t >(Commands::ColMod));
7098 WriteData (0x55 );
71- vTaskDelay (pdMS_TO_TICKS (10 ));
7299}
73100
74101void St7789::MemoryDataAccessControl () {
@@ -105,12 +132,10 @@ void St7789::RowAddressSet() {
105132
106133void St7789::DisplayInversionOn () {
107134 WriteCommand (static_cast <uint8_t >(Commands::DisplayInversionOn));
108- vTaskDelay (pdMS_TO_TICKS (10 ));
109135}
110136
111137void St7789::NormalModeOn () {
112138 WriteCommand (static_cast <uint8_t >(Commands::NormalModeOn));
113- vTaskDelay (pdMS_TO_TICKS (10 ));
114139}
115140
116141void St7789::DisplayOn () {
@@ -145,7 +170,6 @@ void St7789::SetVdv() {
145170
146171void St7789::DisplayOff () {
147172 WriteCommand (static_cast <uint8_t >(Commands::DisplayOff));
148- vTaskDelay (pdMS_TO_TICKS (500 ));
149173}
150174
151175void St7789::VerticalScrollStartAddress (uint16_t line) {
@@ -165,8 +189,13 @@ void St7789::DrawBuffer(uint16_t x, uint16_t y, uint16_t width, uint16_t height,
165189
166190void St7789::HardwareReset () {
167191 nrf_gpio_pin_clear (pinReset);
168- vTaskDelay (pdMS_TO_TICKS (10 ));
192+ vTaskDelay (pdMS_TO_TICKS (1 ));
169193 nrf_gpio_pin_set (pinReset);
194+ // If hardware reset started while sleep out, reset time may be up to 120ms
195+ // Unconditionally wait as hardware reset doesn't need to be performant
196+ sleepIn = true ;
197+ lastSleepExit = xTaskGetTickCount ();
198+ vTaskDelay (pdMS_TO_TICKS (125 ));
170199}
171200
172201void St7789::Sleep () {
0 commit comments