remove dead code in imu.c
[protos/imu.git] / sd_log.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <aversive/pgmspace.h>
5
6 #include "fat.h"
7 #include "fat_config.h"
8 #include "partition.h"
9 #include "sd_raw.h"
10 #include "sd_raw_config.h"
11 #include "sd_log.h"
12
13 static struct fat_file_struct *log_fd = NULL;
14
15 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)
18 {
19         (void)fs;
20
21         while(fat_read_dir(dd, dir_entry)) {
22                 if(strcmp(dir_entry->long_name, name) == 0) {
23                         fat_reset_dir(dd);
24                         return 1;
25                 }
26         }
27
28         return 0;
29 }
30
31 struct fat_file_struct *open_file_in_dir(struct fat_fs_struct* fs,
32         struct fat_dir_struct* dd, const char* name)
33 {
34         struct fat_dir_entry_struct file_entry;
35
36         if(!find_file_in_dir(fs, dd, name, &file_entry))
37                 return 0;
38
39         return fat_open_file(fs, &file_entry);
40 }
41
42 struct fat_file_struct *open_log_file(void)
43 {
44         struct fat_file_struct *fd;
45         struct fat_fs_struct *fs;
46         struct partition_struct *partition ;
47         struct fat_dir_struct *dd;
48         struct fat_dir_entry_struct directory;
49         struct fat_dir_entry_struct file_entry;
50         int16_t i = 0;
51         char name[16];
52
53         /* setup sd card slot */
54         if (!sd_raw_init()) {
55 #if SD_DEBUG
56                 printf_P(PSTR("MMC/SD initialization failed\n"));
57 #endif
58                 return NULL;
59         }
60
61         /* open first partition */
62         partition = partition_open(sd_raw_read,
63                 sd_raw_read_interval,
64 #if SD_RAW_WRITE_SUPPORT
65                 sd_raw_write, sd_raw_write_interval,
66 #else
67                 0, 0,
68 #endif
69                 0);
70
71         if (!partition) {
72                 /* If the partition did not open, assume the storage device
73                  * is a "superfloppy", i.e. has no MBR.
74                  */
75                 partition = partition_open(sd_raw_read,
76                         sd_raw_read_interval,
77 #if SD_RAW_WRITE_SUPPORT
78                         sd_raw_write,
79                         sd_raw_write_interval,
80 #else
81                         0,
82                         0,
83 #endif
84                         -1);
85                 if (!partition) {
86 #if SD_DEBUG
87                         printf_P(PSTR("opening partition failed\n"));
88 #endif
89                         return NULL;
90                 }
91         }
92
93         /* open file system */
94         fs = fat_open(partition);
95         if (!fs) {
96 #if SD_DEBUG
97                 printf_P(PSTR("opening filesystem failed\n"));
98 #endif
99                 return NULL;
100         }
101
102         /* open root directory */
103         fat_get_dir_entry_of_path(fs, "/", &directory);
104         dd = fat_open_dir(fs, &directory);
105         if (!dd) {
106 #if SD_DEBUG
107                 printf_P(PSTR("opening root directory failed\n"));
108 #endif
109                 return NULL;
110         }
111
112         /* print some card information as a boot message */
113         //print_disk_info(fs);
114
115         printf("choose log file name\n");
116         while (1) {
117                 snprintf(name, sizeof(name), "log%.4d", i++);
118                 if (!find_file_in_dir(fs, dd, name, &file_entry))
119                         break;
120         }
121
122         printf("create log file %s\n", name);
123         if (!fat_create_file(dd, name, &file_entry)) {
124                 printf_P(PSTR("error creating file: "));
125         }
126
127         fd = open_file_in_dir(fs, dd, name);
128         if (!fd) {
129                 printf_P(PSTR("error opening "));
130                 return NULL;
131         }
132
133         return fd;
134 }
135
136 struct fat_file_struct *get_log_file(void)
137 {
138         return log_fd;
139 }