4 * \author Bernie Innocenti <bernie@codewiz.org>
6 * \brief Serial data logger for RMS
8 * This application records all incoming data from an RS-232 serial port
9 * to a file on a FAT-formatted SD card. It can be used to log kernel
13 #include "hw/hw_led.h"
17 #include <cfg/debug.h>
20 #include <cpu/power.h>
26 #include <FAT16/fat.h>
27 #include <FAT16/partition.h>
28 #include <FAT16/sd_raw.h>
32 #include <drv/timer.h>
33 #include <kern/proc.h>
46 struct fat_fs_struct *fs;
47 struct partition_struct *partition;
48 struct fat_dir_struct *dd;
49 struct fat_dir_entry_struct dir_entry;
50 struct fat_file_struct *fd;
55 static void fat_init(void)
57 partition = partition_open(
61 sd_raw_write_interval,
67 // If the partition did not open, assume the storage device
68 // is a "superfloppy", i.e. has no MBR.
69 partition = partition_open(
73 sd_raw_write_interval,
82 if (!(fs = fat_open(partition)))
88 fat_get_dir_entry_of_path(fs, "/", &dir_entry);
89 if (!(dd = fat_open_dir(fs, &dir_entry)))
95 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)
97 fat_reset_dir(dd); //Make sure to start from the beginning of the directory!
98 while(fat_read_dir(dd, dir_entry))
100 if(strcmp(dir_entry->long_name, name) == 0)
110 static struct fat_file_struct *open_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name)
112 struct fat_dir_entry_struct file_entry;
113 if(!find_file_in_dir(fs, dd, name, &file_entry))
116 return fat_open_file(fs, &file_entry);
119 #endif // !BERTOS_FAT
121 static void init(void)
123 /* Initialize debugging module (allow kprintf(), etc.) */
126 /* Initialize system timer */
129 /* Enable all the interrupts */
132 /* Kernel initialization: processes */
135 /* Initialize UART0 */
136 ser_init(&ser, SER_UART0);
138 //FIXME: The 16MHz arduino clock causes 3.5% baudrate error at 115200
139 //ser_setbaudrate(&ser, 115200);
140 ser_setbaudrate(&ser, 57600);
143 // At this baud rate, we're fast enought to write a continuous
144 // input stream to the SD card
145 //ser_setbaudrate(&ser, 19200);
147 /* Initialize LED driver */
151 kfile_printf(&ser.fd, "Hello, world!\r\n");
154 ser_init(&spi, SER_SPI);
156 DDRB |= BV(PORTB0) | BV(PORTB2) | BV(PORTB3) | BV(PORTB5);
159 ASSERT(0); for(;;) {}
174 if ((fat_err = f_mount(0, &fs)) != FR_OK)
176 LOG_ERR("Error mounting FAT volume: %d\n", fat_err);
179 fat_err = fatfile_open(&file, "foo.txt", FA_WRITE | FA_CREATE_ALWAYS);
180 ASSERT(fat_err == FR_OK);
182 size_t count = kfile_write(&file.fd, "Hello, world!", strlen("Hello, world!"));
183 ASSERT(count == strlen("Hello, world!"));
187 fat_create_file(dd, "log.txt", &dir_entry);
188 fd = open_file_in_dir(fs, dd, "log.txt");
190 fat_seek_file(fd, &offset, FAT_SEEK_END);
198 bool sync_pending = false;
200 if ((size = ser_read(&ser, buf, sizeof(buf))) != 0)
203 fat_write_file(fd, (void *)buf, size);
209 serstatus_t err = ser_getstatus(&ser);
210 if (err & ~SERRF_RXTIMEOUT)
213 kfile_printf(&ser.fd, "\r\n*** err=%d ***\r\n", err);
215 ser_setstatus(&ser, 0);
222 sync_pending = false;