Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
6 years ago.
How to get the Fat filesystem working again after removal and re-insertion of the SD card in OS5
In my project I like to log on the SD card. This works nice till the card is removed. After re-inserting the card I can't get access to the card anymore. I made a small sample program to show the problem:
SD Remove-Insert test program
// Tested boards: Nucleo-F411RE, Nucleo-L152RE
// Include --------------------------------------------------------------------
#include "mbed.h"
#include "SDBlockDevice.h"
#include "FATFileSystem.h"
#include <stdlib.h>
// Definition -----------------------------------------------------------------
#define USER_SW_ON 0
// Constructor ----------------------------------------------------------------
Serial pc(USBTX, USBRX, 115200);
DigitalIn user_sw(USER_BUTTON);
SDBlockDevice sd(PB_5, PB_4, PB_3, PC_5, 8000000); // SD card over SPI PB 4/5/3 and PC5
FATFileSystem fs("fs");
//------------------------------------------------------------------------------
// Control Program
//------------------------------------------------------------------------------
int main()
{
int linecount = 0;
FILE* fp;
while(1){
pc.printf("\n Program start \n");
pc.printf("Init: %d\n",sd.init());
pc.printf("mount: %d\n",fs.mount(&sd));
fp = fopen("fs/TestFile", "a");
pc.printf("filepointer: %d\n",fp);
if (fp != 0) {
fprintf(fp, "Line %d\n", linecount);
pc.printf("Line %d added\n", linecount);
fclose(fp);
}
pc.printf("unmount: %d\n",fs.unmount());
pc.printf("deinit: %d\n",sd.deinit());
linecount++;
while (user_sw == USER_SW_ON){wait(0.05);};
while (user_sw != USER_SW_ON){wait(0.05);};
}
}
As long as the SD card is inserted the program works nice and inserts a line after every press on the user button. When I remove and insert the SD card, the next time I press the button, fs.mount(&sd) returns -5 and the file can not be opened anymore.
I tried several variations wit mount/unmount and init/deinit but without success
I guess I'm overlooking something, but can't figure out wat it is. I'am new to C++ so it'may be something simple.
1 Answer
6 years ago.
Hello Gerrit,
I haven't tested it but the following might work:
// Tested boards: Nucleo-F411RE, Nucleo-L152RE
// Include --------------------------------------------------------------------
#include "mbed.h"
#include "SDBlockDevice.h"
#include "FATFileSystem.h"
#include <stdlib.h>
// Definition -----------------------------------------------------------------
#define USER_SW_ON 0
// Constructor ----------------------------------------------------------------
Serial pc(USBTX, USBRX, 115200);
DigitalIn user_sw(USER_BUTTON);
SDBlockDevice* sd;
FATFileSystem fs("fs");
//------------------------------------------------------------------------------
// Control Program
//------------------------------------------------------------------------------
int main()
{
int linecount = 0;
FILE* fp;
while(1){
pc.printf("\n Program start \n");
pc.printf("constuct:\n");
sd = new SDBlockDevice(PB_5, PB_4, PB_3, PC_5, 8000000); // SD card over SPI PB 4/5/3 and PC5;
pc.printf("init: %d\n",sd->init());
pc.printf("mount: %d\n",fs.mount(sd));
fp = fopen("fs/TestFile", "a");
pc.printf("filepointer: %d\n",fp);
if (fp != 0) {
fprintf(fp, "Line %d\n", linecount);
pc.printf("Line %d added\n", linecount);
fclose(fp);
}
pc.printf("unmount: %d\n",fs.unmount());
pc.printf("deinit: %d\n",sd->deinit());
pc.printf("destruct:\n);
delete sd;
linecount++;
while (user_sw == USER_SW_ON){wait(0.05);};
while (user_sw != USER_SW_ON){wait(0.05);};
}
}