Changeset - ca7b24de9240
[Not reviewed]
default
0 6 0
ethanzonca@CL-ENS241-08.cedarville.edu - 12 years ago 2013-02-18 22:35:41
ethanzonca@CL-ENS241-08.cedarville.edu
Added robust error logging support
6 files changed with 152 insertions and 62 deletions:
0 comments (0 inline, 0 general)
master/master/config.h
Show inline comments
 
@@ -10,40 +10,46 @@
 
 *
 
 */
 
 
#ifndef CONFIG_H_
 
#define CONFIG_H_
 
 
#include <avr/pgmspace.h>
 
 
// --------------------------------------------------------------------------
 
// Module config (master.c)
 
// --------------------------------------------------------------------------
 
 
//#define DEBUG_OUTPUT
 
 
#define F_CPU 11059200
 
#define MODULE_ID '1'
 
#define BOARDTEMP_ADDR 0x90
 
 
#define HEATER_THRESHOLD 60
 
 
 
// --------------------------------------------------------------------------
 
// Error Codes config (led.c, used throughout code)
 
// --------------------------------------------------------------------------
 
 
// SD Card
 
#define ERROR_SD_INIT 2
 
#define ERROR_SD_PARTITION 3
 
#define ERROR_SD_FILE 4
 
#define ERROR_SLAVETIMEOUT 0
 
#define ERROR_SD_INIT 1
 
#define ERROR_SD_PARTITION 2
 
#define ERROR_SD_FILE 3
 
 
#define ERROR_XBEETIMEOUT 4
 
 
#define ERROR_XBEETIMEOUT 5
 
#define ERROR_NOXBEE 6
 
 
#define ERROR_FATAL 5
 
 
#define ERROR_CRAP 15
 
#define ERROR_ATFAIL 6
 
#define ERROR_EXITAT 7
 
 
#define ERROR_ATFAIL 3
 
#define ERROR_EXITAT 8
 
// !!! Please specify detailed messages for these error codes in logger.c
 
 
// --------------------------------------------------------------------------
 
// Slave Sensors config (slavesensors.c)
 
// --------------------------------------------------------------------------
 
 
#define MAX_NUM_SLAVES 5  // Maximum number of nodes in the system
master/master/lib/logger.c
Show inline comments
 
@@ -23,25 +23,53 @@
 
#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 7
 

	
 
// 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 *const errorMessageLookup[] PROGMEM =
 
{
 
	err_0,
 
	err_1,
 
	err_2,
 
	err_3,
 
	err_4,
 
	err_5,
 
	err_6,
 
	err_7,
 
};
 
 
 
 
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;
 
struct fat_file_struct* fd_datalog;
 
struct fat_file_struct* fd_errorlog;
 
 
void logger_setup()
 
{
 
 
	if(!sd_raw_init())
 
	{
 
		led_errorcode(ERROR_SD_INIT);
 
		error_log(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
 
	
 
@@ -49,94 +77,153 @@ void logger_setup()
 
	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.
 
		led_errorcode(ERROR_SD_PARTITION);
 
		error_log(ERROR_SD_PARTITION);
 
		return;
 
	}
 
	
 
	
 
	// Open FAT filesystem
 
	fs = fat_open(partition);
 
	if(!fs)
 
	{
 
		// opening filesystem failed
 
		led_errorcode(ERROR_SD_PARTITION);
 
		error_log(ERROR_SD_PARTITION);
 
		return;
 
	}
 
	
 
	// Open directory
 
	fat_get_dir_entry_of_path(fs, "/", &directory);
 
	// Open root directory
 
	struct fat_dir_entry_struct rootDirEntry;
 
	fat_get_dir_entry_of_path(fs, "/", &rootDirEntry);
 

	
 
	dd = fat_open_dir(fs, &directory);
 
	dd = fat_open_dir(fs, &rootDirEntry);
 
	if(!dd)
 
	{
 
		// opening root directory failed
 
		_delay_ms(10);
 
		led_errorcode(ERROR_SD_FILE);
 
		error_log(ERROR_SD_FILE);
 
		return;
 
	}
 
	
 
		
 
	// Create new log file
 
	uint8_t logid = eeprom_read_byte(LOGGER_ID_EEPROM_ADDR);
 
	char filename[16];
 
	// 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];
 
	
 
	
 
	// we pre-increment logid here because it starts at 255, then wraps to 0
 
	snprintf(filename, 16, "data%d.csv",++logid);
 
	
 
	// TODO: Catch errors here
 
	if(fat_create_file(dd, filename, &directory) == 0) 
 
	// 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);
 
	}
 
	// Search for file in current directory and open it
 
	fd_errorlog = open_file_in_dir(fs, dd, errorFilename);
 
	if(!fd_errorlog)
 
	{
 
		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)
 
		serial0_sendString("Error open errorlog!\r\n");
 
		error_log(ERROR_SD_FILE);
 
		return;
 
	}
 
	errorOffset=0;
 
	if(!fat_seek_file(fd_errorlog, &errorOffset, FAT_SEEK_SET))
 
	{
 
		led_errorcode(ERROR_SD_FILE);
 
		_delay_ms(10);
 
		// Error seeking to file
 
		serial0_sendString("Error seek errorlog!\r\n");
 
		error_log(ERROR_SD_FILE);
 
		fat_close_file(fd_errorlog);
 
		return;
 
	}
 
	
 
	// Seek to beginning of file
 
	// TODO: Is this needed?
 
	int32_t offset = 0; 
 
	if(!fat_seek_file(fd, &offset, FAT_SEEK_SET))
 
	
 
		
 
	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);
 
	}
 
	// 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);
 
		return;
 
	}
 
	dataOffset=0;
 
	if(!fat_seek_file(fd_datalog, &dataOffset, FAT_SEEK_SET))
 
	{
 
		// Error seeking to file
 
		led_errorcode(ERROR_SD_FILE);
 
		_delay_ms(10);
 
		fat_close_file(fd);
 
		serial0_sendString("Error seek datalog!\r\n");
 
		error_log(ERROR_SD_FILE);
 
		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");
 
}	
 
 
void logger_log(char *buffer) 
 
