-
Notifications
You must be signed in to change notification settings - Fork 18
/
ST7735_t3.cpp
1706 lines (1520 loc) · 57.2 KB
/
ST7735_t3.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
/***************************************************
This is a library for the Adafruit 1.8" SPI display.
This library works with the Adafruit 1.8" TFT Breakout w/SD card
----> http://www.adafruit.com/products/358
as well as Adafruit raw 1.8" TFT display
----> http://www.adafruit.com/products/618
Check out the links above for our tutorials and wiring diagrams
These displays use SPI to communicate, 4 or 5 pins are required to
interface (RST is optional)
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
MIT license, all text above must be included in any redistribution
****************************************************/
#include "ST7735_t3.h"
#include <limits.h>
#include "pins_arduino.h"
#include "wiring_private.h"
#include <SPI.h>
#ifdef ENABLE_ST77XX_FRAMEBUFFER
//#define DEBUG_ASYNC_UPDATE
//#define DEBUG_ASYNC_LEDS
#ifdef DEBUG_ASYNC_LEDS
#define DEBUG_PIN_1 0
#define DEBUG_PIN_2 1
#define DEBUG_PIN_3 2
#endif
volatile short _dma_dummy_rx;
ST7735_t3 *ST7735_t3::_dmaActiveDisplay[3] = {0, 0, 0};
#if defined(__MK66FX1M0__)
DMASetting ST7735_t3::_dmasettings[3][4];
#endif
#if defined(__IMXRT1062__) // Teensy 4.x
// On T4 Setup the buffers to be used one per SPI buss...
// This way we make sure it is hopefully in uncached memory
ST7735DMA_Data ST7735_t3::_dma_data[3]; // one structure for each SPI buss...
#endif
#endif
// Constructor when using software SPI. All output pins are configurable.
ST7735_t3::ST7735_t3(uint8_t cs, uint8_t rs, uint8_t sid, uint8_t sclk, uint8_t rst) :
Adafruit_GFX(ST7735_TFTWIDTH, ST7735_TFTHEIGHT_160), hwSPI(false),
_cs(cs), _rs(rs), _rst(rst), _sid(sid), _sclk(sclk)
{
#ifdef ENABLE_ST77XX_FRAMEBUFFER
_pfbtft = NULL;
_use_fbtft = 0; // Are we in frame buffer mode?
_we_allocated_buffer = NULL;
_dma_state = 0;
#endif
_screenHeight = ST7735_TFTHEIGHT_160;
_screenWidth = ST7735_TFTWIDTH;
}
// Constructor when using hardware SPI. Faster, but must use SPI pins
// specific to each board type (e.g. 11,13 for Uno, 51,52 for Mega, etc.)
ST7735_t3::ST7735_t3(uint8_t cs, uint8_t rs, uint8_t rst) :
Adafruit_GFX(ST7735_TFTWIDTH, ST7735_TFTHEIGHT_160), hwSPI(true),
_cs(cs), _rs(rs), _rst(rst), _sid((uint8_t)-1), _sclk((uint8_t)-1)
{
#ifdef ENABLE_ST77XX_FRAMEBUFFER
_pfbtft = NULL;
_use_fbtft = 0; // Are we in frame buffer mode?
_we_allocated_buffer = NULL;
_dma_state = 0;
#endif
_screenHeight = ST7735_TFTHEIGHT_160;
_screenWidth = ST7735_TFTWIDTH;
}
/***************************************************************/
/* Teensy 3.6 */
/***************************************************************/
#if defined(__MK66FX1M0__)
inline void ST7735_t3::waitTransmitComplete(void) {
uint32_t tmp __attribute__((unused));
while (!(_pkinetisk_spi->SR & SPI_SR_TCF)) ; // wait until final output done
tmp = _pkinetisk_spi->POPR; // drain the final RX FIFO word
}
inline void ST7735_t3::waitTransmitComplete(uint32_t mcr) {
uint32_t tmp __attribute__((unused));
while (1) {
uint32_t sr = _pkinetisk_spi->SR;
if (sr & SPI_SR_EOQF) break; // wait for last transmit
if (sr & 0xF0) tmp = _pkinetisk_spi->POPR;
}
_pkinetisk_spi->SR = SPI_SR_EOQF;
_pkinetisk_spi->MCR = mcr;
while (_pkinetisk_spi->SR & 0xF0) {
tmp = _pkinetisk_spi->POPR;
}
}
inline void ST7735_t3::spiwrite(uint8_t c)
{
// pass 1 if we actually are setup to with MOSI and SCLK on hardware SPI use it...
if (_pspi) {
_pspi->transfer(c);
return;
}
for (uint8_t bit = 0x80; bit; bit >>= 1) {
*datapin = ((c & bit) ? 1 : 0);
*clkpin = 1;
*clkpin = 0;
}
}
inline void ST7735_t3::spiwrite16(uint16_t d)
{
// pass 1 if we actually are setup to with MOSI and SCLK on hardware SPI use it...
if (_pspi) {
_pspi->transfer16(d);
return;
}
spiwrite(d >> 8);
spiwrite(d);
}
void ST7735_t3::writecommand(uint8_t c)
{
if (hwSPI) {
_pkinetisk_spi->PUSHR = c | (pcs_command << 16) | SPI_PUSHR_CTAS(0);
while (((_pkinetisk_spi->SR) & (15 << 12)) > _fifo_full_test) ; // wait if FIFO full
} else {
*rspin = 0;
spiwrite(c);
}
}
void ST7735_t3::writecommand_last(uint8_t c) {
if (hwSPI) {
uint32_t mcr = _pkinetisk_spi->MCR;
_pkinetisk_spi->PUSHR = c | (pcs_command << 16) | SPI_PUSHR_CTAS(0) | SPI_PUSHR_EOQ;
waitTransmitComplete(mcr);
} else {
*rspin = 0;
spiwrite(c);
}
}
void ST7735_t3::writedata(uint8_t c)
{
if (hwSPI) {
_pkinetisk_spi->PUSHR = c | (pcs_data << 16) | SPI_PUSHR_CTAS(0);
while (((_pkinetisk_spi->SR) & (15 << 12)) > _fifo_full_test) ; // wait if FIFO full
} else {
*rspin = 1;
spiwrite(c);
}
}
void ST7735_t3::writedata_last(uint8_t c)
{
if (hwSPI) {
uint32_t mcr = _pkinetisk_spi->MCR;
_pkinetisk_spi->PUSHR = c | (pcs_data << 16) | SPI_PUSHR_CTAS(0) | SPI_PUSHR_EOQ;
waitTransmitComplete(mcr);
} else {
*rspin = 1;
spiwrite(c);
}
}
void ST7735_t3::writedata16(uint16_t d)
{
if (hwSPI) {
_pkinetisk_spi->PUSHR = d | (pcs_data << 16) | SPI_PUSHR_CTAS(1);
while (((_pkinetisk_spi->SR) & (15 << 12)) > _fifo_full_test) ; // wait if FIFO full
} else {
*rspin = 1;
spiwrite16(d);
}
}
void ST7735_t3::writedata16_last(uint16_t d)
{
if (hwSPI) {
uint32_t mcr = _pkinetisk_spi->MCR;
_pkinetisk_spi->PUSHR = d | (pcs_data << 16) | SPI_PUSHR_CTAS(1) | SPI_PUSHR_EOQ;
waitTransmitComplete(mcr);
} else {
*rspin = 1;
spiwrite16(d);
}
}
#define CTAR_24MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | SPI_CTAR_CSSCK(0) | SPI_CTAR_DBR)
#define CTAR_16MHz (SPI_CTAR_PBR(1) | SPI_CTAR_BR(0) | SPI_CTAR_CSSCK(0) | SPI_CTAR_DBR)
#define CTAR_12MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | SPI_CTAR_CSSCK(0))
#define CTAR_8MHz (SPI_CTAR_PBR(1) | SPI_CTAR_BR(0) | SPI_CTAR_CSSCK(0))
#define CTAR_6MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1))
#define CTAR_4MHz (SPI_CTAR_PBR(1) | SPI_CTAR_BR(1) | SPI_CTAR_CSSCK(1))
void ST7735_t3::setBitrate(uint32_t n)
{
if (n >= 24000000) {
ctar = CTAR_24MHz;
} else if (n >= 16000000) {
ctar = CTAR_16MHz;
} else if (n >= 12000000) {
ctar = CTAR_12MHz;
} else if (n >= 8000000) {
ctar = CTAR_8MHz;
} else if (n >= 6000000) {
ctar = CTAR_6MHz;
} else {
ctar = CTAR_4MHz;
}
SIM_SCGC6 |= SIM_SCGC6_SPI0;
_pkinetisk_spi->MCR = SPI_MCR_MDIS | SPI_MCR_HALT;
_pkinetisk_spi->CTAR0 = ctar | SPI_CTAR_FMSZ(7);
_pkinetisk_spi->CTAR1 = ctar | SPI_CTAR_FMSZ(15);
_pkinetisk_spi->MCR = SPI_MCR_MSTR | SPI_MCR_PCSIS(0x1F) | SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF;
}
/***************************************************************/
/* Teensy 4. */
/***************************************************************/
#elif defined(__IMXRT1062__) // Teensy 4.x
inline void ST7735_t3::spiwrite(uint8_t c)
{
//Serial.println(c, HEX);
if (_pspi) {
_pspi->transfer(c);
} else {
// Fast SPI bitbang swiped from LPD8806 library
for(uint8_t bit = 0x80; bit; bit >>= 1) {
if(c & bit) DIRECT_WRITE_HIGH(_mosiport, _mosipinmask);
else DIRECT_WRITE_LOW(_mosiport, _mosipinmask);
DIRECT_WRITE_HIGH(_sckport, _sckpinmask);
asm("nop; nop; nop; nop; nop; nop; nop; nop; nop; nop; nop;");
DIRECT_WRITE_LOW(_sckport, _sckpinmask);
}
}
}
void ST7735_t3::writecommand(uint8_t c)
{
if (hwSPI) {
maybeUpdateTCR(LPSPI_TCR_PCS(0) | LPSPI_TCR_FRAMESZ(7) /*| LPSPI_TCR_CONT*/);
_pimxrt_spi->TDR = c;
_pending_rx_count++; //
waitFifoNotFull();
} else {
DIRECT_WRITE_LOW(_dcport, _dcpinmask);
spiwrite(c);
}
}
void ST7735_t3::writecommand_last(uint8_t c)
{
if (hwSPI) {
maybeUpdateTCR(LPSPI_TCR_PCS(0) | LPSPI_TCR_FRAMESZ(7));
_pimxrt_spi->TDR = c;
_pending_rx_count++; //
waitTransmitComplete();
} else {
DIRECT_WRITE_LOW(_dcport, _dcpinmask);
spiwrite(c);
}
}
void ST7735_t3::writedata(uint8_t c)
{
if (hwSPI) {
maybeUpdateTCR(LPSPI_TCR_PCS(1) | LPSPI_TCR_FRAMESZ(7));
_pimxrt_spi->TDR = c;
_pending_rx_count++; //
waitTransmitComplete();
} else {
DIRECT_WRITE_HIGH(_dcport, _dcpinmask);
spiwrite(c);
}
}
void ST7735_t3::writedata_last(uint8_t c)
{
if (hwSPI) {
maybeUpdateTCR(LPSPI_TCR_PCS(1) | LPSPI_TCR_FRAMESZ(7));
_pimxrt_spi->TDR = c;
_pending_rx_count++; //
waitTransmitComplete();
} else {
DIRECT_WRITE_HIGH(_dcport, _dcpinmask);
spiwrite(c);
}
}
void ST7735_t3::writedata16(uint16_t d)
{
if (hwSPI) {
maybeUpdateTCR(LPSPI_TCR_PCS(1) | LPSPI_TCR_FRAMESZ(15) | LPSPI_TCR_CONT);
_pimxrt_spi->TDR = d;
_pending_rx_count++; //
waitFifoNotFull();
} else {
DIRECT_WRITE_HIGH(_dcport, _dcpinmask);
spiwrite(d >> 8);
spiwrite(d);
}
}
void ST7735_t3::writedata16_last(uint16_t d)
{
if (hwSPI) {
maybeUpdateTCR(LPSPI_TCR_PCS(1) | LPSPI_TCR_FRAMESZ(15));
_pimxrt_spi->TDR = d;
// _pimxrt_spi->SR = LPSPI_SR_WCF | LPSPI_SR_FCF | LPSPI_SR_TCF;
_pending_rx_count++; //
waitTransmitComplete();
} else {
DIRECT_WRITE_HIGH(_dcport, _dcpinmask);
spiwrite(d >> 8);
spiwrite(d);
}
}
void ST7735_t3::setBitrate(uint32_t n)
{
if (n >= 8000000) {
SPI.setClockDivider(SPI_CLOCK_DIV2);
} else if (n >= 4000000) {
SPI.setClockDivider(SPI_CLOCK_DIV4);
} else if (n >= 2000000) {
SPI.setClockDivider(SPI_CLOCK_DIV8);
} else {
SPI.setClockDivider(SPI_CLOCK_DIV16);
}
}
#endif
// Rather than a bazillion writecommand() and writedata() calls, screen
// initialization commands and arguments are organized in these tables
// stored in PROGMEM. The table may look bulky, but that's mostly the
// formatting -- storage-wise this is hundreds of bytes more compact
// than the equivalent code. Companion function follows.
#define DELAY 0x80
static const uint8_t PROGMEM
Bcmd[] = { // Initialization commands for 7735B screens
18, // 18 commands in list:
ST7735_SWRESET, DELAY, // 1: Software reset, no args, w/delay
50, // 50 ms delay
ST7735_SLPOUT , DELAY, // 2: Out of sleep mode, no args, w/delay
255, // 255 = 500 ms delay
ST7735_COLMOD , 1+DELAY, // 3: Set color mode, 1 arg + delay:
0x05, // 16-bit color
10, // 10 ms delay
ST7735_FRMCTR1, 3+DELAY, // 4: Frame rate control, 3 args + delay:
0x00, // fastest refresh
0x06, // 6 lines front porch
0x03, // 3 lines back porch
10, // 10 ms delay
ST7735_MADCTL , 1 , // 5: Memory access ctrl (directions), 1 arg:
0x08, // Row addr/col addr, bottom to top refresh
ST7735_DISSET5, 2 , // 6: Display settings #5, 2 args, no delay:
0x15, // 1 clk cycle nonoverlap, 2 cycle gate
// rise, 3 cycle osc equalize
0x02, // Fix on VTL
ST7735_INVCTR , 1 , // 7: Display inversion control, 1 arg:
0x0, // Line inversion
ST7735_PWCTR1 , 2+DELAY, // 8: Power control, 2 args + delay:
0x02, // GVDD = 4.7V
0x70, // 1.0uA
10, // 10 ms delay
ST7735_PWCTR2 , 1 , // 9: Power control, 1 arg, no delay:
0x05, // VGH = 14.7V, VGL = -7.35V
ST7735_PWCTR3 , 2 , // 10: Power control, 2 args, no delay:
0x01, // Opamp current small
0x02, // Boost frequency
ST7735_VMCTR1 , 2+DELAY, // 11: Power control, 2 args + delay:
0x3C, // VCOMH = 4V
0x38, // VCOML = -1.1V
10, // 10 ms delay
ST7735_PWCTR6 , 2 , // 12: Power control, 2 args, no delay:
0x11, 0x15,
ST7735_GMCTRP1,16 , // 13: Magical unicorn dust, 16 args, no delay:
0x09, 0x16, 0x09, 0x20, // (seriously though, not sure what
0x21, 0x1B, 0x13, 0x19, // these config values represent)
0x17, 0x15, 0x1E, 0x2B,
0x04, 0x05, 0x02, 0x0E,
ST7735_GMCTRN1,16+DELAY, // 14: Sparkles and rainbows, 16 args + delay:
0x0B, 0x14, 0x08, 0x1E, // (ditto)
0x22, 0x1D, 0x18, 0x1E,
0x1B, 0x1A, 0x24, 0x2B,
0x06, 0x06, 0x02, 0x0F,
10, // 10 ms delay
ST7735_CASET , 4 , // 15: Column addr set, 4 args, no delay:
0x00, 0x02, // XSTART = 2
0x00, 0x81, // XEND = 129
ST7735_RASET , 4 , // 16: Row addr set, 4 args, no delay:
0x00, 0x02, // XSTART = 1
0x00, 0x81, // XEND = 160
ST7735_NORON , DELAY, // 17: Normal display on, no args, w/delay
10, // 10 ms delay
ST7735_DISPON , DELAY, // 18: Main screen turn on, no args, w/delay
255 }, // 255 = 500 ms delay
Rcmd1[] = { // Init for 7735R, part 1 (red or green tab)
15, // 15 commands in list:
ST7735_SWRESET, DELAY, // 1: Software reset, 0 args, w/delay
150, // 150 ms delay
ST7735_SLPOUT , DELAY, // 2: Out of sleep mode, 0 args, w/delay
255, // 500 ms delay
ST7735_FRMCTR1, 3 , // 3: Frame rate ctrl - normal mode, 3 args:
0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
ST7735_FRMCTR2, 3 , // 4: Frame rate control - idle mode, 3 args:
0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
ST7735_FRMCTR3, 6 , // 5: Frame rate ctrl - partial mode, 6 args:
0x01, 0x2C, 0x2D, // Dot inversion mode
0x01, 0x2C, 0x2D, // Line inversion mode
ST7735_INVCTR , 1 , // 6: Display inversion ctrl, 1 arg, no delay:
0x07, // No inversion
ST7735_PWCTR1 , 3 , // 7: Power control, 3 args, no delay:
0xA2,
0x02, // -4.6V
0x84, // AUTO mode
ST7735_PWCTR2 , 1 , // 8: Power control, 1 arg, no delay:
0xC5, // VGH25 = 2.4C VGSEL = -10 VGH = 3 * AVDD
ST7735_PWCTR3 , 2 , // 9: Power control, 2 args, no delay:
0x0A, // Opamp current small
0x00, // Boost frequency
ST7735_PWCTR4 , 2 , // 10: Power control, 2 args, no delay:
0x8A, // BCLK/2, Opamp current small & Medium low
0x2A,
ST7735_PWCTR5 , 2 , // 11: Power control, 2 args, no delay:
0x8A, 0xEE,
ST7735_VMCTR1 , 1 , // 12: Power control, 1 arg, no delay:
0x0E,
ST7735_INVOFF , 0 , // 13: Don't invert display, no args, no delay
ST7735_MADCTL , 1 , // 14: Memory access control (directions), 1 arg:
0xC8, // row addr/col addr, bottom to top refresh
ST7735_COLMOD , 1 , // 15: set color mode, 1 arg, no delay:
0x05 }, // 16-bit color
Rcmd2green[] = { // Init for 7735R, part 2 (green tab only)
2, // 2 commands in list:
ST7735_CASET , 4 , // 1: Column addr set, 4 args, no delay:
0x00, 0x02, // XSTART = 0
0x00, 0x7F+0x02, // XEND = 127
ST7735_RASET , 4 , // 2: Row addr set, 4 args, no delay:
0x00, 0x01, // XSTART = 0
0x00, 0x9F+0x01 }, // XEND = 159
Rcmd2red[] = { // Init for 7735R, part 2 (red tab only)
2, // 2 commands in list:
ST7735_CASET , 4 , // 1: Column addr set, 4 args, no delay:
0x00, 0x00, // XSTART = 0
0x00, 0x7F, // XEND = 127
ST7735_RASET , 4 , // 2: Row addr set, 4 args, no delay:
0x00, 0x00, // XSTART = 0
0x00, 0x9F }, // XEND = 159
Rcmd2green144[] = { // Init for 7735R, part 2 (green 1.44 tab)
2, // 2 commands in list:
ST7735_CASET , 4 , // 1: Column addr set, 4 args, no delay:
0x00, 0x00, // XSTART = 0
0x00, 0x7F, // XEND = 127
ST7735_RASET , 4 , // 2: Row addr set, 4 args, no delay:
0x00, 0x00, // XSTART = 0
0x00, 0x7F }, // XEND = 127
Rcmd2green160x80[] = { // 7735R init, part 2 (mini 160x80)
2, // 2 commands in list:
ST7735_CASET, 4, // 1: Column addr set, 4 args, no delay:
0x00, 0x00, // XSTART = 0
0x00, 0x4F, // XEND = 79
ST7735_RASET, 4, // 2: Row addr set, 4 args, no delay:
0x00, 0x00, // XSTART = 0
0x00, 0x9F }, // XEND = 159
Rcmd3[] = { // Init for 7735R, part 3 (red or green tab)
4, // 4 commands in list:
ST7735_GMCTRP1, 16 , // 1: Magical unicorn dust, 16 args, no delay:
0x02, 0x1c, 0x07, 0x12,
0x37, 0x32, 0x29, 0x2d,
0x29, 0x25, 0x2B, 0x39,
0x00, 0x01, 0x03, 0x10,
ST7735_GMCTRN1, 16 , // 2: Sparkles and rainbows, 16 args, no delay:
0x03, 0x1d, 0x07, 0x06,
0x2E, 0x2C, 0x29, 0x2D,
0x2E, 0x2E, 0x37, 0x3F,
0x00, 0x00, 0x02, 0x10,
ST7735_NORON , DELAY, // 3: Normal display on, no args, w/delay
10, // 10 ms delay
ST7735_DISPON , DELAY, // 4: Main screen turn on, no args w/delay
100 }; // 100 ms delay
// Companion code to the above tables. Reads and issues
// a series of LCD commands stored in PROGMEM byte array.
void ST7735_t3::commandList(const uint8_t *addr)
{
uint8_t numCommands, numArgs;
uint16_t ms;
beginSPITransaction();
numCommands = pgm_read_byte(addr++); // Number of commands to follow
//Serial.printf("CommandList: numCmds:%d\n", numCommands); Serial.flush();
while(numCommands--) { // For each command...
writecommand_last(pgm_read_byte(addr++)); // Read, issue command
numArgs = pgm_read_byte(addr++); // Number of args to follow
ms = numArgs & DELAY; // If hibit set, delay follows args
numArgs &= ~DELAY; // Mask out delay bit
while(numArgs > 1) { // For each argument...
writedata(pgm_read_byte(addr++)); // Read, issue argument
numArgs--;
}
if (numArgs) writedata_last(pgm_read_byte(addr++)); // Read, issue argument - wait until this one completes
if(ms) {
ms = pgm_read_byte(addr++); // Read post-command delay time (ms)
if(ms == 255) ms = 500; // If 255, delay for 500 ms
//Serial.printf("delay %d\n", ms); Serial.flush();
endSPITransaction();
delay(ms);
beginSPITransaction();
}
}
endSPITransaction();
}
// Initialization code common to both 'B' and 'R' type displays
void ST7735_t3::commonInit(const uint8_t *cmdList, uint8_t mode)
{
_colstart = _rowstart = 0; // May be overridden in init func
_ystart = _xstart = 0;
#if defined(__MK66FX1M0__)
if (_sid == (uint8_t)-1) _sid = 11;
if (_sclk == (uint8_t)-1) _sclk = 13;
// Lets try to handle cases where DC is not hardware, without going all the way down to bit bang!
//if (SPI.pinIsMOSI(_sid) && SPI.pinIsSCK(_sclk) && SPI.pinIsChipSelect(_rs)) {
if (SPI.pinIsMOSI(_sid) && SPI.pinIsSCK(_sclk)) {
_pspi = &SPI;
_spi_num = 0; // Which buss is this spi on?
_pkinetisk_spi = &KINETISK_SPI0; // Could hack our way to grab this from SPI object, but...
_fifo_full_test = (3 << 12);
//Serial.println("ST7735_t3::commonInit SPI");
#if defined(__MK64FX512__) || defined(__MK66FX1M0__)
} else if (SPI1.pinIsMOSI(_sid) && SPI1.pinIsSCK(_sclk)) {
_pspi = &SPI1;
_spi_num = 1; // Which buss is this spi on?
_pkinetisk_spi = &KINETISK_SPI1;
_fifo_full_test = (0 << 12);
//Serial.println("ST7735_t3::commonInit SPI1");
} else if (SPI2.pinIsMOSI(_sid) && SPI2.pinIsSCK(_sclk)) {
_pspi = &SPI2;
_spi_num = 2; // Which buss is this spi on?
_pkinetisk_spi = &KINETISK_SPI2;
_fifo_full_test = (0 << 12);
//Serial.println("ST7735_t3::commonInit SPI2");
#endif
} else _pspi = nullptr;
if (_pspi) {
hwSPI = true;
_pspi->setMOSI(_sid);
_pspi->setSCK(_sclk);
_pspi->begin();
//Serial.println("After SPI begin");
_spiSettings = SPISettings(ST7735_SPICLOCK, MSBFIRST, mode);
// See if both CS and DC are valid CS pins.
if (_pspi->pinIsChipSelect(_rs, _cs)) {
pcs_data = _pspi->setCS(_cs);
pcs_command = pcs_data | _pspi->setCS(_rs);
cspin = 0; // Let know that we are not setting manual
//Serial.println("Both CS and DC are SPI pins");
// See if at least DC is hardware CS...
} else if (_pspi->pinIsChipSelect(_rs)) {
// We already verified that _rs was valid CS pin above.
pcs_data = 0;
pcs_command = pcs_data | _pspi->setCS(_rs);
if (_cs != 0xff) {
pinMode(_cs, OUTPUT);
cspin = portOutputRegister(digitalPinToPort(_cs));
*cspin = 1;
}
} else {
// DC is not, and won't bother to check CS...
pcs_data = 0;
pcs_command = 0;
if (_cs != 0xff) {
pinMode(_cs, OUTPUT);
cspin = portOutputRegister(digitalPinToPort(_cs));
*cspin = 1;
}
pinMode(_rs, OUTPUT);
rspin = portOutputRegister(digitalPinToPort(_rs));
*rspin = 0;
// Pass 1, now lets set hwSPI false
hwSPI = false;
}
// Hack to get hold of the SPI Hardware information...
uint32_t *pa = (uint32_t*)((void*)_pspi);
_spi_hardware = (SPIClass::SPI_Hardware_t*)(void*)pa[1];
} else {
//Serial.println("ST7735_t3::commonInit Software SPI :(");
hwSPI = false;
_pspi = nullptr;
cspin = (_cs != 0xff)? portOutputRegister(digitalPinToPort(_cs)) : 0;
rspin = portOutputRegister(digitalPinToPort(_rs));
clkpin = portOutputRegister(digitalPinToPort(_sclk));
datapin = portOutputRegister(digitalPinToPort(_sid));
*cspin = 1;
*rspin = 0;
*clkpin = 0;
*datapin = 0;
if (_cs != 0xff) pinMode(_cs, OUTPUT);
pinMode(_rs, OUTPUT);
pinMode(_sclk, OUTPUT);
pinMode(_sid, OUTPUT);
}
// Teensy 4
#elif defined(__IMXRT1062__) // Teensy 4.x
if (_sid == (uint8_t)-1) _sid = 11;
if (_sclk == (uint8_t)-1) _sclk = 13;
if (SPI.pinIsMOSI(_sid) && SPI.pinIsSCK(_sclk)) {
_pspi = &SPI;
_spi_num = 0; // Which buss is this spi on?
_pimxrt_spi = &IMXRT_LPSPI4_S; // Could hack our way to grab this from SPI object, but...
} else if (SPI1.pinIsMOSI(_sid) && SPI1.pinIsSCK(_sclk)) {
_pspi = &SPI1;
_spi_num = 1; // Which buss is this spi on?
_pimxrt_spi = &IMXRT_LPSPI3_S;
} else if (SPI2.pinIsMOSI(_sid) && SPI2.pinIsSCK(_sclk)) {
_pspi = &SPI2;
_spi_num = 2; // Which buss is this spi on?
_pimxrt_spi = &IMXRT_LPSPI1_S;
} else _pspi = nullptr;
if (_pspi) {
hwSPI = true;
_pspi->begin();
_pending_rx_count = 0;
_spiSettings = SPISettings(ST7735_SPICLOCK, MSBFIRST, mode);
_pspi->beginTransaction(_spiSettings); // Should have our settings.
_pspi->transfer(0); // hack to see if it will actually change then...
_pspi->endTransaction();
_spi_tcr_current = _pimxrt_spi->TCR; // get the current TCR value
// uint32_t *phack = (uint32_t* )&_spiSettings;
// Serial.printf("SPI Settings: TCR: %x %x (%x %x)\n", _spi_tcr_current, _pimxrt_spi->TCR, phack[0], phack[1]);
// Hack to get hold of the SPI Hardware information...
uint32_t *pa = (uint32_t*)((void*)_pspi);
_spi_hardware = (SPIClass::SPI_Hardware_t*)(void*)pa[1];
} else {
hwSPI = false;
_sckport = portOutputRegister(_sclk);
_sckpinmask = digitalPinToBitMask(_sclk);
pinMode(_sclk, OUTPUT);
DIRECT_WRITE_LOW(_sckport, _sckpinmask);
_mosiport = portOutputRegister(_sid);
_mosipinmask = digitalPinToBitMask(_sid);
pinMode(_sid, OUTPUT);
DIRECT_WRITE_LOW(_mosiport, _mosipinmask);
}
if (_cs != 0xff) {
_csport = portOutputRegister(_cs);
_cspinmask = digitalPinToBitMask(_cs);
pinMode(_cs, OUTPUT);
DIRECT_WRITE_HIGH(_csport, _cspinmask);
} else _csport = 0;
if (_pspi && _pspi->pinIsChipSelect(_rs)) {
_pspi->setCS(_rs);
_dcport = 0;
_dcpinmask = 0;
} else {
//Serial.println("ST7735_t3: Error not DC is not valid hardware CS pin");
_dcport = portOutputRegister(_rs);
_dcpinmask = digitalPinToBitMask(_rs);
pinMode(_rs, OUTPUT);
DIRECT_WRITE_HIGH(_dcport, _dcpinmask);
}
maybeUpdateTCR(LPSPI_TCR_PCS(1) | LPSPI_TCR_FRAMESZ(7));
#endif
// BUGBUG
// digitalWrite(_cs, LOW);
if (_rst != 0xff) {
pinMode(_rst, OUTPUT);
digitalWrite(_rst, HIGH);
delay(100);
digitalWrite(_rst, LOW);
delay(100);
digitalWrite(_rst, HIGH);
delay(200);
}
if(cmdList) commandList(cmdList);
}
// Initialization for ST7735B screens
void ST7735_t3::initB(void)
{
commonInit(Bcmd);
}
// Initialization for ST7735R screens (green or red tabs)
void ST7735_t3::initR(uint8_t options)
{
commonInit(Rcmd1);
if (options == INITR_GREENTAB) {
commandList(Rcmd2green);
_colstart = 2;
_rowstart = 1;
} else if(options == INITR_144GREENTAB) {
_screenHeight = ST7735_TFTHEIGHT_144;
commandList(Rcmd2green144);
_colstart = 2;
_rowstart = 3;
} else if(options == INITR_144GREENTAB_OFFSET) {
_screenHeight = ST7735_TFTHEIGHT_144;
commandList(Rcmd2green144);
_colstart = 0;
_rowstart = 32;
} else if(options == INITR_MINI160x80) {
_screenHeight = ST7735_TFTHEIGHT_160;
_screenWidth = ST7735_TFTWIDTH_80;
commandList(Rcmd2green160x80);
_colstart = 24;
_rowstart = 0;
} else {
// _colstart, _rowstart left at default '0' values
commandList(Rcmd2red);
}
commandList(Rcmd3);
// if black or mini, change MADCTL color filter
if ((options == INITR_BLACKTAB) || (options == INITR_MINI160x80)){
writecommand(ST7735_MADCTL);
writedata_last(0xC0);
}
tabcolor = options;
setRotation(0);
}
void ST7735_t3::setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1)
{
beginSPITransaction();
setAddr(x0, y0, x1, y1);
writecommand(ST7735_RAMWR); // write to RAM
// The setAddrWindow/pushColor will only work if SPI is kept active during this loop...
endSPITransaction();
}
void ST7735_t3::pushColor(uint16_t color, boolean last_pixel)
{
//beginSPITransaction();
if (last_pixel) {
writedata16_last(color);
endSPITransaction();
} else {
writedata16(color);
}
}
void ST7735_t3::drawPixel(int16_t x, int16_t y, uint16_t color)
{
if ((x < 0) ||(x >= _width) || (y < 0) || (y >= _height)) return;
#ifdef ENABLE_ST77XX_FRAMEBUFFER
if (_use_fbtft) {
_pfbtft[y*_width + x] = color;
} else
#endif
{
beginSPITransaction();
setAddr(x,y,x+1,y+1);
writecommand(ST7735_RAMWR);
writedata16_last(color);
endSPITransaction();
}
}
void ST7735_t3::drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color)
{
// Rudimentary clipping
if ((x >= _width) || (y >= _height)) return;
if ((y+h-1) >= _height) h = _height-y;
#ifdef ENABLE_ST77XX_FRAMEBUFFER
if (_use_fbtft) {
uint16_t * pfbPixel = &_pfbtft[ y*_width + x];
while (h--) {
*pfbPixel = color;
pfbPixel += _width;
}
} else
#endif
{
beginSPITransaction();
setAddr(x, y, x, y+h-1);
writecommand(ST7735_RAMWR);
while (h-- > 1) {
writedata16(color);
}
writedata16_last(color);
endSPITransaction();
}
}
void ST7735_t3::drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color)
{
// Rudimentary clipping
if ((x >= _width) || (y >= _height)) return;
if ((x+w-1) >= _width) w = _width-x;
#ifdef ENABLE_ST77XX_FRAMEBUFFER
if (_use_fbtft) {
if ((x&1) || (w&1)) {
uint16_t * pfbPixel = &_pfbtft[ y*_width + x];
while (w--) {
*pfbPixel++ = color;
}
} else {
// X is even and so is w, try 32 bit writes..
uint32_t color32 = (color << 16) | color;
uint32_t * pfbPixel = (uint32_t*)((uint16_t*)&_pfbtft[ y*_width + x]);
while (w) {
*pfbPixel++ = color32;
w -= 2;
}
}
} else
#endif
{
beginSPITransaction();
setAddr(x, y, x+w-1, y);
writecommand(ST7735_RAMWR);
while (w-- > 1) {
writedata16(color);
}
writedata16_last(color);
endSPITransaction();
}
}
void ST7735_t3::fillScreen(uint16_t color)
{
fillRect(0, 0, _width, _height, color);
}
// fill a rectangle
void ST7735_t3::fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
{
// rudimentary clipping (drawChar w/big text requires this)
if ((x >= _width) || (y >= _height)) return;
if ((x + w - 1) >= _width) w = _width - x;
if ((y + h - 1) >= _height) h = _height - y;
#ifdef ENABLE_ST77XX_FRAMEBUFFER
if (_use_fbtft) {
if ((x&1) || (w&1)) {
uint16_t * pfbPixel_row = &_pfbtft[ y*_width + x];
for (;h>0; h--) {
uint16_t * pfbPixel = pfbPixel_row;
for (int i = 0 ;i < w; i++) {
*pfbPixel++ = color;
}
pfbPixel_row += _width;
}
} else {
// Horizontal is even number so try 32 bit writes instead
uint32_t color32 = (color << 16) | color;
uint32_t * pfbPixel_row = (uint32_t *)((uint16_t*)&_pfbtft[ y*_width + x]);
w = w/2; // only iterate half the times
for (;h>0; h--) {
uint32_t * pfbPixel = pfbPixel_row;
for (int i = 0 ;i < w; i++) {
*pfbPixel++ = color32;
}
pfbPixel_row += (_width/2);
}
}
} else
#endif
{
beginSPITransaction();
setAddr(x, y, x+w-1, y+h-1);
writecommand(ST7735_RAMWR);
for (y=h; y>0; y--) {
for(x=w; x>1; x--) {
writedata16(color);
}
writedata16_last(color);
}
endSPITransaction();
}
}
#define MADCTL_MY 0x80
#define MADCTL_MX 0x40
#define MADCTL_MV 0x20
#define MADCTL_ML 0x10
#define MADCTL_RGB 0x00
#define MADCTL_BGR 0x08
#define MADCTL_MH 0x04
void ST7735_t3::setRotation(uint8_t m)
{
beginSPITransaction();
writecommand(ST7735_MADCTL);
rotation = m % 4; // can't be higher than 3
switch (rotation) {
case 0:
if ((tabcolor == INITR_BLACKTAB) || (tabcolor == INITR_MINI160x80)) {
writedata_last(MADCTL_MX | MADCTL_MY | MADCTL_RGB);
} else {
writedata_last(MADCTL_MX | MADCTL_MY | MADCTL_BGR);
}
_width = _screenWidth;
_height = _screenHeight;
_xstart = _colstart;
_ystart = _rowstart;
break;
case 1:
if ((tabcolor == INITR_BLACKTAB) || (tabcolor == INITR_MINI160x80)) {
writedata_last(MADCTL_MY | MADCTL_MV | MADCTL_RGB);
} else {
writedata_last(MADCTL_MY | MADCTL_MV | MADCTL_BGR);
}
_height = _screenWidth;
_width = _screenHeight;
_ystart = _colstart;
_xstart = _rowstart;
break;
case 2:
if ((tabcolor == INITR_BLACKTAB) || (tabcolor == INITR_MINI160x80)) {
writedata_last(MADCTL_RGB);
} else {
writedata_last(MADCTL_BGR);
}
_width = _screenWidth;
_height = _screenHeight;
_xstart = _colstart;
// hack to make work on a couple different displays
_ystart = (_rowstart==0 || _rowstart==32)? 0 : 1;//_rowstart;
break;
case 3:
if ((tabcolor == INITR_BLACKTAB) || (tabcolor == INITR_MINI160x80)) {
writedata_last(MADCTL_MX | MADCTL_MV | MADCTL_RGB);
} else {
writedata_last(MADCTL_MX | MADCTL_MV | MADCTL_BGR);
}
_width = _screenHeight;
_height = _screenWidth;
_ystart = _colstart;
// hack to make work on a couple different displays
_xstart = (_rowstart==0 || _rowstart==32)? 0 : 1;//_rowstart;
break;
}
_rot = rotation; // remember the rotation...
//Serial.printf("SetRotation(%d) _xstart=%d _ystart=%d _width=%d, _height=%d\n", _rot, _xstart, _ystart, _width, _height);
endSPITransaction();
}
void ST7735_t3::setRowColStart(uint16_t x, uint16_t y) {
_rowstart = x;
_colstart = y;
if (_rot != 0xff) setRotation(_rot);
}