4 #include <aversive/pgmspace.h>
7 #include "fat_config.h"
10 #include "sd_raw_config.h"
13 static struct fat_file_struct *log_fd = NULL;
15 static uint8_t find_file_in_dir(struct fat_fs_struct* fs,
16 struct fat_dir_struct* dd, const char* name,
17 struct fat_dir_entry_struct* dir_entry)
21 while(fat_read_dir(dd, dir_entry)) {
22 if(strcmp(dir_entry->long_name, name) == 0) {
31 static struct fat_file_struct *open_file_in_dir(struct fat_fs_struct* fs,
32 struct fat_dir_struct* dd, const char* name)
34 struct fat_dir_entry_struct file_entry;
36 if(!find_file_in_dir(fs, dd, name, &file_entry))
39 return fat_open_file(fs, &file_entry);
42 /* open the log file on the SD card */
43 int8_t sd_log_open(void)
45 struct fat_file_struct *fd;
46 struct fat_fs_struct *fs;
47 struct partition_struct *partition ;
48 struct fat_dir_struct *dd;
49 struct fat_dir_entry_struct directory;
50 struct fat_dir_entry_struct file_entry;
54 /* setup sd card slot */
57 printf_P(PSTR("MMC/SD initialization failed\n"));
62 /* open first partition */
63 partition = partition_open(sd_raw_read,
65 #if SD_RAW_WRITE_SUPPORT
66 sd_raw_write, sd_raw_write_interval,
73 /* If the partition did not open, assume the storage device
74 * is a "superfloppy", i.e. has no MBR.
76 partition = partition_open(sd_raw_read,
78 #if SD_RAW_WRITE_SUPPORT
80 sd_raw_write_interval,
88 printf_P(PSTR("opening partition failed\n"));
94 /* open file system */
95 fs = fat_open(partition);
98 printf_P(PSTR("opening filesystem failed\n"));
103 /* open root directory */
104 fat_get_dir_entry_of_path(fs, "/", &directory);
105 dd = fat_open_dir(fs, &directory);
108 printf_P(PSTR("opening root directory failed\n"));
113 printf("choose log file name\n");
115 snprintf(name, sizeof(name), "log%.4d", i++);
116 if (!find_file_in_dir(fs, dd, name, &file_entry))
120 printf("create log file %s\n", name);
121 if (!fat_create_file(dd, name, &file_entry)) {
122 printf_P(PSTR("error creating file: "));
125 fd = open_file_in_dir(fs, dd, name);
127 printf_P(PSTR("error opening "));
135 intptr_t sd_log_write(const void *buffer, uintptr_t buffer_len)
140 return fat_write_file(log_fd, buffer, buffer_len);
143 uint8_t sd_log_enabled(void)
145 return log_fd != NULL;