Go Down

Topic: Read and write from SD card along with calculating space and free space? (Read 71 times) previous topic - next topic

Hello!! everyone i am using SDfat library from this link

My requirement is to calculate free space, total space and perform logging operations like reading and writing from and to memory card.

Here is my code free space and user space calculation.

Code: [Select]

#include <SdFat.h>

const uint8_t ChipSelect=4;

Sd2Card Card;
SdVolume Volume;
SdFile myFile;
SdFat intSd;
uint32_t cardSize;
uint32_t cardErase;

void setup()
{
  Serial.begin(115200);
  Serial.println("Initializing Memory Card");
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);
  if(!Card.init(SPI_HALF_SPEED,ChipSelect))
  {
    Serial.println("Card Init Failed");
    return;
  }
  Serial.println("Card Initialized");
 
  cardSize = Card.cardSize();               // This will return the total number of blocks in the card. 
  if(cardSize == 0)
  {
    Serial.println("Card Size Failed");
  }
  float cardSizeKB = cardSize*512/1024.0;  // 1 Block = 512 bytes
  Serial.print(cardSizeKB);
  Serial.println(" KB");
 
  float cardSizeMB = cardSizeKB/1024.0;  // 1 MB = 1024KB
  Serial.print(cardSizeMB);
  Serial.println(" MB");
 
  float cardSizeGB = cardSizeMB/1024.0;  // 1 GB = 1024MB
  Serial.print(cardSizeGB);
  Serial.println(" GB");
 
  Serial.print("Card Type = ");
  switch(Card.type())
  {
    case SD_CARD_TYPE_SD1:
    Serial.println("SD V1");
    break;
    case SD_CARD_TYPE_SD2:
    Serial.println("SD V2");
    break;
    case SD_CARD_TYPE_SDHC:
    Serial.println("SD High Capacity Card");
    break;
  }
  Volume.init(&Card);
  uint32_t clusterSize = Volume.freeClusterCount();
  Serial.print("Free Cluster: ");
  Serial.println(clusterSize,DEC);
 
  float freeSpace = clusterSize*512*Volume.blocksPerCluster();
  Serial.print("Free Space in Memory Card: ");
  Serial.print(freeSpace);
  Serial.println("bytes");
 
  float freeSpaceKB = freeSpace/1024;
  Serial.print("Free Space in Memory Card: ");
  Serial.print(freeSpaceKB);
  Serial.println("KB");
 
  float freeSpaceMB = freeSpaceKB/1024;
  Serial.print("Free Space in Memory Card: ");
  Serial.print(freeSpaceMB);
  Serial.println("MB");
 
  float freeSpaceGB = freeSpaceMB/1024;
  Serial.print("Free Space in Memory Card: ");
  Serial.print(freeSpaceGB);
  Serial.println("GB");
}

void loop()
{
}


This code is working fine.

Now the code to read and write to SD Card is as follow:-

Code: [Select]


#include <SdFat.h>

const uint8_t ChipSelect=4;

Sd2Card Card;
SdVolume Volume;
SdFile myFile;
SdFat intSd;
uint32_t cardSize;
uint32_t cardErase;

void setup()
{
  Serial.begin(115200);
  Serial.println("Initializing Memory Card");
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);

// Writing to Files
  if (!intSd.begin(ChipSelect,SPI_HALF_SPEED))
  {
    Serial.println("SD Card Not Initialized");
    return;
  }
  Serial.println("SD Card Initialized");
  // open the file for write at end like the Native SD library
  if (!myFile.open("testing.txt", O_RDWR | O_CREAT | O_AT_END))
  {
   Serial.println("opening test.txt for write failed");
   return;
  }
  Serial.println("File Opened Successfully and now writing to file");
  myFile.println("Hello!! SD Card Testing");
  myFile.close();  // Close the file
 
  Serial.println("Writing done and now opening file in Reading mode");
  if (!myFile.open("testing.txt", O_READ))
  {
   Serial.println("opening test.txt for reading failed");
   return;
  }
  Serial.println("File Opened Successfully and now reading from file");
  int data;
  while ((data = myFile.read()) >= 0)
  Serial.write(data);
  // close the file:
  myFile.close();
}

void loop()
{
}



How to merge these both codes?
Sd2Card class doesn't works with FAT read write operation.
while SdFat class doesn't work with freeSpace calculation and total space opeartions.

fat16lib

Quote
while SdFat class doesn't work with freeSpace calculation and total space opeartions.
You can get the free cluster count in SdFat.  

You probably want the total cluster count, not SD size.  Operating systems like Windows give you those numbers for total space, free space, and used space.  

This program:
Code: [Select]

#include <SdFat.h>

SdFat sd;

void setup() {
  Serial.begin(9600);
  if (!sd.begin(10)) {
    Serial.println(F("sd.begin failed"));
    return;
  }
  Serial.print(F("Card Size MB:   "));
  Serial.println(0.000512*sd.card()->cardSize());  
  float clusterSize = 0.000512*sd.vol()->blocksPerCluster();
  Serial.print(F("Total space MB: "));
  Serial.println(clusterSize*sd.vol()->clusterCount());
  Serial.print(F("Free Space MB:  "));
  Serial.println(clusterSize*sd.vol()->freeClusterCount());
}
void loop() {}


Prints this for a 32 GB card:
Quote
Card Size MB:   31914.98
Total space MB: 31902.40
Free Space MB:  31897.35
I use 1,000,000 bytes for MB, the same as disk and SD manufactures and the ISO Standard definition of MB.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy