Hi,
I am testing features and currently I have problems with:
- how to read empty space on USB and own Flash, I need it to check if I can create new file or not.
- I create an Flash drive but its size is 1MB, how to change it and how to get/read free space from it.
My test code below.
Regards
#include <DigitalOut.h>
#include <FATFileSystem.h>
#include <Arduino_USBHostMbed5.h>
#include "QSPIFBlockDevice.h"
#include "MBRBlockDevice.h"
#include <PluggableUSBMSD.h>
QSPIFBlockDevice root;
mbed::MBRBlockDevice user_data(&root, 3);
mbed::FATFileSystem data_fs("fs");
USBMSD MassStorage(&root);
int flashSetup()
{
int err = data_fs.mount(&root);
return err;
}
void flashWrite(String fn)
{
String fname="/fs/";
fname.concat(fn);
//fname.concat(".csv");
mbed::fs_file_t file;
struct dirent *ent;
int dirIndex = 0;
int res = 0;
FILE *f = fopen(fname.c_str(), "w+");
for (int i = 0; i < 10; i++)
{
int err = fprintf(f, "%d\n", i);
}
int err = fclose(f);
}
int doesFileExist(String myfile)
{
bool ex=false;
char buf[256];
int mresult=0;
DIR* d = opendir("/fs/");
if (!d)
{
snprintf(buf, sizeof(buf), "error: %s (%d)\r\n", strerror(errno), -errno);
//Serial.print(buf);
}
//Serial.println("done.");
//Serial.println("Root directory:");
unsigned int count { 0 };
while (true)
{
struct dirent* e = readdir(d);
if (!e)
{
break;
}
count++;
snprintf(buf, sizeof(buf), " %s\r\n", e->d_name);
//Serial.print(buf);
String fn=String(e->d_name);
if (fn==myfile)
{
Serial.print("-----exist------");
Serial.println(e->d_name);
mresult=1;
}
}
snprintf(buf, sizeof(buf), "Closing the root directory (fileExist)... ");
//Serial.print(buf);
fflush(stdout);
int err = closedir(d);
snprintf(buf, sizeof(buf), "%s\r\n", (err < 0 ? "Fail :(" : "OK"));
//Serial.print(buf);
if (err < 0) {
snprintf(buf, sizeof(buf), "error: %s (%d)\r\n", strerror(errno), -errno);
Serial.print(buf);
}
return mresult;
}
void FlashDir()
{
Serial.println("Starting Flash Dir List example...");
Serial.print("Mounting Flash device... ");
Serial.println("done.");
char buf[256];
// Display the root directory
Serial.print("Opening the Flash root directory... ");
DIR* d = opendir("/fs/");
Serial.println(!d ? "Fail :(" : "Done");
if (!d)
{
snprintf(buf, sizeof(buf), "error: %s (%d)\r\n", strerror(errno), -errno);
Serial.print(buf);
}
Serial.println("done.");
Serial.println("Root directory:");
unsigned int count { 0 };
while (true)
{
struct dirent* e = readdir(d);
if (!e) {
break;
}
count++;
snprintf(buf, sizeof(buf), " %s\r\n", e->d_name);
Serial.print(buf);
}
Serial.print(count);
Serial.println(" files found!");
snprintf(buf, sizeof(buf), "Closing the Flash root directory... ");
Serial.print(buf);
fflush(stdout);
int err = closedir(d);
snprintf(buf, sizeof(buf), "%s\r\n", (err < 0 ? "Fail :(" : "OK"));
Serial.print(buf);
if (err < 0)
{
snprintf(buf, sizeof(buf), "error: %s (%d)\r\n", strerror(errno), -errno);
Serial.print(buf);
}
}
void setup()
{
Serial.begin(9600);
while (!Serial)
;
flashSetup();
flashWrite("test11NR.csv");
FlashDir();
Serial.print("file exist=");
Serial.println(doesFileExist("test11NR.csv"));
}
void loop()
{
}





