77#include " littlefs_FS_Driver.h"
88#include < stdlib.h>
99
10+ #if (HAL_USE_SDC == TRUE)
11+ #include < sdmmc_cmd.h>
12+
13+ // the mounted card and the volume it belongs to, from Target_System_IO_FileSystem.c
14+ extern " C" sdmmc_card_t *card;
15+ extern " C" char cardDriveLetter;
16+ #endif
17+
1018extern FileSystemVolume *g_FS_Volumes;
1119
1220static int32_t RemoveAllFiles (const char *path);
@@ -81,8 +89,6 @@ HRESULT LITTLEFS_FS_Driver::Format(const VOLUME_ID *volume, const char *volumeLa
8189
8290HRESULT LITTLEFS_FS_Driver::GetSizeInfo (const VOLUME_ID *volume, int64_t *totalSize, int64_t *totalFreeSpace)
8391{
84- (void )totalSize;
85-
8692 // FATFS *fsPtr = &fs;
8793 // char buffer[3];
8894 // DWORD freeClusters, freeSectors, totalSectors;
@@ -109,9 +115,30 @@ HRESULT LITTLEFS_FS_Driver::GetSizeInfo(const VOLUME_ID *volume, int64_t *totalS
109115 // // *totalFreeSpace = (int64_t)freeSectors * FF_MAX_SS;
110116 // // #endif
111117
118+ // -1 means "unknown" to the caller
112119 *totalSize = -1 ;
120+
121+ // free space would need f_getfree(), which walks the FAT and trips the watchdog on large cards
113122 *totalFreeSpace = -1 ;
114123
124+ #if (HAL_USE_SDC == TRUE)
125+
126+ // the driver also serves the internal flash, which has no card behind it
127+ FileSystemVolume *currentVolume = FileSystemVolumeList::FindVolume (volume->volumeId );
128+
129+ if (currentVolume != NULL && card != NULL && cardDriveLetter != 0 &&
130+ currentVolume->m_rootName [0 ] == cardDriveLetter)
131+ {
132+ // physical capacity of the card, not of the file system on it
133+ *totalSize = (int64_t )card->csd .capacity * card->csd .sector_size ;
134+ }
135+
136+ #else
137+
138+ (void )volume;
139+
140+ #endif
141+
115142 return S_OK ;
116143}
117144
@@ -282,6 +309,13 @@ HRESULT LITTLEFS_FS_Driver::Read(void *handle, uint8_t *buffer, int size, int *b
282309
283310 fileHandle = (LITTLEFS_FileHandle *)handle;
284311
312+ if (fileHandle->lastOp == LITTLEFS_LastOperation_Write && fseek (fileHandle->file , 0 , SEEK_CUR ) != 0 )
313+ {
314+ NANOCLR_SET_AND_LEAVE (CLR_E_FILE_IO );
315+ }
316+
317+ fileHandle->lastOp = LITTLEFS_LastOperation_Read;
318+
285319 // read from the file
286320 readCount = fread (buffer, 1 , size, fileHandle->file );
287321
@@ -324,6 +358,13 @@ HRESULT LITTLEFS_FS_Driver::Write(void *handle, uint8_t *buffer, int size, int *
324358
325359 fileHandle = (LITTLEFS_FileHandle *)handle;
326360
361+ if (fileHandle->lastOp == LITTLEFS_LastOperation_Read && fseek (fileHandle->file , 0 , SEEK_CUR ) != 0 )
362+ {
363+ NANOCLR_SET_AND_LEAVE (CLR_E_FILE_IO );
364+ }
365+
366+ fileHandle->lastOp = LITTLEFS_LastOperation_Write;
367+
327368 // write to the file
328369 writeCount = fwrite (buffer, 1 , size, fileHandle->file );
329370
@@ -397,6 +438,8 @@ HRESULT LITTLEFS_FS_Driver::Seek(void *handle, int64_t offset, uint32_t origin,
397438 return CLR_E_FILE_IO ;
398439 }
399440
441+ fileHandle->lastOp = LITTLEFS_LastOperation_None;
442+
400443 // get the current position
401444 *position = ftell (fileHandle->file );
402445
@@ -445,6 +488,8 @@ HRESULT LITTLEFS_FS_Driver::GetLength(void *handle, int64_t *length)
445488 return CLR_E_FILE_IO ;
446489 }
447490
491+ fileHandle->lastOp = LITTLEFS_LastOperation_None;
492+
448493 return S_OK ;
449494}
450495
@@ -489,6 +534,8 @@ HRESULT LITTLEFS_FS_Driver::SetLength(void *handle, int64_t length)
489534 return CLR_E_FILE_IO ;
490535 }
491536
537+ fileHandle->lastOp = LITTLEFS_LastOperation_None;
538+
492539 // Synchronize the file state
493540 fflush (fileHandle->file );
494541
0 commit comments