4 * \author Bernie Innocenti <bernie@codewiz.org>
6 * \brief Serial data logger for RMS
8 * This application logs data
11 #include "hw/hw_led.h"
15 #include <cfg/debug.h>
18 #include <cpu/power.h>
24 #include <FAT16/fat.h>
25 #include <FAT16/partition.h>
26 #include <FAT16/sd_raw.h>
30 #include <drv/timer.h>
31 #include <kern/proc.h>
44 struct fat_fs_struct *fs;
45 struct partition_struct *partition;
46 struct fat_dir_struct *dd;
47 struct fat_dir_entry_struct dir_entry;
48 struct fat_file_struct *fd;
53 static void fat_init(void)
55 partition = partition_open(
59 sd_raw_write_interval,
65 // If the partition did not open, assume the storage device
66 // is a "superfloppy", i.e. has no MBR.
67 partition = partition_open(
71 sd_raw_write_interval,
80 if (!(fs = fat_open(partition)))
86 fat_get_dir_entry_of_path(fs, "/", &dir_entry);
87 if (!(dd = fat_open_dir(fs, &dir_entry)))
93 static uint8_t find_file_in_dir(UNUSED_ARG(struct fat_fs_struct *, fs), struct fat_dir_struct* dd, const char* name, struct fat_dir_entry_struct* dir_entry)
95 fat_reset_dir(dd); //Make sure to start from the beginning of the directory!
96 while(fat_read_dir(dd, dir_entry))
98 if(strcmp(dir_entry->long_name, name) == 0)
108 static struct fat_file_struct *open_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name)
110 struct fat_dir_entry_struct file_entry;
111 if(!find_file_in_dir(fs, dd, name, &file_entry))
114 return fat_open_file(fs, &file_entry);
117 #endif // !BERTOS_FAT
119 static void init(void)
121 /* Initialize debugging module (allow kprintf(), etc.) */
124 /* Initialize system timer */
127 /* Enable all the interrupts */
130 /* Kernel initialization: processes */
133 /* Initialize UART0 */
134 ser_init(&ser, SER_UART0);
136 //ser_setbaudrate(&ser, 115200);
137 //ser_setbaudrate(&ser, 57600);
139 ser_setbaudrate(&ser, 19200);
141 /* Initialize LED driver */
145 kfile_printf(&ser.fd, "Hello, world!\r\n");
148 ser_init(&spi, SER_SPI);
150 DDRB |= BV(PORTB0) | BV(PORTB2) | BV(PORTB3) | BV(PORTB5);
153 ASSERT(0); for(;;) {}
168 if ((fat_err = f_mount(0, &fs)) != FR_OK)
170 LOG_ERR("Error mounting FAT volume: %d\n", fat_err);
173 fat_err = fatfile_open(&file, "foo.txt", FA_WRITE | FA_CREATE_ALWAYS);
174 ASSERT(fat_err == FR_OK);
176 size_t count = kfile_write(&file.fd, "Hello, world!", strlen("Hello, world!"));
177 ASSERT(count == strlen("Hello, world!"));
181 fat_create_file(dd, "log.txt", &dir_entry);
182 fd = open_file_in_dir(fs, dd, "log.txt");
184 fat_seek_file(fd, &offset, FAT_SEEK_END);
192 bool sync_pending = false;
194 if ((size = ser_read(&ser, buf, sizeof(buf))) != 0)
197 fat_write_file(fd, (void *)buf, size);
199 sprintf(buf, "%d\r\n", size);
200 ser_write(&ser, buf, strlen(buf));
205 serstatus_t err = ser_getstatus(&ser);
206 if (err & ~SERRF_RXTIMEOUT)
209 kfile_printf(&ser.fd, "\r\n*** err=%d ***\r\n", err);
211 ser_setstatus(&ser, 0);
218 sync_pending = false;