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_LoadFile.h File Reference

Eve_Hal framework APIs for loading file. More...

#include "EVE_Platform.h"

Go to the source code of this file.

Functions

EVE_HAL_EXPORT bool EVE_Util_loadSdCard (EVE_HalContext *phost)
 Mount the SDcard.
 
EVE_HAL_EXPORT bool EVE_Util_sdCardReady (EVE_HalContext *phost)
 
EVE_HAL_EXPORT bool EVE_Util_loadRawFile (EVE_HalContext *phost, uint32_t address, const char *filename)
 Load a raw file into RAM_G.
 
EVE_HAL_EXPORT bool EVE_Util_loadInflateFile (EVE_HalContext *phost, uint32_t address, const char *filename)
 Load file into RAM_G by CMD_INFLATE.
 
EVE_HAL_EXPORT bool EVE_Util_loadImageFile (EVE_HalContext *phost, uint32_t address, const char *filename, uint32_t *format)
 Load image into RAM_G.
 
EVE_HAL_EXPORT bool EVE_Util_loadCmdFile (EVE_HalContext *phost, const char *filename, uint32_t *transfered)
 
EVE_HAL_EXPORT size_t EVE_Util_readFile (EVE_HalContext *phost, uint8_t *buffer, size_t size, const char *filename)
 
EVE_HAL_EXPORT bool EVE_Util_loadMediaFile (EVE_HalContext *phost, const char *filename, uint32_t *transfered)
 
EVE_HAL_EXPORT void EVE_Util_closeFile (EVE_HalContext *phost)
 

Detailed Description

Eve_Hal framework APIs for loading file.

Author
Bridgetek
Date
2018

MIT License

Copyright (c) [2019] [Bridgetek Pte Ltd (BRTChip)]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Definition in file EVE_LoadFile.h.

Function Documentation

◆ EVE_Util_closeFile()

EVE_HAL_EXPORT void EVE_Util_closeFile ( EVE_HalContext phost)

Definition at line 555 of file EVE_LoadFile_FATFS.c.

556{
557#if defined(EVE_SUPPORT_MEDIAFIFO)
558 if (phost->LoadFileRemaining)
559 {
560#if EVE_ENABLE_FATFS
561 f_close(&phost->LoadFileObj);
562#endif
563 phost->LoadFileRemaining = 0;
564 }
565#endif
566}
FRESULT f_close(FIL *fp)
Definition ff.c:4152
ptrdiff_t LoadFileRemaining

◆ EVE_Util_loadCmdFile()

EVE_HAL_EXPORT bool EVE_Util_loadCmdFile ( EVE_HalContext phost,
const char *  filename,
uint32_t transfered 
)

Load a file into the coprocessor FIFO

Definition at line 334 of file EVE_LoadFile_FATFS.c.

335{
336#if EVE_ENABLE_FATFS
337 FRESULT fResult;
338 FIL InfSrc;
339
340 UINT blocklen;
341 int32_t filesize;
342 uint8_t buffer[512L];
343
344 if (!s_FatFSLoaded)
345 {
346 eve_printf_debug("SD card not ready\n");
347 return false;
348 }
349
350 fResult = f_open(&InfSrc, filename, FA_READ | FA_OPEN_EXISTING);
351 if (fResult == FR_DISK_ERR)
352 {
353 eve_printf_debug("Re-mount SD card\n");
354 s_FatFSLoaded = false;
355 sdhost_init();
356 EVE_Util_loadSdCard(phost);
357 fResult = f_open(&InfSrc, filename, FA_READ | FA_OPEN_EXISTING);
358 }
359 if (fResult == FR_OK)
360 {
361 filesize = f_size(&InfSrc);
362 while (filesize > 0)
363 {
364 fResult = f_read(&InfSrc, buffer, 512, &blocklen); // read a chunk of src file
365 filesize -= blocklen;
366 blocklen += 3;
367 blocklen &= ~3U;
368 if (!EVE_Cmd_wrMem(phost, (uint8_t *)buffer, blocklen))
369 break;
370 if (transfered)
371 *transfered += blocklen;
372 }
373 f_close(&InfSrc);
374 return EVE_Cmd_waitFlush(phost);
375 }
376 else
377 {
378 eve_printf_debug("Unable to open file: \"%s\"\n", filename);
379 return false;
380 }
381#else
382 eve_printf_debug("No filesystem support, cannot open: \"%s\"\n", filename);
383 return false;
384#endif
385}
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
int int32_t
unsigned char uint8_t
static bool s_FatFSLoaded
bool EVE_Util_loadSdCard(EVE_HalContext *phost)
Mount the SDcard.
#define eve_printf_debug(fmt,...)
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
FRESULT f_read(FIL *fp, void *buff, UINT btr, UINT *br)
Definition ff.c:3850
FRESULT f_open(FIL *fp, const TCHAR *path, BYTE mode)
Definition ff.c:3653
#define FA_OPEN_EXISTING
Definition ff.h:388
#define FA_READ
Definition ff.h:386
#define f_size(fp)
Definition ff.h:342
unsigned int UINT
Definition ff.h:58
FRESULT
Definition ff.h:276
@ FR_OK
Definition ff.h:277
@ FR_DISK_ERR
Definition ff.h:278
Definition ff.h:205

