diff --git a/master/master/config.h b/master/master/config.h --- a/master/master/config.h +++ b/master/master/config.h @@ -54,4 +54,14 @@ #define APRS_COMMENT "Trackuino reminder: replace callsign with your own" + +// -------------------------------------------------------------------------- +// Logger config (logger.c) +// -------------------------------------------------------------------------- + +#define LOGGER_ID_EEPROM_ADDR 0x10 + +// Written to the beginning of every log file +#define LOGGER_HEADERTEXT "HAB Control Master - 1.0\n" + #endif /* CONFIG_H_ */ \ No newline at end of file diff --git a/master/master/lib/logger.c b/master/master/lib/logger.c --- a/master/master/lib/logger.c +++ b/master/master/lib/logger.c @@ -8,12 +8,15 @@ #include #include #include +#include +#include #include "sd/fat.h" #include "sd/fat_config.h" #include "sd/partition.h" #include "sd/sd_raw.h" #include "sd/sd_raw_config.h" #include "logger.h" +#include "../config.h" /* //config edits @@ -24,142 +27,138 @@ * write support completely if you only need read support. */ + +struct partition_struct* partition; +struct fat_fs_struct* fs; +struct fat_dir_entry_struct directory; +struct fat_dir_struct* dd; +struct fat_file_struct* fd; + void logger_setup() { - if(!sd_raw_init()) //sd_raw.c this function may need an overhaul - { - // initialization failed!!!!!!! - PORTA |= 0b00000001; - return; - } + if(!sd_raw_init()) + { + // initialization failed + return; + } - //check for SD exist/power/ready END + // TODO: Check SD card switch to see if inserted. + // this was included in the library, but is commented out right now - /* open first partition */ - struct partition_struct* partition = partition_open(sd_raw_read, - sd_raw_read_interval, - sd_raw_write, - sd_raw_write_interval, - 0 - ); - //check that partition was created correctly + // Open first partition + partition = partition_open(sd_raw_read, sd_raw_read_interval, sd_raw_write, sd_raw_write_interval, 0); + + // Check that partition was created correctly + if(!partition) + { + // error + // If the partition did not open, assume the storage device is a "superfloppy", i.e. has no MBR. + partition = partition_open(sd_raw_read, sd_raw_read_interval, sd_raw_write, sd_raw_write_interval, -1); if(!partition) { - // OH GOSH TERRIBLE ERROR HERE - /* If the partition did not open, assume the storage device - * is a "superfloppy", i.e. has no MBR. - */ - partition = partition_open(sd_raw_read, - sd_raw_read_interval, - sd_raw_write, - sd_raw_write_interval, - -1 - ); - if(!partition) - { - // oh frick opening partition failed - PORTA |= 0b00000010; - return; - } - } - //open partition END - - - //open file system BEGIN - struct fat_fs_struct* fs = fat_open(partition); - if(!fs) - { - // opening fs failed! - PORTA |= 0b00000100; + // opening partition failed return; - } - //open file system END + } + } - //open directory BEGIN - /* open root directory */ - struct fat_dir_entry_struct directory; - fat_get_dir_entry_of_path(fs, "/", &directory); - - struct fat_dir_struct* dd = fat_open_dir(fs, &directory); - if(!dd) - { - // frick opening root dir failed - PORTA |= 0b00000101; - return; - } - //open directory END - - - //simplified version of console BEGIN - char buffer[24] = "Omg this is cool, haha!"; - buffer[23] = 0xd; - buffer[24] = 0xa; - - /* search file in current directory and open it */ - struct fat_file_struct* fd = open_file_in_dir(fs, dd, "data.csv"); //logger.h - if(!fd) - { - PORTA |= 0b00000110; - return; - } + // Open FAT filesystem + fs = fat_open(partition); + if(!fs) + { + // opening filesystem failed + return; + } + + // Open directory + fat_get_dir_entry_of_path(fs, "/", &directory); - int32_t offset = 0;//strtolong(offset_value); - if(!fat_seek_file(fd, &offset, FAT_SEEK_SET)) //seek to begin or end or what ?? - { - //error seek to file handling - PORTA |= 0b00000111; - fat_close_file(fd); - return; - } + dd = fat_open_dir(fs, &directory); + if(!dd) + { + // opening root directory failed + PORTA |= 0b00000101; + return; + } + + + // Create new log file + uint8_t logid = eeprom_read_byte(LOGGER_ID_EEPROM_ADDR); + char filename[48]; + // we pre-increment logid here because it starts at 255, then wraps to 0 + sprintf(filename, "data%d.csv",++logid); + // @TODO: Catch errors here + fat_create_file(dd, filename, &directory); + + eeprom_update_byte(LOGGER_ID_EEPROM_ADDR, logid); + + + /* search file in current directory and open it */ + fd = open_file_in_dir(fs, dd, filename); //logger.h + if(!fd) + { + PORTA |= 0b00000110; + return; + } + + int32_t offset = 0; //strtolong(offset_value); + if(!fat_seek_file(fd, &offset, FAT_SEEK_SET)) //seek to begin or end or what ?? + { + //error seek to file handling + PORTA |= 0b00000111; + fat_close_file(fd); + return; + } - /* read text from the shell and write it to the file */ - uint8_t data_len = sizeof(buffer); - - while(1) - { - /* write text to file !! */ - if(fat_write_file(fd, (uint8_t*) buffer, data_len) != data_len) - { - //uart_puts_p(PSTR("error writing to file\n")); - break; - } - } - - fat_close_file(fd); //may want to leave file open ?? - - - - //prepare for closing SD connection BEGIN - /* close directory */ - fat_close_dir(dd); //fat.c - - /* close file system */ - fat_close(fs); //fat.c - - /* close partition */ - partition_close(partition); //partition.c - //prepare for closing SD connection END + // Write header information + logger_log(LOGGER_HEADERTEXT); + logger_log("\n-- BEGIN DATA --\n"); + logger_log("C1, C2, C3, C4, C5, C6\n"); + } -//writes a single line to the SD card -uint8_t logger_writeLine(char* dateLine, uint8_t length) -{ - return length; //does not actually return length +void logger_log(char *buffer) { + uint8_t len = strlen(buffer); + if(fat_write_file(fd, (uint8_t*) buffer, len) != len) + { + //uart_puts_p(PSTR("error writing to file\n")); + return; + } } -//i think opens a file so it can be read/written +void logger_closeLog() { + + fat_close_file(fd); //may want to leave file open ?? + + /* close directory */ + fat_close_dir(dd); //fat.c + + /* close file system */ + fat_close(fs); //fat.c + + /* close partition */ + partition_close(partition); //partition.c +} + + + + + + +// INTERNAL FUNCTIONS + +// Opens a file so it can be read/written struct fat_file_struct* open_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name) { struct fat_dir_entry_struct file_entry; if(!find_file_in_dir(fs, dd, name, &file_entry)) return 0; - return fat_open_file(fs, &file_entry); //fat.h + return fat_open_file(fs, &file_entry); } -//i think searches for a file +// Searches for file in directory listing uint8_t find_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name, struct fat_dir_entry_struct* dir_entry) { while(fat_read_dir(dd, dir_entry)) @@ -170,6 +169,5 @@ uint8_t find_file_in_dir(struct fat_fs_s return 1; } } - return 0; } diff --git a/master/master/lib/logger.h b/master/master/lib/logger.h --- a/master/master/lib/logger.h +++ b/master/master/lib/logger.h @@ -14,4 +14,7 @@ uint8_t logger_writeLine(char* dateLine, struct fat_file_struct* open_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name); uint8_t find_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name, struct fat_dir_entry_struct* dir_entry); +void logger_log(char *buffer); +void logger_closeLog(); + #endif /* LOGGER_H_ */ \ No newline at end of file diff --git a/master/master/master.c b/master/master/master.c --- a/master/master/master.c +++ b/master/master/master.c @@ -34,29 +34,25 @@ int main(void) { // Initialize //micro_setup(); - //led_setup(); + led_setup(); //serial_setup(); // Config serial ports - //afsk_setup(); - - _delay_ms(400); - _delay_ms(400); - logger_setup(); // right now this blocks and writes text to the SD card - - - + afsk_setup(); + logger_setup(); // this takes a few ms + while(1) { + logger_log("123,43,1,2,4,3\n"); //afsk_test(); + + // Wait for transmission to complete before starting another. + // Hopefully this will never delay because it should issue on a timed schedule. Software timers! + while(afsk_busy()); aprs_send(); - - _delay_ms(400); - _delay_ms(400); - _delay_ms(400); - _delay_ms(400); - _delay_ms(400); + + //serial_SendCommand('0','A',0,0);