9 years, 5 months ago.

SDFile system working in standalone but not with other code

Hello,

I am trying to log data to an SD card but seem to be having some issues. I have done some basic testing using just the SD Card (writing simple text to a file on the card) and have found no issues in getting the SD card to work. My issue is now incorporating that into a bigger project, as can be seen in the code below.

SD Card integrated code

int main() {
    int initilised = 0;
    int index = 0;
    int fxos_data_flag_1 = 0;
    int fxos_data_flag_2 = 0;
    int fxas_data_flag_1 = 0;
    int fxas_data_flag_2 = 0;
    
    //Configure interrupts
    fxas_int1_21002.rise(&read_fxas_data);//FIFO -> Int_1
//    fxas_int2_21002.rise(&read_fxas_data);//DRDY -> Int_2
    fxos_int1_8700.rise(&read_fxos_data);//FIFO -> Int_1
//    fxos_int2_8700.rise(&read_fxos_data);//DRDY -> Int_2
    
    sensors.frequency(400000);
    pc.baud(115200);
    
    
    
    mkdir("/sd/sensor", 0777);
    FILE *sd = fopen("/sd/sensor_data/data.txt", "W");
    if (sd == NULL){
        led_1.write(0);
//        while(true);
    }
    fprintf(sd, "Hello fun SD Card World!\n\r\n\r");
    fprintf(sd, "Hello fun SD Card World!\n\r\n\r");
    fprintf(sd, "Thank you for trying this thing out\n\r\n\r");
    fprintf(sd, "Have a good day\n\r");
    fprintf(sd, "Have a good day Round two\n\r");
    fclose(sd); 


I have checked my set up and everything else and as much as possible it represents this code

Wording SD Card code

#include "mbed.h"
#include "SDFileSystem.h"
 
SDFileSystem sd(PTD2, PTD3, PTD1, PTD0, "sd"); // the pinout on the mbed Cool Components workshop board
 
int main() {
    printf("Hello World!\n");   
 
    mkdir("/sd/mydir", 0777);
    
    FILE *fp = fopen("/sd/mydir/sdtest.txt", "w");
    if(fp == NULL) {
        error("Could not open file for write\n");
    }
    fprintf(fp, "Hello fun SD Card World!\n\r\n\r");
    fprintf(fp, "Hello fun SD Card World!\n\r\n\r");
    fprintf(fp, "Thank you for trying this thing out\n\r\n\r");
    fprintf(fp, "Have a good day\n\r");
    fprintf(fp, "Have a good day Round two\n\r");
    fclose(fp); 
 
    printf("Goodbye World!\n\n\r");
}

As this appears to be my last hurdle to completing my project any help would be very much appreciated.

Thankfully Kas

Sorry I seem to have missed explaining the whole point of my question (lack of sleep here). When I incorporated the SDFileSystem code into my bigger project the SD code did not work, there was no data on the SD card and on setup the code would continuously go into the error catching code. After spending a bit more time on this I was able to find where the issues lay but not 100% why the second issue identified causes the outcomes it does.

So what is your problem?

posted by Erik - 22 Jul 2015

After a bit of work (Its hard when there is no debugger and you are so used to using it) I have found there are two culprits for the SDFileSystem not working in my code. The first one makes sense to me and that is the use of LED3, when Freescale setup this board LED3 was connected to PTD1 or better known as SPI CLK... set LED3 as a DigitalOut and no clock so no SD card (Freescale please use a less important pin next time like pin that is not actually broken out...)

The second issue and the one that makes no sense to me is the use of

Setup UART to PC Serial pc(USBTX, USBRX);

pc.baud(115200); <- this line here is the issue

by simply commenting out that one line the issue was resolved but from my understanding this line has nothing to do with SPI and it should not interfere with code after t as everything is sequential and this should only affect the clock and therefore should not be a blocking issue.

posted by Kasriel Lewis 22 Jul 2015

Restructuring the code resolved the second issue. Changing the baud rate AFTER writing to the SD card (or maybe placing a wait(0.2); resolved any issues.

After looking into it more it may have only been affecting the printf("xxxx"); statements and not the writs to the SD card. Either way this is one to watch out for so as to not block yourself when trying to use the SD card as well as the UART at a higher baud rate.

Hopefully now I can finish up my project... hers to hoping.

posted by Kasriel Lewis 22 Jul 2015

The answer to the second question regarding baud rate is that the terminal needs to be set to the new baud rate as well. While this may sound obvious I was expecting at least garbled rubbish and not no output at all from putty. It seems that somehow putty knows when teh baud rate is wrong and then will ignore any data coming to it.

posted by Kasriel Lewis 22 Jul 2015

It is pretty random what happens with a wrong baudrate. Sometimes it is garble, and sometimes it just is nothing at all.

And yeah the LED being shared with SPI clock pin is not a handy one indeed.

posted by Erik - 22 Jul 2015

1 Answer

9 years, 5 months ago.

    mkdir("/sd/sensor", 0777);
    FILE *sd = fopen("/sd/sensor_data/data.txt", "W");

should probably be

    mkdir("/sd/sensor_data", 0777);   // name changed to match where you try to put the log file
    FILE *sd = fopen("/sd/sensor_data/data.txt", "w"); // file mode string is normally lower case (no idea if this is critical or not)

Accepted Answer

Hello Andy,

That was also an issue that was fixed as you mentioned, that along with my above comments allowed this code to work.

Thank you :)

posted by Kasriel Lewis 24 Jul 2015