Skip to content

Commit 3ba46c7

Browse files
Lucme-no-dev
Luc
authored andcommitted
Add totalBytes / usedBytes to SD/SDMMC (espressif#673)
to be same as SPIFFS
1 parent a66b544 commit 3ba46c7

File tree

6 files changed

+65
-0
lines changed

6 files changed

+65
-0
lines changed

libraries/SD/examples/SD_Test/SD_Test.ino

+2
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ void setup(){
212212
renameFile(SD, "/hello.txt", "/foo.txt");
213213
readFile(SD, "/foo.txt");
214214
testFileIO(SD, "/test.txt");
215+
Serial.printf("Total space: %lluMB\n", SD.totalBytes() / (1024 * 1024));
216+
Serial.printf("Used space: %lluMB\n", SD.usedBytes() / (1024 * 1024));
215217
}
216218

217219
void loop(){

libraries/SD/src/SD.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "vfs_api.h"
1616
#include "sd_diskio.h"
17+
#include "ff.h"
1718
#include "FS.h"
1819
#include "SD.h"
1920

@@ -73,4 +74,32 @@ uint64_t SDFS::cardSize()
7374
return (uint64_t)sectors * sectorSize;
7475
}
7576

77+
uint64_t SDFS::totalBytes()
78+
{
79+
FATFS* fsinfo;
80+
DWORD fre_clust;
81+
if(f_getfree("0:",&fre_clust,&fsinfo)!= 0) return 0;
82+
uint64_t size = (fsinfo->csize)*(fsinfo->n_fatent - 2)
83+
#if _MAX_SS != 512
84+
*(fsinfo->ssize);
85+
#else
86+
*512;
87+
#endif
88+
return size;
89+
}
90+
91+
uint64_t SDFS::usedBytes()
92+
{
93+
FATFS* fsinfo;
94+
DWORD fre_clust;
95+
if(f_getfree("0:",&fre_clust,&fsinfo)!= 0) return 0;
96+
uint64_t size = (fsinfo->csize)*((fsinfo->n_fatent - 2) - (fsinfo->free_clst))
97+
#if _MAX_SS != 512
98+
*(fsinfo->ssize);
99+
#else
100+
*512;
101+
#endif
102+
return size;
103+
}
104+
76105
SDFS SD = SDFS(FSImplPtr(new VFSImpl()));

libraries/SD/src/SD.h

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class SDFS : public FS
3232
void end();
3333
sdcard_type_t cardType();
3434
uint64_t cardSize();
35+
uint64_t totalBytes();
36+
uint64_t usedBytes();
3537
};
3638

3739
}

libraries/SD_MMC/examples/SDMMC_Test/SDMMC_Test.ino

+2
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ void setup(){
209209
renameFile(SD_MMC, "/hello.txt", "/foo.txt");
210210
readFile(SD_MMC, "/foo.txt");
211211
testFileIO(SD_MMC, "/test.txt");
212+
Serial.printf("Total space: %lluMB\n", SD_MMC.totalBytes() / (1024 * 1024));
213+
Serial.printf("Used space: %lluMB\n", SD_MMC.usedBytes() / (1024 * 1024));
212214
}
213215

214216
void loop(){

libraries/SD_MMC/src/SD_MMC.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ extern "C" {
2323
#include "driver/sdmmc_defs.h"
2424
#include "sdmmc_cmd.h"
2525
}
26+
#include "ff.h"
2627
#include "SD_MMC.h"
2728

2829
using namespace fs;
@@ -98,5 +99,32 @@ uint64_t SDMMCFS::cardSize()
9899
return (uint64_t)_card->csd.capacity * _card->csd.sector_size;
99100
}
100101

102+
uint64_t SDMMCFS::totalBytes()
103+
{
104+
FATFS* fsinfo;
105+
DWORD fre_clust;
106+
if(f_getfree("0:",&fre_clust,&fsinfo)!= 0) return 0;
107+
uint64_t size = (fsinfo->csize)*(fsinfo->n_fatent - 2)
108+
#if _MAX_SS != 512
109+
*(fsinfo->ssize);
110+
#else
111+
*512;
112+
#endif
113+
return size;
114+
}
115+
116+
uint64_t SDMMCFS::usedBytes()
117+
{
118+
FATFS* fsinfo;
119+
DWORD fre_clust;
120+
if(f_getfree("0:",&fre_clust,&fsinfo)!= 0) return 0;
121+
uint64_t size = (fsinfo->csize)*((fsinfo->n_fatent - 2) - (fsinfo->free_clst))
122+
#if _MAX_SS != 512
123+
*(fsinfo->ssize);
124+
#else
125+
*512;
126+
#endif
127+
return size;
128+
}
101129

102130
SDMMCFS SD_MMC = SDMMCFS(FSImplPtr(new VFSImpl()));

libraries/SD_MMC/src/SD_MMC.h

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class SDMMCFS : public FS
3232
void end();
3333
sdcard_type_t cardType();
3434
uint64_t cardSize();
35+
uint64_t totalBytes();
36+
uint64_t usedBytes();
3537
};
3638

3739
}

0 commit comments

Comments
 (0)