Skip to content

Commit e9ba810

Browse files
authored
Report the SD card capacity in GetSizeInfo (#3529)
***NO_CI***
1 parent 57f04ea commit e9ba810

3 files changed

Lines changed: 65 additions & 7 deletions

File tree

targets/ChibiOS/_FatFs/fatfs_FS_Driver.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,11 @@ HRESULT FATFS_FS_Driver::Format(const VOLUME_ID *volume, const char *volumeLabel
166166

167167
HRESULT FATFS_FS_Driver::GetSizeInfo(const VOLUME_ID *volume, int64_t *totalSize, int64_t *totalFreeSpace)
168168
{
169-
(void)totalSize;
170-
171169
// FATFS *fsPtr = &fs;
172170
// char buffer[3];
173171
// DWORD freeClusters, freeSectors, totalSectors;
174172

175-
// FATFS *fs = GetFatFsByVolumeId(volume, false);
173+
FATFS *fs = GetFatFsByVolumeId(volume, false);
176174

177175
FileSystemVolume *currentVolume = FileSystemVolumeList::FindVolume(volume->volumeId);
178176

@@ -194,9 +192,22 @@ HRESULT FATFS_FS_Driver::GetSizeInfo(const VOLUME_ID *volume, int64_t *totalSize
194192
// *totalFreeSpace = (int64_t)freeSectors * FF_MAX_SS;
195193
// #endif
196194

195+
// -1 means "unknown" to the caller
197196
*totalSize = -1;
197+
198+
// free space would need f_getfree(), see above
198199
*totalFreeSpace = -1;
199200

201+
if (fs != NULL)
202+
{
203+
// capacity of the file system, from the mounted FATFS object, without walking the FAT
204+
#if FF_MAX_SS != FF_MIN_SS
205+
*totalSize = (int64_t)(fs->n_fatent - 2) * fs->csize * fs->ssize;
206+
#else
207+
*totalSize = (int64_t)(fs->n_fatent - 2) * fs->csize * FF_MAX_SS;
208+
#endif
209+
}
210+
200211
return S_OK;
201212
}
202213

targets/ESP32/_common/Target_System_IO_FileSystem.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ static const char *TAG = "SDCard";
4545

4646
sdmmc_card_t *card;
4747

48+
// drive letter the card above is mounted under, 0 when it is not mounted
49+
char cardDriveLetter;
50+
4851
//
4952
// Unmount SD card ( MMC/SDIO or SPI)
5053
//
@@ -62,6 +65,7 @@ bool Storage_UnMountSDCard(int driveIndex)
6265
}
6366

6467
card = NULL;
68+
cardDriveLetter = 0;
6569

6670
return true;
6771
}
@@ -183,7 +187,15 @@ bool Storage_MountMMC(bool bit1Mode, int driveIndex)
183187
errCode = esp_vfs_fat_sdmmc_mount(mountPoint, &host, &slot_config, &mount_config, &card);
184188
}
185189

186-
return LogMountResult(errCode);
190+
if (!LogMountResult(errCode))
191+
{
192+
return false;
193+
}
194+
195+
// only stored on success, a failed mount leaves no card to bind the volume to
196+
cardDriveLetter = INDEX0_DRIVE_LETTER[0] + driveIndex;
197+
198+
return true;
187199
}
188200
#endif
189201

@@ -237,7 +249,15 @@ bool Storage_MountSpi(int spiBus, uint32_t csPin, int driveIndex)
237249
errCode = esp_vfs_fat_sdspi_mount(mountPoint, &host, &slot_config, &mount_config, &card);
238250
}
239251

240-
return LogMountResult(errCode);
252+
if (!LogMountResult(errCode))
253+
{
254+
return false;
255+
}
256+
257+
// only stored on success, a failed mount leaves no card to bind the volume to
258+
cardDriveLetter = INDEX0_DRIVE_LETTER[0] + driveIndex;
259+
260+
return true;
241261
}
242262

243263
#endif

targets/ESP32/_littlefs/littlefs_FS_Driver.cpp

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
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+
1018
extern FileSystemVolume *g_FS_Volumes;
1119

1220
static int32_t RemoveAllFiles(const char *path);
@@ -81,8 +89,6 @@ HRESULT LITTLEFS_FS_Driver::Format(const VOLUME_ID *volume, const char *volumeLa
8189

8290
HRESULT 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

0 commit comments

Comments
 (0)