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
14 #define FILE_NAME "log.txt"
15 #define FILE_OVERWRITE 1
16 #define FILE_PREALLOC_SIZE 102400
19 #include "hw/hw_led.h"
23 #include <cfg/debug.h>
26 #include <cpu/power.h>
28 #include <FAT16/fat.h>
29 #include <FAT16/partition.h>
30 #include <FAT16/sd_raw.h>
33 #include <drv/timer.h>
34 #include <kern/proc.h>
43 struct fat_fs_struct *fs;
44 struct partition_struct *partition;
45 struct fat_dir_struct *dd;
46 struct fat_dir_entry_struct dir_entry;
47 struct fat_file_struct *fd;
50 static void fat_init(void)
52 partition = partition_open(
56 sd_raw_write_interval,
62 // If the partition did not open, assume the storage device
63 // is a "superfloppy", i.e. has no MBR.
64 partition = partition_open(
68 sd_raw_write_interval,
77 if (!(fs = fat_open(partition)))
82 // Open root directory
83 fat_get_dir_entry_of_path(fs, "/", &dir_entry);
84 if (!(dd = fat_open_dir(fs, &dir_entry)))
90 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)
92 fat_reset_dir(dd); //Make sure to start from the beginning of the directory!
93 while(fat_read_dir(dd, dir_entry))
95 if(strcmp(dir_entry->long_name, name) == 0)
105 static struct fat_file_struct *open_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name)
107 struct fat_dir_entry_struct file_entry;
108 if(!find_file_in_dir(fs, dd, name, &file_entry))
111 return fat_open_file(fs, &file_entry);
115 static void init(void)
117 /* Initialize debugging module (allow kprintf(), etc.) */
120 /* Initialize system timer */
123 /* Enable all the interrupts */
126 /* Kernel initialization: processes */
129 /* Initialize UART0 */
130 ser_init(&ser, SER_UART0);
132 //FIXME: The 16MHz arduino clock causes 3.5% baudrate error at 115200
133 //ser_setbaudrate(&ser, 115200);
134 ser_setbaudrate(&ser, 57600);
137 // At this baud rate, we're fast enought to write a continuous
138 // input stream to the SD card
139 //ser_setbaudrate(&ser, 19200);
141 // Initialize LED driver
145 * This gives avrdude some time to start reflashing in case
146 * the bug caused the program to do continous serial output
150 DDRB |= BV(PORTB0) | BV(PORTB2) | BV(PORTB3) | BV(PORTB5);
153 ASSERT(0); for(;;) {}
166 bool sync_pending = false;
169 if(!fat_create_file(dd, FILE_NAME, &dir_entry))
172 fat_delete_file(fs, &dir_entry);
173 fat_create_file(dd, FILE_NAME, &dir_entry);
177 fd = open_file_in_dir(fs, dd, FILE_NAME);
179 /* preallocate file */
180 #if FILE_PREALLOC_SIZE
183 for (int i = 0; i < (int)(FILE_PREALLOC_SIZE / sizeof(buf)); ++i)
184 fat_write_file(fd, (void *)buf, sizeof(buf));
187 fat_seek_file(fd, &offset, FAT_SEEK_SET);
194 if ((size = ser_read(&ser, buf, sizeof(buf))) != 0)
197 fat_write_file(fd, (void *)buf, size);
203 serstatus_t err = ser_getstatus(&ser);
204 if (err & ~SERRF_RXTIMEOUT)
207 kfile_printf(&ser.fd, "\r\n*** err=%d ***\r\n", err);
209 ser_setstatus(&ser, 0);
216 sync_pending = false;