]> cloudbase.mooo.com Git - z180-stamp.git/commitdiff
fat commands are now subcommands of a single fat command. Added 'fat cd' and 'fat...
authorLeo C <erbl259-lmu@yahoo.de>
Tue, 3 Apr 2018 22:08:52 +0000 (00:08 +0200)
committerLeo C <erbl259-lmu@yahoo.de>
Tue, 3 Apr 2018 22:08:52 +0000 (00:08 +0200)
avr/cmd_fat.c
avr/cmd_sd.c
avr/command_tbl.c
avr/main.c
include/cmd_fat.h [new file with mode: 0644]
include/config.h

index fcf5cbce3e38fcf2ad4cb35e5f15151a2e44c57e..81ee945ce3969e846c1e0e0057c4e4f358e2a0e5 100644 (file)
@@ -8,6 +8,7 @@
  * FAT filesystem commands
  */
 
+#include "cmd_fat.h"
 #include "common.h"
 #include <string.h>
 #include <stdbool.h>
 #include "time.h"
 #include "timer.h"
 #include "debug.h"
+#include "env.h"
 
 /* TODO: use memory size test function (cmd_mem.c) */
 #define MAX_MEMORY     (1ul << 19)
 #define BUFFER_SIZE    512
 
 
+FATFS FatFs0;
+FATFS FatFs1;
+
+char chur_drv[3];
+
 DWORD get_fattime (void)
 {
        time_t timer;
@@ -87,6 +94,13 @@ void put_rc (FRESULT rc)
 }
 
 
