forked from ttrftech/NanoVNA
-
Notifications
You must be signed in to change notification settings - Fork 54
/
ili9341.c
1770 lines (1657 loc) · 59.5 KB
/
ili9341.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
/*
* Copyright (c) 2019-2020, Dmitry (DiSlord) [email protected]
* Based on TAKAHASHI Tomohiro (TTRFTECH) [email protected]
* All rights reserved.
*
* This 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, or (at your option)
* any later version.
*
* The software 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 GNU Radio; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#include "ch.h"
#include "hal.h"
#include "nanovna.h"
#ifdef TINYSA4
#include "si4432.h"
#endif
#include "chprintf.h"
#include "spi.h"
// Pin macros for LCD
#ifdef TINYSA4
#define LCD_CS_LOW palClearPad(GPIO_LCD_CS_PORT, GPIO_LCD_CS)
#define LCD_CS_HIGH palSetPad(GPIO_LCD_CS_PORT, GPIO_LCD_CS)
#define LCD_RESET_ASSERT palClearPad(GPIO_LCD_RESET_PORT, GPIO_LCD_RESET)
#define LCD_RESET_NEGATE palSetPad(GPIO_LCD_RESET_PORT, GPIO_LCD_RESET)
#define LCD_DC_CMD palClearPad(GPIO_LCD_CD_PORT, GPIO_LCD_CD)
#define LCD_DC_DATA palSetPad(GPIO_LCD_CD_PORT, GPIO_LCD_CD)
#define SD_CS_LOW palClearLine(LINE_SD_CS)
#define SD_CS_HIGH palSetLine(LINE_SD_CS)
#else
#define LCD_CS_LOW palClearPad(GPIOB, GPIOB_LCD_CS)
#define LCD_CS_HIGH palSetPad(GPIOB, GPIOB_LCD_CS)
#define LCD_RESET_ASSERT palClearPad(GPIOA, GPIOA_LCD_RESET)
#define LCD_RESET_NEGATE palSetPad(GPIOA, GPIOA_LCD_RESET)
#define LCD_DC_CMD palClearPad(GPIOB, GPIOB_LCD_CD)
#define LCD_DC_DATA palSetPad(GPIOB, GPIOB_LCD_CD)
#endif
// LCD display SPI bus
#define LCD_SPI SPI1
// SD card spi bus
#define SD_SPI SPI1
// Custom display definition
#ifdef LCD_DRIVER_ILI9341
// Set SPI bus speed for LCD
#define LCD_SPI_SPEED SPI_BR_DIV2
// Read speed, need more slow, not define if need use some as Tx speed
//#define LCD_SPI_RX_SPEED SPI_BR_DIV4
// Allow enable DMA for read display data (can not stable on full speed, on less speed slower)
#define __USE_DISPLAY_DMA_RX__
#endif
#ifdef LCD_DRIVER_ST7796S
// Set SPI bus speed for LCD
#define LCD_SPI_SPEED SPI_BR_DIV2
// Read speed, need more slow, not define if need use some as Tx speed
#define LCD_SPI_RX_SPEED SPI_BR_DIV4
// Allow enable DMA for read display data
#define __USE_DISPLAY_DMA_RX__
#endif
// Define SD SPI speed on work
#define SD_SPI_SPEED SPI_BR_DIV2
// Define SD SPI speed on initialization (100-400kHz need)
#define SD_INIT_SPI_SPEED SPI_BR_DIV256
// Disable DMA rx on disabled DMA tx
#ifdef __USE_DISPLAY_DMA__
// DMA channels for used in LCD SPI bus
#define LCD_DMA_RX DMA1_Channel2 // DMA1 channel 2 use for SPI1 rx
#define LCD_DMA_TX DMA1_Channel3 // DMA1 channel 3 use for SPI1 tx
#else
#undef __USE_DISPLAY_DMA_RX__
#endif
pixel_t spi_buffer[SPI_BUFFER_SIZE];
// Default foreground & background colors
pixel_t foreground_color = 0;
pixel_t background_color = 0;
// Display width and height definition
#define ILI9341_WIDTH LCD_WIDTH
#define ILI9341_HEIGHT LCD_HEIGHT
// Display commands list
#define ILI9341_NOP 0x00
#define ILI9341_SOFTWARE_RESET 0x01
#define ILI9341_READ_IDENTIFICATION 0x04
#define ILI9341_READ_STATUS 0x09
#define ILI9341_READ_POWER_MODE 0x0A
#define ILI9341_READ_MADCTL 0x0B
#define ILI9341_READ_PIXEL_FORMAT 0x0C
#define ILI9341_READ_IMAGE_FORMAT 0x0D
#define ILI9341_READ_SIGNAL_MODE 0x0E
#define ILI9341_READ_SELF_DIAGNOSTIC 0x0F
#define ILI9341_SLEEP_IN 0x10
#define ILI9341_SLEEP_OUT 0x11
#define ILI9341_PARTIAL_MODE_ON 0x12
#define ILI9341_NORMAL_DISPLAY_MODE_ON 0x13
#define ILI9341_INVERSION_OFF 0x20
#define ILI9341_INVERSION_ON 0x21
#define ILI9341_GAMMA_SET 0x26
#define ILI9341_DISPLAY_OFF 0x28
#define ILI9341_DISPLAY_ON 0x29
#define ILI9341_COLUMN_ADDRESS_SET 0x2A
#define ILI9341_PAGE_ADDRESS_SET 0x2B
#define ILI9341_MEMORY_WRITE 0x2C
#define ILI9341_COLOR_SET 0x2D
#define ILI9341_MEMORY_READ 0x2E
#define ILI9341_PARTIAL_AREA 0x30
#define ILI9341_VERTICAL_SCROLLING_DEF 0x33
#define ILI9341_TEARING_LINE_OFF 0x34
#define ILI9341_TEARING_LINE_ON 0x35
#define ILI9341_MEMORY_ACCESS_CONTROL 0x36
#define ILI9341_VERTICAL_SCROLLING 0x37
#define ILI9341_IDLE_MODE_OFF 0x38
#define ILI9341_IDLE_MODE_ON 0x39
#define ILI9341_PIXEL_FORMAT_SET 0x3A
#define ILI9341_WRITE_MEMORY_CONTINUE 0x3C
#define ILI9341_READ_MEMORY_CONTINUE 0x3E
#define ILI9341_SET_TEAR_SCANLINE 0x44
#define ILI9341_GET_SCANLINE 0x45
#define ILI9341_WRITE_BRIGHTNESS 0x51
#define ILI9341_READ_BRIGHTNESS 0x52
#define ILI9341_WRITE_CTRL_DISPLAY 0x53
#define ILI9341_READ_CTRL_DISPLAY 0x54
#define ILI9341_WRITE_CA_BRIGHTNESS 0x55
#define ILI9341_READ_CA_BRIGHTNESS 0x56
#define ILI9341_WRITE_CA_MIN_BRIGHTNESS 0x5E
#define ILI9341_READ_CA_MIN_BRIGHTNESS 0x5F
#define ILI9341_READ_ID1 0xDA
#define ILI9341_READ_ID2 0xDB
#define ILI9341_READ_ID3 0xDC
#define ILI9341_RGB_INTERFACE_CONTROL 0xB0
#define ILI9341_FRAME_RATE_CONTROL_1 0xB1
#define ILI9341_FRAME_RATE_CONTROL_2 0xB2
#define ILI9341_FRAME_RATE_CONTROL_3 0xB3
#define ILI9341_DISPLAY_INVERSION_CONTROL 0xB4
#define ILI9341_BLANKING_PORCH_CONTROL 0xB5
#define ILI9341_DISPLAY_FUNCTION_CONTROL 0xB6
#define ILI9341_ENTRY_MODE_SET 0xB7
#define ILI9341_BACKLIGHT_CONTROL_1 0xB8
#define ILI9341_BACKLIGHT_CONTROL_2 0xB9
#define ILI9341_BACKLIGHT_CONTROL_3 0xBA
#define ILI9341_BACKLIGHT_CONTROL_4 0xBB
#define ILI9341_BACKLIGHT_CONTROL_5 0xBC
#define ILI9341_BACKLIGHT_CONTROL_7 0xBE
#define ILI9341_BACKLIGHT_CONTROL_8 0xBF
#define ILI9341_POWER_CONTROL_1 0xC0
#define ILI9341_POWER_CONTROL_2 0xC1
#define ILI9341_VCOM_CONTROL_1 0xC5
#define ILI9341_VCOM_CONTROL_2 0xC7
#define ILI9341_POWERA 0xCB
#define ILI9341_POWERB 0xCF
#define ILI9341_NV_MEMORY_WRITE 0xD0
#define ILI9341_NV_PROTECTION_KEY 0xD1
#define ILI9341_NV_STATUS_READ 0xD2
#define ILI9341_READ_ID4 0xD3
#define ILI9341_POSITIVE_GAMMA_CORRECTION 0xE0
#define ILI9341_NEGATIVE_GAMMA_CORRECTION 0xE1
#define ILI9341_DIGITAL_GAMMA_CONTROL_1 0xE2
#define ILI9341_DIGITAL_GAMMA_CONTROL_2 0xE3
#define ILI9341_DTCA 0xE8
#define ILI9341_DTCB 0xEA
#define ILI9341_POWER_SEQ 0xED
#define ILI9341_3GAMMA_EN 0xF2
#define ILI9341_INTERFACE_CONTROL 0xF6
#define ILI9341_PUMP_RATIO_CONTROL 0xF7
//
// ILI9341_MEMORY_ACCESS_CONTROL registers
//
#define ILI9341_MADCTL_MY 0x80
#define ILI9341_MADCTL_MX 0x40
#define ILI9341_MADCTL_MV 0x20
#define ILI9341_MADCTL_ML 0x10
#define ILI9341_MADCTL_BGR 0x08
#define ILI9341_MADCTL_MH 0x04
#define ILI9341_MADCTL_RGB 0x00
#define DISPLAY_ROTATION_270 (ILI9341_MADCTL_MX | ILI9341_MADCTL_BGR)
#define DISPLAY_ROTATION_90 (ILI9341_MADCTL_MY | ILI9341_MADCTL_BGR)
#define DISPLAY_ROTATION_0 (ILI9341_MADCTL_MV | ILI9341_MADCTL_BGR)
#define DISPLAY_ROTATION_180 (ILI9341_MADCTL_MX | ILI9341_MADCTL_MY \
| ILI9341_MADCTL_MV | ILI9341_MADCTL_BGR)
//*****************************************************
// SPI bus functions, data
//*****************************************************
// SPI transmit byte to SPI (no wait complete transmit)
void spi_TxByte(uint8_t data) {
SPI_WRITE_8BIT(LCD_SPI, data);
}
// Transmit word to SPI bus (if SPI in 8 bit mode LSB send first!!!!!)
void spi_TxWord(uint16_t data) {
SPI_WRITE_16BIT(LCD_SPI, data);
}
// Transmit buffer to SPI bus (len should be > 0)
void spi_TxBuffer(const uint8_t *buffer, uint16_t len) {
while(len--) {
while (SPI_TX_IS_NOT_EMPTY(LCD_SPI));
SPI_WRITE_8BIT(LCD_SPI, *buffer++);
}
}
// Receive byte from SPI bus
uint8_t spi_RxByte(void) {
// Start RX clock (by sending data)
SPI_WRITE_8BIT(LCD_SPI, 0xFF);
while (SPI_RX_IS_EMPTY(LCD_SPI)||SPI_IS_BUSY(LCD_SPI));
return SPI_READ_8BIT(LCD_SPI);
}
// Receive buffer from SPI bus (len should be > 0)
void spi_RxBuffer(uint8_t *buffer, uint16_t len) {
do{
SPI_WRITE_8BIT(LCD_SPI, 0xFF);
while (SPI_RX_IS_EMPTY(LCD_SPI));
*buffer++ = SPI_READ_8BIT(LCD_SPI);
}while(--len);
}
void spi_DropRx(void) {
// Drop Rx buffer after tx and wait tx complete
while (SPI_RX_IS_NOT_EMPTY(LCD_SPI)||SPI_IS_BUSY(LCD_SPI))
(void)SPI_READ_8BIT(LCD_SPI);
(void)SPI_READ_8BIT(LCD_SPI);
}
//*****************************************************
// SPI DMA settings and data
//*****************************************************
#ifdef __USE_DISPLAY_DMA__
static const uint32_t txdmamode = 0
| STM32_DMA_CR_PL(STM32_SPI_SPI1_DMA_PRIORITY) // Set priority
| STM32_DMA_CR_DIR_M2P; // Memory to Spi
static const uint32_t rxdmamode = 0
| STM32_DMA_CR_PL(STM32_SPI_SPI1_DMA_PRIORITY) // Set priority
| STM32_DMA_CR_DIR_P2M; // SPI to Memory
// SPI transmit byte buffer use DMA (65535 bytes limit)
static inline void spi_DMATxBuffer(const uint8_t *buffer, uint16_t len, bool wait) {
dmaChannelSetMemory(LCD_DMA_TX, buffer);
dmaChannelSetTransactionSize(LCD_DMA_TX, len);
dmaChannelSetMode(LCD_DMA_TX, txdmamode | STM32_DMA_CR_BYTE | STM32_DMA_CR_MINC | STM32_DMA_CR_EN);
if (wait)
dmaChannelWaitCompletion(LCD_DMA_TX);
}
// Wait DMA Rx completion
static void dmaChannelWaitCompletionRxTx(void){
dmaChannelWaitCompletion(LCD_DMA_TX);
dmaChannelWaitCompletion(LCD_DMA_RX);
while (SPI_IS_BUSY(LCD_SPI)); // Wait SPI tx/rx
}
// SPI receive byte buffer use DMA
static const uint16_t dummy_tx = 0xFFFF;
static inline void spi_DMARxBuffer(uint8_t *buffer, uint16_t len, bool wait) {
// Init Rx DMA buffer, size, mode (spi and mem data size is 8 bit), and start
dmaChannelSetMemory(LCD_DMA_RX, buffer);
dmaChannelSetTransactionSize(LCD_DMA_RX, len);
dmaChannelSetMode(LCD_DMA_RX, rxdmamode | STM32_DMA_CR_BYTE | STM32_DMA_CR_MINC | STM32_DMA_CR_EN);
// Init dummy Tx DMA (for rx clock), size, mode (spi and mem data size is 8 bit), and start
dmaChannelSetMemory(LCD_DMA_TX, &dummy_tx);
dmaChannelSetTransactionSize(LCD_DMA_TX, len);
dmaChannelSetMode(LCD_DMA_TX, txdmamode | STM32_DMA_CR_BYTE | STM32_DMA_CR_EN);
if (wait)
dmaChannelWaitCompletionRxTx();
}
#endif // __USE_DISPLAY_DMA__
void spi_init(void)
{
rccEnableSPI1(FALSE);
LCD_SPI->CR1 = 0;
LCD_SPI->CR1 = SPI_CR1_MSTR // SPI is MASTER
| SPI_CR1_SSM // Software slave management (The external NSS pin is free for other application uses)
| SPI_CR1_SSI // Internal slave select (This bit has an effect only when the SSM bit is set. Allow use NSS pin as I/O)
| LCD_SPI_SPEED // Baud rate control
// | SPI_CR1_CPHA // Clock Phase
// | SPI_CR1_CPOL // Clock Polarity
;
LCD_SPI->CR2 = SPI_CR2_8BIT // SPI data size, set to 8 bit
| SPI_CR2_FRXTH // SPI_SR_RXNE generated every 8 bit data
// | SPI_CR2_SSOE //
#ifdef __USE_DISPLAY_DMA__
| SPI_CR2_TXDMAEN // Tx DMA enable
#ifdef __USE_DISPLAY_DMA_RX__
| SPI_CR2_RXDMAEN // Rx DMA enable
#endif
#endif
;
// Init SPI DMA Peripheral
#ifdef __USE_DISPLAY_DMA__
// Enable DMA, need for spi
rccEnableDMA1(false);
// rccEnableDMA2(false);
dmaChannelSetPeripheral(LCD_DMA_TX, &LCD_SPI->DR); // DMA Peripheral Tx
#ifdef __USE_DISPLAY_DMA_RX__
dmaChannelSetPeripheral(LCD_DMA_RX, &LCD_SPI->DR); // DMA Peripheral Rx
#endif
#endif
// Enable DMA on SPI
LCD_SPI->CR1|= SPI_CR1_SPE; //SPI enable
}
#ifdef TINYSA4
static uint16_t current_spi_mode;
void set_SPI_mode(uint16_t mode){
if (current_spi_mode == mode) return;
dmaChannelWaitCompletionRxTx();
// Disable current mode
switch(current_spi_mode){
case SPI_MODE_LCD:
LCD_CS_HIGH;
break;
case SPI_MODE_SD_CARD:
case SPI_MODE_SD_CARD_LOW:
SD_CS_HIGH;
break;
case SPI_MODE_SI:
stop_SI4432_SPI_mode();
break;
case SPI_MODE_PE:
stop_PE4312_SPI_mode();
break;
}
// Enable new mode
switch(mode){
case SPI_MODE_LCD:
SPI_BR_SET(LCD_SPI, LCD_SPI_SPEED); // Set Baud rate for LCD
break;
case SPI_MODE_SD_CARD:
SPI_BR_SET(LCD_SPI, SD_SPI_SPEED); // Set Baud rate for SD work
break;
case SPI_MODE_SD_CARD_LOW:
SPI_BR_SET(LCD_SPI, SD_INIT_SPI_SPEED); // Set Baud rate for SD init
break;
case SPI_MODE_SI:
start_SI4432_SPI_mode();
break;
case SPI_MODE_PE:
start_PE4312_SPI_mode();
break;
}
current_spi_mode = mode;
}
#endif
// Disable inline for this function
static void send_command(uint8_t cmd, uint8_t len, const uint8_t *data)
{
#ifdef TINYSA4
set_SPI_mode(SPI_MODE_LCD);
#endif
while (SPI_IS_BUSY(LCD_SPI));
LCD_CS_LOW;
LCD_DC_CMD;
SPI_WRITE_8BIT(LCD_SPI, cmd);
// Need wait transfer complete and set data bit
while (SPI_IS_BUSY(LCD_SPI));
LCD_DC_DATA;
spi_TxBuffer(data, len);
// while (SPI_IN_TX_RX(LCD_SPI));
//LCD_CS_HIGH;
}
#ifdef LCD_DRIVER_ST7796S
static const uint8_t ST7796S_init_seq[] = {
// SW reset
ILI9341_SOFTWARE_RESET, 0,
// display off
ILI9341_DISPLAY_OFF, 0,
// Interface Mode Control
ILI9341_RGB_INTERFACE_CONTROL, 1, 0x00,
// Frame Rate
ILI9341_FRAME_RATE_CONTROL_1, 1, 0xA,
// Display Inversion Control , 2 Dot
ILI9341_DISPLAY_INVERSION_CONTROL, 1, 0x02,
// RGB/MCU Interface Control
ILI9341_DISPLAY_FUNCTION_CONTROL, 3, 0x02, 0x02, 0x3B,
// EntryMode
ILI9341_ENTRY_MODE_SET, 1, 0xC6,
// Power Control 1
ILI9341_POWER_CONTROL_1, 2, 0x17, 0x15,
// Power Control 2
ILI9341_POWER_CONTROL_2, 1, 0x41,
// VCOM Control
//ILI9341_VCOM_CONTROL_1, 3, 0x00, 0x4D, 0x90,
ILI9341_VCOM_CONTROL_1, 3, 0x00, 0x12, 0x80,
// Memory Access
ILI9341_MEMORY_ACCESS_CONTROL, 1, 0x28, // landscape, BGR
// ILI9341_MEMORY_ACCESS_CONTROL, 1, 0x20, // landscape, RGB
// Interface Pixel Format, 16bpp DPI and DBI and
ILI9341_PIXEL_FORMAT_SET, 1, 0x55,
// P-Gamma
// ILI9341_POSITIVE_GAMMA_CORRECTION, 15, 0x00, 0x03, 0x09, 0x08, 0x16, 0x0A, 0x3F, 0x78, 0x4C, 0x09, 0x0A, 0x08, 0x16, 0x1A, 0x0F,
// N-Gamma
// ILI9341_NEGATIVE_GAMMA_CORRECTION, 15, 0x00, 0X16, 0X19, 0x03, 0x0F, 0x05, 0x32, 0x45, 0x46, 0x04, 0x0E, 0x0D, 0x35, 0x37, 0x0F,
//Set Image Func
// 0xE9, 1, 0x00,
// Set Brightness to Max
ILI9341_WRITE_BRIGHTNESS, 1, 0xFF,
// Adjust Control
ILI9341_PUMP_RATIO_CONTROL, 4, 0xA9, 0x51, 0x2C, 0x82,
//Exit Sleep
ILI9341_SLEEP_OUT, 0x00,
// display on
ILI9341_DISPLAY_ON, 0,
0 // sentinel
};
#define LCD_INIT ST7796S_init_seq
#endif
#ifdef LCD_DRIVER_ILI9341
static const uint8_t ili9341_init_seq[] = {
// cmd, len, data...,
// SW reset
ILI9341_SOFTWARE_RESET, 0,
// display off
ILI9341_DISPLAY_OFF, 0,
// Power control B
ILI9341_POWERB, 3, 0x00, 0xC1, 0x30,
// Power on sequence control
ILI9341_POWER_SEQ, 4, 0x64, 0x03, 0x12, 0x81,
// Driver timing control A
ILI9341_DTCA, 3, 0x85, 0x00, 0x78,
// Power control A
ILI9341_POWERA, 5, 0x39, 0x2C, 0x00, 0x34, 0x02,
// Pump ratio control
ILI9341_PUMP_RATIO_CONTROL, 1, 0x20,
// Driver timing control B
ILI9341_DTCB, 2, 0x00, 0x00,
// POWER_CONTROL_1
ILI9341_POWER_CONTROL_1, 1, 0x23,
// POWER_CONTROL_2
ILI9341_POWER_CONTROL_2, 1, 0x10,
// VCOM_CONTROL_1
ILI9341_VCOM_CONTROL_1, 2, 0x3e, 0x28,
// VCOM_CONTROL_2
ILI9341_VCOM_CONTROL_2, 1, 0xBE,
// MEMORY_ACCESS_CONTROL
//ILI9341_MEMORY_ACCESS_CONTROL, 1, 0x48, // portlait
ILI9341_MEMORY_ACCESS_CONTROL, 1, DISPLAY_ROTATION_0, // landscape
// COLMOD_PIXEL_FORMAT_SET : 16 bit pixel
ILI9341_PIXEL_FORMAT_SET, 1, 0x55,
// Frame Rate
ILI9341_FRAME_RATE_CONTROL_1, 2, 0x00, 0x18,
// Gamma Function Disable
ILI9341_3GAMMA_EN, 1, 0x00,
// gamma set for curve 01/2/04/08
ILI9341_GAMMA_SET, 1, 0x01,
// positive gamma correction
ILI9341_POSITIVE_GAMMA_CORRECTION, 15, 0x0F, 0x31, 0x2B, 0x0C, 0x0E, 0x08, 0x4E, 0xF1, 0x37, 0x07, 0x10, 0x03, 0x0E, 0x09, 0x00,
// negativ gamma correction
ILI9341_NEGATIVE_GAMMA_CORRECTION, 15, 0x00, 0x0E, 0x14, 0x03, 0x11, 0x07, 0x31, 0xC1, 0x48, 0x08, 0x0F, 0x0C, 0x31, 0x36, 0x0F,
// Column Address Set
// ILI9341_COLUMN_ADDRESS_SET, 4, 0x00, 0x00, 0x01, 0x3f, // width 320
// Page Address Set
// ILI9341_PAGE_ADDRESS_SET, 4, 0x00, 0x00, 0x00, 0xef, // height 240
// entry mode
ILI9341_ENTRY_MODE_SET, 1, 0x06,
// display function control
ILI9341_DISPLAY_FUNCTION_CONTROL, 3, 0x08, 0x82, 0x27,
// Interface Control (set WEMODE=0)
ILI9341_INTERFACE_CONTROL, 3, 0x00, 0x00, 0x00,
// sleep out
ILI9341_SLEEP_OUT, 0,
// display on
ILI9341_DISPLAY_ON, 0,
0 // sentinel
};
#define LCD_INIT ili9341_init_seq
#endif
#ifdef __LCD_BRIGHTNESS__
#if HAL_USE_DAC == TRUE
#error "Need to disable HAL_USE_DAC in halconf.h for use __LCD_BRIGHTNESS__"
#endif
#define BRIGHTNESS_MIN_LEVEL 0
#define BRIGHTNESS_MAX_LEVEL 4000
// Brightness control range 0 - 100
// DAC value range 0 - 4095 (0 to vRef in Volt)
void lcd_setBrightness(uint16_t b){
b = BRIGHTNESS_MIN_LEVEL + b*((BRIGHTNESS_MAX_LEVEL-BRIGHTNESS_MIN_LEVEL)/100);
DAC->DHR12R2 = b;
}
#endif
void ili9341_init(void)
{
LCD_DC_DATA;
LCD_RESET_ASSERT;
chThdSleepMilliseconds(10);
LCD_RESET_NEGATE;
const uint8_t *p;
for (p = LCD_INIT; *p; ) {
send_command(p[0], p[1], &p[2]);
p += 2 + p[1];
chThdSleepMilliseconds(2);
}
#ifdef TINYSA4
if ((hwid % 100) >= 2) {
uint8_t data[] = {0};
send_command(0x21, 1, data);
}
#endif
ili9341_clear_screen();
LCD_CS_HIGH;
}
static void ili9341_setWindow(uint8_t cmd, uint16_t x, uint16_t y, uint16_t w, uint16_t h){
// Any LCD exchange start from this
#ifdef __USE_DISPLAY_DMA__
dmaChannelWaitCompletionRxTx();
#endif
//uint8_t xx[4] = { x >> 8, x, (x+w-1) >> 8, (x+w-1) };
//uint8_t yy[4] = { y >> 8, y, (y+h-1) >> 8, (y+h-1) };
uint32_t xx = __REV16(x | ((x + w - 1) << 16));
uint32_t yy = __REV16(y | ((y + h - 1) << 16));
send_command(ILI9341_COLUMN_ADDRESS_SET, 4, (uint8_t *)&xx);
send_command(ILI9341_PAGE_ADDRESS_SET, 4, (uint8_t *)&yy);
send_command(cmd, 0, NULL);
}
#ifndef __USE_DISPLAY_DMA__
void ili9341_fill(int x, int y, int w, int h)
{
ili9341_setWindow(ILI9341_MEMORY_WRITE, x, y, w, h);
uint32_t len = w * h;
do {
while (SPI_TX_IS_NOT_EMPTY(LCD_SPI))
;
SPI_WRITE_16BIT(LCD_SPI, background_color);
}while(--len);
#ifdef __REMOTE_DESKTOP__
if (auto_capture) {
remote_region_t rd = {"fill\r\n", x, y, w, h};
send_region(&rd, (uint8_t *)&background_color, sizeof(pixel_t));
}
#endif
}
void ili9341_bulk(int x, int y, int w, int h)
{
ili9341_setWindow(ILI9341_MEMORY_WRITE, x, y, w, h);
spi_TxBuffer((uint8_t *)spi_buffer, w * h * sizeof(pixel_t));
#ifdef __REMOTE_DESKTOP__
if (auto_capture) {
remote_region_t rd = {"bulk\r\n", x, y, w, h};
send_region(&rd, (uint8_t *)spi_buffer, w * h * sizeof(pixel_t));
}
#endif
}
#else // LCD DMA mode
//
// Use DMA for send data
//
#define LCD_DMA_MODE STM32_DMA_CR_HWORD
// Fill region by some color
void ili9341_fill(int x, int y, int w, int h)
{
ili9341_setWindow(ILI9341_MEMORY_WRITE, x, y, w, h);
dmaChannelSetMemory(LCD_DMA_TX, &background_color);
uint32_t len = w * h, delta;
while(len) {
delta = len > 0xFFFF ? 0xFFFF : len; // DMA can send only 65535 data in one run
dmaChannelSetTransactionSize(LCD_DMA_TX, delta);
dmaChannelSetMode(LCD_DMA_TX, txdmamode | LCD_DMA_MODE | STM32_DMA_CR_EN);
dmaChannelWaitCompletion(LCD_DMA_TX);
len-=delta;
}
#ifdef __REMOTE_DESKTOP__
if (auto_capture) {
remote_region_t rd = {"fill\r\n", x, y, w, h};
send_region(&rd, (uint8_t *)&background_color, sizeof(pixel_t));
}
#endif
// while (SPI_IN_TX_RX(LCD_SPI));
}
void ili9341_flip(bool flip) {
uint8_t memAcc = flip ? DISPLAY_ROTATION_180 : DISPLAY_ROTATION_0;
send_command(ILI9341_MEMORY_ACCESS_CONTROL, 1, &memAcc);
}
void ili9341_define_scroll(uint16_t tfa, uint16_t bfa) {
uint8_t temp[6];
temp[0] = tfa>>8;
temp[1] = tfa;
temp[2] = 0; // One line
temp[3] = 1;
temp[4] = bfa>>8;
temp[5] = bfa;
send_command(ILI9341_VERTICAL_SCROLLING_DEF,6,temp);
}
void ili9341_scroll(uint16_t start) {
uint8_t temp[6];
temp[0] = start>>8;
temp[1] = start;
send_command(ILI9341_VERTICAL_SCROLLING,2, temp);
}
static void ili9341_DMA_bulk(uint16_t x, uint16_t y, uint16_t w, uint16_t h, pixel_t *buffer){
ili9341_setWindow(ILI9341_MEMORY_WRITE, x, y, w, h);
dmaChannelSetMemory(LCD_DMA_TX, buffer);
dmaChannelSetTransactionSize(LCD_DMA_TX, w * h);
dmaChannelSetMode(LCD_DMA_TX, txdmamode | LCD_DMA_MODE | STM32_DMA_CR_MINC | STM32_DMA_CR_EN);
#ifdef __REMOTE_DESKTOP__
if (auto_capture) {
remote_region_t rd = {"bulk\r\n", x, y, w, h};
send_region(&rd, (uint8_t *)buffer, w * h * sizeof(pixel_t));
}
#endif
// while (SPI_IN_TX_RX(LCD_SPI));
}
// Copy spi_buffer to region
void ili9341_bulk(int x, int y, int w, int h)
{
ili9341_DMA_bulk(x, y, w, h, spi_buffer); // Send data
dmaChannelWaitCompletion(LCD_DMA_TX); // Wait
}
// Used only in double buffer mode
#ifndef ili9341_get_cell_buffer
#define LCD_BUFFER_1 0x01
#define LCD_DMA_RUN 0x02
static uint8_t LCD_dma_status = 0;
// Return free buffer for render
pixel_t *ili9341_get_cell_buffer(void){
return &spi_buffer[(LCD_dma_status&LCD_BUFFER_1) ? SPI_BUFFER_SIZE/2 : 0];
}
#endif
// Wait completion before next data send
#ifndef ili9341_bulk_finish
void ili9341_bulk_finish(void){
dmaChannelWaitCompletion(LCD_DMA_TX); // Wait DMA
//while (SPI_IN_TX_RX(LCD_SPI)); // Wait tx
}
#endif
// Copy part of spi_buffer to region, no wait completion after if buffer count !=1
#ifndef ili9341_bulk_continue
void ili9341_bulk_continue(int x, int y, int w, int h)
{
ili9341_bulk_finish(); // Wait DMA
ili9341_DMA_bulk(x, y, w, h, ili9341_get_cell_buffer()); // Send new cell data
LCD_dma_status^=LCD_BUFFER_1; // Switch buffer
}
#endif
#endif // end LCD DMA mode
#ifdef LCD_DRIVER_ILI9341
// ILI9341 send data in RGB888 format, need parse it
// Copy ILI9341 screen data to buffer
void ili9341_read_memory(int x, int y, int w, int h, uint16_t *out)
{
uint16_t len = w * h;
ili9341_setWindow(ILI9341_MEMORY_READ, x, y, w, h);
// Skip data from rx buffer
spi_DropRx();
// Set read speed (if need different)
#ifdef LCD_SPI_RX_SPEED
SPI_BR_SET(LCD_SPI, LCD_SPI_RX_SPEED);
#endif
// require 8bit dummy clock
spi_RxByte();
// receive pixel data to buffer
#ifndef __USE_DISPLAY_DMA_RX__
spi_RxBuffer((uint8_t *)out, len * 3);
// Parse received data to RGB565 format
uint8_t *rgbbuf = (uint8_t *)out;
do {
uint8_t r, g, b;
// read data is always 18bit
r = rgbbuf[0];
g = rgbbuf[1];
b = rgbbuf[2];
*out++ = RGB565(r, g, b);
rgbbuf += 3;
}while(--len);
#else
// Set data size for DMA read
len*=3;
// Start DMA read, and not wait completion
spi_DMARxBuffer((uint8_t *)out, len, false);
// Parse received data to RGB565 format while data receive by DMA
uint8_t *rgbbuf = (uint8_t *)out;
do {
uint16_t left = dmaChannelGetTransactionSize(LCD_DMA_RX) + 3; // Get DMA data left
if (left > len) continue; // Next pixel RGB data not ready
do{ // Process completed by DMA data
uint8_t r, g, b; // read data is always 18bit in RGB888 format
r = rgbbuf[0];
g = rgbbuf[1];
b = rgbbuf[2];
*out++ = RGB565(r, g, b);
rgbbuf+= 3;
len -= 3;
} while (left < len);
} while(len);
dmaChannelWaitCompletionRxTx(); // Wait DMA completion and stop it
#endif
// restore speed if need
#ifdef LCD_SPI_RX_SPEED
SPI_BR_SET(LCD_SPI, LCD_SPI_SPEED);
#endif
LCD_CS_HIGH;
}
#endif
#ifdef LCD_DRIVER_ST7796S
// ST7796S send data in RGB565 format, not need parse it
// Copy ST7796S screen data to buffer
void ili9341_read_memory(int x, int y, int w, int h, uint16_t *out)
{
uint16_t len = w * h;
ili9341_setWindow(ILI9341_MEMORY_READ, x, y, w, h);
// Skip data from rx buffer
spi_DropRx();
// Set read speed (if need different)
#ifdef LCD_SPI_RX_SPEED
SPI_BR_SET(LCD_SPI, LCD_SPI_RX_SPEED);
#endif
// require 8bit dummy clock
spi_RxByte();
// receive pixel data to buffer
#ifndef __USE_DISPLAY_DMA_RX__
spi_RxBuffer((uint8_t *)out, len * 2);
#else
spi_DMARxBuffer((uint8_t *)out, len * 2, true);
#endif
// restore speed if need
#ifdef LCD_SPI_RX_SPEED
SPI_BR_SET(LCD_SPI, LCD_SPI_SPEED);
#endif
LCD_CS_HIGH;
}
#endif
void ili9341_clear_screen(void)
{
ili9341_fill(0, 0, ILI9341_WIDTH, ILI9341_HEIGHT);
}
void ili9341_set_foreground(uint16_t fg_idx)
{
foreground_color = GET_PALTETTE_COLOR(fg_idx);
}
void ili9341_set_background(uint16_t bg_idx)
{
background_color = GET_PALTETTE_COLOR(bg_idx);
}
void ili9341_set_rotation(uint8_t r)
{
// static const uint8_t rotation_const[]={DISPLAY_ROTATION_0, DISPLAY_ROTATION_90,
// DISPLAY_ROTATION_180, DISPLAY_ROTATION_270};
send_command(ILI9341_MEMORY_ACCESS_CONTROL, 1, &r);
}
void ili9341_blitBitmap(int x, int y, int width, int height, const uint8_t *b)
{
pixel_t *buf = spi_buffer;
uint8_t bits = 0;
for (uint16_t c = 0; c < height; c++) {
for (uint16_t r = 0; r < width; r++) {
if ((r&7) == 0) bits = *b++;
*buf++ = (0x80 & bits) ? foreground_color : background_color;
bits <<= 1;
}
}
ili9341_bulk(x, y, width, height);
}
void ili9341_drawchar(uint8_t ch, int x, int y)
{
ili9341_blitBitmap(x, y, FONT_GET_WIDTH(ch), FONT_GET_HEIGHT, FONT_GET_DATA(ch));
}
void ili9341_drawstring(const char *str, int x, int y)
{
int x_pos = x;
while (*str) {
uint8_t ch = *str++;
if (ch == '\n') {x = x_pos; y+=FONT_STR_HEIGHT; continue;}
const uint8_t *char_buf = FONT_GET_DATA(ch);
uint16_t w = FONT_GET_WIDTH(ch);
ili9341_blitBitmap(x, y, w, FONT_GET_HEIGHT, char_buf);
x += w;
}
}
void ili9341_drawstring_7x13(const char *str, int x, int y)
{
int x_pos = x;
while (*str) {
uint8_t ch = *str++;
if (ch == '\n') {x = x_pos; y+=bFONT_STR_HEIGHT; continue;}
if (ch == '\b') {ili9341_set_foreground(LCD_BRIGHT_COLOR_BLUE); continue;};
const uint8_t *char_buf = bFONT_GET_DATA(ch);
uint16_t w = bFONT_GET_WIDTH(ch);
ili9341_blitBitmap(x, y, w, bFONT_GET_HEIGHT, char_buf);
x += w;
}
}
void ili9341_drawstring_10x14(const char *str, int x, int y)
{
#ifdef wFONT_GET_DATA
int x_pos = x;
while (*str) {
uint8_t ch = *str++;
if (ch == '\n') {x = x_pos; y+=wFONT_STR_HEIGHT; continue;}
const uint8_t *char_buf = wFONT_GET_DATA(ch);
uint16_t w = wFONT_GET_WIDTH(ch);
ili9341_blitBitmap(x, y, (w<=8) ? 9 : w, wFONT_GET_HEIGHT, char_buf);
x += w;
}
#else
ili9341_drawstring_size(str, x, y, 2);
#endif
}
typedef struct {
const void *vmt;
int16_t start_x;
int16_t start_y;
int16_t x;
int16_t y;
} lcdPrintStream;
static msg_t lcd_put5x7(void *ip, uint8_t ch) {
lcdPrintStream *ps = ip;
if (ch == '\n') {ps->x = ps->start_x; ps->y+=FONT_STR_HEIGHT; return MSG_OK;}
uint16_t w = FONT_GET_WIDTH(ch);
ili9341_blitBitmap(ps->x, ps->y, w, FONT_GET_HEIGHT, FONT_GET_DATA(ch));
ps->x+= w;
return MSG_OK;
}
static msg_t lcd_put_7x11b(void *ip, uint8_t ch) {
lcdPrintStream *ps = ip;
if (ch == '\n') {ps->x = ps->start_x; ps->y+=bFONT_STR_HEIGHT; return MSG_OK;}
uint16_t w = bFONT_GET_WIDTH(ch);
ili9341_blitBitmap(ps->x, ps->y, w, bFONT_GET_HEIGHT, bFONT_GET_DATA(ch));
ps->x+= w;
return MSG_OK;
}
typedef msg_t (*font_put_t)(void *ps, uint8_t ch);
static font_put_t put_char = lcd_put5x7;
void lcd_set_font(int type) {put_char = type == FONT_SMALL ? lcd_put5x7 : lcd_put_7x11b;}
// Simple print in buffer function
int lcd_printf(int16_t x, int16_t y, const char *fmt, ...) {
// Init small lcd print stream
struct lcd_printStreamVMT {
_base_sequential_stream_methods
} lcd_vmt = {NULL, NULL, put_char, NULL};
lcdPrintStream ps = {&lcd_vmt, x, y, x, y};
// Performing the print operation using the common code.
va_list ap;
va_start(ap, fmt);
int retval = chvprintf((BaseSequentialStream *)(void *)&ps, fmt, ap);
va_end(ap);
// Return number of bytes that would have been written.
return retval;
}
void ili9341_drawstringV(const char *str, int x, int y)
{
ili9341_set_rotation(DISPLAY_ROTATION_270);
ili9341_drawstring(str, ILI9341_HEIGHT-y, x);
ili9341_set_rotation(DISPLAY_ROTATION_0);
}
#ifdef __LEVEL_METER__
int ili9341_drawchar_size(uint8_t ch, int x, int y, uint8_t size, int x_max)
{
uint16_t *buf = spi_buffer;
const uint16_t *char_buf = (uint16_t *)wFONT_GET_DATA(ch);
uint16_t w = wFONT_GET_WIDTH(ch);
if (x > x_max)
return 0;
if (w*size + x > x_max)
w = (x_max - x)/size;
for (int c = 0; c < wFONT_GET_HEIGHT; c++, char_buf++) {
for (int i = 0; i < size; i++) {
uint16_t bits = *char_buf;bits=(bits>>8)|(bits<<8);
for (int r = 0; r < w; r++, bits <<= 1)
for (int j = 0; j < size; j++)
*buf++ = (0x8000 & bits) ? foreground_color : background_color;
}
}
ili9341_bulk(x, y, w * size, wFONT_GET_HEIGHT * size);
return w*size;
}
int ili9341_drawstring_size(const char *str, int x, int y, uint8_t size, int x_max)
{
while (*str)
x += ili9341_drawchar_size(*str++, x, y, size, x_max);
return x;
}
#endif
void ili9341_drawfont(uint8_t ch, int x, int y)
{
ili9341_blitBitmap(x, y, NUM_FONT_GET_WIDTH, NUM_FONT_GET_HEIGHT,
NUM_FONT_GET_DATA(ch));
}
#if 0
static void ili9341_pixel(int x, int y, uint16_t color)
{
uint32_t xx = __REV16(x|((x)<<16));
uint32_t yy = __REV16(y|((y)<<16));
send_command(ILI9341_COLUMN_ADDRESS_SET, 4, (uint8_t*)&xx);
send_command(ILI9341_PAGE_ADDRESS_SET, 4, (uint8_t*)&yy);
send_command(ILI9341_MEMORY_WRITE, 2, &color);
}
#endif
#define SWAP(x, y) { int z = x; x = y; y = z; }
void ili9341_line(int x0, int y0, int x1, int y1)
{
SWAP(foreground_color, background_color);
#if 0
// modified Bresenham's line algorithm, see https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
int dx = x1 - x0, sx = 1; if (dx < 0) {dx = -dx; sx = -1;}
int dy = y1 - y0, sy = 1; if (dy < 0) {dy = -dy; sy = -1;}
int err = (dx > dy ? dx : -dy) / 2;
while (1) {
ili9341_fill(x0, y0, 1, 1);
if (x0 == x1 && y0 == y1)
break;
int e2 = err;
if (e2 > -dx) { err -= dy; x0 += sx; }
if (e2 < dy) { err += dx; y0 += sy; }
}
#else
if (x0 > x1) {
SWAP(x0, x1);
SWAP(y0, y1);
}
while (x0 <= x1) {
int dx = x1 - x0 + 1;
int dy = y1 - y0;
if (dy >= 0) {
dy++;
if (dy > dx) {
dy /= dx; dx = 1;
} else {
dx /= dy; dy = 1;
}
} else {
dy--;
if (-dy > dx) {
dy /= dx; dx = 1;
} else {
dx /= -dy;dy = -1;
}
}
if (dy > 0)
ili9341_fill(x0, y0, dx, dy);
else
ili9341_fill(x0, y0+dy, dx, -dy);
x0 += dx;
y0 += dy;
}
#endif
SWAP(foreground_color, background_color);
}