SD card write

29 Oct 2009 . Edited: 29 Oct 2009

I am enjoying the mbed system so far.  Thanks for all the previous ground work.

My project is to build an in-car data aquisition system.  My end goal is to interface with my stand-alone fuel injection system for a dash display (already working on AtMega16 board.  I moved to the mbed system due to the extra ports avialable.  I am adding SD card logging, accelerometer and GPS tracking.

I have the display and accelerometer working well with the mbed.  I just added the microSD card setup.  I can write to the card but I only get one line to write.  Any suggestions on file system to continually write?  I moved the file close command outside my while loop but I get a flag "statement unreachable"

Thanks.  Here is my main code.

#include "mbed.h"
#include "TextLCD.h"
#include "LIS302.h"
#include "SDFileSystem.h"

//LCD 2x16 parallel code pin assignment
TextLCD lcd(p21, p22, p23, p24, p25, p26, p30); // rs, rw, e, d0, d1, d2, d3

int column;
int row;
int i;

//Acclerometer - SparkFun LIS302 pin assignment
LIS302 acc (p5,p6,p7,p8);
Serial pc (USBTX,USBRX);

//SD Card pin assignment
SDFileSystem sd(p11, p12, p13, p10, "sd");//DI, D0, SCK, CS

int main() {

acc.calibrate(1.043, -0.935, 0.989, -1.061, 0.9274, -1.1584); //calibrate Accelerometer

FILE *fp = fopen("/sd/testfile.txt", "w");

while (1) {

lcd.locate(0,0);  //X direction
lcd.printf("X=");
lcd.printf("%.3f" ,acc.x());
lcd.locate(8,0);  //Y direction
lcd.printf("Y=");
lcd.printf("%.3f" ,acc.y());
lcd.locate(0,1);  //Z direction
lcd.printf("Z=");
lcd.printf("%.3f" ,acc.z());
//SD Card File access/write
printf("Hello World!\n");

wait(0.1);//accelerometer refresh rate
lcd.cls();

//write accel values to SD card
fprintf(fp, "%.3f %.3f %.3f\n",acc.x(),acc.y(),acc.z());
wait(0.1);
}

fclose(fp);
}

29 Oct 2009

Hi James,

James Novak wrote:
I just added the microSD card setup.  I can write to the card but I only get one line to write.  Any suggestions on file system to continually write?

If I understand you correctly, you are looking to be able to log lots of data, but still close a file each time, and not overwrite what you've already written. I think what you might be looking for is to open a file in "append" mode, meaning add to the end of the file rather than overwrite. This is done using "a" instead of "w" in the fopen command. For example:

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

SDFileSystem sd(p11, p12, p13, p10, "sd");

int main() {
    while(1) {
        FILE *fp = fopen("/sd/testfile.txt", "a");
        fprintf(fp, "hello world!\n");
        fclose(fp);
        wait(1);
    }
}

For details of the fopen modes, see:

Is this what you are after? Or does this give you a hint what might work?

btw, if you've got any photos, please post them as I'd love to see what you've built so far!

Simon

29 Oct 2009

Simon,  Thank you for the quick response.  This does give me a hint.  I tried the "a" command instead of the "w" command.  It did not work.  It still leaves only one line of data in the file.  Let me explain better.

This is the first item on the list. I can write data to the card.  However, the data file contains only 3 values corresponding to the X,Y,Z acclerometer outputs.  The file should contain a column of continuous values. I looked at other SD card code and there is usually a "next line" command at the end of the write sequence.  the logic would go something like...

open file (name XX)

write data

next line

write next data

(etc...)

close file.

I do believe that the append vs overwrite is important.

Secondly, I would also like the system to create a new file name upon each boot sequence.  I assume that that I can do some like the following.

 

for (i=0; i++)
         {
             wait(0.1);
             *fp = fopen("/sd/testfile(i).txt", "w");

 

I have not tried this yet.I am sure I have errors in my syntax but basically one could add an integer statement to the file open statement.

I will see if I can get some pictures up.  They won't be pretty and writing code is just a hobby.

29 Oct 2009

quick update:

I recompiled the code with an 'a' to append the data.  I let the program run for a minute.  I had three data value arrays.  I guess this means it is working but really SLOW.  I even took out the delay at the end of the code.

29 Oct 2009

I figured out why it is so slow.  It is only writing one data set each time the MCU resets.  I still do not have continuous datalogging.

30 Oct 2009

I managed to get a continuous write to the card.  Here is the code correction in the while loop.  I had to insert a comma between each formatted value to create a CSV format.  This data string imports nicely to Excel.

 

//write accel values to SD card
FILE *fp = fopen("/sd/testfile.txt", "a");
fprintf(fp, "%.3f, %.3f, %.3f, \n",acc.x(),acc.y(),acc.z());
fclose(fp);

}

Next step is to write this to a buffer.  I will have a lot more data coming into the buffer and know that timing for R/W will be too slow for my application.  I am aiming for 50Hz or faster.  I also want a timer since I will integrate with other data and need it to all be time correlated.

02 Jan 2010

James, if you are still working on this project could you post an update?  I am also curious as to what your "stand alone EFI" setup is?  I currently have 90s Mustang fuel injection setup on my 1977 Bronco, and am considering building a mega squirt for a 1952 Ford truck I am working on.

 

Pictures please!

 

Matthew