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_Cmd.c
Go to the documentation of this file.
1
36#include "EVE_Cmd.h"
37#include "EVE_Platform.h"
38
44static inline void endFunc(EVE_HalContext *phost)
45{
46 if (phost->Status == EVE_STATUS_WRITING)
47 {
49#if !defined(EVE_SUPPORT_CMDB) || defined(EVE_MULTI_GRAPHICS_TARGET)
50 if (!EVE_Hal_supportCmdB(phost))
51 {
52 EVE_Hal_wr16(phost, REG_CMD_WRITE, phost->CmdWp);
53 }
54#endif
55 }
56}
57
65{
66 uint16_t rp;
67 endFunc(phost);
69 if (EVE_CMD_FAULT(rp))
70 phost->CmdFault = true;
71 return rp;
72}
73
81{
82 endFunc(phost);
83 if (EVE_Hal_supportCmdB(phost))
84 {
86 }
87 else
88 {
89#if !defined(EVE_SUPPORT_CMDB) || defined(EVE_MULTI_GRAPHICS_TARGET)
90 phost->CmdWp = EVE_Hal_rd16(phost, REG_CMD_WRITE) & EVE_CMD_FIFO_MASK;
91 return phost->CmdWp;
92#else
93 eve_assert(false);
94 return 0xffff;
95#endif
96 }
97}
98
106{
107 uint16_t space;
108 uint16_t wp;
109 uint16_t rp;
110 endFunc(phost);
111 if (EVE_Hal_supportCmdB(phost))
112 {
114 if (EVE_CMD_FAULT(space))
115 phost->CmdFault = true;
116 phost->CmdSpace = space;
117 return space;
118 }
119 else
120 {
121 wp = EVE_Cmd_wp(phost);
122 rp = EVE_Cmd_rp(phost);
123 space = (rp - wp - 4) & EVE_CMD_FIFO_MASK;
124 phost->CmdSpace = space;
125 return space;
126 }
127}
128
135{
136 if (phost->Status != EVE_STATUS_WRITING)
137 {
138 if (EVE_Hal_supportCmdB(phost))
139 {
141 }
142 else
143 {
144#if !defined(EVE_SUPPORT_CMDB) || defined(EVE_MULTI_GRAPHICS_TARGET)
145 EVE_Hal_startTransfer(phost, EVE_TRANSFER_WRITE, RAM_CMD + phost->CmdWp);
146#else
147 eve_assert(false);
148#endif
149 }
150 }
151}
152
163static uint32_t wrString(EVE_HalContext *phost, const void *buffer, uint32_t *size, uint32_t transfered, uint32_t transfer)
164{
165 uint32_t t;
166 eve_assert(transfered == 0);
167 eve_assert(transfer == *size); /* Cannot split string transfers */
168 eve_assert(transfer <= phost->CmdSpace);
169 t = EVE_Hal_transferString(phost, (const char *)buffer, transfered, transfer, 0x3);
170 if (t != transfer) /* End of string */
171 {
172 eve_assert(t <= phost->CmdSpace);
173 transfer = t;
174 *size = (transfered + transfer);
175 }
176 eve_assert((transfered + transfer) == *size);
177
178 return transfer;
179}
180
191static uint32_t wrBuffer(EVE_HalContext *phost, const void *buffer, uint32_t size, bool progmem, bool string)
192{
193 uint32_t transfered = 0;
194
195 do
196 {
197 uint32_t transfer = (size - transfered);
198 uint32_t space = phost->CmdSpace;
199 uint32_t req = min((string ? (transfer + 1) : transfer), (EVE_CMD_FIFO_SIZE >> 1));
200 eve_assert(!string || (req == (transfer + 1)));
201 if (space < req)
202 {
203 if (!EVE_Cmd_waitSpace(phost, req))
204 return transfered; /* Coprocessor fault */
205 space = phost->CmdSpace;
206 }
207 if (transfer > space)
208 transfer = space;
209 eve_assert(transfer <= EVE_CMD_FIFO_SIZE - 4);
210 if (transfer)
211 {
212 startBufferTransfer(phost);
213 if (string)
214 {
215 transfer = wrString(phost, buffer, &size, transfered, transfer);
216 }
217 else if (progmem)
218 {
219 EVE_Hal_transferProgMem(phost, NULL, (eve_progmem_const uint8_t *)(uintptr_t)(&((uint8_t *)buffer)[transfered]), transfer);
220 }
221 else
222 {
223 EVE_Hal_transferMem(phost, NULL, &((uint8_t *)buffer)[transfered], transfer);
224 }
225 if (!string && (transfer & 0x3))
226 {
227 uint32_t pad = 4 - (transfer & 0x3);
228 uint8_t padding[4] = { 0 };
229 eve_assert((transfered + transfer) == size);
230 EVE_Hal_transferMem(phost, NULL, padding, pad);
231 transfer += pad;
232 eve_assert(!(transfer & 0x3));
233 }
234 transfered += transfer;
235 if (!phost->CmdFunc) /* Keep alive while writing function */
236 {
237 EVE_Hal_endTransfer(phost);
238 }
239 eve_assert(phost->CmdSpace >= transfer);
240 phost->CmdSpace -= (uint16_t)transfer;
241#if !defined(EVE_SUPPORT_CMDB) || defined(EVE_MULTI_GRAPHICS_TARGET)
242 if (!EVE_Hal_supportCmdB(phost))
243 {
244 phost->CmdWp += (uint16_t)transfer;
245 phost->CmdWp &= EVE_CMD_FIFO_MASK;
246 if (!phost->CmdFunc) /* Defer write pointer */
247 {
248 EVE_Hal_wr16(phost, REG_CMD_WRITE, phost->CmdWp);
249 }
250 }
251#endif
252 }
253 } while (transfered < size);
254 return transfered;
255}
256
263{
264 eve_assert(!phost->CmdWaiting);
265 eve_assert(phost->CmdBufferIndex == 0);
266 phost->CmdFunc = true;
267}
268
275{
276 eve_assert(!phost->CmdWaiting);
277 eve_assert(phost->CmdBufferIndex == 0);
278 endFunc(phost);
279 phost->CmdFunc = false;
280}
281
292{
293 eve_assert(!phost->CmdWaiting);
294 eve_assert(phost->CmdBufferIndex == 0);
295 return wrBuffer(phost, buffer, size, false, false) == size;
296}
297
308{
309 eve_assert(!phost->CmdWaiting);
310 eve_assert(phost->CmdBufferIndex == 0);
311 return wrBuffer(phost, (void *)(uintptr_t)buffer, size, true, false) == size;
312}
313
323{
324 uint32_t transfered;
325 eve_assert(!phost->CmdWaiting);
326 eve_assert(phost->CmdBufferIndex == 0);
327 transfered = wrBuffer(phost, str, maxLength, false, true);
328 return transfered;
329}
330
340{
341 eve_assert(!phost->CmdWaiting);
342 eve_assert(phost->CmdBufferIndex < 4);
343
344 if (phost->CmdBufferIndex < 4)
345 {
346 phost->CmdBuffer[phost->CmdBufferIndex++] = value;
347 }
348
349 if (phost->CmdBufferIndex == 4)
350 {
351 phost->CmdBufferIndex = 0;
352 return EVE_Cmd_wrMem(phost, phost->CmdBuffer, 4);
353 }
354
355 return true;
356}
357
367{
368 eve_assert(!phost->CmdWaiting);
369 eve_assert(phost->CmdBufferIndex < 3);
370
371 if (phost->CmdBufferIndex < 3)
372 {
373 phost->CmdBuffer[phost->CmdBufferIndex++] = value & 0xFF;
374 phost->CmdBuffer[phost->CmdBufferIndex++] = value >> 8;
375 }
376
377 if (phost->CmdBufferIndex == 4)
378 {
379 phost->CmdBufferIndex = 0;
380 return EVE_Cmd_wrMem(phost, phost->CmdBuffer, 4);
381 }
382
383 return true;
384}
385
395{
396 eve_assert(!phost->CmdWaiting);
397 eve_assert(phost->CmdBufferIndex == 0);
398
399 if (phost->CmdSpace < 4 && !EVE_Cmd_waitSpace(phost, 4))
400 return false;
401
402 if (phost->Status != EVE_STATUS_WRITING)
403 {
404 if (EVE_Hal_supportCmdB(phost))
405 {
407 }
408 else
409 {
410#if !defined(EVE_SUPPORT_CMDB) || defined(EVE_MULTI_GRAPHICS_TARGET)
411 EVE_Hal_startTransfer(phost, EVE_TRANSFER_WRITE, RAM_CMD + phost->CmdWp);
412#else
413 eve_assert(false);
414#endif
415 }
416 }
417 EVE_Hal_transfer32(phost, value);
418 if (!phost->CmdFunc) /* Keep alive while writing function */
419 {
420 EVE_Hal_endTransfer(phost);
421 }
422 eve_assert(phost->CmdSpace >= 4);
423 phost->CmdSpace -= 4;
424#if !defined(EVE_SUPPORT_CMDB) || defined(EVE_MULTI_GRAPHICS_TARGET)
425 if (!EVE_Hal_supportCmdB(phost))
426 {
427 phost->CmdWp += 4;
428 phost->CmdWp &= EVE_CMD_FIFO_MASK;
429 if (!phost->CmdFunc) /* Defer write pointer */
430 {
431 EVE_Hal_wr16(phost, REG_CMD_WRITE, phost->CmdWp);
432 }
433 }
434#endif
435
436 return true;
437}
438
447{
448 uint16_t wp;
449 uint16_t prevWp;
450 eve_assert(!phost->CmdWaiting);
451 eve_assert(phost->CmdBufferIndex == 0);
452
453 if (!EVE_Cmd_waitSpace(phost, bytes))
454 return -1;
455
456 prevWp = EVE_Cmd_wp(phost);
457 wp = (prevWp + bytes) & EVE_CMD_FIFO_MASK;
458#if !defined(EVE_SUPPORT_CMDB) || defined(EVE_MULTI_GRAPHICS_TARGET)
459 if (!EVE_Hal_supportCmdB(phost))
460 {
461 phost->CmdWp = wp;
462 }
463#endif
464 EVE_Hal_wr16(phost, REG_CMD_WRITE, wp);
465
466 return prevWp;
467}
468
469#if defined(_DEBUG)
471#endif
472
481static bool checkWait(EVE_HalContext *phost, uint16_t rpOrSpace)
482{
483 /* Check for coprocessor fault */
484 if (EVE_CMD_FAULT(rpOrSpace))
485 {
486 char err[128];
487 err[0] = '\0';
488 /* Coprocessor fault */
489 phost->CmdWaiting = false;
490#if defined(_DEBUG)
491 if (!phost->DebugMessageVisible)
492 {
493 eve_printf_debug("Coprocessor fault\n");
494 debugBackupRamG(phost);
495 if (EVE_CHIPID >= EVE_BT815)
496 {
497 EVE_Hal_rdMem(phost, (uint8_t *)err, RAM_ERR_REPORT, 128);
498 eve_printf_debug("%s\n", err);
499 EVE_Hal_displayMessage(phost, err, 128);
500 }
501 else
502 {
503 EVE_Hal_displayMessage(phost, "Coprocessor fault ", sizeof("Coprocessor fault "));
504 }
505 }
506#endif
507 return false;
508 }
509
510 if (phost->Status == EVE_STATUS_ERROR || phost->Status == EVE_STATUS_CLOSED)
511 {
512 phost->CmdWaiting = false;
513 eve_printf_debug("Host error\n");
514 return false;
515 }
516
517 return true;
518}
519
528static bool handleWait(EVE_HalContext *phost, uint16_t rpOrSpace)
529{
530 /* Check for coprocessor fault */
531 if (!checkWait(phost, rpOrSpace))
532 return false;
533
534 /* Process any idling */
535 EVE_Hal_idle(phost);
536
537 /* Process user idling */
538 if (phost->CbCmdWait && !phost->CbCmdWait(phost))
539 {
540 /* Wait aborted */
541 phost->CmdWaiting = false;
542 eve_printf_debug("Wait for coprocessor aborted\n");
543 return false;
544 }
545 return true;
546}
547
556{
557 uint16_t rp;
558 uint16_t wp;
559
560 eve_assert(!phost->CmdWaiting);
561 phost->CmdWaiting = true;
562 while ((rp = EVE_Cmd_rp(phost)) != (wp = EVE_Cmd_wp(phost)))
563 {
564 if (!handleWait(phost, rp))
565 {
566 phost->CmdSpace = (rp - wp - 4) & EVE_CMD_FIFO_MASK;
567 return false;
568 }
569 }
570
571 /* Command buffer empty */
572 phost->CmdSpace = EVE_CMD_FIFO_SIZE - 4;
573 phost->CmdWaiting = false;
574 return true;
575}
576
581{
582 uint16_t space;
583
584 if (size > (EVE_CMD_FIFO_SIZE - 4))
585 {
586 eve_printf_debug("Requested free space exceeds coprocessor FIFO\n");
587 return 0;
588 }
589
590 eve_assert(!phost->CmdWaiting);
591 phost->CmdWaiting = true;
592
593 space = phost->CmdSpace;
594
595#if 1
596 /* Optimization.
597 Only update space if more space is needed than already known available,
598 or when not actually waiting for any space */
599 if (space < size || !size)
600 space = EVE_Cmd_space(phost);
601 if (!checkWait(phost, space))
602 return 0;
603#endif
604
605 /* Wait until there's sufficient space */
606 while (space < size)
607 {
608 space = EVE_Cmd_space(phost);
609 if (!handleWait(phost, space))
610 return 0;
611 }
612
613 /* Sufficient space */
614 phost->CmdWaiting = false;
615 return space;
616}
617
626{
627 uint16_t rp;
628 uint16_t wp;
629
630 eve_assert(!phost->CmdWaiting);
631 phost->CmdWaiting = true;
632
633 do
634 {
635 rp = EVE_Cmd_rp(phost);
636 wp = EVE_Cmd_wp(phost);
637 if (!handleWait(phost, rp))
638 return false;
639
640 } while ((wp != 0) || (rp != 0));
641
642 phost->CmdWaiting = false;
643 return true;
644}
645
652{
653 uint16_t rp;
654 uint16_t wp;
655
656 eve_assert(!phost->CmdWaiting);
657 phost->CmdWaiting = true;
658 while ((rp = EVE_Cmd_rp(phost)) != (wp = EVE_Cmd_wp(phost)))
659 {
660 if (EVE_Hal_rd32(phost, ptr) == value)
661 {
662 /* Expected value has been read */
663 phost->CmdWaiting = false;
664 return true;
665 }
666 if (!handleWait(phost, rp))
667 {
668 phost->CmdSpace = (rp - wp - 4) & EVE_CMD_FIFO_MASK;
669 return false;
670 }
671 }
672
673 /* Command buffer empty */
674 phost->CmdSpace = EVE_CMD_FIFO_SIZE - 4;
675 phost->CmdWaiting = false;
676 return EVE_Hal_rd32(phost, ptr) == value;
677}
678
683{
684 EVE_Cmd_rp(phost);
685 EVE_Cmd_wp(phost);
686 EVE_Cmd_space(phost);
687}
688
689/* end of file */
EVE_HAL_EXPORT bool EVE_Cmd_wrMem(EVE_HalContext *phost, const uint8_t *buffer, uint32_t size)
Write buffer to Coprocessor's comand fifo.
Definition EVE_Cmd.c:291
static bool checkWait(EVE_HalContext *phost, uint16_t rpOrSpace)
Check for coprocessor fault.
Definition EVE_Cmd.c:481
EVE_HAL_EXPORT bool EVE_Cmd_wr16(EVE_HalContext *phost, uint16_t value)
Write 2 bytes to Coprocessor's command fifo.
Definition EVE_Cmd.c:366
static void endFunc(EVE_HalContext *phost)
End read/write to Coprocessor.
Definition EVE_Cmd.c:44
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 uint32_t EVE_Cmd_wrString(EVE_HalContext *phost, const char *str, uint32_t maxLength)
Write a string into Coprocessor's command fifo.
Definition EVE_Cmd.c:322
static uint32_t wrString(EVE_HalContext *phost, const void *buffer, uint32_t *size, uint32_t transfered, uint32_t transfer)
Write string to Coprocessor's comand fifo.
Definition EVE_Cmd.c:163
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 bool EVE_Cmd_wr8(EVE_HalContext *phost, uint8_t value)
Write a byte to Coprocessor's command fifo.
Definition EVE_Cmd.c:339
EVE_HAL_EXPORT bool EVE_Cmd_waitFlush(EVE_HalContext *phost)
Wait till Command FIFO buffer empty.
Definition EVE_Cmd.c:555
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 handleWait(EVE_HalContext *phost, uint16_t rpOrSpace)
Wait handler.
Definition EVE_Cmd.c:528
static uint32_t wrBuffer(EVE_HalContext *phost, const void *buffer, uint32_t size, bool progmem, bool string)
Write buffer to Coprocessor's comand fifo.
Definition EVE_Cmd.c:191
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 bool EVE_Cmd_waitRead32(EVE_HalContext *phost, uint32_t ptr, uint32_t value)
Definition EVE_Cmd.c:651
static void startBufferTransfer(EVE_HalContext *phost)
Start transfer data to EVE.
Definition EVE_Cmd.c:134
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 void EVE_Cmd_restore(EVE_HalContext *phost)
Definition EVE_Cmd.c:682
EVE_HAL_EXPORT bool EVE_Cmd_waitLogo(EVE_HalContext *phost)
Wait until Coprocessor finished logo animation.
Definition EVE_Cmd.c:625
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
EVE's command read/write.
#define EVE_HAL_EXPORT
#define EVE_BT815
Definition EVE_Config.h:66
#define EVE_CHIPID
#define eve_progmem_const
#define RAM_ERR_REPORT
Definition EVE_GpuDefs.h:79
#define RAM_CMD
#define REG_CMDB_WRITE
#define EVE_CMD_FIFO_SIZE
Definition EVE_GpuDefs.h:58
#define EVE_CMD_FAULT(rp)
Definition EVE_GpuDefs.h:63
#define REG_CMD_WRITE
#define REG_CMDB_SPACE
#define REG_CMD_READ
#define EVE_CMD_FIFO_MASK
Definition EVE_GpuDefs.h:60
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 uint16_t EVE_Hal_rd16(EVE_HalContext *phost, uint32_t addr)
Read 2 bytes from Coprocessor's memory.
Definition EVE_Hal.c:173
void EVE_Hal_displayMessage(EVE_HalContext *phost, const char *str, uint16_t size)
Display a fullscreen debug message using TEXT8X8. Uses the back of RAM_G.
Definition EVE_Hal.c:486
EVE_HAL_EXPORT void EVE_Hal_idle(EVE_HalContext *phost)
Idle handler for Eve_Hal framework.
Definition EVE_Hal.c:136
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_STATUS_CLOSED
Definition EVE_HalDefs.h:57
@ EVE_STATUS_ERROR
Definition EVE_HalDefs.h:61
@ EVE_STATUS_WRITING
Definition EVE_HalDefs.h:60
EVE_HAL_EXPORT void EVE_Hal_startTransfer(EVE_HalContext *phost, EVE_TRANSFER_T rw, uint32_t addr)
Start data transfer to Coprocessor.
@ EVE_TRANSFER_WRITE
Definition EVE_HalDefs.h:68
EVE_HAL_EXPORT uint32_t EVE_Hal_transferString(EVE_HalContext *phost, const char *str, uint32_t index, uint32_t size, uint32_t padMask)
Transfer a string to EVE platform.
static bool EVE_Hal_supportCmdB(EVE_HalContext *phost)
EVE_HAL_EXPORT void EVE_Hal_transferMem(EVE_HalContext *phost, uint8_t *result, const uint8_t *buffer, uint32_t size)
Transfer (read/write) a block data to Coprocessor.
EVE_HAL_EXPORT void EVE_Hal_transferProgMem(EVE_HalContext *phost, uint8_t *result, eve_progmem_const uint8_t *buffer, uint32_t size)
Transfer a block data from program memory.
EVE_HAL_EXPORT void EVE_Hal_endTransfer(EVE_HalContext *phost)
End data transfer.
EVE_HAL_EXPORT uint32_t EVE_Hal_transfer32(EVE_HalContext *phost, uint32_t value)
Write 4 bytes to Coprocessor.
unsigned short uint16_t
unsigned int uint32_t
unsigned char uint8_t
Platform selector.
#define eve_assert(cond)
#define eve_printf_debug(fmt,...)
static ft_uint32_t ft_uint8_t * buffer
Definition FT_Gpu_Hal.h:139
uint8_t CmdBuffer[4]
EVE_Callback CbCmdWait
uint8_t CmdBufferIndex
bool DebugMessageVisible
EVE_STATUS_T Status
uint16_t CmdSpace