image/svg+xml
Brteve's API for EveApps
Version 0.0.1
The reference document for common folder of EveApps project.
 
Loading...
Searching...
No Matches
EVE_Util.c
Go to the documentation of this file.
1
32#include "EVE_Util.h"
33#include "EVE_Platform.h"
34#include "EVE_HalImpl.h"
35
37
39 0, 0, 0, 2, // GPU instruction CLEAR_COLOR_RGB
40 7, 0, 0, 38, // GPU instruction CLEAR
41 0, 0, 0, 0, // GPU instruction DISPLAY
42};
43
46 /* width, height, refreshRate, hsfWidth */
47
48 { 480, 272, 60, 0 }, /* EVE_DISPLAY_DEFAULT (values ignored) */
49
50 /* Landscape */
51 { 320, 240, 56, 0 }, /* EVE_DISPLAY_QVGA_320x240_56Hz */
52 { 480, 272, 60, 0 }, /* EVE_DISPLAY_WQVGA_480x272_60Hz */
53 { 800, 480, 74, 0 }, /* EVE_DISPLAY_WVGA_800x480_74Hz */
54 { 1024, 600, 59, 0 }, /* EVE_DISPLAY_WSVGA_1024x600_59Hz */
55 { 1280, 720, 58, 0 }, /* EVE_DISPLAY_HDTV_1280x720_58Hz */
56 { 1280, 800, 57, 0 }, /* EVE_DISPLAY_WXGA_1280x800_57Hz */
57
58 /* Portrait */
59 { 320, 480, 60, 0 }, /* EVE_DISPLAY_HVGA_320x480_60Hz */
60
61 /* IDM2040-7A, AT-T070SWH-51CP, HX8264-E, 60 to 90Hz capable */
62 { 860, 480, 60, 800 }, /* EVE_DISPLAY_WVGA_800x480_W860_60Hz */
63
64 /* Riverdi */
65 { 320, 240, 62, 0 }, /* EVE_DISPLAY_RIVERDI_IPS35_62Hz */
66 { 480, 272, 58, 0 }, /* EVE_DISPLAY_RIVERDI_IPS43_58Hz */
67 { 800, 480, 63, 0 }, /* EVE_DISPLAY_RIVERDI_IPS50_63Hz */
68 { 1024, 600, 59, 0 }, /* EVE_DISPLAY_RIVERDI_IPS70_59Hz */
69 { 1280, 800, 59, 0 }, /* EVE_DISPLAY_RIVERDI_IPS101_59Hz */
70
71};
72
73#if defined(_WIN32) && defined(EVE_MULTI_GRAPHICS_TARGET)
74
76static const char *s_DisplayNames[EVE_DISPLAY_NB] = {
77 "<Default>",
78
79 "QVGA 320x240 56Hz",
80 "WQVGA 480x272 60Hz",
81 "WVGA 800x480 74Hz",
82 "WSVGA 1024x600 59Hz",
83 "HDTV 1280x720 58Hz",
84 "WXGA 1280x800 57Hz",
85
86 "HVGA 320x480 60Hz",
87
88 "IDM2040-7A WVGA 60Hz",
89
90 "Riverdi IPS 3.5\" 62Hz",
91 "Riverdi IPS 4.3\" 58Hz",
92 "Riverdi IPS 5.0\" 63Hz",
93 "Riverdi IPS 7.0\" 59Hz",
94 "Riverdi IPS 10.1\" 59Hz",
95
96};
97
98#endif
99
100#if defined(_WIN32)
101
103static const char *s_HostDisplayNames[EVE_HOST_NB] = {
104 "<Unknown>",
105
106 "BT8XX Emulator",
107 "FT4222",
108 "MPSSE",
109 "Embedded",
110};
111
112#define EVE_SELECT_CHIP_NB 14
113
114#if defined(EVE_MULTI_GRAPHICS_TARGET)
115
117static const char *s_SelectChipName[EVE_SELECT_CHIP_NB] = {
118 "FT800",
119 "FT801",
120 "FT810",
121 "FT811",
122 "FT812",
123 "FT813",
124 "BT880",
125 "BT881",
126 "BT882",
127 "BT883",
128 "BT815",
129 "BT816",
130 "BT817",
131 "BT818",
132};
133
134static EVE_CHIPID_T s_SelectChipId[EVE_SELECT_CHIP_NB] = {
135 EVE_FT800,
136 EVE_FT801,
137 EVE_FT810,
138 EVE_FT811,
139 EVE_FT812,
140 EVE_FT813,
141 EVE_BT880,
142 EVE_BT881,
143 EVE_BT882,
144 EVE_BT883,
145 EVE_BT815,
146 EVE_BT816,
147 EVE_BT817,
148 EVE_BT818,
149};
150
151#endif
152
153#endif
154
156#if (!defined(BT8XXEMU_PLATFORM) || defined(EVE_MULTI_PLATFORM_TARGET)) \
157 && ((EVE_SUPPORT_CHIPID == EVE_FT811) || (EVE_SUPPORT_CHIPID == EVE_FT813) || defined(EVE_MULTI_GRAPHICS_TARGET))
158#define TOUCH_DATA_LEN 1172
159static eve_progmem_const uint8_t c_TouchDataU8[TOUCH_DATA_LEN] = {
160 26, 255, 255, 255, 32, 32, 48, 0, 4, 0, 0, 0, 2, 0, 0, 0, 34,
161 255, 255, 255, 0, 176, 48, 0, 120, 218, 237, 84, 255, 107, 92,
162 69, 16, 159, 125, 155, 107, 141, 201, 121, 247, 106, 130, 9,
163 225, 244, 238, 37, 246, 146, 52, 63, 53, 98, 172, 53, 48, 243,
164 8, 36, 166, 182, 63, 136, 216, 246, 7, 205, 219, 75, 122, 119,
165 185, 139, 196, 128, 34, 33, 136, 123, 65, 240, 7, 31, 44, 105,
166 41, 69, 72, 12, 210, 166, 6, 17, 12, 104, 64, 165, 73, 133, 134,
167 98, 80, 74, 43, 148, 22, 20, 133, 40, 20, 34, 233, 15, 82, 250,
168 131, 10, 113, 246, 229, 106, 197, 191, 161, 111, 217, 55, 179,
169 59, 59, 59, 243, 153, 47, 251, 135, 15, 58, 204, 11, 109, 114,
170 89, 149, 84, 169, 242, 172, 2, 109, 10, 73, 53, 167, 92, 158,
171 89, 21, 6, 89, 230, 236, 126, 86, 89, 206, 242, 169, 178, 61,
172 187, 115, 62, 85, 190, 199, 133, 129, 44, 132, 249, 90, 12, 243,
173 124, 67, 49, 169, 76, 81, 22, 195, 60, 211, 113, 40, 133, 249,
174 253, 104, 114, 113, 148, 197, 131, 105, 169, 158, 3, 19, 132,
175 65, 28, 187, 240, 25, 112, 3, 147, 99, 235, 163, 80, 98, 63, 10,
176 123, 181, 208, 73, 213, 29, 24, 246, 226, 121, 112, 199, 146,
177 74, 98, 56, 22, 195, 135, 245, 32, 14, 194, 17, 180, 182, 100,
178 46, 169, 220, 114, 244, 103, 171, 114, 252, 41, 222, 143, 97,
179 150, 53, 64, 167, 202, 110, 240, 86, 186, 45, 16, 90, 14, 191,
180 158, 110, 196, 163, 216, 130, 241, 232, 159, 42, 251, 88, 4,
181 169, 90, 80, 42, 163, 226, 248, 168, 206, 97, 132, 136, 169, 69,
182 113, 20, 27, 245, 203, 40, 131, 12, 54, 87, 37, 143, 253, 43,
183 105, 97, 137, 9, 172, 103, 181, 26, 172, 79, 163, 102, 184, 142,
184 57, 83, 0, 61, 139, 144, 159, 79, 135, 193, 62, 61, 0, 157, 250,
185 5, 20, 186, 67, 91, 255, 133, 238, 140, 104, 167, 30, 192, 197,
186 72, 2, 85, 137, 221, 89, 134, 101, 140, 177, 191, 238, 137, 75,
187 105, 137, 23, 97, 18, 226, 31, 175, 98, 31, 174, 165, 141, 90,
188 193, 21, 88, 194, 117, 60, 207, 243, 115, 248, 202, 254, 171,
189 171, 37, 248, 20, 87, 112, 17, 59, 180, 59, 246, 35, 199, 177,
190 83, 175, 35, 140, 93, 224, 187, 172, 45, 169, 172, 108, 29, 47,
191 192, 59, 186, 70, 159, 1, 208, 155, 136, 40, 71, 190, 195, 101,
192 222, 117, 131, 221, 90, 142, 110, 48, 221, 85, 165, 177, 42,
193 173, 169, 82, 89, 165, 78, 149, 138, 136, 110, 224, 13, 108,
194 200, 0, 132, 1, 50, 103, 114, 55, 176, 145, 254, 51, 132, 208,
195 183, 177, 137, 64, 111, 128, 192, 13, 104, 162, 86, 145, 165,
196 198, 255, 141, 86, 209, 68, 79, 146, 84, 87, 33, 163, 95, 132,
197 39, 120, 182, 242, 76, 115, 78, 123, 40, 142, 7, 200, 227, 21,
198 103, 50, 103, 171, 109, 123, 27, 242, 115, 193, 0, 197, 176, 70,
199 63, 141, 125, 212, 35, 14, 8, 95, 28, 20, 246, 116, 155, 30,
200 132, 152, 118, 3, 40, 29, 162, 62, 60, 158, 9, 131, 8, 197, 120,
201 49, 99, 115, 35, 39, 228, 200, 168, 48, 81, 197, 2, 175, 221,
202 224, 205, 140, 173, 148, 4, 87, 13, 148, 94, 19, 235, 216, 205,
203 62, 212, 179, 14, 199, 221, 234, 21, 223, 207, 112, 254, 163,
204 90, 169, 197, 247, 50, 110, 249, 16, 249, 156, 113, 137, 83, 98,
205 146, 163, 23, 215, 115, 16, 146, 173, 102, 142, 158, 74, 160,
206 205, 181, 28, 217, 67, 93, 156, 121, 40, 156, 202, 76, 130, 169,
207 184, 193, 12, 9, 125, 82, 132, 149, 121, 209, 65, 219, 198, 12,
208 47, 80, 7, 15, 182, 163, 97, 52, 171, 206, 211, 20, 186, 229,
209 195, 148, 42, 15, 137, 45, 116, 244, 29, 188, 138, 105, 177, 76,
210 139, 149, 18, 113, 141, 160, 192, 1, 218, 222, 14, 131, 101, 90,
211 224, 17, 195, 239, 51, 113, 156, 194, 111, 245, 21, 49, 137,
212 171, 100, 61, 238, 21, 166, 50, 67, 95, 83, 63, 154, 202, 71,
213 116, 141, 45, 205, 208, 37, 94, 133, 188, 186, 201, 177, 134,
214 82, 86, 117, 84, 109, 46, 137, 52, 237, 214, 102, 248, 22, 253,
215 82, 41, 137, 91, 148, 37, 29, 97, 147, 195, 86, 126, 89, 156,
216 165, 45, 214, 188, 43, 54, 43, 160, 63, 36, 139, 169, 142, 17,
217 73, 175, 31, 255, 230, 232, 164, 175, 244, 161, 197, 38, 71,
218 118, 121, 70, 109, 226, 43, 252, 14, 100, 249, 142, 135, 180,
219 156, 48, 185, 45, 172, 176, 247, 71, 244, 29, 104, 240, 25, 3,
220 37, 216, 78, 179, 159, 101, 171, 9, 63, 225, 95, 166, 6, 126,
221 71, 24, 29, 156, 165, 199, 253, 126, 108, 119, 88, 51, 146, 216,
222 19, 191, 137, 102, 191, 107, 186, 68, 109, 78, 247, 244, 95,
223 130, 99, 94, 134, 49, 183, 128, 158, 84, 207, 58, 245, 129, 9,
224 220, 145, 78, 16, 218, 85, 102, 226, 94, 102, 142, 121, 247, 51,
225 243, 146, 119, 63, 51, 3, 14, 71, 157, 94, 245, 86, 169, 151,
226 134, 156, 99, 212, 206, 189, 148, 208, 9, 109, 239, 43, 242,
227 125, 57, 135, 249, 19, 111, 120, 245, 182, 79, 121, 218, 62,
228 110, 231, 222, 203, 57, 220, 245, 52, 237, 217, 126, 246, 253,
229 183, 89, 95, 59, 59, 145, 238, 165, 15, 28, 91, 45, 86, 239,
230 140, 215, 231, 199, 56, 34, 59, 239, 154, 239, 159, 244, 38, 57,
231 63, 167, 156, 41, 126, 95, 14, 83, 82, 189, 203, 183, 244, 51,
232 186, 33, 39, 12, 234, 244, 105, 156, 79, 111, 161, 100, 186,
233 232, 37, 25, 187, 195, 253, 252, 137, 35, 245, 2, 243, 54, 207,
234 243, 8, 176, 143, 109, 175, 225, 57, 92, 131, 71, 248, 220, 151,
235 222, 113, 255, 28, 26, 114, 131, 139, 17, 130, 111, 24, 65, 63,
236 235, 157, 198, 90, 188, 238, 213, 251, 63, 120, 130, 59, 151,
237 187, 130, 95, 193, 207, 252, 26, 214, 221, 175, 215, 240, 186,
238 211, 85, 217, 197, 107, 67, 123, 113, 15, 217, 42, 252, 201,
239 155, 245, 133, 94, 131, 122, 255, 87, 207, 80, 143, 215, 238,
240 91, 47, 126, 247, 106, 248, 204, 159, 153, 187, 116, 219, 49,
241 116, 45, 115, 147, 182, 48, 242, 4, 190, 240, 127, 118, 224,
242 193, 247, 224, 3, 89, 247, 15, 148, 99, 211, 103, 26, 255, 255,
243 255, 20, 33, 48, 0, 4, 0, 0, 0, 15, 0, 0, 0, 26, 255, 255, 255,
244 32, 32, 48, 0, 4, 0, 0, 0, 0, 0, 0, 0
245};
246
252static inline void uploadTouchFirmware(EVE_HalContext *phost)
253{
254 /* bug fix pen up section */
255 eve_assert_do(EVE_Cmd_wrProgMem(phost, c_TouchDataU8, TOUCH_DATA_LEN));
257}
258#else
259static inline void uploadTouchFirmware(EVE_HalContext *phost)
260{
261 /* no-op */
262}
263#endif
264
275
283{
284 int32_t chipId = EVE_CHIPID;
285 (void)chipId;
286
287 memset(bootup, 0, sizeof(EVE_BootupParameters));
288
289#if !defined(EVE_USE_INTERNAL_OSC)
290 /* Board without external oscillator will not work when ExternalOsc is enabled */
291 bootup->ExternalOsc = true;
292#endif
293
294#ifdef EVE_SYSTEM_CLOCK
295 bootup->SystemClock = EVE_SYSTEM_CLOCK;
296#else
297 if (chipId >= EVE_FT800 && chipId <= EVE_BT818)
298 {
299#if (EVE_SUPPORT_CHIPID >= EVE_BT815)
300 if (chipId >= EVE_BT815)
301 {
302 bootup->SystemClock = EVE_SYSCLK_72M; /* 72Mhz is default for BT8xx */
303 }
304 else
305#endif
306#if (EVE_SUPPORT_CHIPID >= EVE_FT810)
307 if (chipId >= EVE_FT810)
308 {
309 bootup->SystemClock = EVE_SYSCLK_60M; /* 60Mhz is default for FT81x */
310 }
311 else
312#endif
313 {
314 bootup->SystemClock = EVE_SYSCLK_48M; /* 48Mhz is default for FT80x */
315 }
316 }
317 else
318 {
319 /* If the chipid is not known yet, this will reconfigure the system clock during `EVE_Util_bootup`.
320 Additional default clock overrides need to be implemented there as well. */
322 }
323#endif
324
325#if (EVE_SUPPORT_CHIPID >= EVE_FT810)
326#if defined(EVE_MULTI_PLATFORM_TARGET)
327 if (EVE_HOST == EVE_HOST_FT4222)
328 {
330 bootup->SpiDummyBytes = 2;
331 }
332 else
333 {
335 bootup->SpiDummyBytes = 1;
336 }
337#elif defined(ENABLE_SPI_QUAD)
339 bootup->SpiDummyBytes = 2;
340#elif defined(ENABLE_SPI_DUAL)
342 bootup->SpiDummyBytes = 2;
343#else
345 bootup->SpiDummyBytes = 1;
346#endif
347#endif
348}
349
350#ifndef EVE_HARDCODED_DISPLAY_TIMINGS
351#define EVE_HARDCODED_DISPLAY_TIMINGS 1
352#endif
353
354#if (EVE_SUPPORT_CHIPID >= EVE_BT817)
355
362static uint16_t getPClkFreq(uint32_t freq)
363{
364 // uint32_t resFreq;
365 uint32_t res;
366 // eve_printf_debug("Want frequency %i\n", (int)wantFreq);
367 int32_t nearestFreq = 0x7FFFFFFFL;
368 int32_t nearestDiv = 0;
369 int32_t nearestMul = 0;
370 for (uint32_t div = 1; div < 15; ++div)
371 {
372 uint32_t mul = (freq * div) / 6000000;
373 if (mul > 19)
374 break;
375 // eve_printf_debug("Div %i Mul %i\n", (int)div, (int)mul);
376 if (mul > 1)
377 {
378 int32_t resFreq = 6000000 * mul / div;
379 // eve_printf_debug("Res frequency %i\n", (int)resFreq);
380 int32_t diffFreq = resFreq - (int32_t)freq;
381 if (diffFreq >= 0 && diffFreq < (nearestFreq - (int32_t)freq))
382 {
383 nearestFreq = resFreq;
384 nearestDiv = div;
385 nearestMul = mul;
386 }
387 }
388 if (mul && mul <= 18)
389 {
390 int32_t resFreq = 6000000 * (mul + 1) / div;
391 // eve_printf_debug("Res frequency (mul + 1) %i\n", (int)resFreq);
392 int32_t diffFreq = resFreq - (int32_t)freq;
393 if (diffFreq >= 0 && diffFreq < (nearestFreq - (int32_t)freq))
394 {
395 nearestFreq = resFreq;
396 nearestDiv = div;
397 nearestMul = mul + 1;
398 }
399 }
400 }
401 if (nearestFreq != 0x7FFFFFFFL)
402 {
403 // resFreq = nearestFreq;
404 // maxRate = resFreq / minCycles;
405 uint32_t pllFreq = 12 * nearestMul;
406 uint32_t range = pllFreq >= 160 ? 3 : (pllFreq >= 80 ? 2 : (pllFreq >= 40 ? 1 : 0));
407 res = (nearestDiv & 0xF) | ((nearestMul & 0x3F) << 4) | (range << 10);
408 // eve_printf_debug("Set PCLK Freq to %X (freq %i, mul %i, div %i)\n", (int)config->PClkFreq, (int)nearestFreq, (int)nearestMul, (int)nearestDiv);
409 return res;
410 }
411 return 0;
412}
413
420static inline uint32_t getFreqFromPClkFreq(uint16_t pclkFreq)
421{
422 if (!pclkFreq)
423 return 0;
424 uint32_t mul = (pclkFreq >> 4) & 0x3F;
425 uint32_t div = pclkFreq & 0xF;
426 uint32_t res = mul * (12 * 1000 * 1000) / div / 2;
427 return res;
428}
429
430#endif
431
445static bool configDefaultsEx(EVE_HalContext *phost, EVE_ConfigParameters *config, uint32_t width, uint32_t height, uint32_t refreshRate, uint32_t hsfWidth, uint32_t freq)
446{
447 /* Only false if the output resolution will be wrong.
448 Not affected by unsupported HSF or refreshRate */
449 bool supportedResolution = true;
450
451 uint32_t screenWidth;
452 uint32_t pixels;
453 uint32_t minCycles;
454 uint32_t maxRate;
455 uint32_t pclk;
456
457 uint32_t cycles;
458 uint32_t vcycle;
459 uint32_t hcycle;
460 uint32_t hoffset;
461 uint32_t voffset;
462 uint32_t hsync1;
463 uint32_t vsync1;
464
465#ifdef EVE_SUPPORT_HSF
466 uint32_t maxFreq;
467 uint32_t maxRatePass;
468 uint32_t pclkFreq = 0;
469#endif
470
471 memset(config, 0, sizeof(EVE_ConfigParameters));
472
473 /*
474 Refresh rate is as follows:
475 REG_VCYCLE * REG_HCYCLE * REG_PCLK is the number of clocks each frame takes,
476 Divide REG_FREQUENCY by this number to get the frame refresh rate in Hz.
477 REG_HCYCLE must be higher than REG_HSIZE, REG_VCYCLE must be higher than REG_VSIZE.
478
479 By default, FT800 is at 48MHz, FT810 is at 60MHz, and BT815 is increased to 72MHz.
480 User may configure this differently in `EVE_Util_bootup`.
481
482 Clocks per frame by clock frequency at 60Hz:
483 - 84MHz: 1400k
484 - 72MHz: 1200k
485 - 60MHz: 1000k
486 - 48MHz: 800k
487 - 36MHz: 600k
488 - 24MHz: 400k
489
490 Clocks required per frame by display resolution at 60Hz:
491 Max PCLK at frequency: 24 36 48 60 72 84MHz
492 - 320x240: 76.8k 5 7 10 13 15 18
493 - 480x272: 130.56k 3 4 6 7 9 10
494 */
495
496 /* Trim oversize displays */
497 if (EVE_CHIPID >= EVE_FT810)
498 {
499#ifdef EVE_SUPPORT_HSF
500 if (hsfWidth >= width)
501 {
502 eve_printf_debug("Screen HSF width %i is too large, disabled\n", (int)config->HsfWidth);
503 hsfWidth = 0;
504 width = hsfWidth;
505 supportedResolution = false;
506 }
507#endif
508 if (width > 2047)
509 {
510 eve_printf_debug("Line buffer width %i is too large for this configuration\n", (int)config->Width);
511 width = 2047;
512 supportedResolution = false;
513 }
514 if (height > 2047)
515 {
516 eve_printf_debug("Screen height %i is too large for this configuration\n", (int)config->Height);
517 height = 2047;
518 supportedResolution = false;
519 }
520 }
521 else
522 {
523 if (width > 493)
524 {
525 /* 494 and up are not reliable */
526 eve_printf_debug("Screen width %i is too large for this configuration\n", (int)config->Width);
527 width = 493;
528 supportedResolution = false;
529 }
530 if (height > 511)
531 {
532 eve_printf_debug("Screen height %i is too large for this configuration\n", (int)config->Height);
533 height = 511;
534 supportedResolution = false;
535 }
536 }
537
538#ifdef EVE_SUPPORT_HSF
539 if (EVE_Hal_supportHsf(phost))
540 {
541 /* Render width */
542 config->Width = (int16_t)width;
543
544 /* Screen width */
545 config->HsfWidth = (int16_t)hsfWidth;
546
547 if (hsfWidth)
548 {
549 }
550 }
551 else
552#endif
553 if (hsfWidth)
554 {
555 /* Use the screen width if HSF is not supported */
556 config->Width = (int16_t)hsfWidth;
557 eve_printf_debug("Display config specifies HSF but not supported on this platform\n");
558 }
559 else
560 {
561 config->Width = (int16_t)width;
562 }
563 config->Height = (int16_t)height;
564
565 screenWidth = hsfWidth ? hsfWidth : width; /* Use screen width for calculation */
566 pixels = screenWidth * height;
567
568 /* Calculate maximum refresh rate */
569 minCycles = pixels + (pixels >> 2); /* pixels * 1.25 */
570#ifdef EVE_SUPPORT_HSF
571 maxRatePass = (freq - 1) / minCycles;
572 maxFreq = (freq << 1) < 96000000 ? (freq << 1) : 96000000;
573 if (hsfWidth)
574 maxRate = maxRatePass; // No 2X
575 else
576 maxRate = maxFreq / minCycles; // Support 2X
577#else
578 maxRate = freq / minCycles;
579#endif
580
581 /* If the refresh rate is too low, try with tighter settings */
582 if (refreshRate > maxRate)
583 {
584 minCycles = pixels + (pixels >> 3); /* pixels * 1.125 */
585#ifdef EVE_SUPPORT_HSF
586 maxRatePass = (freq - 1) / minCycles;
587 if (hsfWidth)
588 maxRate = maxRatePass; // No 2X
589 else
590 maxRate = maxFreq / minCycles; // Support 2X
591#else
592 maxRate = freq / minCycles;
593#endif
594 }
595
596#ifdef EVE_SUPPORT_HSF
597 if (EVE_Hal_supportHsf(phost) && (hsfWidth || refreshRate > maxRatePass))
598 {
599 /* Setup EXTSYNC with freq lower (not equal) than system frequency for HSF, up to 2X otherwise */
600 /* LL2 frequency = 12MHz * REG_PCLK_FREQ[8:4] */
601 /* PCLK frequency = PLL2 frequency / REG_PCLK_FREQ[3:0] / 2 */
602 /* PCLK frequency = 6MHz * REG_PCLK_FREQ[8:4] / REG_PCLK_FREQ[3:0] */
603 /* PCLK frequency = 6MHz * mul / div */
604 int32_t wantFreq = minCycles * (refreshRate > maxRate ? maxRate : refreshRate); // 25.92m for 800x480
605 config->PClkFreq = getPClkFreq(wantFreq);
606 pclkFreq = getFreqFromPClkFreq(config->PClkFreq);
607 if (pclkFreq)
608 maxRate = pclkFreq / minCycles;
609 }
610#endif
611
612#ifdef EVE_SUPPORT_HSF
613 if (pclkFreq)
614 {
615 pclk = 1;
616 }
617 else
618#endif
619 if (maxRate < refreshRate)
620 {
621 /* Trim unsupported framerate */
622 eve_printf_debug("Frame rate limited to %d\n", (unsigned int)maxRate);
623 refreshRate = maxRate;
624 pclk = 1;
625 }
626 else
627 {
628#ifdef EVE_SUPPORT_HSF
629 pclk = maxRatePass / refreshRate;
630#else
631 pclk = maxRate / refreshRate;
632#endif
633 }
634 config->PCLK = (uint8_t)pclk;
635
636 /* Change PCLKPol if the right edge of the display is noisy,
637 or if the display is showing jagged colors. Values 0 or 1.
638 This toggles the polarity of the PCLK. */
639 config->PCLKPol = 1; /* non-default */
640
641 /* Approximate an average good setting */
642#ifdef EVE_SUPPORT_HSF
643 cycles = (pclkFreq ? pclkFreq : freq) / (refreshRate * pclk);
644#else
645 cycles = freq / (refreshRate * pclk);
646#endif
647 vcycle = height * cycles / pixels; /* max vcycle without blanking, on average 1.125x height, or 1.25x */
648 vcycle = (vcycle + ((vcycle + height) >> 1) + height + height) >> 2; /* vcycle is 1.5 max vcycle + 2.5 height / 4, so on average 1.046875x height, or 1.09375x */
649 hcycle = cycles / vcycle; /* cycles div by vcycle, on average 1.07375x, or 1.1425x */
650 hoffset = (hcycle - screenWidth) >> 1;
651 voffset = (vcycle - height) >> 1;
652 hsync1 = hoffset >> 1;
653 vsync1 = voffset >> 1;
654 hoffset += hsync1;
655 voffset += vsync1;
656#if (EVE_SUPPORT_CHIPID >= EVE_BT817)
657 if (pclkFreq && pclkFreq >= freq)
658 {
659 hcycle &= ~1UL;
660 hoffset &= ~1UL;
661 hsync1 &= ~1UL;
662 }
663#endif
664 config->HCycle = (int16_t)hcycle;
665 config->HSync0 = 0;
666 config->HSync1 = (int16_t)hsync1;
667 config->HOffset = (int16_t)hoffset;
668 config->VCycle = (int16_t)vcycle;
669 config->VSync0 = 0;
670 config->VSync1 = (int16_t)vsync1;
671 config->VOffset = (int16_t)voffset;
672
673 /* Verify */
674 eve_assert(config->HSync1);
675 eve_assert(config->HOffset > config->HSync1);
676#ifdef EVE_SUPPORT_HSF
677 eve_assert(config->HCycle > (config->HsfWidth ? config->HsfWidth : config->Width));
678 eve_assert((config->HCycle - (config->HsfWidth ? config->HsfWidth : config->Width)) > config->HOffset);
679#else
680 eve_assert(config->HCycle > config->Width);
681 eve_assert((config->HCycle - config->Width) > config->HOffset);
682#endif
683 eve_assert(config->VSync1);
684 eve_assert(config->VOffset > config->VSync1);
685 eve_assert(config->VCycle > config->Height);
686 eve_assert((config->VCycle - config->Height) > config->VOffset);
687
688#if (EVE_SUPPORT_CHIPID >= EVE_BT815)
689#ifdef EVE_ADAPTIVE_FRAMERATE
690 config->AdaptiveFramerate = 1;
691#else
692 config->AdaptiveFramerate = 0;
693#endif
694#endif
695
696#if (EVE_SUPPORT_CHIPID >= EVE_BT817)
697 config->AhHCycleMax = 0;
698#endif
699
700 /* Other options */
701 /* Toggle CSpread if you see red and blue fringing on black and white edges */
702 config->CSpread = 0; /* non-default */
703 /* Change this if RGB colors are swapped */
704 config->Swizzle = 0;
705
706 if (EVE_CHIPID == EVE_FT812
710 || EVE_CHIPID >= EVE_BT815)
711 {
712 config->Dither = 0;
713 config->OutBitsR = 8;
714 config->OutBitsG = 8;
715 config->OutBitsB = 8;
716 }
717 else
718 {
719 config->Dither = 1;
720 config->OutBitsR = 6;
721 config->OutBitsG = 6;
722 config->OutBitsB = 6;
723 }
724
725 return supportedResolution;
726}
727
741{
742 uint32_t freq = EVE_Hal_rd32(phost, REG_FREQUENCY);
743 bool res = configDefaultsEx(phost, config, width, height, refreshRate, hsfWidth, freq);
744#ifdef _DEBUG
745#if (EVE_SUPPORT_CHIPID >= EVE_BT817)
746 if (config->PClkFreq)
747 {
748 uint32_t pclkFrequency = getFreqFromPClkFreq(config->PClkFreq);
749 eve_printf_debug("Display refresh rate set to %f Hz using pclk %X freq %f MHz\n", (float)((double)pclkFrequency / ((double)config->HCycle * (double)config->VCycle)), (int)config->PClkFreq, (float)((double)pclkFrequency * 0.000001));
750 }
751 else
752#endif
753 eve_printf_debug("Display refresh rate set to %f Hz\n", (float)((double)freq / ((double)config->HCycle * (double)config->VCycle * (double)config->PCLK)));
754#endif
755 return res;
756}
757
766{
767 bool supportedResolution;
768 uint32_t freq = EVE_Hal_rd32(phost, REG_FREQUENCY);
769 uint32_t width;
770 uint32_t height;
771 uint32_t refreshRate;
772 uint32_t hsfWidth;
773
774 /* NOTE: Update `EVE_DISPLAY_T` in `EVE_Util.h` when adding display presets. */
775 if (!display)
776 {
777 /* Default displays if none was explicitly chosen */
778#if defined(DISPLAY_RESOLUTION_QVGA)
780#elif defined(DISPLAY_RESOLUTION_WQVGA)
782#elif defined(DISPLAY_RESOLUTION_WVGA)
783#if defined(EVE_GRAPHICS_IDM2040)
785#else
787#endif
788#elif defined(DISPLAY_RESOLUTION_WSVGA)
790#elif defined(DISPLAY_RESOLUTION_HDTV)
792#elif defined(DISPLAY_RESOLUTION_WXGA)
794#elif defined(DISPLAY_RESOLUTION_HVGA_PORTRAIT)
796#else
797 if (EVE_CHIPID >= EVE_BT817)
799 else if (EVE_CHIPID >= EVE_BT815)
801 else if (EVE_CHIPID >= EVE_FT810)
803 else if (EVE_CHIPID >= EVE_FT800)
805#endif
806 }
807
808 width = s_DisplayResolutions[display][0];
809 height = s_DisplayResolutions[display][1];
810#if EVE_DISPLAY_REFRESH
811 refreshRate = EVE_DISPLAY_REFRESH;
812#else
813 refreshRate = s_DisplayResolutions[display][2];
814#endif
815 hsfWidth = s_DisplayResolutions[display][3];
816 supportedResolution = configDefaultsEx(phost, config, width, height, refreshRate, hsfWidth, freq);
817
818 /* Known values for specific display models */
819#if EVE_DISPLAY_REFRESH
820 if (EVE_DISPLAY_REFRESH != s_DisplayResolutions[display][2])
821 {
822 /* Don't use known preset values if the requested refresh rate differs from the default */
823 }
824 else
825#endif
826 if (display == EVE_DISPLAY_QVGA_320x240_56Hz && freq == 48000000)
827 {
828 /*
829 FT800 known values:
830 Resolution: 320x240
831 Refresh rate: 55.916Hz
832 */
833#if EVE_HARDCODED_DISPLAY_TIMINGS
834 if (supportedResolution)
835 {
836 config->Width = 320;
837 config->Height = 240;
838 config->HCycle = 408;
839 config->HOffset = 70;
840 config->HSync0 = 0;
841 config->HSync1 = 10;
842 config->VCycle = 263;
843 config->VOffset = 13;
844 config->VSync0 = 0;
845 config->VSync1 = 2;
846 config->PCLK = 8;
847#if (EVE_SUPPORT_CHIPID >= EVE_BT817)
848 config->PClkFreq = 0;
849#endif
850#ifdef EVE_SUPPORT_HSF
851 config->HsfWidth = 0;
852#endif
853 }
854#endif
855 config->Swizzle = 2;
856 config->PCLKPol = 0;
857 config->CSpread = 1;
858 config->Dither = 1;
859 }
860 else if (display == EVE_DISPLAY_WQVGA_480x272_60Hz && freq == 48000000)
861 {
862 /*
863 FT800 known values:
864 Display: LBL-T050BPH-01
865 REG_FREQUENCY: 48MHz
866 REG_PCLK: 5
867 REG_HCYCLE: 548
868 REG_VCYCLE: 292
869 Resolution: 480x272
870 Refresh rate: 59.994Hz
871 */
872#if EVE_HARDCODED_DISPLAY_TIMINGS
873 if (supportedResolution)
874 {
875 config->Width = 480;
876 config->Height = 272;
877 config->HCycle = 548;
878 config->HOffset = 43;
879 config->HSync0 = 0;
880 config->HSync1 = 41;
881 config->VCycle = 292;
882 config->VOffset = 12;
883 config->VSync0 = 0;
884 config->VSync1 = 10;
885 config->PCLK = 5;
886#if (EVE_SUPPORT_CHIPID >= EVE_BT817)
887 config->PClkFreq = 0;
888#endif
889#ifdef EVE_SUPPORT_HSF
890 config->HsfWidth = 0;
891#endif
892 }
893#endif
894 }
895#if (EVE_SUPPORT_CHIPID >= EVE_BT817)
896 else if (display == EVE_DISPLAY_WQVGA_480x272_60Hz && EVE_CHIPID >= EVE_BT817)
897 {
898 /* Same as above, but using PClkFreq */
899#if EVE_HARDCODED_DISPLAY_TIMINGS
900 if (supportedResolution)
901 {
902 config->Width = 480;
903 config->Height = 272;
904 config->HCycle = 548;
905 config->HOffset = 43;
906 config->HSync0 = 0;
907 config->HSync1 = 41;
908 config->VCycle = 292;
909 config->VOffset = 12;
910 config->VSync0 = 0;
911 config->VSync1 = 10;
912 config->PCLK = 1;
913 config->PClkFreq = 0x885; // 9.6MHz
914 config->HsfWidth = 0;
915 }
916#endif
917 }
918#endif
919 else if (display == EVE_DISPLAY_WVGA_800x480_74Hz && freq == 72000000)
920 {
921 /*
922 BT81X known values:
923 Display: MTF070TN83-V1
924 REG_FREQUENCY: 72MHz
925 Resolution: 800x480
926 Refresh rate: 73.892Hz
927 */
928#if EVE_HARDCODED_DISPLAY_TIMINGS
929 if (supportedResolution)
930 {
931 config->Width = 800;
932 config->Height = 480;
933 config->HCycle = 928;
934 config->HOffset = 88;
935 config->HSync0 = 0;
936 config->HSync1 = 48;
937 config->VCycle = 525;
938 config->VOffset = 32;
939 config->VSync0 = 0;
940 config->VSync1 = 3;
941 config->PCLK = 2;
942#if (EVE_SUPPORT_CHIPID >= EVE_BT817)
943 config->PClkFreq = 0;
944#endif
945#ifdef EVE_SUPPORT_HSF
946 config->HsfWidth = 0;
947#endif
948 }
949#endif
950 }
951 else if (display == EVE_DISPLAY_HDTV_1280x720_58Hz && freq == 72000000)
952 {
953 /*
954 BT815 known values:
955 REG_FREQUENCY: 72MHz
956 Resolution: 1280x720
957 Refresh rate: 58.182Hz
958 */
959#if EVE_HARDCODED_DISPLAY_TIMINGS
960 if (supportedResolution)
961 {
962 config->Width = 1280;
963 config->Height = 720;
964 config->HCycle = 1650;
965 config->HOffset = 40 + 220;
966 config->HSync0 = 0;
967 config->HSync1 = 40;
968 config->VCycle = 750;
969 config->VOffset = 5 + 20;
970 config->VSync0 = 0;
971 config->VSync1 = 5;
972 config->PCLK = 1;
973#if (EVE_SUPPORT_CHIPID >= EVE_BT817)
974 config->PClkFreq = 0;
975#endif
976#ifdef EVE_SUPPORT_HSF
977 config->HsfWidth = 0;
978#endif
979 }
980#endif
981 config->CSpread = 0;
982 config->Dither = 0;
983 config->PCLKPol = 0;
984 config->OutBitsR = 0;
985 config->OutBitsG = 0;
986 config->OutBitsB = 0;
987 }
988 else if (display == EVE_DISPLAY_WXGA_1280x800_57Hz)
989 {
990 /*
991 BT817 known values:
992 Resolution: 1280x800
993 Refresh rate: 57.393Hz
994 */
995#if (EVE_SUPPORT_CHIPID >= EVE_BT817)
996#if EVE_HARDCODED_DISPLAY_TIMINGS
997 if (EVE_CHIPID >= EVE_BT817 && supportedResolution)
998 {
999 config->Width = 1280;
1000 config->Height = 800;
1001 config->HCycle = 1411;
1002 config->HOffset = 120;
1003 config->HSync0 = 0;
1004 config->HSync1 = 100;
1005 config->VCycle = 815;
1006 config->VOffset = 14;
1007 config->VSync0 = 0;
1008 config->VSync1 = 10;
1009 config->PCLK = 1;
1010 config->PClkFreq = 0x8B1; // 66MHz
1011#ifdef EVE_SUPPORT_HSF
1012 config->HsfWidth = 0;
1013#endif
1014 }
1015#endif
1016#endif
1017 config->CSpread = 0;
1018 config->Dither = 0;
1019 config->PCLKPol = 0;
1020 config->OutBitsR = 0;
1021 config->OutBitsG = 0;
1022 config->OutBitsB = 0;
1023 }
1024 else if (display == EVE_DISPLAY_WSVGA_1024x600_59Hz)
1025 {
1026 /*
1027 BT817 known values:
1028 Resolution: 1024x600
1029 Refresh rate: 59.758Hz
1030 */
1031#if (EVE_SUPPORT_CHIPID >= EVE_BT817)
1032#if EVE_HARDCODED_DISPLAY_TIMINGS
1033 if (EVE_CHIPID >= EVE_BT817 && supportedResolution)
1034 {
1035 config->Width = 1024;
1036 config->Height = 600;
1037 config->HCycle = 1344;
1038 config->HOffset = 160;
1039 config->HSync0 = 0;
1040 config->HSync1 = 100;
1041 config->VCycle = 635;
1042 config->VOffset = 23;
1043 config->VSync0 = 0;
1044 config->VSync1 = 10;
1045 config->PCLK = 1;
1046 config->PClkFreq = 0xD12; // 51Mhz
1047#ifdef EVE_SUPPORT_HSF
1048 config->HsfWidth = 0;
1049#endif
1050 }
1051#endif
1052#endif
1053 config->CSpread = 0;
1054 config->Dither = 1;
1055 config->PCLKPol = 1;
1056 config->OutBitsR = 0;
1057 config->OutBitsG = 0;
1058 config->OutBitsB = 0;
1059 }
1060
1061#if (EVE_SUPPORT_CHIPID >= EVE_BT817)
1062 /* Disable max cycle if incompatible hardcoded timings are used */
1063 if (config->AhHCycleMax && config->HCycle > config->AhHCycleMax)
1064 config->AhHCycleMax = 0;
1065#endif
1066
1067#ifdef _DEBUG
1068#if (EVE_SUPPORT_CHIPID >= EVE_BT817)
1069 if (config->PClkFreq)
1070 {
1071 uint32_t pclkFrequency = getFreqFromPClkFreq(config->PClkFreq);
1072 eve_printf_debug("Display refresh rate set to %f Hz using pclk %X freq %f MHz\n", (float)((double)pclkFrequency / ((double)config->HCycle * (double)config->VCycle)), (int)config->PClkFreq, (float)((double)pclkFrequency * 0.000001));
1073 }
1074 else
1075#endif
1076 eve_printf_debug("Display refresh rate set to %f Hz\n", (float)((double)freq / ((double)config->HCycle * (double)config->VCycle * (double)config->PCLK)));
1077#endif
1078
1079#if 0
1080 eve_printf_debug("Width: %i\n", (int)config->Width);
1081 eve_printf_debug("Height: %i\n", (int)config->Height);
1082 eve_printf_debug("HCycle: %i\n", (int)config->HCycle);
1083 eve_printf_debug("HOffset: %i\n", (int)config->HOffset);
1084 eve_printf_debug("HSync0: %i\n", (int)config->HSync0);
1085 eve_printf_debug("HSync1: %i\n", (int)config->HSync1);
1086 eve_printf_debug("VCycle: %i\n", (int)config->VCycle);
1087 eve_printf_debug("VOffset: %i\n", (int)config->VOffset);
1088 eve_printf_debug("VSync0: %i\n", (int)config->VSync0);
1089 eve_printf_debug("VSync1: %i\n", (int)config->VSync1);
1090 eve_printf_debug("PCLK: %i\n", (int)config->PCLK);
1091 eve_printf_debug("Swizzle: %i\n", (int)config->Swizzle);
1092 eve_printf_debug("PCLKPol: %i\n", (int)config->PCLKPol);
1093 eve_printf_debug("CSpread: %i\n", (int)config->CSpread);
1094 eve_printf_debug("OutBitsR: %i\n", (int)config->OutBitsR);
1095 eve_printf_debug("OutBitsG: %i\n", (int)config->OutBitsG);
1096 eve_printf_debug("OutBitsB: %i\n", (int)config->OutBitsB);
1097 eve_printf_debug("Dither: %i\n", (int)config->Dither);
1098#if (EVE_SUPPORT_CHIPID >= EVE_BT815)
1099 eve_printf_debug("AdaptiveFramerate: %i\n", (int)config->AdaptiveFramerate);
1100#endif
1101#if (EVE_SUPPORT_CHIPID >= EVE_BT817)
1102 eve_printf_debug("PClkFreq: %X\n", (int)config->PClkFreq);
1103 eve_printf_debug("AhHCycleMax: %i\n", (int)config->AhHCycleMax);
1104#endif
1105#ifdef EVE_SUPPORT_HSF
1106 eve_printf_debug("HsfWidth: %i\n", (int)config->HsfWidth);
1107#endif
1108#endif
1109}
1110
1111#define EXTRACT_CHIPID(romChipId) EVE_extendedChipId((((romChipId) >> 8) & 0xFF) | (((romChipId) & (0xFF)) << 8))
1112
1122{
1123 /* IMPORTANT: Do not use EVE_CoCmd functions here, as they can be overridden by hooks */
1124
1125 const uint32_t expectedChipId = EVE_CHIPID;
1126 uint8_t engineStatus;
1127 uint32_t chipId;
1128 uint8_t id;
1129
1130 do
1131 {
1132 int tries = 0;
1133
1134 /* EVE will be in SPI Single channel after POR */
1135 if (!EVE_Hal_powerCycle(phost, true))
1136 return false;
1137
1138 if (bootup->ExternalOsc)
1139 {
1140 /* Set the clk to external clock. Must disable it when no external clock source on the board */
1142 EVE_sleep(10);
1143 }
1144 else
1145 {
1146 /* Send CLKINT explicitly to play safe for all EVE series chip*/
1148 EVE_sleep(10);
1149 }
1150
1151 /* Update system clock as per user selected */
1152 if (bootup->SystemClock)
1153 {
1154 EVE_Host_selectSysClk(phost, bootup->SystemClock);
1155 }
1156
1157 /* Access address 0 to wake up the FT800 */
1159 EVE_sleep(300);
1160
1161 EVE_Host_coreReset(phost);
1162 /* Wait for valid chip ID */
1163 chipId = EVE_Hal_rd32(phost, ROM_CHIPID);
1164 while (EXTRACT_CHIPID(chipId) < EVE_FT800
1165 || EXTRACT_CHIPID(chipId) > EVE_BT818)
1166 {
1167 eve_printf_debug("EVE ROM_CHIPID after wake up %lx\n", (unsigned long)chipId);
1168
1169 ++tries;
1170 EVE_sleep(20);
1171 if (phost->CbCmdWait && !phost->CbCmdWait(phost))
1172 return false;
1173
1174 chipId = EVE_Hal_rd32(phost, ROM_CHIPID);
1175
1176#ifdef EVE_MULTI_TARGET
1177 /* Turn off external clock if chipId reads incorrectly for 4 times */
1178 /* When not targeting multiple boards, set the EVE_USE_INTERNAL_OSC preprocessor variable, or configure the ExternalOsc parameter at runtime */
1179 if (tries >= 4)
1180 {
1181 bootup->ExternalOsc = !bootup->ExternalOsc;
1182 eve_printf_debug("Reattempting bootup with ExternalOsc changed from %s to %s\n", bootup->ExternalOsc ? "OFF" : "ON", bootup->ExternalOsc ? "ON" : "OFF");
1183
1184 EVE_Host_powerModeSwitch(phost, EVE_SLEEP_M); /* Put EVE into Sleep mode, prepare for clock selection */
1185 chipId = 0; //In case chipId is not zero
1186 break;
1187 }
1188#endif
1189 }
1190 } while (!chipId);
1191
1192 /* Validate chip ID to ensure the correct HAL is used */
1193 /* ROM_CHIPID is valid across all EVE devices */
1194 if (expectedChipId && EXTRACT_CHIPID(chipId) != expectedChipId)
1195 eve_printf_debug("Mismatching EVE chip id %lx, expect model %lx\n", (unsigned long)((chipId >> 8) & 0xFF) | ((chipId & 0xFF) << 8), (unsigned long)expectedChipId);
1196 eve_printf_debug("EVE chip id %lx %lx.%lx (EVE gen %i)\n", (unsigned long)EVE_shortChipId(EXTRACT_CHIPID(chipId)), (unsigned long)((chipId >> 16) & 0xFF), (unsigned long)((chipId >> 24) & 0xFF), EVE_gen(EXTRACT_CHIPID(chipId)));
1197
1198 /* Switch to the proper chip ID if applicable */
1199#ifdef EVE_MULTI_GRAPHICS_TARGET
1200 phost->ChipId = EXTRACT_CHIPID(chipId);
1201 if (phost->ChipId >= EVE_BT815)
1202 phost->GpuDefs = &EVE_GpuDefs_BT81X;
1203 else if (phost->ChipId >= EVE_BT880)
1204 phost->GpuDefs = &EVE_GpuDefs_BT88X;
1205 else if (phost->ChipId >= EVE_FT810)
1206 phost->GpuDefs = &EVE_GpuDefs_FT81X;
1207 else if (phost->ChipId >= EVE_FT800)
1208 phost->GpuDefs = &EVE_GpuDefs_FT80X;
1209#endif
1210
1211 /* Turn off previous audio playback (in case powerdown is not connected) */
1213
1214#ifdef EVE_MULTI_GRAPHICS_TARGET
1215 /* The default clock configuration is only set when the graphics target is known at compile time */
1216 if (!bootup->SystemClock)
1217 {
1218 if (phost->ChipId >= EVE_BT815)
1219 {
1220 /* No clock was set, but we want 72MHz default for BT81X and up */
1221 eve_printf_debug("Set system clock PLL to 72MHz\n");
1222 bootup->SystemClock = EVE_SYSCLK_72M;
1223
1224 /* Sleep */
1226 EVE_sleep(300);
1227
1228 /* Update system clock */
1229 EVE_Host_selectSysClk(phost, bootup->SystemClock);
1230
1231 /* Access address 0 to wake up the FT800 */
1233 EVE_sleep(300);
1234 }
1235 else if (phost->ChipId >= EVE_FT810)
1236 {
1237 /* Assume the default */
1238 bootup->SystemClock = EVE_SYSCLK_60M;
1239 }
1240 }
1241#endif
1242
1243 /* Read Register ID to check if EVE is ready. */
1244 while ((id = EVE_Hal_rd8(phost, REG_ID)) != 0x7C)
1245 {
1246 eve_printf_debug("EVE register ID after wake up %x\n", (unsigned int)id);
1247
1248 EVE_sleep(20);
1249 if (phost->CbCmdWait && !phost->CbCmdWait(phost))
1250 return false;
1251 }
1252 eve_printf_debug("EVE register ID after wake up %x\n", (unsigned int)id);
1253 eve_assert(chipId == EVE_Hal_rd32(phost, ROM_CHIPID));
1254
1255 /* Read REG_CPURESET to check if engines are ready.
1256 Bit 0 for coprocessor engine,
1257 Bit 1 for touch engine,
1258 Bit 2 for audio engine.
1259 */
1260 while ((engineStatus = EVE_Hal_rd8(phost, REG_CPURESET)) != 0x00)
1261 {
1262 if (engineStatus & 0x01)
1263 {
1264 eve_printf_debug("Coprocessor engine is not ready\n");
1265 }
1266 if (engineStatus & 0x02)
1267 {
1268 eve_printf_debug("Touch engine is not ready\n");
1269 if (EVE_HOST == EVE_HOST_BT8XXEMU) // FIXME: Emulator REG_CPURESET bits behaviour on FT800 and FT801 doesn't match
1270 {
1271 EVE_Hal_wr8(phost, REG_CPURESET, 0);
1272 }
1273 }
1274 if (engineStatus & 0x04)
1275 {
1276 eve_printf_debug("Audio engine is not ready\n");
1277 }
1278
1279 EVE_sleep(20);
1280 if (phost->CbCmdWait && !phost->CbCmdWait(phost))
1281 return false;
1282 }
1283 eve_printf_debug("All engines are ready\n");
1284
1285 /* Update REG_FREQUENCY as per user selected */
1286 if (bootup->SystemClock != EVE_SYSCLK_DEFAULT)
1287 {
1288 uint32_t clockMHz = bootup->SystemClock * 12;
1289 EVE_Hal_wr32(phost, REG_FREQUENCY, clockMHz * 1000 * 1000);
1290 EVE_Hal_flush(phost);
1291 eve_printf_debug("EVE clock frequency set to %d MHz\n", (unsigned int)clockMHz);
1292 }
1293 else
1294 {
1295 eve_printf_debug("EVE default clock is %d MHz\n", (unsigned int)(EVE_Hal_rd32(phost, REG_FREQUENCY) / 1000000));
1296 }
1297
1298 /* Switch to configured default SPI channel mode */
1299#if (EVE_SUPPORT_CHIPID >= EVE_FT810)
1300 if (EVE_CHIPID >= EVE_FT810)
1301 {
1302 EVE_Hal_setSPI(phost, bootup->SpiChannels, bootup->SpiDummyBytes);
1303#ifdef _DEBUG
1304 const char *spiChannels = (phost->SpiChannels == EVE_SPI_QUAD_CHANNEL)
1305 ? "Quad"
1306 : ((phost->SpiChannels == EVE_SPI_DUAL_CHANNEL) ? "Dual" : "Single");
1307 const char *requested = (bootup->SpiChannels == EVE_SPI_QUAD_CHANNEL)
1308 ? "Quad"
1309 : ((bootup->SpiChannels == EVE_SPI_DUAL_CHANNEL) ? "Dual" : "Single");
1310 if (bootup->SpiChannels == phost->SpiChannels)
1311 eve_printf_debug("%s channel SPI\n", spiChannels);
1312 else
1313 eve_printf_debug("%s channel SPI (%s unsupported)\n", spiChannels, requested);
1314#endif
1315 }
1316#endif
1317
1318 /* In some cases, for example, QSPI on MM817EV board with MM930LITE,
1319 the drive strength is required to adjust for better signal integrity.
1320 Setting to max current 20mA here is to play safe. Users may adjust the
1321 corresponding bits of REG_GPIOX according to their own situation. */
1322 if (EVE_CHIPID >= EVE_FT810)
1323 {
1324 EVE_Hal_wr16(phost, REG_GPIOX_DIR, 0xffff);
1325 EVE_Hal_wr16(phost, REG_GPIOX, 0xffff);
1326 }
1327 else
1328 {
1329 EVE_Hal_wr8(phost, REG_GPIO_DIR, 0xff);
1330 EVE_Hal_wr8(phost, REG_GPIO, 0xff);
1331 }
1332
1333 /* Update touch firmware */
1334#ifdef _MSC_VER
1335#pragma warning(push)
1336#pragma warning(disable : 6285)
1337#endif
1338 if ((EVE_CHIPID == EVE_FT811 || EVE_CHIPID == EVE_FT813) && (EVE_HOST != EVE_HOST_BT8XXEMU))
1339#ifdef _MSC_VER
1340#pragma warning(pop)
1341#endif
1342 {
1343#if defined(PANL70) || defined(PANL70PLUS)
1344 EVE_Hal_wr8(phost, REG_CPURESET, 2);
1345 EVE_Hal_wr16(phost, REG_TOUCH_CONFIG, 0x05d0);
1346#endif
1347 /* Download new firmware to fix pen up issue */
1348 /* It may cause resistive touch not working any more*/
1349 uploadTouchFirmware(phost);
1350#if defined(PANL70) || defined(PANL70PLUS)
1352#endif
1353 EVE_Hal_flush(phost);
1354 EVE_sleep(100);
1355 }
1356
1357#ifdef _MSC_VER
1358#pragma warning(push)
1359#pragma warning(disable : 6285)
1360#endif
1361 if ((EVE_CHIPID == EVE_BT815 || EVE_CHIPID == EVE_BT817) && (EVE_HOST != EVE_HOST_BT8XXEMU))
1362#ifdef _MSC_VER
1363#pragma warning(pop)
1364#endif
1365 {
1366#if defined(DISPLAY_RESOLUTION_WXGA)
1367 //Assume WXGA is using Goodix touch IC
1368 //To be fixed: use macro for touch configuration
1369 eve_printf_debug("Setup touch for Goodix\n");
1370 EVE_Hal_wr8(phost, REG_CPURESET, 2);
1371 EVE_Hal_wr16(phost, REG_TOUCH_CONFIG, 0x05d0);
1372 EVE_Hal_wr8(phost, REG_CPURESET, 0);
1373#endif
1374 }
1375
1376#if (EVE_SUPPORT_CHIPID >= EVE_BT815)
1377#ifdef EVE_TOUCH_GOODIX
1378 eve_printf_debug("Goodix configuration\n");
1379 EVE_Hal_wr8(phost, REG_CPURESET, 2);
1380 EVE_Hal_wr16(phost, REG_TOUCH_CONFIG, 0x05d0);
1381 EVE_Hal_wr8(phost, REG_CPURESET, 0);
1382#endif
1383#endif
1384
1385 return true;
1386}
1387
1397{
1398 /* IMPORTANT: Do not use EVE_CoCmd functions here, as they can be overridden by hooks */
1399
1400 bool reconfigure = phost->PCLK;
1401 if (reconfigure)
1402 {
1403 /* If PCLK is already set, we're reconfiguring. Quiet flush first */
1404 eve_printf_debug("Reconfiguring\n");
1405 EVE_Cmd_waitFlush(phost);
1406 }
1407
1408 const bool swapXY = EVE_CHIPID >= EVE_FT810 ? EVE_Hal_rd8(phost, REG_ROTATE) & 0x2 : false;
1409 uint16_t wp;
1410 uint16_t rp;
1411
1412 if (EVE_CHIPID < EVE_FT810)
1413 {
1414 eve_assert(config->Width < 512);
1415 eve_assert(config->Height < 512);
1416 }
1417 else
1418 {
1419 eve_assert(config->Width < 2048);
1420 eve_assert(config->Height < 2048);
1421 }
1422
1423 /* Turn off display output clock */
1424 EVE_Hal_wr8(phost, REG_PCLK, 0);
1425 phost->PCLK = 0;
1426
1427 EVE_Hal_wr16(phost, REG_HCYCLE, config->HCycle);
1428 EVE_Hal_wr16(phost, REG_HOFFSET, config->HOffset);
1429 EVE_Hal_wr16(phost, REG_HSYNC0, config->HSync0);
1430 EVE_Hal_wr16(phost, REG_HSYNC1, config->HSync1);
1431 EVE_Hal_wr16(phost, REG_VCYCLE, config->VCycle);
1432 EVE_Hal_wr16(phost, REG_VOFFSET, config->VOffset);
1433 EVE_Hal_wr16(phost, REG_VSYNC0, config->VSync0);
1434 EVE_Hal_wr16(phost, REG_VSYNC1, config->VSync1);
1435 EVE_Hal_wr8(phost, REG_SWIZZLE, config->Swizzle);
1436 EVE_Hal_wr8(phost, REG_PCLK_POL, config->PCLKPol);
1437 EVE_Hal_wr16(phost, REG_HSIZE, config->Width);
1438 EVE_Hal_wr16(phost, REG_VSIZE, config->Height);
1439 EVE_Hal_wr16(phost, REG_CSPREAD, config->CSpread);
1440 EVE_Hal_wr16(phost, REG_DITHER, config->Dither);
1442 (uint16_t)(((config->OutBitsR & 0x7) << 6)
1443 | ((config->OutBitsG & 0x7) << 3)
1444 | (config->OutBitsB & 0x7)));
1445 if (swapXY)
1446 {
1447 phost->Width = config->Height;
1448 phost->Height = config->Width;
1449 }
1450 else
1451 {
1452 phost->Width = config->Width;
1453 phost->Height = config->Height;
1454 }
1455
1456#if (EVE_SUPPORT_CHIPID >= EVE_BT817)
1457 bool pclk2X = false;
1458 if (EVE_CHIPID >= EVE_BT817)
1459 {
1460 uint32_t freq = EVE_Hal_rd32(phost, REG_FREQUENCY);
1461 uint16_t pclkFreq = config->PClkFreq;
1462 if (!pclkFreq)
1463 {
1464 /* If not configured, match system frequency, in case a legacy configuration with PClk 1 is specified */
1465 /* PClk Freq is 60MHz by default, but we raise the system frequency to 72MHz during bootup */
1466 uint32_t refMul = freq / 6000000;
1467 uint32_t pllFreq = 12 * refMul;
1468 uint32_t range = pllFreq >= 160 ? 3 : (pllFreq >= 80 ? 2 : (pllFreq >= 40 ? 1 : 0));
1469 pclkFreq = 1 | ((refMul & 0x3F) << 4) | (range << 10);
1470 }
1471 EVE_Hal_wr32(phost, REG_PCLK_FREQ, pclkFreq);
1472 if (config->PClk2X)
1473 {
1474 /* User forces 2X */
1475 EVE_Hal_wr8(phost, REG_PCLK_2X, 1);
1476 }
1477 else if (config->PCLK == 1)
1478 {
1479 /* Send two pixels per system clock to the EXTSYNC block if PCLK_frequency > SystemClock */
1480 uint32_t pclkFrequency = getFreqFromPClkFreq(pclkFreq);
1481 pclk2X = pclkFrequency > freq;
1482 EVE_Hal_wr8(phost, REG_PCLK_2X, pclk2X);
1483 }
1484 else
1485 {
1486 EVE_Hal_wr8(phost, REG_PCLK_2X, 0);
1487 }
1488 }
1489#endif
1490
1491#if (EVE_SUPPORT_CHIPID >= EVE_BT815)
1492 if (EVE_CHIPID >= EVE_BT815)
1493 {
1495 }
1496#endif
1497
1498#if (EVE_SUPPORT_CHIPID >= EVE_BT817)
1499 if (EVE_CHIPID >= EVE_BT817)
1500 {
1501 EVE_Hal_wr16(phost, REG_AH_HCYCLE_MAX, config->AhHCycleMax);
1502 }
1503#endif
1504
1505#ifdef RESISTANCE_THRESHOLD /* TODO: From config */
1506 if (EVE_Hal_isScreenResistive(phost))
1507 {
1508 /* Touch configuration - configure the resistance value to 1200 - this value is specific to customer requirement and derived by experiment */
1509 /* TODO: From config */
1511 }
1512#endif
1513
1514 EVE_Util_clearScreen(phost);
1515
1516 /* Refresh fifo */
1517 wp = EVE_Cmd_wp(phost);
1518 rp = EVE_Cmd_rp(phost);
1519 EVE_Cmd_space(phost);
1520#if (EVE_SUPPORT_CHIPID >= EVE_FT810)
1521 if (EVE_CHIPID >= EVE_FT810)
1522 {
1523 phost->MediaFifoAddress = 0;
1524 phost->MediaFifoSize = 0;
1525 }
1526#endif
1527
1528 /* Coprocessor needs a reset */
1529 if (reconfigure ? EVE_CMD_FAULT(rp) : (wp || rp))
1530 {
1531 /* If this occurs after powerdown and bootup, powerdown is not working */
1532 eve_printf_debug("Coprocessor fifo not empty\n");
1533 if (!EVE_Util_resetCoprocessor(phost))
1534 return false;
1535 }
1536 else
1537 {
1538 /* Setup coprocessor defaults */
1539 eve_printf_debug("Configure coprocessor defaults\n");
1540 EVE_Cmd_wr32(phost, CMD_DLSTART);
1542
1543 /* Set the latest API level.
1544 CMD_APILEVEL applicable since BT817. */
1545#if (EVE_SUPPORT_CHIPID >= EVE_BT817)
1546 if (EVE_CHIPID >= EVE_BT817)
1547 {
1548 EVE_Cmd_wr32(phost, CMD_APILEVEL);
1549 EVE_Cmd_wr32(phost, 2);
1550 }
1551#endif
1552
1553#ifdef EVE_FLASH_AVAILABLE
1554 if (EVE_CHIPID >= EVE_BT815)
1555 {
1556 /* Reattach flash to avoid inconsistent state */
1559 }
1560#endif
1561
1563 }
1564
1565#ifdef EVE_SUPPORT_HSF
1566 if (config->HsfWidth)
1567 {
1568 if (EVE_Hal_supportHsf(phost) && !pclk2X)
1569 {
1570 EVE_Cmd_startFunc(phost);
1571 EVE_Cmd_wr32(phost, CMD_HSF);
1572 EVE_Cmd_wr32(phost, config->HsfWidth);
1573 EVE_Cmd_endFunc(phost);
1574 }
1575 else
1576 {
1577 /* Set width to screen width if HSF is not supported here */
1578 eve_printf_debug("HSF specified but not supported on this device\n");
1579 EVE_Hal_wr16(phost, REG_HSIZE, config->HsfWidth);
1580 if (swapXY)
1581 phost->Height = config->HsfWidth;
1582 else
1583 phost->Width = config->HsfWidth;
1584 }
1585 }
1586#endif
1587
1588 EVE_Hal_flush(phost);
1589 if (!EVE_Cmd_waitFlush(phost))
1590 return false;
1591
1592 EVE_Hal_wr8(phost, REG_PCLK, config->PCLK); /* After this display is visible on the LCD */
1593 phost->PCLK = config->PCLK;
1594
1595#ifdef EVE_SUPPORT_HSF
1596 if (config->HsfWidth)
1597 {
1598 if (EVE_Hal_supportHsf(phost))
1599 {
1600 eve_printf_debug("HSize: %i, HSF HSize: %i\n", EVE_Hal_rd32(phost, REG_HSIZE), EVE_Hal_rd32(phost, REG_HSF_HSIZE));
1601 }
1602 }
1603#endif
1604
1605#if (defined(ENABLE_ILI9488_HVGA_PORTRAIT) || defined(ENABLE_KD2401_HVGA_PORTRAIT))
1606 /* to cross check reset pin */
1607 EVE_Hal_wr8(phost, REG_GPIO, 0xff);
1608 EVE_Hal_flush(phost);
1609 EVE_sleep(120);
1610 EVE_Hal_wr8(phost, REG_GPIO, 0x7f);
1611 EVE_Hal_flush(phost);
1612 EVE_sleep(120);
1613 EVE_Hal_wr8(phost, REG_GPIO, 0xff);
1614 EVE_Hal_flush(phost);
1615 EVE_sleep(120);
1616#endif
1617
1618#if defined(ENABLE_ILI9488_HVGA_PORTRAIT) || defined(ENABLE_KD2401_HVGA_PORTRAIT)
1619#if defined(RP2040_PLATFORM)
1620 EVE_ILI9488_RP2040_bootup(phost);
1621 eve_printf_debug("after ILI9488 RP2040 bootup\n");
1622 EVE_Hal_restoreSPI(phost);
1623#endif
1624#if defined(FT9XX_PLATFORM)
1625 EVE_ILI9488_bootup();
1626 eve_printf_debug("after ILI9488 bootup\n");
1627 EVE_Hal_restoreSPI(phost);
1628#endif
1629#endif
1630
1631#if 0
1632 eve_printf_debug("REG_HSIZE: %i\n", (int)EVE_Hal_rd16(phost, REG_HSIZE));
1633 eve_printf_debug("REG_VSIZE: %i\n", (int)EVE_Hal_rd16(phost, REG_VSIZE));
1634 eve_printf_debug("REG_HCYCLE: %i\n", (int)EVE_Hal_rd16(phost, REG_HCYCLE));
1635 eve_printf_debug("REG_HOFFSET: %i\n", (int)EVE_Hal_rd16(phost, REG_HOFFSET));
1636 eve_printf_debug("REG_HSYNC0: %i\n", (int)EVE_Hal_rd16(phost, REG_HSYNC0));
1637 eve_printf_debug("REG_HSYNC1: %i\n", (int)EVE_Hal_rd16(phost, REG_HSYNC1));
1638 eve_printf_debug("REG_VCYCLE: %i\n", (int)EVE_Hal_rd16(phost, REG_VCYCLE));
1639 eve_printf_debug("REG_VOFFSET: %i\n", (int)EVE_Hal_rd16(phost, REG_VOFFSET));
1640 eve_printf_debug("REG_VSYNC0: %i\n", (int)EVE_Hal_rd16(phost, REG_VSYNC0));
1641 eve_printf_debug("REG_VSYNC1: %i\n", (int)EVE_Hal_rd16(phost, REG_VSYNC1));
1642 eve_printf_debug("REG_PCLK: %i\n", (int)EVE_Hal_rd16(phost, REG_PCLK));
1643 eve_printf_debug("REG_SWIZZLE: %i\n", (int)EVE_Hal_rd16(phost, REG_SWIZZLE));
1644 eve_printf_debug("REG_PCLK_POL: %i\n", (int)EVE_Hal_rd16(phost, REG_PCLK_POL));
1645 eve_printf_debug("REG_CSPREAD: %i\n", (int)EVE_Hal_rd16(phost, REG_CSPREAD));
1646 eve_printf_debug("REG_OUTBITS: %i\n", (int)EVE_Hal_rd16(phost, REG_OUTBITS));
1647 eve_printf_debug("REG_DITHER: %i\n", (int)EVE_Hal_rd16(phost, REG_DITHER));
1648#if (EVE_SUPPORT_CHIPID >= EVE_BT815)
1649 eve_printf_debug("REG_ADAPTIVE_FRAMERATE: %i\n", (int)EVE_Hal_rd16(phost, REG_ADAPTIVE_FRAMERATE));
1650#endif
1651#if (EVE_SUPPORT_CHIPID >= EVE_BT817)
1652 eve_printf_debug("REG_PCLK_FREQ: %X\n", (int)EVE_Hal_rd16(phost, REG_PCLK_FREQ));
1653 eve_printf_debug("REG_AH_HCYCLE_MAX: %i\n", (int)EVE_Hal_rd16(phost, REG_AH_HCYCLE_MAX));
1654 eve_printf_debug("REG_HSF_HSIZE: %i\n", (int)EVE_Hal_rd16(phost, REG_HSF_HSIZE));
1655#endif
1656#endif
1657
1658 eve_printf_debug("EVE configuration ready\n");
1659 return true;
1660}
1661
1668{
1669 if (EVE_CHIPID >= EVE_FT810)
1670 {
1671 EVE_Hal_wr16(phost, REG_GPIOX_DIR, 0xffff);
1672 EVE_Hal_wr16(phost, REG_GPIOX, 0);
1673 }
1674 else
1675 {
1676 EVE_Hal_wr8(phost, REG_GPIO_DIR, 0xff);
1677 EVE_Hal_wr8(phost, REG_GPIO, 0);
1678 }
1679 EVE_Hal_wr8(phost, REG_PCLK, 0);
1680 EVE_Hal_flush(phost);
1681 EVE_Hal_powerCycle(phost, false);
1682}
1683
1688#define EVE_SUBPATCH_PTR 0x7ffeU /* NOTE: This address is only valid for FT81X */
1695static inline bool EVE_Util_needsSubPatch(EVE_HalContext *phost)
1696{
1697 return (EVE_CHIPID >= EVE_FT810) && (EVE_CHIPID <= EVE_FT813);
1698}
1699
1704#define EVE_VIDEOPATCH_ADDR 0x309162UL /* NOTE: This address is only valid for BT815 and BT816 */
1712{
1713 return (EVE_CHIPID >= EVE_BT815) && (EVE_CHIPID <= EVE_BT816);
1714}
1715
1716#if defined(_DEBUG)
1723{
1724 if (!phost->DebugMessageVisible)
1725 {
1726 EVE_Hal_rdMem(phost, phost->DebugBackup, RAM_G + RAM_G_SIZE - sizeof(phost->DebugBackup), sizeof(phost->DebugBackup));
1727 phost->DebugMessageVisible = true;
1728 }
1729}
1736{
1737 if (phost->DebugMessageVisible)
1738 {
1739 EVE_Hal_wrMem(phost, RAM_G + RAM_G_SIZE - sizeof(phost->DebugBackup), phost->DebugBackup, sizeof(phost->DebugBackup));
1740 phost->DebugMessageVisible = false;
1741 }
1742}
1743#endif
1744
1753{
1754 /* IMPORTANT: Do not use EVE_CoCmd functions here, as they can be overridden by hooks */
1755
1756 const bool needsVideoPatch = EVE_Util_needsVideoPatch(phost);
1757 uint16_t videoPatchVector;
1758 bool ready;
1759 uint16_t rd;
1760 uint16_t wr;
1761 (void)rd;
1762 (void)wr;
1763
1764 eve_printf_debug("Reset coprocessor\n");
1765
1766 if (needsVideoPatch)
1767 {
1768 /* BT81X video patch */
1769 videoPatchVector = EVE_Hal_rd16(phost, EVE_VIDEOPATCH_ADDR);
1770 }
1771
1772 /* Set REG_CPURESET to 1, to hold the coprocessor in the reset condition */
1773 EVE_Hal_wr8(phost, REG_CPURESET, 1);
1774 EVE_Hal_flush(phost);
1775 EVE_sleep(100);
1776
1777 /* Set REG_CMD_READ and REG_CMD_WRITE to zero */
1778 EVE_Hal_wr16(phost, REG_CMD_READ, 0);
1779 EVE_Hal_wr16(phost, REG_CMD_WRITE, 0);
1780 EVE_Hal_wr16(phost, REG_CMD_DL, 0);
1781 EVE_Hal_wr8(phost, REG_PCLK, phost->PCLK); /* j1 will set the pclk to 0 for that error case */
1782
1783 /* Stop playing audio in case video with audio was playing during reset */
1784 EVE_Hal_wr8(phost, REG_PLAYBACK_PLAY, 0);
1785
1786 if (EVE_Util_needsSubPatch(phost))
1787 {
1788 /* Enable patched rom in case the reset is requested while CMD_LOGO is running.
1789 This is necessary as CMD_LOGO may swap to the other rom page */
1790 EVE_Hal_wr8(phost, REG_ROMSUB_SEL, 3);
1791 }
1792
1793 /* Default */
1794 phost->CmdFault = false;
1795
1796 /* Set REG_CPURESET to 0, to restart the coprocessor */
1797 EVE_Hal_wr8(phost, REG_CPURESET, 0);
1798 EVE_Hal_flush(phost);
1799 EVE_sleep(100);
1800 eve_assert((wr = EVE_Hal_rd16(phost, REG_CMD_WRITE)) == 0);
1801 eve_assert((rd = EVE_Hal_rd16(phost, REG_CMD_READ)) == 0);
1802
1803 /* Refresh fifo */
1804 EVE_Cmd_waitFlush(phost);
1805#if (EVE_SUPPORT_CHIPID >= EVE_FT810)
1806 if (EVE_CHIPID >= EVE_FT810)
1807 {
1808 phost->MediaFifoAddress = 0;
1809 phost->MediaFifoSize = 0;
1810 }
1811#endif
1812
1813 if (EVE_Util_needsSubPatch(phost))
1814 {
1815 /* Clear cmd with CMD_STOP, exiting CMD_EXECUTE may loop over, depending on OTP */
1817 for (int i = 0; i < EVE_CMD_FIFO_SIZE; i += 4)
1819 EVE_Hal_endTransfer(phost);
1820
1821 /* Go back into the patched coprocessor main loop */
1822 EVE_Hal_wr8(phost, REG_ROMSUB_SEL, 3);
1823 EVE_Cmd_startFunc(phost);
1824 EVE_Cmd_wr32(phost, (4294967047UL));
1826 EVE_Cmd_wr32(phost, 0);
1827 EVE_Cmd_endFunc(phost);
1828 EVE_Hal_flush(phost);
1829 eve_assert((wr = EVE_Hal_rd16(phost, REG_CMD_WRITE)) == 12);
1830 eve_assert((rd = EVE_Hal_rd16(phost, REG_CMD_READ)) == 0);
1831
1832 /* Difficult to check when CMD_EXECUTE is processed when there's an OTP,
1833 since the read pointer keeps looping back to 0. */
1834 EVE_sleep(100);
1835 eve_assert((wr = EVE_Hal_rd16(phost, REG_CMD_WRITE)) == 12);
1836 eve_assert((rd = EVE_Hal_rd16(phost, REG_CMD_READ)) == 0);
1837
1838 /* Need to manually stop previous command from repeating infinitely,
1839 however, this may cause the coprocessor to overshoot the command fifo,
1840 hence it's been filled with harmless CMD_STOP commands. */
1841 EVE_Hal_wr16(phost, REG_CMD_WRITE, 0);
1842 EVE_Hal_flush(phost);
1843 EVE_sleep(100);
1844
1845 /* Refresh fifo */
1846 EVE_Cmd_waitFlush(phost);
1847 eve_assert((wr = EVE_Hal_rd16(phost, REG_CMD_WRITE)) == 0);
1848 eve_assert((rd = EVE_Hal_rd16(phost, REG_CMD_READ)) == 0);
1849 }
1850
1851 if (needsVideoPatch)
1852 {
1853 /* BT81X video patch */
1854 EVE_Hal_wr16(phost, EVE_VIDEOPATCH_ADDR, videoPatchVector);
1855 }
1856
1857 /* Cold start. Ensure that the coprocessor is ready. */
1858 EVE_Cmd_wr32(phost, CMD_DLSTART);
1860
1861 /* Set the latest API level.
1862 CMD_APILEVEL applicable since BT817. */
1863#if (EVE_SUPPORT_CHIPID >= EVE_BT817)
1864 if (EVE_CHIPID >= EVE_BT817)
1865 {
1866 EVE_Cmd_wr32(phost, CMD_APILEVEL);
1867 EVE_Cmd_wr32(phost, 2);
1868 }
1869#endif
1870
1871#ifdef EVE_FLASH_AVAILABLE
1872 if (EVE_CHIPID >= EVE_BT815)
1873 {
1874 /* Reattach flash to avoid inconsistent state */
1876 }
1877#endif
1878
1879 /* Wait for coprocessor to be ready */
1880 ready = EVE_Cmd_waitFlush(phost);
1881 if (phost->CbCoprocessorReset) /* Notify */
1882 phost->CbCoprocessorReset(phost, !ready);
1883
1884#if defined(_DEBUG)
1885 debugRestoreRamG(phost);
1886#endif
1888 return ready;
1889}
1890
1899{
1900 EVE_BootupParameters bootup;
1901 EVE_ConfigParameters config;
1902
1903 EVE_Util_bootupDefaults(phost, &bootup);
1904 if (!EVE_Util_bootup(phost, &bootup))
1905 {
1906 return false;
1907 }
1908
1910 if (!EVE_Util_config(phost, &config))
1911 {
1912 EVE_Util_shutdown(phost);
1913 return false;
1914 }
1915
1916 return true;
1917}
1918
1919/**********************
1920** INTERACTIVE SETUP **
1921**********************/
1924
1925#if defined(_WIN32)
1932void EVE_Util_selectDeviceInteractive(EVE_CHIPID_T *chipId, size_t *deviceIdx)
1933{
1934 char buf[1024];
1935 EVE_DeviceInfo info;
1936 int selectedDeviceIdx;
1937 size_t deviceCount;
1938 size_t i;
1939
1940SELECTDEVICE:
1941 deviceCount = EVE_Hal_list();
1942 size_t realDeviceCount = 0;
1943 for (i = 0; i < deviceCount; ++i)
1944 {
1945 EVE_Hal_info(&info, i);
1946 if (info.Host)
1947 {
1948 ++realDeviceCount;
1949 *deviceIdx = i;
1950 }
1951 }
1952 if (realDeviceCount > 1)
1953 {
1954 buf[0] = '\0';
1955 printf("Select a device:\n");
1956 for (i = 0; i < deviceCount; ++i)
1957 {
1958 EVE_Hal_info(&info, i);
1959 if (info.Host)
1960 printf("- [%d] %s (%s, %s)\n", (unsigned int)i, info.DisplayName, s_HostDisplayNames[info.Host], info.SerialNumber);
1961 }
1962 fgets(buf, sizeof(buf), stdin);
1963 if (sscanf_s(buf, "%i", &selectedDeviceIdx) != 1)
1964 goto SELECTDEVICE;
1965 *deviceIdx = selectedDeviceIdx;
1966 EVE_Hal_info(&info, *deviceIdx);
1967 if (!info.Host)
1968 goto SELECTDEVICE;
1969 printf("\n");
1970 }
1971 else if (realDeviceCount > 0)
1972 {
1973 EVE_Hal_info(&info, *deviceIdx);
1974 printf("%s (%s, %s)\n", info.DisplayName, s_HostDisplayNames[info.Host], info.SerialNumber);
1975 }
1976 else
1977 {
1978 *deviceIdx = -1;
1979 *chipId = EVE_SUPPORT_CHIPID;
1980 return;
1981 }
1982
1983#ifdef EVE_MULTI_GRAPHICS_TARGET
1984SelectChipId:
1985 buf[0] = '\0';
1986 if (info.Host == EVE_HOST_BT8XXEMU)
1987 {
1988 int selectedChipId;
1989 printf("Select a chip:\n");
1990 for (i = 0; i < EVE_SELECT_CHIP_NB; ++i)
1991 {
1992 if (s_SelectChipId[i] <= EVE_SUPPORT_CHIPID)
1993 printf("- [%d] %s\n", (unsigned int)i, s_SelectChipName[i]);
1994 }
1995 fgets(buf, sizeof(buf), stdin);
1996 if (sscanf_s(buf, "%i", &selectedChipId) != 1)
1997 goto SelectChipId;
1998 if (selectedChipId >= 0 && selectedChipId < EVE_SELECT_CHIP_NB)
1999 *chipId = s_SelectChipId[selectedChipId];
2000 else if (selectedChipId >= EVE_FT800 && selectedChipId <= EVE_BT818)
2001 *chipId = selectedChipId;
2002 else
2003 goto SelectChipId;
2004 printf("\n");
2005 }
2006 else
2007 {
2008 *chipId = 0;
2009 }
2010#else
2011 *chipId = EVE_SUPPORT_CHIPID;
2012#endif
2013}
2014
2015#ifdef EVE_MULTI_GRAPHICS_TARGET
2022{
2023 char buf[1024];
2024 int selectedDisplay;
2025 size_t i;
2026
2027 printf("\n");
2028SelectDisplay:
2029 buf[0] = '\0';
2030 printf("Select a display (or press ENTER to use the default):\n");
2031 for (i = 1; i < EVE_DISPLAY_NB; ++i)
2032 {
2033 printf("- [%d] %s\n", (unsigned int)i, s_DisplayNames[i]);
2034 }
2035 fgets(buf, sizeof(buf), stdin);
2036 if (buf[0] == '\n' || buf[0] == '\r')
2037 {
2038 *display = EVE_DISPLAY_DEFAULT;
2039 }
2040 else
2041 {
2042 if (sscanf_s(buf, "%i", &selectedDisplay) != 1)
2043 goto SelectDisplay;
2044 if (selectedDisplay > 0 && selectedDisplay < EVE_DISPLAY_NB)
2045 *display = selectedDisplay;
2046 else
2047 goto SelectDisplay;
2048 }
2049 printf("\n");
2050}
2051#endif
2052
2053#endif
2054
2055#if defined(_WIN32) && defined(EVE_FLASH_AVAILABLE)
2066void EVE_Util_selectFlashFileInteractive(eve_tchar_t *flashPath, size_t flashPathSize, bool *updateFlash, bool *updateFlashFirmware, const EVE_HalParameters *params, const eve_tchar_t *flashFile)
2067{
2068 size_t flashPathSz;
2069 errno_t ferr;
2070 FILE *f = NULL;
2071
2072SELECTFLASH:
2073 *updateFlash = false;
2074 *updateFlashFirmware = false;
2075 if (flashFile)
2076 {
2077#if defined(EVE_MULTI_PLATFORM_TARGET) || !defined(BT8XXEMU_PLATFORM)
2078 uint8_t buffer[4096];
2079#if defined(EVE_MULTI_PLATFORM_TARGET)
2080 if (params->Host != EVE_HOST_BT8XXEMU && flashFile[0])
2081#endif
2082 {
2083 /* Query user if they want to update the flash file on the device */
2084 printf("Upload flash image (y/n, or press ENTER to skip):\n");
2085 buffer[0] = '\0';
2086 fgets((char *)buffer, sizeof(buffer), stdin);
2087 /* Fast string to bool, reliably defined for strings starting
2088 with 0, 1, t, T, f, F, y, Y, n, N, anything else is undefined. */
2089 *updateFlash = (buffer[0] == '1' || (buffer[0] & 0xD2) == 0x50);
2090 printf("\n");
2091 if (*updateFlash)
2092 {
2093 printf("Upload flash firmware (y/n, or press ENTER to skip):\n");
2094 buffer[0] = '\0';
2095 fgets((char *)buffer, sizeof(buffer), stdin);
2096 *updateFlashFirmware = (buffer[0] == '1' || (buffer[0] & 0xD2) == 0x50);
2097 printf("\n");
2098 }
2099 }
2100#endif
2101 /* When uploading, or under emulator when the default flash is specified,
2102 offer to confirm which flash file will be used. */
2103 if (*updateFlash
2104#if defined(EVE_MULTI_PLATFORM_TARGET) || defined(BT8XXEMU_PLATFORM)
2105 || (
2106#if defined(EVE_MULTI_PLATFORM_TARGET)
2107 params->Host == EVE_HOST_BT8XXEMU
2108#elif defined(BT8XXEMU_PLATFORM)
2109 true
2110#endif
2111#ifdef _WIN32
2112 && !wcscmp(flashFile, L"__Flash.bin")
2113#else
2114 && !strcmp(flashFile, "__Flash.bin")
2115#endif
2116 )
2117#endif
2118 )
2119 {
2120 /* Query user for any changes to the flash file name */
2121 printf("Select flash file %s(or press ENTER to use %s%ls%s):\n",
2122 *updateFlashFirmware ? "with firmware " : "",
2123 flashFile[0] ? "\"" : "", /* Quote*/
2124 flashFile[0] ? flashFile : L"no flash",
2125 flashFile[0] ? "\"" : ""); /* Quote*/
2126
2127 fgetws(flashPath, MAX_PATH, stdin);
2128 if (flashPath[0] == '\r' || flashPath[0] == '\n')
2129 flashPath[0] = '\0';
2130 flashPathSz = wcslen(flashPath);
2131 while (flashPathSz && (flashPath[flashPathSz - 1] == '\r' || flashPath[flashPathSz - 1] == '\n'))
2132 {
2133 /* Trim flash path */
2134 flashPath[flashPathSz - 1] = '\0';
2135 --flashPathSz;
2136 }
2137
2138 if (flashFile[0] == '\0' && flashPath[0] == '\0')
2139 {
2140 /* No flash */
2141 printf("\n");
2142 return;
2143 }
2144
2145 /* Check if this file can be opened */
2146 ferr = _wfopen_s(&f, flashPath[0] ? flashPath : flashFile, L"rb");
2147 if (ferr || !f)
2148 {
2149 printf("File \"%ls\" cannot be opened\n", flashPath[0] ? flashPath : flashFile);
2150 goto SELECTFLASH;
2151 }
2152 fseek(f, 0, SEEK_END);
2153 fclose(f);
2154 f = NULL;
2155 printf("\n");
2156 }
2157 if (*updateFlash
2158#if defined(EVE_MULTI_PLATFORM_TARGET)
2159 || params->Host == EVE_HOST_BT8XXEMU
2160#elif defined(BT8XXEMU_PLATFORM)
2161 || true
2162#endif
2163 )
2164 {
2165 if (!flashPath[0])
2166 wcscpy_s(flashPath, flashPathSize, flashFile);
2167 }
2168 }
2169}
2170#endif
2171
2172#if defined(BT8XXEMU_PLATFORM)
2173#if defined(ESD_SIMULATION) && defined(EVE_FLASH_AVAILABLE)
2174extern void Esd_SetFlashFirmware__ESD(const eve_tchar_t *path);
2175#endif
2183void EVE_Util_emulatorDefaults(EVE_HalParameters *params, void *emulatorParams, EVE_CHIPID_T chipId)
2184{
2185#if defined(EVE_MULTI_PLATFORM_TARGET)
2186 if (params->Host != EVE_HOST_BT8XXEMU)
2187 return;
2188#endif
2189
2190 BT8XXEMU_EmulatorParameters *pEmulatorParams = emulatorParams;
2191
2192 BT8XXEMU_defaults(BT8XXEMU_VERSION_API, pEmulatorParams, EVE_shortChipId(chipId)); // TODO: should be pEmulatorParams->mode?
2193 pEmulatorParams->Flags &= (~BT8XXEMU_EmulatorEnableDynamicDegrade & ~BT8XXEMU_EmulatorEnableRegPwmDutyEmulation);
2194
2195 // TODO: emulatorParams.Log
2196 params->EmulatorParameters = pEmulatorParams;
2197}
2198
2199#if defined(EVE_FLASH_AVAILABLE)
2208void EVE_Util_emulatorFlashDefaults(EVE_HalParameters *params, const void *emulatorParams, void *flashParams, const eve_tchar_t *flashPath)
2209{
2210 const BT8XXEMU_EmulatorParameters *pEmulatorParams = emulatorParams;
2211 BT8XXEMU_FlashParameters *pFlashParams = flashParams;
2212
2213#if defined(EVE_MULTI_PLATFORM_TARGET)
2214 if (params->Host != EVE_HOST_BT8XXEMU)
2215 return;
2216#endif
2217 if (pEmulatorParams->Mode < BT8XXEMU_EmulatorBT815)
2218 return;
2219 if (!flashPath || !flashPath[0])
2220 return;
2221
2223 wcscpy_s(pFlashParams->DataFilePath, _countof(pFlashParams->DataFilePath), flashPath);
2224#if defined(_DEBUG)
2225 pFlashParams->StdOut = 1;
2226#endif
2227
2228#if defined(EVE_FLASH_FIRMWARE) /* only defined under ESD_SIMULATION */
2229 Esd_SetFlashFirmware__ESD(EVE_FLASH_FIRMWARE);
2230#endif
2231
2232#ifdef EVE_FLASH_SIZE
2233 pFlashParams->SizeBytes = EVE_FLASH_SIZE * 1024 * 1024;
2234#else
2235 pFlashParams->SizeBytes = 2 * 1024 * 1024;
2236#ifdef _WIN32
2237#pragma warning(push)
2238#pragma warning(disable : 4996)
2239 FILE *f = _wfopen(flashPath, L"rb");
2240#pragma warning(pop)
2241#else
2242 FILE *f = fopen(flashPath, "rb");
2243#endif
2244 if (f)
2245 {
2246 fseek(f, 0, SEEK_END);
2247 int64_t flashSize = ftell(f);
2248 fclose(f);
2249 while (pFlashParams->SizeBytes < flashSize)
2250 pFlashParams->SizeBytes *= 2;
2251 }
2252#endif
2253
2254 // TODO: flashParams.Log
2255 params->EmulatorFlashParameters = pFlashParams;
2256}
2257#endif
2258
2259#endif
2260
2261#if defined(_WIN32) && defined(EVE_FLASH_AVAILABLE)
2262#pragma warning(push)
2263#pragma warning(disable : 6262) // Large stack due to buffer
2271EVE_HAL_EXPORT void EVE_Util_uploadFlashFileInteractive(EVE_HalContext *phost, const eve_tchar_t *flashPath, bool updateFlashFirmware)
2272{
2273 ptrdiff_t flashSize;
2274 FILE *f = NULL;
2275 uint8_t buffer[64 * 4096];
2276 uint8_t rbuffer[64 * 4096];
2277 errno_t err = 0;
2278
2279 /* Upload flash */
2280
2281 EVE_BootupParameters bootupParams;
2282 printf("Preparing to upload flash...\n");
2283
2284 /* Open flash file and get size */
2285#ifdef _WIN32
2286 err = _wfopen_s(&f, flashPath, L"rb");
2287#else
2288 err = fopen_s(&f, flashPath, "rb");
2289#endif
2290
2291 if (err || !f)
2292 {
2293 printf("Flash file cannot be opened\n");
2294 }
2295 else
2296 {
2297
2298 fseek(f, 0, SEEK_END);
2299 flashSize = ftell(f);
2300 fseek(f, 0, SEEK_SET);
2301
2302 /* Get the default bootup parameters for the device */
2303 EVE_Util_bootupDefaults(phost, &bootupParams);
2304
2305 if (EVE_Util_bootup(phost, &bootupParams))
2306 {
2307 /* Get the default bootup parameters for the device */
2308 EVE_ConfigParameters configParams;
2309 EVE_Util_configDefaults(phost, &configParams, EVE_DISPLAY_DEFAULT);
2310
2311 /* No display */
2312 configParams.PCLK = 0;
2313
2314 /* Boot up */
2315 if (EVE_Util_config(phost, &configParams))
2316 {
2317 do
2318 {
2319 uint32_t flashStatus;
2320 uint32_t flashDeviceSize;
2321 size_t remainingBytes;
2322 uint32_t flashAddr;
2323
2324 if (!EVE_Hal_supportFlash(phost))
2325 {
2326 printf("This device doesn't support flash\n");
2327 break;
2328 }
2329
2330 flashStatus = EVE_Hal_rd32(phost, REG_FLASH_STATUS);
2331 if (flashStatus == FLASH_STATUS_DETACHED)
2332 {
2334 if (!EVE_Cmd_waitFlush(phost)) /* Wait for command completion */
2335 {
2336 printf("Coprocessor fault\n");
2337 break;
2338 }
2339 }
2340
2341 flashStatus = EVE_Hal_rd32(phost, REG_FLASH_STATUS);
2342 if (flashStatus < FLASH_STATUS_BASIC)
2343 {
2344 printf("Flash could not be attached\n");
2345 break;
2346 }
2347
2348 if (updateFlashFirmware)
2349 {
2350 printf("Upload flash firmware from image...\n");
2351 if (fread(buffer, 4096, 1, f) != 1)
2352 {
2353 printf("Could not read file\n");
2354 break;
2355 }
2356 EVE_Hal_wrMem(phost, 0, buffer, 4096);
2357 EVE_Cmd_startFunc(phost);
2359 EVE_Cmd_wr32(phost, 0);
2360 EVE_Cmd_wr32(phost, 0);
2361 EVE_Cmd_wr32(phost, 4096);
2362 EVE_Cmd_endFunc(phost);
2363 if (!EVE_Cmd_waitFlush(phost)) /* Wait for command completion */
2364 {
2365 printf("Coprocessor fault\n");
2366 break;
2367 }
2368 }
2369 else
2370 {
2371 if (fseek(f, 4096, SEEK_CUR))
2372 {
2373 printf("Could not seek file\n");
2374 }
2375 }
2376
2377 if (flashStatus == FLASH_STATUS_BASIC)
2378 {
2379 uint32_t resAddr;
2380 printf("Enter fast flash mode\n");
2381 EVE_Cmd_startFunc(phost);
2383 resAddr = EVE_Cmd_moveWp(phost, 4); /* Get the address where the coprocessor will write the result */
2384 EVE_Cmd_endFunc(phost);
2385 if (!EVE_Cmd_waitFlush(phost)) /* Wait for command completion */
2386 {
2387 printf("Coprocessor fault\n");
2388 break;
2389 }
2390 EVE_Hal_rd32(phost, RAM_CMD + resAddr); /* Fetch result */
2391 }
2392
2393 flashStatus = EVE_Hal_rd32(phost, REG_FLASH_STATUS);
2394 if (flashStatus < FLASH_STATUS_FULL)
2395 {
2396 printf("Flash could not enter fast mode. Has the correct firmware been uploaded?\n");
2397 break;
2398 }
2399
2400 flashDeviceSize = EVE_Hal_rd32(phost, REG_FLASH_SIZE) * 1024 * 1024;
2401 if (flashDeviceSize < flashSize)
2402 {
2403 printf("Not enough space on flash, need %i bytes, have %i bytes\n", (int)flashSize, (int)flashDeviceSize);
2404 break;
2405 }
2406
2407 remainingBytes = flashSize - 4096;
2408 flashAddr = 4096;
2409
2410 printf("Upload flash from image...\n");
2411 eve_assert(sizeof(buffer) >= 4096);
2412
2413 while (remainingBytes)
2414 {
2415 /* Read from file */
2416 size_t el;
2417 size_t sz;
2418 if (remainingBytes < 4096)
2419 {
2420 el = fread(buffer, 1, remainingBytes, f);
2421 sz = 4096;
2422 remainingBytes = 0;
2423 }
2424 else
2425 {
2426 el = fread(buffer, 4096, min(remainingBytes, sizeof(buffer)) / 4096, f);
2427 sz = el * 4096;
2428 remainingBytes -= sz;
2429 }
2430 if (!el)
2431 {
2432 printf("\nFailed to read file\n");
2433 break;
2434 }
2435 printf("B");
2436
2437 /* Write to flash */
2438 REWRITE:
2439 EVE_Hal_wrMem(phost, 0, buffer, (uint32_t)sz);
2440 EVE_Cmd_startFunc(phost);
2442 EVE_Cmd_wr32(phost, flashAddr);
2443 EVE_Cmd_wr32(phost, 0);
2444 EVE_Cmd_wr32(phost, (uint32_t)sz);
2445 EVE_Cmd_endFunc(phost);
2446 if (!EVE_Cmd_waitFlush(phost)) /* Wait for command completion */
2447 {
2448 printf("\nCoprocessor fault\n");
2449 break;
2450 }
2451 printf("R");
2452
2453 /* Verify using CMD_FLASHREAD */
2455 for (size_t i = 0; i < sz; ++i)
2456 EVE_Hal_transfer8(phost, 0x0F);
2457 EVE_Hal_endTransfer(phost);
2458 EVE_Cmd_startFunc(phost);
2460 EVE_Cmd_wr32(phost, 0);
2461 EVE_Cmd_wr32(phost, flashAddr);
2462 EVE_Cmd_wr32(phost, (uint32_t)sz);
2463 EVE_Cmd_endFunc(phost);
2464 if (!EVE_Cmd_waitFlush(phost)) /* Wait for command completion */
2465 {
2466 printf("\nCoprocessor fault\n");
2467 break;
2468 }
2469 EVE_Hal_rdMem(phost, rbuffer, 0, (uint32_t)sz);
2470 for (size_t i = 0; i < sz; ++i)
2471 {
2472 if (buffer[i] != rbuffer[i])
2473 {
2474 printf("\nVerification failed\n");
2475 goto REWRITE;
2476 }
2477 }
2478 printf("T");
2479
2480 flashAddr += (uint32_t)sz;
2481 }
2482
2483 if (!remainingBytes)
2484 {
2485 printf("\nFlash upload is ready\n");
2486 }
2487 printf("\n");
2488 } while (false); /* breakable scope */
2489 }
2490 else
2491 {
2492 printf("Failed to bootup the device\n");
2493 }
2494
2495 /* Shutdown */
2496 EVE_Util_shutdown(phost);
2497 }
2498 else
2499 {
2500 printf("Failed to bootup the device\n");
2501 }
2502 }
2503 printf("\n");
2504
2505 if (f)
2506 {
2507 fclose(f);
2508 }
2509}
2510#pragma warning(pop)
2511#endif
2512
2521bool EVE_Util_openDeviceInteractive(EVE_HalContext *phost, const wchar_t *flashFile)
2522{
2523 EVE_CHIPID_T chipId;
2524 size_t deviceIdx;
2525 bool opened;
2526
2527#ifdef BT8XXEMU_PLATFORM
2528 BT8XXEMU_EmulatorParameters emulatorParams;
2529#if defined(EVE_FLASH_AVAILABLE)
2530 BT8XXEMU_FlashParameters flashParams;
2531#endif
2532#endif
2533 EVE_HalParameters params = { 0 };
2534#if defined(_WIN32) && defined(EVE_FLASH_AVAILABLE)
2535 bool updateFlash = false;
2536 bool updateFlashFirmware = false;
2537 wchar_t flashPath[MAX_PATH];
2538 flashPath[0] = '\0';
2539#endif
2540
2541 /* Interactive device selection */
2542 EVE_Util_selectDeviceInteractive(&chipId, &deviceIdx);
2543
2544 /* Fetch the default parameters for a device. Set the expected chip id.
2545 Pass the device index, or -1 to select the first device */
2546 EVE_Hal_defaultsEx(&params, deviceIdx);
2547
2548#if defined(_WIN32) && defined(EVE_FLASH_AVAILABLE)
2549 if (chipId >= EVE_BT815 || (chipId <= 0 && flashFile && flashFile[0]))
2550 EVE_Util_selectFlashFileInteractive(flashPath, sizeof(flashPath) / sizeof(flashPath[0]), &updateFlash, &updateFlashFirmware, &params, flashFile);
2551#endif
2552
2553#if defined(BT8XXEMU_PLATFORM)
2554 EVE_Util_emulatorDefaults(&params, &emulatorParams, chipId);
2555#if defined(EVE_FLASH_AVAILABLE)
2556 EVE_Util_emulatorFlashDefaults(&params, &emulatorParams, &flashParams, flashPath);
2557#endif
2558#endif
2559
2560 opened = EVE_Hal_open(phost, &params);
2561
2562 if (!opened)
2563 return false;
2564
2565#if defined(_WIN32) && defined(EVE_FLASH_AVAILABLE)
2566 if (updateFlash && flashPath[0])
2567 EVE_Util_uploadFlashFileInteractive(phost, flashPath, updateFlashFirmware);
2568#endif
2569
2570 return true;
2571}
2572
2583{
2584 EVE_DISPLAY_T selectedDisplay;
2585 EVE_BootupParameters bootup;
2586 EVE_ConfigParameters config;
2587
2588 EVE_Util_bootupDefaults(phost, &bootup);
2589 if (!EVE_Util_bootup(phost, &bootup))
2590 {
2591 return false;
2592 }
2593
2594 EVE_Util_selectDisplayInteractive(&selectedDisplay);
2595 if (!selectedDisplay)
2596 selectedDisplay = display;
2597
2598 EVE_Util_configDefaults(phost, &config, selectedDisplay);
2599 if (!EVE_Util_config(phost, &config))
2600 {
2601 EVE_Util_shutdown(phost);
2602 return false;
2603 }
2604
2605 return true;
2606}
2607
2614void EVE_Util_forceFault(EVE_HalContext *phost, const char *err)
2615{
2616 /* Check if we're already in fault state */
2617 if (!EVE_Cmd_waitSpace(phost, 0))
2618 return;
2619#if 1
2620 /* Go into reset state and mimic a fault */
2621 EVE_Hal_wr8(phost, REG_CPURESET, 1);
2622 EVE_Hal_flush(phost);
2623 EVE_sleep(100);
2624 EVE_Hal_wr16(phost, REG_CMD_READ, 0xFFF);
2625 EVE_Hal_wr16(phost, REG_CMD_WRITE, 0xFFF);
2626#else
2627 /* TODO: Clean this up a bit */
2628 if (!EVE_Util_resetCoprocessor(phost))
2629 return;
2630 EVE_CoCmd_dlStart(phost);
2631 for (int i = 0; i < EVE_DL_COUNT; ++i)
2632 EVE_Cmd_wr32(phost, DISPLAY());
2633 EVE_Cmd_waitFlush(phost);
2634 EVE_Cmd_wr32(phost, DISPLAY());
2635 EVE_Hal_flush(phost);
2636 EVE_sleep(100);
2637#endif
2638#if (EVE_SUPPORT_CHIPID >= EVE_BT815)
2639 if (EVE_CHIPID >= EVE_BT815 && (EVE_Cmd_rp(phost) & 0x3))
2640 {
2641 /* Write our own error to the error report area */
2642 uint32_t errLen = (uint32_t)strlen(err);
2643 EVE_Hal_wrMem(phost, RAM_ERR_REPORT, (const uint8_t *)err, errLen);
2644 if (errLen < RAM_ERR_REPORT_MAX)
2645 {
2647 for (int i = (int)errLen; i < RAM_ERR_REPORT_MAX; ++i)
2648 EVE_Hal_transfer8(phost, 0);
2649 EVE_Hal_endTransfer(phost);
2650 }
2651 }
2652#endif
2653 /* Fault state now */
2654 EVE_Cmd_waitFlush(phost);
2655#if 0
2656#if defined(_DEBUG)
2657#if (EVE_SUPPORT_CHIPID >= EVE_BT815)
2658 if (EVE_CHIPID >= EVE_BT815
2659 && phost->DebugMessageVisible)
2660 {
2661 uint32_t errLen = (uint32_t)strlen(err);
2662 EVE_Hal_wrMem(phost, RAM_G + RAM_G_SIZE - sizeof(phost->DebugBackup), (const uint8_t *)err, errLen);
2663 if (errLen < sizeof(phost->DebugBackup))
2664 {
2665 EVE_Hal_startTransfer(phost, EVE_TRANSFER_WRITE, RAM_G + RAM_G_SIZE - sizeof(phost->DebugBackup) + errLen);
2666 for (int i = (int)errLen; i < sizeof(phost->DebugBackup); ++i)
2667 EVE_Hal_transfer8(phost, 0);
2668 EVE_Hal_endTransfer(phost);
2669 }
2670 }
2671#endif
2672#endif
2673#endif
2674}
2676
2677/* end of file */
static uint32_t f
Definition Common.c:41
EVE_HAL_EXPORT uint16_t EVE_Cmd_space(EVE_HalContext *phost)
Get free space of Coprocessor's command buffer.
Definition EVE_Cmd.c:105
EVE_HAL_EXPORT uint16_t EVE_Cmd_moveWp(EVE_HalContext *phost, uint16_t bytes)
Move the write pointer forward by the specified number of bytes. Returns the previous write pointer.
Definition EVE_Cmd.c:446
EVE_HAL_EXPORT uint32_t EVE_Cmd_waitSpace(EVE_HalContext *phost, uint32_t size)
Definition EVE_Cmd.c:580
EVE_HAL_EXPORT void EVE_Cmd_startFunc(EVE_HalContext *phost)
Begin writing a function, keeps the transfer open.
Definition EVE_Cmd.c:262
EVE_HAL_EXPORT uint16_t EVE_Cmd_rp(EVE_HalContext *phost)
Read from Coprocessor.
Definition EVE_Cmd.c:64
EVE_HAL_EXPORT void EVE_Cmd_endFunc(EVE_HalContext *phost)
End writing a function, closes the transfer.
Definition EVE_Cmd.c:274
EVE_HAL_EXPORT uint16_t EVE_Cmd_wp(EVE_HalContext *phost)
Write to Coprocessor.
Definition EVE_Cmd.c:80
EVE_HAL_EXPORT bool EVE_Cmd_wr32(EVE_HalContext *phost, uint32_t value)
Write 4 bytes to Coprocessor's command fifo.
Definition EVE_Cmd.c:394
EVE_HAL_EXPORT bool EVE_Cmd_wrProgMem(EVE_HalContext *phost, eve_progmem_const uint8_t *buffer, uint32_t size)
Write buffer in ProgMem to Coprocessor's comand fifo.
Definition EVE_Cmd.c:307
static void EVE_CoCmd_dlStart(EVE_HalContext *phost)
Send CMD_DLSTART.
Definition EVE_CoCmd.h:159
#define EVE_BT818
Definition EVE_Config.h:69
#define EVE_FT811
Definition EVE_Config.h:59
#define EVE_BT882
Definition EVE_Config.h:64
#define EVE_BT817
Definition EVE_Config.h:68
#define EVE_FT812
Definition EVE_Config.h:60
#define EVE_BT883
Definition EVE_Config.h:65
#define EVE_HAL_EXPORT
#define EVE_FT800
Definition EVE_Config.h:56
char eve_tchar_t
#define EVE_DISPLAY_REFRESH
#define EVE_BT881
Definition EVE_Config.h:63
#define EVE_SUPPORT_CHIPID
#define EVE_FT813
Definition EVE_Config.h:61
#define EVE_BT815
Definition EVE_Config.h:66
#define EVE_BT880
Definition EVE_Config.h:62
#define EVE_CHIPID
#define eve_progmem_const
#define EVE_BT816
Definition EVE_Config.h:67
#define EVE_FT810
Definition EVE_Config.h:58
#define EVE_FT801
Definition EVE_Config.h:57
#define REG_PCLK_POL
#define REG_TOUCH_CONFIG
#define RAM_ERR_REPORT
Definition EVE_GpuDefs.h:79
#define REG_VSYNC0
#define REG_GPIO_DIR
#define EVE_DL_COUNT
Definition EVE_GpuDefs.h:57
#define REG_HSIZE
#define REG_ROTATE
#define REG_ROMSUB_SEL
#define CMD_FLASHDETACH
#define REG_DLSWAP
#define REG_HSYNC1
#define REG_GPIOX
#define RAM_CMD
#define CMD_STOP
#define REG_VSIZE
#define DISPLAY()
#define CMD_HSF
#define REG_DITHER
#define REG_ADAPTIVE_FRAMERATE
#define EVE_CMD_FIFO_SIZE
Definition EVE_GpuDefs.h:58
#define REG_TOUCH_RZTHRESH
#define CMD_DLSTART
#define FLASH_STATUS_BASIC
#define CMD_FLASHFAST
#define REG_FLASH_SIZE
#define REG_CSPREAD
#define CMD_FLASHATTACH
#define REG_PCLK_2X
#define FLASH_STATUS_FULL
#define EVE_CMD_FAULT(rp)
Definition EVE_GpuDefs.h:63
#define REG_HCYCLE
#define REG_CMD_WRITE
#define REG_FREQUENCY
#define CMD_FLASHREAD
#define RAM_DL
Definition EVE_GpuDefs.h:95
#define REG_CPURESET
#define REG_VSYNC1
#define DLSWAP_FRAME
#define REG_ID
#define REG_VCYCLE
#define REG_AH_HCYCLE_MAX
#define REG_HOFFSET
#define REG_GPIOX_DIR
#define RAM_G_SIZE
Definition EVE_GpuDefs.h:98
#define REG_PCLK_FREQ
#define REG_GPIO
#define REG_OUTBITS
#define REG_PLAYBACK_LENGTH
#define CMD_FLASHUPDATE
#define CMD_APILEVEL
#define CMD_COLDSTART
#define REG_HSF_HSIZE
#define REG_CMD_DL
#define REG_CMD_READ
#define REG_VOFFSET
#define REG_PLAYBACK_PLAY
#define ROM_CHIPID
Definition EVE_GpuDefs.h:78
#define FLASH_STATUS_DETACHED
#define RAM_G
Definition EVE_GpuDefs.h:77
#define REG_PCLK
#define REG_HSYNC0
#define REG_FLASH_STATUS
#define REG_SWIZZLE
@ EVE_SPI_QUAD_CHANNEL
@ EVE_SPI_SINGLE_CHANNEL
@ EVE_SPI_DUAL_CHANNEL
@ EVE_SLEEP_M
@ EVE_ACTIVE_M
@ EVE_INTERNAL_OSC
@ EVE_EXTERNAL_OSC
@ EVE_SYSCLK_60M
@ EVE_SYSCLK_DEFAULT
@ EVE_SYSCLK_48M
@ EVE_SYSCLK_72M
EVE_HAL_EXPORT void EVE_Host_powerModeSwitch(EVE_HalContext *phost, EVE_POWER_MODE_T pwrmode)
Switch power mode for Coprocessor.
Definition EVE_Hal.c:369
EVE_HAL_EXPORT uint32_t EVE_Hal_rd32(EVE_HalContext *phost, uint32_t addr)
Read 4 bytes from Coprocessor's memory.
Definition EVE_Hal.c:189
EVE_HAL_EXPORT void EVE_Hal_rdMem(EVE_HalContext *phost, uint8_t *result, uint32_t addr, uint32_t size)
Read a block data from Coprocessor's memory.
Definition EVE_Hal.c:206
EVE_HAL_EXPORT void EVE_Hal_wrProgMem(EVE_HalContext *phost, uint32_t addr, eve_progmem_const uint8_t *buffer, uint32_t size)
Write a buffer in ProgMem to Coprocessor's memory.
Definition EVE_Hal.c:278
EVE_HAL_EXPORT void EVE_Hal_defaultsEx(EVE_HalParameters *parameters, size_t deviceIdx)
Setup default parameters for Eve_Hal framework.
Definition EVE_Hal.c:89
EVE_HAL_EXPORT uint16_t EVE_Hal_rd16(EVE_HalContext *phost, uint32_t addr)
Read 2 bytes from Coprocessor's memory.
Definition EVE_Hal.c:173
EVE_HAL_EXPORT void EVE_Host_selectSysClk(EVE_HalContext *phost, EVE_81X_PLL_FREQ_T freq)
Set system clock for Coprocessor.
Definition EVE_Hal.c:390
EVE_HAL_EXPORT bool EVE_Hal_open(EVE_HalContext *phost, const EVE_HalParameters *parameters)
Open the Eve_Hal framework.
Definition EVE_Hal.c:103
EVE_HAL_EXPORT void EVE_Hal_wr8(EVE_HalContext *phost, uint32_t addr, uint8_t v)
Write 8 bits to Coprocessor's memory.
Definition EVE_Hal.c:220
EVE_HAL_EXPORT void EVE_Host_coreReset(EVE_HalContext *phost)
Send reset signal to Coprocessor.
Definition EVE_Hal.c:379
EVE_HAL_EXPORT void EVE_Hal_wr16(EVE_HalContext *phost, uint32_t addr, uint16_t v)
Write 2 bytes to Coprocessor's memory.
Definition EVE_Hal.c:234
EVE_HAL_EXPORT void EVE_Hal_wr32(EVE_HalContext *phost, uint32_t addr, uint32_t v)
Write 4 bytes to Coprocessor's memory.
Definition EVE_Hal.c:248
EVE_HAL_EXPORT void EVE_Hal_wrMem(EVE_HalContext *phost, uint32_t addr, const uint8_t *buffer, uint32_t size)
Write a buffer to Coprocessor's memory.
Definition EVE_Hal.c:263
EVE_HAL_EXPORT uint8_t EVE_Hal_rd8(EVE_HalContext *phost, uint32_t addr)
Read 8 bits from Coprocessor's memory.
Definition EVE_Hal.c:157
EVE_HAL_EXPORT bool EVE_Hal_powerCycle(EVE_HalContext *phost, bool up)
Toggle PD_N pin of FT800 board for a power cycle.
EVE_HAL_EXPORT uint8_t EVE_Hal_transfer8(EVE_HalContext *phost, uint8_t value)
Write 8 bits to Coprocessor.
EVE_HAL_EXPORT void EVE_Hal_info(EVE_DeviceInfo *deviceInfo, size_t deviceIdx)
Get info of the specified device. Devices of type EVE_HOST_UNKNOWN should be ignored.
EVE_HAL_EXPORT void EVE_Hal_startTransfer(EVE_HalContext *phost, EVE_TRANSFER_T rw, uint32_t addr)
Start data transfer to Coprocessor.
static int EVE_gen(EVE_CHIPID_T chipId)
EVE_HAL_EXPORT void EVE_Hal_hostCommand(EVE_HalContext *phost, uint8_t cmd)
Send a host command to Coprocessor.
@ EVE_TRANSFER_WRITE
Definition EVE_HalDefs.h:68
EVE_HAL_EXPORT void EVE_Hal_setSPI(EVE_HalContext *phost, EVE_SPI_CHANNELS_T numchnls, uint8_t numdummy)
Set number of SPI channel.
@ EVE_HOST_NB
@ EVE_HOST_FT4222
@ EVE_HOST_BT8XXEMU
static int EVE_shortChipId(EVE_CHIPID_T chipId)
static bool EVE_Hal_supportHsf(EVE_HalContext *phost)
EVE_HAL_EXPORT size_t EVE_Hal_list()
List the available devices.
static bool EVE_Hal_supportFlash(EVE_HalContext *phost)
EVE_HAL_EXPORT void EVE_sleep(uint32_t ms)
Sleep in milisecond.
#define RAM_ERR_REPORT_MAX
Definition EVE_HalDefs.h:89
static bool EVE_Hal_isScreenResistive(EVE_HalContext *phost)
EVE_HAL_EXPORT void EVE_Hal_restoreSPI(EVE_HalContext *phost)
EVE_HAL_EXPORT void EVE_Hal_endTransfer(EVE_HalContext *phost)
End data transfer.
EVE_CHIPID_T
Definition EVE_HalDefs.h:72
EVE_HAL_EXPORT void EVE_Hal_flush(EVE_HalContext *phost)
Flush data to Coprocessor.
EVE_HAL_EXPORT uint32_t EVE_Hal_transfer32(EVE_HalContext *phost, uint32_t value)
Write 4 bytes to Coprocessor.
Eve_Hal framework APIs.
bool EVE_UtilImpl_bootupDisplayGpio(EVE_HalContext *phost)
Display GPIO pins.
unsigned short uint16_t
int int32_t
unsigned int uint32_t
long long int64_t
short int16_t
unsigned char uint8_t
Platform selector.
#define eve_assert(cond)
#define eve_printf_debug(fmt,...)
#define eve_assert_do(cond)
bool EVE_Util_openDeviceInteractive(EVE_HalContext *phost, const wchar_t *flashFile)
Definition EVE_Util.c:2521
EVE_HAL_EXPORT void EVE_Util_configDefaults(EVE_HalContext *phost, EVE_ConfigParameters *config, EVE_DISPLAY_T display)
Definition EVE_Util.c:765
EVE_HAL_EXPORT void EVE_Util_clearScreen(EVE_HalContext *phost)
Clear the screen.
Definition EVE_Util.c:270
#define EVE_VIDEOPATCH_ADDR
Definition EVE_Util.c:1704
static bool EVE_Util_needsVideoPatch(EVE_HalContext *phost)
Definition EVE_Util.c:1711
static const uint16_t s_DisplayResolutions[EVE_DISPLAY_NB][4]
Definition EVE_Util.c:45
EVE_HAL_EXPORT bool EVE_Util_configDefaultsEx(EVE_HalContext *phost, EVE_ConfigParameters *config, uint32_t width, uint32_t height, uint32_t refreshRate, uint32_t hsfWidth)
Definition EVE_Util.c:740
EVE_HAL_EXPORT void EVE_Util_bootupDefaults(EVE_HalContext *phost, EVE_BootupParameters *bootup)
Definition EVE_Util.c:282
static bool configDefaultsEx(EVE_HalContext *phost, EVE_ConfigParameters *config, uint32_t width, uint32_t height, uint32_t refreshRate, uint32_t hsfWidth, uint32_t freq)
Definition EVE_Util.c:445
EVE_HAL_EXPORT bool EVE_Util_bootupConfigInteractive(EVE_HalContext *phost, EVE_DISPLAY_T display)
Calls EVE_Util_bootup and EVE_Util_config using the default parameters. Falls back to no interactivit...
Definition EVE_Util.c:2582
#define EVE_SUBPATCH_PTR
Definition EVE_Util.c:1688
EVE_HAL_EXPORT bool EVE_Util_config(EVE_HalContext *phost, EVE_ConfigParameters *config)
Definition EVE_Util.c:1396
EVE_HAL_EXPORT bool EVE_Util_resetCoprocessor(EVE_HalContext *phost)
Reset Coprocessor.
Definition EVE_Util.c:1752
EVE_HAL_EXPORT bool EVE_Util_bootup(EVE_HalContext *phost, EVE_BootupParameters *bootup)
Definition EVE_Util.c:1121
void debugBackupRamG(EVE_HalContext *phost)
Backup the last 128 bytes of RAM_G, which may be used for an error message.
Definition EVE_Util.c:1722
static bool EVE_Util_needsSubPatch(EVE_HalContext *phost)
Definition EVE_Util.c:1695
EVE_HAL_EXPORT bool EVE_Util_bootupConfig(EVE_HalContext *phost)
Bootup Coprocessor.
Definition EVE_Util.c:1898
EVE_HAL_EXPORT void EVE_CoDlImpl_resetCoState(EVE_HalContext *phost)
Definition EVE_CoDl.c:83
static void uploadTouchFirmware(EVE_HalContext *phost)
Definition EVE_Util.c:259
void EVE_Util_forceFault(EVE_HalContext *phost, const char *err)
Definition EVE_Util.c:2614
static eve_progmem_const uint8_t c_DlCodeBootup[12]
Definition EVE_Util.c:38
static void debugRestoreRamG(EVE_HalContext *phost)
Definition EVE_Util.c:1735
#define EXTRACT_CHIPID(romChipId)
Definition EVE_Util.c:1111
EVE_HAL_EXPORT void EVE_Util_shutdown(EVE_HalContext *phost)
Definition EVE_Util.c:1667
EVE HAL framework utilities.
static void EVE_Util_uploadFlashFileInteractive(EVE_HalContext *phost, const eve_tchar_t *flashPath, bool updateFlashFirmware)
Definition EVE_Util.h:209
static void EVE_Util_selectDeviceInteractive(EVE_CHIPID_T *chipId, size_t *deviceIdx)
Definition EVE_Util.h:178
static void EVE_Util_selectFlashFileInteractive(eve_tchar_t *flashPath, size_t flashPathSize, bool *updateFlash, bool *updateFlashFirmware, const EVE_HalParameters *params, const eve_tchar_t *flashFile)
Definition EVE_Util.h:198
static void EVE_Util_selectDisplayInteractive(EVE_DISPLAY_T *display)
Definition EVE_Util.h:189
EVE_DISPLAY_T
Definition EVE_Util.h:101
@ EVE_DISPLAY_QVGA_320x240_56Hz
Definition EVE_Util.h:105
@ EVE_DISPLAY_HDTV_1280x720_58Hz
Definition EVE_Util.h:109
@ EVE_DISPLAY_DEFAULT
Definition EVE_Util.h:102
@ EVE_DISPLAY_HVGA_320x480_60Hz
Definition EVE_Util.h:113
@ EVE_DISPLAY_WSVGA_1024x600_59Hz
Definition EVE_Util.h:108
@ EVE_DISPLAY_WQVGA_480x272_60Hz
Definition EVE_Util.h:106
@ EVE_DISPLAY_NB
Definition EVE_Util.h:125
@ EVE_DISPLAY_WVGA_800x480_74Hz
Definition EVE_Util.h:107
@ EVE_DISPLAY_WVGA_800x480_W860_60Hz
Definition EVE_Util.h:116
@ EVE_DISPLAY_WXGA_1280x800_57Hz
Definition EVE_Util.h:110
static ft_uint32_t ft_uint8_t * buffer
Definition FT_Gpu_Hal.h:139
void EVE_Cmd_waitFlush(EVE_HalContext *host)
Definition Gpu_Hal.cpp:775
#define RESISTANCE_THRESHOLD
Definition Platform.h:59
BT8XXEMU_API void BT8XXEMU_defaults(uint32_t versionApi, BT8XXEMU_EmulatorParameters *params, BT8XXEMU_EmulatorMode mode)
BT8XXEMU_API void BT8XXEMU_Flash_defaults(uint32_t versionApi, BT8XXEMU_FlashParameters *params)
#define BT8XXEMU_VERSION_API
Definition bt8xxemu.h:52
@ BT8XXEMU_EmulatorBT815
Definition bt8xxemu.h:122
BT8XXEMU_EmulatorMode Mode
Definition bt8xxemu.h:184
eve_tchar_t DataFilePath[260]
Definition bt8xxemu.h:252
EVE_SPI_CHANNELS_T SpiChannels
Definition EVE_Util.h:50
uint8_t SpiDummyBytes
Definition EVE_Util.h:51
EVE_81X_PLL_FREQ_T SystemClock
Definition EVE_Util.h:43
char DisplayName[256]
char SerialNumber[16]
EVE_HOST_T Host
uint32_t Height
uint32_t MediaFifoAddress
uint32_t MediaFifoSize
EVE_Callback CbCmdWait
bool DebugMessageVisible
uint8_t DebugBackup[RAM_ERR_REPORT_MAX]
EVE_ResetCallback CbCoprocessorReset
EVE_SPI_CHANNELS_T SpiChannels