5 years, 10 months ago.

Mbed FatFileSystem and SDBlock Device unexpected behavior and errors

Hello everyone, I am currently working on WIZnet WIZwiki-7500p, and in my project I need to write some files on a SD Card. The code behaves unexpectedly, as some times it works and some others it fails. Below is my code:

main.cpp

#include "mbed.h"
#include "SDFileSystem.h"
#include <stdio.h>

FATFileSystem fs("sd");

SDBlockDevice bd(
    MBED_CONF_SD_SPI_MOSI,
    MBED_CONF_SD_SPI_MISO,
    MBED_CONF_SD_SPI_CLK,
    MBED_CONF_SD_SPI_CS
);

SDFileSystem sd(fs,bd);
InterruptIn irq(PC_6);

void onInterrupt() {
    sd.erase();
}   

int main() {
    printf(" Mbed OS filesystem example \n");
    irq.fall(mbed_event_queue()->event(onInterrupt));
    sd.mount();
    for ( unsigned long long int tag = 1547517475; tag < 1547517477 ; tag++  ) {
        char out_string [12];
        sprintf(out_string, "%llu", tag);
        sd.setTimeStampt(out_string);
        sd.displayFile("/sd/tagsTimeStampt.txt");
    }
    sd.displayFile("/sd/numbers.txt");

    printf("Mbed OS filesystem example done!\n");
}

SDFileSystem.cpp

#ifndef SDFILESYSTEM_H
#define SDFILESYSTEM_H

#include "mbed.h"
#include <stdio.h>
#include <errno.h>
#include "FATFileSystem.h"
#include "SDBlockDevice.h"

class SDFileSystem {
private:
    FATFileSystem& fs;
    SDBlockDevice& bd;
    FILE* f;
    int err;
public:
    SDFileSystem(FATFileSystem& fs , SDBlockDevice& bd);
    void setTimeStampt( char* buffer );
    void erase();
    void mount();
    void displayFile(char* path);
};

#endif

SDFileSystem.h

#include "SDFileSystem.h"

SDFileSystem::SDFileSystem(FATFileSystem& fs , SDBlockDevice& bd): fs(fs) , bd(bd){
}

void SDFileSystem::mount(){
    printf("Mounting the filesystem... ");
    fflush(stdout);
    int err = fs.mount(&bd);
    printf("%s\n", (err ? "Fail :(" : "OK"));
    if (err) {
        printf("No filesystem found, formatting... ");
        fflush(stdout);
        err = fs.reformat(&bd);
        printf("%s\n", (err ? "Fail :(" : "OK"));
        if (err) {
            error("error: %s (%d)\n", strerror(-err), err);
        }
    }
}

void SDFileSystem::erase() {
    printf("Initializing the block device... ");
    fflush(stdout);
    int err = bd.init();
    printf("%s\n", (err ? "Fail :(" : "OK"));
    if (err) {
        error("error: %s (%d)\n", strerror(-err), err);
    }
    printf("Erasing the block device... ");
    fflush(stdout);
    err = bd.erase(0, bd.size());
    printf("%s\n", (err ? "Fail :(" : "OK"));
    if (err) {
        error("error: %s (%d)\n", strerror(-err), err);
    }
    printf("Deinitializing the block device... ");
    fflush(stdout);
    err = bd.deinit();
    printf("%s\n", (err ? "Fail :(" : "OK"));
    if (err) {
        error("error: %s (%d)\n", strerror(-err), err);
    }
}

void SDFileSystem::displayFile( char* path){
    printf("\r\nOpening \"%s\"... ", path);
    fflush(stdout);
    fflush(f);
    f = fopen(path, "r");
    printf("%s\n", (!f ? "Fail :(" : "OK"));
    if (!f) {
        error("error: %s (%d)\n", strerror(errno), -errno);
    }
    printf("data:\n");
    while (!feof(f)) {
        int c = fgetc(f);
        putc(c, stdout);
    }
    fflush(f);
    printf("\nClosing \"%s\"... ",path);
    err = fclose(f);
    printf("%s, err: %d\n", (err < 0 ? "Fail :(" : "OK"), err);
}

void SDFileSystem::setTimeStampt( char* timestamp ){
    printf("\r\nOpening \"/sd/tagsTimeStampt.txt\"... ");
    f = fopen("/sd/tagsTimeStampt.txt", "w");
    printf("%s\n", (!f ? "Fail :(" : "OK"));
    printf("\rWriting timestampt: %s... ", timestamp);
    err = fprintf(f, "timestamp: %s\n",timestamp);
    fflush(f);
    printf("%s, err: %d\n", (err != 0 ? "Fail :(" : "OK"), err);
    if (err < 0) {
        printf("Fail :(\n");
        error("error: %s (%d)\n", strerror(errno), -errno);
    }
    fflush(stdout);
    printf("Closing \"/sd/tagsTimeStampt.txt\"... ");
    err = fclose(f);
    printf("%s, err: %d\n", (err < 0 ? "Fail :(" : "OK"), err);
}

Results

Success

 Mbed OS filesystem example 
Mounting the filesystem... OK

Opening "/sd/tagsTimeStampt.txt"... OK
Writing timestampt: 1547517475... Fail :(, err: 22
Closing "/sd/tagsTimeStampt.txt"... OK, err: 0

Opening "/sd/tagsTimeStampt.txt"... OK
data:
timestamp: 1547517475
�
Closing "/sd/tagsTimeStampt.txt"... OK, err: 0

Opening "/sd/tagsTimeStampt.txt"... OK
Writing timestampt: 1547517476... Fail :(, err: 22
Closing "/sd/tagsTimeStampt.txt"... OK, err: 0

Opening "/sd/tagsTimeStampt.txt"... OK
data:
timestamp: 1547517476
�
Closing "/sd/tagsTimeStampt.txt"... OK, err: 0

Opening "/sd/numbers.txt"... OK
data:
    120
    121
    122
    123
    124
    125
    126
    127
    128
�
Closing "/sd/numbers.txt"... OK, err: 0
Mbed OS filesystem example done!

Fail

 Mbed OS filesystem example 
Mounting the filesystem... OK

Opening "/sd/tagsTimeStampt.txt"... Fail :(
Writing timestampt: 1547517475... Fail :(, err: 22
Closing "/sd/tagsTimeStampt.txt"... OK, err: 0

Opening "/sd/tagsTimeStampt.txt"... OK
data:
�
Closing "/sd/tagsTimeStampt.txt"... OK, err: 0

Opening "/sd/tagsTimeStampt.txt"... OK
Writing timestampt: 1547517476... Fail :(, err: 22
Closing "/sd/tagsTimeStampt.txt"... Fail :(, err: -1

Opening "/sd/tagsTimeStampt.txt"... OK
data:
�
Closing "/sd/tagsTimeStampt.txt"... OK, err: 0

Opening "/sd/numbers.txt"... OK
data:
����������������������������������������������������������������������������������������������������� .... (it continues to print this indefinitely....)

Do you know what am I missing to prevent this errors?

Be the first to answer this question.