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