FabGL
ESP32 Display Controller and Graphics Library
TFTControllerSpecif.cpp
1 /*
2  Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) - <http://www.fabgl.com>
3  Copyright (c) 2019-2020 Fabrizio Di Vittorio.
4  All rights reserved.
5 
6  This file is part of FabGL Library.
7 
8  FabGL is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  FabGL is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with FabGL. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 
23 
24 #include "fabutils.h"
25 #include "TFTControllerSpecif.h"
26 
27 
28 
29 namespace fabgl {
30 
31 
32 
35 // ST7789
36 
37 
38 #define ST7789_SWRST 0x01
39 #define ST7789_RDDCOLMOD 0x0C
40 #define ST7789_SLPOUT 0x11
41 #define ST7789_PTLON 0x12
42 #define ST7789_NORON 0x13
43 #define ST7789_INVOFF 0x20
44 #define ST7789_INVON 0x21
45 #define ST7789_DISPON 0x29
46 #define ST7789_PTLAR 0x30
47 #define ST7789_COLMOD 0x3A
48 #define ST7789_WRDISBV 0x51
49 #define ST7789_WRCTRLD 0x53
50 #define ST7789_WRCACE 0x55
51 #define ST7789_WRCABCMB 0x5E
52 #define ST7789_RAMCTRL 0xB0
53 #define ST7789_PORCTRL 0xB2
54 #define ST7789_GCTRL 0xB7
55 #define ST7789_VCOMS 0xBB
56 #define ST7789_LCMCTRL 0xC0
57 #define ST7789_VDVVRHEN 0xC2
58 #define ST7789_VRHS 0xC3
59 #define ST7789_VDVS 0xC4
60 #define ST7789_FRCTRL2 0xC6
61 #define ST7789_PWCTRL1 0xD0
62 #define ST7789_PVGAMCTRL 0xE0
63 #define ST7789_NVGAMCTRL 0xE1
64 
65 
66 void ST7789Controller::softReset()
67 {
68  // software reset
69  SPIBeginWrite();
70  writeCommand(ST7789_SWRST);
71  SPIEndWrite();
72  vTaskDelay(150 / portTICK_PERIOD_MS);
73 
74  SPIBeginWrite();
75 
76  // Sleep Out
77  writeCommand(ST7789_SLPOUT);
78  vTaskDelay(120 / portTICK_PERIOD_MS);
79 
80  // Normal Display Mode On
81  writeCommand(ST7789_NORON);
82 
83  setupOrientation();
84 
85  // 0x55 = 0 (101) 0 (101) => 65K of RGB interface, 16 bit/pixel
86  writeCommand(ST7789_COLMOD);
87  writeByte(0x55);
88  vTaskDelay(10 / portTICK_PERIOD_MS);
89 
90  // Porch Setting
91  writeCommand(ST7789_PORCTRL);
92  writeByte(0x0c);
93  writeByte(0x0c);
94  writeByte(0x00);
95  writeByte(0x33);
96  writeByte(0x33);
97 
98  // Gate Control
99  // VGL = -10.43V
100  // VGH = 13.26V
101  writeCommand(ST7789_GCTRL);
102  writeByte(0x35);
103 
104  // VCOM Setting
105  // 1.1V
106  writeCommand(ST7789_VCOMS);
107  writeByte(0x28);
108 
109  // LCM Control
110  // XMH, XMX
111  writeCommand(ST7789_LCMCTRL);
112  writeByte(0x0C);
113 
114  // VDV and VRH Command Enable
115  // CMDEN = 1, VDV and VRH register value comes from command write.
116  writeCommand(ST7789_VDVVRHEN);
117  writeByte(0x01);
118  writeByte(0xFF);
119 
120  // VRH Set
121  // VAP(GVDD) = 4.35+( vcom+vcom offset+vdv) V
122  // VAN(GVCL) = -4.35+( vcom+vcom offset-vdv) V
123  writeCommand(ST7789_VRHS);
124  writeByte(0x10);
125 
126  // VDV Set
127  // VDV = 0V
128  writeCommand(ST7789_VDVS);
129  writeByte(0x20);
130 
131  // Frame Rate Control in Normal Mode
132  // RTNA = 0xf (60Hz)
133  // NLA = 0 (dot inversion)
134  writeCommand(ST7789_FRCTRL2);
135  writeByte(0x0f);
136 
137  // Power Control 1
138  // VDS = 2.3V
139  // AVCL = -4.8V
140  // AVDD = 6.8v
141  writeCommand(ST7789_PWCTRL1);
142  writeByte(0xa4);
143  writeByte(0xa1);
144 
145  // Positive Voltage Gamma Control
146  writeCommand(ST7789_PVGAMCTRL);
147  writeByte(0xd0);
148  writeByte(0x00);
149  writeByte(0x02);
150  writeByte(0x07);
151  writeByte(0x0a);
152  writeByte(0x28);
153  writeByte(0x32);
154  writeByte(0x44);
155  writeByte(0x42);
156  writeByte(0x06);
157  writeByte(0x0e);
158  writeByte(0x12);
159  writeByte(0x14);
160  writeByte(0x17);
161 
162  // Negative Voltage Gamma Control
163  writeCommand(ST7789_NVGAMCTRL);
164  writeByte(0xd0);
165  writeByte(0x00);
166  writeByte(0x02);
167  writeByte(0x07);
168  writeByte(0x0a);
169  writeByte(0x28);
170  writeByte(0x31);
171  writeByte(0x54);
172  writeByte(0x47);
173  writeByte(0x0e);
174  writeByte(0x1c);
175  writeByte(0x17);
176  writeByte(0x1b);
177  writeByte(0x1e);
178 
179  // Display Inversion On
180  writeCommand(ST7789_INVON);
181 
182  // Display On
183  writeCommand(ST7789_DISPON);
184 
185  SPIEndWrite();
186 }
187 
188 
189 
192 // ILI9341
193 
194 
195 #define ILI9341_SWRESET 0x01
196 #define ILI9341_SLEEPOUT 0x11
197 #define ILI9341_NORON 0x13
198 #define ILI9341_GAMMASET 0x26
199 #define ILI9341_DISPON 0x29
200 #define ILI9341_PIXELFORMATSET 0x3A
201 #define ILI9341_FRAMERATECTRL1 0xB1
202 #define ILI9341_DISPLAYFUNCCTRL 0xB6
203 #define ILI9341_POWERCTR1 0xC0
204 #define ILI9341_POWERCTR2 0xC1
205 #define ILI9341_VCOMCTR1 0xC5
206 #define ILI9341_VCOMCTR2 0xC7
207 #define ILI9341_POWERCTRLA 0xCB
208 #define ILI9341_POWERCTRLB 0xCF
209 #define ILI9341_POSGAMMACORR 0xE0
210 #define ILI9341_NEGGAMMACORR 0xE1
211 #define ILI9341_DRIVERTIMINGCTRLA 0xE8
212 #define ILI9341_DRIVERTIMINGCTRLB 0xEA
213 #define ILI9341_POWERONSEQCTRL 0xED
214 #define ILI9341_DEVICECODE 0xEF
215 #define ILI9341_ENABLE3G 0xF2
216 #define ILI9341_PUMPRATIOCTRL 0xF7
217 
218 
219 
220 void ILI9341Controller::softReset()
221 {
222  m_reverseHorizontal = true;
223 
224  // software reset
225  SPIBeginWrite();
226  writeCommand(ILI9341_SWRESET);
227  SPIEndWrite();
228  vTaskDelay(150 / portTICK_PERIOD_MS);
229 
230  SPIBeginWrite();
231 
232  // unknown but required init sequence!
233  writeCommand(ILI9341_DEVICECODE);
234  writeByte(0x03);
235  writeByte(0x80);
236  writeByte(0x02);
237 
238  // Power control B
239  writeCommand(ILI9341_POWERCTRLB);
240  writeByte(0x00);
241  writeByte(0XC1);
242  writeByte(0X30);
243 
244  // Power on sequence control
245  writeCommand(ILI9341_POWERONSEQCTRL);
246  writeByte(0x64);
247  writeByte(0x03);
248  writeByte(0X12);
249  writeByte(0X81);
250 
251  // Driver timing control A
252  writeCommand(ILI9341_DRIVERTIMINGCTRLA);
253  writeByte(0x85);
254  writeByte(0x00);
255  writeByte(0x78);
256 
257  // Power control A
258  writeCommand(ILI9341_POWERCTRLA);
259  writeByte(0x39);
260  writeByte(0x2C);
261  writeByte(0x00);
262  writeByte(0x34);
263  writeByte(0x02);
264 
265  // Pump ratio control
266  writeCommand(ILI9341_PUMPRATIOCTRL);
267  writeByte(0x20);
268 
269  // Driver timing control B
270  writeCommand(ILI9341_DRIVERTIMINGCTRLB);
271  writeByte(0x00);
272  writeByte(0x00);
273 
274  // Power Control 1
275  writeCommand(ILI9341_POWERCTR1);
276  writeByte(0x23);
277 
278  // Power Control 2
279  writeCommand(ILI9341_POWERCTR2);
280  writeByte(0x10);
281 
282  // VCOM Control 1
283  writeCommand(ILI9341_VCOMCTR1);
284  writeByte(0x3e);
285  writeByte(0x28);
286 
287  // VCOM Control 2
288  writeCommand(ILI9341_VCOMCTR2);
289  writeByte(0x86);
290 
291  setupOrientation();
292 
293  // COLMOD: Pixel Format Set
294  writeCommand(ILI9341_PIXELFORMATSET);
295  writeByte(0x55);
296 
297  // Frame Rate Control (In Normal Mode/Full Colors)
298  writeCommand(ILI9341_FRAMERATECTRL1);
299  writeByte(0x00);
300  writeByte(0x13); // 0x18 79Hz, 0x1B 70Hz (default), 0x13 100Hz
301 
302  // Display Function Control
303  writeCommand(ILI9341_DISPLAYFUNCCTRL);
304  writeByte(0x08);
305  writeByte(0x82);
306  writeByte(0x27);
307 
308  // Enable 3G (gamma control)
309  writeCommand(ILI9341_ENABLE3G);
310  writeByte(0x00); // bit 0: 0 => disable 3G
311 
312  // Gamma Set
313  writeCommand(ILI9341_GAMMASET);
314  writeByte(0x01); // 1 = Gamma curve 1 (G2.2)
315 
316  // Positive Gamma Correction
317  writeCommand(ILI9341_POSGAMMACORR);
318  writeByte(0x0F);
319  writeByte(0x31);
320  writeByte(0x2B);
321  writeByte(0x0C);
322  writeByte(0x0E);
323  writeByte(0x08);
324  writeByte(0x4E);
325  writeByte(0xF1);
326  writeByte(0x37);
327  writeByte(0x07);
328  writeByte(0x10);
329  writeByte(0x03);
330  writeByte(0x0E);
331  writeByte(0x09);
332  writeByte(0x00);
333 
334  // Negative Gamma Correction
335  writeCommand(ILI9341_NEGGAMMACORR);
336  writeByte(0x00);
337  writeByte(0x0E);
338  writeByte(0x14);
339  writeByte(0x03);
340  writeByte(0x11);
341  writeByte(0x07);
342  writeByte(0x31);
343  writeByte(0xC1);
344  writeByte(0x48);
345  writeByte(0x08);
346  writeByte(0x0F);
347  writeByte(0x0C);
348  writeByte(0x31);
349  writeByte(0x36);
350  writeByte(0x0F);
351 
352  // Sleep Out
353  writeCommand(ILI9341_SLEEPOUT);
354 
355  // Normal Display Mode On
356  writeCommand(ILI9341_NORON);
357 
358  SPIEndWrite();
359 
360  vTaskDelay(120 / portTICK_PERIOD_MS);
361 
362  SPIBeginWrite();
363 
364  // Display ON
365  writeCommand(ILI9341_DISPON);
366 
367  SPIEndWrite();
368 }
369 
370 
371 } // end of namespace
This file contains TFT controllers definitions.
This file contains some utility classes and functions.
Definition: canvas.cpp:31