forked from prenticedavid/MCUFRIEND_kbv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MCUFRIEND_kbv.cpp
2787 lines (2692 loc) · 114 KB
/
MCUFRIEND_kbv.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//#define SUPPORT_0139 //not working +238 bytes
#define SUPPORT_0154 //S6D0154 +320 bytes
//#define SUPPORT_1289 //costs about 408 bytes
//#define SUPPORT_1580 //R61580 Untested
#define SUPPORT_1963 //only works with 16BIT bus anyway
//#define SUPPORT_4532 //LGDP4532 +120 bytes. thanks Leodino
#define SUPPORT_4535 //LGDP4535 +180 bytes
#define SUPPORT_68140 //RM68140 +52 bytes defaults to PIXFMT=0x55
#define SUPPORT_7781 //ST7781 +172 bytes
//#define SUPPORT_8230 //UC8230 +118 bytes
//#define SUPPORT_8347D //HX8347-D, HX8347-G, HX8347-I +520 bytes, 0.27s
//#define SUPPORT_8347A //HX8347-A +500 bytes, 0.27s
//#define SUPPORT_8352A //HX8352A +486 bytes, 0.27s
//#define SUPPORT_8352B //HX8352B UNTESTED
#define SUPPORT_9225 //ILI9225-B, ILI9225-G ID=0x9225, ID=0x9226
//#define SUPPORT_9326_5420 //ILI9326, SPFD5420 +246 bytes
//#define SUPPORT_9342 //costs +114 bytes
//#define SUPPORT_9806
#define SUPPORT_9488_555 //costs +230 bytes, 0.03s / 0.19s
#define SUPPORT_B509_7793 //R61509, ST7793 +244 bytes
#define OFFSET_9327 32 //costs about 103 bytes, 0.08s
#include "MCUFRIEND_kbv.h"
#if defined(USE_SERIAL)
#include "utility/mcufriend_serial.h"
//uint8_t running;
#elif defined(__MBED__)
#include "utility/mcufriend_mbed.h"
#elif defined(__CC_ARM) || defined(__CROSSWORKS_ARM)
#include "utility/mcufriend_keil.h"
#else
#include "utility/mcufriend_shield.h"
#endif
#define MIPI_DCS_REV1 (1<<0)
#define AUTO_READINC (1<<1)
#define READ_BGR (1<<2)
#define READ_LOWHIGH (1<<3)
#define READ_24BITS (1<<4)
#define XSA_XEA_16BIT (1<<5)
#define READ_NODUMMY (1<<6)
#define INVERT_GS (1<<8)
#define INVERT_SS (1<<9)
#define MV_AXIS (1<<10)
#define INVERT_RGB (1<<11)
#define REV_SCREEN (1<<12)
#define FLIP_VERT (1<<13)
#define FLIP_HORIZ (1<<14)
#if (defined(USES_16BIT_BUS)) //only comes from SPECIALs
#define USING_16BIT_BUS 1
#else
#define USING_16BIT_BUS 0
#endif
#if defined USE_GFX_KBV
MCUFRIEND_kbv::MCUFRIEND_kbv():Adafruit_GFX(240, 320)
{
// we can not access GPIO pins until AHB has been enabled.
}
#else
MCUFRIEND_kbv::MCUFRIEND_kbv(int CS, int RS, int WR, int RD, int RST):Adafruit_GFX(240, 320)
{
// we can not access GPIO pins until AHB has been enabled.
}
#endif
static uint8_t done_reset, is8347, is555;
static uint16_t color565_to_555(uint16_t color) {
return (color & 0xFFC0) | ((color & 0x1F) << 1) | ((color & 0x01)); //lose Green LSB, extend Blue LSB
}
static uint16_t color555_to_565(uint16_t color) {
return (color & 0xFFC0) | ((color & 0x0400) >> 5) | ((color & 0x3F) >> 1); //extend Green LSB
}
void MCUFRIEND_kbv::reset(void)
{
done_reset = 1;
setWriteDir();
CTL_INIT();
CS_IDLE;
RD_IDLE;
WR_IDLE;
RESET_IDLE;
delay(50);
RESET_ACTIVE;
delay(100);
RESET_IDLE;
delay(100);
WriteCmdData(0xB0, 0x0000); //R61520 needs this to read ID
}
static void writecmddata(uint16_t cmd, uint16_t dat)
{
CS_ACTIVE;
WriteCmd(cmd);
WriteData(dat);
CS_IDLE;
}
void MCUFRIEND_kbv::WriteCmdData(uint16_t cmd, uint16_t dat) { writecmddata(cmd, dat); }
static void WriteCmdParamN(uint16_t cmd, int8_t N, uint8_t * block)
{
CS_ACTIVE;
WriteCmd(cmd);
while (N-- > 0) {
uint8_t u8 = *block++;
write8(u8);
if (N && is8347) {
cmd++;
WriteCmd(cmd);
}
}
CS_IDLE;
}
static inline void WriteCmdParam4(uint8_t cmd, uint8_t d1, uint8_t d2, uint8_t d3, uint8_t d4)
{
uint8_t d[4];
d[0] = d1, d[1] = d2, d[2] = d3, d[3] = d4;
WriteCmdParamN(cmd, 4, d);
}
//#define WriteCmdParam4(cmd, d1, d2, d3, d4) {uint8_t d[4];d[0] = d1, d[1] = d2, d[2] = d3, d[3] = d4;WriteCmdParamN(cmd, 4, d);}
void MCUFRIEND_kbv::pushCommand(uint16_t cmd, uint8_t * block, int8_t N) { WriteCmdParamN(cmd, N, block); }
static uint16_t read16bits(void)
{
uint16_t ret;
uint8_t lo;
#if USING_16BIT_BUS
READ_16(ret); //single strobe to read whole bus
if (ret > 255) //ID might say 0x00D3
return ret;
#else
READ_8(ret);
#endif
//all MIPI_DCS_REV1 style params are 8-bit
READ_8(lo);
return (ret << 8) | lo;
}
uint16_t MCUFRIEND_kbv::readReg(uint16_t reg, int8_t index)
{
uint16_t ret;
uint8_t lo;
if (!done_reset)
reset();
CS_ACTIVE;
WriteCmd(reg);
setReadDir();
delay(1); //1us should be adequate
// READ_16(ret);
do { ret = read16bits(); }while (--index >= 0); //need to test with SSD1963
RD_IDLE;
CS_IDLE;
setWriteDir();
return ret;
}
uint32_t MCUFRIEND_kbv::readReg32(uint16_t reg)
{
uint16_t h = readReg(reg, 0);
uint16_t l = readReg(reg, 1);
return ((uint32_t) h << 16) | (l);
}
uint32_t MCUFRIEND_kbv::readReg40(uint16_t reg)
{
uint16_t h = readReg(reg, 0);
uint16_t m = readReg(reg, 1);
uint16_t l = readReg(reg, 2);
return ((uint32_t) h << 24) | (m << 8) | (l >> 8);
}
uint16_t MCUFRIEND_kbv::readID(void)
{
uint16_t ret, ret2;
uint8_t msb;
ret = readReg(0); //forces a reset() if called before begin()
if (ret == 0x5408) //the SPFD5408 fails the 0xD3D3 test.
return 0x5408;
if (ret == 0x5420) //the SPFD5420 fails the 0xD3D3 test.
return 0x5420;
if (ret == 0x8989) //SSD1289 is always 8989
return 0x1289;
ret = readReg(0x67); //HX8347-A
if (ret == 0x4747)
return 0x8347;
//#if defined(SUPPORT_1963) && USING_16BIT_BUS
ret = readReg32(0xA1); //SSD1963: [01 57 61 01]
if (ret == 0x6101)
return 0x1963;
if (ret == 0xFFFF) //R61526: [xx FF FF FF]
return 0x1526; //subsequent begin() enables Command Access
// if (ret == 0xFF00) //R61520: [xx FF FF 00]
// return 0x1520; //subsequent begin() enables Command Access
//#endif
ret = readReg40(0xBF);
if (ret == 0x8357) //HX8357B: [xx 01 62 83 57 FF]
return 0x8357;
if (ret == 0x9481) //ILI9481: [xx 02 04 94 81 FF]
return 0x9481;
if (ret == 0x1511) //?R61511: [xx 02 04 15 11] not tested yet
return 0x1511;
if (ret == 0x1520) //?R61520: [xx 01 22 15 20]
return 0x1520;
if (ret == 0x1526) //?R61526: [xx 01 22 15 26]
return 0x1526;
if (ret == 0x1581) //R61581: [xx 01 22 15 81]
return 0x1581;
if (ret == 0x1400) //?RM68140:[xx FF 68 14 00] not tested yet
return 0x6814;
ret = readReg32(0xD4);
if (ret == 0x5310) //NT35310: [xx 01 53 10]
return 0x5310;
ret = readReg32(0xD7);
if (ret == 0x8031) //weird unknown from BangGood [xx 20 80 31] PrinceCharles
return 0x8031;
ret = readReg40(0xEF); //ILI9327: [xx 02 04 93 27 FF]
if (ret == 0x9327)
return 0x9327;
ret = readReg32(0xFE) >> 8; //weird unknown from BangGood [04 20 53]
if (ret == 0x2053)
return 0x2053;
uint32_t ret32 = readReg32(0x04);
msb = ret32 >> 16;
ret = ret32;
// if (msb = 0x38 && ret == 0x8000) //unknown [xx 38 80 00] with D3 = 0x1602
if (msb == 0x00 && ret == 0x8000) { //HX8357-D [xx 00 80 00]
#if 1
uint8_t cmds[] = {0xFF, 0x83, 0x57};
pushCommand(0xB9, cmds, 3);
msb = readReg(0xD0);
if (msb == 0x99 || msb == 0x90)
#endif
return 0x9090; //BIG CHANGE: HX8357-D was 0x8357
}
// if (msb == 0xFF && ret == 0xFFFF) //R61526 [xx FF FF FF]
// return 0x1526; //subsequent begin() enables Command Access
if (ret == 0x1526) //R61526 [xx 06 15 26] if I have written NVM
return 0x1526; //subsequent begin() enables Command Access
if (ret == 0x8552) //ST7789V: [xx 85 85 52]
return 0x7789;
if (ret == 0xAC11) //?unknown [xx 61 AC 11]
return 0xAC11;
ret = readReg32(0xD3); //for ILI9488, 9486, 9340, 9341
msb = ret >> 8;
if (msb == 0x93 || msb == 0x94 || msb == 0x98 || msb == 0x77 || msb == 0x16)
return ret; //0x9488, 9486, 9340, 9341, 7796
if (ret == 0x00D3 || ret == 0xD3D3)
return ret; //16-bit write-only bus
/*
msb = 0x12; //read 3rd,4th byte. does not work in parallel
pushCommand(0xD9, &msb, 1);
ret2 = readReg(0xD3);
msb = 0x13;
pushCommand(0xD9, &msb, 1);
ret = (ret2 << 8) | readReg(0xD3);
// if (ret2 == 0x93)
return ret2;
*/
return readReg(0); //0154, 7783, 9320, 9325, 9335, B505, B509
}
// independent cursor and window registers. S6D0154, ST7781 increments. ILI92320/5 do not.
int16_t MCUFRIEND_kbv::readGRAM(int16_t x, int16_t y, uint16_t * block, int16_t w, int16_t h)
{
uint16_t ret, dummy, _MR = _MW;
int16_t n = w * h, row = 0, col = 0;
uint8_t r, g, b, tmp;
if (!is8347 && _lcd_capable & MIPI_DCS_REV1) // HX8347 uses same register
_MR = 0x2E;
setAddrWindow(x, y, x + w - 1, y + h - 1);
while (n > 0) {
if (!(_lcd_capable & MIPI_DCS_REV1)) {
WriteCmdData(_MC, x + col);
WriteCmdData(_MP, y + row);
}
CS_ACTIVE;
WriteCmd(_MR);
setReadDir();
if (_lcd_capable & READ_NODUMMY) {
;
} else if ((_lcd_capable & MIPI_DCS_REV1) || _lcd_ID == 0x1289) {
READ_8(r);
} else {
READ_16(dummy);
}
if (_lcd_ID == 0x1511) READ_8(r); //extra dummy for R61511
while (n) {
if (_lcd_capable & READ_24BITS) {
READ_8(r);
READ_8(g);
READ_8(b);
if (_lcd_capable & READ_BGR)
ret = color565(b, g, r);
else
ret = color565(r, g, b);
} else {
READ_16(ret);
if (_lcd_capable & READ_LOWHIGH)
ret = (ret >> 8) | (ret << 8);
if (_lcd_capable & READ_BGR)
ret = (ret & 0x07E0) | (ret >> 11) | (ret << 11);
}
#if defined(SUPPORT_9488_555)
if (is555) ret = color555_to_565(ret);
#endif
*block++ = ret;
n--;
if (!(_lcd_capable & AUTO_READINC))
break;
}
if (++col >= w) {
col = 0;
if (++row >= h)
row = 0;
}
RD_IDLE;
CS_IDLE;
setWriteDir();
}
if (!(_lcd_capable & MIPI_DCS_REV1))
setAddrWindow(0, 0, width() - 1, height() - 1);
return 0;
}
void MCUFRIEND_kbv::setRotation(uint8_t r)
{
uint16_t GS, SS_v, ORG, REV = _lcd_rev;
uint8_t val, d[3];
rotation = r & 3; // just perform the operation ourselves on the protected variables
_width = (rotation & 1) ? HEIGHT : WIDTH;
_height = (rotation & 1) ? WIDTH : HEIGHT;
switch (rotation) {
case 0: //PORTRAIT:
val = 0x48; //MY=0, MX=1, MV=0, ML=0, BGR=1
break;
case 1: //LANDSCAPE: 90 degrees
val = 0x28; //MY=0, MX=0, MV=1, ML=0, BGR=1
break;
case 2: //PORTRAIT_REV: 180 degrees
val = 0x98; //MY=1, MX=0, MV=0, ML=1, BGR=1
break;
case 3: //LANDSCAPE_REV: 270 degrees
val = 0xF8; //MY=1, MX=1, MV=1, ML=1, BGR=1
break;
}
if (_lcd_capable & INVERT_GS)
val ^= 0x80;
if (_lcd_capable & INVERT_SS)
val ^= 0x40;
if (_lcd_capable & INVERT_RGB)
val ^= 0x08;
if (_lcd_capable & MIPI_DCS_REV1) {
if (_lcd_ID == 0x6814) { //.kbv my weird 0x9486 might be 68140
GS = (val & 0x80) ? (1 << 6) : 0; //MY
SS_v = (val & 0x40) ? (1 << 5) : 0; //MX
val &= 0x28; //keep MV, BGR, MY=0, MX=0, ML=0
d[0] = 0;
d[1] = GS | SS_v | 0x02; //MY, MX
d[2] = 0x3B;
WriteCmdParamN(0xB6, 3, d);
goto common_MC;
} else if (_lcd_ID == 0x1963 || _lcd_ID == 0x9481 || _lcd_ID == 0x1511) {
if (val & 0x80)
val |= 0x01; //GS
if ((val & 0x40))
val |= 0x02; //SS
if (_lcd_ID == 0x1963) val &= ~0xC0;
if (_lcd_ID == 0x9481) val &= ~0xD0;
if (_lcd_ID == 0x1511) {
val &= ~0x10; //remove ML
val |= 0xC0; //force penguin 180 rotation
}
// val &= (_lcd_ID == 0x1963) ? ~0xC0 : ~0xD0; //MY=0, MX=0 with ML=0 for ILI9481
goto common_MC;
} else if (is8347) {
_MC = 0x02, _MP = 0x06, _MW = 0x22, _SC = 0x02, _EC = 0x04, _SP = 0x06, _EP = 0x08;
if (_lcd_ID == 0x5252) {
val |= 0x02; //VERT_SCROLLON
if (val & 0x10) val |= 0x04; //if (ML) SS=1 kludge mirror in XXX_REV modes
}
goto common_BGR;
}
common_MC:
_MC = 0x2A, _MP = 0x2B, _MW = 0x2C, _SC = 0x2A, _EC = 0x2A, _SP = 0x2B, _EP = 0x2B;
common_BGR:
WriteCmdParamN(is8347 ? 0x16 : 0x36, 1, &val);
_lcd_madctl = val;
// if (_lcd_ID == 0x1963) WriteCmdParamN(0x13, 0, NULL); //NORMAL mode
}
// cope with 9320 variants
else {
switch (_lcd_ID) {
#if defined(SUPPORT_9225)
case 0x9225:
case 0x9226:
_SC = 0x37, _EC = 0x36, _SP = 0x39, _EP = 0x38;
_MC = 0x20, _MP = 0x21, _MW = 0x22;
GS = (val & 0x80) ? (1 << 9) : 0;
SS_v = (val & 0x40) ? (1 << 8) : 0;
WriteCmdData(0x01, GS | SS_v | 0x001C); // set Driver Output Control
goto common_ORG;
#endif
#if defined(SUPPORT_0139) || defined(SUPPORT_0154)
#ifdef SUPPORT_0139
case 0x0139:
_SC = 0x46, _EC = 0x46, _SP = 0x48, _EP = 0x47;
goto common_S6D;
#endif
#ifdef SUPPORT_0154
case 0x0154:
_SC = 0x37, _EC = 0x36, _SP = 0x39, _EP = 0x38;
goto common_S6D;
#endif
common_S6D:
_MC = 0x20, _MP = 0x21, _MW = 0x22;
GS = (val & 0x80) ? (1 << 9) : 0;
SS_v = (val & 0x40) ? (1 << 8) : 0;
WriteCmdData(0x01, GS | SS_v | 0x0028); // set Driver Output Control
goto common_ORG;
#endif
case 0x5420:
case 0x7793:
case 0x9326:
case 0xB509:
_MC = 0x200, _MP = 0x201, _MW = 0x202, _SC = 0x210, _EC = 0x211, _SP = 0x212, _EP = 0x213;
GS = (val & 0x80) ? (1 << 15) : 0;
uint16_t NL;
NL = ((432 / 8) - 1) << 9;
if (_lcd_ID == 0x9326 || _lcd_ID == 0x5420) NL >>= 1;
WriteCmdData(0x400, GS | NL);
goto common_SS;
default:
_MC = 0x20, _MP = 0x21, _MW = 0x22, _SC = 0x50, _EC = 0x51, _SP = 0x52, _EP = 0x53;
GS = (val & 0x80) ? (1 << 15) : 0;
WriteCmdData(0x60, GS | 0x2700); // Gate Scan Line (0xA700)
common_SS:
SS_v = (val & 0x40) ? (1 << 8) : 0;
WriteCmdData(0x01, SS_v); // set Driver Output Control
common_ORG:
ORG = (val & 0x20) ? (1 << 3) : 0;
#ifdef SUPPORT_8230
if (_lcd_ID == 0x8230) { // UC8230 has strange BGR and READ_BGR behaviour
if (rotation == 1 || rotation == 2) {
val ^= 0x08; // change BGR bit for LANDSCAPE and PORTRAIT_REV
}
}
#endif
if (val & 0x08)
ORG |= 0x1000; //BGR
_lcd_madctl = ORG | 0x0030;
WriteCmdData(0x03, _lcd_madctl); // set GRAM write direction and BGR=1.
break;
#ifdef SUPPORT_1289
case 0x1289:
_MC = 0x4E, _MP = 0x4F, _MW = 0x22, _SC = 0x44, _EC = 0x44, _SP = 0x45, _EP = 0x46;
if (rotation & 1)
val ^= 0xD0; // exchange Landscape modes
GS = (val & 0x80) ? (1 << 14) | (1 << 12) : 0; //called TB (top-bottom)
SS_v = (val & 0x40) ? (1 << 9) : 0; //called RL (right-left)
ORG = (val & 0x20) ? (1 << 3) : 0; //called AM
_lcd_drivOut = GS | SS_v | (REV << 13) | 0x013F; //REV=0, BGR=0, MUX=319
if (val & 0x08)
_lcd_drivOut |= 0x0800; //BGR
WriteCmdData(0x01, _lcd_drivOut); // set Driver Output Control
WriteCmdData(0x11, ORG | 0x6070); // set GRAM write direction.
break;
#endif
}
}
if ((rotation & 1) && ((_lcd_capable & MV_AXIS) == 0)) {
uint16_t x;
x = _MC, _MC = _MP, _MP = x;
x = _SC, _SC = _SP, _SP = x; //.kbv check 0139
x = _EC, _EC = _EP, _EP = x; //.kbv check 0139
}
setAddrWindow(0, 0, width() - 1, height() - 1);
vertScroll(0, HEIGHT, 0); //reset scrolling after a rotation
}
void MCUFRIEND_kbv::drawPixel(int16_t x, int16_t y, uint16_t color)
{
// MCUFRIEND just plots at edge if you try to write outside of the box:
if (x < 0 || y < 0 || x >= width() || y >= height())
return;
#if defined(SUPPORT_9488_555)
if (is555) color = color565_to_555(color);
#endif
setAddrWindow(x, y, x, y);
// CS_ACTIVE; WriteCmd(_MW); write16(color); CS_IDLE; //-0.01s +98B
WriteCmdData(_MW, color);
}
void MCUFRIEND_kbv::setAddrWindow(int16_t x, int16_t y, int16_t x1, int16_t y1)
{
#if defined(OFFSET_9327)
if (_lcd_ID == 0x9327) {
if (rotation == 2) y += OFFSET_9327, y1 += OFFSET_9327;
if (rotation == 3) x += OFFSET_9327, x1 += OFFSET_9327;
}
#endif
#if 1
if (_lcd_ID == 0x1526 && (rotation & 1)) {
int16_t dx = x1 - x, dy = y1 - y;
if (dy == 0) { y1++; }
else if (dx == 0) { x1 += dy; y1 -= dy; }
}
#endif
if (_lcd_capable & MIPI_DCS_REV1) {
#if 1
CS_ACTIVE; //-0.26s, +272B
WriteCmd(_MC); write8(x>>8); write8(x); write8(x1>>8); write8(x1);
WriteCmd(_MP); write8(y>>8); write8(y); write8(y1>>8); write8(y1);
CS_IDLE;
#else
WriteCmdParam4(_MC, x >> 8, x, x1 >> 8, x1);
WriteCmdParam4(_MP, y >> 8, y, y1 >> 8, y1);
#endif
} else {
WriteCmdData(_MC, x);
WriteCmdData(_MP, y);
if (!(x == x1 && y == y1)) { //only need MC,MP for drawPixel
if (_lcd_capable & XSA_XEA_16BIT) {
if (rotation & 1)
y1 = y = (y1 << 8) | y;
else
x1 = x = (x1 << 8) | x;
}
WriteCmdData(_SC, x);
WriteCmdData(_SP, y);
WriteCmdData(_EC, x1);
WriteCmdData(_EP, y1);
}
}
}
void MCUFRIEND_kbv::fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
{
int16_t end;
#if defined(SUPPORT_9488_555)
if (is555) color = color565_to_555(color);
#endif
if (w < 0) {
w = -w;
x -= w;
} //+ve w
end = x + w;
if (x < 0)
x = 0;
if (end > width())
end = width();
w = end - x;
if (h < 0) {
h = -h;
y -= h;
} //+ve h
end = y + h;
if (y < 0)
y = 0;
if (end > height())
end = height();
h = end - y;
setAddrWindow(x, y, x + w - 1, y + h - 1);
CS_ACTIVE;
WriteCmd(_MW);
if (h > w) {
end = h;
h = w;
w = end;
}
uint8_t hi = color >> 8, lo = color & 0xFF;
while (h-- > 0) {
end = w;
#if USING_16BIT_BUS
#if defined(__SAM3X8E__)
#define STROBE_16BIT {WR_ACTIVE;WR_ACTIVE;WR_ACTIVE;WR_IDLE;WR_IDLE;}
#else
#define STROBE_16BIT {WR_ACTIVE; WR_IDLE;}
#endif
write_16(color); //we could just do the strobe
lo = end & 7;
hi = end >> 3;
if (hi)
do {
STROBE_16BIT;
STROBE_16BIT;
STROBE_16BIT;
STROBE_16BIT;
STROBE_16BIT;
STROBE_16BIT;
STROBE_16BIT;
STROBE_16BIT;
} while (--hi > 0);
while (lo-- > 0) {
STROBE_16BIT;
}
#else
do {
write8(hi);
write8(lo);
} while (--end != 0);
#endif
}
CS_IDLE;
if (!(_lcd_capable & MIPI_DCS_REV1) || ((_lcd_ID == 0x1526) && (rotation & 1)))
setAddrWindow(0, 0, width() - 1, height() - 1);
}
static void pushColors_any(uint16_t cmd, uint8_t * block, int16_t n, bool first, uint8_t flags)
{
uint16_t color;
uint8_t h, l;
bool isconst = flags & 1;
bool isbigend = (flags & 2) != 0;
CS_ACTIVE;
if (first) {
WriteCmd(cmd);
}
while (n-- > 0) {
if (isconst) {
h = pgm_read_byte(block++);
l = pgm_read_byte(block++);
} else {
h = (*block++);
l = (*block++);
}
color = (isbigend) ? (h << 8 | l) : (l << 8 | h);
#if defined(SUPPORT_9488_555)
if (is555) color = color565_to_555(color);
#endif
write16(color);
}
CS_IDLE;
}
void MCUFRIEND_kbv::pushColors(uint16_t * block, int16_t n, bool first)
{
pushColors_any(_MW, (uint8_t *)block, n, first, 0);
}
void MCUFRIEND_kbv::pushColors(uint8_t * block, int16_t n, bool first)
{
pushColors_any(_MW, (uint8_t *)block, n, first, 2); //regular bigend
}
void MCUFRIEND_kbv::pushColors(const uint8_t * block, int16_t n, bool first, bool bigend)
{
pushColors_any(_MW, (uint8_t *)block, n, first, bigend ? 3 : 1);
}
void MCUFRIEND_kbv::vertScroll(int16_t top, int16_t scrollines, int16_t offset)
{
#if defined(OFFSET_9327)
if (_lcd_ID == 0x9327) {
if (rotation == 2 || rotation == 3) top += OFFSET_9327;
}
#endif
int16_t bfa = HEIGHT - top - scrollines; // bottom fixed area
int16_t vsp;
int16_t sea = top;
if (_lcd_ID == 0x9327) bfa += 32;
if (offset <= -scrollines || offset >= scrollines) offset = 0; //valid scroll
vsp = top + offset; // vertical start position
if (offset < 0)
vsp += scrollines; //keep in unsigned range
sea = top + scrollines - 1;
if (_lcd_capable & MIPI_DCS_REV1) {
uint8_t d[6]; // for multi-byte parameters
/*
if (_lcd_ID == 0x9327) { //panel is wired for 240x432
if (rotation == 2 || rotation == 3) { //180 or 270 degrees
if (scrollines == HEIGHT) {
scrollines = 432; // we get a glitch but hey-ho
vsp -= 432 - HEIGHT;
}
if (vsp < 0)
vsp += 432;
}
bfa = 432 - top - scrollines;
}
*/
d[0] = top >> 8; //TFA
d[1] = top;
d[2] = scrollines >> 8; //VSA
d[3] = scrollines;
d[4] = bfa >> 8; //BFA
d[5] = bfa;
WriteCmdParamN(is8347 ? 0x0E : 0x33, 6, d);
// if (offset == 0 && rotation > 1) vsp = top + scrollines; //make non-valid
d[0] = vsp >> 8; //VSP
d[1] = vsp;
WriteCmdParamN(is8347 ? 0x14 : 0x37, 2, d);
if (is8347) {
d[0] = (offset != 0) ? (_lcd_ID == 0x8347 ? 0x02 : 0x08) : 0;
WriteCmdParamN(_lcd_ID == 0x8347 ? 0x18 : 0x01, 1, d); //HX8347-D
} else if (offset == 0 && (_lcd_capable & MIPI_DCS_REV1)) {
WriteCmdParamN(0x13, 0, NULL); //NORMAL i.e. disable scroll
}
return;
}
// cope with 9320 style variants:
switch (_lcd_ID) {
case 0x7783:
WriteCmdData(0x61, _lcd_rev); //!NDL, !VLE, REV
WriteCmdData(0x6A, vsp); //VL#
break;
#ifdef SUPPORT_0139
case 0x0139:
WriteCmdData(0x41, sea); //SEA
WriteCmdData(0x42, top); //SSA
WriteCmdData(0x43, vsp - top); //SST
break;
#endif
#if defined(SUPPORT_0154) || defined(SUPPORT_9225) //thanks tongbajiel
case 0x9225:
case 0x9226:
case 0x0154:
WriteCmdData(0x31, sea); //SEA
WriteCmdData(0x32, top); //SSA
WriteCmdData(0x33, vsp - top); //SST
break;
#endif
#ifdef SUPPORT_1289
case 0x1289:
WriteCmdData(0x41, vsp); //VL#
break;
#endif
case 0x5420:
case 0x7793:
case 0x9326:
case 0xB509:
WriteCmdData(0x401, (1 << 1) | _lcd_rev); //VLE, REV
WriteCmdData(0x404, vsp); //VL#
break;
default:
// 0x6809, 0x9320, 0x9325, 0x9335, 0xB505 can only scroll whole screen
WriteCmdData(0x61, (1 << 1) | _lcd_rev); //!NDL, VLE, REV
WriteCmdData(0x6A, vsp); //VL#
break;
}
}
void MCUFRIEND_kbv::invertDisplay(boolean i)
{
uint8_t val;
_lcd_rev = ((_lcd_capable & REV_SCREEN) != 0) ^ i;
if (_lcd_capable & MIPI_DCS_REV1) {
if (is8347) {
// HX8347D: 0x36 Panel Characteristic. REV_Panel
// HX8347A: 0x36 is Display Control 10
if (_lcd_ID == 0x8347 || _lcd_ID == 0x5252) // HX8347-A, HX5352-A
val = _lcd_rev ? 6 : 2; //INVON id bit#2, NORON=bit#1
else val = _lcd_rev ? 8 : 10; //HX8347-D, G, I: SCROLLON=bit3, INVON=bit1
// HX8347: 0x01 Display Mode has diff bit mapping for A, D
WriteCmdParamN(0x01, 1, &val);
} else
WriteCmdParamN(_lcd_rev ? 0x21 : 0x20, 0, NULL);
return;
}
// cope with 9320 style variants:
switch (_lcd_ID) {
#ifdef SUPPORT_0139
case 0x0139:
#endif
case 0x9225: //REV is in reg(0x07) like Samsung
case 0x9226:
case 0x0154:
WriteCmdData(0x07, 0x13 | (_lcd_rev << 2)); //.kbv kludge
break;
#ifdef SUPPORT_1289
case 0x1289:
_lcd_drivOut &= ~(1 << 13);
if (_lcd_rev)
_lcd_drivOut |= (1 << 13);
WriteCmdData(0x01, _lcd_drivOut);
break;
#endif
case 0x5420:
case 0x7793:
case 0x9326:
case 0xB509:
WriteCmdData(0x401, (1 << 1) | _lcd_rev); //.kbv kludge VLE
break;
default:
WriteCmdData(0x61, _lcd_rev);
break;
}
}
#define TFTLCD_DELAY 0xFFFF
#define TFTLCD_DELAY8 0x7F
static void init_table(const void *table, int16_t size)
{
uint8_t *p = (uint8_t *) table, dat[24]; //R61526 has GAMMA[22]
while (size > 0) {
uint8_t cmd = pgm_read_byte(p++);
uint8_t len = pgm_read_byte(p++);
if (cmd == TFTLCD_DELAY8) {
delay(len);
len = 0;
} else {
for (uint8_t i = 0; i < len; i++)
dat[i] = pgm_read_byte(p++);
WriteCmdParamN(cmd, len, dat);
}
size -= len + 2;
}
}
static void init_table16(const void *table, int16_t size)
{
uint16_t *p = (uint16_t *) table;
while (size > 0) {
uint16_t cmd = pgm_read_word(p++);
uint16_t d = pgm_read_word(p++);
if (cmd == TFTLCD_DELAY)
delay(d);
else {
writecmddata(cmd, d); //static function
}
size -= 2 * sizeof(int16_t);
}
}
void MCUFRIEND_kbv::begin(uint16_t ID)
{
int16_t *p16; //so we can "write" to a const protected variable.
const uint8_t *table8_ads = NULL;
int16_t table_size;
reset();
_lcd_xor = 0;
switch (_lcd_ID = ID) {
/*
static const uint16_t _regValues[] PROGMEM = {
0x0000, 0x0001, // start oscillation
0x0007, 0x0000, // source output control 0 D0
0x0013, 0x0000, // power control 3 off
0x0011, 0x2604, //
0x0014, 0x0015, //
0x0010, 0x3C00, //
// 0x0013, 0x0040, //
// 0x0013, 0x0060, //
// 0x0013, 0x0070, //
0x0013, 0x0070, // power control 3 PON PON1 AON
0x0001, 0x0127, // driver output control
// 0x0002, 0x0700, // field 0 b/c waveform xor waveform
0x0003, 0x1030, //
0x0007, 0x0000, //
0x0008, 0x0404, //
0x000B, 0x0200, //
0x000C, 0x0000, //
0x00015,0x0000, //
//gamma setting
0x0030, 0x0000,
0x0031, 0x0606,
0x0032, 0x0006,
0x0033, 0x0403,
0x0034, 0x0107,
0x0035, 0x0101,
0x0036, 0x0707,
0x0037, 0x0304,
0x0038, 0x0A00,
0x0039, 0x0706,
0x0040, 0x0000,
0x0041, 0x0000,
0x0042, 0x013F,
0x0043, 0x0000,
0x0044, 0x0000,
0x0045, 0x0000,
0x0046, 0xEF00,
0x0047, 0x013F,
0x0048, 0x0000,
0x0007, 0x0011,
0x0007, 0x0017,
};
*/
#ifdef SUPPORT_0139
case 0x0139:
_lcd_capable = AUTO_READINC | REV_SCREEN | XSA_XEA_16BIT;
static const uint16_t S6D0139_regValues[] PROGMEM = {
0x0000, 0x0001, //Start oscillator
0x0011, 0x1a00, //Power Control 2
0x0014, 0x2020, //Power Control 4
0x0010, 0x0900, //Power Control 1
0x0013, 0x0040, //Power Control 3
0x0013, 0x0060, //Power Control 3
0x0013, 0x0070, //Power Control 3
0x0011, 0x1a04, //Power Control 2
0x0010, 0x2f00, //Power Control 1
0x0001, 0x0127, //Driver Control: SM=0, GS=0, SS=1, 240x320
0x0002, 0x0100, //LCD Control: (.kbv was 0700) FLD=0, BC= 0, EOR=1
0x0003, 0x1030, //Entry Mode: TR1=0, DFM=0, BGR=1, I_D=3
0x0007, 0x0000, //Display Control: everything off
0x0008, 0x0808, //Blank Period: FP=98, BP=8
0x0009, 0x0000, //f.k.
0x000b, 0x0000, //Frame Control:
0x000c, 0x0000, //Interface Control: system i/f
0x0040, 0x0000, //Scan Line
0x0041, 0x0000, //Vertical Scroll Control
0x0007, 0x0014, //Display Control: SPT=1, REV=1
0x0007, 0x0016, //Display Control: SPT=1, REV=1, display on
0x0007, 0x0017, //Display Control: SPT=1, REV=1, display on, GON
};
init_table16(S6D0139_regValues, sizeof(S6D0139_regValues));
break;
#endif
#ifdef SUPPORT_0154
case 0x0154:
_lcd_capable = AUTO_READINC | REV_SCREEN;
static const uint16_t S6D0154_regValues[] PROGMEM = {
0x0011, 0x001A,
0x0012, 0x3121, //BT=3, DC1=1, DC2=2, DC3=1
0x0013, 0x006C, //GVD=108
0x0014, 0x4249, //VCM=66, VML=73
0x0010, 0x0800, //SAP=8
TFTLCD_DELAY, 10,
0x0011, 0x011A, //APON=0, PON=1, AON=0, VCI1_EN=1, VC=10
TFTLCD_DELAY, 10,
0x0011, 0x031A, //APON=0, PON=3, AON=0, VCI1_EN=1, VC=10
TFTLCD_DELAY, 10,
0x0011, 0x071A, //APON=0, PON=7, AON=0, VCI1_EN=1, VC=10
TFTLCD_DELAY, 10,
0x0011, 0x0F1A, //APON=0, PON=15, AON=0, VCI1_EN=1, VC=10
TFTLCD_DELAY, 10,
0x0011, 0x0F3A, //APON=0, PON=15, AON=1, VCI1_EN=1, VC=10
TFTLCD_DELAY, 30,
0x0001, 0x0128,
0x0002, 0x0100,
0x0003, 0x1030,
0x0007, 0x1012,
0x0008, 0x0303,
0x000B, 0x1100,
0x000C, 0x0000,
0x000F, 0x1801,
0x0015, 0x0020,
0x0050,0x0101,
0x0051,0x0603,
0x0052,0x0408,
0x0053,0x0000,
0x0054,0x0605,
0x0055,0x0406,
0x0056,0x0303,
0x0057,0x0303,
0x0058,0x0010,
0x0059,0x1000,
0x0007, 0x0012, //GON=1, REV=0, D=2
TFTLCD_DELAY, 40,
0x0007, 0x0013, //GON=1, REV=0, D=3
0x0007, 0x0017, //GON=1, REV=1, D=3 DISPLAY ON
};
init_table16(S6D0154_regValues, sizeof(S6D0154_regValues));
break;
#endif
#ifdef SUPPORT_1289
case 0x1289:
_lcd_capable = 0 | XSA_XEA_16BIT | REV_SCREEN;
// came from MikroElektronika library http://www.hmsprojects.com/tft_lcd.html
static const uint16_t SSD1289_regValues[] PROGMEM = {
0x0000, 0x0001,
0x0003, 0xA8A4,
0x000C, 0x0000,
0x000D, 0x080C, // was 0x800C
0x000E, 0x2B00,
0x001E, 0x00B7,
0x0001, 0x2B3F, // was 0x2B3F,
0x0002, 0x0400, // was 0x0600
0x0010, 0x0000,
0x0011, 0x6070, // was 0x6070
0x0005, 0x0000,
0x0006, 0x0000,
0x0016, 0xEF1C,
0x0017, 0x0003,
0x0007, 0x0233,
0x000B, 0x0000,
0x000F, 0x0000,
0x0041, 0x0000,
0x0042, 0x0000,
0x0048, 0x0000,
0x0049, 0x013F,
0x004A, 0x0000,
0x004B, 0x0000,
0x0044, 0xEF95,
0x0045, 0x0000,
0x0046, 0x013F,
0x0030, 0x0707,
0x0031, 0x0204,
0x0032, 0x0204,
0x0033, 0x0502,