◆ EVE_Util_loadImageFile()

EVE_HAL_EXPORT bool EVE_Util_loadImageFile ( EVE_HalContext phost,
uint32_t  address,
const char *  filename,
uint32_t format 
)

Load image into RAM_G.

Load a file using CMD_LOADIMAGE. The image format is provided as output to the optional format argument

Parameters
phostPointer to Hal context
addressAddress in RAM_G
filenameFile to load
formatTarget format of image
Returns
true True if ok
false False if error

Definition at line 270 of file EVE_LoadFile_FATFS.c.

271{
272#if EVE_ENABLE_FATFS
273 FRESULT fResult;
274 FIL InfSrc;
275
276 UINT blocklen;
277 int32_t filesize;
278 uint8_t buffer[512L];
279
280 if (!s_FatFSLoaded)
281 {
282 eve_printf_debug("SD card not ready\n");
283 return false;
284 }
285
286 if (phost->CmdFault)
287 return false;
288
289 fResult = f_open(&InfSrc, filename, FA_READ | FA_OPEN_EXISTING);
290 if (fResult == FR_DISK_ERR)
291 {
292 eve_printf_debug("Re-mount SD card\n");
293 s_FatFSLoaded = false;
294 sdhost_init();
295 EVE_Util_loadSdCard(phost);
296 fResult = f_open(&InfSrc, filename, FA_READ | FA_OPEN_EXISTING);
297 }
298 if (fResult == FR_OK)
299 {
301 EVE_Cmd_wr32(phost, address);
302 EVE_Cmd_wr32(phost, OPT_NODL);
303 filesize = f_size(&InfSrc);
304 while (filesize > 0)
305 {
306 fResult = f_read(&InfSrc, buffer, 512, &blocklen); // read a chunk of src file
307 filesize -= blocklen;
308 blocklen += 3;
309 blocklen &= ~3U;
310 if (!EVE_Cmd_wrMem(phost, (uint8_t *)buffer, blocklen))
311 break;
312 }
313 f_close(&InfSrc);
314
315 if (!EVE_Cmd_waitFlush(phost))
316 return false;
317
318 if (format)
319 *format = EVE_Hal_rd32(phost, 0x3097e8);
320
321 return true;
322 }
323 else
324 {
325 eve_printf_debug("Unable to open file: \"%s\"\n", filename);
326 return false;
327 }
328#else
329 eve_printf_debug("No filesystem support, cannot open: \"%s\"\n", filename);
330 return false;
331#endif
332}
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
#define OPT_NODL
#define CMD_LOADIMAGE
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_Util_loadInflateFile()

EVE_HAL_EXPORT bool EVE_Util_loadInflateFile ( EVE_HalContext phost,
uint32_t  address,
const char *  filename 
)

Load file into RAM_G by CMD_INFLATE.

Parameters
phostPointer to Hal context
addressAddress to write
filenameFile to load
Returns
true True if ok
false False if error

