From: Bernie Innocenti Date: Sun, 20 Mar 2011 22:31:01 +0000 (-0400) Subject: First working release X-Git-Url: https://codewiz.org/gitweb?p=rmslog.git;a=commitdiff_plain;h=3bf87cc3ac05c14e3fd921fa35333d946386b660 First working release --- diff --git a/buildrev.h b/buildrev.h index 2c1018d..802acab 100644 --- a/buildrev.h +++ b/buildrev.h @@ -1,2 +1,2 @@ -#define VERS_BUILD 2 +#define VERS_BUILD 433 #define VERS_HOST "giskard.codewiz.org" diff --git a/rmslog/cfg/cfg_debug.h b/rmslog/cfg/cfg_debug.h index 1063984..00822d3 100644 --- a/rmslog/cfg/cfg_debug.h +++ b/rmslog/cfg/cfg_debug.h @@ -45,6 +45,10 @@ */ #define CONFIG_KDEBUG_PORT 0 +#define CONFIG_KDEBUG_TRACE 0 +#define CONFIG_KDEBUG_VERBOSE_ASSERT 0 +#define CONFIG_KDEBUG_WALLS 0 + /** * Baudrate for the debug console. * $WIZ$ type = "int"; min = 300 diff --git a/rmslog/cfg/cfg_fat.h b/rmslog/cfg/cfg_fat.h index fcd717a..45ab9ae 100644 --- a/rmslog/cfg/cfg_fat.h +++ b/rmslog/cfg/cfg_fat.h @@ -54,7 +54,7 @@ * $WIZ$ type = "enum" * $WIZ$ value_list = "log_format" */ -#define FAT_LOG_FORMAT LOG_FMT_VERBOSE +#define FAT_LOG_FORMAT LOG_FMT_TERSE /** @@ -75,7 +75,7 @@ * Minimization level to remove some functions. * $WIZ$ type = "int"; min = 0; max = 3 */ -#define CONFIG_FAT_FS_MINIMIZE 0 +#define CONFIG_FAT_FS_MINIMIZE 2 #define _FS_MINIMIZE CONFIG_FAT_FS_MINIMIZE /** diff --git a/rmslog/cfg/cfg_formatwr.h b/rmslog/cfg/cfg_formatwr.h index bac8aca..ff929c5 100644 --- a/rmslog/cfg/cfg_formatwr.h +++ b/rmslog/cfg/cfg_formatwr.h @@ -48,7 +48,7 @@ * \sa PRINTF_NOFLOAT * \sa PRINTF_FULL */ -#define CONFIG_PRINTF PRINTF_FULL +#define CONFIG_PRINTF PRINTF_NOFLOAT /** * Size of buffer to format "%" sequences in printf. diff --git a/rmslog/cfg/cfg_heap.h b/rmslog/cfg/cfg_heap.h deleted file mode 100644 index e1a37b7..0000000 --- a/rmslog/cfg/cfg_heap.h +++ /dev/null @@ -1,50 +0,0 @@ -/** - * \file - * - * - * \brief Configuration file for heap module. - * - * - * \author Daniele Basile - */ - -#ifndef CFG_HEAP_H -#define CFG_HEAP_H - -/** - * Enable malloc/free like API. - * $WIZ$ type = "boolean" - */ -#define CONFIG_HEAP_MALLOC 1 - -#endif /* CFG_HEAP_H */ - - diff --git a/rmslog/cfg/cfg_monitor.h b/rmslog/cfg/cfg_monitor.h index d0e24da..78f222b 100644 --- a/rmslog/cfg/cfg_monitor.h +++ b/rmslog/cfg/cfg_monitor.h @@ -42,6 +42,6 @@ * Process monitor. * $WIZ$ type = "autoenabled" */ -#define CONFIG_KERN_MONITOR 1 +#define CONFIG_KERN_MONITOR 0 #endif /* CFG_MONITOR_H */ diff --git a/rmslog/cfg/cfg_proc.h b/rmslog/cfg/cfg_proc.h index e779ebe..bd3772a 100644 --- a/rmslog/cfg/cfg_proc.h +++ b/rmslog/cfg/cfg_proc.h @@ -43,7 +43,7 @@ * * $WIZ$ type = "autoenabled" */ -#define CONFIG_KERN 1 +#define CONFIG_KERN 0 /** * Kernel interrupt supervisor. WARNING: Experimental, still incomplete! @@ -72,14 +72,14 @@ * Priority-based scheduling policy. * $WIZ$ type = "boolean" */ -#define CONFIG_KERN_PRI 1 +#define CONFIG_KERN_PRI 0 /** * Dynamic memory allocation for processes. * $WIZ$ type = "boolean" * $WIZ$ conditional_deps = "heap" */ -#define CONFIG_KERN_HEAP 1 +#define CONFIG_KERN_HEAP 0 /** * Size of the dynamic memory pool used by processes. @@ -102,6 +102,6 @@ * $WIZ$ type = "enum" * $WIZ$ value_list = "log_format" */ -#define KERN_LOG_FORMAT LOG_FMT_VERBOSE +#define KERN_LOG_FORMAT LOG_FMT_TERSE #endif /* CFG_PROC_H */ diff --git a/rmslog/cfg/cfg_pwm.h b/rmslog/cfg/cfg_pwm.h deleted file mode 100644 index 595189e..0000000 --- a/rmslog/cfg/cfg_pwm.h +++ /dev/null @@ -1,56 +0,0 @@ -/** - * \file - * - * - * \brief Configuration file for PWM module. - * - * \author Daniele Basile - */ - -#ifndef CFG_PWM_H -#define CFG_PWM_H - -/** - * Module logging level. - * - * $WIZ$ type = "enum" - * $WIZ$ value_list = "log_level" - */ -#define PWM_LOG_LEVEL LOG_LVL_INFO - -/** - * Module logging format. - * - * $WIZ$ type = "enum" - * $WIZ$ value_list = "log_format" - */ -#define PWM_LOG_FORMAT LOG_FMT_VERBOSE - -#endif /* CFG_PWM_H */ diff --git a/rmslog/cfg/cfg_sd.h b/rmslog/cfg/cfg_sd.h index 8c40f82..81dccb5 100644 --- a/rmslog/cfg/cfg_sd.h +++ b/rmslog/cfg/cfg_sd.h @@ -53,7 +53,7 @@ * $WIZ$ type = "enum" * $WIZ$ value_list = "log_format" */ -#define SD_LOG_FORMAT LOG_FMT_VERBOSE +#define SD_LOG_FORMAT LOG_FMT_TERSE /** @@ -70,6 +70,6 @@ * * $WIZ$ type = "boolean" */ -#define CONFIG_SD_OLD_INIT 1 +#define CONFIG_SD_OLD_INIT 0 #endif /* CFG_SD_H */ diff --git a/rmslog/cfg/cfg_ser.h b/rmslog/cfg/cfg_ser.h index 91a10e0..b5ce58a 100644 --- a/rmslog/cfg/cfg_ser.h +++ b/rmslog/cfg/cfg_ser.h @@ -56,7 +56,7 @@ * $WIZ$ type = "int" * $WIZ$ min = 2 */ -#define CONFIG_UART0_RXBUFSIZE 32 +#define CONFIG_UART0_RXBUFSIZE 512 /** * Size of the outbound FIFO buffer for port 1 [bytes]. @@ -169,7 +169,7 @@ * $WIZ$ type = "int" * $WIZ$ supports = "avr" */ -#define CONFIG_SPI_CLOCK_DIV 16 +#define CONFIG_SPI_CLOCK_DIV 128 /** * SPI clock polarity: normal low or normal high. @@ -200,7 +200,7 @@ * $WIZ$ type = "int" * $WIZ$ min = -1 */ -#define CONFIG_SER_RXTIMEOUT -1 +#define CONFIG_SER_RXTIMEOUT 200 /** * Use RTS/CTS handshake. diff --git a/rmslog/cfg/cfg_signal.h b/rmslog/cfg/cfg_signal.h index ce5324e..e48d86a 100644 --- a/rmslog/cfg/cfg_signal.h +++ b/rmslog/cfg/cfg_signal.h @@ -42,6 +42,6 @@ * Inter-process signals. * $WIZ$ type = "autoenabled" */ -#define CONFIG_KERN_SIGNALS 1 +#define CONFIG_KERN_SIGNALS 0 #endif /* CFG_SIGNAL_H */ diff --git a/rmslog/hw/hw_sd.h b/rmslog/hw/hw_sd.h index ed10b80..d6c0d1b 100644 --- a/rmslog/hw/hw_sd.h +++ b/rmslog/hw/hw_sd.h @@ -39,14 +39,8 @@ #ifndef HW_SD_H #define HW_SD_H -#warning FIXME: This is an example implementation, you must implement it - -#define SD_CS_INIT() do { /* implement me */} while(0) -#define SD_CS_ON() do { /* implement me */} while(0) -#define SD_CS_OFF() do { /* implement me */} while(0) - -#define SD_PIN_INIT() do { /* implement me */} while(0) -#define SD_CARD_PRESENT() true /* implement me */ -#define SD_WRITE_PROTECT() false /* implement me */ +#define SD_CS_INIT() do { SD_CS_OFF(); DDRB |= BV(0) | BV(2); } while(0) +#define SD_CS_ON() do { PORTB &= ~BV(0); } while(0) +#define SD_CS_OFF() do { PORTB |= BV(0); } while(0) #endif /* HW_SD_H */ diff --git a/rmslog/main.c b/rmslog/main.c index 663386f..aa33cf7 100644 --- a/rmslog/main.c +++ b/rmslog/main.c @@ -1,122 +1,223 @@ /** * \file - * - * - * \author Andrea Righi - * - * \brief Kernel project. - * - * This is a minimalist kernel project: it just initializes the hardware and - * creates an independent process to blink an LED, while the main loop - * continues to monitor the stack utilization of all the processes. + * This application logs data */ #include "hw/hw_led.h" +#define LOG_LEVEL 3 +#include #include #include #include -#include -#include +#ifdef BERTOS_FAT + #include + #include +#else + #include + #include + #include +#endif +#include +#include #include -#include -static Serial out; +#include -static void init(void) + +static Serial ser; + +#ifdef BERTOS_FAT + static Serial spi; + static FATFS fs; + static FatFile file; + static Sd sd; +#else + struct fat_fs_struct *fs; + struct partition_struct *partition; + struct fat_dir_struct *dd; + struct fat_dir_entry_struct dir_entry; + struct fat_file_struct *fd; +#endif + + +#ifndef BERTOS_FAT +static void fat_init(void) { - /* Enable all the interrupts */ - IRQ_ENABLE; + partition = partition_open( + sd_raw_read, + sd_raw_read_interval, + sd_raw_write, + sd_raw_write_interval, + 0 + ); + + if (!partition) + { + // If the partition did not open, assume the storage device + // is a "superfloppy", i.e. has no MBR. + partition = partition_open( + sd_raw_read, + sd_raw_read_interval, + sd_raw_write, + sd_raw_write_interval, + 0 + ); + } + if (!partition) + { + ASSERT(0); for(;;) {} + } + + if (!(fs = fat_open(partition))) + { + ASSERT(0); for(;;) {} + } + + //Open root directory + fat_get_dir_entry_of_path(fs, "/", &dir_entry); + if (!(dd = fat_open_dir(fs, &dir_entry))) + { + ASSERT(0); for(;;) {} + } +} + +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) +{ + fat_reset_dir(dd); //Make sure to start from the beginning of the directory! + while(fat_read_dir(dd, dir_entry)) + { + if(strcmp(dir_entry->long_name, name) == 0) + { + //fat_reset_dir(dd); + return 1; + } + } + + return 0; +} + +static struct fat_file_struct *open_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name) +{ + struct fat_dir_entry_struct file_entry; + if(!find_file_in_dir(fs, dd, name, &file_entry)) + return 0; + + return fat_open_file(fs, &file_entry); +} + +#endif // !BERTOS_FAT +static void init(void) +{ /* Initialize debugging module (allow kprintf(), etc.) */ kdbg_init(); + /* Initialize system timer */ timer_init(); - /* - * XXX: Arduino has a single UART port that was previously - * initialized for debugging purpose. - * In order to activate the serial driver you should disable - * the debugging module. - */ -#if 0 + + /* Enable all the interrupts */ + IRQ_ENABLE; + + /* Kernel initialization: processes */ + proc_init(); + /* Initialize UART0 */ - ser_init(&out, SER_UART0); - /* Configure UART0 to work at 115.200 bps */ - ser_setbaudrate(&out, 115200); -#else - (void)out; -#endif + ser_init(&ser, SER_UART0); + //FIXME + //ser_setbaudrate(&ser, 115200); + //ser_setbaudrate(&ser, 57600); + //UCSR0A = BV(U2X0); + ser_setbaudrate(&ser, 19200); + /* Initialize LED driver */ LED_INIT(); - /* - * Kernel initialization: processes (allow to create and dispatch - * processes using proc_new()). - */ - proc_init(); -} - -static void NORETURN led_process(void) -{ - int i; + timer_delay(1000); + kfile_printf(&ser.fd, "Hello, world!\r\n"); - /* Periodically blink the led (toggle each 100 ms) */ - for (i = 0; ; i = !i) +#ifdef BERTOS_FAT + ser_init(&spi, SER_SPI); +#else + DDRB |= BV(PORTB0) | BV(PORTB2) | BV(PORTB3) | BV(PORTB5); + if (!sd_raw_init()) { - if (i) - LED_ON(); - else - LED_OFF(); - timer_delay(100); + ASSERT(0); for(;;) {} } +#endif + + fat_init(); } + int main(void) { - /* Hardware initialization */ init(); - /* Create a new child process */ - proc_new(led_process, NULL, KERN_MINSTACKSIZE * 2, NULL); +#ifdef BERTOS_FAT + + FRESULT fat_err; + if ((fat_err = f_mount(0, &fs)) != FR_OK) + { + LOG_ERR("Error mounting FAT volume: %d\n", fat_err); + } + + fat_err = fatfile_open(&file, "foo.txt", FA_WRITE | FA_CREATE_ALWAYS); + ASSERT(fat_err == FR_OK); + + size_t count = kfile_write(&file.fd, "Hello, world!", strlen("Hello, world!")); + ASSERT(count == strlen("Hello, world!")); + +#else + + fat_create_file(dd, "log.txt", &dir_entry); + fd = open_file_in_dir(fs, dd, "log.txt"); + int32_t offset = 0; + fat_seek_file(fd, &offset, FAT_SEEK_END); + +#endif - /* - * The main process is kept to periodically report the stack - * utilization of all the processes (1 probe per second). - */ while (1) { - monitor_report(); - timer_delay(1000); + size_t size; + char buf[128]; + bool sync_pending = false; + + if ((size = ser_read(&ser, buf, sizeof(buf))) != 0) + { + LED_ON(); + fat_write_file(fd, (void *)buf, size); + sync_pending = true; + sprintf(buf, "%d\r\n", size); + ser_write(&ser, buf, strlen(buf)); + LED_OFF(); + } + else + { + serstatus_t err = ser_getstatus(&ser); + if (err & ~SERRF_RXTIMEOUT) + { + LED_ON(); + kfile_printf(&ser.fd, "\r\n*** err=%d ***\r\n", err); + } + ser_setstatus(&ser, 0); + + if (sync_pending) + { + LED_ON(); + sd_raw_sync(); + LED_OFF(); + sync_pending = false; + } + } } + fat_close_file(fd); } diff --git a/rmslog/rmslog.mk b/rmslog/rmslog.mk index ff0f055..ee717fa 100644 --- a/rmslog/rmslog.mk +++ b/rmslog/rmslog.mk @@ -20,9 +20,6 @@ rmslog_HW_PATH = rmslog # Files automatically generated by the wizard. DO NOT EDIT, USE rmslog_USER_CSRC INSTEAD! rmslog_WIZARD_CSRC = \ bertos/mware/event.c \ - bertos/kern/sem.c \ - bertos/kern/monitor.c \ - bertos/struct/heap.c \ bertos/io/kfile.c \ bertos/mware/formatwr.c \ bertos/cpu/avr/drv/timer_avr.c \ @@ -30,10 +27,19 @@ rmslog_WIZARD_CSRC = \ bertos/kern/signal.c \ bertos/kern/proc.c \ bertos/cpu/avr/drv/ser_avr.c \ + bertos/drv/sd.c \ bertos/drv/ser.c \ bertos/mware/hex.c \ + bertos/fs/fat.c \ + bertos/fs/fatfs/ff.c \ + bertos/fs/fatfs/diskio.c \ + bertos/io/kblock.c \ + FAT16/fat.c \ + FAT16/sd_raw.c \ + FAT16/partition.c \ # + # Files automatically generated by the wizard. DO NOT EDIT, USE rmslog_USER_PCSRC INSTEAD! rmslog_WIZARD_PCSRC = \ bertos/mware/formatwr.c \ diff --git a/rmslog/rmslog_user.mk b/rmslog/rmslog_user.mk index b3a1285..4150f7e 100644 --- a/rmslog/rmslog_user.mk +++ b/rmslog/rmslog_user.mk @@ -8,6 +8,7 @@ rmslog_PROGRAMMER_TYPE = avrdude rmslog_PROGRAMMER_PORT = /dev/ttyACM0 flash: $(OUTDIR)/rmslog.hex + sudo killall minicom && sleep 3 || : sudo avrdude -c arduino -p $(rmslog_MCU) -P $(rmslog_PROGRAMMER_PORT) -e -U flash:w:$(OUTDIR)/rmslog.hex # Files included by the user.