Skip to content

Commit aff747a

Browse files
committed
Apply display driver datasheet delays
1 parent ff5bdd8 commit aff747a

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
lines changed

src/drivers/St7789.cpp

+38-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
#include "drivers/St7789.h"
2-
#include <hal/nrf_gpio.h>
3-
#include <nrfx_log.h>
4-
#include "drivers/Spi.h"
52

63
using namespace Pinetime::Drivers;
74

@@ -43,22 +40,52 @@ void St7789::WriteSpi(const uint8_t* data, size_t size) {
4340
}
4441

4542
void St7789::SoftwareReset() {
43+
EnsureSleepOutPostDelay();
4644
WriteCommand(static_cast<uint8_t>(Commands::SoftwareReset));
47-
vTaskDelay(pdMS_TO_TICKS(150));
45+
// If sleep in: must wait 120ms before sleep out can sent (see driver datasheet)
46+
// Unconditionally wait as software reset doesn't need to be performant
47+
sleepIn = true;
48+
lastSleepExit = xTaskGetTickCount();
49+
vTaskDelay(pdMS_TO_TICKS(125));
4850
}
4951

5052
void St7789::SleepOut() {
53+
if (!sleepIn) {
54+
return;
55+
}
5156
WriteCommand(static_cast<uint8_t>(Commands::SleepOut));
57+
// Wait 5ms for clocks to stabilise
58+
// pdMS rounds down => 6 used here
59+
vTaskDelay(pdMS_TO_TICKS(6));
60+
// Cannot send sleep in or software reset for 120ms
61+
lastSleepExit = xTaskGetTickCount();
62+
sleepIn = false;
63+
}
64+
65+
void St7789::EnsureSleepOutPostDelay() {
66+
TickType_t delta = xTaskGetTickCount() - lastSleepExit;
67+
// Due to timer wraparound, there is a chance of delaying when not necessary
68+
// It is very low (pdMS_TO_TICKS(125)/2^32) and waiting an extra 125ms isn't too bad
69+
if (delta >= 0 && delta < pdMS_TO_TICKS(125)) {
70+
vTaskDelay(pdMS_TO_TICKS(125) - delta);
71+
}
5272
}
5373

5474
void St7789::SleepIn() {
75+
if (sleepIn) {
76+
return;
77+
}
78+
EnsureSleepOutPostDelay();
5579
WriteCommand(static_cast<uint8_t>(Commands::SleepIn));
80+
// Wait 5ms for clocks to stabilise
81+
// pdMS rounds down => 6 used here
82+
vTaskDelay(pdMS_TO_TICKS(6));
83+
sleepIn = true;
5684
}
5785

5886
void St7789::ColMod() {
5987
WriteCommand(static_cast<uint8_t>(Commands::ColMod));
6088
WriteData(0x55);
61-
vTaskDelay(pdMS_TO_TICKS(10));
6289
}
6390

6491
void St7789::MemoryDataAccessControl() {
@@ -95,12 +122,10 @@ void St7789::RowAddressSet() {
95122

96123
void St7789::DisplayInversionOn() {
97124
WriteCommand(static_cast<uint8_t>(Commands::DisplayInversionOn));
98-
vTaskDelay(pdMS_TO_TICKS(10));
99125
}
100126

101127
void St7789::NormalModeOn() {
102128
WriteCommand(static_cast<uint8_t>(Commands::NormalModeOn));
103-
vTaskDelay(pdMS_TO_TICKS(10));
104129
}
105130

106131
void St7789::DisplayOn() {
@@ -136,7 +161,6 @@ void St7789::SetVdv() {
136161

137162
void St7789::DisplayOff() {
138163
WriteCommand(static_cast<uint8_t>(Commands::DisplayOff));
139-
vTaskDelay(pdMS_TO_TICKS(500));
140164
}
141165

142166
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,
157181

158182
void St7789::HardwareReset() {
159183
nrf_gpio_pin_clear(pinReset);
160-
vTaskDelay(pdMS_TO_TICKS(10));
184+
vTaskDelay(pdMS_TO_TICKS(1));
161185
nrf_gpio_pin_set(pinReset);
186+
// If hardware reset started while sleep out, reset time may be up to 120ms
187+
// Unconditionally wait as hardware reset doesn't need to be performant
188+
sleepIn = true;
189+
lastSleepExit = xTaskGetTickCount();
190+
vTaskDelay(pdMS_TO_TICKS(125));
162191
}
163192

164193
void St7789::Sleep() {

src/drivers/St7789.h

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
#include <cstddef>
33
#include <cstdint>
44

5+
#include <hal/nrf_gpio.h>
6+
#include <nrfx_log.h>
7+
#include "drivers/Spi.h"
8+
59
namespace Pinetime {
610
namespace Drivers {
711
class Spi;
@@ -29,10 +33,13 @@ namespace Pinetime {
2933
uint8_t pinDataCommand;
3034
uint8_t pinReset;
3135
uint8_t verticalScrollingStartAddress = 0;
36+
bool sleepIn;
37+
TickType_t lastSleepExit;
3238

3339
void HardwareReset();
3440
void SoftwareReset();
3541
void SleepOut();
42+
void EnsureSleepOutPostDelay();
3643
void SleepIn();
3744
void ColMod();
3845
void MemoryDataAccessControl();

0 commit comments

Comments
 (0)