Definition at line 207 of file EVE_LoadFile_FATFS.c.

208{
209#if EVE_ENABLE_FATFS
210 FRESULT fResult;
211 FIL InfSrc;
212
213 UINT blocklen;
214 int32_t filesize;
215 uint8_t buffer[512L];
216
217 if (!s_FatFSLoaded)
218 {
219 eve_printf_debug("SD card not ready\n");
220 return false;
221 }
222
223 fResult = f_open(&InfSrc, filename, FA_READ | FA_OPEN_EXISTING);
224 if (fResult == FR_DISK_ERR)
225 {
226 eve_printf_debug("Re-mount SD card\n");
227 s_FatFSLoaded = false;
228 sdhost_init();
229 EVE_Util_loadSdCard(phost);
230 fResult = f_open(&InfSrc, filename, FA_READ | FA_OPEN_EXISTING);
231 }
232 if (fResult == FR_OK)
233 {
235 EVE_Cmd_wr32(phost, address);
236 filesize = f_size(&InfSrc);
237 while (filesize > 0)
238 {
239 fResult = f_read(&InfSrc, buffer, 512, &blocklen); // read a chunk of src file
240 filesize -= blocklen;
241 blocklen += 3;
242 blocklen &= ~3U;
243 if (!EVE_Cmd_wrMem(phost, (uint8_t *)buffer, blocklen))
244 break;
245 }
246 f_close(&InfSrc);
247 return EVE_Cmd_waitFlush(phost);
248 }
249 else
250 {
251 eve_printf_debug("Unable to open file: \"%s\"\n", filename);
252 return false;
253 }
254#else
255 eve_printf_debug("No filesystem support, cannot open: \"%s\"\n", filename);
256 return false;
257#endif
258}
#define CMD_INFLATE

◆ EVE_Util_loadMediaFile()

EVE_HAL_EXPORT bool EVE_Util_loadMediaFile ( EVE_HalContext phost,
const char *  filename,
uint32_t transfered 
)

Load a file into the media FIFO. If transfered is set, the file may be streamed partially, and will be kept open until EVE_Util_closeFile is called, and stop once the coprocessor has processed it. Filename may be omitted in subsequent calls

Definition at line 432 of file EVE_LoadFile_FATFS.c.

