From bbd45c46a75edd930486980c0d6a94c52d8cf403 Mon Sep 17 00:00:00 2001 From: Leo C Date: Fri, 15 May 2015 14:59:49 +0200 Subject: [PATCH] fatfs f_mount now allways done globally at start up --- avr/cli.c | 18 ++++---- avr/cmd_fat.c | 102 +++++++++++++++++++-------------------------- avr/cmd_loadcpm3.c | 7 +--- avr/cmd_misc.c | 45 ++++++++++---------- avr/command_tbl.c | 7 ++-- avr/main.c | 21 ++++++---- avr/z180-serv.c | 23 ---------- avr/z80-if.c | 3 +- 8 files changed, 94 insertions(+), 132 deletions(-) diff --git a/avr/cli.c b/avr/cli.c index 52c85db..6b26b6f 100644 --- a/avr/cli.c +++ b/avr/cli.c @@ -27,6 +27,9 @@ #include "con-utils.h" #include "cli.h" + +/* FIXME: Quoting problems */ + #define DEBUG_PARSER 0 /* set to 1 to debug */ #define debug_parser(fmt, args...) \ @@ -46,7 +49,7 @@ static int cli_parse_line(char *line, char *argv[]) inp++) { switch (state) { - case 0: + case 0: /* before arg string, waiting for arg start */ if (isblank(c)) continue; @@ -55,7 +58,7 @@ static int cli_parse_line(char *line, char *argv[]) state = 1; /* fall thru */ - case 1: + case 1: /* in arg string, waiting for end of arg string */ if (c == '\\') { ++state; continue; @@ -71,7 +74,7 @@ static int cli_parse_line(char *line, char *argv[]) } break; - case 3: + case 3: /* in quote */ if (c == '\\' && quote == '\"') { ++state; continue; @@ -82,7 +85,7 @@ static int cli_parse_line(char *line, char *argv[]) } break; - case 2: + case 2: /* waiting for next char */ case 4: --state; break; @@ -252,12 +255,13 @@ static int cli_run_command(const char *cmd, int flag) */ for (inquotes = 0, sep = str; *sep; sep++) { if ((*sep == '\'') && - (*(sep - 1) != '\\')) + (sep != str) && /* past string start */ + (*(sep - 1) != '\\')) /* and NOT escaped */ inquotes = !inquotes; if (!inquotes && - (*sep == ';' || *sep == '\n') && /* separator */ - (sep != str) && /* past string start */ + (*sep == ';' || *sep == '\n') && /* separator */ + (sep != str) && /* past string start */ (*(sep - 1) != '\\')) /* and NOT escaped */ break; } diff --git a/avr/cmd_fat.c b/avr/cmd_fat.c index c5791e5..af2f772 100644 --- a/avr/cmd_fat.c +++ b/avr/cmd_fat.c @@ -157,7 +157,7 @@ FRESULT scan_files ( */ command_ret_t do_fat_stat(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { - FATFS *FatFs, *fs; + FATFS *fs; DWORD nfreeclst; FRESULT res; char *path; @@ -165,64 +165,57 @@ command_ret_t do_fat_stat(cmd_tbl_t *cmdtp, int flag, int argc, char * const arg (void) cmdtp; (void) flag; (void) argc; - FatFs = (FATFS *) malloc(sizeof (FATFS)); path = (char *) malloc(BUFFER_SIZE); - if (FatFs == NULL || path == NULL) { + if (path == NULL) { printf_P(PSTR("fatstat: Out of Memory!\n")); free(path); - free(FatFs); return CMD_RET_FAILURE; } - res = f_mount(FatFs, argv[1], 0); + res = f_getfree(argv[1], &nfreeclst, &fs); if (!res) { - res = f_getfree(argv[1], &nfreeclst, &fs); - if (!res) { - printf_P(PSTR( - "FAT type: %u\n" - "Bytes/Cluster: %lu\n" - "Number of FATs: %u\n" - "Root DIR entries: %u\n" - "Sectors/FAT: %lu\n" - "Number of clusters: %lu\n" - "FAT start (lba): %lu\n" - "DIR start (lba,cluster): %lu\n" - "Data start (lba): %lu\n"), - fs->fs_type, (DWORD)fs->csize * 512, fs->n_fats, - fs->n_rootdir, fs->fsize, fs->n_fatent - 2, - fs->fatbase, fs->dirbase, fs->database); + printf_P(PSTR( + "FAT type: %u\n" + "Bytes/Cluster: %lu\n" + "Number of FATs: %u\n" + "Root DIR entries: %u\n" + "Sectors/FAT: %lu\n" + "Number of clusters: %lu\n" + "FAT start (lba): %lu\n" + "DIR start (lba,cluster): %lu\n" + "Data start (lba): %lu\n"), + fs->fs_type, (DWORD)fs->csize * 512, fs->n_fats, + fs->n_rootdir, fs->fsize, fs->n_fatent - 2, + fs->fatbase, fs->dirbase, fs->database); #if _USE_LABEL - TCHAR label[12]; - DWORD serial; - res = f_getlabel(argv[1], label, &serial); - if (!res) { - printf_P(PSTR( - "Volume name: %s\n" - "Volume S/N: %04X-%04X\n"), - label, (WORD)(serial >> 16), (WORD)(serial & 0xFFFF)); - } + TCHAR label[12]; + DWORD serial; + res = f_getlabel(argv[1], label, &serial); + if (!res) { + printf_P(PSTR( + "Volume name: %s\n" + "Volume S/N: %04X-%04X\n"), + label, (WORD)(serial >> 16), (WORD)(serial & 0xFFFF)); + } #endif - if (!res) { - my_puts_P(PSTR("\nCounting... ")); - statp.AccSize = statp.AccFiles = statp.AccDirs = 0; - strcpy(path, argv[1]); + if (!res) { + my_puts_P(PSTR("\nCounting... ")); + statp.AccSize = statp.AccFiles = statp.AccDirs = 0; + strcpy(path, argv[1]); - res = scan_files(path, &statp); - } - if (!res) { - printf_P(PSTR("\r%u files, %lu bytes.\n%u folders.\n" - "%lu KB total disk space.\n%lu KB available.\n"), - statp.AccFiles, statp.AccSize, statp.AccDirs, - (fs->n_fatent - 2) * (fs->csize / 2), nfreeclst * (fs->csize / 2) - ); - } + res = scan_files(path, &statp); + } + if (!res) { + printf_P(PSTR("\r%u files, %lu bytes.\n%u folders.\n" + "%lu KB total disk space.\n%lu KB available.\n"), + statp.AccFiles, statp.AccSize, statp.AccDirs, + (fs->n_fatent - 2) * (fs->csize / 2), nfreeclst * (fs->csize / 2) + ); } } free(path); - free(FatFs); - f_mount(NULL, argv[1], 0); if (res) { put_rc(res); return CMD_RET_FAILURE; @@ -237,7 +230,7 @@ command_ret_t do_fat_stat(cmd_tbl_t *cmdtp, int flag, int argc, char * const arg */ command_ret_t do_fat_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { - FATFS FatFs, *fs; + FATFS *fs; DIR Dir; /* Directory object */ FILINFO Finfo; unsigned long p1; @@ -251,9 +244,7 @@ command_ret_t do_fat_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[ (void) cmdtp; (void) flag; (void) argc; - res = f_mount(&FatFs, argv[1], 0); - if (!res) - res = f_opendir(&Dir, argv[1]); + res = f_opendir(&Dir, argv[1]); if (res) { put_rc(res); return CMD_RET_FAILURE; @@ -341,7 +332,6 @@ FRESULT mkpath(TCHAR *path) */ command_ret_t do_fat_rw(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { - FATFS *FatFs; FIL File; uint32_t addr; unsigned long bytes; @@ -376,21 +366,15 @@ command_ret_t do_fat_rw(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[ if (addr + bytes > MAX_MEMORY) bytes = MAX_MEMORY - addr; - FatFs = (FATFS *) malloc(sizeof (FATFS)); buffer = (uint8_t *) malloc(BUFFER_SIZE); - if (FatFs == NULL || buffer == NULL) { + if (buffer == NULL) { printf_P(PSTR("fatstat: Out of Memory!\n")); - free(FatFs); free(buffer); return CMD_RET_FAILURE; } - res = f_mount(FatFs, argv[1], 0); - - if (!res) { - if (dowrite) { - res = mkpath(argv[1]); - } + if (dowrite) { + res = mkpath(argv[1]); } if (!res) { res = f_open(&File, argv[1], dowrite ? FA_WRITE | FA_CREATE_ALWAYS @@ -451,11 +435,9 @@ command_ret_t do_fat_rw(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[ bytes_rw, bytes_rw, timer ? (bytes_rw * 1000 / timer) : 0); } } - f_mount(NULL, argv[1], 0); } free(buffer); - free(FatFs); if (buserr) my_puts_P(PSTR("Bus timeout\n")); diff --git a/avr/cmd_loadcpm3.c b/avr/cmd_loadcpm3.c index f2a30f1..fd685b2 100644 --- a/avr/cmd_loadcpm3.c +++ b/avr/cmd_loadcpm3.c @@ -65,7 +65,6 @@ command_ret_t do_loadcpm3(cmd_tbl_t *cmdtp, int flag, int argc, char * const arg uint16_t osentry_adr = 0; long offset = 0; char *fname; - FATFS FatFs; FIL File; /* TODO: put CONFIG_PATH_CPM3SYS in flash */ char default_fname[] = CONFIG_PATH_CPM3SYS; @@ -88,12 +87,9 @@ command_ret_t do_loadcpm3(cmd_tbl_t *cmdtp, int flag, int argc, char * const arg if (fname == NULL || *fname == '\0') fname = default_fname; - res = f_mount(&FatFs, fname, 0); - if (!res) - res = f_open(&File, fname, FA_READ ); + res = f_open(&File, fname, FA_READ ); if (res) { printf_P(PSTR("Error: failed to open '%s'\n"), fname); - f_mount(NULL, fname, 0); return CMD_RET_FAILURE; } @@ -132,7 +128,6 @@ command_ret_t do_loadcpm3(cmd_tbl_t *cmdtp, int flag, int argc, char * const arg out: f_close(&File); - f_mount(NULL, fname, 0); if (res) { printf_P(PSTR("Error: failed to read '%s'\n"), fname); diff --git a/avr/cmd_misc.c b/avr/cmd_misc.c index b29aedd..c9a3c45 100644 --- a/avr/cmd_misc.c +++ b/avr/cmd_misc.c @@ -9,46 +9,43 @@ #include "common.h" #include +#include #include "command.h" #include "timer.h" #include "con-utils.h" +#include "getopt-min.h" command_ret_t do_echo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { - uint_fast8_t putnl = 1; + bool put_newline = true; (void) cmdtp; (void) flag; - for (uint_fast8_t i = 1; i < argc; i++) { + /* reset getopt() */ + optind = 1; + + int opt; + while ((opt = getopt(argc, argv, PSTR("n"))) != -1) { + switch (opt) { + case 'n': + put_newline = false; + break; + default: /* '?' */ + return CMD_RET_USAGE; + } + } - uint_fast8_t backslash = 0; - char *p = argv[i]; - char c; + for (uint_fast8_t i = optind; i < argc; i++) { - if (i != 1) + if (i != optind) putchar(' '); - while ((c = *p++) != '\0') { - - if(backslash) { - backslash = 0; - if (c == 'c') { - putnl = 0; - continue; - } else - putchar('\\'); - } else { - if (c == '\\') { - backslash = 1; - continue; - } - } - putchar(c); - } + + my_puts(argv[i]); } - if (putnl) + if (put_newline) putchar('\n'); return CMD_RET_SUCCESS; diff --git a/avr/command_tbl.c b/avr/command_tbl.c index 238f132..f030b54 100644 --- a/avr/command_tbl.c +++ b/avr/command_tbl.c @@ -76,9 +76,10 @@ CMD_TBL_ITEM( ), CMD_TBL_ITEM( echo, CONFIG_SYS_MAXARGS, 1, do_echo, - "echo args to console", - "[args..]\n" - " - echo args to console; \\c suppresses newline" + "display a line of text", + "[-n] [argument ...]\n" + " - echo the argument(s) to console.\n" + " -n do not output the trailing newline" ), CMD_TBL_ITEM( sleep , 2, 1, do_sleep, diff --git a/avr/main.c b/avr/main.c index 8bffef9..5a0d792 100644 --- a/avr/main.c +++ b/avr/main.c @@ -11,7 +11,7 @@ #include #include "config.h" -#include "debug.h" +#include "ff.h" #include "z80-if.h" #include "i2c.h" #include "con-utils.h" @@ -23,6 +23,7 @@ #include "gpio.h" #include "time.h" #include "rtc.h" +#include "debug.h" static uint8_t mcusr; @@ -138,6 +139,17 @@ void setup_system_time(void) set_system_time(mk_gmtime(&rtc_time) ); } + + +static void setup_fatfs(void) +{ + static FATFS FatFs0; + static FATFS FatFs1; + + f_mount(&FatFs0, "0:", 0); + f_mount(&FatFs1, "1:", 0); +} + /*--------------------------------------------------------------------------*/ /* Stored value of bootdelay, used by autoboot_command() */ @@ -254,14 +266,9 @@ int main(void) print_reset_reason(); #endif -#if DEBUG - unsigned long i_speed = getenv_ulong(PSTR("i2c_clock"), 10, CONFIG_SYS_I2C_CLOCK); - debug("### Setting I2C clock Frequency to %lu Hz.\n", i_speed); - i2c_init(i_speed); -#else i2c_init(CONFIG_SYS_I2C_CLOCK); -#endif setup_system_time(); + setup_fatfs(); printf_P(PSTR("\nATMEGA1281+Z8S180 Stamp Monitor\n\n")); diff --git a/avr/z180-serv.c b/avr/z180-serv.c index c80daeb..22dafe1 100644 --- a/avr/z180-serv.c +++ b/avr/z180-serv.c @@ -104,15 +104,9 @@ void do_msg_echo(uint8_t subf, int len, uint8_t * msg) /* ---------------------------------------------------------------------------*/ -#define MAX_DEVICE 2 #define MAX_DRIVE 2 #define BLOCK_SIZE 512 -struct fat_device_s { - FATFS dd; - bool active; -}; - struct cpm_drive_s { uint8_t drv; @@ -122,7 +116,6 @@ struct cpm_drive_s { }; static uint8_t disk_buffer[BLOCK_SIZE]; -static struct fat_device_s device_table[MAX_DEVICE]; static struct cpm_drive_s drv_table[MAX_DRIVE]; /* @@ -158,17 +151,6 @@ void do_msg_cpm_login(uint8_t subf, int len, uint8_t * msg) goto out; } - /* TODO: this has to be done somewhere globaly */ - - if (!device_table[0].active) { - f_mount(&device_table[0].dd, "0:", 0); - device_table[0].active = true; - } - if (!device_table[1].active) { - f_mount(&device_table[1].dd, "1:", 0); - device_table[1].active = true; - } - /* uint32_t dph = ((uint32_t)msg[4] << 16) + ((uint16_t)msg[3] << 8) + msg[2]; */ @@ -489,11 +471,6 @@ void restart_z180_serv(void) z80_memfifo_init(i, 0); bg_setstat(handle_msg_handling, 0); - f_mount(NULL, "0:", 0); - device_table[0].active = false; - f_mount(NULL, "1:", 0); - device_table[1].active = false; - } /*--------------------------------------------------------------------------*/ diff --git a/avr/z80-if.c b/avr/z80-if.c index d5dc2d7..21ffeac 100644 --- a/avr/z80-if.c +++ b/avr/z80-if.c @@ -560,10 +560,9 @@ void z80_memfifo_init(const fifo_t f, uint32_t addr) { fifo_dsc[f].base = addr; - if (addr != 0) { - DBG_P(2, "z80_memfifo_init: %i, %lx\n", f, addr); + if (addr != 0) { z80_bus_cmd(Request); fifo_dsc[f].mask = z80_read(addr + FIFO_BUFSIZE_MASK); fifo_dsc[f].idx_in = z80_read(addr + FIFO_INDEX_IN); -- 2.39.2