Files
@ 6ab6b1fe2314
Branch filter:
Location: seniordesign-firmware/master/master/lib/logger.c
6ab6b1fe2314
6.6 KiB
text/plain
Added InfoText logger for generic informational messages, GPS fix light now reports 3d location fixes, log is made when fix is acquired/lost. Added maximum retransmission limit.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | /*
* Master Firmware: SD Card Data Logger
*
* Wireless Observational Modular Aerial Network
*
* Ethan Zonca
* Matthew Kanning
* Kyle Ripperger
* Matthew Kroening
*
*/
#include "../config.h"
#include <util/delay.h>
#include <string.h>
#include <stdio.h>
#include <avr/pgmspace.h>
#include <avr/sleep.h>
#include <avr/eeprom.h>
#include <string.h>
#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"
#include "looptime.h"
#define MAX_ERRNO 8
// Label lookup table
// Make sure there are never more labels than there are MAX_NUM_SENSORS!
const char err_0[] PROGMEM = "slave timeout";
const char err_1[] PROGMEM = "initializing SD card failed";
const char err_2[] PROGMEM = "opening SD partition failed";
const char err_3[] PROGMEM = "opening SD file failed";
const char err_4[] PROGMEM = "XBee timeout";
const char err_5[] PROGMEM = "FATAL UNHANDLED ERROR";
const char err_6[] PROGMEM = "enter AT mode failed";
const char err_7[] PROGMEM = "exit AT mode failed";
const char err_8[] PROGMEM = "infotext";
const char *const errorMessageLookup[] PROGMEM =
{
err_0,
err_1,
err_2,
err_3,
err_4,
err_5,
err_6,
err_7,
err_8,
};
struct partition_struct* partition;
struct fat_fs_struct* fs;
struct fat_dir_struct* dd;
struct fat_file_struct* fd_datalog;
struct fat_file_struct* fd_errorlog;
void logger_setup()
{
if(!sd_raw_init())
{
error_log(ERROR_SD_INIT, true);
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)
{
// Error opening partition. MBR might be screwed up.
error_log(ERROR_SD_PARTITION, true);
return;
}
// Open FAT filesystem
fs = fat_open(partition);
if(!fs)
{
// opening filesystem failed
error_log(ERROR_SD_PARTITION, true);
return;
}
// Open root directory
struct fat_dir_entry_struct rootDirEntry;
fat_get_dir_entry_of_path(fs, "/", &rootDirEntry);
dd = fat_open_dir(fs, &rootDirEntry);
if(!dd)
{
// opening root directory failed
_delay_ms(10);
error_log(ERROR_SD_FILE, true);
return;
}
// we pre-increment logid here because it starts at 255, then wraps to 0
uint8_t logid = eeprom_read_byte(LOGGER_ID_EEPROM_ADDR) + 1;
eeprom_update_byte(LOGGER_ID_EEPROM_ADDR, logid);
int32_t errorOffset = 0;
char errorFilename[17];
// Form filename
snprintf(errorFilename, 17, "run%derror.csv",logid);
struct fat_dir_entry_struct errorDirEntry;
if(fat_create_file(dd, errorFilename, &errorDirEntry) == 0)
{
serial0_sendString("Error create errorlog\r\n");
error_log(ERROR_SD_FILE, true);
}
// Search for file in current directory and open it
fd_errorlog = open_file_in_dir(fs, dd, errorFilename);
if(!fd_errorlog)
{
serial0_sendString("Error open errorlog!\r\n");
error_log(ERROR_SD_FILE, true);
return;
}
errorOffset=0;
if(!fat_seek_file(fd_errorlog, &errorOffset, FAT_SEEK_SET))
{
// Error seeking to file
serial0_sendString("Error seek errorlog!\r\n");
error_log(ERROR_SD_FILE, true);
fat_close_file(fd_errorlog);
return;
}
int32_t dataOffset = 0;
char dataFilename[17];
// Form filename
snprintf(dataFilename, 17, "run%ddata.csv",logid);
struct fat_dir_entry_struct dataDirEntry;
// Create new data log file
if(fat_create_file(dd, dataFilename, &dataDirEntry) == 0)
{
serial0_sendString("Error create datalog\r\n");
error_log(ERROR_SD_FILE, true);
}
// Search for file in current directory and open it
fd_datalog = open_file_in_dir(fs, dd, dataFilename);
if(!fd_datalog)
{
serial0_sendString("Error open datalog!\r\n");
error_log(ERROR_SD_FILE, true);
return;
}
dataOffset=0;
if(!fat_seek_file(fd_datalog, &dataOffset, FAT_SEEK_SET))
{
// Error seeking to file
serial0_sendString("Error seek datalog!\r\n");
error_log(ERROR_SD_FILE, true);
fat_close_file(fd_datalog);
return;
}
// Write header information
logger_log(LOGGER_HEADERTEXT);
logger_log("\n-- BEGIN DATA --\n");
error_log_rawwrite(LOGGER_HEADERTEXT);
error_log_rawwrite("\n-- BEGIN ERROR --\n");
error_log_rawwrite("\nErrorNo,ErrorMsg,ErrorInfo,\r\n");
}
void logger_log(char *buffer)
{
uint8_t len = strlen(buffer);
if(fat_write_file(fd_datalog, (uint8_t*) buffer, len) != len)
{
// Error writing to file
return;
}
}
void error_log(uint8_t errNo, bool flashLED)
{
char labelBuffer[32];
labelBuffer[0] = 0x00;
if(errNo <= MAX_ERRNO)
{
strncpy_P(labelBuffer,(char*)pgm_read_word(&(errorMessageLookup[errNo])),32);
}
char errorLine[128];
snprintf(errorLine, 128, "%lu, %u, %s,,\r\n", time_millis(), errNo, labelBuffer);
error_log_rawwrite(errorLine);
led_on(LED_ERROR);
if(flashLED)
{
led_errorcode(errNo);
}
}
void error_log_msg(uint8_t errNo, bool flashLED, char* infoText)
{
char labelBuffer[32];
labelBuffer[0] = 0x00;
if(errNo <= MAX_ERRNO)
{
strncpy_P(labelBuffer,(char*)pgm_read_word(&(errorMessageLookup[errNo])),32);
}
char errorLine[256];
snprintf(errorLine, 256, "%lu,%u,%s,%s,\r\n", time_millis(), errNo, labelBuffer, infoText);
error_log_rawwrite(errorLine);
led_on(LED_ERROR);
if(flashLED)
{
led_errorcode(errNo);
}
}
void info_log_msg(char* infoText)
{
char errorLine[256];
snprintf(errorLine, 256, "%lu,8,infotext,%s,\r\n", time_millis(), infoText);
error_log_rawwrite(errorLine);
}
void error_log_rawwrite(char *buffer)
{
uint8_t len = strlen(buffer);
if(fat_write_file(fd_errorlog, (uint8_t*) buffer, len) != len)
{
// Error writing to file
return;
}
}
void logger_closeLog()
{
fat_close_file(fd_datalog);
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;
}
|