433{
434
435#if EVE_ENABLE_FATFS && defined(EVE_SUPPORT_MEDIAFIFO)
436 FRESULT fResult = FR_INVALID_OBJECT;
437
438 UINT blocklen;
439 int32_t filesize;
440 uint32_t blockSize = min(512, ((phost->MediaFifoSize >> 3) << 2) - 4);
441 uint8_t buffer[512L];
442
443 if (!s_FatFSLoaded)
444 {
445 eve_printf_debug("SD card not ready\n");
446 return false;
447 }
448
449 if (phost->CmdFault)
450 return false;
451
452 if (transfered && phost->LoadFileRemaining)
453 {
454 filesize = phost->LoadFileRemaining;
455 }
456 else
457 {
458 if (!transfered)
459 {
460 EVE_Util_closeFile(phost);
461 }
462 fResult = f_open(&phost->LoadFileObj, filename, FA_READ | FA_OPEN_EXISTING);
463 if (fResult == FR_DISK_ERR)
464 {
465 eve_printf_debug("Re-mount SD card\n");
466 s_FatFSLoaded = false;
467 sdhost_init();
468 EVE_Util_loadSdCard(phost);
469 fResult = f_open(&phost->LoadFileObj, filename, FA_READ | FA_OPEN_EXISTING);
470 }
471 if (fResult == FR_OK)
472 {
473 filesize = f_size(&phost->LoadFileObj);
474 if (transfered)
475 {
476 phost->LoadFileRemaining = filesize;
477 }
478 if (filesize == 0)
479 {
480 /* Empty file, no-op */
481 eve_printf_debug("Empty file: \"%s\"\n", filename);
482 f_close(&phost->LoadFileObj);
483 return true;
484 }
485 }
486 else
487 {
488 eve_printf_debug("Unable to open file: \"%s\"\n", filename);
489 return false;
490 }
491 }
492
493 while (filesize > 0)
494 {
495 fResult = f_read(&phost->LoadFileObj, buffer, blockSize, &blocklen); // read a chunk of src file
496 if (fResult != FR_OK)
497 {
498 if (fResult == FR_DISK_ERR)
499 {
500 eve_printf_debug("Lost SD card\n");
501 s_FatFSLoaded = false;
502 sdhost_init();
503 }
504 break;
505 }
506
507 filesize -= blocklen;
508 blocklen += 3;
509 blocklen &= ~3U;
510
511 if (transfered)
512 {
513 uint32_t transferedPart = 0;
514 bool wrRes = EVE_MediaFifo_wrMem(phost, buffer, blocklen, &transferedPart); /* copy data continuously into media fifo memory */
515 *transfered += transferedPart;
516 if (transferedPart < blocklen)
517 {
518 long offset = (long)transferedPart - (long)blocklen; /* Negative */
519 f_lseek(&phost->LoadFileObj, f_tell(&phost->LoadFileObj) + offset); /* Seek back */
520 filesize -= offset; /* Increments remaining (double negative) */
521 break; /* Early exit, processing done */
522 }
523 if (!wrRes)
524 break;
525 }
526 else
527 {
528 if (!EVE_MediaFifo_wrMem(phost, buffer, blocklen, NULL)) /* copy data continuously into media fifo memory */
529 break; /* Coprocessor fault */
530 }
531 }
532
533 if (!transfered)
534 {
535 f_close(&phost->LoadFileObj); /* Close the opened file */
536 }
537 else if (filesize)
538 {
539 phost->LoadFileRemaining = filesize; /* Save remaining */
540 }
541 else
542 {
543 f_close(&phost->LoadFileObj);
544 phost->LoadFileRemaining = 0;
545 }
546
547 return (fResult == FR_OK) && (transfered ? EVE_Cmd_waitFlush(phost) : EVE_MediaFifo_waitFlush(phost, false));
548
549#else
550 eve_printf_debug("No filesystem support, cannot open: \"%s\"\n", filename);
551 return false;
552#endif
553}
unsigned int uint32_t
#define false
void EVE_Util_closeFile(EVE_HalContext *phost)
bool EVE_MediaFifo_waitFlush(EVE_HalContext *phost, bool orCmdFlush)
Wait for the media FIFO to fully empty.
bool EVE_MediaFifo_wrMem(EVE_HalContext *phost, const uint8_t *buffer, uint32_t size, uint32_t *transfered)
Write a buffer to the media FIFO. Waits if there is not enough space in the media FIFO.
FRESULT f_lseek(FIL *fp, FSIZE_t ofs)
Definition ff.c:4367
@ FR_INVALID_OBJECT
Definition ff.h:286
#define f_tell(fp)
Definition ff.h:341
uint32_t MediaFifoSize

◆ EVE_Util_loadRawFile()

EVE_HAL_EXPORT bool EVE_Util_loadRawFile ( EVE_HalContext phost,
uint32_t  address,
const char *  filename 
)

Load a raw file into RAM_G.

Parameters
phostPointer to Hal context
addressAddress in RAM_G
filenameFile to load
Returns
true True if ok
false False if error

Definition at line 148 of file EVE_LoadFile_FATFS.c.

149{
150#if EVE_ENABLE_FATFS
151 FRESULT fResult;
152 FIL InfSrc;
153
154 UINT blocklen;
155 int32_t filesize;
156 uint8_t buffer[512L];
157 uint32_t addr = address;
158
159 if (!s_FatFSLoaded)
160 {
161 eve_printf_debug("SD card not ready\n");
162 return false;
163 }
164
165 fResult = f_open(&InfSrc, filename, FA_READ | FA_OPEN_EXISTING);
166 if (fResult == FR_DISK_ERR)
167 {
168 eve_printf_debug("Re-mount SD card\n");
169 s_FatFSLoaded = false;
170 sdhost_init();
171 EVE_Util_loadSdCard(phost);
172 fResult = f_open(&InfSrc, filename, FA_READ | FA_OPEN_EXISTING);
173 }
174 if (fResult == FR_OK)
175 {
176 filesize = f_size(&InfSrc);
177 while (filesize > 0)
178 {
179 fResult = f_read(&InfSrc, buffer, 512, &blocklen); // read a chunk of src file
180 filesize -= blocklen;
181 EVE_Hal_wrMem(phost, addr, buffer, blocklen);
182 addr += blocklen;
183 }
184 f_close(&InfSrc);
185 return true;
186 }
187 else
188 {
189 eve_printf_debug("Unable to open file: \"%s\"\n", filename);
190 return false;
191 }
192#else
193 eve_printf_debug("No filesystem support, cannot open: \"%s\"\n", filename);
194 return false;
195#endif
196}
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
static ft_uint32_t addr
Definition FT_Gpu_Hal.h:139

