Skip to content

Commit 7b11101

Browse files
mark9064JF002
authored andcommitted
Apply display driver datasheet delays
1 parent 7e460d3 commit 7b11101

2 files changed

Lines changed: 45 additions & 9 deletions

File tree

src/drivers/St7789.cpp

Lines changed: 38 additions & 9 deletions
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

@@ -53,22 +50,52 @@ void St7789::WriteSpi(const uint8_t* data, size_t size, const std::function<void
5350
}
5451

5552
void 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

6062
void 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

6484
void 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

6896
void St7789::ColMod() {
6997
WriteCommand(static_cast<uint8_t>(Commands::ColMod));
7098
WriteData(0x55);
71-
vTaskDelay(pdMS_TO_TICKS(10));
7299
}
73100

74101
void St7789::MemoryDataAccessControl() {
@@ -105,12 +132,10 @@ void St7789::RowAddressSet() {
105132

106133
void St7789::DisplayInversionOn() {
107134
WriteCommand(static_cast<uint8_t>(Commands::DisplayInversionOn));
108-
vTaskDelay(pdMS_TO_TICKS(10));
109135
}
110136

111137
void St7789::NormalModeOn() {
112138
WriteCommand(static_cast<uint8_t>(Commands::NormalModeOn));
113-
vTaskDelay(pdMS_TO_TICKS(10));
114139
}
115140

116141
void St7789::DisplayOn() {
@@ -145,7 +170,6 @@ void St7789::SetVdv() {
145170

146171
void St7789::DisplayOff() {
147172
WriteCommand(static_cast<uint8_t>(Commands::DisplayOff));
148-
vTaskDelay(pdMS_TO_TICKS(500));
149173
}
150174

151175
void 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

166190
void 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

172201
void St7789::Sleep() {

src/drivers/St7789.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
#include <cstdint>
44
#include <functional>
55

6+
#include <hal/nrf_gpio.h>
7+
#include <nrfx_log.h>
8+
#include "drivers/Spi.h"
9+
610
namespace Pinetime {
711
namespace Drivers {
812
class Spi;
@@ -30,10 +34,13 @@ namespace Pinetime {
3034
uint8_t pinDataCommand;
3135
uint8_t pinReset;
3236
uint8_t verticalScrollingStartAddress = 0;
37+
bool sleepIn;
38+
TickType_t lastSleepExit;
3339

3440
void HardwareReset();
3541
void SoftwareReset();
3642
void SleepOut();
43+
void EnsureSleepOutPostDelay();
3744
void SleepIn();
3845
void ColMod();
3946
void MemoryDataAccessControl();

0 commit comments

Comments
 (0)