-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAntares.c
2183 lines (1825 loc) · 80.8 KB
/
Antares.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
//Antares.h - FTDI/Bridgetek Eve 2,3,4 driver for Propeller2 and FlexSpin C
//Copyright 2020-2021 Raymond Allen, MIT licence (except parts from "gd2-lib", which have BSD-3 license)
//This code is based on "gd2-lib" by James Bowman (BSD-3 license) and "EVE2-Library" by Matrix Orbital (MIT license)
//We are very appreciative of these codes
//"gd2-lib" and license info can be found here: https://github.com/jamesbowman/gd2-lib
//"EVE2-Library" and license info can be found here: https://github.com/MatrixOrbital/EVE2-Library
//Do we want to use a cog for SPI comms to get faster speed?
//#define USECOGFORSPI
#ifndef USECOGFORSPI
#include "SPI.c" //This version doesn't use a cog
Spi SPI;
#else
struct __using("SPI.spin2") SPI; //RJA: //This version uses a cog //Visual Studio will show an error here, but it's OK
#endif // !USECOGFORSPI
// This list of headers is for functionality required by this library and should not change. Also fine for Arduino.
#include <stdint.h> // Find integer types like "uint8_t"
#include <stdbool.h> // bool
#include <stdlib.h> // calloc(), free()
#include <string.h> // strlen()
#include "Antares.h" //RJA: This circular include not needed, but added so code editor doesn't mark some things as undefined
// A manual calibration screen for the touch digitizer
void Calibrate(void)
{
//Pick one of the two methods below for calibration
Calibrate_Manual(Display_Width(), Display_Height(), Display_VOffset(), Display_HOffset()); //Matrix Orbital calibration code
//MakeScreen_Calibrate(); //Eve's built in way of calibrating
swap(); //get display back on track, ready for next screen
}
// Using EVE to do it's own touch screen calibration
void MakeScreen_Calibrate(void)
{
Log("Enter Calibrate\n");
Send_CMD(CMD_DLSTART);
Send_CMD(CLEAR_COLOR_RGB(0, 0, 0));
Send_CMD(CLEAR(1, 1, 1));
Cmd_Text(100, 240, 27, OPT_CENTER, "Tap on the dots");
Cmd_Calibrate(0); // This widget generates a blocking screen that doesn't unblock until 3 dots have been touched
Send_CMD(DISPLAY());
Send_CMD(CMD_SWAP);
UpdateFIFO(); // Trigger the CoProcessor to start processing commands out of the FIFO
Wait4CoProFIFOEmptyCalibration(); // wait here until the coprocessor has read and executed every pending command.
Log("Leaving Calibrate\n");
}
void Eve_Reset_HW(void)
{//Power Cycle EVE
printf("Power Cycling EVE\n");
if (FT800_PD_N < 0) //Note: Compile errors starting here probably means you don't have a board defined in Platform.h
{//dazzler doesn't have access to PDn, but need to bring other CSs high
_pinh(Dazzler_CS); //dazzler doesn't have access to this
_pinh(DazzlerSD_CS);
return;
}
//for (int i=0;i<500000;i++)
{
_pinl(FT800_PD_N);
MyDelay(50);
_pinh(FT800_PD_N);
MyDelay(200);
}
}
void SPI_Enable()
{
_pinl(FT800_CS);
}
void SPI_Disable()
{//Raise Chip select
#ifdef USECOGFORSPI //For the cogged version, need to wait for command to complete before raising CS
SPI.SendCommand(0,0); //wait until it's finished
#endif
_pinh(FT800_CS);
}
void SPI_Write(unsigned char c)
{//send byte over spi
SPI.Write8(FT800_MOSI, FT800_CLK, c & 0xFF);
}
void SPI_WriteBuffer(uint8_t* buff, int TransferSize)
{//send bytes over spi
SPI.WriteN(FT800_MOSI, FT800_CLK, buff, TransferSize);
}
void SPI_ReadBuffer(unsigned char *pBuffer, int n)
{//read bytes over spi
//unsigned char c = SPI.SHIFTIN(FT800_MISO, FT800_CLK, SPI.MSBPRE, 8); //RJA dummy read (could also have been a dummy write, as in FTDI's Gpu_Hal_StartTransfer())
unsigned char c = SPI.Read8(FT800_MISO, FT800_CLK); //RJA dummy read (could also have been a dummy write, as in FTDI's Gpu_Hal_StartTransfer())
for (int i = 0; i < n; i++)
{
pBuffer[i] = SPI.Read8(FT800_MISO, FT800_CLK);//RJA trying to speed it up SPI.SHIFTIN(FT800_MISO, FT800_CLK, SPI.MSBPRE, 8);
}
}
// Call this function once at powerup to reset and initialize the Eve chip
void FT81x_Init(void)
{
////REMOVE ME
//_drvh(13); //PSAVE: select EVE as source for sync signals and not P2 on Eval Board Adapter
//Start up P2 SPI driver
//for cogless version of SPI: With clockdelay==0, get max bitrate ~6 MHz with 297 MHz P2 clock
//for cogged version of SPI: With clockdelay==0, get max bitrate ~24 MHz with 297 MHz P2 clock
SPI.Start(10+0, 0); //(ClockDelay, ClockState)
printf("SPI Started\n");
Eve_Reset(); // Hard reset of the Eve chip
printf("EVE powercycle complete.\n");
uint32_t Ready = false;
//Turn on 12.375 MHz pin output for EVE, if needed
#ifdef NCOPIN
__asm {
wrpin #(P_NCO_FREQ | P_TT_11), #NCOPIN //smartpin NCO // 0b00000000000000000000000011001100, #NCOPIN //smartpin NCO
WXPIN #NCOXPIN, #NCOPIN//##$0000_0080,#45'55 //base period
WYPIN ##NCOYPIN, #NCOPIN//##$2000_0000,#45'55 //add amount per clock
drvh #NCOPIN
}
#endif
// Wakeup Eve
printf("Waking up EVE.\n");
HostCommand(HCMD_SLEEP);
HostCommand(HCMD_CLKEXT);
MyDelay(300);
//HostCommand(HCMD_CLKINT);
HostCommand(HCMD_ACTIVE);
MyDelay(300);
//Change to 72 MHz (Note: For VGA also possible to do 48 MHz with PCLK lowered from 3 to 2)
HostCommand(HCMD_SLEEP);
#ifdef Set72MHzEveClock
HostCommand3((uint32_t)0x61 | (0x40 << 8) | (0x06 << 8)); //72MHz
#else
HostCommand3((uint32_t)0x61 | (0x40 << 8) | (0x05 << 8)); //60 MHz
#endif //Set72MHzEveClock
HostCommand(HCMD_ACTIVE);
MyDelay(300);
do
{
Ready = Cmd_READ_REG_ID();
}while (!Ready);
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);
#ifdef Set72MHzEveClock
wr32(REG_FREQUENCY + RAM_REG, 0x44AA200);//72 MHz // Configure the system clock to 72MHz
#else
wr32(REG_FREQUENCY + RAM_REG, 0x3938700); // Configure the system clock to 60MHz
#endif //Set72MHzEveClock
// Before we go any further with Eve, it is a good idea to check to see if it 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.
uint16_t Patch_Add = rd16(REG_COPRO_PATCH_PTR + RAM_REG);
wr32(REG_CPU_RESET + RAM_REG, 1);
wr32(REG_CMD_READ + RAM_REG, 0);
wr32(REG_CMD_WRITE + RAM_REG, 0);
wr32(REG_CMD_DL + RAM_REG, 0);
wr32(REG_PCLK + RAM_REG, 0);
FifoWriteLocation = 0; // reset our marker tracker variables
wr32(REG_CPU_RESET + RAM_REG, 0);
MyDelay(100);
wr16(REG_COPRO_PATCH_PTR + RAM_REG, Patch_Add);
Log( "Eve Needed Whacking\n" );
printf("Eve Needed Whacking\n");
}
// 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
#ifdef ResistiveTouch
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
#endif
#ifdef CapacitiveTouch
wr8(REG_CTOUCH_MODE + RAM_REG, 0x03); // set touch on: continous - this is default
#endif
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, 0x00); // Backlight PWM duty (off)
//New for EVE4
//wr32(0x614 + RAM_REG, 0x08c1);// 0x8c1);//REG_PCLK_FREQ
//wr32(0x618 + RAM_REG, 0x1);//REG_PCLK_2X
//Send_CMD(CMD_PCLKFREQ);
//Send_CMD(72000000);
//Send_CMD(00); //nearest
//Send_CMD(0);
//UpdateFIFO();
//Wait4CoProFIFOEmpty();
// 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 (as soon as we turn backlight on)
Log("First screen written\n");
//RJA: Turn on backlight
wr8(REG_PWM_DUTY + RAM_REG, BacklightSetting); // Backlight PWM duty (on)
////turn off audio and set volume to midrange
wr8(REG_VOL_SOUND + RAM_REG, 128);
wr8(REG_PLAY + RAM_REG, 0);
Ready = rd32(REG_FREQUENCY + RAM_REG);
printf("Clock freq = %d\n", Ready);
}
// Reset Eve chip via the hardware PDN line
void Eve_Reset(void)
{
Eve_Reset_HW();
}
// *** 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");
SPI_Enable();
/* SPI_Write(HCMD | 0x40); // In case the manual is making you believe that you just found the bug you were looking for - no. */
SPI_Write(HCMD);
SPI_Write(0x00); // This second byte is set to 0 but if there is need for fancy, never used setups, then rewrite.
SPI_Write(0x00);
SPI_Disable();
}
///***************************************************************************
//* Interface Description : This API sends a 3byte command from host to EVE
//* Implementation :
//* Return Value : void
//* Author :
//****************************************************************************/
//void Gpu_HostCommand_Ext3(Gpu_Hal_Context_t* host, uint32_t cmd)
//{
// _pinl(host->hal_config.spi_cs_pin_no);
// SPI.SHIFTOUT(FT800_MOSI, FT800_CLK, SPI.MSBFIRST, 8, cmd & 0xFF); //RJA needs to see what is going on here... Not something I did in Spin2 version...
// SPI.SHIFTOUT(FT800_MOSI, FT800_CLK, SPI.MSBFIRST, 8, (cmd >> 8) & 0xff);
// SPI.SHIFTOUT(FT800_MOSI, FT800_CLK, SPI.MSBFIRST, 8, (cmd >> 16) & 0xff);
// _pinh(host->hal_config.spi_cs_pin_no);
//
//}
void HostCommand3(uint32_t cmd)
{//RJA adding this one to write 3 bytes
// Log("Inside HostCommand\n");
SPI_Enable();
/* SPI_Write(HCMD | 0x40); // In case the manual is making you believe that you just found the bug you were looking for - no. */
SPI_Write(cmd & 0xFF);
SPI_Write((cmd >> 8) & 0xff); // This second byte is set to 0 but if there is need for fancy, never used setups, then rewrite.
SPI_Write((cmd >> 16) & 0xff);
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)
{
SPI_Enable();
SPI_Write((uint8_t)((address >> 16) | 0x80)); // RAM_REG = 0x302000 and high bit is set - result always 0xB0
SPI_Write((uint8_t)(address >> 8)); // Next byte of the register address
SPI_Write((uint8_t)address); // Low byte of register address - usually just the 1 byte offset
SPI_Write((uint8_t)(parameter & 0xff)); // Little endian (yes, it is most significant bit first and least significant byte first)
SPI_Write((uint8_t)((parameter >> 8) & 0xff));
SPI_Write((uint8_t)((parameter >> 16) & 0xff));
SPI_Write((uint8_t)((parameter >> 24) & 0xff));
SPI_Disable();
}
void wr16(uint32_t address, uint16_t parameter)
{
SPI_Enable();
SPI_Write((uint8_t)((address >> 16) | 0x80)); // RAM_REG = 0x302000 and high bit is set - result always 0xB0
SPI_Write((uint8_t)(address >> 8)); // Next byte of the register address
SPI_Write((uint8_t)address); // Low byte of register address - usually just the 1 byte offset
SPI_Write((uint8_t)(parameter & 0xff)); // Little endian (yes, it is most significant bit first and least significant byte first)
SPI_Write((uint8_t)(parameter >> 8) & 0xff);
SPI_Disable();
}
void wr8(uint32_t address, uint8_t parameter)
{
SPI_Enable();
SPI_Write((uint8_t)((address >> 16) | 0x80)); // RAM_REG = 0x302000 and high bit is set - result always 0xB0
SPI_Write((uint8_t)(address >> 8)); // Next byte of the register address
SPI_Write((uint8_t)(address)); // Low byte of register address - usually just the 1 byte offset
SPI_Write(parameter);
SPI_Disable();
}
uint32_t rd32(uint32_t address)
{
uint8_t buf[4];
uint32_t Data32;
SPI_Enable();
SPI_Write((address >> 16) & 0x3F);
SPI_Write((address >> 8) & 0xff);
SPI_Write(address & 0xff);
SPI_ReadBuffer(buf, 4);
SPI_Disable();
//printf("RD32 gives %d, %d, %d, %d", buf[0], buf[1], buf[2], buf[3]);
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];
SPI_Enable();
SPI_Write((address >> 16) & 0x3F);
SPI_Write((address >> 8) & 0xff);
SPI_Write(address & 0xff);
SPI_ReadBuffer(buf, 2);
SPI_Disable();
//uint16_t Data16 = buf[0] + ((uint16_t)buf[1] << 8);
return ((uint16_t)*buf);// Data16);
}
uint8_t rd8(uint32_t address)
{
uint8_t buf[1];
SPI_Enable();
SPI_Write((address >> 16) & 0x3F);
SPI_Write((address >> 8) & 0xff);
SPI_Write(address & 0xff);
SPI_ReadBuffer(buf, 1);
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];
SPI_Enable();
SPI_Write(0x30); // Base address RAM_REG = 0x302000
SPI_Write(0x20);
SPI_Write(REG_ID); // REG_ID offset = 0x00
SPI_ReadBuffer(readData, 1); // There was a dummy read of the first byte in there
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("need %d, got %d\n", 0x7C, readData[0]);
//Log("need 0x7C, got 0x%02x \n", 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);
}
void Cmd_AnimDraw(int8_t AnimID)
{
Send_CMD(CMD_ANIMDRAW);
Send_CMD( (int32_t)AnimID );
}
void Cmd_AnimDrawFrame(uint32_t addr, uint16_t Xpos, uint16_t Ypos, uint8_t Frame)
{
Send_CMD(CMD_ANIMFRAME);
Send_CMD(((uint32_t)Ypos << 16) + Xpos);
Send_CMD(addr);
Send_CMD( (uint32_t)Frame );
}
// *** 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 = 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++)
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 = 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_SetFont(uint32_t font, uint32_t ptr) //Added by RJA
{
Send_CMD(CMD_SETFONT);
Send_CMD(font);
Send_CMD(ptr);
}
void Cmd_SetFont2(uint32_t font, uint32_t ptr, uint32_t firstchar) //Added by RJA
{
Send_CMD(CMD_SETFONT2);
Send_CMD(font);
Send_CMD(ptr);
Send_CMD(firstchar);
}
// *** 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;
char num[2];
uint8_t touch_lock = 1;
// These values determine where your calibration points will be drawn on your display
displayX[0] = (Width * 0.15) + H_Offset;
displayY[0] = (Height * 0.15) + V_Offset;
displayX[1] = (Width * 0.85) + H_Offset;
displayY[1] = (Height / 2) + V_Offset;
displayX[2] = (Width / 2) + H_Offset;
displayY[2] = (Height * 0.85) + V_Offset;
while (count < 3)
{
Send_CMD(CMD_DLSTART);
Send_CMD(CLEAR_COLOR_RGB(64, 64, 64));
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.
while(1)
{
touchValue = rd32(REG_TOUCH_DIRECT_XY + RAM_REG); // Read for any new touch tag inputs
if(touch_lock)
{
if(touchValue & 0x80000000) // check if we have no touch
{
touch_lock = 0;
}
}
else
{
if (!(touchValue & 0x80000000)) // check if a touch is detected
{
touchX[count] = (touchValue>>16) & 0x03FF; // Raw Touchscreen Y coordinate
touchY[count] = touchValue & 0x03FF; // Raw Touchscreen Y coordinate
touch_lock = 1;
count++;
break; // leave while(1)
}
}
}
}
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] = CalcCoef(tmp, k);
tmp = (((touchX[0] - touchX[2]) * (displayX[1] - displayX[2])) - ((displayX[0] - displayX[2])*(touchX[1] - touchX[2])));
TransMatrix[1] = CalcCoef(tmp, 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] = CalcCoef(tmp, k);
tmp = (((displayY[0] - displayY[2]) * (touchY[1] - touchY[2])) - ((displayY[1] - displayY[2])*(touchY[0] - touchY[2])));
TransMatrix[3] = CalcCoef(tmp, k);
tmp = (((touchX[0] - touchX[2]) * (displayY[1] - displayY[2])) - ((displayY[0] - displayY[2])*(touchX[1] - touchX[2])));
TransMatrix[4] = CalcCoef(tmp, 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] = CalcCoef(tmp, 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);
#ifdef UseFilesForTouchCalibration
count = 0;
f = fopen("ResTouch.dat", "wb");
if (f)
{
printf("File, ResTouch.dat, opened for writing.\n");
count = fwrite(TransMatrix, 1, 6 * 4, f);
fclose(f);
}
if (count == 6 * 4)
printf("Resistive calibration saved to file.\n");
else
printf("Write to Resistive calibration file failed, only %d bytes written\n",count);
#endif //UseFilesForTouchCalibration
}
// ***************************************************************************************************************
// *** 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();
//usleep(100);
}while(getfreespace < room);
}
// Sit and wait until the CoPro FIFO is empty
// Detect operational errors and print the error and stop.
bool Wait4CoProFIFOEmpty(void)
{
uint16_t ReadReg;
uint16_t WriteReg;
uint8_t ErrChar;
uint8_t buffy[2];
uint8_t LoopCount = 0;
WriteReg = rd16(REG_CMD_WRITE + RAM_REG);
do
{
ReadReg = rd16(REG_CMD_READ + RAM_REG);
if(ReadReg == 0xFFF)
{
Log("\nW4CPFE: ");
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.
uint16_t Patch_Add = rd16( REG_COPRO_PATCH_PTR + RAM_REG );
wr32( REG_CPU_RESET + RAM_REG, 1 );
wr32( REG_CMD_READ + RAM_REG, 0 );
wr32( REG_CMD_WRITE + RAM_REG, 0 );
wr32( REG_CMD_DL + RAM_REG, 0 );
wr32( REG_PCLK + RAM_REG, 0 );
WriteReg = FifoWriteLocation = 0; // reset our marker tracker variables
wr32( REG_CPU_RESET + RAM_REG, 0 );
MyDelay( 100 );
wr16( REG_COPRO_PATCH_PTR + RAM_REG, Patch_Add );
// If this is a BT81x and you were previously in fast flash mode, you will not be any more
// - probably need to track the state and redo fast mode here.
}
if (LoopCount++ == 100) // 100 times around is waaay too much
{
Log("Eve stuck - W:%lu R:%lu\n", WriteReg, ReadReg);
return false;
}
MyDelay(2); // normalize operation for processor speed
}while( ReadReg != WriteReg );
return true;
}
// Sit and wait until the CoPro FIFO is empty
// Same as above, but without timeout
bool Wait4CoProFIFOEmptyCalibration(void)
{
uint16_t ReadReg;
uint16_t WriteReg;
uint8_t ErrChar;
uint8_t buffy[2];
uint8_t LoopCount = 0;
WriteReg = rd16(REG_CMD_WRITE + RAM_REG);
do
{
ReadReg = rd16(REG_CMD_READ + RAM_REG);
if (ReadReg == 0xFFF)
{
Log("\nW4CPFE: ");
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.
uint16_t Patch_Add = rd16(REG_COPRO_PATCH_PTR + RAM_REG);
wr32(REG_CPU_RESET + RAM_REG, 1);
wr32(REG_CMD_READ + RAM_REG, 0);
wr32(REG_CMD_WRITE + RAM_REG, 0);
wr32(REG_CMD_DL + RAM_REG, 0);
wr32(REG_PCLK + RAM_REG, 0);
WriteReg = FifoWriteLocation = 0; // reset our marker tracker variables
wr32(REG_CPU_RESET + RAM_REG, 0);
MyDelay(100);
wr16(REG_COPRO_PATCH_PTR + RAM_REG, Patch_Add);
// If this is a BT81x and you were previously in fast flash mode, you will not be any more
// - probably need to track the state and redo fast mode here.
}
MyDelay(2); // normalize operation for processor speed
} while (ReadReg != WriteReg);
return true;
}
// Every CoPro transaction starts with enabling the SPI and sending an address
void StartCoProTransfer(uint32_t address, uint8_t reading)
{
SPI_Enable();
if (reading){
SPI_Write(address >> 16);
SPI_Write(address >> 8);