-
Notifications
You must be signed in to change notification settings - Fork 6
/
ILI9481.c
1672 lines (1302 loc) · 42.6 KB
/
ILI9481.c
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
/* ************************************************************************
*
* driver functions for ILI9481 compatible color graphic displays
* - 320 x 480 pixels
* - interfaces
* - 8 bit parallel
* - 9 bit parallel (not supported)
* - 16 bit parallel (untested)
* - 18 bit parallel (not supported)
* - 3 line SPI (not supported, would be insanely slow)
* - 4 line SPI (untested, hardware SPI strongly recommended)
*
* (c) 2020-2024 by Markus Reschke
*
* ************************************************************************ */
/*
* hints:
* - pin assignment for 8 bit parallel interface
* LCD_PORT/LCD_DDR:
* /RESX LCD_RES (optional)
* /CSX LCD_CS (optional)
* D/CX LCD_DC
* WRX LCD_WR
* RDX LCD_RD (optional)
* LCD_PORT2/LCD_DDR2/LCD_PIN2:
* DB0 LCD_DB0 (LCD_PORT2 pin #0)
* DB1 LCD_DB1 (LCD_PORT2 pin #1)
* DB2 LCD_DB2 (LCD_PORT2 pin #2)
* DB3 LCD_DB3 (LCD_PORT2 pin #3)
* DB4 LCD_DB4 (LCD_PORT2 pin #4)
* DB5 LCD_DB5 (LCD_PORT2 pin #5)
* DB6 LCD_DB6 (LCD_PORT2 pin #6)
* DB7 LCD_DB7 (LCD_PORT2 pin #7)
* - pin assignment for 16 bit parallel interface
* same as 8 bit parallel but additionally
* LCD_PORT3/LCD_DDR3/LCD_PIN3:
* DB8 LCD_DB8 (LCD_PORT3 pin #0)
* DB9 LCD_DB9 (LCD_PORT3 pin #1)
* DB10 LCD_DB10 (LCD_PORT3 pin #2)
* DB11 LCD_DB11 (LCD_PORT3 pin #3)
* DB12 LCD_DB12 (LCD_PORT3 pin #4)
* DB13 LCD_DB13 (LCD_PORT3 pin #5)
* DB14 LCD_DB14 (LCD_PORT3 pin #6)
* DB15 LCD_DB15 (LCD_PORT3 pin #7)
* - max. clock rate for parallel bus: 10MHz write and 2.2MHz read
* - pin assignment for 4 line SPI
* /RESX Vcc or LCD_RES (optional)
* /CSX Gnd or LCD_CS (optional)
* D/CX LCD_DC
* SCL (WRX) LCD_SCL / SPI_SCK
* DIN/SDA LCD_SDA / SPI_MOSI
* DOUT LCD_SDO / SPI_MISO (not used yet)
* For hardware SPI LCD_SCL and LCD_SDA have to be the MCU's SCK and
* MOSI pins.
* - max. SPI clock: 10MHz write and 3.3MHz read
*/
/* local includes */
#include "config.h" /* global configuration */
#ifdef LCD_ILI9481
/*
* local constants
*/
/* source management */
#define LCD_DRIVER_C
/*
* include header files
*/
/* local includes */
#include "common.h" /* common header file */
#include "variables.h" /* global variables */
#include "functions.h" /* external functions */
#include "colors.h" /* color definitions */
#include "ILI9481.h" /* ILI9481 specifics */
/* fonts and symbols */
/* horizontally aligned, horizontal bit order flipped */
#include "font_8x8_hf.h"
#include "font_8x16_alt_hf.h"
#include "font_12x16_hf.h"
#include "font_16x26_hf.h"
#include "font_10x16_iso8859-2_hf.h"
#include "font_12x16_iso8859-2_hf.h"
#include "font_16x26_iso8859-2_hf.h"
#include "font_16x26_win1251_hf.h"
#include "symbols_24x24_hf.h"
#include "symbols_24x24_alt1_hf.h"
#include "symbols_24x24_alt2_hf.h"
#include "symbols_24x24_old_hf.h"
#include "symbols_32x32_hf.h"
#include "symbols_32x32_alt1_hf.h"
#include "symbols_32x32_alt2_hf.h"
#include "symbols_32x32_old_hf.h"
#include "symbols_32x39_hf.h"
/* sanity check */
#ifndef FONT_SET
#error <<< No font selected! >>>
#endif
#ifdef SW_SYMBOLS
#ifndef SYMBOL_SET
#error <<< No symbols selected! >>>
#endif
#endif
/*
* derived constants
*/
/* maximum number of pixels for X and Y direction */
#ifdef LCD_ROTATE
#define LCD_PIXELS_X LCD_DOTS_Y
#define LCD_PIXELS_Y LCD_DOTS_X
#else
#define LCD_PIXELS_X LCD_DOTS_X
#define LCD_PIXELS_Y LCD_DOTS_Y
#endif
/* number of lines and characters per line */
#define LCD_CHAR_X (LCD_PIXELS_X / FONT_SIZE_X)
#define LCD_CHAR_Y (LCD_PIXELS_Y / FONT_SIZE_Y)
/* component symbols */
#ifdef SW_SYMBOLS
/* resize symbols by a factor of 2 */
#define SYMBOL_RESIZE 2
/* size in relation to a character */
#define LCD_SYMBOL_CHAR_X (((SYMBOL_SIZE_X * SYMBOL_RESIZE) + FONT_SIZE_X - 1) / FONT_SIZE_X)
#define LCD_SYMBOL_CHAR_Y (((SYMBOL_SIZE_Y * SYMBOL_RESIZE) + FONT_SIZE_Y - 1) / FONT_SIZE_Y)
/* check y size: we need at least 2 lines */
#if LCD_SYMBOL_CHAR_Y < 2
#error <<< Symbols too small! >>>
#endif
#endif
/* color modes */
#ifdef LCD_SPI
/* SPI interface: use RGB666 (RGB565 not supported by SPI) */
#define COLORMODE_RGB666
#else
/* parallel interface: use RGB565 */
#define COLORMODE_RGB565
#endif
/*
* local variables
*/
/* address window */
uint16_t X_Start; /* start position X (column) */
uint16_t X_End; /* end position X (column) */
uint16_t Y_Start; /* start position Y (page/row) */
uint16_t Y_End; /* end position Y (page/row) */
/* text line management */
uint16_t LineFlags; /* bitfield for up to 16 lines */
#ifdef COLORMODE_RGB666
/* colors in RGB666 8-bit frame format */
uint8_t RGB666_FG[3]; /* foreground/pen color */
uint8_t RGB666_BG[3]; /* background color */
#endif
/* ************************************************************************
* low level functions for 4 line SPI interface
* ************************************************************************ */
/*
* protocol:
* - write: CSX low -> D/CX -> D7-0 with rising edge of SCL
* - D/CX: high = data / low = command
*
* hints:
* - RGB111 and RGB666 are supported when using 4-line SPI
*/
#if defined (LCD_SPI) && ! defined (SPI_9)
/*
* set up interface bus
* - should be called at firmware startup
*/
void LCD_BusSetup(void)
{
uint8_t Bits; /* register bits */
/*
* set control signals
*/
Bits = LCD_DDR; /* get current directions */
/* basic output pins */
Bits |= (1 << LCD_DC); /* D/CX */
/* optional output pins */
#ifdef LCD_RES
Bits |= (1 << LCD_RES); /* /RESX */
#endif
#ifdef LCD_CS
Bits |= (1 << LCD_CS); /* /CSX */
#endif
LCD_DDR = Bits; /* set new directions */
/* set default levels */
#ifdef LCD_CS
/* disable chip */
LCD_PORT |= (1 << LCD_CS); /* set /CSX high */
#endif
#ifdef LCD_RES
/* disable reset */
LCD_PORT |= (1 << LCD_RES); /* set /RESX high */
#endif
/*
* init SPI bus
* - SPI bus is set up already in main()
*/
#ifdef SPI_HARDWARE
/*
* set SPI clock rate (10MHz worst case)
* - max. MCU clock 20MHz / 2 = 10MHz
* - f_osc/2 (SPR1 = 0, SPR0 = 0, SPI2X = 1)
*/
SPI.ClockRate = SPI_CLOCK_2X; /* set clock rate flags */
SPI_Clock(); /* update SPI clock */
#endif
}
/*
* send a command to the LCD
*
* requires:
* - Cmd: byte value to send
*/
void LCD_Cmd(uint8_t Cmd)
{
/* indicate command mode */
LCD_PORT &= ~(1 << LCD_DC); /* set D/CX low */
#ifdef LCD_CS
/* select chip */
LCD_PORT &= ~(1 << LCD_CS); /* set /CSX low */
#endif
/* send command */
SPI_Write_Byte(Cmd); /* write command byte */
#ifdef LCD_CS
/* deselect chip */
LCD_PORT |= (1 << LCD_CS); /* set /CSX high */
#endif
}
/*
* send data to the LCD
*
* requires:
* - Data: byte value to send
*/
void LCD_Data(uint8_t Data)
{
/* indicate data mode */
LCD_PORT |= (1 << LCD_DC); /* set D/CX high */
#ifdef LCD_CS
/* select chip */
LCD_PORT &= ~(1 << LCD_CS); /* set /CSX low */
#endif
/* send data */
SPI_Write_Byte(Data); /* write data byte */
#ifdef LCD_CS
/* deselect chip */
LCD_PORT |= (1 << LCD_CS); /* set /CSX high */
#endif
}
/*
* send data to the LCD
*
* requires:
* - Data: 2-byte value to send
*/
void LCD_Data2(uint16_t Data)
{
uint8_t Byte; /* data byte */
/* indicate data mode */
LCD_PORT |= (1 << LCD_DC); /* set D/CX high */
#ifdef LCD_CS
/* select chip */
LCD_PORT &= ~(1 << LCD_CS); /* set /CSX low */
#endif
/* send data: 2 bytes */
Byte = (uint8_t)Data; /* save LSB */
Data >>= 8; /* get MSB */
SPI_Write_Byte((uint8_t)Data); /* write MSB */
SPI_Write_Byte(Byte); /* write LSB */
#ifdef LCD_CS
/* deselect chip */
LCD_PORT |= (1 << LCD_CS); /* set /CSX high */
#endif
}
#endif
/* ************************************************************************
* low level functions for 8 bit parallel interface
* - LCD_PORT (LCD_DDR) for control signals
* - LCD_PORT2 (LCD_DDR2/LCD_PIN2) for data signals 0-7
* ************************************************************************ */
/*
* protocol:
* - write: CSX low -> D/CX -> WRX low to init write -> set D7-0 ->
* rising edge of WRX triggers read
* - read: CSX low -> D/CX -> RDX low to trigger output ->
* read D7-0 with rising edge of RDX
* - D/CX: high = data / low = command
*
* hints:
* - RGB565 and RGB666 are supported when using the 8-bit parallel interface
*/
#ifdef LCD_PAR_8
/*
* set up interface bus
* - should be called at firmware startup
*/
void LCD_BusSetup(void)
{
uint8_t Bits; /* register bits */
/*
* set data signals
* - LCD_PORT2
*/
/* all data pins are in output mode by default */
LCD_DDR2 = 0b11111111; /* DB0-7 */
/*
* set control signals
* - LCD_PORT
*/
/* set directions */
Bits = LCD_DDR; /* get current directions */
/* basic output pins */
Bits |= (1 << LCD_DC) | (1 << LCD_WR); /* D/CX, WRX */
/* optional output pins */
#ifdef LCD_RD
Bits |= (1 << LCD_RD); /* RDX */
#endif
#ifdef LCD_RES
Bits |= (1 << LCD_RES); /* /RES */
#endif
#ifdef LCD_CS
Bits |= (1 << LCD_CS); /* /CS */
#endif
LCD_DDR = Bits; /* set new directions */
/* set default levels */
Bits = LCD_PORT; /* get current levels */
/* basic output pins */
Bits |= (1 << LCD_WR); /* set WRX high */
/* optional output pins */
#ifdef LCD_RD
Bits |= (1 << LCD_RD); /* set RDX high */
#endif
#ifdef LCD_CS
/* disable chip */
Bits |= (1 << LCD_CS); /* set /CSX high */
#endif
#ifdef LCD_RES
/* disable reset */
Bits |= (1 << LCD_RES); /* set /RESX high */
#endif
LCD_PORT = Bits; /* set new levels */
}
/*
* send a byte (data or command) to the LCD
*
* requires:
* - Byte: byte value to send
*/
void LCD_SendByte(uint8_t Byte)
{
/* set data signals */
LCD_PORT2 = Byte; /* DB0-7 */
/* create write strobe (rising edge takes data in) */
LCD_PORT &= ~(1 << LCD_WR); /* set WRX low */
/* wait 25ns */
LCD_PORT |= (1 << LCD_WR); /* set WRX high */
/* data hold time 25ns */
/* next write cycle after 30ns WRX being high */
}
/*
* send a command to the LCD
*
* requires:
* - Cmd: byte value to send
*/
void LCD_Cmd(uint8_t Cmd)
{
#ifdef LCD_CS
/* select chip */
LCD_PORT &= ~(1 << LCD_CS); /* set /CSX low */
#endif
/* indicate command mode */
LCD_PORT &= ~(1 << LCD_DC); /* set D/CX low */
/* send command */
LCD_SendByte(Cmd); /* send command byte */
#ifdef LCD_CS
/* deselect chip */
LCD_PORT |= (1 << LCD_CS); /* set /CSX high */
#endif
}
/*
* send data to the LCD
*
* requires:
* - Data: byte value to send
*/
void LCD_Data(uint8_t Data)
{
#ifdef LCD_CS
/* select chip */
LCD_PORT &= ~(1 << LCD_CS); /* set /CSX low */
#endif
/* indicate data mode */
LCD_PORT |= (1 << LCD_DC); /* set D/CX high */
/* send data */
LCD_SendByte(Data); /* send data byte */
#ifdef LCD_CS
/* deselect chip */
LCD_PORT |= (1 << LCD_CS); /* set /CSX high */
#endif
}
/*
* send data to the LCD
*
* requires:
* - Data: 2-byte value to send
*/
void LCD_Data2(uint16_t Data)
{
uint8_t Byte; /* data byte */
#ifdef LCD_CS
/* select chip */
LCD_PORT &= ~(1 << LCD_CS); /* set /CSX low */
#endif
/* indicate data mode */
LCD_PORT |= (1 << LCD_DC); /* set D/CX high */
/* send data */
Byte = (uint8_t)Data; /* save LSB */
Data >>= 8; /* get MSB */
LCD_SendByte((uint8_t)Data); /* send MSB */
LCD_SendByte(Byte); /* send LSB */
#ifdef LCD_CS
/* deselect chip */
LCD_PORT |= (1 << LCD_CS); /* set /CSX high */
#endif
}
#if 0
/*
* read byte from LCD
* - timing for register data
* - not suitable for frame memory
*
* returns:
* - byte
*/
uint8_t LCD_ReadByte(void)
{
uint8_t Byte; /* return value */
/* set data pins to input mode before reading */
LCD_DDR2 = 0b00000000; /* DB0-7 */
#ifdef LCD_CS
/* select chip */
LCD_PORT &= ~(1 << LCD_CD); /* set /CSX low */
#endif
/* indicate data mode */
LCD_PORT |= (1 << LCD_DC); /* set D/CX high */
/* start read cycle (RDX low for min. 170ns) */
LCD_PORT &= ~(1 << LCD_RD); /* set RDX low */
/* wait for LCD to fetch data: max. 340ns */
wait1us(); /* wait 1µs */
/* read data */
Byte = LCD_PIN2;
/* end read cycle */
LCD_PORT |= (1 << LCD_RD); /* set RDX high */
/* wait for LCD to release data lines: max. 250ns */
wait1us(); /* wait 1µs */
/* next read cycle after 90ns RDX being high */
#ifdef LCD_CS
/* deselect chip */
LCD_PORT |= (1 << LCD_CS); /* set /CSX high */
#endif
/* set data pins back to output mode after reading */
LCD_DDR2 = 0b11111111; /* DB0-7 */
return Byte;
}
#endif
#endif
/* ************************************************************************
* low level functions for 16 bit parallel interface
* - LCD_PORT (LCD_DDR) for control signals
* - LCD_PORT2 (LCD_DDR2/LCD_PIN2) for data signals 0-7
* - LCD_PORT3 (LCD_DDR3/LCD_PIN3) for data signals 8-15
* ************************************************************************ */
/*
* protocol:
* - write: CSX low -> D/CX -> WRX low to init write -> set D15-0 ->
* rising edge of WRX triggers read
* - D/CX: high = data / low = command
* - commands have to be sent as 2 bytes
*
* hints:
* - RGB565 and RGB666 are supported when using the 16-bit parallel interface
*/
#ifdef LCD_PAR_16
/*
* set up interface bus
* - should be called at firmware startup
*/
void LCD_BusSetup(void)
{
uint8_t Bits; /* register bits */
/*
* set data signals
* - LCD_PORT2
* - LCD_PORT3
*/
/* all data pins are in output mode by default */
LCD_DDR2 = 0b11111111; /* DB0-7 */
LCD_DDR3 = 0b11111111; /* DB8-15 */
/*
* set control signals
* - LCD_PORT
*/
/* set directions */
Bits = LCD_DDR; /* get current directions */
/* basic output pins */
Bits |= (1 << LCD_DC) | (1 << LCD_WR); /* D/CX, WRX */
/* optional output pins */
#ifdef LCD_RD
Bits |= (1 << LCD_RD); /* RDX */
#endif
#ifdef LCD_RES
Bits |= (1 << LCD_RES); /* /RES */
#endif
#ifdef LCD_CS
Bits |= (1 << LCD_CS); /* /CS */
#endif
LCD_DDR = Bits; /* set new directions */
/* set default levels */
Bits = LCD_PORT; /* get current levels */
/* basic output pins */
Bits |= (1 << LCD_WR); /* set WRX high */
/* optional output pins */
#ifdef LCD_RD
Bits |= (1 << LCD_RD); /* set RDX high */
#endif
#ifdef LCD_CS
/* disable chip */
Bits |= (1 << LCD_CS); /* set /CSX high */
#endif
#ifdef LCD_RES
/* disable reset */
Bits |= (1 << LCD_RES); /* set /RESX high */
#endif
LCD_PORT = Bits; /* set new levels */
}
/*
* send a byte (data or command) to the LCD
*
* requires:
* - Byte: byte value to send
*/
void LCD_SendByte(uint8_t Byte)
{
/* set data signals */
LCD_PORT2 = Byte; /* DB0-7 */
/* create write strobe (rising edge takes data in) */
LCD_PORT &= ~(1 << LCD_WR); /* set WRX low */
/* wait 25ns */
LCD_PORT |= (1 << LCD_WR); /* set WRX high */
/* data hold time 25ns */
/* next write cycle after 30ns WRX being high */
}
/*
* send a command to the LCD
*
* requires:
* - Cmd: byte value to send
*/
void LCD_Cmd(uint8_t Cmd)
{
#ifdef LCD_CS
/* select chip */
LCD_PORT &= ~(1 << LCD_CS); /* set /CSX low */
#endif
/* indicate command mode */
LCD_PORT &= ~(1 << LCD_DC); /* set D/CX low */
/* send command */
LCD_SendByte(Cmd); /* send command byte */
#ifdef LCD_CS
/* deselect chip */
LCD_PORT |= (1 << LCD_CS); /* set /CSX high */
#endif
}
/*
* send data to the LCD
*
* requires:
* - Data: byte value to send
*/
void LCD_Data(uint8_t Data)
{
#ifdef LCD_CS
/* select chip */
LCD_PORT &= ~(1 << LCD_CS); /* set /CSX low */
#endif
/* indicate data mode */
LCD_PORT |= (1 << LCD_DC); /* set D/CX high */
/* send data */
LCD_SendByte(Data); /* send data byte */
#ifdef LCD_CS
/* deselect chip */
LCD_PORT |= (1 << LCD_CS); /* set /CSX high */
#endif
}
/*
* send data to the LCD
*
* requires:
* - Data: 2-byte value to send
*/
void LCD_Data2(uint16_t Data)
{
#ifdef LCD_CS
/* select chip */
LCD_PORT &= ~(1 << LCD_CS); /* set /CSX low */
#endif
/* indicate data mode */
LCD_PORT |= (1 << LCD_DC); /* set D/CX high */
/* set data signals */
LCD_PORT2 = (uint8_t)Data; /* set LSB (DB0-7) */
Data >>= 8; /* get MSB */
LCD_PORT3 = (uint8_t)Data; /* set MSB (DB8-15) */
/* create write strobe (rising edge takes data in) */
LCD_PORT &= ~(1 << LCD_WR); /* set WRX low */
/* wait 15ns */
LCD_PORT |= (1 << LCD_WR); /* set WRX high */
/* data hold time 10ns */
/* next write cycle after 15ns WRX being high */
#ifdef LCD_CS
/* deselect chip */
LCD_PORT |= (1 << LCD_CS); /* set /CSX high */
#endif
}
#endif
/* ************************************************************************
* conversion functions
* ************************************************************************ */
#ifdef COLORMODE_RGB666
/*
* RGB565 to RGB666
* - DB[15-11] -> R[5-1], DB15 -> R0
* R[5-0] = (R[4-0] << 1) | (R4 >> 4)
* - DB[10-5] -> G[5-0]
* G[5-0] = G[5-0]
* - DB[4-0] -> B[5-1], DB4 -> B0
* B[5-0] = (B[4-0] << 1) | (B4 >> 4)
*/
/*
* 8-bit frame format for RGB666
* - first byte: R[5-0] -> D[7-2], D[1-0] are 0
* - second byte: G[5-0] -> D[7-2], D[1-0] are 0
* - third byte: B[5-0] -> D[7-2], D[1-0] are 0
*/
/*
* expand RGB565 value to RGB666 in 8-bit frame format
*
* requires:
* - Color: RGB565 color value
* - RGB666: pointer to array of uint8_t for R, G and B
*/
void RGB565_2_RGB666(uint16_t Color, uint8_t *RGB666)
{
uint8_t R6; /* red */
uint8_t G6; /* green */
uint8_t B6; /* blue */
/*
* blue
*/
/* get blue-5 and convert to blue-6 */
B6 = Color & 0b0000000000011111; /* get DB[4-0] */
B6 <<= 1; /* shift one step left */
if (B6 & 0b00100000) /* DB4/B4 set (now B5) */
{
B6 |= 0b00000001; /* set B0 (copy B4 to B0) */
}
/* convert to frame format */
B6 <<= 2; /* shift two steps left */
/*
* green
*/
/* get green-6 */
Color >>= 5; /* shift right for G */
G6 = Color & 0b0000000000111111; /* get DB[10-5] */
/* convert to frame format */
G6 <<= 2; /* shift two steps left */
/*
* red
*/
/* get red-5 and convert to red-6 */
Color >>= 6; /* shift right for R */
R6 = Color & 0b0000000000011111; /* get DB[15-11] */
R6 <<= 1; /* shift one step left */
if (R6 & 0b00100000) /* DB15/R4 set (now R5) */
{
R6 |= 0b00000001; /* set R0 (copy R4 to R0) */
}
/* convert to frame format */
R6 <<= 2; /* shift two steps left */
/* copy converted values */
*RGB666 = R6; /* red */
RGB666++; /* next byte */
*RGB666 = G6; /* green */
RGB666++; /* next byte */
*RGB666 = B6; /* blue */
}
#endif
/* ************************************************************************
* high level functions
* ************************************************************************ */
/*
* set address window
* - 0 up to (max - 1)
*/
void LCD_AddressWindow(void)
{
/* X -> column */
LCD_Cmd(CMD_COL_ADDR_SET);
LCD_Data2(X_Start); /* start column */
LCD_Data2(X_End); /* end column */
/* Y -> page/row */
LCD_Cmd(CMD_PAGE_ADDR_SET);
LCD_Data2(Y_Start); /* start page */
LCD_Data2(Y_End); /* end page */
}
/*
* set LCD character position
*
* requires:
* - x: horizontal position (1-)
* - y: vertical position (1-)
*/
void LCD_CharPos(uint8_t x, uint8_t y)
{
uint16_t Mask = 1;
/* update UI */
UI.CharPos_X = x;
UI.CharPos_Y = y;
y--; /* start at zero */
/* mark text line as used */
if (y < 16) /* prevent overflow */
{
Mask <<= y; /* shift to bit position for line */
LineFlags |= Mask; /* set bit for line */
}
/*
* calculate dot position
* - top left of character
*/
/* horizontal position (column) */
x--; /* columns start at 0 */
Mask = x; /* expand to 16 bit */
Mask *= FONT_SIZE_X; /* offset for character */
X_Start = Mask; /* update start position */
/* vertical position (page) */
Mask = y; /* expand to 16 bit */
Mask *= FONT_SIZE_Y; /* offset for character */
Y_Start = Mask; /* update start position */
}
/*
* clear one single character line
*
* requires:
* - Line: line number (1-)
* special case line 0: clear remaining space in current line
*/
void LCD_ClearLine(uint8_t Line)
{
uint16_t x; /* x position */
uint8_t y; /* y position */
uint8_t Pos = 1; /* character position */