/* * Master Firmware: SD Card Data Logger * * Wireless Observational Modular Aerial Network * * Ethan Zonca * Matthew Kanning * Kyle Ripperger * Matthew Kroening * */ #include "../config.h" #include #include #include #include #include #include #include #include "sdcard/fat.h" #include "sdcard/fat_config.h" #include "sdcard/partition.h" #include "sdcard/sd_raw.h" #include "sdcard/sd_raw_config.h" #include "serial.h" #include "logger.h" #include "led.h" 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()) { // Initializing SD card failed! #ifdef DEBUG_OUTPUT serial0_sendString("SD> Error initializing.\r\n"); #endif led_errorcode(ERROR_SD_INIT); return; } // TODO: Check SD card switch to see if inserted. // this was included in the library, but is commented out right now // 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) { #ifdef DEBUG_OUTPUT serial0_sendString("SD> Error opening partition.\r\n"); #endif // Error opening partition. MBR might be screwed up. led_errorcode(ERROR_SD_PARTITION); return; } // Open FAT filesystem fs = fat_open(partition); if(!fs) { // opening filesystem failed led_errorcode(ERROR_SD_PARTITION); return; } // Open directory fat_get_dir_entry_of_path(fs, "/", &directory); dd = fat_open_dir(fs, &directory); if(!dd) { // opening root directory failed _delay_ms(10); led_errorcode(ERROR_SD_FILE); 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 if(fat_create_file(dd, filename, &directory) == 0) { led_errorcode(ERROR_SD_FILE); } eeprom_update_byte(LOGGER_ID_EEPROM_ADDR, logid); // Search for file in current directory and open it fd = open_file_in_dir(fs, dd, filename); if(!fd) { led_errorcode(ERROR_SD_FILE); _delay_ms(10); return; } // Seek to beginning of file // TODO: Is this needed? int32_t offset = 0; if(!fat_seek_file(fd, &offset, FAT_SEEK_SET)) { // Error seeking to file led_errorcode(ERROR_SD_FILE); _delay_ms(10); fat_close_file(fd); return; } // Write header information logger_log(LOGGER_HEADERTEXT); logger_log("\n-- BEGIN DATA --\n"); } void logger_log(char *buffer) { uint8_t len = strlen(buffer); if(fat_write_file(fd, (uint8_t*) buffer, len) != len) { // Error writing to file return; } } void logger_closeLog() { fat_close_file(fd); fat_close_dir(dd); fat_close(fs); partition_close(partition); } // 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); } // 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)) { if(strcmp(dir_entry->long_name, name) == 0) { fat_reset_dir(dd); return 1; } } return 0; }