Files
@ 28bdbd881b55
Branch filter:
Location: seniordesign-firmware/master/master/lib/logger.c
28bdbd881b55
3.7 KiB
text/plain
Added looptime and other optimizations. LoopTime interrupt is firing, but incrementing may or may not work.
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 | /*
* CFile1.c
*
* Created: 11/7/2012 8:05:44 PM
* Author: mkanning
*/
#include "../config.h"
#include <util/delay.h>
#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"
/*
//config edits
* By changing the MCU* variables in the Makefile, you can use other Atmel
* microcontrollers or different clock speeds. You might also want to change
* the configuration defines in the files fat_config.h, partition_config.h,
* sd_raw_config.h and sd-reader_config.h. For example, you could disable
* 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())
{
// initialization failed
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
// 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)
{
// opening partition failed
return;
}
}
// Open FAT filesystem
fs = fat_open(partition);
if(!fs)
{
// opening filesystem failed
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);
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 for file in current directory and open it
fd = open_file_in_dir(fs, dd, filename);
if(!fd)
{
_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
_delay_ms(10);
fat_close_file(fd);
return;
}
// Write header information
logger_log(LOGGER_HEADERTEXT);
logger_log("\n-- BEGIN DATA --\n");
logger_log("C1, C2, C3, C4, C5, C6\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;
}
|