4 #include <aversive/pgmspace.h>
7 #include "fat_config.h"
10 #include "sd_raw_config.h"
14 static struct fat_file_struct *log_fd = NULL;
16 static uint8_t find_file_in_dir(struct fat_fs_struct* fs,
17 struct fat_dir_struct* dd, const char* name,
18 struct fat_dir_entry_struct* dir_entry)
22 while(fat_read_dir(dd, dir_entry)) {
23 if(strcmp(dir_entry->long_name, name) == 0) {
32 static struct fat_file_struct *open_file_in_dir(struct fat_fs_struct* fs,
33 struct fat_dir_struct* dd, const char* name)
35 struct fat_dir_entry_struct file_entry;
37 if(!find_file_in_dir(fs, dd, name, &file_entry))
40 return fat_open_file(fs, &file_entry);
43 /* open the log file on the SD card */
44 int8_t sd_log_open(void)
46 struct fat_file_struct *fd;
47 struct fat_fs_struct *fs;
48 struct partition_struct *partition ;
49 struct fat_dir_struct *dd;
50 struct fat_dir_entry_struct directory;
51 struct fat_dir_entry_struct file_entry;
55 /* setup sd card slot */
58 printf_P(PSTR("MMC/SD initialization failed\n"));
63 /* open first partition */
64 partition = partition_open(sd_raw_read,
66 #if SD_RAW_WRITE_SUPPORT
67 sd_raw_write, sd_raw_write_interval,
74 /* If the partition did not open, assume the storage device
75 * is a "superfloppy", i.e. has no MBR.
77 partition = partition_open(sd_raw_read,
79 #if SD_RAW_WRITE_SUPPORT
81 sd_raw_write_interval,
89 printf_P(PSTR("opening partition failed\n"));
95 /* open file system */
96 fs = fat_open(partition);
99 printf_P(PSTR("opening filesystem failed\n"));
104 /* open root directory */
105 fat_get_dir_entry_of_path(fs, "/", &directory);
106 dd = fat_open_dir(fs, &directory);
109 printf_P(PSTR("opening root directory failed\n"));
114 printf_P(PSTR("choose log file name\n"));
116 snprintf(name, sizeof(name), "log%.4d", i++);
117 if (!find_file_in_dir(fs, dd, name, &file_entry))
121 printf_P(PSTR("create log file %s\n"), name);
122 if (!fat_create_file(dd, name, &file_entry)) {
123 printf_P(PSTR("error creating file: "));
126 fd = open_file_in_dir(fs, dd, name);
128 printf_P(PSTR("error opening "));
133 imuboard.flags |= IMUBOARD_F_SDCARD_OK;
138 intptr_t sd_log_write(const void *buffer, uintptr_t buffer_len)
143 return fat_write_file(log_fd, buffer, buffer_len);
146 uint8_t sd_log_enabled(void)
148 return log_fd != NULL;