-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathILI9486_t3n.cpp
4624 lines (4095 loc) · 144 KB
/
ILI9486_t3n.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 library is for Waveshare 4inch RPi LCD (C) with ILI9486 and Teensy 4.x usage only
// Forked from:
// https://github.com/kurte/ILI9341_t3n
#include "ILI9486_t3n.h"
#include <SPI.h>
//#define DEBUG_ASYNC_UPDATE // Enable to print out dma info
//#define DEBUG_ASYNC_LEDS // Enable to use digitalWrites to Debug
#ifdef DEBUG_ASYNC_LEDS
#define DEBUG_PIN_1 0
#define DEBUG_PIN_2 1
#define DEBUG_PIN_3 4
#endif
#ifdef ENABLE_ILI9486_FRAMEBUFFER
#define CBALLOC (ILI9486_TFTHEIGHT*ILI9486_TFTWIDTH*2)
#define COUNT_WORDS_WRITE ((ILI9486_TFTHEIGHT*ILI9486_TFTWIDTH)/SCREEN_DMA_NUM_SETTINGS) // Note I know the divide will give whole number
#if defined(__MK66FX1M0__)
// T3.6 use Scatter/gather with chain to do transfer
DMASetting ILI9486_t3n::_dmasettings[4];
DMAChannel ILI9486_t3n::_dmatx;
#elif defined(__IMXRT1052__) || defined(__IMXRT1062__) // Teensy 4.x
//DMASetting ILI9486_t3n::_dmasettings[4];
//DMAChannel ILI9486_t3n::_dmatx;
#elif defined(__MK64FX512__)
// T3.5 - had issues scatter/gather so do just use channels/interrupts
// and update and continue
DMAChannel ILI9486_t3n::_dmatx;
DMAChannel ILI9486_t3n::_dmarx;
uint16_t ILI9486_t3n::_dma_count_remaining;
uint16_t ILI9486_t3n::_dma_write_size_words;
volatile short _dma_dummy_rx;
#endif
#if defined(__IMXRT1052__) || defined(__IMXRT1062__) // Teensy 4.x
ILI9486_t3n *ILI9486_t3n::_dmaActiveDisplay[3] = {0, 0, 0};
#else
ILI9486_t3n *ILI9486_t3n::_dmaActiveDisplay = 0;
#endif
//volatile uint8_t ILI9486_t3n::_dma_state = 0; // Use pointer to this as a way to get back to object...
//volatile uint32_t ILI9486_t3n::_dma_frame_count = 0; // Can return a frame count...
#if defined(__IMXRT1052__) || defined(__IMXRT1062__) // Teensy 4.x
void ILI9486_t3n::dmaInterrupt(void) {
if (_dmaActiveDisplay[0]) {
_dmaActiveDisplay[0]->process_dma_interrupt();
}
}
void ILI9486_t3n::dmaInterrupt1(void) {
if (_dmaActiveDisplay[1]) {
_dmaActiveDisplay[1]->process_dma_interrupt();
}
}
void ILI9486_t3n::dmaInterrupt2(void) {
if (_dmaActiveDisplay[2]) {
_dmaActiveDisplay[2]->process_dma_interrupt();
}
}
#else
void ILI9486_t3n::dmaInterrupt(void) {
if (_dmaActiveDisplay) {
_dmaActiveDisplay->process_dma_interrupt();
}
}
#endif
#ifdef DEBUG_ASYNC_UPDATE
extern void dumpDMA_TCD(DMABaseClass *dmabc, const char *psx_title);
#endif
void ILI9486_t3n::process_dma_interrupt(void) {
#ifdef DEBUG_ASYNC_LEDS
digitalWriteFast(DEBUG_PIN_2, HIGH);
#endif
#if defined(__MK66FX1M0__)
// T3.6
_dmatx.clearInterrupt();
#ifdef DEBUG_ASYNC_UPDATE
static uint8_t print_count;
if (print_count < 10) {
print_count++;
Serial.printf("TCD: %x D1:%x %x%c\n", (uint32_t)_dmatx.TCD->SADDR ,
(uint32_t)_dmasettings[1].TCD->SADDR, (uint32_t)_dmatx.TCD->DLASTSGA,
(_dmatx.TCD->SADDR > _dmasettings[1].TCD->SADDR)? '>' : '<');
}
#endif
if (_frame_callback_on_HalfDone && (_dmatx.TCD->SADDR > _dmasettings[1].TCD->SADDR)) {
_dma_sub_frame_count = 1; // set as partial frame.
} else {
_dma_frame_count++;
// See if we are in continuous mode or not..
if ((_dma_state & ILI9486_DMA_CONT) == 0) {
// We are in single refresh mode or the user has called cancel so
// Lets try to release the CS pin
waitFifoNotFull();
writecommand_last(ILI9486_NOP);
endSPITransaction();
_dma_state &= ~ILI9486_DMA_ACTIVE;
_dmaActiveDisplay = 0; // We don't have a display active any more...
}
_dma_sub_frame_count = 0; // set as partial frame.
}
if (_frame_complete_callback) (*_frame_complete_callback)();
// See if we should do call back or not...
#elif defined(__IMXRT1052__) || defined(__IMXRT1062__) // Teensy 4.x
// T4
#ifdef DEBUG_ASYNC_UPDATE
static uint8_t print_count;
if (print_count < 10) {
print_count++;
Serial.printf("TCD: %x D1:%x %x%c\n", (uint32_t)_dmatx.TCD->SADDR ,
(uint32_t)_dmasettings[1].TCD->SADDR, (uint32_t)_dmatx.TCD->DLASTSGA,
(_dmatx.TCD->SADDR > _dmasettings[1].TCD->SADDR)? '>' : '<');
}
#endif
_dmatx.clearInterrupt();
if (_frame_callback_on_HalfDone && (_dmatx.TCD->SADDR > _dmasettings[1].TCD->SADDR)) {
_dma_sub_frame_count = 1; // set as partial frame.
if (_frame_complete_callback) (*_frame_complete_callback)();
//Serial.print("-");
} else {
_dma_frame_count++;
_dma_sub_frame_count = 0;
//Serial.print(".");
//if ((_dma_frame_count & 0x1f) == 0)Serial.println();
#ifdef DEBUG_ASYNC_LEDS
digitalWriteFast(DEBUG_PIN_3, HIGH);
#endif
// See if we are in continuous mode or not..
if ((_dma_state & ILI9486_DMA_CONT) == 0) {
// We are in single refresh mode or the user has called cancel so
// Lets try to release the CS pin
//Serial.printf("Before FSR wait: %x %x\n", _pimxrt_spi->FSR, _pimxrt_spi->SR);
while (_pimxrt_spi->FSR & 0x1f) ; // wait until this one is complete
//Serial.printf("Before SR busy wait: %x\n", _pimxrt_spi->SR);
while (_pimxrt_spi->SR & LPSPI_SR_MBF) ; // wait until this one is complete
_dmatx.clearComplete();
//Serial.println("Restore FCR");
_pimxrt_spi->FCR = LPSPI_FCR_TXWATER(15); // _spi_fcr_save; // restore the FSR status...
_pimxrt_spi->DER = 0; // DMA no longer doing TX (or RX)
_pimxrt_spi->CR = LPSPI_CR_MEN | LPSPI_CR_RRF | LPSPI_CR_RTF; // actually clear both...
_pimxrt_spi->SR = 0x3f00; // clear out all of the other status...
maybeUpdateTCR(_tcr_dc_assert | LPSPI_TCR_FRAMESZ(7)); // output Command with 8 bits
// Serial.printf("Output NOP (SR %x CR %x FSR %x FCR %x %x TCR:%x)\n", _pimxrt_spi->SR, _pimxrt_spi->CR, _pimxrt_spi->FSR,
// _pimxrt_spi->FCR, _spi_fcr_save, _pimxrt_spi->TCR);
writecommand_last(ILI9486_NOP);
endSPITransaction();
_dma_state &= ~ILI9486_DMA_ACTIVE;
_dmaActiveDisplay[_spi_num] = 0; // We don't have a display active any more...
} else {
// Lets try to flush out memory
if (_frame_complete_callback) (*_frame_complete_callback)();
else if ((uint32_t)_pfbtft >= 0x20200000u) arm_dcache_flush(_pfbtft, CBALLOC);
}
#ifdef DEBUG_ASYNC_LEDS
digitalWriteFast(DEBUG_PIN_3, LOW);
#endif
}
asm("dsb");
#elif defined(__MK64FX512__)
//
// T3.5...
_dmarx.clearInterrupt();
_dmatx.clearComplete();
_dmarx.clearComplete();
if (!_dma_count_remaining && !(_dma_state & ILI9486_DMA_CONT)) {
// The DMA transfers are done.
_dma_frame_count++;
#ifdef DEBUG_ASYNC_LEDS
digitalWriteFast(DEBUG_PIN_3, HIGH);
#endif
_pkinetisk_spi->RSER = 0;
//_pkinetisk_spi->MCR = SPI_MCR_MSTR | SPI_MCR_CLR_RXF | SPI_MCR_PCSIS(0x1F); // clear out the queue
_pkinetisk_spi->SR = 0xFF0F0000;
_pkinetisk_spi->CTAR0 &= ~(SPI_CTAR_FMSZ(8)); // Hack restore back to 8 bits
writecommand_last(ILI9486_NOP);
endSPITransaction();
_dma_state &= ~ILI9486_DMA_ACTIVE;
_dmaActiveDisplay = 0; // We don't have a display active any more...
_dma_sub_frame_count = 0;
if (_frame_complete_callback) (*_frame_complete_callback)();
#ifdef DEBUG_ASYNC_LEDS
digitalWriteFast(DEBUG_PIN_3, LOW);
#endif
} else {
uint16_t w;
if (_dma_count_remaining) { // Still part of one frome.
bool half_done = _dma_count_remaining == (CBALLOC/4);
_dma_count_remaining -= _dma_write_size_words;
w = *((uint16_t*)_dmatx.TCD->SADDR);
_dmatx.TCD->SADDR = (volatile uint8_t*)(_dmatx.TCD->SADDR) + 2;
if (_frame_complete_callback && _frame_callback_on_HalfDone && half_done) {
_dma_sub_frame_count = 1;
(*_frame_complete_callback)();
}
} else { // start a new frame
_dma_frame_count++;
_dmatx.sourceBuffer(&_pfbtft[1], (_dma_write_size_words-1)*2);
_dmatx.TCD->SLAST = 0; // Finish with it pointing to next location
w = _pfbtft[0];
_dma_count_remaining = CBALLOC/2 - _dma_write_size_words; // how much more to transfer?
_dma_sub_frame_count = 0;
if (_frame_complete_callback) (*_frame_complete_callback)();
}
#ifdef DEBUG_ASYNC_UPDATE
// dumpDMA_TCD(&_dmatx);
// dumpDMA_TCD(&_dmarx);
#endif
_pkinetisk_spi->PUSHR = (w | SPI_PUSHR_CTAS(0) | SPI_PUSHR_CONT);
_dmarx.enable();
_dmatx.enable();
}
#endif
#ifdef DEBUG_ASYNC_LEDS
digitalWriteFast(DEBUG_PIN_2, LOW);
#endif
}
#endif
// Teensy 3.1 can only generate 30 MHz SPI when running at 120 MHz (overclock)
#define WIDTH ILI9486_TFTWIDTH
#define HEIGHT ILI9486_TFTHEIGHT
// Constructor when using hardware ILI9241_KINETISK__pspi-> Faster, but must use SPI pins
// specific to each board type (e.g. 11,13 for Uno, 51,52 for Mega, etc.)
ILI9486_t3n::ILI9486_t3n(uint8_t cs, uint8_t dc, uint8_t rst,
uint8_t mosi, uint8_t sclk, uint8_t miso)
{
_cs = cs;
_dc = dc;
_rst = rst;
_mosi = mosi;
_sclk = sclk;
_miso = miso;
_width = WIDTH;
_height = HEIGHT;
rotation = 0;
cursor_y = cursor_x = 0;
textsize_x = textsize_y = 1;
textcolor = textbgcolor = 0xFFFF;
wrap = true;
font = NULL;
gfxFont = NULL;
setClipRect();
setOrigin();
// Added to see how much impact actually using non hardware CS pin might be
_cspinmask = 0;
_csport = NULL;
#ifdef ENABLE_ILI9486_FRAMEBUFFER
_pfbtft = NULL;
_use_fbtft = 0; // Are we in frame buffer mode?
_we_allocated_buffer = NULL;
#endif
}
//=======================================================================
// Add optinal support for using frame buffer to speed up complex outputs
//=======================================================================
void ILI9486_t3n::setFrameBuffer(uint16_t *frame_buffer)
{
#ifdef ENABLE_ILI9486_FRAMEBUFFER
_pfbtft = frame_buffer;
/* // Maybe you don't want the memory cleared as you may be playing games wiht multiple buffers.
if (_pfbtft != NULL) {
memset(_pfbtft, 0, ILI9486_TFTHEIGHT*ILI9486_TFTWIDTH*2);
}
*/
_dma_state &= ~ILI9486_DMA_INIT; // clear that we init the dma chain as our buffer has changed...
#endif
}
#ifdef ENABLE_ILI9486_FRAMEBUFFER
void ILI9486_t3n::setFrameCompleteCB(void (*pcb)(), bool fCallAlsoHalfDone)
{
_frame_complete_callback = pcb;
_frame_callback_on_HalfDone = pcb ? fCallAlsoHalfDone : false;
noInterrupts();
_dma_state &= ~ILI9486_DMA_INIT; // Lets setup the call backs on next call out
interrupts();
}
#endif
uint8_t ILI9486_t3n::useFrameBuffer(boolean b) // use the frame buffer? First call will allocate
{
#ifdef ENABLE_ILI9486_FRAMEBUFFER
if (b) {
// First see if we need to allocate buffer
if (_pfbtft == NULL) {
// Hack to start frame buffer on 32 byte boundary
_we_allocated_buffer = (uint16_t *)malloc(CBALLOC+32);
if (_we_allocated_buffer == NULL)
return 0; // failed
_pfbtft = (uint16_t*) (((uintptr_t)_we_allocated_buffer + 32) & ~ ((uintptr_t) (31)));
memset(_pfbtft, 0, CBALLOC);
}
_use_fbtft = 1;
clearChangedRange(); // make sure the dirty range is updated.
} else
_use_fbtft = 0;
return _use_fbtft;
#else
return 0;
#endif
}
void ILI9486_t3n::freeFrameBuffer(void) // explicit call to release the buffer
{
#ifdef ENABLE_ILI9486_FRAMEBUFFER
if (_we_allocated_buffer) {
free(_we_allocated_buffer);
_pfbtft = NULL;
_use_fbtft = 0; // make sure the use is turned off
_we_allocated_buffer = NULL;
}
#endif
}
void ILI9486_t3n::updateScreen(void) // call to say update the screen now.
{
// Not sure if better here to check flag or check existence of buffer.
// Will go by buffer as maybe can do interesting things?
#ifdef ENABLE_ILI9486_FRAMEBUFFER
if (_use_fbtft) {
beginSPITransaction(_SPI_CLOCK);
if (_standard && !_updateChangedAreasOnly) {
// Doing full window.
setAddr(0, 0, _width-1, _height-1);
writecommand_cont(ILI9486_RAMWR);
// BUGBUG doing as one shot. Not sure if should or not or do like
// main code and break up into transactions...
uint16_t *pfbtft_end = &_pfbtft[(ILI9486_TFTWIDTH*ILI9486_TFTHEIGHT)-1]; // setup
uint16_t *pftbft = _pfbtft;
// Quick write out the data;
while (pftbft < pfbtft_end) {
writedata16_cont(*pftbft++);
}
writedata16_last(*pftbft);
} else {
// setup just to output the clip rectangle area anded with updated area if enabled
int16_t start_x = _displayclipx1;
int16_t start_y = _displayclipy1;
int16_t end_x = _displayclipx2 - 1;
int16_t end_y = _displayclipy2 - 1;
if (_updateChangedAreasOnly) {
// maybe update range of values to update...
if (_changed_min_x > start_x) start_x = _changed_min_x;
if (_changed_min_y > start_y) start_y = _changed_min_y;
if (_changed_max_x < end_x) end_x = _changed_max_x;
if (_changed_max_y < end_y) end_y = _changed_max_y;
}
// Only do if actual area to update
if ((start_x <= end_x) && (start_y <= end_y)) {
setAddr(start_x, start_y, end_x, end_y);
writecommand_cont(ILI9486_RAMWR);
// BUGBUG doing as one shot. Not sure if should or not or do like
// main code and break up into transactions...
uint16_t * pfbPixel_row = &_pfbtft[ start_y*_width + start_x];
for (uint16_t y = start_y; y <= end_y; y++) {
uint16_t * pfbPixel = pfbPixel_row;
for (uint16_t x = start_x; x < end_x; x++) {
writedata16_cont(*pfbPixel++);
}
if (y < (end_y))
writedata16_cont(*pfbPixel);
else
writedata16_last(*pfbPixel);
pfbPixel_row += _width; // setup for the next row.
}
}
}
endSPITransaction();
}
clearChangedRange(); // make sure the dirty range is updated.
#endif
}
#ifdef DEBUG_ASYNC_UPDATE
void dumpDMA_TCD(DMABaseClass *dmabc, const char *psz_title)
{
if (psz_title) Serial.print(psz_title);
Serial.printf("%x %x:", (uint32_t)dmabc, (uint32_t)dmabc->TCD);
Serial.printf("SA:%x SO:%d AT:%x NB:%x SL:%d DA:%x DO: %d CI:%x DL:%x CS:%x BI:%x\n", (uint32_t)dmabc->TCD->SADDR,
dmabc->TCD->SOFF, dmabc->TCD->ATTR, dmabc->TCD->NBYTES, dmabc->TCD->SLAST, (uint32_t)dmabc->TCD->DADDR,
dmabc->TCD->DOFF, dmabc->TCD->CITER, dmabc->TCD->DLASTSGA, dmabc->TCD->CSR, dmabc->TCD->BITER);
}
#endif
#ifdef ENABLE_ILI9486_FRAMEBUFFER
//==============================================
#ifdef ENABLE_ILI9486_FRAMEBUFFER
void ILI9486_t3n::initDMASettings(void)
{
// Serial.printf("initDMASettings called %d\n", _dma_state);
if (_dma_state) { // should test for init, but...
return; // we already init this.
}
//Serial.println("InitDMASettings");
uint8_t dmaTXevent = _spi_hardware->tx_dma_channel;
#if defined(__MK66FX1M0__)
// T3.6
// BUGBUG:: check for -1 as wont work on SPI2 on T3.5
// uint16_t *fbtft_start_dma_addr = _pfbtft;
//Serial.printf("CWW: %d %d %d\n", CBALLOC, SCREEN_DMA_NUM_SETTINGS, count_words_write);
// Now lets setup DMA access to this memory...
_dmasettings[0].sourceBuffer(&_pfbtft[1], (COUNT_WORDS_WRITE-1)*2);
_dmasettings[0].destination(_pkinetisk_spi->PUSHR);
// Hack to reset the destination to only output 2 bytes.
_dmasettings[0].TCD->ATTR_DST = 1;
_dmasettings[0].replaceSettingsOnCompletion(_dmasettings[1]);
_dmasettings[1].sourceBuffer(&_pfbtft[COUNT_WORDS_WRITE], COUNT_WORDS_WRITE*2);
_dmasettings[1].destination(_pkinetisk_spi->PUSHR);
_dmasettings[1].TCD->ATTR_DST = 1;
_dmasettings[1].replaceSettingsOnCompletion(_dmasettings[2]);
if (_frame_callback_on_HalfDone) _dmasettings[1].interruptAtHalf();
else _dmasettings[1].TCD->CSR &= ~DMA_TCD_CSR_INTHALF;
_dmasettings[2].sourceBuffer(&_pfbtft[COUNT_WORDS_WRITE*2], COUNT_WORDS_WRITE*2);
_dmasettings[2].destination(_pkinetisk_spi->PUSHR);
_dmasettings[2].TCD->ATTR_DST = 1;
_dmasettings[2].replaceSettingsOnCompletion(_dmasettings[3]);
// Sort of hack - but wrap around to output the first word again.
// This version wraps again but instead outputs whole first group, bypass 0...
_dmasettings[2].interruptAtCompletion(); // 2 is end of frame
_dmasettings[3].sourceBuffer(_pfbtft, COUNT_WORDS_WRITE*2);
_dmasettings[3].destination(_pkinetisk_spi->PUSHR);
_dmasettings[3].TCD->ATTR_DST = 1;
_dmasettings[3].replaceSettingsOnCompletion(_dmasettings[1]);
// Setup DMA main object
//Serial.println("Setup _dmatx");
_dmatx.begin(true);
_dmatx.triggerAtHardwareEvent(dmaTXevent);
_dmatx = _dmasettings[0];
_dmatx.attachInterrupt(dmaInterrupt);
#elif defined(__IMXRT1052__) || defined(__IMXRT1062__) // Teensy 4.x
// See if moving the frame buffer to other memory that is not cached helps out
// to remove tearing and the like...I know with 256 it will be either 256 or 248...
// 320*240/3 = 25600
_dmasettings[0].sourceBuffer(_pfbtft, (COUNT_WORDS_WRITE)*2);
_dmasettings[0].destination(_pimxrt_spi->TDR);
_dmasettings[0].TCD->ATTR_DST = 1;
_dmasettings[0].replaceSettingsOnCompletion(_dmasettings[1]);
_dmasettings[1].sourceBuffer(&_pfbtft[COUNT_WORDS_WRITE], COUNT_WORDS_WRITE*2);
_dmasettings[1].destination(_pimxrt_spi->TDR);
_dmasettings[1].TCD->ATTR_DST = 1;
_dmasettings[1].replaceSettingsOnCompletion(_dmasettings[2]);
if (_frame_callback_on_HalfDone) _dmasettings[1].interruptAtHalf();
else _dmasettings[1].TCD->CSR &= ~DMA_TCD_CSR_INTHALF;
_dmasettings[2].sourceBuffer(&_pfbtft[COUNT_WORDS_WRITE*2], COUNT_WORDS_WRITE*2);
_dmasettings[2].destination(_pimxrt_spi->TDR);
_dmasettings[2].TCD->ATTR_DST = 1;
_dmasettings[2].replaceSettingsOnCompletion(_dmasettings[0]);
_dmasettings[2].interruptAtCompletion();
// Setup DMA main object
//Serial.println("Setup _dmatx");
// Serial.println("DMA initDMASettings - before dmatx");
_dmatx.begin(true);
_dmatx.triggerAtHardwareEvent(dmaTXevent);
_dmatx = _dmasettings[0];
if (_spi_num == 0) _dmatx.attachInterrupt(dmaInterrupt);
else if (_spi_num == 1) _dmatx.attachInterrupt(dmaInterrupt1);
else _dmatx.attachInterrupt(dmaInterrupt2);
#elif defined(__MK64FX512__)
// T3.5
// Lets setup the write size. For SPI we can use up to 32767 so same size as we use on T3.6...
// But SPI1 and SPI2 max of 511. We will use 480 in that case as even divider...
_dmarx.disable();
_dmarx.source(_pkinetisk_spi->POPR);
_dmarx.TCD->ATTR_SRC = 1;
_dmarx.destination(_dma_dummy_rx);
_dmarx.disableOnCompletion();
_dmarx.triggerAtHardwareEvent( _spi_hardware->rx_dma_channel);
_dmarx.attachInterrupt(dmaInterrupt);
_dmarx.interruptAtCompletion();
// We may be using settings chain here so lets set it up.
// Now lets setup TX chain. Note if trigger TX is not set
// we need to have the RX do it for us.
_dmatx.disable();
_dmatx.destination(_pkinetisk_spi->PUSHR);
_dmatx.TCD->ATTR_DST = 1;
_dmatx.disableOnCompletion();
// SPI on T3.5 only SPI object can do full size...
if (_spi_num == 0) {
_dmatx.triggerAtHardwareEvent(dmaTXevent);
_dma_write_size_words = COUNT_WORDS_WRITE;
} else {
_dma_write_size_words = 480;
_dmatx.triggerAtTransfersOf(_dmarx);
}
//Serial.printf("Init DMA Settings: TX:%d size:%d\n", dmaTXevent, _dma_write_size_words);
#endif
_dma_state = ILI9486_DMA_INIT; // Should be first thing set!
// Serial.println("DMA initDMASettings - end");
}
#endif
void ILI9486_t3n::dumpDMASettings() {
#ifdef DEBUG_ASYNC_UPDATE
#if defined(__MK66FX1M0__)
// T3.6
Serial.printf("DMA dump TCDs %d\n", _dmatx.channel);
dumpDMA_TCD(&_dmatx,"TX: ");
dumpDMA_TCD(&_dmasettings[0], " 0: ");
dumpDMA_TCD(&_dmasettings[1], " 1: ");
dumpDMA_TCD(&_dmasettings[2], " 2: ");
dumpDMA_TCD(&_dmasettings[3], " 3: ");
#elif defined(__IMXRT1052__) || defined(__IMXRT1062__) // Teensy 4.x
// Serial.printf("DMA dump TCDs %d\n", _dmatx.channel);
dumpDMA_TCD(&_dmatx,"TX: ");
dumpDMA_TCD(&_dmasettings[0], " 0: ");
dumpDMA_TCD(&_dmasettings[1], " 1: ");
dumpDMA_TCD(&_dmasettings[2], " 2: ");
#elif defined(__MK64FX512__)
Serial.printf("DMA dump TX:%d RX:%d\n", _dmatx.channel, _dmarx.channel);
dumpDMA_TCD(&_dmatx);
dumpDMA_TCD(&_dmarx);
#endif
#endif
}
bool ILI9486_t3n::updateScreenAsync(bool update_cont) // call to say update the screen now.
{
// Not sure if better here to check flag or check existence of buffer.
// Will go by buffer as maybe can do interesting things?
// BUGBUG:: only handles full screen so bail on the rest of it...
#ifdef ENABLE_ILI9486_FRAMEBUFFER
if (!_use_fbtft) return false;
#if defined(__MK64FX512__) // If T3.5 only allow on SPI...
// The T3.5 DMA to SPI has issues with preserving stuff like we want 16 bit mode
// and we want CS to stay on... So hack it. We will turn off using CS for the CS
// pin.
if (!_csport) {
pcs_data = 0;
pcs_command = pcs_data | _pspi->setCS(_dc);
pinMode(_cs, OUTPUT);
_csport = portOutputRegister(digitalPinToPort(_cs));
_cspinmask = digitalPinToBitMask(_cs);
*_csport |= _cspinmask;
}
#endif
#ifdef DEBUG_ASYNC_LEDS
digitalWriteFast(DEBUG_PIN_1, HIGH);
#endif
// Init DMA settings.
initDMASettings();
// Don't start one if already active.
if (_dma_state & ILI9486_DMA_ACTIVE) {
#ifdef DEBUG_ASYNC_LEDS
digitalWriteFast(DEBUG_PIN_1, LOW);
#endif
return false;
}
#if defined(__MK66FX1M0__)
//==========================================
// T3.6
//==========================================
if (update_cont) {
// Try to link in #3 into the chain
//_dmasettings[2].replaceSettingsOnCompletion(_dmasettings[3]);
//_dmasettings[2].TCD->CSR &= ~(DMA_TCD_CSR_INTMAJOR | DMA_TCD_CSR_DREQ); // Don't interrupt on this one...
_dmasettings[2].TCD->CSR &= ~(DMA_TCD_CSR_DREQ); // Don't disable on this one
_dma_state |= ILI9486_DMA_CONT;
} else {
// In this case we will only run through once...
//_dmasettings[2].replaceSettingsOnCompletion(_dmasettings[0]);
_dmasettings[2].interruptAtCompletion();
_dmasettings[2].disableOnCompletion();
_dma_state &= ~ILI9486_DMA_CONT;
}
#ifdef DEBUG_ASYNC_UPDATE
dumpDMASettings();
#endif
beginSPITransaction(_SPI_CLOCK);
// Doing full window.
setAddr(0, 0, _width-1, _height-1);
writecommand_cont(ILI9486_RAMWR);
// Write the first Word out before enter DMA as to setup the proper CS/DC/Continue flaugs
writedata16_cont(*_pfbtft);
// now lets start up the DMA
// volatile uint16_t biter = _dmatx.TCD->BITER;
//DMA_CDNE_CDNE(_dmatx.channel);
_dmatx = _dmasettings[0];
// _dmatx.TCD->BITER = biter;
_dma_frame_count = 0; // Set frame count back to zero.
_dmaActiveDisplay = this;
_dma_state |= ILI9486_DMA_ACTIVE;
_pkinetisk_spi->RSER |= SPI_RSER_TFFF_DIRS | SPI_RSER_TFFF_RE; // Set DMA Interrupt Request Select and Enable register
_pkinetisk_spi->MCR &= ~SPI_MCR_HALT; //Start transfers.
_dmatx.enable();
//==========================================
// T4
//==========================================
#elif defined(__IMXRT1052__) || defined(__IMXRT1062__) // Teensy 4.x
/////////////////////////////
// BUGBUG try first not worry about continueous or not.
// Start off remove disable on completion from both...
// it will be the ISR that disables it...
if ((uint32_t)_pfbtft >= 0x20200000u) arm_dcache_flush(_pfbtft, CBALLOC);
_dmasettings[2].TCD->CSR &= ~( DMA_TCD_CSR_DREQ);
beginSPITransaction(_SPI_CLOCK);
// Doing full window.
setAddr(0, 0, _width-1, _height-1);
writecommand_last(ILI9486_RAMWR);
// Update TCR to 16 bit mode. and output the first entry.
_spi_fcr_save = _pimxrt_spi->FCR; // remember the FCR
_pimxrt_spi->FCR = 0; // clear water marks...
maybeUpdateTCR(_tcr_dc_not_assert | LPSPI_TCR_FRAMESZ(15) | LPSPI_TCR_RXMSK /*| LPSPI_TCR_CONT*/);
_pimxrt_spi->DER = LPSPI_DER_TDDE;
_pimxrt_spi->SR = 0x3f00; // clear out all of the other status...
//_dmatx.triggerAtHardwareEvent( _spi_hardware->tx_dma_channel );
_dmatx = _dmasettings[0];
_dmatx.begin(false);
_dmatx.enable();
_dma_frame_count = 0; // Set frame count back to zero.
_dmaActiveDisplay[_spi_num] = this;
if (update_cont) {
_dma_state |= ILI9486_DMA_CONT;
} else {
_dmasettings[2].disableOnCompletion();
_dma_state &= ~ILI9486_DMA_CONT;
}
_dma_state |= ILI9486_DMA_ACTIVE;
#ifdef DEBUG_ASYNC_UPDATE
dumpDMASettings();
#endif
#elif defined(__MK64FX512__)
//==========================================
// T3.5
//==========================================
// lets setup the initial pointers.
_dmatx.sourceBuffer(&_pfbtft[1], (_dma_write_size_words-1)*2);
_dmatx.TCD->SLAST = 0; // Finish with it pointing to next location
_dmarx.transferCount(_dma_write_size_words);
_dma_count_remaining = CBALLOC/2 - _dma_write_size_words; // how much more to transfer?
//Serial.printf("SPI1/2 - TC:%d TR:%d\n", _dma_write_size_words, _dma_count_remaining);
#ifdef DEBUG_ASYNC_UPDATE
dumpDMASettings();
#endif
beginSPITransaction(_SPI_CLOCK);
// Doing full window.
setAddr(0, 0, _width-1, _height-1);
writecommand_cont(ILI9486_RAMWR);
// Write the first Word out before enter DMA as to setup the proper CS/DC/Continue flaugs
// On T3.5 DMA only appears to work with CTAR 0 so hack it up...
_pkinetisk_spi->CTAR0 |= SPI_CTAR_FMSZ(8); // Hack convert from 8 bit to 16 bit...
_pkinetisk_spi->MCR = SPI_MCR_MSTR | SPI_MCR_CLR_RXF | SPI_MCR_PCSIS(0x1F);
_pkinetisk_spi->SR = 0xFF0F0000;
// Lets try to output the first byte to make sure that we are in 16 bit mode...
_pkinetisk_spi->PUSHR = *_pfbtft | SPI_PUSHR_CTAS(0) | SPI_PUSHR_CONT;
if (_spi_num == 0) {
// SPI - has both TX and RX so use it
_pkinetisk_spi->RSER = SPI_RSER_RFDF_RE | SPI_RSER_RFDF_DIRS | SPI_RSER_TFFF_RE | SPI_RSER_TFFF_DIRS;
_dmarx.enable();
_dmatx.enable();
} else {
_pkinetisk_spi->RSER = SPI_RSER_RFDF_RE | SPI_RSER_RFDF_DIRS ;
_dmatx.triggerAtTransfersOf(_dmarx);
_dmatx.enable();
_dmarx.enable();
}
_dma_frame_count = 0; // Set frame count back to zero.
_dmaActiveDisplay = this;
if (update_cont) {
_dma_state |= ILI9486_DMA_CONT;
} else {
_dma_state &= ~ILI9486_DMA_CONT;
}
_dma_state |= ILI9486_DMA_ACTIVE;
#endif
#ifdef DEBUG_ASYNC_LEDS
digitalWriteFast(DEBUG_PIN_1, LOW);
#endif
return true;
#else
return false; // no frame buffer so will never start...
#endif
}
void ILI9486_t3n::endUpdateAsync() {
// make sure it is on
#ifdef ENABLE_ILI9486_FRAMEBUFFER
if (_dma_state & ILI9486_DMA_CONT) {
_dma_state &= ~ILI9486_DMA_CONT; // Turn of the continueous mode
#if defined(__MK66FX1M0__) || defined(__IMXRT1062__)
_dmasettings[2].disableOnCompletion();
#endif
}
#endif
}
void ILI9486_t3n::waitUpdateAsyncComplete(void)
{
#ifdef ENABLE_ILI9486_FRAMEBUFFER
#ifdef DEBUG_ASYNC_LEDS
digitalWriteFast(DEBUG_PIN_3, HIGH);
#endif
while ((_dma_state & ILI9486_DMA_ACTIVE)) {
// asm volatile("wfi");
};
#ifdef DEBUG_ASYNC_LEDS
digitalWriteFast(DEBUG_PIN_3, LOW);
#endif
#endif
}
#endif
//=======================================================================
void ILI9486_t3n::setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1)
{
beginSPITransaction(_SPI_CLOCK);
setAddr(x0, y0, x1, y1);
writecommand_last(ILI9486_RAMWR); // write to RAM
endSPITransaction();
}
void ILI9486_t3n::pushColor(uint16_t color)
{
beginSPITransaction(_SPI_CLOCK);
writedata16_last(color);
endSPITransaction();
}
void ILI9486_t3n::drawPixel(int16_t x, int16_t y, uint16_t color) {
x += _originx;
y += _originy;
if((x < _displayclipx1) ||(x >= _displayclipx2) || (y < _displayclipy1) || (y >= _displayclipy2)) return;
#ifdef ENABLE_ILI9486_FRAMEBUFFER
if (_use_fbtft) {
updateChangedRange(x, y); // update the range of the screen that has been changed;
_pfbtft[y*_width + x] = color;
} else
#endif
{
beginSPITransaction(_SPI_CLOCK);
setAddr(x, y, x, y);
writecommand_cont(ILI9486_RAMWR);
writedata16_last(color);
endSPITransaction();
}
}
void ILI9486_t3n::drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color)
{
x+=_originx;
y+=_originy;
// Rectangular clipping
if((x < _displayclipx1) || (x >= _displayclipx2) || (y >= _displayclipy2)) return;
if(y < _displayclipy1) { h = h - (_displayclipy1 - y); y = _displayclipy1;}
if((y+h-1) >= _displayclipy2) h = _displayclipy2-y;
if(h<1) return;
#ifdef ENABLE_ILI9486_FRAMEBUFFER
if (_use_fbtft) {
updateChangedRange(x, y, 1, h); // update the range of the screen that has been changed;
uint16_t * pfbPixel = &_pfbtft[ y*_width + x];
while (h--) {
*pfbPixel = color;
pfbPixel += _width;
}
} else
#endif
{
beginSPITransaction(_SPI_CLOCK);
setAddr(x, y, x, y+h-1);
writecommand_cont(ILI9486_RAMWR);
while (h-- > 1) {
writedata16_cont(color);
}
writedata16_last(color);
endSPITransaction();
}
}
void ILI9486_t3n::drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color)
{
x+=_originx;
y+=_originy;
// Rectangular clipping
if((y < _displayclipy1) || (x >= _displayclipx2) || (y >= _displayclipy2)) return;
if(x<_displayclipx1) { w = w - (_displayclipx1 - x); x = _displayclipx1; }
if((x+w-1) >= _displayclipx2) w = _displayclipx2-x;
if (w<1) return;
#ifdef ENABLE_ILI9486_FRAMEBUFFER
if (_use_fbtft) {
updateChangedRange(x, y, w, 1); // update the range of the screen that has been changed;
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(_SPI_CLOCK);
setAddr(x, y, x+w-1, y);
writecommand_cont(ILI9486_RAMWR);
while (w-- > 1) {
writedata16_cont(color);
}
writedata16_last(color);
endSPITransaction();
}
}
void ILI9486_t3n::fillScreen(uint16_t color)
{
#ifdef ENABLE_ILI9486_FRAMEBUFFER
if (_use_fbtft && _standard) {
// Speed up lifted from Franks DMA code... _standard is if no offsets and rects..
updateChangedRange(0, 0, _width, _height); // update the range of the screen that has been changed;
uint32_t color32 = (color << 16) | color;
uint32_t *pfbPixel = (uint32_t *)_pfbtft;
uint32_t *pfbtft_end = (uint32_t *)((uint16_t *)&_pfbtft[(ILI9486_TFTWIDTH * ILI9486_TFTHEIGHT)]); // setup
while (pfbPixel < pfbtft_end) {
*pfbPixel++ = color32; *pfbPixel++ = color32; *pfbPixel++ = color32;*pfbPixel++ = color32;
*pfbPixel++ = color32; *pfbPixel++ = color32; *pfbPixel++ = color32;*pfbPixel++ = color32;
*pfbPixel++ = color32; *pfbPixel++ = color32; *pfbPixel++ = color32;*pfbPixel++ = color32;
*pfbPixel++ = color32; *pfbPixel++ = color32; *pfbPixel++ = color32;*pfbPixel++ = color32;
*pfbPixel++ = color32; *pfbPixel++ = color32; *pfbPixel++ = color32;*pfbPixel++ = color32;
*pfbPixel++ = color32; *pfbPixel++ = color32; *pfbPixel++ = color32;*pfbPixel++ = color32;
*pfbPixel++ = color32; *pfbPixel++ = color32; *pfbPixel++ = color32;*pfbPixel++ = color32;
*pfbPixel++ = color32; *pfbPixel++ = color32; *pfbPixel++ = color32;*pfbPixel++ = color32;
}
} else
#endif
{
fillRect(0, 0, _width, _height, color);
}
}
// fill a rectangle
void ILI9486_t3n::fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
{
x+=_originx;
y+=_originy;
// Rectangular clipping (drawChar w/big text requires this)
if((x >= _displayclipx2) || (y >= _displayclipy2)) return;
if (((x+w) <= _displayclipx1) || ((y+h) <= _displayclipy1)) return;
if(x < _displayclipx1) { w -= (_displayclipx1-x); x = _displayclipx1; }
if(y < _displayclipy1) { h -= (_displayclipy1 - y); y = _displayclipy1; }
if((x + w - 1) >= _displayclipx2) w = _displayclipx2 - x;
if((y + h - 1) >= _displayclipy2) h = _displayclipy2 - y;
#ifdef ENABLE_ILI9486_FRAMEBUFFER
if (_use_fbtft) {
updateChangedRange(x, y, w, h); // update the range of the screen that has been changed;
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
{
// TODO: this can result in a very long transaction time
// should break this into multiple transactions, even though
// it'll cost more overhead, so we don't stall other SPI libs
beginSPITransaction(_SPI_CLOCK);
setAddr(x, y, x+w-1, y+h-1);
writecommand_cont(ILI9486_RAMWR);
for(y=h; y>0; y--) {
for(x=w; x>1; x--) {
writedata16_cont(color);
}
writedata16_last(color);
#if 0
if (y > 1 && (y & 1)) {
endSPITransaction();
beginSPITransaction(_SPI_CLOCK);
}
#endif
}
endSPITransaction();
}
}
// fillRectVGradient - fills area with vertical gradient
void ILI9486_t3n::fillRectVGradient(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color1, uint16_t color2)
{
x+=_originx;
y+=_originy;