Problems with SDCard

24 Feb 2010
I am doing a project with mbed using the library of SDCard. 
What I am trying to do is to log the data into the SD card.
I have followed the instruction of SDCard library. 

Firstly, connect the mbed with the SD socket as the following instruction. 
MicroSD Breakout    mbed
   CS  o-------------o 13   (DigitalOut cs)
   DI  o-------------o 5    (SPI mosi)
   VCC o-------------o VOUT
   SCK o-------------o 7    (SPI sclk)
   GND o-------------o GND  
   DO  o-------------o 6    (SPI miso)
   CD  o

Secondly, the cards were formatted using:

  • Windows 7, Right-click, Format...
  • Capacity: 59.4MB
  • Filesystem: FAT (Default) [FAT, FAT32, NTFS, exFAT]
  • Allocation size: 1024 bytes

Thirdly, import the FatFileSystem library and SDFileSystem code.

Lastly, change my code as follows:


#include "mbed.h"
#include "SDFileSystem.h"

AnalogIn xin(p20);
AnalogIn yin(p19);

SDFileSystem sd(p5, p6, p7, p13, "sd");

int main() {
    float xval;
    float yval;
    int loop;
    
    FILE *fp = fopen("/sd/foo.txt", "w");
    if(fp == NULL) {
        error("Could not open file for write\n");
    }
    
    while(1){
        loop=loop+1;
        xval=xin.read();
        yval=yin.read();
        fprintf(fp, "x: %f, y: %f\r\n", xval, yval);
        if (loop>=10000) break;
    }        
    fclose(fp);
}
However, there is no any files when I plug the disk back in to my PC.

Could anyone help me to figure this out?

24 Feb 2010

I think FatFileSystem currently supports only FAT32, not FAT.

24 Feb 2010

Igor Skochinsky wrote:

I think FatFileSystem currently supports only FAT32, not FAT.

Sorry, the SD card was formatted using:

  • Windows 7, Right-click, Format...
  • Capacity: 59.4MB
  • Filesystem: FAT (Default) [FAT, NTFS, exFAT]
  • Allocation size: 1024 bytes

There is no FAT32 actually.

24 Feb 2010

Try from commandline:

format S: /FS:FAT32 /Q

I think you can also ask the filesystem class to format the card (try sd.format(); ).

24 Feb 2010

Igor Skochinsky wrote:

Try from commandline:

format S: /FS:FAT32 /Q

I think you can also ask the filesystem class to format the card (try sd.format(); ).

Sorry, I dont understand what your meaning is. Could you explicate your point?

24 Feb 2010

1) open a command prompt window (e.g. Start menu - type 'cmd', Enter)

2) type 'format S: /FS:FAT32 /Q' (replace S: by the drive letter of your SD card), press Enter.

24 Feb 2010

Which SD card are you using?

24 Feb 2010

Igor Skochinsky wrote:

1) open a command prompt window (e.g. Start menu - type 'cmd', Enter)

2) type 'format S: /FS:FAT32 /Q' (replace S: by the drive letter of your SD card), press Enter.

Thx, I have done what you said, but it is still not working. There is no files existing yet.

24 Feb 2010

XueTao Yuan wrote:

 

Igor Skochinsky wrote:

1) open a command prompt window (e.g. Start menu - type 'cmd', Enter)

2) type 'format S: /FS:FAT32 /Q' (replace S: by the drive letter of your SD card), press Enter.

Thx, I have done what you said, but it is still not working. There is no files existing yet.
And it dislpays after formating that:

55.5MB total dis space.

55.5MB are available.

512 bytes in each allocation unit.

113,624 allocation units available on disk.

32 bits in each FAT entry.

Volume Serial Number is 5CF8-3693

 

01 Mar 2011

Odd. SDFileSystem has been working fine on my 2G uSD card formatted with FAT (FAT16). Well, it was working fine up until yesterday. It's no longer creating log files reliably. Not sure what's going on.

23 Jan 2014

Really late to the party, but fclose(fp); is never called as the mbed is stuck in the while(1) loop. You would probably need to have some end condition on the loop, or alternatively keep on opening and closing the file in the while(1) loop. Using fflush(fp) might work, but I remember reading somewhere that it didn't work...