forked from InfiniTimeOrg/InfiniTime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSt7789.cpp
205 lines (176 loc) · 5.59 KB
/
St7789.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "drivers/St7789.h"
using namespace Pinetime::Drivers;
St7789::St7789(Spi& spi, uint8_t pinDataCommand, uint8_t pinReset) : spi {spi}, pinDataCommand {pinDataCommand}, pinReset {pinReset} {
}
void St7789::Init() {
nrf_gpio_cfg_output(pinDataCommand);
nrf_gpio_cfg_output(pinReset);
nrf_gpio_pin_set(pinReset);
HardwareReset();
SoftwareReset();
SleepOut();
ColMod();
MemoryDataAccessControl();
ColumnAddressSet();
RowAddressSet();
// P8B Mirrored version does not need display inversion.
#ifndef DRIVER_DISPLAY_MIRROR
DisplayInversionOn();
#endif
NormalModeOn();
SetVdv();
DisplayOn();
}
void St7789::WriteCommand(uint8_t cmd) {
nrf_gpio_pin_clear(pinDataCommand);
WriteSpi(&cmd, 1);
}
void St7789::WriteData(uint8_t data) {
nrf_gpio_pin_set(pinDataCommand);
WriteSpi(&data, 1);
}
void St7789::WriteSpi(const uint8_t* data, size_t size) {
spi.Write(data, size);
}
void St7789::SoftwareReset() {
EnsureSleepOutPostDelay();
WriteCommand(static_cast<uint8_t>(Commands::SoftwareReset));
// 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<uint8_t>(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<uint8_t>(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<uint8_t>(Commands::ColMod));
WriteData(0x55);
}
void St7789::MemoryDataAccessControl() {
WriteCommand(static_cast<uint8_t>(Commands::MemoryDataAccessControl));
#ifdef DRIVER_DISPLAY_MIRROR
// [7] = MY = Page Address Order, 0 = Top to bottom, 1 = Bottom to top
// [6] = MX = Column Address Order, 0 = Left to right, 1 = Right to left
// [5] = MV = Page/Column Order, 0 = Normal mode, 1 = Reverse mode
// [4] = ML = Line Address Order, 0 = LCD refresh from top to bottom, 1 = Bottom to top
// [3] = RGB = RGB/BGR Order, 0 = RGB, 1 = BGR
// [2] = MH = Display Data Latch Order, 0 = LCD refresh from left to right, 1 = Right to left
// [0 .. 1] = Unused
WriteData(0b01000000);
#else
WriteData(0x00);
#endif
}
void St7789::ColumnAddressSet() {
WriteCommand(static_cast<uint8_t>(Commands::ColumnAddressSet));
WriteData(0x00);
WriteData(0x00);
WriteData(Width >> 8u);
WriteData(Width & 0xffu);
}
void St7789::RowAddressSet() {
WriteCommand(static_cast<uint8_t>(Commands::RowAddressSet));
WriteData(0x00);
WriteData(0x00);
WriteData(320u >> 8u);
WriteData(320u & 0xffu);
}
void St7789::DisplayInversionOn() {
WriteCommand(static_cast<uint8_t>(Commands::DisplayInversionOn));
}
void St7789::NormalModeOn() {
WriteCommand(static_cast<uint8_t>(Commands::NormalModeOn));
}
void St7789::DisplayOn() {
WriteCommand(static_cast<uint8_t>(Commands::DisplayOn));
}
void St7789::SetAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) {
WriteCommand(static_cast<uint8_t>(Commands::ColumnAddressSet));
WriteData(x0 >> 8);
WriteData(x0 & 0xff);
WriteData(x1 >> 8);
WriteData(x1 & 0xff);
WriteCommand(static_cast<uint8_t>(Commands::RowAddressSet));
WriteData(y0 >> 8);
WriteData(y0 & 0xff);
WriteData(y1 >> 8);
WriteData(y1 & 0xff);
WriteToRam();
}
void St7789::WriteToRam() {
WriteCommand(static_cast<uint8_t>(Commands::WriteToRam));
}
void St7789::SetVdv() {
// By default there is a large step from pixel brightness zero to one.
// After experimenting with VCOMS, VRH and VDV, this was found to produce good results.
WriteCommand(static_cast<uint8_t>(Commands::VdvSet));
WriteData(0x10);
}
void St7789::DisplayOff() {
WriteCommand(static_cast<uint8_t>(Commands::DisplayOff));
}
void St7789::VerticalScrollStartAddress(uint16_t line) {
verticalScrollingStartAddress = line;
WriteCommand(static_cast<uint8_t>(Commands::VerticalScrollStartAddress));
WriteData(line >> 8u);
WriteData(line & 0x00ffu);
}
void St7789::Uninit() {
}
void St7789::DrawBuffer(uint16_t x, uint16_t y, uint16_t width, uint16_t height, const uint8_t* data, size_t size) {
SetAddrWindow(x, y, x + width - 1, y + height - 1);
nrf_gpio_pin_set(pinDataCommand);
WriteSpi(data, size);
}
void St7789::HardwareReset() {
nrf_gpio_pin_clear(pinReset);
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() {
SleepIn();
nrf_gpio_cfg_default(pinDataCommand);
NRF_LOG_INFO("[LCD] Sleep");
}
void St7789::Wakeup() {
nrf_gpio_cfg_output(pinDataCommand);
SleepOut();
VerticalScrollStartAddress(verticalScrollingStartAddress);
DisplayOn();
NRF_LOG_INFO("[LCD] Wakeup")
}