From 3ba46c7fbcb71e46acc9b7d0fcfe07e6b657426e Mon Sep 17 00:00:00 2001 From: Luc Date: Thu, 28 Sep 2017 10:01:02 +0800 Subject: [PATCH] Add totalBytes / usedBytes to SD/SDMMC (#673) to be same as SPIFFS --- libraries/SD/examples/SD_Test/SD_Test.ino | 2 ++ libraries/SD/src/SD.cpp | 29 +++++++++++++++++++ libraries/SD/src/SD.h | 2 ++ .../SD_MMC/examples/SDMMC_Test/SDMMC_Test.ino | 2 ++ libraries/SD_MMC/src/SD_MMC.cpp | 28 ++++++++++++++++++ libraries/SD_MMC/src/SD_MMC.h | 2 ++ 6 files changed, 65 insertions(+) diff --git a/libraries/SD/examples/SD_Test/SD_Test.ino b/libraries/SD/examples/SD_Test/SD_Test.ino index 69c59766..6059446e 100644 --- a/libraries/SD/examples/SD_Test/SD_Test.ino +++ b/libraries/SD/examples/SD_Test/SD_Test.ino @@ -212,6 +212,8 @@ void setup(){ renameFile(SD, "/hello.txt", "/foo.txt"); readFile(SD, "/foo.txt"); testFileIO(SD, "/test.txt"); + Serial.printf("Total space: %lluMB\n", SD.totalBytes() / (1024 * 1024)); + Serial.printf("Used space: %lluMB\n", SD.usedBytes() / (1024 * 1024)); } void loop(){ diff --git a/libraries/SD/src/SD.cpp b/libraries/SD/src/SD.cpp index 65e68c0f..be307f39 100644 --- a/libraries/SD/src/SD.cpp +++ b/libraries/SD/src/SD.cpp @@ -14,6 +14,7 @@ #include "vfs_api.h" #include "sd_diskio.h" +#include "ff.h" #include "FS.h" #include "SD.h" @@ -73,4 +74,32 @@ uint64_t SDFS::cardSize() return (uint64_t)sectors * sectorSize; } +uint64_t SDFS::totalBytes() +{ + FATFS* fsinfo; + DWORD fre_clust; + if(f_getfree("0:",&fre_clust,&fsinfo)!= 0) return 0; + uint64_t size = (fsinfo->csize)*(fsinfo->n_fatent - 2) +#if _MAX_SS != 512 + *(fsinfo->ssize); +#else + *512; +#endif + return size; +} + +uint64_t SDFS::usedBytes() +{ + FATFS* fsinfo; + DWORD fre_clust; + if(f_getfree("0:",&fre_clust,&fsinfo)!= 0) return 0; + uint64_t size = (fsinfo->csize)*((fsinfo->n_fatent - 2) - (fsinfo->free_clst)) +#if _MAX_SS != 512 + *(fsinfo->ssize); +#else + *512; +#endif + return size; +} + SDFS SD = SDFS(FSImplPtr(new VFSImpl())); diff --git a/libraries/SD/src/SD.h b/libraries/SD/src/SD.h index cf49bb8f..54e41ce3 100644 --- a/libraries/SD/src/SD.h +++ b/libraries/SD/src/SD.h @@ -32,6 +32,8 @@ public: void end(); sdcard_type_t cardType(); uint64_t cardSize(); + uint64_t totalBytes(); + uint64_t usedBytes(); }; } diff --git a/libraries/SD_MMC/examples/SDMMC_Test/SDMMC_Test.ino b/libraries/SD_MMC/examples/SDMMC_Test/SDMMC_Test.ino index bfd98681..b9e87285 100644 --- a/libraries/SD_MMC/examples/SDMMC_Test/SDMMC_Test.ino +++ b/libraries/SD_MMC/examples/SDMMC_Test/SDMMC_Test.ino @@ -209,6 +209,8 @@ void setup(){ renameFile(SD_MMC, "/hello.txt", "/foo.txt"); readFile(SD_MMC, "/foo.txt"); testFileIO(SD_MMC, "/test.txt"); + Serial.printf("Total space: %lluMB\n", SD_MMC.totalBytes() / (1024 * 1024)); + Serial.printf("Used space: %lluMB\n", SD_MMC.usedBytes() / (1024 * 1024)); } void loop(){ diff --git a/libraries/SD_MMC/src/SD_MMC.cpp b/libraries/SD_MMC/src/SD_MMC.cpp index 54b97c1e..d554413b 100644 --- a/libraries/SD_MMC/src/SD_MMC.cpp +++ b/libraries/SD_MMC/src/SD_MMC.cpp @@ -23,6 +23,7 @@ extern "C" { #include "driver/sdmmc_defs.h" #include "sdmmc_cmd.h" } +#include "ff.h" #include "SD_MMC.h" using namespace fs; @@ -98,5 +99,32 @@ uint64_t SDMMCFS::cardSize() return (uint64_t)_card->csd.capacity * _card->csd.sector_size; } +uint64_t SDMMCFS::totalBytes() +{ + FATFS* fsinfo; + DWORD fre_clust; + if(f_getfree("0:",&fre_clust,&fsinfo)!= 0) return 0; + uint64_t size = (fsinfo->csize)*(fsinfo->n_fatent - 2) +#if _MAX_SS != 512 + *(fsinfo->ssize); +#else + *512; +#endif + return size; +} + +uint64_t SDMMCFS::usedBytes() +{ + FATFS* fsinfo; + DWORD fre_clust; + if(f_getfree("0:",&fre_clust,&fsinfo)!= 0) return 0; + uint64_t size = (fsinfo->csize)*((fsinfo->n_fatent - 2) - (fsinfo->free_clst)) +#if _MAX_SS != 512 + *(fsinfo->ssize); +#else + *512; +#endif + return size; +} SDMMCFS SD_MMC = SDMMCFS(FSImplPtr(new VFSImpl())); diff --git a/libraries/SD_MMC/src/SD_MMC.h b/libraries/SD_MMC/src/SD_MMC.h index fad774cb..e3dc1543 100644 --- a/libraries/SD_MMC/src/SD_MMC.h +++ b/libraries/SD_MMC/src/SD_MMC.h @@ -32,6 +32,8 @@ public: void end(); sdcard_type_t cardType(); uint64_t cardSize(); + uint64_t totalBytes(); + uint64_t usedBytes(); }; }