◆ EVE_Util_loadSdCard()

EVE_HAL_EXPORT bool EVE_Util_loadSdCard ( EVE_HalContext phost)

Mount the SDcard.

Load SD card

Parameters
phostPointer to Hal context
Returns
true True if ok
false False if error

Definition at line 98 of file EVE_LoadFile_FATFS.c.

99{
100#if EVE_ENABLE_FATFS
101#ifndef PANL_APPLET
102 SDHOST_STATUS status = sdhost_card_detect();
103 if (status == SDHOST_CARD_INSERTED)
104 {
105 if (!s_FatFSLoaded && (f_mount(&s_FatFS, MOUNT_POINT, 1) != FR_OK))
106 {
107 printf("FatFS SD card mount failed\n");
108 }
109 else
110 {
111 printf("FatFS SD card mounted successfully\n");
112 s_FatFSLoaded = true;
113 }
114 }
115 else
116 {
117 printf("SD card not detected\n");
118 s_FatFSLoaded = false;
119 }
120#else
121 s_FatFSLoaded = true;
122#endif
123 return s_FatFSLoaded;
124#else
125 return false;
126#endif
127}
static FATFS s_FatFS
#define MOUNT_POINT
FRESULT f_mount(FATFS *fs, const TCHAR *path, BYTE opt)
Definition ff.c:3605

◆ EVE_Util_readFile()

EVE_HAL_EXPORT size_t EVE_Util_readFile ( EVE_HalContext phost,
uint8_t buffer,
size_t  size,
const char *  filename 
)

Read a file into a buffer, returns the number of bytes read

Definition at line 387 of file EVE_LoadFile_FATFS.c.

388{
389 // Read up to `size` number of bytes from the file into `buffer`, then return the number of read bytes
390#if EVE_ENABLE_FATFS
391 FRESULT fResult;
392 FIL InfSrc;
393
394 if (!s_FatFSLoaded)
395 {
396 eve_printf_debug("SD card not ready\n");
397 return false;
398 }
399
400 fResult = f_open(&InfSrc, filename, FA_READ | FA_OPEN_EXISTING);
401 if (fResult == FR_DISK_ERR)
402 {
403 eve_printf_debug("Re-mount SD card\n");
404 s_FatFSLoaded = false;
405#ifndef RP2040_PLATFORM
406 sdhost_init();
407#else
408 sd_init_card();
409#endif
410 EVE_Util_loadSdCard(phost);
411 fResult = f_open(&InfSrc, filename, FA_READ | FA_OPEN_EXISTING);
412 }
413 if (fResult == FR_OK)
414 {
415 size_t read;
416 fResult = f_read(&InfSrc, buffer, size, &read);
417 f_close(&InfSrc);
418 return read;
419 }
420 else
421 {
422 eve_printf_debug("Unable to open file: \"%s\"\n", filename);
423 return 0;
424 }
425
426#else
427 eve_printf_debug("No filesystem support, cannot open: \"%s\"\n", filename);
428 return 0;
429#endif
430}

◆ EVE_Util_sdCardReady()

EVE_HAL_EXPORT bool EVE_Util_sdCardReady ( EVE_HalContext phost)

Definition at line 129 of file EVE_LoadFile_FATFS.c.

130{
131 /* no-op */
132#if EVE_ENABLE_FATFS
133 return s_FatFSLoaded;
134#else
135 return false;
136#endif
137}