forked from sumotoy/RA8875
-
Notifications
You must be signed in to change notification settings - Fork 4
/
RA8875.cpp
5728 lines (5356 loc) · 173 KB
/
RA8875.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
/*
Part of RA8875 library from https://github.com/sumotoy/RA8875
License:GNU General Public License v3.0
RA8875 fast SPI library for RAiO SPI RA8875 drived TFT
Copyright (C) 2014 egidio massimo costa sumotoy (a t) gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <SPI.h>
#include "RA8875.h"
#if defined (USE_FT5206_TOUCH)
#include <Wire.h>
#if defined(___DUESTUFF) && defined(USE_DUE_WIRE1_INTERFACE)
#define Wire Wire1
#endif
const uint8_t _ctpAdrs = 0x38;
const uint8_t coordRegStart[5] = {0x03,0x09,0x0F,0x15,0x1B};
static volatile bool _FT5206_INT = false;
#endif
static volatile uint8_t _RA8875_INTS = 0b00000000;//container for INT states
/*------------------------------
Bit: Called by: In use:
--------------------------------
0: isr triggered [*]
1: Resistive TS [*]
2: KeyScan [*]
3: DMA
4: BTE
5: FT5206 TS [*]
6: -na-
7: -na-
--------------------------------*/
/**************************************************************************/
/*!
Contructors
CSp: SPI SS pin
RSTp: Reset pin
INTp: INT pin
//Teensy 3.0 , 3.1 , LC
mosi_pin
sclk_pin
miso_pin
Note:
Teensy CS SPI1:[MOSI1(0) MISO1(1) CLK1(20) CS1(6)]
*/
/**************************************************************************/
//------------------------------TEENSY 3/3.1 ---------------------------------------
#if defined(__MK20DX128__) || defined(__MK20DX256__)
RA8875::RA8875(const uint8_t CSp,const uint8_t RSTp,const uint8_t mosi_pin,const uint8_t sclk_pin,const uint8_t miso_pin)
{
_mosi = mosi_pin;
_miso = miso_pin;
_sclk = sclk_pin;
_cs = CSp;
_rst = RSTp;
//------------------------------Teensy LC-------------------------------------------
#elif defined(__MKL26Z64__)
RA8875::RA8875(const uint8_t CSp,const uint8_t RSTp,const uint8_t mosi_pin,const uint8_t sclk_pin,const uint8_t miso_pin)
{
_mosi = mosi_pin;
_miso = miso_pin;
_sclk = sclk_pin;
_cs = CSp;
_rst = RSTp;
_altSPI = false;
//---------------------------------DUE--------------------------------------------
#elif defined(___DUESTUFF)//DUE
RA8875::RA8875(const uint8_t CSp, const uint8_t RSTp)
{
_cs = CSp;
_rst = RSTp;
//------------------------------ENERGIA-------------------------------------------
#elif defined(NEEDS_SET_MODULE)
RA8875::RA8875(const uint8_t module, const uint8_t RSTp)
{
selectCS(module);
_rst = RSTp;
_cs = 255;
//----------------------------8 BIT ARDUINO's---------------------------------------
#else
RA8875::RA8875(const uint8_t CSp, const uint8_t RSTp)
{
_cs = CSp;
_rst = RSTp;
#endif
}
/**************************************************************************/
/*!
Helper for Energia, it will set CS pin accordly module selected
module: 0...3
[private]
*/
/**************************************************************************/
#if defined(NEEDS_SET_MODULE)
void RA8875::selectCS(uint8_t module)
{
if (module > 3) module = 3;
switch(module){
case 0:
_cs = PA_3;
break;
case 1:
_cs = PF_3;
break;
case 2:
_cs = PB_5;
break;
case 3:
_cs = PD_1;
break;
}
SPImodule = module;
}
#endif
/**************************************************************************/
/*!
Initialize library and SPI
Parameter:
(display type)
RA8875_480x272 (4.3" displays)
RA8875_800x480 (5" and 7" displays)
Adafruit_480x272 (4.3" Adafruit displays)
Adafruit_800x480 (5" and 7" Adafruit displays)
(colors) - The color depth (default 16) 8 or 16 (bit)
-------------------------------------------------------------
UPDATE! in Energia IDE some devices needs an extra parameter!
module: sets the SPI interface (it depends from MCU). Default:0
*/
/**************************************************************************/
void RA8875::begin(const enum RA8875sizes s,uint8_t colors)
{
_errorCode = 0;
_displaySize = s;
_rotation = 0;
_portrait = false;
_inited = false;
_sleep = false;
_hasLayerLimits = false;
_intPin = 255;
_intNum = 0;
_useISR = false;
_enabledInterrups = 0b00000000;
/* used to understand wat causes an INT
bit
0:
1:
2: Touch (resistive)
3:
4:
5:
6:
7:
8:
*/
#if defined(USE_FT5206_TOUCH)
_intCTSPin = 255;
_intCTSNum = 0;
#endif
#if defined(USE_RA8875_SEPARATE_TEXT_COLOR)
_TXTForeColor = _RA8875_DEFAULTTXTFRGRND;
#if defined(_RA8875_DEFAULTTXTBKGRND)
_TXTBackColor = _RA8875_DEFAULTTXTBKGRND;
#else
_TXTBackColor = 0x0000;
#endif
_TXTrecoverColor = false;
#endif
_maxLayers = 2;
_currentLayer = 0;
_useMultiLayers = false;//starts with one layer only
_textMode = false;
_brightness = 255;
_cursorX = 0; _cursorY = 0; _scrollXL = 0; _scrollXR = 0; _scrollYT = 0; _scrollYB = 0;
_scaleX = 1; _scaleY = 1;
_scaling = false;
_EXTFNTsize = X16;
_FNTspacing = 0;
//_FNTrender = false;
/* set--> _TXTparameters <--
0:_extFontRom = false;
1:_autoAdvance = true;
2:_textWrap = user defined
3:_fontFullAlig = false;
4:_fontRotation = false;//not used
5:_alignXToCenter = false;
6:_alignYToCenter = false;
7: render = false;
*/
_TXTparameters = 0b00000010;
bitWrite(_TXTparameters,2,_DFT_RA8875_TEXTWRAP);//set _textWrap
_relativeCenter = false;
_absoluteCenter = false;
_EXTFNTrom = _DFT_RA8875_EXTFONTROMTYPE;
_EXTFNTcoding = _DFT_RA8875_EXTFONTROMCODING;
//_FNTsource = INT;
_FNTinterline = 0;
_EXTFNTfamily = STANDARD;
_FNTcursorType = NOCURSOR;
_FNTgrandient = false;
_arcAngle_max = ARC_ANGLE_MAX;
_arcAngle_offset = ARC_ANGLE_OFFSET;
_angle_offset = ANGLE_OFFSET;
_color_bpp = 16;
_colorIndex = 0;
if (colors != 16) {
_color_bpp = 8;
_colorIndex = 3;
}
switch (_displaySize){
case RA8875_480x272:
case Adafruit_480x272:
_width = 480;
_height = 272;
_initIndex = 0;
break;
case RA8875_800x480:
case Adafruit_800x480:
case RA8875_800x480ALT:
_width = 800;
_height = 480;
_hasLayerLimits = true;
_maxLayers = 1;
if (_color_bpp < 16) _maxLayers = 2;
_initIndex = 1;
if (_displaySize == RA8875_800x480ALT) _initIndex = 2;
break;
default:
_errorCode |= (1 << 0);//set
return;
}
RA8875_WIDTH = _width;
RA8875_HEIGHT = _height;
_activeWindowXL = 0;
_activeWindowXR = RA8875_WIDTH;
_activeWindowYT = 0;
_activeWindowYB = RA8875_HEIGHT;
#if !defined(_AVOID_TOUCHSCREEN)//common to all touch
_clearTInt = false;
_touchEnabled = false;
#if defined(USE_RA8875_TOUCH)//resistive touch
_calibrated = _isCalibrated();//check calibration at startup
if (!_calibrated) {
_tsAdcMinX = 0; _tsAdcMinY = 0; _tsAdcMaxX = 1023; _tsAdcMaxY = 1023;
} else {
_tsAdcMinX = TOUCSRCAL_XLOW; _tsAdcMinY = TOUCSRCAL_YLOW; _tsAdcMaxX = TOUCSRCAL_XHIGH; _tsAdcMaxY = TOUCSRCAL_YHIGH;
}
#endif
#endif
#if defined(USE_RA8875_KEYMATRIX)
_keyMatrixEnabled = false;
#endif
/* Display Configuration Register [0x20]
7: (Layer Setting Control) 0:one Layer, 1:two Layers
6,5,4: (na)
3: (Horizontal Scan Direction) 0: SEG0 to SEG(n-1), 1: SEG(n-1) to SEG0
2: (Vertical Scan direction) 0: COM0 to COM(n-1), 1: COM(n-1) to COM0
1,0: (na) */
_DPCR_Reg = 0b00000000;
/* Memory Write Control Register 0 [0x40]
7: 0(graphic mode), 1(textx mode)
6: 0(font-memory cursor not visible), 1(visible)
5: 0(normal), 1(blinking)
4: na
3-2: 00(LR,TB), 01(RL,TB), 10(TB,LR), 11(BT,LR)
1: 0(Auto Increase in write), 1(no)
0: 0(Auto Increase in read), 1(no) */
_MWCR0_Reg = 0b00000000;
/* Font Control Register 0 [0x21]
7: 0(CGROM font is selected), 1(CGRAM font is selected)
6: na
5: 0(Internal CGROM [reg 0x2F to 00]), 1(External CGROM [0x2E reg, bit6,7 to 0)
4-2: na
1-0: 00(ISO/IEC 8859-1), 01(ISO/IEC 8859-2), 10(ISO/IEC 8859-3), 11(ISO/IEC 8859-4)*/
_FNCR0_Reg = 0b00000000;
/* Font Control Register 1 [0x22]
7: 0(Full Alignment off), 1(Full Alignment on)
6: 0(no-trasparent), 1(trasparent)
5: na
4: 0(normal), 1(90degrees)
3-2: 00(x1), 01(x2), 10(x3), 11(x3) Horizontal font scale
1-0: 00(x1), 01(x2), 10(x3), 11(x3) Vertical font scale */
_FNCR1_Reg = 0b00000000;
/* Font Write Type Setting Register [0x2E]
7-6: 00(16x16,8x16,nx16), 01(24x24,12x24,nx24), 1x(32x32,16x32, nx32)
5-0: 00...3F (font width off to 63 pixels)*/
_FWTSET_Reg = 0b00000000;
/* Serial Font ROM Setting [0x2F]
GT Serial Font ROM Select
7-5: 000(GT21L16TW/GT21H16T1W),001(GT30L16U2W),010(GT30L24T3Y/GT30H24T3Y),011(GT30L24M1Z),111(GT30L32S4W/GT30H32S4W)
FONT ROM Coding Setting
4-2: 000(GB2312),001(GB12345/GB18030),010(BIG5),011(UNICODE),100(ASCII),101(UNI-Japanese),110(JIS0208),111(Latin/Greek/Cyrillic/Arabic)
1-0: 00...11
bits ASCII Lat/Gr/Cyr Arabic
00 normal normal na
01 Arial var Wdth Pres Forms A
10 Roman na Pres Forms B
11 Bold na na */
_SFRSET_Reg = 0b00000000;
/* Interrupt Control Register1 [0xF0]
7,6,5: (na)
4: KEYSCAN Interrupt Enable Bit
3: DMA Interrupt Enable Bit
2: TOUCH Panel Interrupt Enable Bit
1: BTE Process Complete Interrupt Enable Bit
0:
When MCU-relative BTE operation is selected(*1) and BTE
Function is Enabled(REG[50h] Bit7 = 1), this bit is used to
Enable the BTE Interrupt for MCU R/W:
0 : Disable BTE interrupt for MCU R/W.
1 : Enable BTE interrupt for MCU R/W.
When the BTE Function is Disabled, this bit is used to
Enable the Interrupt of Font Write Function:
0 : Disable font write interrupt.
1 : Enable font write interrupt.
*/
_INTC1_Reg = 0b00000000;
//------------------------------- Start SPI initialization ------------------------------------------
#if defined(__MK20DX128__) || defined(__MK20DX256__)//Teensy 3,3.1
//always uses SPI transaction
if ((_mosi == 11 || _mosi == 7) && (_miso == 12 || _miso == 8) && (_sclk == 13 || _sclk == 14)) {//valid SPI pins?
if (_mosi != 11) SPI.setMOSI(_mosi);
if (_miso != 12) SPI.setMISO(_miso);
if (_sclk != 13) SPI.setSCK(_sclk);
} else {
_errorCode |= (1 << 1);//set
return;
}
if (!SPI.pinIsChipSelect(_cs)) {
_errorCode |= (1 << 2);//set
return;
}
SPI.begin();
#elif defined(__MKL26Z64__)//TeensyLC
//always uses SPI ransaction
#if TEENSYDUINO > 121//not supported prior 1.22!
if ((_mosi == 11 || _mosi == 7 || _mosi == 0 || _mosi == 21) && (_miso == 12 || _miso == 8 || _miso == 1 || _miso == 5) && (_sclk == 13 || _sclk == 14 || _sclk == 20)) {//valid SPI pins?
if ((_mosi == 0 || _mosi == 21) && (_miso == 1 || _miso == 5) && (_sclk == 20)) {//identify alternate SPI channel 1 (24Mhz)
_altSPI = true;
if (_cs != 6){//on SPI1 cs should be only 6!
_errorCode |= (1 << 2);//set
return;
}
if (_mosi != 11) SPI1.setMOSI(_mosi);
if (_miso != 12) SPI1.setMISO(_miso);
if (_sclk != 13) SPI1.setSCK(_sclk);
SPI1.begin();
} else {//default SPI channel 0 (12Mhz)
_altSPI = false;
if (_mosi != 11) SPI.setMOSI(_mosi);
if (_miso != 12) SPI.setMISO(_miso);
if (_sclk != 13) SPI.setSCK(_sclk);
if (!SPI.pinIsChipSelect(_cs)) {//ERROR
_errorCode |= (1 << 2);//set
return;
}
SPI.begin();
}
} else {
_errorCode |= (1 << 1);//set
return;
}
#else
_altSPI = false;
SPI.begin();
_errorCode |= (1 << 3);//set
#endif
#endif
#if !defined(ENERGIA)//everithing but ENERGIA
#if defined(___TEENSYES)//all of them (32 bit only)
pinMode(_cs, OUTPUT);
digitalWrite(_cs, HIGH);
#elif defined(___DUESTUFF)// DUE
#if defined(SPI_DUE_MODE_EXTENDED)
//DUE SPI mode extended you can use only follow pins
if (_cs == 4 || _cs == 10 || _cs == 52) {
SPI.begin(_cs);
} else {
_errorCode |= (1 << 2);//error! wrong cs pin
return;
}
#else
//DUE in normal SPI mode
SPI.begin();
pinMode(_cs, OUTPUT);
#if defined(_FASTSSPORT)
csport = portOutputRegister(digitalPinToPort(_cs));
cspinmask = digitalPinToBitMask(_cs);
*csport |= cspinmask;//hi
#else
digitalWrite(_cs, HIGH);
#endif
#endif
#elif defined(__XTENSA__)
SPI.begin();
pinMode(_cs, OUTPUT);
digitalWrite(_cs, HIGH);
#else
//UNO,MEGA,Yun,nano,duemilanove and other 8 bit arduino's
SPI.begin();
pinMode(_cs, OUTPUT);
csport = portOutputRegister(digitalPinToPort(_cs));//pinMode(_cs, OUTPUT);
cspinmask = digitalPinToBitMask(_cs);
*csport |= cspinmask;//hi
#endif
#endif
if (_rst < 255){//time for hardware reset RA8875
pinMode(_rst, OUTPUT);
digitalWrite(_rst, HIGH);
delay(10);
digitalWrite(_rst, LOW);
delay(220);//120
digitalWrite(_rst, HIGH);
delay(300);//200
}
#if defined(ENERGIA) && defined(NEEDS_SET_MODULE)//energia specific
SPI.setModule(SPImodule);
#endif
//set SPI SPEED, starting at low speed, after init will raise up!
#if defined(SPI_HAS_TRANSACTION)
_SPImaxSpeed = 4000000UL;//we start in low speed here!
#else//do not use SPItransactons
#if defined (__AVR__)//8 bit arduino's
SPI.begin();
SPI.setClockDivider(SPI_SPEED_SAFE);
delay(1);
SPI.setDataMode(SPI_MODE3);
#else
#if defined(___DUESTUFF) && defined(SPI_DUE_MODE_EXTENDED)
SPI.setClockDivider(_cs,SPI_SPEED_SAFE);
delay(1);
SPI.setDataMode(_cs,SPI_MODE3);
#else
SPI.setClockDivider(SPI_SPEED_SAFE);
delay(1);
SPI.setDataMode(SPI_MODE3);
#endif
#endif
#endif
#if defined(ENERGIA)//dunno why but energia wants this here or not work!
pinMode(_cs, OUTPUT);
digitalWrite(_cs, HIGH);
#endif
//SPI initialization done
if (_errorCode != 0) return;//ouch, error/s.Better stop here!
_initialize();//----->Time to Initialize the RA8875!
//------- time for capacitive touch stuff -----------------
#if defined(USE_FT5206_TOUCH)
Wire.begin();
#if defined(___DUESTUFF)
Wire.setClock(400000UL); // Set I2C frequency to 400kHz
/*
#if !defined(USE_DUE_WIRE1_INTERFACE)//sorry but I do not own a DUE, have to learn about Wire1
// Force 400 KHz I2C, rawr! (Uses pins 20, 21 for SDA, SCL)
TWI1->TWI_CWGR = 0;
TWI1->TWI_CWGR = ((VARIANT_MCK / (2 * 400000)) - 4) * 0x101;
#endif
*/
#else
#if ARDUINO >= 157
Wire.setClock(400000UL); // Set I2C frequency to 400kHz
#else
TWBR = ((F_CPU / 400000UL) - 16) / 2; // Set I2C frequency to 400kHz
#endif
#endif
delay(10);
_initializeFT5206();//initialize FT5206 controller
_maxTouch = 5;
_gesture = 0;
_currentTouches = 0;
_currentTouchState = 0;
_needISRrearm = false;
//TO DO
//Modify RA8875 registers and disconnect internal Touch Screen totally
#endif
}
/************************* Initialization *********************************/
/**************************************************************************/
/*!
Hardware initialization of RA8875 and turn on
[private]
*/
/**************************************************************************/
void RA8875::_initialize()
{
_inited = false;
if (_rst == 255) {// soft reset time ?
writeCommand(RA8875_PWRR);
_writeData(RA8875_PWRR_SOFTRESET);
delay(20);
_writeData(RA8875_PWRR_NORMAL);
delay(200);
}
//set the sysClock
_setSysClock(initStrings[_initIndex][0],initStrings[_initIndex][1],initStrings[_initIndex][2]);
//color space setup
if (_color_bpp < 16){//256
_writeRegister(RA8875_SYSR,0x00);//256
_colorIndex = 3;
} else {
_writeRegister(RA8875_SYSR,0x0C);//65K
_colorIndex = 0;
}
_writeRegister(RA8875_HDWR,initStrings[_initIndex][3]); //LCD Horizontal Display Width Register
_writeRegister(RA8875_HNDFTR,initStrings[_initIndex][4]); //Horizontal Non-Display Period Fine Tuning Option Register
_writeRegister(RA8875_HNDR,initStrings[_initIndex][5]); //LCD Horizontal Non-Display Period Register
_writeRegister(RA8875_HSTR,initStrings[_initIndex][6]); //HSYNC Start Position Register
_writeRegister(RA8875_HPWR,initStrings[_initIndex][7]); //HSYNC Pulse Width Register
_writeRegister(RA8875_VDHR0,initStrings[_initIndex][8]); //LCD Vertical Display Height Register0
_writeRegister(RA8875_VDHR0+1,initStrings[_initIndex][9]); //LCD Vertical Display Height Register1
_writeRegister(RA8875_VNDR0,initStrings[_initIndex][10]); //LCD Vertical Non-Display Period Register 0
_writeRegister(RA8875_VNDR0+1,initStrings[_initIndex][11]); //LCD Vertical Non-Display Period Register 1
_writeRegister(RA8875_VSTR0,initStrings[_initIndex][12]); //VSYNC Start Position Register 0
_writeRegister(RA8875_VSTR0+1,initStrings[_initIndex][13]); //VSYNC Start Position Register 1
_writeRegister(RA8875_VPWR,initStrings[_initIndex][14]); //VSYNC Pulse Width Register
_updateActiveWindow(true); //set the whole screen as active
//clearActiveWindow();
delay(10); //100
setCursorBlinkRate(DEFAULTCURSORBLINKRATE);//set default blink rate
setIntFontCoding(DEFAULTINTENCODING);//set default internal font encoding
setFont(INT); //set internal font use
//postburner PLL!
_setSysClock(sysClockPar[_initIndex][0],sysClockPar[_initIndex][1],initStrings[_initIndex][2]);
_inited = true;//from here we will go at high speed!
#if defined(_FASTCPU)
_slowDownSPI(false);
#else
#if defined(SPI_HAS_TRANSACTION)
#if defined(__MKL26Z64__)
if (_altSPI){
_SPImaxSpeed = MAXSPISPEED2;
} else {
_SPImaxSpeed = MAXSPISPEED;
}
#else
_SPImaxSpeed = MAXSPISPEED;
#endif
#else
#if defined(___DUESTUFF) && defined(SPI_DUE_MODE_EXTENDED)
SPI.setClockDivider(_cs,SPI_SPEED_WRITE);
#else
SPI.setClockDivider(SPI_SPEED_WRITE);
#endif
#endif
#endif
delay(1);
clearMemory();//clearMemory(true);
delay(1);
displayOn(true);//turn On Display
delay(1);
fillWindow(_RA8875_DEFAULTBACKLIGHT);//set screen black
backlight(true);
setRotation(_RA8875_DEFAULTSCRROT);
_setTextMode(false);
setActiveWindow();
#if defined(_RA8875_DEFAULTTXTBKGRND)
#if defined(USE_RA8875_SEPARATE_TEXT_COLOR)
setForegroundColor(_TXTForeColor);
setBackgroundColor(_TXTBackColor);
#else
setForegroundColor(_RA8875_DEFAULTTXTFRGRND);
setBackgroundColor(_RA8875_DEFAULTTXTBKGRND);
#endif
_backTransparent = false;
_FNCR1_Reg &= ~(1 << 6);//clear
#else
#if defined(USE_RA8875_SEPARATE_TEXT_COLOR)
setForegroundColor(_TXTForeColor);
#else
setForegroundColor(_RA8875_DEFAULTTXTFRGRND);
#endif
_backTransparent = true;
_FNCR1_Reg |= (1 << 6);//set
#endif
_writeRegister(RA8875_FNCR1,_FNCR1_Reg);
setCursor(0,0);
}
/**************************************************************************/
/*!
This function set the sysClock accordly datasheet
Parameters:
pll1: PLL Control Register 1
pll2: PLL Control Register 2
pixclk: Pixel Clock Setting Register
[private]
*/
/**************************************************************************/
void RA8875::_setSysClock(uint8_t pll1,uint8_t pll2,uint8_t pixclk)
{
_writeRegister(RA8875_PLLC1,pll1);////PLL Control Register 1
delay(1);
_writeRegister(RA8875_PLLC1+1,pll2);////PLL Control Register 2
delay(1);
_writeRegister(RA8875_PCSR,pixclk);//Pixel Clock Setting Register
delay(1);
}
/**************************************************************************/
/*!
This return a byte with the error code/s:
bit--------------------------------------------------------------------
0: The display it's not supported!
1: The MOSI or MISO or SCLK choosed for Teensy it's out permitted range!
2: The CS pin you selected it's out permitted range!
3: You have to upgrade to Teensyduino 1.22 or better to use this feature!
4:
5:
6:
7:
0b00000000 = no error
*/
/**************************************************************************/
uint8_t RA8875::errorCode(void)
{
return _errorCode;
}
/**************************************************************************/
/*!
return true when register has done the job, otherwise false.
*/
/**************************************************************************/
boolean RA8875::_waitPoll(uint8_t regname, uint8_t waitflag)
{
uint8_t temp;
unsigned long timeout = millis();
while (1) {
temp = _readRegister(regname);
if (!(temp & waitflag)) return true;
if ((millis() - timeout) > 20) return false;//emergency exit!
}
return false;
}
/**************************************************************************/
/*!
Just another specified wait routine until job it's done
Parameters:
res:
0x80(for most operations),
0x40(BTE wait),
0x01(DMA wait)
*/
/**************************************************************************/
void RA8875::_waitBusy(uint8_t res)
{
uint8_t temp;
unsigned long start = millis();//M.Sandercock
do {
if (res == 0x01) writeCommand(RA8875_DMACR);//dma
temp = readStatus();
if ((millis() - start) > 10) return;
} while ((temp & res) == res);
}
/**************************************************************************/
/*!
Clear memory (different from fillWindow!)
Parameters:
full: true(clear all memory), false(clear active window only)
When in multilayer it automatically clear L1 & L1 and switch back to current layer
*/
/**************************************************************************/
/*
void RA8875::clearMemory(boolean full)
{
uint8_t temp = 0b10000000;
if (!full && !_useMultiLayers) temp |= (1 << 6);//set 6
_writeRegister(RA8875_MCLR,temp);
_waitBusy(0x80);
if (full && _useMultiLayers){
temp = 0b10000000;
uint8_t templ = _currentLayer;//remember current layer
if (templ > 0){//we are in L2
writeTo(L1);//switch to L1
} else {
writeTo(L2);//switch to L2
}
_writeRegister(RA8875_MCLR,temp);
_waitBusy(0x80);
if (templ > 0){//we was in L2
writeTo(L2);//switch back to L2
} else {
writeTo(L1);//switch back to L1
}
}
}
*/
/**************************************************************************/
/*!
Clear memory (different from fillWindow!)
Parameters:
stop: stop clear memory operation
*/
/**************************************************************************/
void RA8875::clearMemory(bool stop)
{
uint8_t temp;
temp = _readRegister(RA8875_MCLR);
stop == true ? temp &= ~(1 << 7) : temp |= (1 << 7);
_writeData(temp);
if (!stop) _waitBusy(0x80);
}
/**************************************************************************/
/*!
Clear the active window
Parameters:
full: false(clear current window), true clear full window
*/
/**************************************************************************/
void RA8875::clearActiveWindow(bool full)
{
uint8_t temp;
temp = _readRegister(RA8875_MCLR);
full == true ? temp &= ~(1 << 6) : temp |= (1 << 6);
_writeData(temp);
//_waitBusy(0x80);
}
/**************************************************************************/
/*!
Clear width BG color
Parameters:
bte: false(clear width BTE BG color), true(clear width font BG color)
*/
/**************************************************************************/
void RA8875::clearWidthColor(bool bte)
{
uint8_t temp;
temp = _readRegister(RA8875_MCLR);
bte == true ? temp &= ~(1 << 0) : temp |= (1 << 0);
_writeData(temp);
//_waitBusy(0x80);
}
/**************************************************************************/
/*!
turn display on/off
*/
/**************************************************************************/
void RA8875::displayOn(boolean on)
{
on == true ? _writeRegister(RA8875_PWRR, RA8875_PWRR_NORMAL | RA8875_PWRR_DISPON) : _writeRegister(RA8875_PWRR, RA8875_PWRR_NORMAL | RA8875_PWRR_DISPOFF);
}
/**************************************************************************/
/*!
Set the Active Window
Parameters:
XL: Horizontal Left
XR: Horizontal Right
YT: Vertical TOP
YB: Vertical Bottom
*/
/**************************************************************************/
void RA8875::setActiveWindow(int16_t XL,int16_t XR ,int16_t YT ,int16_t YB)
{
if (_portrait){ swapvals(XL,YT); swapvals(XR,YB);}
if (XR >= RA8875_WIDTH) XR = RA8875_WIDTH;
if (YB >= RA8875_HEIGHT) YB = RA8875_HEIGHT;
_activeWindowXL = XL; _activeWindowXR = XR;
_activeWindowYT = YT; _activeWindowYB = YB;
_updateActiveWindow(false);
}
/**************************************************************************/
/*!
Set the Active Window as FULL SCREEN
*/
/**************************************************************************/
void RA8875::setActiveWindow(void)
{
_activeWindowXL = 0; _activeWindowXR = RA8875_WIDTH;
_activeWindowYT = 0; _activeWindowYB = RA8875_HEIGHT;
if (_portrait){swapvals(_activeWindowXL,_activeWindowYT); swapvals(_activeWindowXR,_activeWindowYB);}
_updateActiveWindow(true);
}
/**************************************************************************/
/*!
Set the Active Window
Parameters:
XL: Horizontal Left
XR: Horizontal Right
YT: Vertical TOP
YB: Vertical Bottom
*/
/**************************************************************************/
void RA8875::getActiveWindow(int16_t &XL,int16_t &XR ,int16_t &YT ,int16_t &YB)//0.69b24
{
XL = _activeWindowXL; XR = _activeWindowXR;
YT = _activeWindowYT; YB = _activeWindowYB;
}
/**************************************************************************/
/*!
Return the max tft width.
Parameters:
absolute: if true will return the phisical width
*/
/**************************************************************************/
uint16_t RA8875::width(bool absolute) const
{
if (absolute) return RA8875_WIDTH;
return _width;
}
/**************************************************************************/
/*!
Return the max tft height.
Parameters:
absolute: if true will return the phisical height
*/
/**************************************************************************/
uint16_t RA8875::height(bool absolute) const
{
if (absolute) return RA8875_HEIGHT;
return _height;
}
/**************************************************************************/
/*!
Change the mode between graphic and text
Parameters:
m: can be GRAPHIC or TEXT
[private]
*/
/**************************************************************************/
void RA8875::_setTextMode(bool m)
{
if (m == _textMode) return;
writeCommand(RA8875_MWCR0);
//if (m != 0){//text
if (m){//text
_MWCR0_Reg |= (1 << 7);
_textMode = true;
} else {//graph
_MWCR0_Reg &= ~(1 << 7);
_textMode = false;
}
_writeData(_MWCR0_Reg);
}
/**************************************************************************/
/*!
Change the beam scan direction on display
Parameters:
invertH: true(inverted),false(normal) horizontal
invertV: true(inverted),false(normal) vertical
*/
/**************************************************************************/
void RA8875::_scanDirection(boolean invertH,boolean invertV)
{
invertH == true ? _DPCR_Reg |= (1 << 3) : _DPCR_Reg &= ~(1 << 3);
invertV == true ? _DPCR_Reg |= (1 << 2) : _DPCR_Reg &= ~(1 << 2);
_writeRegister(RA8875_DPCR,_DPCR_Reg);
}
/**************************************************************************/
/*!
Change the rotation of the screen
Parameters:
rotation:
0 = default
1 = 90
2 = 180
3 = 270
*/
/**************************************************************************/
void RA8875::setRotation(uint8_t rotation)//0.69b32 - less code
{
_rotation = rotation % 4; //limit to the range 0-3
switch (_rotation) {
case 0:
//default, connector to bottom
_portrait = false;
_scanDirection(0,0);
#if defined(USE_RA8875_TOUCH)
if (!_calibrated) {
_tsAdcMinX = 0; _tsAdcMinY = 0; _tsAdcMaxX = 1023; _tsAdcMaxY = 1023;
} else {
_tsAdcMinX = TOUCSRCAL_XLOW; _tsAdcMinY = TOUCSRCAL_YLOW; _tsAdcMaxX = TOUCSRCAL_XHIGH; _tsAdcMaxY = TOUCSRCAL_YHIGH;
}
#endif
break;
case 1:
//90
_portrait = true;
_scanDirection(1,0);
#if defined(USE_RA8875_TOUCH)
if (!_calibrated) {
_tsAdcMinX = 1023; _tsAdcMinY = 0; _tsAdcMaxX = 0; _tsAdcMaxY = 1023;
} else {
_tsAdcMinX = TOUCSRCAL_XHIGH; _tsAdcMinY = TOUCSRCAL_YLOW; _tsAdcMaxX = TOUCSRCAL_XLOW; _tsAdcMaxY = TOUCSRCAL_YHIGH;
}
#endif
break;
case 2:
//180
_portrait = false;
_scanDirection(1,1);
#if defined(USE_RA8875_TOUCH)
if (!_calibrated) {
_tsAdcMinX = 1023; _tsAdcMinY = 1023; _tsAdcMaxX = 0; _tsAdcMaxY = 0;
} else {
_tsAdcMinX = TOUCSRCAL_XHIGH; _tsAdcMinY = TOUCSRCAL_YHIGH; _tsAdcMaxX = TOUCSRCAL_XLOW; _tsAdcMaxY = TOUCSRCAL_YLOW;
}
#endif
break;
case 3:
//270
_portrait = true;
_scanDirection(0,1);
#if defined(USE_RA8875_TOUCH)
if (!_calibrated) {
_tsAdcMinX = 0; _tsAdcMinY = 1023; _tsAdcMaxX = 1023; _tsAdcMaxY = 0;
} else {
_tsAdcMinX = TOUCSRCAL_XLOW; _tsAdcMinY = TOUCSRCAL_YHIGH; _tsAdcMaxX = TOUCSRCAL_XHIGH; _tsAdcMaxY = TOUCSRCAL_YLOW;
}
#endif
break;
}
if (_portrait){
_width = RA8875_HEIGHT;
_height = RA8875_WIDTH;
_FNCR1_Reg |= (1 << 4);
} else {
_width = RA8875_WIDTH;
_height = RA8875_HEIGHT;
_FNCR1_Reg &= ~(1 << 4);
}
_writeRegister(RA8875_FNCR1,_FNCR1_Reg);//0.69b21
setActiveWindow();
}
/**************************************************************************/
/*!
Get rotation setting
*/
/**************************************************************************/
uint8_t RA8875::getRotation()
{
return _rotation;
}
/**************************************************************************/
/*!
true if rotation 1 or 3
*/
/**************************************************************************/
boolean RA8875::isPortrait(void)
{
return _portrait;
}