forked from RudolphRiedel/FT800-FT813
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEVE_commands.c
3946 lines (3599 loc) · 132 KB
/
EVE_commands.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
/*
@file EVE_commands.c
@brief contains FT8xx / BT8xx functions
@version 5.0
@date 2024-02-20
@author Rudolph Riedel
@section info
At least for Arm Cortex-M0 and Cortex-M4 I have fastest execution with -O2.
The c-standard is C99.
@section LICENSE
MIT License
Copyright (c) 2016-2024 Rudolph Riedel
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@section History
5.0
- added EVE_cmd_pclkfreq()
- put back writing of REG_CSSPREAD as it needs to be deactivated for higher frequencies
- added the configuration of the second PLL for the pixel clock in BT817/BT818 to EVE_init() in case the display config
has EVE_PCLK_FREQ defined
- replaced BT81X_ENABLE with "EVE_GEN > 2"
- removed FT81X_ENABLE as FT81x already is the lowest supported chip revision now
- removed the formerly as deprected marked EVE_get_touch_tag()
- changed EVE_color_rgb() to use a 32 bit value like the rest of the color commands
- removed the meta-commands EVE_cmd_point(), EVE_cmd_line() and EVE_cmd_rect()
- split all display-list commands into two functions: EVE_cmd_XXX() and EVE_cmd_XXX_burst()
- switched from using EVE_RAM_CMD + cmdOffset to REG_CMDB_WRITE
- as a side effect from switching to REG_CMDB_WRITE, every coprocessor command is automatically executed now
- renamed EVE_LIB_GetProps() back to EVE_cmd_getprops() since it does not do anything special to justify a special name
- added helper function EVE_memWrite_sram_buffer()
- added EVE_cmd_bitmap_transform() and EVE_cmd_bitmap_transform_burst()
- added zero-pointer protection to commands using block_transfer()
- added EVE_cmd_playvideo()
- changed EVE_cmd_setfont() to a display-list command and added EVE_cmd_setfont_burst()
- changed EVE_cmd_setfont2() to a display-list command and added EVE_cmd_setfont2_burst()
- added EVE_cmd_videoframe()
- restructured: functions are sorted by chip-generation and within their group in alphabetical order
- reimplementedEVE_cmd_getmatrix() again, it needs to read values, not write them
- added EVE_cmd_fontcache() and EVE_cmd_fontcachequery()
- added EVE_cmd_calibratesub()
- added EVE_cmd_animframeram(), EVE_cmd_animframeram_burst(), EVE_cmd_animstartram(), EVE_cmd_animstartram_burst()
- added EVE_cmd_apilevel(), EVE_cmd_apilevel_burst()
- added EVE_cmd_calllist(), EVE_cmd_calllist_burst()
- added EVE_cmd_hsf(), EVE_cmd_hsf_burst()
- added EVE_cmd_linetime()
- added EVE_cmd_newlist(), EVE_cmd_newlist_burst()
- added EVE_cmd_runanim(), EVE_cmd_runanim_burst()
- added a safeguard to EVE_start_cmd_burst() to protect it from overlapping transfers with DMA and segmented lists
- used spi_transmit_32() to shorten this file by around 600 lines with no functional change
- removed the history from before 4.0
- removed a couple of spi_transmit_32() calls from EVE_cmd_getptr() to make it work again
- Bugfix: EVE_cmd_setfont2_burst() was using CMD_SETFONT instead of CMD_SETFONT2
- removed a check for cmd_burst from EVE_cmd_getimage() as it is in the group of commands that are not used for display
lists
- moved EVE_cmd_newlist() to the group of commands that are not used for display lists
- removed EVE_cmd_newlist_burst()
- renamed spi_flash_write() to private_block_write() and made it static
- renamed EVE_write_string() to private_string_write() and made it static
- made EVE_start_command() static
- Bugfix: ESP8266 needs 32 bit alignment for 32 bit pointers,
changed private_string_write() for burst-mode to read 8-bit values
- Bugfix: somehow messed up private_string_write() for burst-mode
but only for 8-Bit controllers
- changed EVE_memRead8(), EVE_memRead16() and EVE_memRead32() to use
spi_transmit_32() for the initial address+zero byte transfer
This speeds up ESP32/ESP8266 by several us, has no measureable effect
for ATSAMD51 and is a little slower for AVR.
- Bugfix: not sure why but setting private_block_write() to static broke it, without "static" it works
- Bugfix: EVE_cmd_flashspirx() was using CMD_FLASHREAD
- fixed a warning in EVE_init() when compiling for EVE4
- renamed internal function EVE_begin_cmd() to eve_begin_cmd() and made it static
- changed all the EVE_start_command() calls to eve_begin_cmd() calls following the report on Github from
Michael Wachs that these are identical - they weren't prior to V5
- removed EVE_start_command()
- Bugfix: EVE_init() was only checking the first two bits of REG_CPURESET and ignored the bit for the audio-engine, not
an issue but not correct either.
- fixed a few clang-tidy warnings
- fixed a few cppcheck warnings
- fixed a few CERT warnings
- converted all TABs to SPACEs
- made EVE_TOUCH_RZTHRESH in EVE_init() optional to a) remove it from EVE_config.h and b) make it configureable
externally
- changed EVE_init() to write 1200U to REG_TOUCH_RZTHRESH if EVE_TOUCH_RZTHRESH is not defined
- changed EVE_init() to return E_OK = 0x00 in case of success and more meaningfull values in case of failure
- changed EVE_busy() to return EVE_IS_BUSY if EVE is busy and E_OK = 0x00 if EVE is not busy - no real change in
functionality
- finally removed EVE_cmd_start() after setting it to deprecatd with the first 5.0 release
- renamed EVE_cmd_execute() to EVE_execute_cmd() to be more consistent, this is is not an EVE command
- changed EVE_init_flash() to return E_OK in case of success and more meaningfull values in case of failure
- added the return-value of EVE_FIFO_HALF_EMPTY to EVE_busy() to indicate there is more than 2048 bytes available
- minor cleanup, less break and else statements
- added the burst code back into all the functions for which there is a _burst version, this allows to use the version
without the traling _burst in the name when exceution speed is not an issue - e.g. with all targets supporting DMA
- removed the 4.0 history
- added the optional parameter EVE_ROTATE as define to EVE_init() to allow for screen rotation during init
thanks for the idea to AndrejValand on Github!
- added the optional parameter EVE_BACKLIGHT_PWM to EVE_init() to allow setting the backlight during init
- modified EVE_calibrate_manual() to work better with bar type displays
- fixed a large number of MISRA-C issues - mostly more casts for explicit type conversion and more brackets
- changed the varargs versions of cmd_button, cmd_text and cmd_toggle to use an array of uint32_t values to comply with MISRA-C
- basic maintenance: checked for violations of white space and indent rules
- more linter fixes for minor issues like variables shorter than 3 characters
- added EVE_color_a() / EVE_color_a_burst()
- more minor tweaks and fixes to make the static analyzer happy
- changed the burst variant of private_string_write() back to the older and faster version
- refactoring of EVE_init() to single return
- added prototype for EVE_write_display_parameters()
- added EVE_memRead_sram_buffer()
- Bugfix issue #81: neither DISP or the pixel clock are enabled for EVE4 configurations not using EVE_PCLK_FREQ.
thanks for the report to grados73 on Github!
- added a few support lines for the Gameduino GD3X to EVE_init()
- switched from using CMD_PCLKFREQ to writing to REG_PCLK_FREQ directly
- added define EVE_SET_REG_PCLK_2X to set REG_PCLK_2X to 1 when necessary
- Bugfix: EVE_init() did not set the audio engine to "mute" as intended, but to "silent"
- Bugfix: EVE_busy() returns E_NOT_OK now on coprocessor faults.
thanks for the report to Z0ld3n on Github!
- Fix: reworked EVE_busy() to return EVE_FAULT_RECOVERED on deteced coprocessor faults,
removed the flash commands from the fault recovery sequence as these are project specific.
- added EVE_get_and_reset_fault_state() to check if EVE_busy() triggered a fault recovery
- added notes on how to use to EVE_cmd_setfont2() and EVE_cmd_romfont()
- new optional parameter in EVE_init(): EVE_BACKLIGHT_FREQ
- fixed a couple of minor issues from static code analysis
- reworked the burst part of private_string_write() to be less complex
- renamed chipid references to regid as suggested by #93 on github
- Bugfix: broke transfers of buffers larger than 3840 when fixing issues from static code analysis
- changed a number of function parameters from signed to unsigned following the
updated BT81x series programming guide V2.4
- did another linter pass and fixed some things
- started to improve the embedded documentation
- added more documentation
- removed EVE_cmd_hsf_burst()
- new parameter for EVE_init(): EVE_SOFT_RESET
*/
#include "EVE_commands.h"
/* EVE Memory Commands - used with EVE_memWritexx and EVE_memReadxx */
#define MEM_WRITE 0x80U /* EVE Host Memory Write */
/* #define MEM_READ 0x00U */ /* EVE Host Memory Read */
/* define NULL if it not already is */
#ifndef NULL
#include <stdio.h>
#endif
static volatile uint8_t cmd_burst = 0U; /* flag to indicate cmd-burst is active */
static volatile uint8_t fault_recovered = E_OK; /* flag to indicate if EVE_busy triggered a fault recovery */
/* ##################################################################
helper functions
##################################################################### */
/**
* @brief Send a host command.
*/
void EVE_cmdWrite(uint8_t const command, uint8_t const parameter)
{
EVE_cs_set();
spi_transmit(command);
spi_transmit(parameter);
spi_transmit(0U);
EVE_cs_clear();
}
/**
* @brief Implementation of rd8() function, reads 8 bits.
*/
uint8_t EVE_memRead8(uint32_t const ft_address)
{
uint8_t data;
EVE_cs_set();
spi_transmit_32(((ft_address >> 16U) & 0x0000007fUL) + (ft_address & 0x0000ff00UL) + ((ft_address & 0x000000ffUL) << 16U));
data = spi_receive(0U); /* read data byte by sending another dummy byte */
EVE_cs_clear();
return (data);
}
/**
* @brief Implementation of rd16() function, reads 16 bits.
*/
uint16_t EVE_memRead16(uint32_t const ft_address)
{
uint16_t data;
EVE_cs_set();
spi_transmit_32(((ft_address >> 16U) & 0x0000007fUL) + (ft_address & 0x0000ff00UL) + ((ft_address & 0x000000ffUL) << 16U));
uint8_t const lowbyte = spi_receive(0U); /* read low byte */
uint8_t const hibyte = spi_receive(0U); /* read high byte */
data = ((uint16_t) hibyte * 256U) | lowbyte;
EVE_cs_clear();
return (data);
}
/**
* @brief Implementation of rd32() function, reads 32 bits.
*/
uint32_t EVE_memRead32(uint32_t const ft_address)
{
uint32_t data;
EVE_cs_set();
spi_transmit_32(((ft_address >> 16U) & 0x0000007fUL) + (ft_address & 0x0000ff00UL) + ((ft_address & 0x000000ffUL) << 16U));
data = ((uint32_t) spi_receive(0U)); /* read low byte */
data = ((uint32_t) spi_receive(0U) << 8U) | data;
data = ((uint32_t) spi_receive(0U) << 16U) | data;
data = ((uint32_t) spi_receive(0U) << 24U) | data; /* read high byte */
EVE_cs_clear();
return (data);
}
/**
* @brief Implementation of wr8() function, writes 8 bits.
*/
void EVE_memWrite8(uint32_t const ft_address, uint8_t const ft_data)
{
EVE_cs_set();
spi_transmit((uint8_t) (ft_address >> 16U) | MEM_WRITE);
spi_transmit((uint8_t) (ft_address >> 8U));
spi_transmit((uint8_t) (ft_address & 0x000000ffUL));
spi_transmit(ft_data);
EVE_cs_clear();
}
/**
* @brief Implementation of wr16() function, writes 16 bits.
*/
void EVE_memWrite16(uint32_t const ft_address, uint16_t const ft_data)
{
EVE_cs_set();
spi_transmit((uint8_t) (ft_address >> 16U) | MEM_WRITE); /* send Memory Write plus high address byte */
spi_transmit((uint8_t) (ft_address >> 8U)); /* send middle address byte */
spi_transmit((uint8_t) (ft_address & 0x000000ffUL)); /* send low address byte */
spi_transmit((uint8_t) (ft_data & 0x00ffU)); /* send data low byte */
spi_transmit((uint8_t) (ft_data >> 8U)); /* send data high byte */
EVE_cs_clear();
}
/**
* @brief Implementation of wr32() function, writes 32 bits.
*/
void EVE_memWrite32(uint32_t const ft_address, uint32_t const ft_data)
{
EVE_cs_set();
spi_transmit((uint8_t) (ft_address >> 16U) | MEM_WRITE); /* send Memory Write plus high address byte */
spi_transmit((uint8_t) (ft_address >> 8U)); /* send middle address byte */
spi_transmit((uint8_t) (ft_address & 0x000000ffUL)); /* send low address byte */
spi_transmit_32(ft_data);
EVE_cs_clear();
}
/**
* @brief Helper function, write a block of memory from the FLASH of the host controller to EVE.
*/
void EVE_memWrite_flash_buffer(uint32_t const ft_address, const uint8_t *p_data, uint32_t const len)
{
if (p_data != NULL)
{
EVE_cs_set();
spi_transmit((uint8_t) (ft_address >> 16U) | MEM_WRITE);
spi_transmit((uint8_t) (ft_address >> 8U));
spi_transmit((uint8_t) (ft_address & 0x000000ffUL));
// uint32_t length = (len + 3U) & (~3U);
for (uint32_t count = 0U; count < len; count++)
{
spi_transmit(fetch_flash_byte(&p_data[count]));
}
EVE_cs_clear();
}
}
/**
* @brief Helper function, write a block of memory from the SRAM of the host controller to EVE.
*/
void EVE_memWrite_sram_buffer(uint32_t const ft_address, const uint8_t *p_data, uint32_t const len)
{
if (p_data != NULL)
{
EVE_cs_set();
spi_transmit((uint8_t) (ft_address >> 16U) | MEM_WRITE);
spi_transmit((uint8_t) (ft_address >> 8U));
spi_transmit((uint8_t) (ft_address & 0x000000ffUL));
// uint32_t length = (len + 3U) & (~3U);
for (uint32_t count = 0U; count < len; count++)
{
spi_transmit(p_data[count]);
}
EVE_cs_clear();
}
}
/**
* @brief Helper function, read a block of memory from EVE to the SRAM of the host controller.
*/
void EVE_memRead_sram_buffer(uint32_t const ft_address, uint8_t *p_data, uint32_t const len)
{
if (p_data != NULL)
{
EVE_cs_set();
spi_transmit_32(((ft_address >> 16U) & 0x0000007fUL) + (ft_address & 0x0000ff00UL) + ((ft_address & 0x000000ffUL) << 16U));
for (uint32_t count = 0U; count < len; count++)
{
p_data[count] = spi_receive(0U); /* read data byte by sending another dummy byte */
}
EVE_cs_clear();
}
}
static void CoprocessorFaultRecover(void)
{
#if EVE_GEN > 2
uint16_t copro_patch_pointer;
copro_patch_pointer = EVE_memRead16(REG_COPRO_PATCH_PTR);
#endif
EVE_memWrite8(REG_CPURESET, 1U); /* hold coprocessor engine in the reset condition */
EVE_memWrite16(REG_CMD_READ, 0U); /* set REG_CMD_READ to 0 */
EVE_memWrite16(REG_CMD_WRITE, 0U); /* set REG_CMD_WRITE to 0 */
EVE_memWrite16(REG_CMD_DL, 0U); /* reset REG_CMD_DL to 0 as required by the BT81x programming guide, should not hurt FT8xx */
#if EVE_GEN > 2
EVE_memWrite16(REG_COPRO_PATCH_PTR, copro_patch_pointer);
/* restore REG_PCLK in case it was set to zero by an error */
#if (EVE_GEN > 3) && (defined EVE_PCLK_FREQ)
EVE_memWrite16(REG_PCLK_FREQ, (uint16_t) EVE_PCLK_FREQ);
EVE_memWrite8(REG_PCLK, 1U); /* enable extsync mode */
#else
EVE_memWrite8(REG_PCLK, EVE_PCLK);
#endif
#endif
EVE_memWrite8(REG_CPURESET, 0U); /* set REG_CPURESET to 0 to restart the coprocessor engine*/
DELAY_MS(10U); /* just to be safe */
}
/**
* @brief Check if the coprocessor completed executing the current command list.
* @return - E_OK - if EVE is not busy (no DMA transfer active and REG_CMDB_SPACE has the value 0xffc, meaning the CMD-FIFO is empty
* @return - EVE_IS_BUSY - if a DMA transfer is active or REG_CMDB_SPACE has a value smaller than 0xffc
* @return - EVE_FIFO_HALF_EMPTY - if no DMA transfer is active and REG_CMDB_SPACE shows more than 2048 bytes available
* @return - E_NOT_OK - if there was a coprocessor fault and the recovery sequence was executed
* @note - if there is a coprocessor fault the external flash is not reinitialized by EVE_busy()
*/
uint8_t EVE_busy(void)
{
uint16_t space;
uint8_t ret = EVE_IS_BUSY;
#if defined (EVE_DMA)
if (0 == EVE_dma_busy)
{
#endif
space = EVE_memRead16(REG_CMDB_SPACE);
/* (REG_CMDB_SPACE & 0x03) != 0 -> we have a coprocessor fault */
if ((space & 3U) != 0U) /* we have a coprocessor fault, make EVE play with us again */
{
ret = EVE_FAULT_RECOVERED;
fault_recovered = EVE_FAULT_RECOVERED; /* save fault recovery state */
CoprocessorFaultRecover();
}
else
{
if (0xffcU == space)
{
ret = E_OK;
}
else if (space > 0x800U)
{
ret = EVE_FIFO_HALF_EMPTY;
}
else
{
ret = EVE_IS_BUSY;
}
}
#if defined (EVE_DMA)
}
#endif
return (ret);
}
/**
* @brief Helper function to check if EVE_busy() tried to recover from a coprocessor fault.
* The internal fault indicator is cleared so it could be set by EVE_busy() again.
* @return - EVE_FAULT_RECOVERED - if EVE_busy() detected a coprocessor fault
* @return - E_OK - if EVE_busy() did not detect a coprocessor fault
*/
uint8_t EVE_get_and_reset_fault_state(void)
{
uint8_t ret = E_OK;
if (EVE_FAULT_RECOVERED == fault_recovered)
{
ret = EVE_FAULT_RECOVERED;
fault_recovered = E_OK;
}
return (ret);
}
/**
* @brief Helper function, wait for the coprocessor to complete the FIFO queue.
*/
void EVE_execute_cmd(void)
{
while (EVE_busy() != E_OK)
{
}
}
/* begin a coprocessor command, this is used for non-display-list and non-burst-mode commands.*/
static void eve_begin_cmd(uint32_t command)
{
EVE_cs_set();
spi_transmit((uint8_t) 0xB0U); /* high-byte of REG_CMDB_WRITE + MEM_WRITE */
spi_transmit((uint8_t) 0x25U); /* middle-byte of REG_CMDB_WRITE */
spi_transmit((uint8_t) 0x78U); /* low-byte of REG_CMDB_WRITE */
spi_transmit_32(command);
}
void private_block_write(const uint8_t *p_data, uint16_t len); /* prototype to comply with MISRA */
void private_block_write(const uint8_t *p_data, uint16_t len)
{
uint8_t padding;
padding = (uint8_t) (len & 3U); /* 0, 1, 2 or 3 */
padding = 4U - padding; /* 4, 3, 2 or 1 */
padding &= 3U; /* 3, 2 or 1 */
for (uint16_t count = 0U; count < len; count++)
{
spi_transmit(fetch_flash_byte(&p_data[count]));
}
while (padding > 0U)
{
spi_transmit(0U);
padding--;
}
}
void block_transfer(const uint8_t *p_data, uint32_t len); /* prototype to comply with MISRA */
void block_transfer(const uint8_t *p_data, uint32_t len)
{
uint32_t bytes_left;
uint32_t offset = 0U;
bytes_left = len;
while (bytes_left > 0U)
{
uint32_t block_len;
block_len = (bytes_left > 3840UL) ? 3840UL : bytes_left;
EVE_cs_set();
spi_transmit((uint8_t) 0xB0U); /* high-byte of REG_CMDB_WRITE + MEM_WRITE */
spi_transmit((uint8_t) 0x25U); /* middle-byte of REG_CMDB_WRITE */
spi_transmit((uint8_t) 0x78U); /* low-byte of REG_CMDB_WRITE */
private_block_write(&p_data[offset], (uint16_t) block_len);
EVE_cs_clear();
offset += block_len;
bytes_left -= block_len;
EVE_execute_cmd();
}
}
/* ##################################################################
coprocessor commands that are not used in displays lists,
these are not to be used with burst transfers
################################################################### */
/* BT817 / BT818 */
#if EVE_GEN > 3
/**
* @brief Write "num" bytes from src in RAM_G to the previously erased external flash of a BT81x at address dest.
* @note - dest must be 4096-byte aligned, src must be 4-byte aligned, num must be a multiple of 4096
* @note - EVE will not do anything if the alignment requirements are not met
* @note - the address ptr is relative to the flash so the first address is 0x000000 not 0x800000
* @note - this looks exactly the same as EVE_cmd_flashupdate() but it needs the flash to be empty
* @note - Meant to be called outside display-list building.
* @note - Includes executing the command and waiting for completion.
* @note - Does not support burst-mode.
*/
void EVE_cmd_flashprogram(uint32_t dest, uint32_t src, uint32_t num)
{
eve_begin_cmd(CMD_FLASHPROGRAM);
spi_transmit_32(dest);
spi_transmit_32(src);
spi_transmit_32(num);
EVE_cs_clear();
EVE_execute_cmd();
}
/**
* @brief Enable the font cache.
* @note - Meant to be called outside display-list building.
* @note - Includes executing the command and waiting for completion.
* @note - Does not support burst-mode.
*/
void EVE_cmd_fontcache(uint32_t font, uint32_t ptr, uint32_t num)
{
eve_begin_cmd(CMD_FONTCACHE);
spi_transmit_32(font);
spi_transmit_32(ptr);
spi_transmit_32(num);
EVE_cs_clear();
EVE_execute_cmd();
}
/**
* @brief Queries the capacity and utilization of the font cache.
* @note - Meant to be called outside display-list building.
* @note - Includes executing the command and waiting for completion.
* @note - Does not support burst-mode.
*/
void EVE_cmd_fontcachequery(uint32_t *p_total, uint32_t *p_used)
{
uint16_t cmdoffset;
eve_begin_cmd(CMD_FONTCACHEQUERY);
spi_transmit_32(0UL);
spi_transmit_32(0UL);
EVE_cs_clear();
EVE_execute_cmd();
cmdoffset = EVE_memRead16(REG_CMD_WRITE); /* read the coprocessor write pointer */
if (p_total != NULL)
{
*p_total = EVE_memRead32(EVE_RAM_CMD + ((cmdoffset - 8UL) & 0xfffUL));
}
if (p_used != NULL)
{
*p_used = EVE_memRead32(EVE_RAM_CMD + ((cmdoffset - 4UL) & 0xfffUL));
}
}
/**
* @brief Returns all the attributes of the bitmap made by the previous CMD_LOADIMAGE, CMD_PLAYVIDEO, CMD_VIDEOSTART or CMD_VIDEOSTARTF.
* @note - Meant to be called outside display-list building.
* @note - Includes executing the command and waiting for completion.
* @note - Does not support burst-mode.
*/
void EVE_cmd_getimage(uint32_t *p_source, uint32_t *p_fmt, uint32_t *p_width, uint32_t *p_height, uint32_t *p_palette)
{
uint16_t cmdoffset;
eve_begin_cmd(CMD_GETIMAGE);
spi_transmit_32(0UL);
spi_transmit_32(0UL);
spi_transmit_32(0UL);
spi_transmit_32(0UL);
spi_transmit_32(0UL);
EVE_cs_clear();
EVE_execute_cmd();
cmdoffset = EVE_memRead16(REG_CMD_WRITE); /* read the coprocessor write pointer */
if (p_palette != NULL)
{
*p_palette = EVE_memRead32(EVE_RAM_CMD + ((cmdoffset - 4UL) & 0xfffUL));
}
if (p_height != NULL)
{
*p_height = EVE_memRead32(EVE_RAM_CMD + ((cmdoffset - 8UL) & 0xfffUL));
}
if (p_width != NULL)
{
*p_width = EVE_memRead32(EVE_RAM_CMD + ((cmdoffset - 12UL) & 0xfffUL));
}
if (p_fmt != NULL)
{
*p_fmt = EVE_memRead32(EVE_RAM_CMD + ((cmdoffset - 16UL) & 0xfffUL));
}
if (p_source != NULL)
{
*p_source = EVE_memRead32(EVE_RAM_CMD + ((cmdoffset - 20UL) & 0xfffUL));
}
}
/**
* @brief Undocumented command.
* @note - Meant to be called outside display-list building.
* @note - Includes executing the command and waiting for completion.
* @note - Does not support burst-mode.
*/
void EVE_cmd_linetime(uint32_t dest)
{
eve_begin_cmd(CMD_LINETIME);
spi_transmit_32(dest);
EVE_cs_clear();
EVE_execute_cmd();
}
/**
* @brief Starts the compilation of a command list into RAM_G.
* @note - Meant to be called outside display-list building.
* @note - Includes executing the command and waiting for completion.
* @note - Does not support burst-mode.
*/
void EVE_cmd_newlist(uint32_t adr)
{
eve_begin_cmd(CMD_NEWLIST);
spi_transmit_32(adr);
EVE_cs_clear();
EVE_execute_cmd();
}
/**
* @brief Sets REG_PCLK_FREQ to generate the closest possible frequency to the one requested.
* @return - the frequency achieved or zero if no frequency was found
* @note - When using this command, the flash BLOB is required.
* @note - Meant to be called outside display-list building.
* @note - Includes executing the command and waiting for completion.
* @note - Does not support burst-mode.
*/
uint32_t EVE_cmd_pclkfreq(uint32_t ftarget, int32_t rounding)
{
uint16_t cmdoffset;
eve_begin_cmd(CMD_PCLKFREQ);
spi_transmit_32(ftarget);
spi_transmit_32((uint32_t) rounding);
spi_transmit_32(0UL);
EVE_cs_clear();
EVE_execute_cmd();
cmdoffset = EVE_memRead16(REG_CMD_WRITE); /* read the coprocessor write pointer */
cmdoffset -= 4U;
cmdoffset &= 0x0fffU;
return (EVE_memRead32(EVE_RAM_CMD + cmdoffset));
}
/**
* @brief Waits for a specified number of microseconds.
* @note - Meant to be called outside display-list building.
* @note - Includes executing the command and waiting for completion.
* @note - Does not support burst-mode.
*/
void EVE_cmd_wait(uint32_t usec)
{
eve_begin_cmd(CMD_WAIT);
spi_transmit_32(usec);
EVE_cs_clear();
EVE_execute_cmd();
}
#endif /* EVE_GEN > 3 */
/* BT815 / BT816 */
#if EVE_GEN > 2
/**
* @brief Clears the graphics engine’s internal flash cache.
* @note - This function includes clearing out the display list.
* @note - Meant to be called outside display-list building.
* @note - Includes executing the command and waiting for completion.
* @note - Does not support burst-mode.
*/
void EVE_cmd_clearcache(void)
{
EVE_cmd_dl(CMD_DLSTART);
EVE_cmd_dl(CMD_SWAP);
EVE_execute_cmd();
EVE_cmd_dl(CMD_DLSTART);
EVE_cmd_dl(CMD_SWAP);
EVE_execute_cmd();
EVE_cmd_dl(CMD_CLEARCACHE);
EVE_execute_cmd();
}
/**
* @brief Re-connect to the attached SPI flash storage.
* @note - Meant to be called outside display-list building.
* @note - Includes executing the command and waiting for completion.
* @note - Does not support burst-mode.
*/
void EVE_cmd_flashattach(void)
{
eve_begin_cmd(CMD_FLASHATTACH);
EVE_cs_clear();
EVE_execute_cmd();
}
/**
* @brief Dis-connect from the attached SPI flash storage.
* @note - Meant to be called outside display-list building.
* @note - Includes executing the command and waiting for completion.
* @note - Does not support burst-mode.
*/
void EVE_cmd_flashdetach(void)
{
eve_begin_cmd(CMD_FLASHDETACH);
EVE_cs_clear();
EVE_execute_cmd();
}
/**
* @brief Erases the attached SPI flash storage.
* @note - Meant to be called outside display-list building.
* @note - Includes executing the command and waiting for completion.
* @note - Does not support burst-mode.
*/
void EVE_cmd_flasherase(void)
{
eve_begin_cmd(CMD_FLASHERASE);
EVE_cs_clear();
EVE_execute_cmd();
}
/**
* @brief Drive the attached SPI flash storage in full-speed mode, if possible.
* @return - Zero on success, error code on failure
* @note - When using this command, the flash BLOB is required.
* @note - Meant to be called outside display-list building.
* @note - Includes executing the command and waiting for completion.
* @note - Does not support burst-mode.
*/
uint32_t EVE_cmd_flashfast(void)
{
uint16_t cmdoffset;
eve_begin_cmd(CMD_FLASHFAST);
spi_transmit_32(0UL);
EVE_cs_clear();
EVE_execute_cmd();
cmdoffset = EVE_memRead16(REG_CMD_WRITE); /* read the coprocessor write pointer */
cmdoffset -= 4U;
cmdoffset &= 0x0fffU;
return (EVE_memRead32(EVE_RAM_CMD + cmdoffset));
}
/**
* @brief De-asserts the SPI CS signal of the attached SPI flash storage.
* @note - Only works when the attached SPI flash storage has been detached.
* @note - Meant to be called outside display-list building.
* @note - Includes executing the command and waiting for completion.
* @note - Does not support burst-mode.
*/
void EVE_cmd_flashspidesel(void)
{
eve_begin_cmd(CMD_FLASHSPIDESEL);
EVE_cs_clear();
EVE_execute_cmd();
}
/**
* @brief Copies "num" bytes from "src" in attached SPI flash storage to "dest" in RAM_G.
* @note - src must be 64-byte aligned, dest must be 4-byte aligned, num must be a multiple of 4
* @note - EVE will not do anything if the alignment requirements are not met
* @note - The src pointer is relative to the flash so the first address is 0x000000 not 0x800000.
* @note - Meant to be called outside display-list building.
* @note - Includes executing the command and waiting for completion.
* @note - Does not support burst-mode.
*/
void EVE_cmd_flashread(uint32_t dest, uint32_t src, uint32_t num)
{
eve_begin_cmd(CMD_FLASHREAD);
spi_transmit_32(dest);
spi_transmit_32(src);
spi_transmit_32(num);
EVE_cs_clear();
EVE_execute_cmd();
}
/**
* @brief Set the source address for flash data loaded by the CMD_LOADIMAGE, CMD_PLAYVIDEO, CMD_VIDEOSTARTF and CMD_INFLATE2 commands with the OPT_FLASH option.
* @note - Address must be 64-byte aligned.
* @note - EVE will not do anything if the alignment requirements are not met.
* @note - The pointer is relative to the flash, so the first address is 0x000000 not 0x800000.
* @note - Meant to be called outside display-list building.
* @note - Includes executing the command and waiting for completion.
* @note - Does not support burst-mode.
*/
void EVE_cmd_flashsource(uint32_t ptr)
{
eve_begin_cmd(CMD_FLASHSOURCE);
spi_transmit_32(ptr);
EVE_cs_clear();
EVE_execute_cmd();
}
/**
* @brief Receives bytes from the flash SPI interface and writes them to main memory.
* @note - Only works when the attached SPI flash storage has been detached.
* @note - Meant to be called outside display-list building.
* @note - Includes executing the command and waiting for completion.
* @note - Does not support burst-mode.
*/
void EVE_cmd_flashspirx(uint32_t dest, uint32_t num)
{
eve_begin_cmd(CMD_FLASHSPIRX);
spi_transmit_32(dest);
spi_transmit_32(num);
EVE_cs_clear();
EVE_execute_cmd();
}
/**
* @brief Transmits bytes over the flash SPI interface.
* @note - Only works when the attached SPI flash storage has been detached.
* @note - Meant to be called outside display-list building.
* @note - Includes executing the command and waiting for completion.
* @note - Does not support burst-mode.
*/
void EVE_cmd_flashspitx(uint32_t num, const uint8_t *p_data)
{
eve_begin_cmd(CMD_FLASHSPITX);
spi_transmit_32(num);
EVE_cs_clear();
block_transfer(p_data, num);
}
/**
* @brief Write "num" bytes from src in RAM_G to the attached SPI flash storage at address dest.
* @note - dest must be 4096-byte aligned, src must be 4-byte aligned, num must be a multiple of 4096
* @note - EVE will not do anything if the alignment requirements are not met.
* @note - The address ptr is relative to the flash so the first address is 0x000000 not 0x800000.
* @note - Meant to be called outside display-list building.
* @note - Includes executing the command and waiting for completion.
* @note - Does not support burst-mode.
*/
void EVE_cmd_flashupdate(uint32_t dest, uint32_t src, uint32_t num)
{
eve_begin_cmd(CMD_FLASHUPDATE);
spi_transmit_32(dest);
spi_transmit_32(src);
spi_transmit_32(num);
EVE_cs_clear();
EVE_execute_cmd();
}
/**
* @brief Write "num" bytes to the attached SPI flash storage at address dest.
* @note - dest must be 256-byte aligned, num must be a multiple of 256
* @note - EVE will not do anything if the alignment requirements are not met.
* @note - The address ptr is relative to the flash so the first address is 0x000000 not 0x800000.
* @note - Meant to be called outside display-list building.
* @note - Includes executing the command and waiting for completion.
* @note - Does not support burst-mode.
*/
void EVE_cmd_flashwrite(uint32_t ptr, uint32_t num, const uint8_t *p_data)
{
eve_begin_cmd(CMD_FLASHWRITE);
spi_transmit_32(ptr);
spi_transmit_32(num);
EVE_cs_clear();
if (p_data != NULL)
{
block_transfer(p_data, num);
}
}
/**
* @brief Decompress data into RAM_G.
* @note - The data must be correct and complete.
* @note - Meant to be called outside display-list building.
* @note - Includes executing the command and waiting for completion.
* @note - Does not support burst-mode.
*/
void EVE_cmd_inflate2(uint32_t ptr, uint32_t options, const uint8_t *p_data, uint32_t len)
{
eve_begin_cmd(CMD_INFLATE2);
spi_transmit_32(ptr);
spi_transmit_32(options);
EVE_cs_clear();
if (0UL == options) /* direct data, not by Media-FIFO or Flash */
{
if (p_data != NULL)
{
block_transfer(p_data, len);
}
}
}
#endif /* EVE_GEN > 2 */
/**
* @brief Returns the source address and size of the bitmap loaded by the previous CMD_LOADIMAGE.
* @note - Meant to be called outside display-list building.
* @note - Includes executing the command and waiting for completion.
* @note - Does not support burst-mode.
*/
void EVE_cmd_getprops(uint32_t *p_pointer, uint32_t *p_width, uint32_t *p_height)
{
uint16_t cmdoffset;
eve_begin_cmd(CMD_GETPROPS);
spi_transmit_32(0UL);
spi_transmit_32(0UL);
spi_transmit_32(0UL);
EVE_cs_clear();
EVE_execute_cmd();
cmdoffset = EVE_memRead16(REG_CMD_WRITE); /* read the coprocessor write pointer */
if (p_pointer != NULL)
{
*p_pointer = EVE_memRead32(EVE_RAM_CMD + ((cmdoffset - 12UL) & 0xfffUL));
}
if (p_width != NULL)
{
*p_width = EVE_memRead32(EVE_RAM_CMD + ((cmdoffset - 8UL) & 0xfffUL));
}
if (p_height != NULL)
{
*p_height = EVE_memRead32(EVE_RAM_CMD + ((cmdoffset - 4UL) & 0xfffUL));
}
}
/**
* @brief Returns the next address after a CMD_INFLATE and other commands.
* @note - Meant to be called outside display-list building.
* @note - Includes executing the command and waiting for completion.
* @note - Does not support burst-mode.
*/
uint32_t EVE_cmd_getptr(void)
{
uint16_t cmdoffset;
eve_begin_cmd(CMD_GETPTR);
spi_transmit_32(0UL);
EVE_cs_clear();
EVE_execute_cmd();
cmdoffset = EVE_memRead16(REG_CMD_WRITE); /* read the coprocessor write pointer */
cmdoffset -= 4U;
cmdoffset &= 0x0fffU;
return (EVE_memRead32(EVE_RAM_CMD + cmdoffset));
}
/**
* @brief Decompress data into RAM_G.
* @note - The data must be correct and complete.
* @note - Meant to be called outside display-list building.
* @note - Includes executing the command and waiting for completion.
* @note - Does not support burst-mode.
*/
void EVE_cmd_inflate(uint32_t ptr, const uint8_t *p_data, uint32_t len)
{
eve_begin_cmd(CMD_INFLATE);
spi_transmit_32(ptr);
EVE_cs_clear();
if (p_data != NULL)
{
block_transfer(p_data, len);
}
}
/**
* @brief Trigger interrupt INT_CMDFLAG.
* @note - Meant to be called outside display-list building.
* @note - Includes executing the command and waiting for completion.
* @note - Does not support burst-mode.
*/
void EVE_cmd_interrupt(uint32_t msec)
{
eve_begin_cmd(CMD_INTERRUPT);
spi_transmit_32(msec);
EVE_cs_clear();
EVE_execute_cmd();
}