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 #define FILE_NAME "log.txt"
14 #define FILE_OVERWRITE 1
15 #define FILE_PREALLOC_SIZE 102400
18 #include "hw/hw_led.h"
22 #include <cfg/debug.h>
25 #include <cpu/power.h>
31 #include <FAT16/fat.h>
32 #include <FAT16/partition.h>
33 #include <FAT16/sd_raw.h>
37 #include <drv/timer.h>
38 #include <kern/proc.h>
51 struct fat_fs_struct *fs;
52 struct partition_struct *partition;
53 struct fat_dir_struct *dd;
54 struct fat_dir_entry_struct dir_entry;
55 struct fat_file_struct *fd;
60 static void fat_init(void)
62 partition = partition_open(
66 sd_raw_write_interval,
72 // If the partition did not open, assume the storage device
73 // is a "superfloppy", i.e. has no MBR.
74 partition = partition_open(
78 sd_raw_write_interval,
87 if (!(fs = fat_open(partition)))
93 fat_get_dir_entry_of_path(fs, "/", &dir_entry);
94 if (!(dd = fat_open_dir(fs, &dir_entry)))
100 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)
102 fat_reset_dir(dd); //Make sure to start from the beginning of the directory!
103 while(fat_read_dir(dd, dir_entry))
105 if(strcmp(dir_entry->long_name, name) == 0)
115 static struct fat_file_struct *open_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name)
117 struct fat_dir_entry_struct file_entry;
118 if(!find_file_in_dir(fs, dd, name, &file_entry))
121 return fat_open_file(fs, &file_entry);
124 #endif // !BERTOS_FAT
126 static void init(void)
128 /* Initialize debugging module (allow kprintf(), etc.) */
131 /* Initialize system timer */
134 /* Enable all the interrupts */
137 /* Kernel initialization: processes */
140 /* Initialize UART0 */
141 ser_init(&ser, SER_UART0);
143 //FIXME: The 16MHz arduino clock causes 3.5% baudrate error at 115200
144 //ser_setbaudrate(&ser, 115200);
145 ser_setbaudrate(&ser, 57600);
148 // At this baud rate, we're fast enought to write a continuous
149 // input stream to the SD card
150 //ser_setbaudrate(&ser, 19200);
152 /* Initialize LED driver */
156 kfile_printf(&ser.fd, "Hello, world!\r\n");
158 DDRB |= BV(PORTB0) | BV(PORTB2) | BV(PORTB3) | BV(PORTB5);
161 ASSERT(0); for(;;) {}
175 bool sync_pending = false;
178 if(!fat_create_file(dd, FILE_NAME, &dir_entry))
181 fat_delete_file(fs, &dir_entry);
182 fat_create_file(dd, FILE_NAME, &dir_entry);
186 fd = open_file_in_dir(fs, dd, FILE_NAME);
188 /* preallocate file */
189 #if FILE_PREALLOC_SIZE
192 for (int i = 0; i < (int)(FILE_PREALLOC_SIZE / sizeof(buf)); ++i)
193 fat_write_file(fd, (void *)buf, sizeof(buf));
196 fat_seek_file(fd, &offset, FAT_SEEK_SET);
203 if ((size = ser_read(&ser, buf, sizeof(buf))) != 0)
206 fat_write_file(fd, (void *)buf, size);
212 serstatus_t err = ser_getstatus(&ser);
213 if (err & ~SERRF_RXTIMEOUT)
216 kfile_printf(&ser.fd, "\r\n*** err=%d ***\r\n", err);
218 ser_setstatus(&ser, 0);
225 sync_pending = false;