-
Notifications
You must be signed in to change notification settings - Fork 0
/
Eve2_81x.c
1147 lines (983 loc) · 39.2 KB
/
Eve2_81x.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
// Eve2 Processor Agnostic Library (Condensed)
//
// This "library" consists of the files "Eve2_81x.c" and "Eve2_81x.h".
//
// In persuit of the common goal of simplicity and understandability I find that I am unable to
// make function prototypes that match Bridgetek example code. I draw the line between the
// Eve and all other hardware. The library is "clean" and includes no abstraction at all, unlike
// much of the example code on the Internet which is sort of application and abstraction mixed
// together in a confusing abuse of my eye-holes.
// My intent is to be as straight forward and understandable as possible, so while function
// names and parameter lists are different than Bridgetek code examples, they should be easily
// recognizable. I have also made every attempt to reference Bridgetek documentation against
// the code to act as a translation to help in understanding.
// Notes on the operation of the Eve command processing engine - THE FIFO
//
// First be aware that the FTDI documentation variously refers to you as "User", "MCU", "Host".
//
// The FIFO, like all FIFO's needs pointers to indicate the starting address of buffered data and
// the end address of buffered data. There is wrapping involved, but the basic idea is clear.
// Eve takes data into it's FIFO using a fully defined write operation to a memory address - that
// is, you need to take care of the wrapping - to you, it is not a FIFO - it is a piece of memory.
// Eve keeps track of it's own read address location, but relies on you to write the address
// of the end of buffered data.
//
// So as commands are loaded into RAM - into the FIFO space - Eve will do nothing in response.
// Eve is happy to take your data and store it for you while it sits with it's read address and
// write address set to the same value. Once the commands are loaded, the next available address
// is manually written (by you) to the register in which Eve stores the FIFO write pointer
// (REG_CMD_WRITE).
//
// Following this, Eve discovers that the addresses are different and begins processing commands while
// updating it's own read pointer until the read and write pointers are the same.
//
// Be aware that Eve stores only the offset into the "FIFO" as 16 bits, so any use of the offset
// requires adding the base address (RAM_CMD 0x308000) to the resultant 32 bit value.
#include <stdio.h>
#include <stdint.h> // Find integer types like "uint8_t"
#include <stdbool.h> // for true/false
#include "Eve2_81x.h" // Header for this file with prototypes, defines, and typedefs
#include "MatrixEve2Conf.h" // Header for display selection
#include "hw_api.h" // for spi abstraction
#define WorkBuffSz 512
#define Log printf
// Global Variables
uint16_t FifoWriteLocation = 0;
char LogBuf[WorkBuffSz]; // The singular universal data array used for all things including logging
static uint32_t Width;
static uint32_t Height;
static uint32_t HOffset;
static uint32_t VOffset;
static uint8_t Touch;
uint32_t Display_Width()
{
return Width;
}
uint32_t Display_Height()
{
return Height;
}
uint8_t Display_Touch()
{
return Touch;
}
uint32_t Display_HOffset()
{
return HOffset;
}
uint32_t Display_VOffset()
{
return VOffset;
}
extern MLog(const char* x);
// Call this function once at powerup to reset and initialize the Eve chip
void FT81x_Init(int display, int board, int touch)
{
uint32_t Ready = false;
int DWIDTH;
int DHEIGHT;
int PIXVOFFSET;
int PIXHOFFSET;
int HCYCLE;
int HOFFSET;
int HSYNC0;
int HSYNC1;
int VCYCLE;
int VOFFSET;
int VSYNC0;
int VSYNC1;
int PCLK;
int SWIZZLE;
int PCLK_POL;
int HSIZE;
int VSIZE;
int CSPREAD;
int DITHER;
switch (display)
{
case DISPLAY_70:
DWIDTH = 800;
DHEIGHT = 480;
PIXVOFFSET = 0;
PIXHOFFSET = 0;
HCYCLE = 928;
HOFFSET = 88;
HSYNC0 = 0;
HSYNC1 = 48;
VCYCLE = 525;
VOFFSET = 32;
VSYNC0 = 0;
VSYNC1 = 3;
PCLK = 2;
SWIZZLE = 0;
PCLK_POL = 1;
HSIZE = 800;
VSIZE = 480;
CSPREAD = 0;
DITHER = 1;
break;
case DISPLAY_50:
DWIDTH = 800;
DHEIGHT = 480;
PIXVOFFSET = 0;
PIXHOFFSET = 0;
HCYCLE = 928;
HOFFSET = 88;
HSYNC0 = 0;
HSYNC1 = 48;
VCYCLE = 525;
VOFFSET = 32;
VSYNC0 = 0;
VSYNC1 = 3;
PCLK = 2;
SWIZZLE = 0;
PCLK_POL = 1;
HSIZE = 800;
VSIZE = 480;
CSPREAD = 0;
DITHER = 1;
break;
case DISPLAY_43:
DWIDTH = 480;
DHEIGHT = 272;
PIXVOFFSET = 0;
PIXHOFFSET = 0;
HCYCLE = 548;
HOFFSET = 43;
HSYNC0 = 0;
HSYNC1 = 41;
VCYCLE = 292;
VOFFSET = 12;
VSYNC0 = 0;
VSYNC1 = 10;
PCLK = 5;
SWIZZLE = 0;
PCLK_POL = 1;
HSIZE = 480;
VSIZE = 272;
CSPREAD = 1;
DITHER = 1;
break;
case DISPLAY_39:
DWIDTH = 480;
DHEIGHT = 128;
PIXVOFFSET = 0;
PIXHOFFSET = 0;
HCYCLE = 524;
HOFFSET = 16;
HSYNC1 = 44;
HSYNC0 = 0;
VCYCLE = 288;
VOFFSET = 12;
VSYNC1 = 8;
VSYNC0 = 11;
PCLK = 5;
SWIZZLE = 0;
PCLK_POL = 1;
HSIZE = 480;
VSIZE = 272;
CSPREAD = 1;
DITHER = 1;
break;
case DISPLAY_38:
DWIDTH = 480;
DHEIGHT = 116;
PIXVOFFSET = 10;
PIXHOFFSET = 0;
HCYCLE = 524;
HOFFSET = 43;
HSYNC0 = 0;
HSYNC1 = 41;
VCYCLE = 292;
VOFFSET = 12;
VSYNC0 = 152;
VSYNC1 = 10;
PCLK = 5;
SWIZZLE = 0;
PCLK_POL = 1;
HSIZE = 480;
VSIZE = 272;
CSPREAD = 1;
DITHER = 1;
break;
case DISPLAY_35:
DWIDTH = 320;
DHEIGHT = 240;
PIXVOFFSET = 0;
PIXHOFFSET = 0;
HCYCLE = 408;
HOFFSET = 68;
HSYNC0 = 0;
HSYNC1 = 10;
VCYCLE = 262;
VOFFSET = 18;
VSYNC0 = 0;
VSYNC1 = 2;
PCLK = 8;
SWIZZLE = 0;
PCLK_POL = 0;
HSIZE = 320;
VSIZE = 240;
CSPREAD = 1;
DITHER = 1;
break;
case DISPLAY_29:
DWIDTH = 320;
DHEIGHT = 102;
PIXVOFFSET = 0;
PIXHOFFSET = 0;
HCYCLE = 408;
HOFFSET = 70;
HSYNC0 = 0;
HSYNC1 = 10;
VCYCLE = 262;
VOFFSET = 156;
VSYNC0 = 0;
VSYNC1 = 2;
PCLK = 8;
SWIZZLE = 0;
PCLK_POL = 0;
HSIZE = 320;
VSIZE = 102;
CSPREAD = 1;
DITHER = 1;
break;
}
Width = DWIDTH;
Height = DHEIGHT;
HOffset = PIXHOFFSET;
VOffset = PIXVOFFSET;
Touch = touch;
DBG("reset\n");
Eve_Reset(); // Hard reset of the Eve chip
// Wakeup Eve
if (board == BOARD_EVE3)
{
HostCommand(HCMD_CLKEXT);
}
HostCommand(HCMD_ACTIVE);
HAL_Delay(300);
do
{
DBG("Cmd_READ_REG_ID\n");
Ready = Cmd_READ_REG_ID();
if (Ready)
DBG("Cmd_READ_REG_ID - OK\n");
else
DBG("Cmd_READ_REG_ID - fail\n");
} while (!Ready);
DBG("Cmd_READ_REG_ID - Complete\n");
// Log("Eve now ACTIVE\n"); //
Ready = rd32(REG_CHIP_ID);
uint16_t ValH = Ready >> 16;
uint16_t ValL = Ready & 0xFFFF;
Log("Chip ID = 0x%04x%04x\n", ValH, ValL);
wr32(REG_FREQUENCY + RAM_REG, 0x3938700); // Configure the system clock to 60MHz
// Before we go any further with Eve, it is a good idea to check to see if she is wigging out about something
// that happened before the last reset. If Eve has just done a power cycle, this would be unnecessary.
if (rd16(REG_CMD_READ + RAM_REG) == 0xFFF)
{
// Eve is unhappy - needs a paddling.
uint32_t Patch_Add = rd32(REG_COPRO_PATCH_PTR + RAM_REG);
wr8(REG_CPU_RESET + RAM_REG, 1);
wr16(REG_CMD_READ + RAM_REG, 0);
wr16(REG_CMD_WRITE + RAM_REG, 0);
wr16(REG_CMD_DL + RAM_REG, 0);
wr8(REG_CPU_RESET + RAM_REG, 0);
wr32(REG_COPRO_PATCH_PTR + RAM_REG, Patch_Add);
}
// turn off screen output during startup
wr8(REG_GPIOX + RAM_REG, 0); // Set REG_GPIOX to 0 to turn off the LCD DISP signal
wr8(REG_PCLK + RAM_REG, 0); // Pixel Clock Output disable
// load parameters of the physical screen to the Eve
// All of these registers are 32 bits, but most bits are reserved, so only write what is actually used
wr16(REG_HCYCLE + RAM_REG, HCYCLE); // Set H_Cycle to 548
wr16(REG_HOFFSET + RAM_REG, HOFFSET); // Set H_Offset to 43
wr16(REG_HSYNC0 + RAM_REG, HSYNC0); // Set H_SYNC_0 to 0
wr16(REG_HSYNC1 + RAM_REG, HSYNC1); // Set H_SYNC_1 to 41
wr16(REG_VCYCLE + RAM_REG, VCYCLE); // Set V_Cycle to 292
wr16(REG_VOFFSET + RAM_REG, VOFFSET); // Set V_OFFSET to 12
wr16(REG_VSYNC0 + RAM_REG, VSYNC0); // Set V_SYNC_0 to 0
wr16(REG_VSYNC1 + RAM_REG, VSYNC1); // Set V_SYNC_1 to 10
wr8(REG_SWIZZLE + RAM_REG, SWIZZLE); // Set SWIZZLE to 0
wr8(REG_PCLK_POL + RAM_REG, PCLK_POL); // Set PCLK_POL to 1
wr16(REG_HSIZE + RAM_REG, HSIZE); // Set H_SIZE to 480
wr16(REG_VSIZE + RAM_REG, VSIZE); // Set V_SIZE to 272
wr8(REG_CSPREAD + RAM_REG, CSPREAD); // Set CSPREAD to 1 (32 bit register - write only 8 bits)
wr8(REG_DITHER + RAM_REG, DITHER); // Set DITHER to 1 (32 bit register - write only 8 bits)
// configure touch & audio
if (touch == TOUCH_TPR)
{
wr16(REG_TOUCH_CONFIG + RAM_REG, 0x8381);
}
else if (touch == TOUCH_TPC)
{
wr16(REG_TOUCH_CONFIG + RAM_REG, 0x5d0);
if (board == BOARD_EVE2)
{
Cap_Touch_Upload();
}
}
wr16(REG_TOUCH_RZTHRESH + RAM_REG, 1200); // set touch resistance threshold
wr8(REG_TOUCH_MODE + RAM_REG, 0x02); // set touch on: continous - this is default
wr8(REG_TOUCH_ADC_MODE + RAM_REG, 0x01); // set ADC mode: differential - this is default
wr8(REG_TOUCH_OVERSAMPLE + RAM_REG, 15); // set touch oversampling to max
wr16(REG_GPIOX_DIR + RAM_REG, 0x8000 ); // Set Disp GPIO Direction
wr16(REG_GPIOX + RAM_REG, 0x8000 ); // Enable Disp (if used)
wr16(REG_PWM_HZ + RAM_REG, 0x00FA); // Backlight PWM frequency
wr8(REG_PWM_DUTY + RAM_REG, 32); // Backlight PWM duty (on)
// write first display list (which is a clear and blank screen)
wr32(RAM_DL+0, CLEAR_COLOR_RGB(0,0,0));
wr32(RAM_DL+4, CLEAR(1,1,1));
wr32(RAM_DL+8, DISPLAY());
wr8(REG_DLSWAP + RAM_REG, DLSWAP_FRAME); // swap display lists
wr8(REG_PCLK + RAM_REG, PCLK); // after this display is visible on the LCD
}
// Reset Eve chip via the hardware PDN line
void Eve_Reset(void)
{
HAL_Eve_Reset_HW();
}
// Upload Goodix Calibration file
void Cap_Touch_Upload(void)
{
#include "touch_cap_811.h"
//---Goodix911 Configuration from AN336
//Load the TOUCH_DATA_U8 or TOUCH_DATA_U32 array from file “touch_cap_811.h” via the FT81x command buffer RAM_CMD
uint8_t CTOUCH_CONFIG_DATA_G911[] = { TOUCH_DATA_U8 };
CoProWrCmdBuf(CTOUCH_CONFIG_DATA_G911, TOUCH_DATA_LEN);
//Execute the commands till completion
UpdateFIFO();
Wait4CoProFIFOEmpty();
//Hold the touch engine in reset(write REG_CPURESET = 2)
wr8(REG_CPU_RESET + RAM_REG, 2);
//Set GPIO3 output LOW
wr8(REG_GPIOX_DIR + RAM_REG, (rd8(RAM_REG + REG_GPIOX_DIR) | 0x08)); // Set Disp GPIO Direction
wr8(REG_GPIOX + RAM_REG, (rd8(RAM_REG + REG_GPIOX) | 0xF7)); // Clear GPIO
//Wait more than 100us
HAL_Delay(1);
//Write REG_CPURESET=0
wr8(REG_CPU_RESET + RAM_REG, 0);
//Wait more than 55ms
HAL_Delay(100);
//Set GPIO3 to input (floating)
wr8(REG_GPIOX_DIR + RAM_REG, (rd8(RAM_REG + REG_GPIOX_DIR) & 0xF7)); // Set Disp GPIO Direction
//---Goodix911 Configuration from AN336
}
// *** Host Command - FT81X Embedded Video Engine Datasheet - 4.1.5 **********************************************
// Host Command is a function for changing hardware related parameters of the Eve chip. The name is confusing.
// These are related to power modes and the like. All defined parameters have HCMD_ prefix
void HostCommand(uint8_t HCMD)
{
// Log("Inside HostCommand\n");
HAL_SPI_Enable();
/* HAL_SPI_Write(HCMD | 0x40); // In case the manual is making you believe that you just found the bug you were looking for - no. */
HAL_SPI_Write(HCMD);
HAL_SPI_Write(0x00); // This second byte is set to 0 but if there is need for fancy, never used setups, then rewrite.
HAL_SPI_Write(0x00);
HAL_SPI_Disable();
}
// *** Eve API Reference Definitions *****************************************************************************
// FT81X Embedded Video Engine Datasheet 1.3 - Section 4.1.4, page 16
// These are all functions related to writing / reading data of various lengths with a memory address of 32 bits
// ***************************************************************************************************************
void wr32(uint32_t address, uint32_t parameter)
{
HAL_SPI_Enable();
HAL_SPI_Write((uint8_t)((address >> 16) | 0x80)); // RAM_REG = 0x302000 and high bit is set - result always 0xB0
HAL_SPI_Write((uint8_t)(address >> 8)); // Next byte of the register address
HAL_SPI_Write((uint8_t)address); // Low byte of register address - usually just the 1 byte offset
HAL_SPI_Write((uint8_t)(parameter & 0xff)); // Little endian (yes, it is most significant bit first and least significant byte first)
HAL_SPI_Write((uint8_t)((parameter >> 8) & 0xff));
HAL_SPI_Write((uint8_t)((parameter >> 16) & 0xff));
HAL_SPI_Write((uint8_t)((parameter >> 24) & 0xff));
HAL_SPI_Disable();
}
void wr16(uint32_t address, uint16_t parameter)
{
HAL_SPI_Enable();
HAL_SPI_Write((uint8_t)((address >> 16) | 0x80)); // RAM_REG = 0x302000 and high bit is set - result always 0xB0
HAL_SPI_Write((uint8_t)(address >> 8)); // Next byte of the register address
HAL_SPI_Write((uint8_t)address); // Low byte of register address - usually just the 1 byte offset
HAL_SPI_Write((uint8_t)(parameter & 0xff)); // Little endian (yes, it is most significant bit first and least significant byte first)
HAL_SPI_Write((uint8_t)(parameter >> 8));
HAL_SPI_Disable();
}
void wr8(uint32_t address, uint8_t parameter)
{
HAL_SPI_Enable();
HAL_SPI_Write((uint8_t)((address >> 16) | 0x80)); // RAM_REG = 0x302000 and high bit is set - result always 0xB0
HAL_SPI_Write((uint8_t)(address >> 8)); // Next byte of the register address
HAL_SPI_Write((uint8_t)(address)); // Low byte of register address - usually just the 1 byte offset
HAL_SPI_Write(parameter);
HAL_SPI_Disable();
}
uint32_t rd32(uint32_t address)
{
uint8_t buf[4];
uint32_t Data32;
HAL_SPI_Enable();
HAL_SPI_Write((address >> 16) & 0x3F);
HAL_SPI_Write((address >> 8) & 0xff);
HAL_SPI_Write(address & 0xff);
HAL_SPI_ReadBuffer(buf, 4);
HAL_SPI_Disable();
Data32 = buf[0] + ((uint32_t)buf[1] << 8) + ((uint32_t)buf[2] << 16) + ((uint32_t)buf[3] << 24);
return (Data32);
}
uint16_t rd16(uint32_t address)
{
uint8_t buf[2] = { 0,0 };
HAL_SPI_Enable();
HAL_SPI_Write((address >> 16) & 0x3F);
HAL_SPI_Write((address >> 8) & 0xff);
HAL_SPI_Write(address & 0xff);
HAL_SPI_ReadBuffer(buf, 2);
HAL_SPI_Disable();
uint16_t Data16 = buf[0] + ((uint16_t)buf[1] << 8);
return (Data16);
}
uint8_t rd8(uint32_t address)
{
uint8_t buf[1];
HAL_SPI_Enable();
HAL_SPI_Write((address >> 16) & 0x3F);
HAL_SPI_Write((address >> 8) & 0xff);
HAL_SPI_Write(address & 0xff);
HAL_SPI_ReadBuffer(buf, 1);
HAL_SPI_Disable();
return (buf[0]);
}
// *** Send_Cmd() - this is like cmd() in (some) Eve docs - sends 32 bits but does not update the write pointer ***
// FT81x Series Programmers Guide Section 5.1.1 - Circular Buffer (AKA "the FIFO" and "Command buffer" and "CoProcessor")
// Don't miss section 5.3 - Interaction with RAM_DL
void Send_CMD(uint32_t data)
{
wr32(FifoWriteLocation + RAM_CMD, data); // write the command at the globally tracked "write pointer" for the FIFO
FifoWriteLocation += FT_CMD_SIZE; // Increment the Write Address by the size of a command - which we just sent
FifoWriteLocation %= FT_CMD_FIFO_SIZE; // Wrap the address to the FIFO space
}
// UpdateFIFO - Cause the CoProcessor to realize that it has work to do in the form of a
// differential between the read pointer and write pointer. The CoProcessor (FIFO or "Command buffer") does
// nothing until you tell it that the write position in the FIFO RAM has changed
void UpdateFIFO(void)
{
wr16(REG_CMD_WRITE + RAM_REG, FifoWriteLocation); // We manually update the write position pointer
}
// Read the specific ID register and return TRUE if it is the expected 0x7C otherwise.
uint8_t Cmd_READ_REG_ID(void)
{
uint8_t readData[2];
HAL_SPI_Enable();
HAL_SPI_Write(0x30); // Base address RAM_REG = 0x302000
HAL_SPI_Write(0x20);
HAL_SPI_Write(REG_ID); // REG_ID offset = 0x00
HAL_SPI_ReadBuffer(readData, 1); // There was a dummy read of the first byte in there
HAL_SPI_Disable();
if (readData[0] == 0x7C) // FT81x Datasheet section 5.1, Table 5-2. Return value always 0x7C
{
// Log("\nGood ID: 0x%02x\n", readData[0]);
return 1;
}
else
{
// Log("0x%02x ", readData[0]);
return 0;
}
}
// **************************************** Co-Processor/GPU/FIFO/Command buffer Command Functions ***************
// These are discussed in FT81x Series Programmers Guide, starting around section 5.10
// While display list commands can be sent to the CoPro, these listed commands are specific to it. They are
// mostly widgets like graphs, but also touch related functions like cmd_track() and memory operations.
// Essentially, these commands set up parameters for CoPro functions which expand "macros" using those parameters
// to then write a series of commands into the Display List to create all the primitives which make that widget.
// ***************************************************************************************************************
// ******************** Screen Object Creation CoProcessor Command Functions ******************************
// *** Draw Slider - FT81x Series Programmers Guide Section 5.38 *************************************************
void Cmd_Slider(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t options, uint16_t val, uint16_t range)
{
Send_CMD(CMD_SLIDER);
Send_CMD( ((uint32_t)y << 16) | x );
Send_CMD( ((uint32_t)h << 16) | w );
Send_CMD( ((uint32_t)val << 16) | options );
Send_CMD( (uint32_t)range );
}
// *** Draw Spinner - FT81x Series Programmers Guide Section 5.54 *************************************************
void Cmd_Spinner(uint16_t x, uint16_t y, uint16_t style, uint16_t scale)
{
Send_CMD(CMD_SPINNER);
Send_CMD( ((uint32_t)y << 16) | x );
Send_CMD( ((uint32_t)scale << 16) | style );
}
// *** Draw Gauge - FT81x Series Programmers Guide Section 5.33 **************************************************
void Cmd_Gauge(uint16_t x, uint16_t y, uint16_t r, uint16_t options, uint16_t major, uint16_t minor, uint16_t val, uint16_t range)
{
Send_CMD(CMD_GAUGE);
Send_CMD( ((uint32_t)y << 16) | x );
Send_CMD( ((uint32_t)options << 16) | r );
Send_CMD( ((uint32_t)minor << 16) | major );
Send_CMD( ((uint32_t)range << 16) | val );
}
// *** Draw Dial - FT81x Series Programmers Guide Section 5.39 **************************************************
// This is much like a Gauge except for the helpful range parameter. For some reason, all dials are 65535 around.
void Cmd_Dial(uint16_t x, uint16_t y, uint16_t r, uint16_t options, uint16_t val)
{
Send_CMD(CMD_DIAL);
Send_CMD( ((uint32_t)y << 16) | x );
Send_CMD( ((uint32_t)options << 16) | r );
Send_CMD( (uint32_t)val );
}
// *** Make Track (for a slider) - FT81x Series Programmers Guide Section 5.62 ************************************
// tag refers to the tag # previously assigned to the object that this track is tracking.
void Cmd_Track(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t tag)
{
Send_CMD(CMD_TRACK);
Send_CMD( ((uint32_t)y << 16) | x );
Send_CMD( ((uint32_t)h << 16) | w );
Send_CMD( (uint32_t)tag );
}
// *** Draw Number - FT81x Series Programmers Guide Section 5.43 *************************************************
void Cmd_Number(uint16_t x, uint16_t y, uint16_t font, uint16_t options, uint32_t num)
{
Send_CMD(CMD_NUMBER);
Send_CMD( ((uint32_t)y << 16) | x );
Send_CMD( ((uint32_t)options << 16) | font );
Send_CMD(num);
}
// *** Draw Smooth Color Gradient - FT81x Series Programmers Guide Section 5.34 **********************************
void Cmd_Gradient(uint16_t x0, uint16_t y0, uint32_t rgb0, uint16_t x1, uint16_t y1, uint32_t rgb1)
{
Send_CMD(CMD_GRADIENT);
Send_CMD( ((uint32_t)y0<<16)|x0 );
Send_CMD(rgb0);
Send_CMD( ((uint32_t)y1<<16)|x1 );
Send_CMD(rgb1);
}
// *** Draw Button - FT81x Series Programmers Guide Section 5.28 **************************************************
void Cmd_Button(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t font, uint16_t options, const char* str)
{
uint16_t DataPtr, LoopCount, StrPtr;
uint16_t length = (uint16_t)strlen(str);
if(!length)
return;
uint32_t* data = (uint32_t*) calloc((length/4)+1, sizeof(uint32_t));
StrPtr = 0;
for(DataPtr=0; DataPtr<(length/4); DataPtr++, StrPtr += 4)
data[DataPtr] = (uint32_t)str[StrPtr+3]<<24 | (uint32_t)str[StrPtr+2]<<16 | (uint32_t)str[StrPtr+1]<<8 | (uint32_t)str[StrPtr];
for(LoopCount=0; LoopCount<(length%4); LoopCount++, StrPtr++)
data[DataPtr] |= (uint32_t)str[StrPtr] << (LoopCount * 8);
Send_CMD(CMD_BUTTON);
Send_CMD( ((uint32_t)y << 16) | x ); // Put two 16 bit values together into one 32 bit value - do it little endian
Send_CMD( ((uint32_t)h << 16) | w );
Send_CMD( ((uint32_t)options << 16) | font );
for (LoopCount = 0; LoopCount <= length / 4; LoopCount++)
{
printf("%.8x\n", data[LoopCount]);
Send_CMD(data[LoopCount]);
}
free(data);
}
// *** Draw Text - FT81x Series Programmers Guide Section 5.41 ***************************************************
void Cmd_Text(uint16_t x, uint16_t y, uint16_t font, uint16_t options, const char* str)
{
uint16_t DataPtr, LoopCount, StrPtr;
uint16_t length = (uint16_t) strlen(str);
if(!length)
return;
uint32_t* data = (uint32_t*) calloc((length / 4) + 1, sizeof(uint32_t)); // Allocate memory for the string expansion
StrPtr = 0;
for(DataPtr=0; DataPtr<(length/4); ++DataPtr, StrPtr=StrPtr+4)
data[DataPtr] = (uint32_t)str[StrPtr+3]<<24 | (uint32_t)str[StrPtr+2]<<16 | (uint32_t)str[StrPtr+1]<<8 | (uint32_t)str[StrPtr];
for(LoopCount=0; LoopCount<(length%4); ++LoopCount, ++StrPtr)
data[DataPtr] |= (uint32_t)str[StrPtr] << (LoopCount*8);
// Set up the command
Send_CMD(CMD_TEXT);
Send_CMD( ((uint32_t)y << 16) | x );
Send_CMD( ((uint32_t)options << 16) | font );
// Send out the text
for(LoopCount = 0; LoopCount <= length/4; LoopCount++)
Send_CMD(data[LoopCount]); // These text bytes get sucked up 4 at a time and fired at the FIFO
free(data);
}
// ******************** Miscellaneous Operation CoProcessor Command Functions ******************************
// *** Cmd_SetBitmap - generate DL commands for bitmap parms - FT81x Series Programmers Guide Section 5.65 *******
void Cmd_SetBitmap(uint32_t addr, uint16_t fmt, uint16_t width, uint16_t height)
{
Send_CMD( CMD_SETBITMAP );
Send_CMD( addr );
Send_CMD( ((uint32_t)width << 16) | fmt );
Send_CMD( (uint32_t)height);
}
// *** Cmd_Memcpy - background copy a block of data - FT81x Series Programmers Guide Section 5.27 ****************
void Cmd_Memcpy(uint32_t dest, uint32_t src, uint32_t num)
{
Send_CMD(CMD_MEMCPY);
Send_CMD(dest);
Send_CMD(src);
Send_CMD(num);
}
// *** Cmd_GetPtr - Get the last used address from CoPro operation - FT81x Series Programmers Guide Section 5.47 *
void Cmd_GetPtr(void)
{
Send_CMD(CMD_GETPTR);
Send_CMD(0);
}
// *** Set Highlight Gradient Color - FT81x Series Programmers Guide Section 5.32 ********************************
void Cmd_GradientColor(uint32_t c)
{
Send_CMD(CMD_GRADCOLOR);
Send_CMD(c);
}
// *** Set FG color - FT81x Series Programmers Guide Section 5.30 ************************************************
void Cmd_FGcolor(uint32_t c)
{
Send_CMD(CMD_FGCOLOR);
Send_CMD(c);
}
// *** Set BG color - FT81x Series Programmers Guide Section 5.31 ************************************************
void Cmd_BGcolor(uint32_t c)
{
Send_CMD(CMD_BGCOLOR);
Send_CMD(c);
}
// *** Translate Matrix - FT81x Series Programmers Guide Section 5.51 ********************************************
void Cmd_Translate(uint32_t tx, uint32_t ty)
{
Send_CMD(CMD_TRANSLATE);
Send_CMD(tx);
Send_CMD(ty);
}
// *** Rotate Matrix - FT81x Series Programmers Guide Section 5.50 ***********************************************
void Cmd_Rotate(uint32_t a)
{
Send_CMD(CMD_ROTATE);
Send_CMD(a);
}
// *** Rotate Screen - FT81x Series Programmers Guide Section 5.53 ***********************************************
void Cmd_SetRotate(uint32_t rotation)
{
Send_CMD(CMD_SETROTATE);
Send_CMD(rotation);
}
// *** Scale Matrix - FT81x Series Programmers Guide Section 5.49 ************************************************
void Cmd_Scale(uint32_t sx, uint32_t sy)
{
Send_CMD(CMD_SCALE);
Send_CMD(sx);
Send_CMD(sy);
}
void Cmd_Flash_Fast(void)
{
Send_CMD(CMD_FLASHFAST);
Send_CMD(0);
}
// *** Calibrate Touch Digitizer - FT81x Series Programmers Guide Section 5.52 ***********************************
// * This business about "result" in the manual really seems to be simply leftover cruft of no purpose - send zero
void Cmd_Calibrate(uint32_t result)
{
Send_CMD(CMD_CALIBRATE);
Send_CMD(result);
}
// An interactive calibration screen is created and executed.
// New calibration values are written to the touch matrix registers of Eve.
void Calibrate_Manual(uint16_t Width, uint16_t Height, uint16_t V_Offset, uint16_t H_Offset)
{
uint32_t displayX[3], displayY[3];
uint32_t touchX[3], touchY[3];
uint32_t touchValue = 0, storedValue = 0;
int32_t tmp, k;
int32_t TransMatrix[6];
uint8_t count = 0;
uint8_t pressed = 0;
char num[2];
// These values determine where your calibration points will be drawn on your display
displayX[0] = (uint32_t) (Width * 0.15) + H_Offset;
displayY[0] = (uint32_t) (Height * 0.15) + V_Offset;
displayX[1] = (uint32_t) (Width * 0.85) + H_Offset;
displayY[1] = (uint32_t) (Height / 2) + V_Offset;
displayX[2] = (uint32_t) (Width / 2) + H_Offset;
displayY[2] = (uint32_t) (Height * 0.85) + V_Offset;
while (count < 3)
{
Send_CMD(CMD_DLSTART);
Send_CMD(CLEAR_COLOR_RGB(0, 0, 0));
Send_CMD(CLEAR(1,1,1));
// Draw Calibration Point on screen
Send_CMD(COLOR_RGB(255, 0, 0));
Send_CMD(POINT_SIZE(20 * 16));
Send_CMD(BEGIN(POINTS));
Send_CMD(VERTEX2F((uint32_t)(displayX[count]) * 16, (uint32_t)((displayY[count])) * 16));
Send_CMD(END());
Send_CMD(COLOR_RGB(255, 255, 255));
Cmd_Text((Width / 2) + H_Offset, (Height / 3) + V_Offset, 27, OPT_CENTER, "Calibrating");
Cmd_Text((Width / 2) + H_Offset, (Height / 2) + V_Offset, 27, OPT_CENTER, "Please tap the dots");
num[0] = count + 0x31; num[1] = 0; // null terminated string of one character
Cmd_Text(displayX[count], displayY[count], 27, OPT_CENTER, num);
Send_CMD(DISPLAY());
Send_CMD(CMD_SWAP);
UpdateFIFO(); // Trigger the CoProcessor to start processing commands out of the FIFO
Wait4CoProFIFOEmpty(); // wait here until the coprocessor has read and executed every pending command.
HAL_Delay(300);
while (pressed == count)
{
touchValue = rd32(REG_TOUCH_DIRECT_XY + RAM_REG); // Read for any new touch tag inputs
if (!(touchValue & 0x80000000))
{
touchX[count] = (touchValue >> 16) & 0x03FF; // Raw Touchscreen Y coordinate
touchY[count] = touchValue & 0x03FF; // Raw Touchscreen Y coordinate
//Log("\ndisplay x[%d]: %ld display y[%d]: %ld\n", count, displayX[count], count, displayY[count]);
//Log("touch x[%d]: %ld touch y[%d]: %ld\n", count, touchX[count], count, touchY[count]);
count++;
}
}
pressed = count;
}
k = ((touchX[0] - touchX[2])*(touchY[1] - touchY[2])) - ((touchX[1] - touchX[2])*(touchY[0] - touchY[2]));
tmp = (((displayX[0] - displayX[2]) * (touchY[1] - touchY[2])) - ((displayX[1] - displayX[2])*(touchY[0] - touchY[2])));
TransMatrix[0] = ((int64_t)tmp << 16) / k;
tmp = (((touchX[0] - touchX[2]) * (displayX[1] - displayX[2])) - ((displayX[0] - displayX[2])*(touchX[1] - touchX[2])));
TransMatrix[1] = ((int64_t)tmp << 16) / k;
tmp = ((touchY[0] * (((touchX[2] * displayX[1]) - (touchX[1] * displayX[2])))) + (touchY[1] * (((touchX[0] * displayX[2]) - (touchX[2] * displayX[0])))) + (touchY[2] * (((touchX[1] * displayX[0]) - (touchX[0] * displayX[1])))));
TransMatrix[2] = ((int64_t)tmp << 16) / k;
tmp = (((displayY[0] - displayY[2]) * (touchY[1] - touchY[2])) - ((displayY[1] - displayY[2])*(touchY[0] - touchY[2])));
TransMatrix[3] = ((int64_t)tmp << 16) / k;
tmp = (((touchX[0] - touchX[2]) * (displayY[1] - displayY[2])) - ((displayY[0] - displayY[2])*(touchX[1] - touchX[2])));
TransMatrix[4] = ((int64_t)tmp << 16) / k;
tmp = ((touchY[0] * (((touchX[2] * displayY[1]) - (touchX[1] * displayY[2])))) + (touchY[1] * (((touchX[0] * displayY[2]) - (touchX[2] * displayY[0])))) + (touchY[2] * (((touchX[1] * displayY[0]) - (touchX[0] * displayY[1])))));
TransMatrix[5] = ((int64_t)tmp << 16) / k;
count = 0;
do
{
wr32(REG_TOUCH_TRANSFORM_A + RAM_REG + (count * 4), TransMatrix[count]); // Write to Eve config registers
// uint16_t ValH = TransMatrix[count] >> 16;
// uint16_t ValL = TransMatrix[count] & 0xFFFF;
// Log("TM%d: 0x%04x %04x\n", count, ValH, ValL);
count++;
}while(count < 6);
}
// ***************************************************************************************************************
// *** Animation functions ***************************************************************************************
// ***************************************************************************************************************
void Cmd_AnimStart(int32_t ch, uint32_t aoptr, uint32_t loop)
{
Send_CMD(CMD_ANIMSTART);
Send_CMD(ch);
Send_CMD(aoptr);
Send_CMD(loop);
}
void Cmd_AnimStop(int32_t ch)
{
Send_CMD(CMD_ANIMSTOP);
Send_CMD(ch);
}
void Cmd_AnimXY(int32_t ch, int16_t x, int16_t y)
{
Send_CMD(CMD_ANIMXY);
Send_CMD(ch);
Send_CMD(((uint32_t)y << 16) | x);
}
void Cmd_AnimDraw(int32_t ch)
{
Send_CMD(CMD_ANIMDRAW);
Send_CMD(ch);
}
void Cmd_AnimDrawFrame(int16_t x, int16_t y, uint32_t aoptr, uint32_t frame)
{
Send_CMD(CMD_ANIMFRAME);
Send_CMD(((uint32_t)y << 16) | x);
Send_CMD(aoptr);
Send_CMD(frame);
}
void Cmd_RomFont(uint32_t font, uint32_t romslot)
{
Send_CMD(CMD_ROMFONT);
Send_CMD(font);
Send_CMD(romslot);
}
// ***************************************************************************************************************
// *** Utility and helper functions ******************************************************************************
// ***************************************************************************************************************
// Find the space available in the GPU AKA CoProcessor AKA command buffer AKA FIFO
uint16_t CoProFIFO_FreeSpace(void)
{
uint16_t cmdBufferDiff, cmdBufferRd, cmdBufferWr, retval;
cmdBufferRd = rd16(REG_CMD_READ + RAM_REG);
cmdBufferWr = rd16(REG_CMD_WRITE + RAM_REG);
cmdBufferDiff = (cmdBufferWr-cmdBufferRd) % FT_CMD_FIFO_SIZE; // FT81x Programmers Guide 5.1.1
retval = (FT_CMD_FIFO_SIZE - 4) - cmdBufferDiff;
return (retval);
}
// Sit and wait until there are the specified number of bytes free in the <GPU/CoProcessor> incoming FIFO
void Wait4CoProFIFO(uint32_t room)
{
uint16_t getfreespace;
do {
getfreespace = CoProFIFO_FreeSpace();
}while(getfreespace < room);
}
// Sit and wait until the CoPro FIFO is empty
// Detect operational errors and print the error and stop.
void Wait4CoProFIFOEmpty(void)
{
uint16_t ReadReg;
uint8_t ErrChar;
uint8_t buffy[2];
do
{
ReadReg = rd16(REG_CMD_READ + RAM_REG);
if(ReadReg == 0xFFF)
{
// this is a error which would require sophistication to fix and continue but we fake it somewhat unsuccessfully
Log("\n");
uint8_t Offset = 0;
do
{
// Get the error character and display it
ErrChar = rd8(RAM_ERR_REPORT + Offset);
Offset++;
sprintf(buffy, "%c", ErrChar);
Log(buffy);
}while ( (ErrChar != 0) && (Offset < 128) ); // when the last stuffed character was null, we are done
Log("\n");
// Eve is unhappy - needs a paddling.
uint32_t Patch_Add = rd32(REG_COPRO_PATCH_PTR + RAM_REG);
wr8(REG_CPU_RESET + RAM_REG, 1);
wr8(REG_CMD_READ + RAM_REG, 0);
wr8(REG_CMD_WRITE + RAM_REG, 0);
wr8(REG_CMD_DL + RAM_REG, 0);
wr8(REG_CPU_RESET + RAM_REG, 0);
wr32(REG_COPRO_PATCH_PTR + RAM_REG, Patch_Add);
HAL_Delay(250); // we already saw one error message and we don't need to see then 1000 times a second
}
}while( ReadReg != rd16(REG_CMD_WRITE + RAM_REG) );
}
// Every CoPro transaction starts with enabling the SPI and sending an address
void StartCoProTransfer(uint32_t address, uint8_t reading)
{
HAL_SPI_Enable();
if (reading){
HAL_SPI_Write(address >> 16);