Freertos / Fatfs with hot card detection

I’ve got Fatfs running nicely with Freertos, but i would like to have the ability to have hot card detection, thus if the card is removed and new one added the file system is remounted. Hi has anyone tried doing this ? If so did you do all the card management in a task of its own ?

Freertos / Fatfs with hot card detection

Yes, I’m doing this unmount/mount based on card insert/removal. See FatFs with Kinetis Arduino Data-Logger Shield with the FRDM-KL25Z Board

Freertos / Fatfs with hot card detection

Nowhere on the links is there any description on how this would be achieved

Freertos / Fatfs with hot card detection

You would have to check the sources on GitHub 🙂 anyway, for your convenience, here is the important code: FAT1_CheckCardPresence() does the magic: static portTASK_FUNCTION(ShellTask, pvParameters) {

if PLHASSD_CARD

bool cardMounted = FALSE; static FAT1_FATFS fileSystemObject;

endif

unsigned char buf[48]; (void)pvParameters; /* not used / buf[0] = ‘’; (void)CLS1_ParseWithCommandTable((unsigned char)CLS1CMDHELP, CLS1_GetStdio(), CmdParserTable);

if PLHASSD_CARD

FAT1_Init();

endif

for(;;) {

if PLHASSD_CARD

(void)FAT1_CheckCardPresence(&cardMounted,
    0 /* volume */, &fileSystemObject, CLS1_GetStdio());

endif

(void)CLS1_ReadAndParseWithCommandTable(buf, sizeof(buf), CLS1_GetStdio(), CmdParserTable);
FRTOS1_vTaskDelay(50/portTICK_RATE_MS);
LEDG_Neg();
} } Which is implemented as: byte FAT1CheckCardPresence(bool *cardMounted, byte drive, FATFS *fileSystemObject, const CLS1StdIOType io) { if (!(cardMounted) && FAT1isDiskPresent()) { /* card inserted */ if (FAT1MountFileSystem(fileSystemObject, drive, io)==ERROK) { *cardMounted = TRUE; if (io!=NULL) { CLS1SendStr((unsigned char)”File System mountedrn”, io->stdOut); } } else { return ERR_FAILED; } } else if (cardMounted && !FAT1isDiskPresent()) { /* card removed */ if (FAT1UnMountFileSystem(drive, io)==ERROK) { *cardMounted = FALSE; if (io!=NULL) { CLS1SendStr((unsigned char*)”File System unmountedrn”, io->stdOut); } } else { return ERR_FAILED; } } return ERR_OK; } Everything else is on GitHub: https://github.com/ErichStyger/mcuoneclipse/tree/master/Examples/FRDM-KL25Z/Freedom_FatFS

Freertos / Fatfs with hot card detection

on Github there is very little source code, there is alot of *.src ??? i believe this is all PEX modules. where can i find what these functions are doing, i.e what interaction with FatFS FAT1MountFileSystem FAT1UnMountFileSystem

Freertos / Fatfs with hot card detection

These are simple wrappers the FatFS mount() and unmount(). The PEx module code for this module is here: https://github.com/ErichStyger/mcuoneclipse/blob/master/Drivers/sw/FAT_FileSystem.drv Search for MountFileSystem there. I hope this helps.

Freertos / Fatfs with hot card detection

Erich thanks for that, all working nicely 🙂 i do have one other question, to ensure FatFs can be accessed from multiple tasks did you do anything else other than the mutex protection that is in syscall.c ?

Freertos / Fatfs with hot card detection

That mutex protection is what I used. But if you have other tasks acessing the bus (SPI) or pins to the SD card, then of course you need to add another layer of mutual exclusion.