{
 
	uint8_t len = strlen(buffer);
 
	if(fat_write_file(fd, (uint8_t*) buffer, len) != len)
 
	if(fat_write_file(fd_datalog, (uint8_t*) buffer, len) != len)
 
	{
 
		// Error writing to file
 
		return;
 
	}
 
}
 
 
void error_log(uint8_t errNo)
 
{
 
	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_errorcode(errNo);
 
}
 
 
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);
 
	fat_close_file(fd_datalog);
 
	fat_close_dir(dd);
 
	fat_close(fs);
 
	partition_close(partition);
 
}
 
 
master/master/lib/logger.h
Show inline comments
 
@@ -15,11 +15,12 @@
 
#define LOGGER_H_
 
 
void logger_setup();
 
uint8_t logger_writeLine(char* dateLine, uint8_t length);
 
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 error_log(uint8_t errNo);
 
void error_log_rawwrite(char *buffer);
 
void logger_log(char *buffer);
 
void logger_closeLog();
 
 
#endif /* LOGGER_H_ */
 
\ No newline at end of file
master/master/lib/sdcard/fat_config.h
Show inline comments
 
@@ -99,19 +99,19 @@ void get_datetime(uint16_t* year, uint8_
 
#define FAT_FS_COUNT 1
 

	
 
/**
 
 * \ingroup fat_config
 
 * Maximum number of file handles.
 
 */
 
#define FAT_FILE_COUNT 1
 
#define FAT_FILE_COUNT 2
 

	
 
/**
 
 * \ingroup fat_config
 
 * Maximum number of directory handles.
 
 */
 
#define FAT_DIR_COUNT 2
 
#define FAT_DIR_COUNT 3
 

	
 
/**
 
 * @}
 
 */
 

	
 
#if FAT_FAT32_SUPPORT
master/master/lib/sensordata.c
Show inline comments
 
@@ -80,18 +80,15 @@ void sensordata_logvalues()
 
		// Only generate/write header the first time data is ready
 
		if(!dataWasReady) 
 
		{
 
			#define CSV_BUFFER_SIZE 64
 
			char csvHeader[CSV_BUFFER_SIZE];
 
			csvHeader[0] = 0x00;
 
			
 
			slavesensors_selectlogger();
 
			
 
			// Add master data headers
 
			logger_log("Time,BoardTemp,GPSTime,GPSLat,GPSLon,GPSSpeed,GPSHDOP,GPSCourse,GPSSV,");
 
			serial0_sendString("Time,BoardTemp,GPSTime,GPSLat,GPSLon,GPSSpeed,GPSHDOP,GPSCourse,GPSSV,");
 
			
 
			// Add slave data headers
 
			for(uint8_t i=0; i<MAX_NUM_SLAVES; i++) 
 
			{
 
				for(uint8_t j=0; j<MAX_NUM_SENSORS; j++) 
 
				{
 
@@ -99,22 +96,20 @@ void sensordata_logvalues()
 
					
 
					// If a sensor value exists, write a header for it
 
					if(tmp != -2111111111) 
 
					{
 
						snprintf(csvHeader, CSV_BUFFER_SIZE,"%s-%s,", slavesensors_slavename(i), slavesensors_getLabel(j));
 
						logger_log(csvHeader);
 
						serial0_sendString(csvHeader);
 
					}
 
				}
 
			}
 
		
 
			// End line and write to SD card
 
			snprintf(csvHeader, CSV_BUFFER_SIZE,"\r\n");
 
			logger_log(csvHeader);
 

	
 
			serial0_sendString(csvHeader);
 
			
 
			dataWasReady = true;
 
		}
 
	
 
		// Write CSV sensor values to SD card
 
		#define CSV_LOGLINE_SIZE 512
 
		char logbuf[CSV_LOGLINE_SIZE];
master/master/lib/slavesensors.c
Show inline comments
 
@@ -48,13 +48,13 @@ const char *const labelLookup[] PROGMEM 
 
	label_5,
 
	label_6,
 
	label_7,
 
	label_8,
 
};
 

	
 
