X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=rmslog%2Fmain.c;h=95d988ce0016d8e8a5bd0570aa644f2427584be9;hb=2d09396ef3609269e9a42b40a84d53ebca434646;hp=d4bb6a0a5641796d2cb6fb0e14278ac58d53dd64;hpb=953c42c0ea7e1c27793ba465db862e2fbc7ffdf9;p=rmslog.git diff --git a/rmslog/main.c b/rmslog/main.c index d4bb6a0..95d988c 100644 --- a/rmslog/main.c +++ b/rmslog/main.c @@ -1,17 +1,16 @@ -/** - * \file - * - * \author Bernie Innocenti - * - * \brief Serial data logger for RMS +/* + * Copyleft 2011 Bernie Innocenti + * Serial data logger for RMS * * This application records all incoming data from an RS-232 serial port * to a file on a FAT-formatted SD card. It can be used to log kernel * console messages. */ +// Configuration #define FILE_NAME "log.txt" -#define FILE_OVERWRITE 1 +#define FILE_NAME_PATTERN "log%04d.txt" +#define FILE_OVERWRITE 0 #define FILE_PREALLOC_SIZE 102400 @@ -24,39 +23,29 @@ #include #include -#ifdef BERTOS_FAT - #include - #include -#else - #include - #include - #include -#endif +#include +#include +#include #include #include #include -#include +#include // sprintf() +#include // strcmp() +// Our serial port static Serial ser; -#ifdef BERTOS_FAT - static Serial spi; - static FATFS fs; - static FatFile file; - static Sd sd; -#else - struct fat_fs_struct *fs; - struct partition_struct *partition; - struct fat_dir_struct *dd; - struct fat_dir_entry_struct dir_entry; - struct fat_file_struct *fd; -#endif - - -#ifndef BERTOS_FAT +// FAT global state +struct fat_fs_struct *fs; +struct partition_struct *partition; +struct fat_dir_struct *dd; +struct fat_dir_entry_struct dir_entry; +struct fat_file_struct *fd; + + static void fat_init(void) { partition = partition_open( @@ -89,7 +78,7 @@ static void fat_init(void) ASSERT(0); for(;;) {} } - //Open root directory + // Open root directory fat_get_dir_entry_of_path(fs, "/", &dir_entry); if (!(dd = fat_open_dir(fs, &dir_entry))) { @@ -121,7 +110,6 @@ static struct fat_file_struct *open_file_in_dir(struct fat_fs_struct* fs, struct return fat_open_file(fs, &file_entry); } -#endif // !BERTOS_FAT static void init(void) { @@ -149,18 +137,20 @@ static void init(void) // input stream to the SD card //ser_setbaudrate(&ser, 19200); - /* Initialize LED driver */ + // Initialize LED driver LED_INIT(); + /* + * This gives avrdude some time to start reflashing in case + * the bug caused the program to do continous serial output + */ timer_delay(1000); - kfile_printf(&ser.fd, "Hello, world!\r\n"); DDRB |= BV(PORTB0) | BV(PORTB2) | BV(PORTB3) | BV(PORTB5); if (!sd_raw_init()) { ASSERT(0); for(;;) {} } -#endif fat_init(); } @@ -171,24 +161,39 @@ int main(void) init(); size_t size; - char buf[256]; bool sync_pending = false; + char buf[256]; - /* create file */ - if(!fat_create_file(dd, FILE_NAME, &dir_entry)) + #if FILE_OVERWRITE { - #if FILE_OVERWRITE - fat_delete_file(fs, &dir_entry); - fat_create_file(dd, FILE_NAME, &dir_entry); - #endif + if(!fat_create_file(dd, FILE_NAME, &dir_entry)) + { + fat_delete_file(fs, &dir_entry); + fat_create_file(dd, FILE_NAME, &dir_entry); + } + + fd = open_file_in_dir(fs, dd, FILE_NAME); } + #else /* Generate a new filename */ + { + int n; + char filename[8+1+3+1]; - fd = open_file_in_dir(fs, dd, FILE_NAME); + for (n = 0; /**/; ++n) + { + sprintf(filename, FILE_NAME_PATTERN, n); + if (fat_create_file(dd, filename, &dir_entry)) + break; + } + + fd = open_file_in_dir(fs, dd, filename); + } + #endif - /* preallocate file */ #if FILE_PREALLOC_SIZE { LED_ON(); + memset(buf, ' ', sizeof(buf)); for (int i = 0; i < (int)(FILE_PREALLOC_SIZE / sizeof(buf)); ++i) fat_write_file(fd, (void *)buf, sizeof(buf)); @@ -198,7 +203,7 @@ int main(void) } #endif - while (1) + for (;;) { if ((size = ser_read(&ser, buf, sizeof(buf))) != 0) {