Changeset - 137d4566a1d4
[Not reviewed]
default
0 4 0
ethanzonca@CL-SEC241-08.cedarville.edu - 13 years ago 2012-11-15 15:41:02
ethanzonca@CL-SEC241-08.cedarville.edu
SD card logging operational with more features
4 files changed with 82 insertions and 75 deletions:
0 comments (0 inline, 0 general)
master/master/config.h
Show inline comments
 
@@ -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
master/master/lib/logger.c
Show inline comments
 
@@ -8,12 +8,15 @@
 
#include <string.h>
 
#include <avr/pgmspace.h>
 
#include <avr/sleep.h>
 
#include <avr/eeprom.h>
 
#include <string.h>
 
#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,81 +27,75 @@
 
  * 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
 
	if(!sd_raw_init())
 
		{
 
			// initialization failed!!!!!!!
 
			PORTA |= 0b00000001;
 
		// 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)
 
		{
 
			// 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
 
										);
 
		// 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 frick opening partition failed
 
				PORTA |= 0b00000010;
 
			// opening partition failed
 
				return;
 
			}
 
		}
 
		//open partition END
 
	
 
	
 
		//open file system BEGIN
 
		struct fat_fs_struct* fs = fat_open(partition);
 
	// Open FAT filesystem
 
	fs = fat_open(partition);
 
		if(!fs)
 
		{
 
			// opening fs failed!
 
			PORTA |= 0b00000100;
 
		// opening filesystem failed
 
			return;
 
		}
 
		//open file system END
 
	
 
	
 
		//open directory BEGIN
 
		/* open root directory */
 
		struct fat_dir_entry_struct directory;
 
	// Open directory
 
		fat_get_dir_entry_of_path(fs, "/", &directory);
 

	
 
		struct fat_dir_struct* dd = fat_open_dir(fs, &directory);
 
	dd = fat_open_dir(fs, &directory);
 
		if(!dd)
 
		{
 
			// frick opening root dir failed
 
		// opening root directory 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;
 
	// 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 */
 
		struct fat_file_struct* fd = open_file_in_dir(fs, dd, "data.csv"); //logger.h
 
	fd = open_file_in_dir(fs, dd, filename); //logger.h
 
		if(!fd)
 
		{
 
			PORTA |= 0b00000110;
 
@@ -114,24 +111,26 @@ void logger_setup()
 
			return;
 
		}
 
		
 
		/* read text from the shell and write it to the file */
 
		uint8_t data_len = sizeof(buffer);
 
	// Write header information
 
	logger_log(LOGGER_HEADERTEXT);
 
	logger_log("\n-- BEGIN DATA --\n");
 
	logger_log("C1, C2, C3, C4, C5, C6\n");
 
			
 
		while(1)
 
		{
 
			/* write text to file !! */
 
			if(fat_write_file(fd, (uint8_t*) buffer, data_len) != data_len)
 
}	
 
 
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"));
 
				break;
 
		return;
 
			}
 
		}
 

	
 
void logger_closeLog() {
 
	
 
		fat_close_file(fd); //may want to leave file open ??
 
		
 
 
		
 
		//prepare for closing SD connection BEGIN
 
		/* close directory */
 
		fat_close_dir(dd); //fat.c
 

	
 
@@ -140,26 +139,26 @@ void logger_setup()
 

	
 
		/* close partition */
 
		partition_close(partition); //partition.c
 
		//prepare for closing SD connection END
 
}	
 
 
//writes a single line to the SD card
 
uint8_t logger_writeLine(char* dateLine, uint8_t length)
 
{
 
	return length; //does not actually return length
 
}
 
 
 
 
 
//i think opens a file so it can be read/written
 
 
// 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;
 
}
master/master/lib/logger.h
Show inline comments
 
@@ -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
master/master/master.c
Show inline comments
 
@@ -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);
 
		
 
		
0 comments (0 inline, 0 general)