+void setup_fatfs(void)
+{
+       f_mount(&FatFs0, "0:", 0);
+       f_mount(&FatFs1, "1:", 0);
+}
+
+
 static void swirl(void)
 {
        static const FLASH char swirlchar[] = { '-','\\','|','/' };
@@ -155,7 +169,7 @@ FRESULT scan_files (
  * fatstat path - Show logical drive status
  *
  */
-command_ret_t do_fat_stat(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+command_ret_t do_stat(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
        FATFS *fs;
        DWORD nfreeclst;
@@ -223,24 +237,109 @@ command_ret_t do_fat_stat(cmd_tbl_t *cmdtp, int flag, int argc, char * const arg
        return CMD_RET_SUCCESS;
 }
 
+/*
+ * pwd - Print current directory of the current drive.
+ *
+ */
+command_ret_t do_pwd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+       FRESULT res;
+       char *buf;
+
+       (void) cmdtp; (void) flag; (void) argc; (void) argv;
+
+       buf = (char *) malloc(BUFFER_SIZE);
+       if (buf == NULL) {
+               printf_P(PSTR("pwd: Out of Memory!\n"));
+               free(buf);
+               return CMD_RET_FAILURE;
+       }
+
+       res = f_getcwd(buf, BUFFER_SIZE);  /* Get current directory path */
+
+       if (!res) {
+               puts(buf);
+       }
+       free(buf);
+       if (res) {
+               put_rc(res);
+               return CMD_RET_FAILURE;
+       }
+       return CMD_RET_SUCCESS;
+}
+
+
+/*
+ * cd - Change the current/working directory.
+ *
+ */
+command_ret_t do_cd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+       char *arg;
+       FRESULT res = 0;
+
+       (void) cmdtp; (void) flag; (void) argc;
+
+       if (argc < 2) {
+               arg = getenv_str(PSTR("HOME"));
+               if (arg == NULL) {
+                       printf_P(PSTR("%s: \"%S\" is not set\n"), argv[0], PSTR("HOME"));
+                       return CMD_RET_FAILURE;
+               }
+       } else
+               arg = argv[1];
+
+       if (arg[1] == ':') {
+               char drv[3];
+               drv[2] = '\0';
+               drv[1] = ':';
+               drv[0] = arg[0];
+               res = f_chdrive(drv);
+       }
+       if (!res) {
+               res = f_chdir(arg);
+       }
+
+       if (res) {
+               put_rc(res);
+               return CMD_RET_FAILURE;
+       }
+       return CMD_RET_SUCCESS;
+}
+
 
 /*
  * fatls path - Directory listing
  *
  */
-command_ret_t do_fat_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+command_ret_t do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
        FATFS *fs;
        DIR Dir;                                        /* Directory object */
        FILINFO Finfo;
        unsigned long p1;
        unsigned int s1, s2;
-       FRESULT res;
+       FRESULT res = FR_OK;
+       char *buf;
 
        (void) cmdtp; (void) flag; (void) argc;
 
-       res = f_opendir(&Dir, argv[1]);
-       if (res) {
+       buf = (char *) malloc(BUFFER_SIZE);
+       if (buf == NULL) {
+               printf_P(PSTR("pwd: Out of Memory!\n"));
+               free(buf);
+               return CMD_RET_FAILURE;
+       }
+
+       if (argc < 2)
+               res = f_getcwd(buf, BUFFER_SIZE);  /* Get current directory path */
+       else
+               strncpy(buf, argv[1], BUFFER_SIZE);
+
+       if (res == FR_OK)
+               res = f_opendir(&Dir, buf);
+       if (res != FR_OK) {
+               free(buf);
                put_rc(res);
                return CMD_RET_FAILURE;
        }
@@ -270,10 +369,11 @@ command_ret_t do_fat_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[
 
        if (res == FR_OK) {
                printf_P(PSTR("%4u File(s),%10lu bytes total\n%4u Dir(s)"), s1, p1, s2);
-               if (f_getfree(argv[1], (DWORD*)&p1, &fs) == FR_OK)
+               if (f_getfree(buf, (DWORD*)&p1, &fs) == FR_OK)
                        printf_P(PSTR(", %10luK bytes free\n"), p1 * fs->csize / 2);
        }
 
+       free(buf);
        if (res) {
                put_rc(res);
                return CMD_RET_FAILURE;
@@ -316,7 +416,7 @@ FRESULT mkpath(TCHAR *path)
  *             read  <d:/path/filename> <addr> [bytes [pos]]
  *             write <d:/path/filename> <addr> <bytes>
  */
-command_ret_t do_fat_rw(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+command_ret_t do_rw(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
        FIL File;
        uint32_t addr;
@@ -434,3 +534,85 @@ command_ret_t do_fat_rw(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[
 
        return CMD_RET_SUCCESS;
 }
+
+
+static
+command_ret_t do_help(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
+
+cmd_tbl_t cmd_fat_sub[] = {
+CMD_TBL_ITEM(
+       stat,   2,      1,      do_stat,
+       "Show logical drive status",
+       "dev"
+),
+CMD_TBL_ITEM(
+       pwd,    2,      1,      do_pwd,
+       "Print name of current/working directory",
+       ""
+),
+CMD_TBL_ITEM(
+       cd,     2,      1,      do_cd,
+       "Change the current/working directory.",
+       "path"
+),
+CMD_TBL_ITEM(
+       ls,     2,      1,      do_ls,
+       "Directory listing",
+       "path"
+),
+CMD_TBL_ITEM(
+       load,   5,      0,      do_rw,
+       "load binary file from a dos filesystem",
+       "<d:/path/filename> <addr> [bytes [pos]]\n"
+       "    - Load binary file 'path/filename' on logical drive 'd'\n"
+       "      to address 'addr' from dos filesystem.\n"
+       "      'pos' gives the file position to start loading from.\n"
+       "      If 'pos' is omitted, 0 is used. 'pos' requires 'bytes'.\n"
+       "      'bytes' gives the size to load. If 'bytes' is 0 or omitted,\n"
+       "      the load stops on end of file."
+),
+CMD_TBL_ITEM(
+       write,  4,      0,      do_rw,
+       "write file into a dos filesystem",
+       "<d:/path/filename> <addr> <bytes>\n"
+       "    - Write file to 'path/filename' on logical drive 'd' from RAM\n"
+       "      starting at address 'addr'.\n"
+),
+
+/* This does not use the CMD_TBL_ITEM macro as ? can't be used in symbol names */
+       {FSTR("?"),   CONFIG_SYS_MAXARGS, 1, do_help,
+        FSTR("Alias for 'help'"),
+#ifdef  CONFIG_SYS_LONGHELP
+       FSTR(""),
+#endif /* CONFIG_SYS_LONGHELP */
+#ifdef CONFIG_AUTO_COMPLETE
+       0,
+#endif
+},
+};
+
+static
+command_ret_t do_help(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+       return _do_help(cmd_fat_sub, ARRAY_SIZE(cmd_fat_sub), cmdtp, flag, argc, argv);
+}
+
+
+command_ret_t do_fat(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+       cmd_tbl_t *cp;
+
+       if (argc < 2)
+               return CMD_RET_USAGE;
+
+       /* drop initial "fat" arg */
+       argc--;
+       argv++;
+
+       cp = find_cmd_tbl(argv[0], cmd_fat_sub, ARRAY_SIZE(cmd_fat_sub));
+
+       if (cp)
+               return cp->cmd(cmdtp, flag, argc, argv);
+
+       return CMD_RET_USAGE;
+}
index b84c4be758ec94b0d6797479c92ad56062dd7fc6..1963d59cd58f8e48dd81dfedb12b897d2186121e 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
  *
- * SPDX-License-Identifier:    GPL-2.0+
+ * SPDX-License-Identifier:    GPL-2.0
  */
 
 #include "common.h"
index ed14db9e259ff18d48ef7460118a29781a2711f0..95b6a5919084f004822e10fe68230c7c4abab20a 100644 (file)
@@ -33,9 +33,7 @@ extern command_ret_t do_busreq_pulse(cmd_tbl_t *, int, int, char * const []);
 extern command_ret_t do_date(cmd_tbl_t *, int, int, char * const []);
 extern command_ret_t do_gpio(cmd_tbl_t *, int, int, char * const []);
 extern command_ret_t do_sd(cmd_tbl_t *, int, int, char * const []);
-extern command_ret_t do_fat_stat(cmd_tbl_t *, int, int, char * const []);
-extern command_ret_t do_fat_ls(cmd_tbl_t *, int, int, char * const []);
-extern command_ret_t do_fat_rw(cmd_tbl_t *, int, int, char * const []);
+extern command_ret_t do_fat(cmd_tbl_t *, int, int, char * const []);
 extern command_ret_t do_run(cmd_tbl_t *, int, int, char * const []);
 extern command_ret_t do_source(cmd_tbl_t *, int, int, char * const []);
 extern command_ret_t do_attach(cmd_tbl_t *, int, int, char * const []);
@@ -346,35 +344,14 @@ CMD_TBL_ITEM(
        "sd help\n"
        "    - print help on subcommands"
 ),
-
-CMD_TBL_ITEM(
-       fatstat,        2,      1,      do_fat_stat,
-       "Show logical drive status",
-       "dev"
-),
-CMD_TBL_ITEM(
-       fatls,  2,      1,      do_fat_ls,
-       "Directory listing",
-       "path"
-),
 CMD_TBL_ITEM(
-       fatload,        5,      0,      do_fat_rw,
-       "load binary file from a dos filesystem",
-       "<d:/path/filename> <addr> [bytes [pos]]\n"
-       "    - Load binary file 'path/filename' on logical drive 'd'\n"
-       "      to address 'addr' from dos filesystem.\n"
-       "      'pos' gives the file position to start loading from.\n"
-       "      If 'pos' is omitted, 0 is used. 'pos' requires 'bytes'.\n"
-       "      'bytes' gives the size to load. If 'bytes' is 0 or omitted,\n"
-       "      the load stops on end of file."
-),
-CMD_TBL_ITEM(
-       fatwrite,       4,      0,      do_fat_rw,
-       "write file into a dos filesystem",
-       "<d:/path/filename> <addr> <bytes>\n"
-       "    - Write file to 'path/filename' on logical drive 'd' from RAM\n"
-       "      starting at address 'addr'.\n"
+       fat,   CONFIG_SYS_MAXARGS, 1, do_fat,
+       "fat filesystem commands",
+       "<subcommand> args ...\n"
+       "fat help\n"
+       "    - print help on subcommands"
 ),
+
 CMD_TBL_ITEM(
        attach, CONFIG_SYS_MAXARGS,     1,      do_attach,
        "attach filesystem image file to CP/M drive",
index 86dcc50caa6c506476e333ed06434c5bb1509552..1e5a6f97f94df4eb1610f8a8b3e0e2839904356f 100644 (file)
@@ -24,6 +24,8 @@
 #include "time.h"
 #include "rtc.h"
 #include "debug.h"
+#include "cmd_fat.h"
+
 
 uint8_t mcusr __attribute__ ((section (".noinit")));
 
@@ -146,16 +148,6 @@ void setup_system_time(void)
 }
 
 
-
-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() */
diff --git a/include/cmd_fat.h b/include/cmd_fat.h
new file mode 100644 (file)
index 0000000..66be559
--- /dev/null
@@ -0,0 +1,12 @@
+/*
+ * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
+ *
+ * SPDX-License-Identifier:    GPL-2.0
+ */
+
+#ifndef CMD_FAT_H
+#define CMD_FAT_H
+
+void setup_fatfs(void);
+
+#endif /* CMD_FAT_H */
index 8d7f348028eeb6afefea6900e92474abeaf42172..2b677093ec5c225a58f13656d1dc946b880ffec1 100644 (file)
@@ -29,7 +29,7 @@
 
 #define CONFIG_ENV_SIZE                        1600
 #define CONFIG_ENV_OFFSET              0
-#define CONFIG_ENVVAR_MAX              30
+#define CONFIG_ENVVAR_MAX              40
 
 #define CONFIG_BAUDRATE                        115200L
 #define CONFIG_PWRON_DELAY             2000    /* ms to wait after power on */