SparkFun Forums 

Where electronics enthusiasts find answers.

Everything ARM and LPC
By ozzyace
#74520
Hi all,
I am writing data to a file on my SD card successfully. Unfortunately, When I turn off the Logomatic V2 and re-start it, the new data over-writes the old data. I would like to be able to append the data to the existing file.
I am sure it must be my programming skills (which are beginner level)
Maybe I need to use file16_file_write differently, or another command.
Any ideas? Thank You
By gussy
#74524
Without seeing your code I can't say exactly, but it should be something pretty easy to fix.

When you turn it on, there will be some sort of initialization of the file? Have a look at that, maybe even try removing it.

Your code is already doing an append when its running, each time it logs data, so see how it's doing that and maybe convert your initialization code to that append code.

Your not using the stock logomatic firmware are you?
By ozzyace
#74526
Hi Gussy,
Thanks for your reply!
I tried to insert some code to show you, but I got 'you have entered a forbidden word' warning?!
I'll try again soon. Anyway, the initialization routine you mentioned - could it be here after Boot up()?
  • int main (void)
    {
    CCR &= ~(1<<0); //Disable the RTC
    CCR |= (1<<1); //Reset the RTC
    YEAR=2009;
    MONTH=6;
    DOM=9;
    DOW=0;
    DOY=0;
    HOUR=13;
    MIN=43;
    SEC=0;
    CCR &= ~(1<<1); //Enable the RTC
    CCR |= (1<<0); //Enable the Clock

    boot_up(); //Init LPC, IO pins, and FAT


    //log_string("Log started\r\n");

    set_pin_mode(1, "DI");

    char str_data[128];
    string_printf(str_data, "%02d/%02d/%04d,%02d:%02d:%02d Data Goes Here\r\n", DOM, MONTH, YEAR, HOUR, MIN, SEC);
    log_string(str_data);

    write_digital_pin(STAT1, ON); //Turn on LED
    delay_ms(50);
    write_digital_pin(STAT1, OFF); //Turn off LED
    delay_ms(250);
    delay_ms(700);
    fat16_close_file(handle);
    }

    void log_string(char *buf)
    {
    int stringSize = strlen(buf);
    fat16_write_file(handle, (unsigned char*)buf, stringSize);
    sd_raw_sync();
    }

    //Basic file init and stuff
    void boot_up(void)
    {
    set_pin_mode(STAT0, "DO"); //Set status LEDs as outputs
    set_pin_mode(STAT1, "DO");

    rprintf_devopen(putc_serial0); //Open up serial port 0 for debugging

    card_init(); //Init SD card interface

    //Creat the main log file
    char name[32];
    string_printf(name,"PKDATA.txt");
    handle = root_open(name);
    sd_raw_sync();

    //Setup TIMER1 to run
    T1TCR = 0; //Reset Timer1
    T1PR = 60000; //Set prescalar to 60,000 - every tick is 1ms
    T1TCR = 1; //Start Timer1

    flash_it(50); //Flash Status Lights
    }
Thanks again
By gussy
#74533
This the the code you need to focus on here:
Code: Select all
//Creat the main log file
char name[32];
string_printf(name,"PKDATA.txt");
handle = root_open(name);
sd_raw_sync();
Specifically the root_open() function, you should find it in the rootdir.c file.

The function root_open should look like this:
Code: Select all
struct fat16_file_struct * root_open(char* name)
{
    return(open_file_in_dir(fs,dd,name));
}
By Iquaba
#74570
I've been meaning to release this for a while and it should be just what you're looking for. It's available here.

If you do use it, please get back to me with whether or not it worked correctly. Thanks!
By ozzyace
#74615
:D Thank you gussy and Iquaba. You were both correct.
gussy - the 'open_root' function now looks like this:
Code: Select all
	string_printf(name, "MainLog.txt");
		if(!root_file_exists(name)){
			handle = root_open_new(name);
		}
		else{
			handle = root_open_append(name);
		}
Iquaba - You will recognize the code from your timely work!

Thank you both again!