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_FATFS.c
Go to the documentation of this file.
1
32#include "EVE_LoadFile.h"
33#include "EVE_Platform.h"
34
35#if defined(FT9XX_PLATFORM) || defined(RP2040_PLATFORM)
36#define MOUNT_POINT ""
37#if EVE_ENABLE_FATFS
38#include "ff.h"
39static bool s_FatFSLoaded = false;
41#endif
42
43#if defined(RP2040_PLATFORM)
44
45SDHOST_STATUS sdhost_card_detect(void) {
46 FRESULT fr; /* FatFs return code */
47 UINT br;
48 UINT bw;
49
50 fr = f_mount(&s_FatFS, MOUNT_POINT, 1);
51 if (fr != FR_OK) {
52 eve_printf_debug("mount error %d\n", fr);
53 return SDHOST_CARD_REMOVED;
54 }
55 eve_printf_debug("mount ok\n");
56 return SDHOST_CARD_INSERTED;
57}
58
59void sdhost_init(void) {
60 FRESULT fr; /* FatFs return code */
61 UINT br;
62 UINT bw;
63
64 fr = f_mount(&s_FatFS, MOUNT_POINT, 1);
65 if (fr != FR_OK) {
66 eve_printf_debug("mount error %d\n", fr);
67 }
68 eve_printf_debug("mount ok\n");
69
70 switch (s_FatFS.fs_type) {
71 case FS_FAT12:
72 printf("Type is FAT12\n");
73 break;
74 case FS_FAT16:
75 printf("Type is FAT16\n");
76 break;
77 case FS_FAT32:
78 printf("Type is FAT32\n");
79 break;
80 case FS_EXFAT:
81 printf("Type is EXFAT\n");
82 break;
83 default:
84 printf("Type is unknown\n");
85 break;
86 }
87 eve_printf_debug("Card size: %7.2f GB (GB = 1E9 bytes)\n\n", s_FatFS.csize * s_FatFS.n_fatent * 512E-9);
88}
89#endif
90
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}
128
130{
131 /* no-op */
132#if EVE_ENABLE_FATFS
133 return s_FatFSLoaded;
134#else
135 return false;
136#endif
137}
138
148bool EVE_Util_loadRawFile(EVE_HalContext *phost, uint32_t address, const char *filename)
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}
197
207bool EVE_Util_loadInflateFile(EVE_HalContext *phost, uint32_t address, const char *filename)
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}
259
270bool EVE_Util_loadImageFile(EVE_HalContext *phost, uint32_t address, const char *filename, uint32_t *format)
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}
333
334bool EVE_Util_loadCmdFile(EVE_HalContext *phost, const char *filename, uint32_t *transfered)
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}
386
387size_t EVE_Util_readFile(EVE_HalContext *phost, uint8_t *buffer, size_t size, const char *filename)
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}
431
432bool EVE_Util_loadMediaFile(EVE_HalContext *phost, const char *filename, uint32_t *transfered)
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}
554
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}
567
568#endif
569
570/* 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
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 EVE_HAL_EXPORT
#define OPT_NODL
#define CMD_INFLATE
#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_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
int int32_t
unsigned int uint32_t
unsigned char uint8_t
Eve_Hal framework APIs for loading file.
bool EVE_Util_loadRawFile(EVE_HalContext *phost, uint32_t address, const char *filename)
Load a raw file into RAM_G.
static bool s_FatFSLoaded
static FATFS s_FatFS
bool EVE_Util_loadSdCard(EVE_HalContext *phost)
Mount the SDcard.
void EVE_Util_closeFile(EVE_HalContext *phost)
bool EVE_Util_loadCmdFile(EVE_HalContext *phost, const char *filename, uint32_t *transfered)
bool EVE_Util_loadInflateFile(EVE_HalContext *phost, uint32_t address, const char *filename)
Load file into RAM_G by CMD_INFLATE.
#define MOUNT_POINT
bool EVE_Util_loadImageFile(EVE_HalContext *phost, uint32_t address, const char *filename, uint32_t *format)
Load image into RAM_G.
size_t EVE_Util_readFile(EVE_HalContext *phost, uint8_t *buffer, size_t size, const char *filename)
bool EVE_Util_loadMediaFile(EVE_HalContext *phost, const char *filename, uint32_t *transfered)
EVE_HAL_EXPORT bool EVE_Util_sdCardReady(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.
Platform selector.
#define eve_printf_debug(fmt,...)
static ft_uint32_t ft_uint8_t * buffer
Definition FT_Gpu_Hal.h:139
static ft_uint32_t addr
Definition FT_Gpu_Hal.h:139
void EVE_Cmd_waitFlush(EVE_HalContext *host)
Definition Gpu_Hal.cpp:775
FRESULT f_mount(FATFS *fs, const TCHAR *path, BYTE opt)
Definition ff.c:3605
FRESULT f_close(FIL *fp)
Definition ff.c:4152
FRESULT f_lseek(FIL *fp, FSIZE_t ofs)
Definition ff.c:4367
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_INVALID_OBJECT
Definition ff.h:286
@ FR_OK
Definition ff.h:277
@ FR_DISK_ERR
Definition ff.h:278
#define FS_EXFAT
Definition ff.h:408
#define f_tell(fp)
Definition ff.h:341
#define FS_FAT16
Definition ff.h:406
#define FS_FAT12
Definition ff.h:405
#define FS_FAT32
Definition ff.h:407
uint32_t MediaFifoSize
ptrdiff_t LoadFileRemaining
Definition ff.h:132
DWORD n_fatent
Definition ff.h:165
WORD csize
Definition ff.h:140
BYTE fs_type
Definition ff.h:133
Definition ff.h:205