char labelBuffer[15]; // Size to length of label
 
char labelBuffer[32]; // Size to length of label
 
char* slavesensors_getLabel(uint8_t sensorID) 
 
{
 
	if(sensorID < 9)
 
	{
 
		strncpy_P(labelBuffer,(char*)pgm_read_word(&(labelLookup[sensorID])),15);
 
		
 
@@ -261,22 +261,22 @@ void slavesensors_selectnode(uint8_t nod
 
		
 
		snprintf(tmpBuf, 23, "ATDH %s%c",slaveAddressHigh[nodeIndex], 0x0D);
 
		serial0_sendString(tmpBuf);
 
		
 
		if(xbeeIsOk() != 0) 
 
		{
 
			led_errorcode(ERROR_NOXBEE);
 
			error_log(ERROR_XBEETIMEOUT);
 
			return;
 
		}
 
		
 
		snprintf(tmpBuf, 23, "ATDL %s%c",slaveAddressLow[nodeIndex], 0x0D);
 
		serial0_sendString(tmpBuf);
 
		
 
		if(xbeeIsOk() != 0) 
 
		{
 
			led_errorcode(ERROR_NOXBEE);
 
			error_log(ERROR_XBEETIMEOUT);
 
			return;
 
		}
 
		
 
		slavesensors_exitAT();
 
		selectedNode = nodeIndex;
 
	}
 
@@ -325,13 +325,13 @@ bool waitTimeout(uint32_t timeout) {
 
	uint32_t scanStart = time_millis();
 
	uint32_t lastBlink = 0;
 
	while(!serial0_hasChar())
 
	{
 
		if(time_millis() - scanStart > timeout)
 
		{
 
			led_errorcode(ERROR_XBEETIMEOUT);
 
			error_log(ERROR_XBEETIMEOUT);
 
			return true;
 
		}
 
		if(time_millis() - lastBlink > 50)
 
		{
 
			led_spin();
 
			
 
@@ -358,23 +358,23 @@ int slavesensors_enterAT()
 
	return xbeeIsOk();
 
}
 
 
int xbeeIsOk() 
 
{
 
	if(waitTimeout(2000)) {
 
		led_errorcode(ERROR_XBEETIMEOUT);
 
		error_log(ERROR_XBEETIMEOUT);
 
		return 1;
 
	}
 
	char* tmppntr = serial0_readLine();
 
	if(strcmp(tmppntr, "OK") == 0)
 
	{
 
		return 0;
 
	}
 
	else
 
	{
 
		led_errorcode(ERROR_NOXBEE);
 
		error_log(ERROR_SLAVETIMEOUT);
 
		return 1;
 
	}
 
}
 
 
bool slavesensors_dataReady() 
 
{
 
@@ -487,12 +487,13 @@ void slavesensors_process(uint8_t parseR
 
	{
 
		// Wait for data
 
		if(requesting && time_millis() - beginRequest > 1000) {
 
			// if we're requesting, we have no data, and we're over the timeout, this is bad!
 
			// setParserState(STATE_RESET); - meh, can't do this because it freaking increments the cirbufptr
 
			gotoNextSlaveOrSensor(true);
 
			error_log(ERROR_SLAVETIMEOUT);
 
		}
 
	}
 
	
 
	// Finished reception of a message (one sensor data value). If not finished, send out command to get the next one
 
	else if(parseResult == PARSERESULT_PARSEOK)
 
	{
0 comments (0 inline, 0 general)