![](/media/cache/profiles/f0fcf351df4eb6786e9bb6fc4e2dee02.jpg.50x50_q85.jpg)
SD-Card Control Program / Using Micro-SD / based on SDCardTest Program (http://mbed.org/users/simon/programs/SDCardTest/gpdz4x/)
Dependencies: mbed SDFileSystem
Please refer following my Notebook page.
/users/kenjiArai/notebook/sd-card-control-new/#
FatFs_Mon/mon.cpp@7:4467fbfa888b, 2020-05-02 (annotated)
- Committer:
- kenjiArai
- Date:
- Sat May 02 03:45:17 2020 +0000
- Revision:
- 7:4467fbfa888b
- Parent:
- 5:e222c59853e2
changed starting process(wait key input)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenjiArai | 2:1397a54382ec | 1 | /* |
kenjiArai | 2:1397a54382ec | 2 | * mbed Application program for the mbed |
kenjiArai | 2:1397a54382ec | 3 | * FatFs Check program / monitor part |
kenjiArai | 2:1397a54382ec | 4 | * |
kenjiArai | 7:4467fbfa888b | 5 | * Copyright (c) 2015,'18,'19,'20 Kenji Arai / JH1PJL |
kenjiArai | 7:4467fbfa888b | 6 | * http://www7b.biglobe.ne.jp/~kenjia/ |
kenjiArai | 2:1397a54382ec | 7 | * https://os.mbed.com/users/kenjiArai/ |
kenjiArai | 2:1397a54382ec | 8 | * Created: May 5th, 2015 |
kenjiArai | 2:1397a54382ec | 9 | * Revised: June 14th, 2015 |
kenjiArai | 2:1397a54382ec | 10 | * Revised: April 7th, 2018 |
kenjiArai | 7:4467fbfa888b | 11 | * Revised: May 2nd, 2020 |
kenjiArai | 2:1397a54382ec | 12 | */ |
kenjiArai | 2:1397a54382ec | 13 | |
kenjiArai | 2:1397a54382ec | 14 | /* |
kenjiArai | 2:1397a54382ec | 15 | *---------------- REFERENCE --------------------------------------------------- |
kenjiArai | 2:1397a54382ec | 16 | * Original Source Information |
kenjiArai | 2:1397a54382ec | 17 | * FatFs sample program |
kenjiArai | 2:1397a54382ec | 18 | * ChaN FatFs http://elm-chan.org/ |
kenjiArai | 2:1397a54382ec | 19 | * http://elm-chan.org/fsw/ff/00index_e.html |
kenjiArai | 2:1397a54382ec | 20 | */ |
kenjiArai | 2:1397a54382ec | 21 | /*----------------------------------------------------------------------*/ |
kenjiArai | 2:1397a54382ec | 22 | /* FAT file system sample project for FatFs (C)ChaN, 2016 */ |
kenjiArai | 2:1397a54382ec | 23 | /*----------------------------------------------------------------------*/ |
kenjiArai | 2:1397a54382ec | 24 | |
kenjiArai | 2:1397a54382ec | 25 | // Include -------------------------------------------------------------------- |
kenjiArai | 2:1397a54382ec | 26 | #include "mbed.h" |
kenjiArai | 2:1397a54382ec | 27 | #if (MBED_MAJOR_VERSION == 2) |
kenjiArai | 2:1397a54382ec | 28 | #include "SDFileSystem.h" |
kenjiArai | 2:1397a54382ec | 29 | #elif (MBED_MAJOR_VERSION == 5) |
kenjiArai | 2:1397a54382ec | 30 | #include "FATFileSystem.h" |
kenjiArai | 2:1397a54382ec | 31 | #endif |
kenjiArai | 2:1397a54382ec | 32 | #include "ff.h" |
kenjiArai | 2:1397a54382ec | 33 | #include "ffconf.h" |
kenjiArai | 2:1397a54382ec | 34 | #include "diskio.h" |
kenjiArai | 2:1397a54382ec | 35 | #include "mon.h" |
kenjiArai | 2:1397a54382ec | 36 | |
kenjiArai | 2:1397a54382ec | 37 | // Definition ----------------------------------------------------------------- |
kenjiArai | 2:1397a54382ec | 38 | #define DO_DEBUG 0 |
kenjiArai | 2:1397a54382ec | 39 | |
kenjiArai | 2:1397a54382ec | 40 | #if DO_DEBUG |
kenjiArai | 2:1397a54382ec | 41 | #define DEBUG_LINE pc.printf("line:%d\r\n", __LINE__); |
kenjiArai | 2:1397a54382ec | 42 | #else |
kenjiArai | 2:1397a54382ec | 43 | #define DEBUG_LINE {;} |
kenjiArai | 2:1397a54382ec | 44 | #endif |
kenjiArai | 2:1397a54382ec | 45 | |
kenjiArai | 2:1397a54382ec | 46 | // Com |
kenjiArai | 2:1397a54382ec | 47 | #if 1 |
kenjiArai | 2:1397a54382ec | 48 | #define BAUD(x) pc.baud(x) |
kenjiArai | 2:1397a54382ec | 49 | #define GETC(x) pc.getc(x) |
kenjiArai | 2:1397a54382ec | 50 | #define PUTC(x) pc.putc(x) |
kenjiArai | 2:1397a54382ec | 51 | #define PUTS(x) pc.puts(x) |
kenjiArai | 2:1397a54382ec | 52 | #define PRINTF(...) pc.printf(__VA_ARGS__) |
kenjiArai | 2:1397a54382ec | 53 | #define READABLE(x) pc.readable(x) |
kenjiArai | 2:1397a54382ec | 54 | #else |
kenjiArai | 2:1397a54382ec | 55 | #define BAUD(x) {;} |
kenjiArai | 2:1397a54382ec | 56 | #define GETC(x) {;} |
kenjiArai | 2:1397a54382ec | 57 | #define PUTC(x) {;} |
kenjiArai | 2:1397a54382ec | 58 | #define PRINTF(...) {;} |
kenjiArai | 2:1397a54382ec | 59 | #define READABLE(x) {;} |
kenjiArai | 2:1397a54382ec | 60 | #endif |
kenjiArai | 2:1397a54382ec | 61 | |
kenjiArai | 2:1397a54382ec | 62 | #define UTC_JST_OFFSET (32400) // +9 hours |
kenjiArai | 2:1397a54382ec | 63 | |
kenjiArai | 2:1397a54382ec | 64 | // from ffconf.h |
kenjiArai | 2:1397a54382ec | 65 | #define _VOLUMES 1 |
kenjiArai | 2:1397a54382ec | 66 | |
kenjiArai | 2:1397a54382ec | 67 | #if !defined(FF_FS_RPATH) |
kenjiArai | 2:1397a54382ec | 68 | #define FF_FS_RPATH 0 |
kenjiArai | 2:1397a54382ec | 69 | #endif |
kenjiArai | 2:1397a54382ec | 70 | |
kenjiArai | 2:1397a54382ec | 71 | #define DW_CHAR sizeof(char) |
kenjiArai | 2:1397a54382ec | 72 | #define DW_SHORT sizeof(short) |
kenjiArai | 2:1397a54382ec | 73 | #define DW_LONG sizeof(long) |
kenjiArai | 2:1397a54382ec | 74 | |
kenjiArai | 2:1397a54382ec | 75 | /* These types must be 16-bit, 32-bit or larger integer */ |
kenjiArai | 2:1397a54382ec | 76 | typedef int INT; |
kenjiArai | 2:1397a54382ec | 77 | typedef unsigned int UINT; |
kenjiArai | 2:1397a54382ec | 78 | |
kenjiArai | 2:1397a54382ec | 79 | /* These types must be 8-bit integer */ |
kenjiArai | 2:1397a54382ec | 80 | typedef char CHAR; |
kenjiArai | 2:1397a54382ec | 81 | typedef unsigned char UCHAR; |
kenjiArai | 2:1397a54382ec | 82 | typedef unsigned char BYTE; |
kenjiArai | 2:1397a54382ec | 83 | |
kenjiArai | 2:1397a54382ec | 84 | /* These types must be 16-bit integer */ |
kenjiArai | 2:1397a54382ec | 85 | typedef short SHORT; |
kenjiArai | 2:1397a54382ec | 86 | typedef unsigned short USHORT; |
kenjiArai | 2:1397a54382ec | 87 | typedef unsigned short WORD; |
kenjiArai | 2:1397a54382ec | 88 | typedef unsigned short WCHAR; |
kenjiArai | 2:1397a54382ec | 89 | |
kenjiArai | 2:1397a54382ec | 90 | /* These types must be 32-bit integer */ |
kenjiArai | 2:1397a54382ec | 91 | typedef long LONG; |
kenjiArai | 2:1397a54382ec | 92 | typedef unsigned long ULONG; |
kenjiArai | 2:1397a54382ec | 93 | typedef unsigned long DWORD; |
kenjiArai | 2:1397a54382ec | 94 | /* by Kenji Arai / JH1PJL September 10th, 2012 */ |
kenjiArai | 2:1397a54382ec | 95 | typedef unsigned long long DDWORD; |
kenjiArai | 2:1397a54382ec | 96 | |
kenjiArai | 2:1397a54382ec | 97 | // RAM ------------------------------------------------------------------------ |
kenjiArai | 2:1397a54382ec | 98 | BYTE Buff[4096]; |
kenjiArai | 2:1397a54382ec | 99 | char Linebuf[128]; // Console input buffer |
kenjiArai | 2:1397a54382ec | 100 | FATFS Fatfs[_VOLUMES]; // File system object for each logical drive |
kenjiArai | 2:1397a54382ec | 101 | FIL File1, File2; // File objects |
kenjiArai | 2:1397a54382ec | 102 | FATFS_DIR* Dirx; |
kenjiArai | 2:1397a54382ec | 103 | FILINFO Finfo; |
kenjiArai | 2:1397a54382ec | 104 | DWORD AccSize; // Work register for fs command |
kenjiArai | 2:1397a54382ec | 105 | WORD AccFiles, AccDirs; |
kenjiArai | 2:1397a54382ec | 106 | |
kenjiArai | 2:1397a54382ec | 107 | // ROM / Constant data -------------------------------------------------------- |
kenjiArai | 4:fc36c8ec2966 | 108 | const char *const monmsg0 = "Start monitor program for FatFs File System\r\n"; |
kenjiArai | 4:fc36c8ec2966 | 109 | const char *const monmsg1 = " <Please press any key to start the monitor>"; |
kenjiArai | 2:1397a54382ec | 110 | |
kenjiArai | 2:1397a54382ec | 111 | static const char HelpMsg0[] = |
kenjiArai | 2:1397a54382ec | 112 | "dir <full_pass>\r\n" |
kenjiArai | 2:1397a54382ec | 113 | "type <file_name>\r\n" |
kenjiArai | 2:1397a54382ec | 114 | "vol\r\n" |
kenjiArai | 2:1397a54382ec | 115 | "ren <org_file_name> <new_file_name>\r\n" |
kenjiArai | 2:1397a54382ec | 116 | "copy <file_name> <file_name>\r\n" |
kenjiArai | 2:1397a54382ec | 117 | "mkdir <dir_name>\r\n" |
kenjiArai | 2:1397a54382ec | 118 | "cd <dir_name>\r\n" |
kenjiArai | 2:1397a54382ec | 119 | "x extend commands mode\r\n" |
kenjiArai | 2:1397a54382ec | 120 | "q Return to main\r\n" |
kenjiArai | 2:1397a54382ec | 121 | "t Show current time or Adjust time\r\n" |
kenjiArai | 7:4467fbfa888b | 122 | " e.g. t 20 5 2 10 11 12 -> May 02,'20, 10:11:12\r\n" |
kenjiArai | 2:1397a54382ec | 123 | "? Help/You know the command\r\n" |
kenjiArai | 2:1397a54382ec | 124 | "\r\n"; |
kenjiArai | 2:1397a54382ec | 125 | |
kenjiArai | 2:1397a54382ec | 126 | static const char HelpMsg1[] = |
kenjiArai | 2:1397a54382ec | 127 | "[File system controls]\r\n" |
kenjiArai | 2:1397a54382ec | 128 | " fi <ld#> [<mount>]- Force initialized the volume\r\n" |
kenjiArai | 2:1397a54382ec | 129 | " fs [<path>] - Show volume status\r\n" |
kenjiArai | 2:1397a54382ec | 130 | " fl [<path>] - Show a directory\r\n" |
kenjiArai | 2:1397a54382ec | 131 | " fo <mode> <file> - Open a file\r\n" |
kenjiArai | 2:1397a54382ec | 132 | " <mode> Read=1, Write=2\r\n" |
kenjiArai | 2:1397a54382ec | 133 | " fc - Close the file\r\n" |
kenjiArai | 2:1397a54382ec | 134 | " fe <ofs> - Move fp in normal seek\r\n" |
kenjiArai | 2:1397a54382ec | 135 | " fd <len> - Read and dump the file\r\n" |
kenjiArai | 2:1397a54382ec | 136 | " fr <len> - Read the file\r\n" |
kenjiArai | 2:1397a54382ec | 137 | " fw <len> <val> - Write to the file\r\n" |
kenjiArai | 2:1397a54382ec | 138 | " fn <org.name> <new.name> - Rename an object\r\n" |
kenjiArai | 2:1397a54382ec | 139 | " fu <name> - Unlink an object\r\n" |
kenjiArai | 2:1397a54382ec | 140 | " fv - Truncate the file at current fp\r\n" |
kenjiArai | 2:1397a54382ec | 141 | " fk <name> - Create a directory\r\n" |
kenjiArai | 2:1397a54382ec | 142 | " fa <atrr> <mask> <object name> - Change attribute of an object\r\n" |
kenjiArai | 2:1397a54382ec | 143 | " ft <year> <month> <day> <hour> <min> <sec> <name>" |
kenjiArai | 2:1397a54382ec | 144 | " - Change timestamp of an object\r\n" |
kenjiArai | 2:1397a54382ec | 145 | " fx <src.file> <dst.file> - Copy a file\r\n" |
kenjiArai | 2:1397a54382ec | 146 | " fg <path> - Change current directory\r\n" |
kenjiArai | 2:1397a54382ec | 147 | " fq - Show current directory\r\n" |
kenjiArai | 2:1397a54382ec | 148 | " fb <name> - Set volume label\r\n" |
kenjiArai | 2:1397a54382ec | 149 | " fm <ld#> <type> <csize> - Create file system\r\n" |
kenjiArai | 2:1397a54382ec | 150 | " fz [<len>] - Change/Show R/W length for fr/fw/fx command\r\n" |
kenjiArai | 2:1397a54382ec | 151 | "[Disk contorls]\r\n" |
kenjiArai | 2:1397a54382ec | 152 | " di <pd#> - Initialize disk\r\n" |
kenjiArai | 2:1397a54382ec | 153 | " dd [<pd#> <lba>] - Dump a secrtor\r\n" |
kenjiArai | 2:1397a54382ec | 154 | " ds <pd#> - Show disk status\r\n" |
kenjiArai | 2:1397a54382ec | 155 | "[Buffer controls]\r\n" |
kenjiArai | 2:1397a54382ec | 156 | " bd <ofs> - Dump working buffer\r\n" |
kenjiArai | 2:1397a54382ec | 157 | " be <ofs> [<data>] ... - Edit working buffer\r\n" |
kenjiArai | 2:1397a54382ec | 158 | " br <pd#> <lba> [<count>] - Read disk into working buffer\r\n" |
kenjiArai | 2:1397a54382ec | 159 | " bw <pd#> <lba> [<count>] - Write working buffer into disk\r\n" |
kenjiArai | 2:1397a54382ec | 160 | " bf <val> - Fill working buffer\r\n" |
kenjiArai | 2:1397a54382ec | 161 | "[Misc commands]\r\n" |
kenjiArai | 2:1397a54382ec | 162 | " q Return\r\n" |
kenjiArai | 2:1397a54382ec | 163 | " ? Help\r\n" |
kenjiArai | 2:1397a54382ec | 164 | "\r\n"; |
kenjiArai | 2:1397a54382ec | 165 | |
kenjiArai | 2:1397a54382ec | 166 | // Function prototypes -------------------------------------------------------- |
kenjiArai | 2:1397a54382ec | 167 | #if (MBED_MAJOR_VERSION == 2) |
kenjiArai | 2:1397a54382ec | 168 | extern SDFileSystem fs; |
kenjiArai | 2:1397a54382ec | 169 | #elif (MBED_MAJOR_VERSION == 5) |
kenjiArai | 2:1397a54382ec | 170 | extern HeapBlockDevice bd; |
kenjiArai | 2:1397a54382ec | 171 | extern FATFileSystem fs; |
kenjiArai | 2:1397a54382ec | 172 | #endif |
kenjiArai | 2:1397a54382ec | 173 | |
kenjiArai | 2:1397a54382ec | 174 | static void extended_mon( char *ptr ); |
kenjiArai | 2:1397a54382ec | 175 | static void v_next( char *ptr ); |
kenjiArai | 2:1397a54382ec | 176 | static void d_next( char *ptr ); |
kenjiArai | 2:1397a54382ec | 177 | static void c_next( char *ptr ); |
kenjiArai | 2:1397a54382ec | 178 | static void m_next( char *ptr ); |
kenjiArai | 2:1397a54382ec | 179 | static void r_next( char *ptr ); |
kenjiArai | 2:1397a54382ec | 180 | static void t_next( char *ptr ); |
kenjiArai | 2:1397a54382ec | 181 | static void memory_inf(char *ptr); |
kenjiArai | 2:1397a54382ec | 182 | static void disk_inf(char *ptr); |
kenjiArai | 2:1397a54382ec | 183 | |
kenjiArai | 2:1397a54382ec | 184 | static void crlf( void ); |
kenjiArai | 2:1397a54382ec | 185 | static FRESULT scan_files( char* path ); |
kenjiArai | 2:1397a54382ec | 186 | static void put_rc( FRESULT rc ); |
kenjiArai | 2:1397a54382ec | 187 | static void file_inf( char *ptr ); |
kenjiArai | 2:1397a54382ec | 188 | static void put_dump( void* buff, unsigned long addr, int len, int width ); |
kenjiArai | 2:1397a54382ec | 189 | static void chk_and_set_time(char *ptr); |
kenjiArai | 2:1397a54382ec | 190 | static int xatoi ( char **str, long *res ); |
kenjiArai | 2:1397a54382ec | 191 | |
kenjiArai | 2:1397a54382ec | 192 | void get_line (char *buff, int len); |
kenjiArai | 2:1397a54382ec | 193 | |
kenjiArai | 2:1397a54382ec | 194 | // Object --------------------------------------------------------------------- |
kenjiArai | 2:1397a54382ec | 195 | extern Serial pc; |
kenjiArai | 2:1397a54382ec | 196 | Timer t; |
kenjiArai | 2:1397a54382ec | 197 | |
kenjiArai | 2:1397a54382ec | 198 | //------------------------------------------------------------------------------ |
kenjiArai | 2:1397a54382ec | 199 | // Control Program |
kenjiArai | 2:1397a54382ec | 200 | //------------------------------------------------------------------------------ |
kenjiArai | 2:1397a54382ec | 201 | // Monitor program for File control |
kenjiArai | 2:1397a54382ec | 202 | void mon () |
kenjiArai | 2:1397a54382ec | 203 | { |
kenjiArai | 2:1397a54382ec | 204 | char *ptr; |
kenjiArai | 2:1397a54382ec | 205 | |
kenjiArai | 2:1397a54382ec | 206 | Dirx = new FATFS_DIR; |
kenjiArai | 2:1397a54382ec | 207 | /* Open Uart to communicate with Host PC */ |
kenjiArai | 2:1397a54382ec | 208 | PUTS(monmsg0); |
kenjiArai | 2:1397a54382ec | 209 | PUTS(monmsg1); |
kenjiArai | 2:1397a54382ec | 210 | crlf(); |
kenjiArai | 2:1397a54382ec | 211 | for (;;) { |
kenjiArai | 2:1397a54382ec | 212 | DEBUG_LINE |
kenjiArai | 2:1397a54382ec | 213 | PUTC('>'); |
kenjiArai | 2:1397a54382ec | 214 | ptr = Linebuf; |
kenjiArai | 2:1397a54382ec | 215 | get_line( ptr, sizeof(Linebuf) ); |
kenjiArai | 2:1397a54382ec | 216 | switch ( *ptr++ ) { |
kenjiArai | 2:1397a54382ec | 217 | // vol |
kenjiArai | 2:1397a54382ec | 218 | case 'v' : |
kenjiArai | 2:1397a54382ec | 219 | v_next(ptr); |
kenjiArai | 2:1397a54382ec | 220 | break; |
kenjiArai | 2:1397a54382ec | 221 | // dir |
kenjiArai | 2:1397a54382ec | 222 | case 'd' : |
kenjiArai | 2:1397a54382ec | 223 | d_next(ptr); |
kenjiArai | 2:1397a54382ec | 224 | break; |
kenjiArai | 2:1397a54382ec | 225 | // cd, copy |
kenjiArai | 2:1397a54382ec | 226 | case 'c' : |
kenjiArai | 2:1397a54382ec | 227 | c_next(ptr); |
kenjiArai | 2:1397a54382ec | 228 | break; |
kenjiArai | 2:1397a54382ec | 229 | // mkdir |
kenjiArai | 2:1397a54382ec | 230 | case 'm' : |
kenjiArai | 2:1397a54382ec | 231 | m_next(ptr); |
kenjiArai | 2:1397a54382ec | 232 | break; |
kenjiArai | 2:1397a54382ec | 233 | // ren |
kenjiArai | 2:1397a54382ec | 234 | case 'r' : |
kenjiArai | 2:1397a54382ec | 235 | r_next(ptr); |
kenjiArai | 2:1397a54382ec | 236 | break; |
kenjiArai | 2:1397a54382ec | 237 | case 't' : |
kenjiArai | 2:1397a54382ec | 238 | t_next(ptr); |
kenjiArai | 2:1397a54382ec | 239 | break; |
kenjiArai | 2:1397a54382ec | 240 | case 'x' : |
kenjiArai | 2:1397a54382ec | 241 | extended_mon(ptr); |
kenjiArai | 2:1397a54382ec | 242 | break; |
kenjiArai | 2:1397a54382ec | 243 | // Help |
kenjiArai | 2:1397a54382ec | 244 | case '?' : |
kenjiArai | 2:1397a54382ec | 245 | PUTS(HelpMsg0); |
kenjiArai | 2:1397a54382ec | 246 | break; |
kenjiArai | 2:1397a54382ec | 247 | // Exit monitor (return to main()) |
kenjiArai | 2:1397a54382ec | 248 | case 'q' : |
kenjiArai | 2:1397a54382ec | 249 | PUTS("Return to main\r\n"); |
kenjiArai | 2:1397a54382ec | 250 | return; |
kenjiArai | 2:1397a54382ec | 251 | // Not a command |
kenjiArai | 2:1397a54382ec | 252 | default: |
kenjiArai | 2:1397a54382ec | 253 | PUTS("? [HELP]=?"); |
kenjiArai | 2:1397a54382ec | 254 | crlf(); |
kenjiArai | 2:1397a54382ec | 255 | break; |
kenjiArai | 2:1397a54382ec | 256 | } |
kenjiArai | 2:1397a54382ec | 257 | } |
kenjiArai | 2:1397a54382ec | 258 | } |
kenjiArai | 2:1397a54382ec | 259 | |
kenjiArai | 2:1397a54382ec | 260 | uint32_t get_disk_freespace(void) |
kenjiArai | 2:1397a54382ec | 261 | { |
kenjiArai | 2:1397a54382ec | 262 | long p1; |
kenjiArai | 2:1397a54382ec | 263 | UINT s1, s2; |
kenjiArai | 2:1397a54382ec | 264 | FATFS *fs; |
kenjiArai | 2:1397a54382ec | 265 | BYTE res; |
kenjiArai | 2:1397a54382ec | 266 | |
kenjiArai | 2:1397a54382ec | 267 | if (Dirx == NULL){ |
kenjiArai | 2:1397a54382ec | 268 | Dirx = new FATFS_DIR; |
kenjiArai | 2:1397a54382ec | 269 | } |
kenjiArai | 2:1397a54382ec | 270 | char p = NULL; |
kenjiArai | 2:1397a54382ec | 271 | res = f_opendir(Dirx, &p); |
kenjiArai | 2:1397a54382ec | 272 | if (res) { |
kenjiArai | 2:1397a54382ec | 273 | return 0; |
kenjiArai | 2:1397a54382ec | 274 | } |
kenjiArai | 2:1397a54382ec | 275 | p1 = s1 = s2 = 0; |
kenjiArai | 2:1397a54382ec | 276 | for(;;) { |
kenjiArai | 2:1397a54382ec | 277 | res = f_readdir(Dirx, &Finfo); |
kenjiArai | 2:1397a54382ec | 278 | if ((res != FR_OK) || !Finfo.fname[0]) break; |
kenjiArai | 2:1397a54382ec | 279 | if (Finfo.fattrib & AM_DIR) { |
kenjiArai | 2:1397a54382ec | 280 | s2++; |
kenjiArai | 2:1397a54382ec | 281 | } else { |
kenjiArai | 2:1397a54382ec | 282 | s1++; |
kenjiArai | 2:1397a54382ec | 283 | p1 += Finfo.fsize; |
kenjiArai | 2:1397a54382ec | 284 | } |
kenjiArai | 2:1397a54382ec | 285 | } |
kenjiArai | 2:1397a54382ec | 286 | res = f_getfree(&p, (DWORD*)&p1, &fs); |
kenjiArai | 2:1397a54382ec | 287 | uint32_t size = p1 * fs->csize * 512; |
kenjiArai | 2:1397a54382ec | 288 | if (res == FR_OK) { |
kenjiArai | 2:1397a54382ec | 289 | return size; |
kenjiArai | 2:1397a54382ec | 290 | } else { |
kenjiArai | 2:1397a54382ec | 291 | return 0; |
kenjiArai | 2:1397a54382ec | 292 | } |
kenjiArai | 2:1397a54382ec | 293 | } |
kenjiArai | 2:1397a54382ec | 294 | |
kenjiArai | 5:e222c59853e2 | 295 | uint32_t get_data_file_size(const char *const file_name) |
kenjiArai | 5:e222c59853e2 | 296 | { |
kenjiArai | 5:e222c59853e2 | 297 | BYTE res; |
kenjiArai | 5:e222c59853e2 | 298 | //const char *file_name ="acc_data.txt"; |
kenjiArai | 5:e222c59853e2 | 299 | |
kenjiArai | 5:e222c59853e2 | 300 | if (Dirx == NULL){ |
kenjiArai | 5:e222c59853e2 | 301 | Dirx = new FATFS_DIR; |
kenjiArai | 5:e222c59853e2 | 302 | } |
kenjiArai | 5:e222c59853e2 | 303 | char p = NULL; |
kenjiArai | 5:e222c59853e2 | 304 | res = f_opendir(Dirx, &p); |
kenjiArai | 5:e222c59853e2 | 305 | if (res) { |
kenjiArai | 5:e222c59853e2 | 306 | put_rc((FRESULT)res); |
kenjiArai | 5:e222c59853e2 | 307 | return 0; |
kenjiArai | 5:e222c59853e2 | 308 | } |
kenjiArai | 5:e222c59853e2 | 309 | for(;;) { |
kenjiArai | 5:e222c59853e2 | 310 | res = f_readdir(Dirx, &Finfo); |
kenjiArai | 5:e222c59853e2 | 311 | if ((res != FR_OK) || !Finfo.fname[0]) break; |
kenjiArai | 5:e222c59853e2 | 312 | if (strcmp(Finfo.fname, file_name) == 0){ |
kenjiArai | 5:e222c59853e2 | 313 | return Finfo.fsize; |
kenjiArai | 5:e222c59853e2 | 314 | } |
kenjiArai | 5:e222c59853e2 | 315 | } |
kenjiArai | 5:e222c59853e2 | 316 | return 0; |
kenjiArai | 5:e222c59853e2 | 317 | } |
kenjiArai | 5:e222c59853e2 | 318 | |
kenjiArai | 2:1397a54382ec | 319 | static void extended_mon( char *ptr ) |
kenjiArai | 2:1397a54382ec | 320 | { |
kenjiArai | 2:1397a54382ec | 321 | PUTS(HelpMsg1); |
kenjiArai | 2:1397a54382ec | 322 | while(true) { |
kenjiArai | 2:1397a54382ec | 323 | PUTS("e>"); |
kenjiArai | 2:1397a54382ec | 324 | ptr = Linebuf; |
kenjiArai | 2:1397a54382ec | 325 | get_line( ptr, sizeof(Linebuf) ); |
kenjiArai | 2:1397a54382ec | 326 | switch ( *ptr++ ) { |
kenjiArai | 2:1397a54382ec | 327 | case 'f' : |
kenjiArai | 2:1397a54382ec | 328 | DEBUG_LINE; |
kenjiArai | 2:1397a54382ec | 329 | file_inf(ptr); |
kenjiArai | 2:1397a54382ec | 330 | break; |
kenjiArai | 2:1397a54382ec | 331 | case 'd' : |
kenjiArai | 2:1397a54382ec | 332 | DEBUG_LINE; |
kenjiArai | 2:1397a54382ec | 333 | disk_inf(ptr); |
kenjiArai | 2:1397a54382ec | 334 | break; |
kenjiArai | 2:1397a54382ec | 335 | case 'm' : |
kenjiArai | 2:1397a54382ec | 336 | DEBUG_LINE; |
kenjiArai | 2:1397a54382ec | 337 | memory_inf(ptr); |
kenjiArai | 2:1397a54382ec | 338 | break; |
kenjiArai | 2:1397a54382ec | 339 | case '?' : |
kenjiArai | 2:1397a54382ec | 340 | DEBUG_LINE; |
kenjiArai | 2:1397a54382ec | 341 | PUTS(HelpMsg1); |
kenjiArai | 2:1397a54382ec | 342 | break; |
kenjiArai | 2:1397a54382ec | 343 | case 'q' : |
kenjiArai | 2:1397a54382ec | 344 | DEBUG_LINE; |
kenjiArai | 2:1397a54382ec | 345 | return; |
kenjiArai | 2:1397a54382ec | 346 | default: |
kenjiArai | 2:1397a54382ec | 347 | PUTS( "?\r\n" ); |
kenjiArai | 2:1397a54382ec | 348 | } |
kenjiArai | 2:1397a54382ec | 349 | } |
kenjiArai | 2:1397a54382ec | 350 | } |
kenjiArai | 2:1397a54382ec | 351 | |
kenjiArai | 2:1397a54382ec | 352 | //------------------------------------------------------------------------------ |
kenjiArai | 2:1397a54382ec | 353 | // General monitor functions |
kenjiArai | 2:1397a54382ec | 354 | static void v_next( char *ptr ) |
kenjiArai | 2:1397a54382ec | 355 | { |
kenjiArai | 2:1397a54382ec | 356 | switch ( *ptr++ ) { |
kenjiArai | 2:1397a54382ec | 357 | case 'o' : |
kenjiArai | 2:1397a54382ec | 358 | if (*ptr == 'l') { |
kenjiArai | 2:1397a54382ec | 359 | *ptr = 's'; |
kenjiArai | 2:1397a54382ec | 360 | file_inf(ptr); // fs [<path>] - Show volume status |
kenjiArai | 2:1397a54382ec | 361 | } |
kenjiArai | 2:1397a54382ec | 362 | break; |
kenjiArai | 2:1397a54382ec | 363 | default: |
kenjiArai | 2:1397a54382ec | 364 | PUTS( "?\r\n" ); |
kenjiArai | 2:1397a54382ec | 365 | } |
kenjiArai | 2:1397a54382ec | 366 | } |
kenjiArai | 2:1397a54382ec | 367 | |
kenjiArai | 2:1397a54382ec | 368 | static void d_next(char *ptr) |
kenjiArai | 2:1397a54382ec | 369 | { |
kenjiArai | 2:1397a54382ec | 370 | switch ( *ptr++ ) { |
kenjiArai | 2:1397a54382ec | 371 | case 'i' : |
kenjiArai | 2:1397a54382ec | 372 | if (*ptr == 'r') { |
kenjiArai | 2:1397a54382ec | 373 | *ptr = 'l'; |
kenjiArai | 2:1397a54382ec | 374 | file_inf(ptr); // fl [<path>] - Directory listing |
kenjiArai | 2:1397a54382ec | 375 | } |
kenjiArai | 2:1397a54382ec | 376 | break; |
kenjiArai | 2:1397a54382ec | 377 | default: |
kenjiArai | 2:1397a54382ec | 378 | PUTS( "?\r\n" ); |
kenjiArai | 2:1397a54382ec | 379 | } |
kenjiArai | 2:1397a54382ec | 380 | } |
kenjiArai | 2:1397a54382ec | 381 | |
kenjiArai | 2:1397a54382ec | 382 | static void c_next(char *ptr) |
kenjiArai | 2:1397a54382ec | 383 | { |
kenjiArai | 2:1397a54382ec | 384 | switch ( *ptr++ ) { |
kenjiArai | 2:1397a54382ec | 385 | case 'o' : |
kenjiArai | 2:1397a54382ec | 386 | if ((*ptr == 'p') && (*(ptr + 1) == 'y')) { |
kenjiArai | 2:1397a54382ec | 387 | ptr++; |
kenjiArai | 2:1397a54382ec | 388 | *ptr = 'x'; |
kenjiArai | 2:1397a54382ec | 389 | file_inf(ptr); // fx <src_name> <dst_name> - Copy file |
kenjiArai | 2:1397a54382ec | 390 | } |
kenjiArai | 2:1397a54382ec | 391 | break; |
kenjiArai | 2:1397a54382ec | 392 | case 'd' : |
kenjiArai | 2:1397a54382ec | 393 | *ptr = 'g'; |
kenjiArai | 2:1397a54382ec | 394 | file_inf(ptr); // fx <src_name> <dst_name> - Copy file |
kenjiArai | 2:1397a54382ec | 395 | break; |
kenjiArai | 2:1397a54382ec | 396 | default: |
kenjiArai | 2:1397a54382ec | 397 | PUTS( "?\r\n" ); |
kenjiArai | 2:1397a54382ec | 398 | } |
kenjiArai | 2:1397a54382ec | 399 | } |
kenjiArai | 2:1397a54382ec | 400 | |
kenjiArai | 2:1397a54382ec | 401 | static void m_next(char *ptr) |
kenjiArai | 2:1397a54382ec | 402 | { |
kenjiArai | 2:1397a54382ec | 403 | switch ( *ptr++ ) { |
kenjiArai | 2:1397a54382ec | 404 | case 'k' : |
kenjiArai | 2:1397a54382ec | 405 | if ((*ptr == 'd') && (*(ptr + 1) == 'i') && (*(ptr + 2) == 'r')) { |
kenjiArai | 2:1397a54382ec | 406 | ptr += 2; |
kenjiArai | 2:1397a54382ec | 407 | *ptr = 'k'; |
kenjiArai | 2:1397a54382ec | 408 | file_inf(ptr); // fk <name> - Create a directory |
kenjiArai | 2:1397a54382ec | 409 | } |
kenjiArai | 2:1397a54382ec | 410 | break; |
kenjiArai | 2:1397a54382ec | 411 | default: |
kenjiArai | 2:1397a54382ec | 412 | PUTS("?\r\n"); |
kenjiArai | 2:1397a54382ec | 413 | } |
kenjiArai | 2:1397a54382ec | 414 | } |
kenjiArai | 2:1397a54382ec | 415 | |
kenjiArai | 2:1397a54382ec | 416 | static void r_next(char *ptr) |
kenjiArai | 2:1397a54382ec | 417 | { |
kenjiArai | 2:1397a54382ec | 418 | switch (*ptr++) { |
kenjiArai | 2:1397a54382ec | 419 | case 'e' : |
kenjiArai | 2:1397a54382ec | 420 | if (*ptr == 'n') { |
kenjiArai | 2:1397a54382ec | 421 | // fn <old_name> <new_name> - Change file/dir name |
kenjiArai | 2:1397a54382ec | 422 | file_inf(ptr); |
kenjiArai | 2:1397a54382ec | 423 | } |
kenjiArai | 2:1397a54382ec | 424 | break; |
kenjiArai | 2:1397a54382ec | 425 | default: |
kenjiArai | 2:1397a54382ec | 426 | PUTS("?\r\n"); |
kenjiArai | 2:1397a54382ec | 427 | } |
kenjiArai | 2:1397a54382ec | 428 | } |
kenjiArai | 2:1397a54382ec | 429 | |
kenjiArai | 2:1397a54382ec | 430 | static void t_next(char *ptr) |
kenjiArai | 2:1397a54382ec | 431 | { |
kenjiArai | 2:1397a54382ec | 432 | switch (*ptr++) { |
kenjiArai | 2:1397a54382ec | 433 | case ' ' : |
kenjiArai | 2:1397a54382ec | 434 | case 0x0d: |
kenjiArai | 2:1397a54382ec | 435 | chk_and_set_time(ptr); |
kenjiArai | 2:1397a54382ec | 436 | case 'y' : |
kenjiArai | 2:1397a54382ec | 437 | if ((*ptr == 'p') && (*(ptr + 1) == 'e')) { |
kenjiArai | 2:1397a54382ec | 438 | ptr++; |
kenjiArai | 2:1397a54382ec | 439 | *ptr = '&'; |
kenjiArai | 2:1397a54382ec | 440 | file_inf(ptr); |
kenjiArai | 2:1397a54382ec | 441 | } |
kenjiArai | 2:1397a54382ec | 442 | break; |
kenjiArai | 2:1397a54382ec | 443 | default: |
kenjiArai | 2:1397a54382ec | 444 | PUTS("?\r\n"); |
kenjiArai | 2:1397a54382ec | 445 | } |
kenjiArai | 2:1397a54382ec | 446 | } |
kenjiArai | 2:1397a54382ec | 447 | |
kenjiArai | 2:1397a54382ec | 448 | static FRESULT scan_files ( |
kenjiArai | 2:1397a54382ec | 449 | char* path /* Pointer to the path name working buffer */ |
kenjiArai | 2:1397a54382ec | 450 | ) |
kenjiArai | 2:1397a54382ec | 451 | { |
kenjiArai | 2:1397a54382ec | 452 | FATFS_DIR dirs; |
kenjiArai | 2:1397a54382ec | 453 | FRESULT res; |
kenjiArai | 2:1397a54382ec | 454 | BYTE i; |
kenjiArai | 2:1397a54382ec | 455 | char *fn; |
kenjiArai | 2:1397a54382ec | 456 | |
kenjiArai | 2:1397a54382ec | 457 | if ((res = f_opendir(&dirs, path)) == FR_OK) { |
kenjiArai | 2:1397a54382ec | 458 | i = strlen(path); |
kenjiArai | 2:1397a54382ec | 459 | PRINTF("path: %s, n=%u\r\n", path, i); |
kenjiArai | 2:1397a54382ec | 460 | while (((res = f_readdir(&dirs, &Finfo)) == FR_OK) && Finfo.fname[0]) { |
kenjiArai | 2:1397a54382ec | 461 | if (FF_FS_RPATH && Finfo.fname[0] == '.') { |
kenjiArai | 2:1397a54382ec | 462 | continue; |
kenjiArai | 2:1397a54382ec | 463 | } |
kenjiArai | 2:1397a54382ec | 464 | fn = Finfo.fname; |
kenjiArai | 2:1397a54382ec | 465 | if (Finfo.fattrib & AM_DIR) { |
kenjiArai | 2:1397a54382ec | 466 | AccDirs++; |
kenjiArai | 2:1397a54382ec | 467 | *(path+i) = '/'; |
kenjiArai | 2:1397a54382ec | 468 | strcpy(path+i+1, fn); |
kenjiArai | 2:1397a54382ec | 469 | res = scan_files(path); |
kenjiArai | 2:1397a54382ec | 470 | *(path+i) = '\0'; |
kenjiArai | 2:1397a54382ec | 471 | if (res != FR_OK) break; |
kenjiArai | 2:1397a54382ec | 472 | } else { |
kenjiArai | 2:1397a54382ec | 473 | PRINTF("%s/%s\r\n", path, fn); |
kenjiArai | 2:1397a54382ec | 474 | AccFiles++; |
kenjiArai | 2:1397a54382ec | 475 | AccSize += Finfo.fsize; |
kenjiArai | 2:1397a54382ec | 476 | } |
kenjiArai | 2:1397a54382ec | 477 | } |
kenjiArai | 2:1397a54382ec | 478 | } |
kenjiArai | 2:1397a54382ec | 479 | return res; |
kenjiArai | 2:1397a54382ec | 480 | } |
kenjiArai | 2:1397a54382ec | 481 | |
kenjiArai | 2:1397a54382ec | 482 | static void put_rc (FRESULT rc) |
kenjiArai | 2:1397a54382ec | 483 | { |
kenjiArai | 2:1397a54382ec | 484 | const char *str = |
kenjiArai | 2:1397a54382ec | 485 | "OK\0" "DISK_ERR\0" "INT_ERR\0" "NOT_READY\0" "NO_FILE\0" "NO_PATH\0" |
kenjiArai | 2:1397a54382ec | 486 | "INVALID_NAME\0" "DENIED\0" "EXIST\0" "INVALID_OBJECT\0" |
kenjiArai | 2:1397a54382ec | 487 | "WRITE_PROTECTED\0" "INVALID_DRIVE\0" "NOT_ENABLED\0" |
kenjiArai | 2:1397a54382ec | 488 | "NO_FILE_SYSTEM\0" "MKFS_ABORTED\0" "TIMEOUT\0" |
kenjiArai | 2:1397a54382ec | 489 | "LOCKED\0" "NOT_ENOUGH_CORE\0" "TOO_MANY_OPEN_FILES\0"; |
kenjiArai | 2:1397a54382ec | 490 | int i; |
kenjiArai | 2:1397a54382ec | 491 | |
kenjiArai | 2:1397a54382ec | 492 | for ( i = 0; i != rc && *str; i++ ) { |
kenjiArai | 2:1397a54382ec | 493 | while ( *str++ ) { |
kenjiArai | 2:1397a54382ec | 494 | ; |
kenjiArai | 2:1397a54382ec | 495 | } |
kenjiArai | 2:1397a54382ec | 496 | } |
kenjiArai | 2:1397a54382ec | 497 | PRINTF( "rc=%u FR_%s\r\n", (UINT)rc, str ); |
kenjiArai | 2:1397a54382ec | 498 | } |
kenjiArai | 2:1397a54382ec | 499 | |
kenjiArai | 2:1397a54382ec | 500 | static void file_inf(char *ptr) |
kenjiArai | 2:1397a54382ec | 501 | { |
kenjiArai | 2:1397a54382ec | 502 | long p1, p2, p3; |
kenjiArai | 2:1397a54382ec | 503 | CHAR *ptr2; |
kenjiArai | 2:1397a54382ec | 504 | BYTE f_res; |
kenjiArai | 2:1397a54382ec | 505 | UINT s1, s2, cnt, blen = sizeof Buff; |
kenjiArai | 2:1397a54382ec | 506 | FATFS *fs; |
kenjiArai | 2:1397a54382ec | 507 | static const BYTE ft[] = {0, 12, 16, 32}; |
kenjiArai | 2:1397a54382ec | 508 | BYTE res; |
kenjiArai | 2:1397a54382ec | 509 | DWORD ofs = 0; |
kenjiArai | 2:1397a54382ec | 510 | uint32_t tim; |
kenjiArai | 2:1397a54382ec | 511 | |
kenjiArai | 2:1397a54382ec | 512 | switch (*ptr++) { |
kenjiArai | 2:1397a54382ec | 513 | case '&' : |
kenjiArai | 2:1397a54382ec | 514 | DEBUG_LINE; |
kenjiArai | 2:1397a54382ec | 515 | while (*ptr == ' ') ptr++; |
kenjiArai | 2:1397a54382ec | 516 | /* Open a file */ |
kenjiArai | 2:1397a54382ec | 517 | f_res = f_open(&File1, ptr, FA_READ); |
kenjiArai | 2:1397a54382ec | 518 | if ( f_res ) { |
kenjiArai | 2:1397a54382ec | 519 | put_rc((FRESULT)f_res); |
kenjiArai | 2:1397a54382ec | 520 | break; |
kenjiArai | 2:1397a54382ec | 521 | } |
kenjiArai | 2:1397a54382ec | 522 | DEBUG_LINE; |
kenjiArai | 2:1397a54382ec | 523 | /* Read all lines and display it */ |
kenjiArai | 2:1397a54382ec | 524 | while(true) { |
kenjiArai | 2:1397a54382ec | 525 | f_res = f_read(&File1, (TCHAR*)Buff, blen, &cnt); |
kenjiArai | 2:1397a54382ec | 526 | if ( f_res ) { |
kenjiArai | 2:1397a54382ec | 527 | put_rc((FRESULT)f_res); |
kenjiArai | 2:1397a54382ec | 528 | break; |
kenjiArai | 2:1397a54382ec | 529 | } |
kenjiArai | 2:1397a54382ec | 530 | for (s1 = 0; s1 < cnt; s1++) { |
kenjiArai | 2:1397a54382ec | 531 | PUTC(Buff[s1]); |
kenjiArai | 2:1397a54382ec | 532 | } |
kenjiArai | 2:1397a54382ec | 533 | if (cnt != blen) { |
kenjiArai | 2:1397a54382ec | 534 | break; |
kenjiArai | 2:1397a54382ec | 535 | } |
kenjiArai | 2:1397a54382ec | 536 | } |
kenjiArai | 2:1397a54382ec | 537 | DEBUG_LINE; |
kenjiArai | 2:1397a54382ec | 538 | /* Close the file */ |
kenjiArai | 2:1397a54382ec | 539 | f_close(&File1); |
kenjiArai | 2:1397a54382ec | 540 | break; |
kenjiArai | 2:1397a54382ec | 541 | |
kenjiArai | 2:1397a54382ec | 542 | case 'i' : /* fi [<opt>]- Initialize logical drive */ |
kenjiArai | 2:1397a54382ec | 543 | if ( !xatoi(&ptr, &p1) ) { |
kenjiArai | 2:1397a54382ec | 544 | break; |
kenjiArai | 2:1397a54382ec | 545 | } |
kenjiArai | 2:1397a54382ec | 546 | if (!xatoi(&ptr, &p2)) p2 = 0; |
kenjiArai | 2:1397a54382ec | 547 | put_rc(f_mount(&Fatfs[p1], (const TCHAR*)p1, 0)); |
kenjiArai | 2:1397a54382ec | 548 | break; |
kenjiArai | 2:1397a54382ec | 549 | |
kenjiArai | 2:1397a54382ec | 550 | case 's' : /* fs [<path>] - Show volume status */ |
kenjiArai | 2:1397a54382ec | 551 | f_res = f_getfree( ptr, (DWORD*)&p2, &fs ); |
kenjiArai | 2:1397a54382ec | 552 | if ( f_res ) { |
kenjiArai | 2:1397a54382ec | 553 | put_rc((FRESULT)f_res); |
kenjiArai | 2:1397a54382ec | 554 | break; |
kenjiArai | 2:1397a54382ec | 555 | } |
kenjiArai | 2:1397a54382ec | 556 | PRINTF |
kenjiArai | 2:1397a54382ec | 557 | ( |
kenjiArai | 2:1397a54382ec | 558 | "\rFAT type = FAT%u\r\nBytes/Cluster" |
kenjiArai | 2:1397a54382ec | 559 | " = %lu\r\nNumber of FATs = %u\r\n" |
kenjiArai | 2:1397a54382ec | 560 | "Root DIR entries = %u\r\n" |
kenjiArai | 2:1397a54382ec | 561 | "Sectors/FAT = %lu\r\n" |
kenjiArai | 2:1397a54382ec | 562 | "Number of clusters = %lu\r\n" |
kenjiArai | 2:1397a54382ec | 563 | "FAT start (lba) = %lu\r\n" |
kenjiArai | 2:1397a54382ec | 564 | "DIR start (lba,clustor) = %lu\r\n" |
kenjiArai | 2:1397a54382ec | 565 | "Data start (lba) = %lu\r\n", |
kenjiArai | 2:1397a54382ec | 566 | ft[fs->fs_type & 3], (DWORD)fs->csize * 512, fs->n_fats, |
kenjiArai | 2:1397a54382ec | 567 | fs->n_rootdir, fs->fsize, (DWORD)fs->n_fatent - 2, |
kenjiArai | 2:1397a54382ec | 568 | fs->fatbase, fs->dirbase, fs->database |
kenjiArai | 2:1397a54382ec | 569 | ); |
kenjiArai | 2:1397a54382ec | 570 | AccSize = AccFiles = AccDirs = 0; |
kenjiArai | 2:1397a54382ec | 571 | break; |
kenjiArai | 2:1397a54382ec | 572 | case 'l' : /* fl [<path>] - Directory listing */ |
kenjiArai | 2:1397a54382ec | 573 | while (*ptr == ' ') ptr++; |
kenjiArai | 2:1397a54382ec | 574 | res = f_opendir(Dirx, ptr); |
kenjiArai | 2:1397a54382ec | 575 | if (res) { |
kenjiArai | 2:1397a54382ec | 576 | put_rc((FRESULT)res); |
kenjiArai | 2:1397a54382ec | 577 | break; |
kenjiArai | 2:1397a54382ec | 578 | } |
kenjiArai | 2:1397a54382ec | 579 | p1 = s1 = s2 = 0; |
kenjiArai | 2:1397a54382ec | 580 | for(;;) { |
kenjiArai | 2:1397a54382ec | 581 | res = f_readdir(Dirx, &Finfo); |
kenjiArai | 2:1397a54382ec | 582 | if ((res != FR_OK) || !Finfo.fname[0]) break; |
kenjiArai | 2:1397a54382ec | 583 | if (Finfo.fattrib & AM_DIR) { |
kenjiArai | 2:1397a54382ec | 584 | s2++; |
kenjiArai | 2:1397a54382ec | 585 | } else { |
kenjiArai | 2:1397a54382ec | 586 | s1++; |
kenjiArai | 2:1397a54382ec | 587 | p1 += Finfo.fsize; |
kenjiArai | 2:1397a54382ec | 588 | } |
kenjiArai | 2:1397a54382ec | 589 | PRINTF("%c%c%c%c%c %u/%02u/%02u %02u:%02u %9lu %s\r\n", |
kenjiArai | 2:1397a54382ec | 590 | (Finfo.fattrib & AM_DIR) ? 'D' : '-', |
kenjiArai | 2:1397a54382ec | 591 | (Finfo.fattrib & AM_RDO) ? 'R' : '-', |
kenjiArai | 2:1397a54382ec | 592 | (Finfo.fattrib & AM_HID) ? 'H' : '-', |
kenjiArai | 2:1397a54382ec | 593 | (Finfo.fattrib & AM_SYS) ? 'S' : '-', |
kenjiArai | 2:1397a54382ec | 594 | (Finfo.fattrib & AM_ARC) ? 'A' : '-', |
kenjiArai | 2:1397a54382ec | 595 | (Finfo.fdate >> 9) + 1980, (Finfo.fdate >> 5) & 15, |
kenjiArai | 2:1397a54382ec | 596 | Finfo.fdate & 31, |
kenjiArai | 2:1397a54382ec | 597 | (Finfo.ftime >> 11), (Finfo.ftime >> 5) & 63, |
kenjiArai | 2:1397a54382ec | 598 | Finfo.fsize, Finfo.fname); |
kenjiArai | 2:1397a54382ec | 599 | } |
kenjiArai | 2:1397a54382ec | 600 | #if 0 // f_getfree cannnot count under Dir, subdirectory area |
kenjiArai | 2:1397a54382ec | 601 | PRINTF("%4u File(s),%10lu bytes total\r\n%4u Dir(s)", s1, p1, s2); |
kenjiArai | 2:1397a54382ec | 602 | res = f_getfree(ptr, (DWORD*)&p1, &fs); |
kenjiArai | 2:1397a54382ec | 603 | if (res == FR_OK) |
kenjiArai | 2:1397a54382ec | 604 | PRINTF(", %10lu bytes free\r\n", p1 * fs->csize * 512); |
kenjiArai | 2:1397a54382ec | 605 | else |
kenjiArai | 2:1397a54382ec | 606 | put_rc((FRESULT)res); |
kenjiArai | 2:1397a54382ec | 607 | #else |
kenjiArai | 2:1397a54382ec | 608 | PRINTF("%4u File(s) = %10lu bytes total, %4u Dir(s)\r\n", |
kenjiArai | 2:1397a54382ec | 609 | s1, p1, s2); |
kenjiArai | 2:1397a54382ec | 610 | #endif |
kenjiArai | 2:1397a54382ec | 611 | break; |
kenjiArai | 2:1397a54382ec | 612 | |
kenjiArai | 2:1397a54382ec | 613 | case 'o' : /* fo <mode> <file> - Open a file */ |
kenjiArai | 2:1397a54382ec | 614 | if (!xatoi(&ptr, &p1)) break; |
kenjiArai | 2:1397a54382ec | 615 | while (*ptr == ' ') ptr++; |
kenjiArai | 2:1397a54382ec | 616 | put_rc(f_open(&File1, ptr, (BYTE)p1)); |
kenjiArai | 2:1397a54382ec | 617 | #if 0 |
kenjiArai | 2:1397a54382ec | 618 | put_rc(f_open(&File1, "savedata.txt", 1)); |
kenjiArai | 2:1397a54382ec | 619 | PRINTF("Open savedata.txt as read mode\r\n"); |
kenjiArai | 2:1397a54382ec | 620 | #endif |
kenjiArai | 2:1397a54382ec | 621 | break; |
kenjiArai | 2:1397a54382ec | 622 | |
kenjiArai | 2:1397a54382ec | 623 | case 'c' : /* fc - Close a file */ |
kenjiArai | 2:1397a54382ec | 624 | put_rc(f_close(&File1)); |
kenjiArai | 2:1397a54382ec | 625 | break; |
kenjiArai | 2:1397a54382ec | 626 | |
kenjiArai | 2:1397a54382ec | 627 | case 'e' : /* fe - Seek file pointer */ |
kenjiArai | 2:1397a54382ec | 628 | if (!xatoi(&ptr, &p1)) break; |
kenjiArai | 2:1397a54382ec | 629 | res = f_lseek(&File1, p1); |
kenjiArai | 2:1397a54382ec | 630 | put_rc((FRESULT)res); |
kenjiArai | 2:1397a54382ec | 631 | if (res == FR_OK) |
kenjiArai | 2:1397a54382ec | 632 | PRINTF("fptr=%lu(0x%lX)\r\n", File1.fptr, File1.fptr); |
kenjiArai | 2:1397a54382ec | 633 | break; |
kenjiArai | 2:1397a54382ec | 634 | |
kenjiArai | 2:1397a54382ec | 635 | case 'd' : /* fd <len> - read and dump file from current fp */ |
kenjiArai | 2:1397a54382ec | 636 | if (!xatoi(&ptr, &p1)) break; |
kenjiArai | 2:1397a54382ec | 637 | ofs = File1.fptr; |
kenjiArai | 2:1397a54382ec | 638 | while (p1) { |
kenjiArai | 2:1397a54382ec | 639 | if ((UINT)p1 >= 16) { |
kenjiArai | 2:1397a54382ec | 640 | cnt = 16; |
kenjiArai | 2:1397a54382ec | 641 | p1 -= 16; |
kenjiArai | 2:1397a54382ec | 642 | } else { |
kenjiArai | 2:1397a54382ec | 643 | cnt = p1; |
kenjiArai | 2:1397a54382ec | 644 | p1 = 0; |
kenjiArai | 2:1397a54382ec | 645 | } |
kenjiArai | 2:1397a54382ec | 646 | res = f_read(&File1, Buff, cnt, &cnt); |
kenjiArai | 2:1397a54382ec | 647 | if (res != FR_OK) { |
kenjiArai | 2:1397a54382ec | 648 | put_rc((FRESULT)res); |
kenjiArai | 2:1397a54382ec | 649 | break; |
kenjiArai | 2:1397a54382ec | 650 | } |
kenjiArai | 2:1397a54382ec | 651 | if (!cnt) break; |
kenjiArai | 2:1397a54382ec | 652 | put_dump(Buff, ofs, cnt, DW_CHAR); |
kenjiArai | 2:1397a54382ec | 653 | ofs += 16; |
kenjiArai | 2:1397a54382ec | 654 | } |
kenjiArai | 2:1397a54382ec | 655 | break; |
kenjiArai | 2:1397a54382ec | 656 | |
kenjiArai | 2:1397a54382ec | 657 | case 'r' : /* fr <len> - read file */ |
kenjiArai | 2:1397a54382ec | 658 | if (!xatoi(&ptr, &p1)) break; |
kenjiArai | 2:1397a54382ec | 659 | p2 = 0; |
kenjiArai | 2:1397a54382ec | 660 | t.reset(); |
kenjiArai | 2:1397a54382ec | 661 | t.start(); |
kenjiArai | 2:1397a54382ec | 662 | while (p1) { |
kenjiArai | 2:1397a54382ec | 663 | if ((UINT)p1 >= blen) { |
kenjiArai | 2:1397a54382ec | 664 | cnt = blen; |
kenjiArai | 2:1397a54382ec | 665 | p1 -= blen; |
kenjiArai | 2:1397a54382ec | 666 | } else { |
kenjiArai | 2:1397a54382ec | 667 | cnt = p1; |
kenjiArai | 2:1397a54382ec | 668 | p1 = 0; |
kenjiArai | 2:1397a54382ec | 669 | } |
kenjiArai | 2:1397a54382ec | 670 | res = f_read(&File1, Buff, cnt, &s2); |
kenjiArai | 2:1397a54382ec | 671 | if (res != FR_OK) { |
kenjiArai | 2:1397a54382ec | 672 | put_rc((FRESULT)res); |
kenjiArai | 2:1397a54382ec | 673 | break; |
kenjiArai | 2:1397a54382ec | 674 | } |
kenjiArai | 2:1397a54382ec | 675 | p2 += s2; |
kenjiArai | 2:1397a54382ec | 676 | if (cnt != s2) break; |
kenjiArai | 2:1397a54382ec | 677 | } |
kenjiArai | 2:1397a54382ec | 678 | tim = t.read_ms(); |
kenjiArai | 2:1397a54382ec | 679 | PRINTF("%lu bytes read with %lu kB/sec.\r\n", |
kenjiArai | 2:1397a54382ec | 680 | p2, tim ? (p2 / tim) : 0); |
kenjiArai | 2:1397a54382ec | 681 | break; |
kenjiArai | 2:1397a54382ec | 682 | |
kenjiArai | 2:1397a54382ec | 683 | case 'w' : /* fw <len> <val> - write file */ |
kenjiArai | 2:1397a54382ec | 684 | if (!xatoi(&ptr, &p1) || !xatoi(&ptr, &p2)) break; |
kenjiArai | 2:1397a54382ec | 685 | memset(Buff, (BYTE)p2, blen); |
kenjiArai | 2:1397a54382ec | 686 | p2 = 0; |
kenjiArai | 2:1397a54382ec | 687 | t.reset(); |
kenjiArai | 2:1397a54382ec | 688 | t.start(); |
kenjiArai | 2:1397a54382ec | 689 | while (p1) { |
kenjiArai | 2:1397a54382ec | 690 | if ((UINT)p1 >= blen) { |
kenjiArai | 2:1397a54382ec | 691 | cnt = blen; |
kenjiArai | 2:1397a54382ec | 692 | p1 -= blen; |
kenjiArai | 2:1397a54382ec | 693 | } else { |
kenjiArai | 2:1397a54382ec | 694 | cnt = p1; |
kenjiArai | 2:1397a54382ec | 695 | p1 = 0; |
kenjiArai | 2:1397a54382ec | 696 | } |
kenjiArai | 2:1397a54382ec | 697 | res = f_write(&File1, Buff, cnt, &s2); |
kenjiArai | 2:1397a54382ec | 698 | if (res != FR_OK) { |
kenjiArai | 2:1397a54382ec | 699 | put_rc((FRESULT)res); |
kenjiArai | 2:1397a54382ec | 700 | break; |
kenjiArai | 2:1397a54382ec | 701 | } |
kenjiArai | 2:1397a54382ec | 702 | p2 += s2; |
kenjiArai | 2:1397a54382ec | 703 | if (cnt != s2) break; |
kenjiArai | 2:1397a54382ec | 704 | } |
kenjiArai | 2:1397a54382ec | 705 | tim = t.read_ms(); |
kenjiArai | 2:1397a54382ec | 706 | PRINTF("%lu bytes written with %lu kB/sec.\r\n", |
kenjiArai | 2:1397a54382ec | 707 | p2, tim ? (p2 / tim) : 0); |
kenjiArai | 2:1397a54382ec | 708 | break; |
kenjiArai | 2:1397a54382ec | 709 | |
kenjiArai | 2:1397a54382ec | 710 | case 'n' : /* fn <org.name> <new.name> - Change name of an object */ |
kenjiArai | 2:1397a54382ec | 711 | while (*ptr == ' ') ptr++; |
kenjiArai | 2:1397a54382ec | 712 | ptr2 = strchr(ptr, ' '); |
kenjiArai | 2:1397a54382ec | 713 | if (!ptr2) break; |
kenjiArai | 2:1397a54382ec | 714 | *ptr2++ = 0; |
kenjiArai | 2:1397a54382ec | 715 | while (*ptr2 == ' ') ptr2++; |
kenjiArai | 2:1397a54382ec | 716 | put_rc(f_rename(ptr, ptr2)); |
kenjiArai | 2:1397a54382ec | 717 | break; |
kenjiArai | 2:1397a54382ec | 718 | |
kenjiArai | 2:1397a54382ec | 719 | case 'u' : /* fu <name> - Unlink an object */ |
kenjiArai | 2:1397a54382ec | 720 | while (*ptr == ' ') ptr++; |
kenjiArai | 2:1397a54382ec | 721 | put_rc(f_unlink(ptr)); |
kenjiArai | 2:1397a54382ec | 722 | break; |
kenjiArai | 2:1397a54382ec | 723 | |
kenjiArai | 2:1397a54382ec | 724 | case 'v' : /* fv - Truncate file */ |
kenjiArai | 2:1397a54382ec | 725 | put_rc(f_truncate(&File1)); |
kenjiArai | 2:1397a54382ec | 726 | break; |
kenjiArai | 2:1397a54382ec | 727 | |
kenjiArai | 2:1397a54382ec | 728 | case 'k' : /* fk <name> - Create a directory */ |
kenjiArai | 2:1397a54382ec | 729 | while (*ptr == ' ') ptr++; |
kenjiArai | 2:1397a54382ec | 730 | put_rc(f_mkdir(ptr)); |
kenjiArai | 2:1397a54382ec | 731 | break; |
kenjiArai | 2:1397a54382ec | 732 | #if 0 |
kenjiArai | 2:1397a54382ec | 733 | case 'a' : /* fa <atrr> <mask> <name> - Change attribute of an object */ |
kenjiArai | 2:1397a54382ec | 734 | if (!xatoi(&ptr, &p1) || !xatoi(&ptr, &p2)) break; |
kenjiArai | 2:1397a54382ec | 735 | while (*ptr == ' ') ptr++; |
kenjiArai | 2:1397a54382ec | 736 | put_rc(f_chmod(ptr, p1, p2)); |
kenjiArai | 2:1397a54382ec | 737 | break; |
kenjiArai | 2:1397a54382ec | 738 | #endif |
kenjiArai | 2:1397a54382ec | 739 | #if 0 |
kenjiArai | 2:1397a54382ec | 740 | /* ft <year> <month> <day> <hour> <min> <sec> <name> |
kenjiArai | 2:1397a54382ec | 741 | - Change timestamp of an object */ |
kenjiArai | 2:1397a54382ec | 742 | case 't' : |
kenjiArai | 2:1397a54382ec | 743 | if (!xatoi(&ptr, &p1) || !xatoi(&ptr, &p2) || !xatoi(&ptr, &p3)) { |
kenjiArai | 2:1397a54382ec | 744 | break; |
kenjiArai | 2:1397a54382ec | 745 | } |
kenjiArai | 2:1397a54382ec | 746 | Finfo.fdate = ((p1 - 1980) << 9) | ((p2 & 15) << 5) | (p3 & 31); |
kenjiArai | 2:1397a54382ec | 747 | if (!xatoi(&ptr, &p1) || !xatoi(&ptr, &p2) || !xatoi(&ptr, &p3)) { |
kenjiArai | 2:1397a54382ec | 748 | break; |
kenjiArai | 2:1397a54382ec | 749 | } |
kenjiArai | 2:1397a54382ec | 750 | Finfo.ftime = |
kenjiArai | 2:1397a54382ec | 751 | ((p1 & 31) << 11) | ((p2 & 63) << 5) | ((p3 >> 1) & 31); |
kenjiArai | 2:1397a54382ec | 752 | put_rc(f_utime(ptr, &Finfo)); |
kenjiArai | 2:1397a54382ec | 753 | break; |
kenjiArai | 2:1397a54382ec | 754 | #endif |
kenjiArai | 2:1397a54382ec | 755 | #if FILCPY_NOTUSE == 0 |
kenjiArai | 2:1397a54382ec | 756 | case 'x' : /* fx <src_name> <dst_name> - Copy file */ |
kenjiArai | 2:1397a54382ec | 757 | while ( *ptr == ' ' ) { |
kenjiArai | 2:1397a54382ec | 758 | ptr++; |
kenjiArai | 2:1397a54382ec | 759 | } |
kenjiArai | 2:1397a54382ec | 760 | ptr2 = strchr( ptr, ' ' ); |
kenjiArai | 2:1397a54382ec | 761 | if ( !ptr2 ) { |
kenjiArai | 2:1397a54382ec | 762 | break; |
kenjiArai | 2:1397a54382ec | 763 | } |
kenjiArai | 2:1397a54382ec | 764 | *ptr2++ = 0; |
kenjiArai | 2:1397a54382ec | 765 | while ( *ptr2 == ' ' ) { |
kenjiArai | 2:1397a54382ec | 766 | ptr2++; |
kenjiArai | 2:1397a54382ec | 767 | } |
kenjiArai | 2:1397a54382ec | 768 | f_res = f_open( &File1, ptr, FA_OPEN_EXISTING | FA_READ ); |
kenjiArai | 2:1397a54382ec | 769 | PRINTF("Opening %s \r\n", ptr); |
kenjiArai | 2:1397a54382ec | 770 | if ( f_res ) { |
kenjiArai | 2:1397a54382ec | 771 | put_rc( (FRESULT)f_res ); |
kenjiArai | 2:1397a54382ec | 772 | break; |
kenjiArai | 2:1397a54382ec | 773 | } |
kenjiArai | 2:1397a54382ec | 774 | f_res = f_open( &File2, ptr2, FA_CREATE_ALWAYS | FA_WRITE ); |
kenjiArai | 2:1397a54382ec | 775 | PRINTF(" Creating %s \r\n", ptr2); |
kenjiArai | 2:1397a54382ec | 776 | if ( f_res ) { |
kenjiArai | 2:1397a54382ec | 777 | put_rc( (FRESULT)f_res ); |
kenjiArai | 2:1397a54382ec | 778 | f_close( &File1 ); |
kenjiArai | 2:1397a54382ec | 779 | break; |
kenjiArai | 2:1397a54382ec | 780 | } |
kenjiArai | 2:1397a54382ec | 781 | PRINTF("Copying file..."); |
kenjiArai | 2:1397a54382ec | 782 | p1 = 0; |
kenjiArai | 2:1397a54382ec | 783 | for ( ;; ) { |
kenjiArai | 2:1397a54382ec | 784 | f_res = f_read( &File1, Buff, blen, &s1 ); |
kenjiArai | 2:1397a54382ec | 785 | if ( f_res || s1 == 0 ) { |
kenjiArai | 2:1397a54382ec | 786 | break; /* error or eof */ |
kenjiArai | 2:1397a54382ec | 787 | } |
kenjiArai | 2:1397a54382ec | 788 | f_res = f_write( &File2, Buff, s1, &s2 ); |
kenjiArai | 2:1397a54382ec | 789 | p1 += s2; |
kenjiArai | 2:1397a54382ec | 790 | if ( f_res || s2 < s1 ) { |
kenjiArai | 2:1397a54382ec | 791 | break; /* error or disk full */ |
kenjiArai | 2:1397a54382ec | 792 | } |
kenjiArai | 2:1397a54382ec | 793 | } |
kenjiArai | 2:1397a54382ec | 794 | f_close( &File1 ); |
kenjiArai | 2:1397a54382ec | 795 | f_close( &File2 ); |
kenjiArai | 2:1397a54382ec | 796 | crlf(); |
kenjiArai | 2:1397a54382ec | 797 | break; |
kenjiArai | 2:1397a54382ec | 798 | #endif |
kenjiArai | 2:1397a54382ec | 799 | #if 0 |
kenjiArai | 2:1397a54382ec | 800 | case 'x' : /* fx <src.name> <dst.name> - Copy a file */ |
kenjiArai | 2:1397a54382ec | 801 | while (*ptr == ' ') ptr++; |
kenjiArai | 2:1397a54382ec | 802 | ptr2 = strchr(ptr, ' '); |
kenjiArai | 2:1397a54382ec | 803 | if (!ptr2) break; |
kenjiArai | 2:1397a54382ec | 804 | *ptr2++ = 0; |
kenjiArai | 2:1397a54382ec | 805 | while (*ptr2 == ' ') ptr2++; |
kenjiArai | 2:1397a54382ec | 806 | PRINTF("Opening \"%s\"", ptr); |
kenjiArai | 2:1397a54382ec | 807 | res = f_open(&File1, ptr, FA_OPEN_EXISTING | FA_READ); |
kenjiArai | 2:1397a54382ec | 808 | PUTS("\r\n"); |
kenjiArai | 2:1397a54382ec | 809 | if (res) { |
kenjiArai | 2:1397a54382ec | 810 | put_rc((FRESULT)res); |
kenjiArai | 2:1397a54382ec | 811 | break; |
kenjiArai | 2:1397a54382ec | 812 | } |
kenjiArai | 2:1397a54382ec | 813 | PRINTF("Creating \"%s\"", ptr2); |
kenjiArai | 2:1397a54382ec | 814 | res = f_open(&File1, ptr2, FA_CREATE_ALWAYS | FA_WRITE); |
kenjiArai | 2:1397a54382ec | 815 | PUTS("\r\n"); |
kenjiArai | 2:1397a54382ec | 816 | if (res) { |
kenjiArai | 2:1397a54382ec | 817 | put_rc((FRESULT)res); |
kenjiArai | 2:1397a54382ec | 818 | f_close(&File1); |
kenjiArai | 2:1397a54382ec | 819 | break; |
kenjiArai | 2:1397a54382ec | 820 | } |
kenjiArai | 2:1397a54382ec | 821 | PRINTF("Copying file..."); |
kenjiArai | 2:1397a54382ec | 822 | t.reset(); |
kenjiArai | 2:1397a54382ec | 823 | t.start(); |
kenjiArai | 2:1397a54382ec | 824 | p1 = 0; |
kenjiArai | 2:1397a54382ec | 825 | for (;;) { |
kenjiArai | 2:1397a54382ec | 826 | res = f_read(&File1, Buff, blen, &s1); |
kenjiArai | 2:1397a54382ec | 827 | if (res || s1 == 0) break; /* error or eof */ |
kenjiArai | 2:1397a54382ec | 828 | res = f_write(&File2, Buff, s1, &s2); |
kenjiArai | 2:1397a54382ec | 829 | p1 += s2; |
kenjiArai | 2:1397a54382ec | 830 | if (res || s2 < s1) break; /* error or disk full */ |
kenjiArai | 2:1397a54382ec | 831 | } |
kenjiArai | 2:1397a54382ec | 832 | tim = t.read_ms(); |
kenjiArai | 2:1397a54382ec | 833 | PRINTF("\r\n%lu bytes copied with %lu kB/sec.\r\n", |
kenjiArai | 2:1397a54382ec | 834 | p1, tim ? (p1 / tim) : 0); |
kenjiArai | 2:1397a54382ec | 835 | f_close(&File1); |
kenjiArai | 2:1397a54382ec | 836 | f_close(&File2); |
kenjiArai | 2:1397a54382ec | 837 | break; |
kenjiArai | 2:1397a54382ec | 838 | #endif |
kenjiArai | 2:1397a54382ec | 839 | #if FF_FS_RPATH |
kenjiArai | 2:1397a54382ec | 840 | case 'g' : /* fg <path> - Change current directory */ |
kenjiArai | 2:1397a54382ec | 841 | while (*ptr == ' ') ptr++; |
kenjiArai | 2:1397a54382ec | 842 | put_rc(f_chdir(ptr)); |
kenjiArai | 2:1397a54382ec | 843 | break; |
kenjiArai | 2:1397a54382ec | 844 | #if FF_FS_RPATH >= 2 |
kenjiArai | 2:1397a54382ec | 845 | case 'q' : /* fq - Show current dir path */ |
kenjiArai | 2:1397a54382ec | 846 | res = f_getcwd(Linebuf, sizeof Linebuf); |
kenjiArai | 2:1397a54382ec | 847 | if (res) |
kenjiArai | 2:1397a54382ec | 848 | put_rc(res); |
kenjiArai | 2:1397a54382ec | 849 | else |
kenjiArai | 2:1397a54382ec | 850 | PRINTF("%s\r\n", Linebuf); |
kenjiArai | 2:1397a54382ec | 851 | break; |
kenjiArai | 2:1397a54382ec | 852 | #endif |
kenjiArai | 2:1397a54382ec | 853 | #endif |
kenjiArai | 2:1397a54382ec | 854 | #if FF_USE_LABEL |
kenjiArai | 2:1397a54382ec | 855 | case 'b' : /* fb <name> - Set volume label */ |
kenjiArai | 2:1397a54382ec | 856 | while (*ptr == ' ') ptr++; |
kenjiArai | 2:1397a54382ec | 857 | put_rc(f_setlabel(ptr)); |
kenjiArai | 2:1397a54382ec | 858 | break; |
kenjiArai | 2:1397a54382ec | 859 | #endif /* FF_USE_LABEL */ |
kenjiArai | 2:1397a54382ec | 860 | #if FF_USE_MKFS |
kenjiArai | 2:1397a54382ec | 861 | case 'm' : /* fm <type> <csize> - Create file system */ |
kenjiArai | 2:1397a54382ec | 862 | if (!xatoi(&ptr, &p2) || !xatoi(&ptr, &p3)) break; |
kenjiArai | 2:1397a54382ec | 863 | PRINTF("The volume will be formatted. Are you sure? (Y/n)="); |
kenjiArai | 2:1397a54382ec | 864 | get_line(Linebuf, sizeof Linebuf); |
kenjiArai | 2:1397a54382ec | 865 | if (Linebuf[0] == 'Y') |
kenjiArai | 2:1397a54382ec | 866 | put_rc(f_mkfs("", (BYTE)p2, (DWORD)p3, Buff, sizeof Buff)); |
kenjiArai | 2:1397a54382ec | 867 | break; |
kenjiArai | 2:1397a54382ec | 868 | #endif /* FF_USE_MKFS */ |
kenjiArai | 2:1397a54382ec | 869 | /* fz [<size>] - Change/Show R/W length for fr/fw/fx command */ |
kenjiArai | 2:1397a54382ec | 870 | case 'z' : |
kenjiArai | 2:1397a54382ec | 871 | if (xatoi(&ptr, &p1) && p1 >= 1 && p1 <= (long)sizeof Buff) |
kenjiArai | 2:1397a54382ec | 872 | blen = p1; |
kenjiArai | 2:1397a54382ec | 873 | PRINTF("blen=%u\r\n", blen); |
kenjiArai | 2:1397a54382ec | 874 | break; |
kenjiArai | 2:1397a54382ec | 875 | } |
kenjiArai | 2:1397a54382ec | 876 | } |
kenjiArai | 2:1397a54382ec | 877 | |
kenjiArai | 2:1397a54382ec | 878 | static void memory_inf(char *ptr) |
kenjiArai | 2:1397a54382ec | 879 | { |
kenjiArai | 2:1397a54382ec | 880 | long p1, p2, p3; |
kenjiArai | 2:1397a54382ec | 881 | |
kenjiArai | 2:1397a54382ec | 882 | switch (*ptr++) { |
kenjiArai | 2:1397a54382ec | 883 | case 'd' : /* md[b|h|w] <address> [<count>] - Dump memory */ |
kenjiArai | 2:1397a54382ec | 884 | switch (*ptr++) { |
kenjiArai | 2:1397a54382ec | 885 | case 'w': |
kenjiArai | 2:1397a54382ec | 886 | p3 = DW_LONG; |
kenjiArai | 2:1397a54382ec | 887 | break; |
kenjiArai | 2:1397a54382ec | 888 | case 'h': |
kenjiArai | 2:1397a54382ec | 889 | p3 = DW_SHORT; |
kenjiArai | 2:1397a54382ec | 890 | break; |
kenjiArai | 2:1397a54382ec | 891 | default: |
kenjiArai | 2:1397a54382ec | 892 | p3 = DW_CHAR; |
kenjiArai | 2:1397a54382ec | 893 | } |
kenjiArai | 2:1397a54382ec | 894 | if (!xatoi(&ptr, &p1)) break; |
kenjiArai | 2:1397a54382ec | 895 | if (!xatoi(&ptr, &p2)) p2 = 128 / p3; |
kenjiArai | 2:1397a54382ec | 896 | for (ptr = (char*)p1; p2 >= 16 / p3; ptr += 16, p2 -= 16 / p3) |
kenjiArai | 2:1397a54382ec | 897 | put_dump(ptr, (DWORD)ptr, 16 / p3, p3); |
kenjiArai | 2:1397a54382ec | 898 | if (p2) put_dump((BYTE*)ptr, (UINT)ptr, p2, p3); |
kenjiArai | 2:1397a54382ec | 899 | break; |
kenjiArai | 2:1397a54382ec | 900 | case 'f' : /* mf <address> <value> <count> - Fill memory */ |
kenjiArai | 2:1397a54382ec | 901 | if (!xatoi(&ptr, &p1) || !xatoi(&ptr, &p2) || !xatoi(&ptr, &p3)) { |
kenjiArai | 2:1397a54382ec | 902 | break; |
kenjiArai | 2:1397a54382ec | 903 | } |
kenjiArai | 2:1397a54382ec | 904 | while (p3--) { |
kenjiArai | 2:1397a54382ec | 905 | *(BYTE*)p1 = (BYTE)p2; |
kenjiArai | 2:1397a54382ec | 906 | p1++; |
kenjiArai | 2:1397a54382ec | 907 | } |
kenjiArai | 2:1397a54382ec | 908 | break; |
kenjiArai | 2:1397a54382ec | 909 | case 'e' : /* me[b|h|w] <address> [<value> ...] - Edit memory */ |
kenjiArai | 2:1397a54382ec | 910 | switch (*ptr++) { /* Get data width */ |
kenjiArai | 2:1397a54382ec | 911 | case 'w': |
kenjiArai | 2:1397a54382ec | 912 | p3 = DW_LONG; |
kenjiArai | 2:1397a54382ec | 913 | break; |
kenjiArai | 2:1397a54382ec | 914 | case 'h': |
kenjiArai | 2:1397a54382ec | 915 | p3 = DW_SHORT; |
kenjiArai | 2:1397a54382ec | 916 | break; |
kenjiArai | 2:1397a54382ec | 917 | default: |
kenjiArai | 2:1397a54382ec | 918 | p3 = DW_CHAR; |
kenjiArai | 2:1397a54382ec | 919 | } |
kenjiArai | 2:1397a54382ec | 920 | if (!xatoi(&ptr, &p1)) break; /* Get start address */ |
kenjiArai | 2:1397a54382ec | 921 | if (xatoi(&ptr, &p2)) { /* 2nd parameter is given (direct mode) */ |
kenjiArai | 2:1397a54382ec | 922 | do { |
kenjiArai | 2:1397a54382ec | 923 | switch (p3) { |
kenjiArai | 2:1397a54382ec | 924 | case DW_LONG: |
kenjiArai | 2:1397a54382ec | 925 | *(DWORD*)p1 = (DWORD)p2; |
kenjiArai | 2:1397a54382ec | 926 | break; |
kenjiArai | 2:1397a54382ec | 927 | case DW_SHORT: |
kenjiArai | 2:1397a54382ec | 928 | *(WORD*)p1 = (WORD)p2; |
kenjiArai | 2:1397a54382ec | 929 | break; |
kenjiArai | 2:1397a54382ec | 930 | default: |
kenjiArai | 2:1397a54382ec | 931 | *(BYTE*)p1 = (BYTE)p2; |
kenjiArai | 2:1397a54382ec | 932 | } |
kenjiArai | 2:1397a54382ec | 933 | p1 += p3; |
kenjiArai | 2:1397a54382ec | 934 | } while (xatoi(&ptr, &p2)); /* Get next value */ |
kenjiArai | 2:1397a54382ec | 935 | break; |
kenjiArai | 2:1397a54382ec | 936 | } |
kenjiArai | 2:1397a54382ec | 937 | for (;;) { /* 2nd parameter is not given (interactive mode) */ |
kenjiArai | 2:1397a54382ec | 938 | switch (p3) { |
kenjiArai | 2:1397a54382ec | 939 | case DW_LONG: |
kenjiArai | 4:fc36c8ec2966 | 940 | PRINTF("%08X 0x%08X-", (unsigned int)p1, *(unsigned int*)p1); |
kenjiArai | 2:1397a54382ec | 941 | break; |
kenjiArai | 2:1397a54382ec | 942 | case DW_SHORT: |
kenjiArai | 4:fc36c8ec2966 | 943 | PRINTF("%08X 0x%04X-", (unsigned int)p1, *(WORD*)p1); |
kenjiArai | 2:1397a54382ec | 944 | break; |
kenjiArai | 2:1397a54382ec | 945 | default: |
kenjiArai | 4:fc36c8ec2966 | 946 | PRINTF("%08X 0x%02X-", (unsigned int)p1, *(BYTE*)p1); |
kenjiArai | 2:1397a54382ec | 947 | } |
kenjiArai | 2:1397a54382ec | 948 | ptr = Linebuf; |
kenjiArai | 2:1397a54382ec | 949 | get_line(ptr, sizeof Linebuf); |
kenjiArai | 2:1397a54382ec | 950 | if (*ptr == '.') break; |
kenjiArai | 2:1397a54382ec | 951 | if ((BYTE)*ptr >= ' ') { |
kenjiArai | 2:1397a54382ec | 952 | if (!xatoi(&ptr, &p2)) continue; |
kenjiArai | 2:1397a54382ec | 953 | switch (p3) { |
kenjiArai | 2:1397a54382ec | 954 | case DW_LONG: |
kenjiArai | 2:1397a54382ec | 955 | *(DWORD*)p1 = (DWORD)p2; |
kenjiArai | 2:1397a54382ec | 956 | break; |
kenjiArai | 2:1397a54382ec | 957 | case DW_SHORT: |
kenjiArai | 2:1397a54382ec | 958 | *(WORD*)p1 = (WORD)p2; |
kenjiArai | 2:1397a54382ec | 959 | break; |
kenjiArai | 2:1397a54382ec | 960 | default: |
kenjiArai | 2:1397a54382ec | 961 | *(BYTE*)p1 = (BYTE)p2; |
kenjiArai | 2:1397a54382ec | 962 | } |
kenjiArai | 2:1397a54382ec | 963 | } |
kenjiArai | 2:1397a54382ec | 964 | p1 += p3; |
kenjiArai | 2:1397a54382ec | 965 | } |
kenjiArai | 2:1397a54382ec | 966 | } |
kenjiArai | 2:1397a54382ec | 967 | } |
kenjiArai | 2:1397a54382ec | 968 | |
kenjiArai | 2:1397a54382ec | 969 | static void disk_inf(char *ptr) |
kenjiArai | 2:1397a54382ec | 970 | { |
kenjiArai | 2:1397a54382ec | 971 | long p1, p2; |
kenjiArai | 2:1397a54382ec | 972 | UINT s1; |
kenjiArai | 2:1397a54382ec | 973 | BYTE res, b, drv = 0; |
kenjiArai | 2:1397a54382ec | 974 | DWORD ofs = 0, sect = 0, blk[2]; |
kenjiArai | 2:1397a54382ec | 975 | |
kenjiArai | 2:1397a54382ec | 976 | switch (*ptr++) { |
kenjiArai | 2:1397a54382ec | 977 | case 'd' : /* dd [<pd#> <sect>] - Dump secrtor */ |
kenjiArai | 2:1397a54382ec | 978 | if (!xatoi(&ptr, &p1)) { |
kenjiArai | 2:1397a54382ec | 979 | p1 = drv; |
kenjiArai | 2:1397a54382ec | 980 | p2 = sect; |
kenjiArai | 2:1397a54382ec | 981 | } else { |
kenjiArai | 2:1397a54382ec | 982 | if (!xatoi(&ptr, &p2)) break; |
kenjiArai | 2:1397a54382ec | 983 | } |
kenjiArai | 2:1397a54382ec | 984 | drv = (BYTE)p1; |
kenjiArai | 2:1397a54382ec | 985 | sect = p2; |
kenjiArai | 2:1397a54382ec | 986 | res = disk_read(drv, Buff, sect, 1); |
kenjiArai | 2:1397a54382ec | 987 | if (res) { |
kenjiArai | 2:1397a54382ec | 988 | PRINTF("rc=%d\r\n", (WORD)res); |
kenjiArai | 2:1397a54382ec | 989 | break; |
kenjiArai | 2:1397a54382ec | 990 | } |
kenjiArai | 2:1397a54382ec | 991 | PRINTF("PD#:%u LBA:%lu\r\n", drv, sect++); |
kenjiArai | 2:1397a54382ec | 992 | for (ptr=(char*)Buff, ofs = 0; ofs < 0x200; ptr += 16, ofs += 16) |
kenjiArai | 2:1397a54382ec | 993 | put_dump((BYTE*)ptr, ofs, 16, DW_CHAR); |
kenjiArai | 2:1397a54382ec | 994 | break; |
kenjiArai | 2:1397a54382ec | 995 | |
kenjiArai | 2:1397a54382ec | 996 | case 'i' : /* di <pd#> - Initialize disk */ |
kenjiArai | 2:1397a54382ec | 997 | if (!xatoi(&ptr, &p1)) break; |
kenjiArai | 2:1397a54382ec | 998 | PRINTF("rc=%d\r\n", (WORD)disk_initialize((BYTE)p1)); |
kenjiArai | 2:1397a54382ec | 999 | break; |
kenjiArai | 2:1397a54382ec | 1000 | |
kenjiArai | 2:1397a54382ec | 1001 | case 's' : /* ds <pd#> - Show disk status */ |
kenjiArai | 2:1397a54382ec | 1002 | if (!xatoi(&ptr, &p1)) break; |
kenjiArai | 2:1397a54382ec | 1003 | if (disk_ioctl((BYTE)p1, GET_SECTOR_COUNT, &p2) == RES_OK) { |
kenjiArai | 2:1397a54382ec | 1004 | PRINTF("Drive size: %lu sectors\r\n", p2); |
kenjiArai | 2:1397a54382ec | 1005 | } |
kenjiArai | 2:1397a54382ec | 1006 | if (disk_ioctl((BYTE)p1, GET_BLOCK_SIZE, &p2) == RES_OK) { |
kenjiArai | 2:1397a54382ec | 1007 | PRINTF("Block size: %lu sectors\r\n", p2); |
kenjiArai | 2:1397a54382ec | 1008 | } |
kenjiArai | 2:1397a54382ec | 1009 | if (disk_ioctl((BYTE)p1, MMC_GET_TYPE, &b) == RES_OK) { |
kenjiArai | 2:1397a54382ec | 1010 | PRINTF("Media type: %u\r\n", b); |
kenjiArai | 2:1397a54382ec | 1011 | } |
kenjiArai | 2:1397a54382ec | 1012 | if (disk_ioctl((BYTE)p1, MMC_GET_CSD, Buff) == RES_OK) { |
kenjiArai | 2:1397a54382ec | 1013 | PUTS("CSD:\r\n"); |
kenjiArai | 2:1397a54382ec | 1014 | put_dump(Buff, 0, 16, DW_CHAR); |
kenjiArai | 2:1397a54382ec | 1015 | } |
kenjiArai | 2:1397a54382ec | 1016 | if (disk_ioctl((BYTE)p1, MMC_GET_CID, Buff) == RES_OK) { |
kenjiArai | 2:1397a54382ec | 1017 | PUTS("CID:\r\n"); |
kenjiArai | 2:1397a54382ec | 1018 | put_dump(Buff, 0, 16, DW_CHAR); |
kenjiArai | 2:1397a54382ec | 1019 | } |
kenjiArai | 2:1397a54382ec | 1020 | if (disk_ioctl((BYTE)p1, MMC_GET_OCR, Buff) == RES_OK) { |
kenjiArai | 2:1397a54382ec | 1021 | PUTS("OCR:\r\n"); |
kenjiArai | 2:1397a54382ec | 1022 | put_dump(Buff, 0, 4, DW_CHAR); |
kenjiArai | 2:1397a54382ec | 1023 | } |
kenjiArai | 2:1397a54382ec | 1024 | if (disk_ioctl((BYTE)p1, MMC_GET_SDSTAT, Buff) == RES_OK) { |
kenjiArai | 2:1397a54382ec | 1025 | PUTS("SD Status:\r\n"); |
kenjiArai | 2:1397a54382ec | 1026 | for (s1 = 0; s1 < 64; s1 += 16) { |
kenjiArai | 2:1397a54382ec | 1027 | put_dump(Buff+s1, s1, 16, DW_CHAR); |
kenjiArai | 2:1397a54382ec | 1028 | } |
kenjiArai | 2:1397a54382ec | 1029 | } |
kenjiArai | 2:1397a54382ec | 1030 | break; |
kenjiArai | 2:1397a54382ec | 1031 | |
kenjiArai | 2:1397a54382ec | 1032 | case 'c' : /* Disk ioctl */ |
kenjiArai | 2:1397a54382ec | 1033 | switch (*ptr++) { |
kenjiArai | 2:1397a54382ec | 1034 | case 's' : /* dcs <pd#> - CTRL_SYNC */ |
kenjiArai | 2:1397a54382ec | 1035 | if (!xatoi(&ptr, &p1)) break; |
kenjiArai | 2:1397a54382ec | 1036 | PRINTF("rc=%d\r\n", disk_ioctl((BYTE)p1, CTRL_SYNC, 0)); |
kenjiArai | 2:1397a54382ec | 1037 | break; |
kenjiArai | 2:1397a54382ec | 1038 | case 'e' : /* dce <pd#> <s.lba> <e.lba> - CTRL_TRIM */ |
kenjiArai | 2:1397a54382ec | 1039 | if (!xatoi(&ptr, &p1) || |
kenjiArai | 2:1397a54382ec | 1040 | !xatoi(&ptr, (long*)&blk[0]) || |
kenjiArai | 2:1397a54382ec | 1041 | !xatoi(&ptr, (long*)&blk[1])) { |
kenjiArai | 2:1397a54382ec | 1042 | break; |
kenjiArai | 2:1397a54382ec | 1043 | } |
kenjiArai | 2:1397a54382ec | 1044 | PRINTF("rc=%d\r\n", disk_ioctl((BYTE)p1, CTRL_TRIM, blk)); |
kenjiArai | 2:1397a54382ec | 1045 | break; |
kenjiArai | 2:1397a54382ec | 1046 | } |
kenjiArai | 2:1397a54382ec | 1047 | } |
kenjiArai | 2:1397a54382ec | 1048 | } |
kenjiArai | 2:1397a54382ec | 1049 | |
kenjiArai | 2:1397a54382ec | 1050 | void put_dump ( |
kenjiArai | 2:1397a54382ec | 1051 | void* buff, /* Pointer to the array to be dumped */ |
kenjiArai | 2:1397a54382ec | 1052 | unsigned long addr, /* Heading address value */ |
kenjiArai | 2:1397a54382ec | 1053 | int len, /* Number of items to be dumped */ |
kenjiArai | 2:1397a54382ec | 1054 | int width /* Size of the items (DW_CHAR, DW_SHORT, DW_LONG) */ |
kenjiArai | 2:1397a54382ec | 1055 | ) |
kenjiArai | 2:1397a54382ec | 1056 | { |
kenjiArai | 2:1397a54382ec | 1057 | int i; |
kenjiArai | 2:1397a54382ec | 1058 | unsigned char *bp; |
kenjiArai | 2:1397a54382ec | 1059 | unsigned short *sp; |
kenjiArai | 2:1397a54382ec | 1060 | unsigned long *lp; |
kenjiArai | 2:1397a54382ec | 1061 | |
kenjiArai | 2:1397a54382ec | 1062 | PRINTF( "%08lx ", addr ); /* address */ |
kenjiArai | 2:1397a54382ec | 1063 | switch ( width ) { |
kenjiArai | 2:1397a54382ec | 1064 | case DW_CHAR: |
kenjiArai | 2:1397a54382ec | 1065 | bp = (unsigned char *)buff; |
kenjiArai | 2:1397a54382ec | 1066 | for ( i = 0; i < len; i++ ) { /* Hexdecimal dump */ |
kenjiArai | 2:1397a54382ec | 1067 | PRINTF( " %02x", bp[i] ); |
kenjiArai | 2:1397a54382ec | 1068 | } |
kenjiArai | 2:1397a54382ec | 1069 | PUTC(' '); |
kenjiArai | 2:1397a54382ec | 1070 | for ( i = 0; i < len; i++ ) { /* ASCII dump */ |
kenjiArai | 2:1397a54382ec | 1071 | PUTC( (bp[i] >= ' ' && bp[i] <= '~') ? bp[i] : '.' ); |
kenjiArai | 2:1397a54382ec | 1072 | } |
kenjiArai | 2:1397a54382ec | 1073 | break; |
kenjiArai | 2:1397a54382ec | 1074 | case DW_SHORT: |
kenjiArai | 2:1397a54382ec | 1075 | sp = (unsigned short *)buff; |
kenjiArai | 2:1397a54382ec | 1076 | do { /* Hexdecimal dump */ |
kenjiArai | 2:1397a54382ec | 1077 | PRINTF( " %04x", *sp++ ); |
kenjiArai | 2:1397a54382ec | 1078 | } while ( --len ); |
kenjiArai | 2:1397a54382ec | 1079 | break; |
kenjiArai | 2:1397a54382ec | 1080 | case DW_LONG: |
kenjiArai | 2:1397a54382ec | 1081 | lp = (unsigned long *)buff; |
kenjiArai | 2:1397a54382ec | 1082 | do { /* Hexdecimal dump */ |
kenjiArai | 2:1397a54382ec | 1083 | PRINTF( " %08lx", *lp++ ); |
kenjiArai | 2:1397a54382ec | 1084 | } while ( --len ); |
kenjiArai | 2:1397a54382ec | 1085 | break; |
kenjiArai | 2:1397a54382ec | 1086 | } |
kenjiArai | 2:1397a54382ec | 1087 | PUTS( "\r\n" ); |
kenjiArai | 2:1397a54382ec | 1088 | } |
kenjiArai | 2:1397a54382ec | 1089 | |
kenjiArai | 2:1397a54382ec | 1090 | // RTC related subroutines |
kenjiArai | 2:1397a54382ec | 1091 | void chk_and_set_time(char *ptr) |
kenjiArai | 2:1397a54382ec | 1092 | { |
kenjiArai | 2:1397a54382ec | 1093 | char buf[64]; |
kenjiArai | 2:1397a54382ec | 1094 | |
kenjiArai | 2:1397a54382ec | 1095 | long p1; |
kenjiArai | 2:1397a54382ec | 1096 | struct tm t; |
kenjiArai | 2:1397a54382ec | 1097 | time_t seconds; |
kenjiArai | 2:1397a54382ec | 1098 | |
kenjiArai | 2:1397a54382ec | 1099 | if (xatoi(&ptr, &p1)) { |
kenjiArai | 2:1397a54382ec | 1100 | t.tm_year = (uint8_t)p1 + 100; |
kenjiArai | 4:fc36c8ec2966 | 1101 | pc.printf("Year:%ld ",p1); |
kenjiArai | 2:1397a54382ec | 1102 | xatoi( &ptr, &p1 ); |
kenjiArai | 2:1397a54382ec | 1103 | t.tm_mon = (uint8_t)p1 - 1; |
kenjiArai | 4:fc36c8ec2966 | 1104 | pc.printf("Month:%ld ",p1); |
kenjiArai | 2:1397a54382ec | 1105 | xatoi( &ptr, &p1 ); |
kenjiArai | 2:1397a54382ec | 1106 | t.tm_mday = (uint8_t)p1; |
kenjiArai | 4:fc36c8ec2966 | 1107 | pc.printf("Day:%ld ",p1); |
kenjiArai | 2:1397a54382ec | 1108 | xatoi( &ptr, &p1 ); |
kenjiArai | 2:1397a54382ec | 1109 | t.tm_hour = (uint8_t)p1; |
kenjiArai | 4:fc36c8ec2966 | 1110 | pc.printf("Hour:%ld ",p1); |
kenjiArai | 2:1397a54382ec | 1111 | xatoi( &ptr, &p1 ); |
kenjiArai | 2:1397a54382ec | 1112 | t.tm_min = (uint8_t)p1; |
kenjiArai | 4:fc36c8ec2966 | 1113 | pc.printf("Min:%ld ",p1); |
kenjiArai | 2:1397a54382ec | 1114 | xatoi( &ptr, &p1 ); |
kenjiArai | 2:1397a54382ec | 1115 | t.tm_sec = (uint8_t)p1; |
kenjiArai | 4:fc36c8ec2966 | 1116 | pc.printf("Sec: %ld \r\n",p1); |
kenjiArai | 2:1397a54382ec | 1117 | seconds = mktime(&t); |
kenjiArai | 2:1397a54382ec | 1118 | set_time(seconds); |
kenjiArai | 2:1397a54382ec | 1119 | } else { |
kenjiArai | 2:1397a54382ec | 1120 | seconds = time(NULL); |
kenjiArai | 2:1397a54382ec | 1121 | } |
kenjiArai | 2:1397a54382ec | 1122 | strftime(buf, 50, " %B %d,'%y, %H:%M:%S\r\n", localtime(&seconds)); |
kenjiArai | 2:1397a54382ec | 1123 | pc.printf("[Time] %s", buf); |
kenjiArai | 2:1397a54382ec | 1124 | } |
kenjiArai | 2:1397a54382ec | 1125 | |
kenjiArai | 2:1397a54382ec | 1126 | // Get key input data |
kenjiArai | 2:1397a54382ec | 1127 | void get_line (char *buff, int len) |
kenjiArai | 2:1397a54382ec | 1128 | { |
kenjiArai | 2:1397a54382ec | 1129 | char c; |
kenjiArai | 2:1397a54382ec | 1130 | int idx = 0; |
kenjiArai | 2:1397a54382ec | 1131 | |
kenjiArai | 2:1397a54382ec | 1132 | for (;;) { |
kenjiArai | 2:1397a54382ec | 1133 | c = GETC(); |
kenjiArai | 2:1397a54382ec | 1134 | // Added by Kenji Arai / JH1PJL May 9th, 2010 |
kenjiArai | 2:1397a54382ec | 1135 | if (c == '\r') { |
kenjiArai | 2:1397a54382ec | 1136 | buff[idx++] = c; |
kenjiArai | 2:1397a54382ec | 1137 | break; |
kenjiArai | 2:1397a54382ec | 1138 | } |
kenjiArai | 2:1397a54382ec | 1139 | if ((c == '\b') && idx) { |
kenjiArai | 2:1397a54382ec | 1140 | idx--; |
kenjiArai | 2:1397a54382ec | 1141 | PUTC(c); |
kenjiArai | 2:1397a54382ec | 1142 | PUTC(' '); |
kenjiArai | 2:1397a54382ec | 1143 | PUTC(c); |
kenjiArai | 2:1397a54382ec | 1144 | } |
kenjiArai | 2:1397a54382ec | 1145 | if (((uint8_t)c >= ' ') && (idx < len - 1)) { |
kenjiArai | 2:1397a54382ec | 1146 | buff[idx++] = c; |
kenjiArai | 2:1397a54382ec | 1147 | PUTC(c); |
kenjiArai | 2:1397a54382ec | 1148 | } |
kenjiArai | 2:1397a54382ec | 1149 | } |
kenjiArai | 2:1397a54382ec | 1150 | buff[idx] = 0; |
kenjiArai | 2:1397a54382ec | 1151 | PUTS("\r\n"); |
kenjiArai | 2:1397a54382ec | 1152 | } |
kenjiArai | 2:1397a54382ec | 1153 | |
kenjiArai | 2:1397a54382ec | 1154 | /* Outpur LF & CR */ |
kenjiArai | 2:1397a54382ec | 1155 | void crlf( void ) |
kenjiArai | 2:1397a54382ec | 1156 | { |
kenjiArai | 2:1397a54382ec | 1157 | PRINTF( "\r\n" ); |
kenjiArai | 2:1397a54382ec | 1158 | } |
kenjiArai | 2:1397a54382ec | 1159 | |
kenjiArai | 2:1397a54382ec | 1160 | /* Check key input */ |
kenjiArai | 2:1397a54382ec | 1161 | unsigned int check_hit_key (void) |
kenjiArai | 2:1397a54382ec | 1162 | { |
kenjiArai | 2:1397a54382ec | 1163 | return ( READABLE() ); |
kenjiArai | 2:1397a54382ec | 1164 | } |
kenjiArai | 2:1397a54382ec | 1165 | |
kenjiArai | 2:1397a54382ec | 1166 | /*----------------------------------------------*/ |
kenjiArai | 2:1397a54382ec | 1167 | /* Get a value of the string */ |
kenjiArai | 2:1397a54382ec | 1168 | /*----------------------------------------------*/ |
kenjiArai | 2:1397a54382ec | 1169 | /* "123 -5 0x3ff 0b1111 0377 w " |
kenjiArai | 2:1397a54382ec | 1170 | ^ 1st call returns 123 and next ptr |
kenjiArai | 2:1397a54382ec | 1171 | ^ 2nd call returns -5 and next ptr |
kenjiArai | 2:1397a54382ec | 1172 | ^ 3rd call returns 1023 and next ptr |
kenjiArai | 2:1397a54382ec | 1173 | ^ 4th call returns 15 and next ptr |
kenjiArai | 2:1397a54382ec | 1174 | ^ 5th call returns 255 and next ptr |
kenjiArai | 2:1397a54382ec | 1175 | ^ 6th call fails and returns 0 |
kenjiArai | 2:1397a54382ec | 1176 | */ |
kenjiArai | 2:1397a54382ec | 1177 | int xatoi ( /* 0:Failed, 1:Successful */ |
kenjiArai | 2:1397a54382ec | 1178 | char **str, /* Pointer to pointer to the string */ |
kenjiArai | 2:1397a54382ec | 1179 | long *res /* Pointer to the valiable to store the value */ |
kenjiArai | 2:1397a54382ec | 1180 | ) |
kenjiArai | 2:1397a54382ec | 1181 | { |
kenjiArai | 2:1397a54382ec | 1182 | unsigned long val; |
kenjiArai | 2:1397a54382ec | 1183 | unsigned char c, r, s = 0; |
kenjiArai | 2:1397a54382ec | 1184 | |
kenjiArai | 2:1397a54382ec | 1185 | *res = 0; |
kenjiArai | 2:1397a54382ec | 1186 | while ( (c = **str) == ' ' ) { |
kenjiArai | 2:1397a54382ec | 1187 | (*str)++; /* Skip leading spaces */ |
kenjiArai | 2:1397a54382ec | 1188 | } |
kenjiArai | 2:1397a54382ec | 1189 | if ( c == '-' ) { /* negative? */ |
kenjiArai | 2:1397a54382ec | 1190 | s = 1; |
kenjiArai | 2:1397a54382ec | 1191 | c = *(++(*str)); |
kenjiArai | 2:1397a54382ec | 1192 | } |
kenjiArai | 2:1397a54382ec | 1193 | if ( c == '0' ) { |
kenjiArai | 2:1397a54382ec | 1194 | c = *(++(*str)); |
kenjiArai | 2:1397a54382ec | 1195 | switch (c) { |
kenjiArai | 2:1397a54382ec | 1196 | case 'x': /* hexdecimal */ |
kenjiArai | 2:1397a54382ec | 1197 | r = 16; |
kenjiArai | 2:1397a54382ec | 1198 | c = *(++(*str)); |
kenjiArai | 2:1397a54382ec | 1199 | break; |
kenjiArai | 2:1397a54382ec | 1200 | case 'b': /* binary */ |
kenjiArai | 2:1397a54382ec | 1201 | r = 2; |
kenjiArai | 2:1397a54382ec | 1202 | c = *(++(*str)); |
kenjiArai | 2:1397a54382ec | 1203 | break; |
kenjiArai | 2:1397a54382ec | 1204 | default: |
kenjiArai | 2:1397a54382ec | 1205 | if ( c <= ' ' ) return 1; /* single zero */ |
kenjiArai | 2:1397a54382ec | 1206 | if ( c < '0' || c > '9' ) return 0; /* invalid char */ |
kenjiArai | 2:1397a54382ec | 1207 | r = 8; /* octal */ |
kenjiArai | 2:1397a54382ec | 1208 | } |
kenjiArai | 2:1397a54382ec | 1209 | } else { |
kenjiArai | 2:1397a54382ec | 1210 | if ( c < '0' || c > '9' ) return 0; /* EOL or invalid char */ |
kenjiArai | 2:1397a54382ec | 1211 | r = 10; /* decimal */ |
kenjiArai | 2:1397a54382ec | 1212 | } |
kenjiArai | 2:1397a54382ec | 1213 | val = 0; |
kenjiArai | 2:1397a54382ec | 1214 | while ( c > ' ' ) { |
kenjiArai | 2:1397a54382ec | 1215 | if ( c >= 'a' ) { |
kenjiArai | 2:1397a54382ec | 1216 | c -= 0x20; |
kenjiArai | 2:1397a54382ec | 1217 | } |
kenjiArai | 2:1397a54382ec | 1218 | c -= '0'; |
kenjiArai | 2:1397a54382ec | 1219 | if ( c >= 17 ) { |
kenjiArai | 2:1397a54382ec | 1220 | c -= 7; |
kenjiArai | 2:1397a54382ec | 1221 | if ( c <= 9 ) { |
kenjiArai | 2:1397a54382ec | 1222 | return 0; /* invalid char */ |
kenjiArai | 2:1397a54382ec | 1223 | } |
kenjiArai | 2:1397a54382ec | 1224 | } |
kenjiArai | 2:1397a54382ec | 1225 | if ( c >= r ) { |
kenjiArai | 2:1397a54382ec | 1226 | return 0; /* invalid char for current radix */ |
kenjiArai | 2:1397a54382ec | 1227 | } |
kenjiArai | 2:1397a54382ec | 1228 | val = val * r + c; |
kenjiArai | 2:1397a54382ec | 1229 | c = *(++(*str)); |
kenjiArai | 2:1397a54382ec | 1230 | } |
kenjiArai | 2:1397a54382ec | 1231 | if (s) val = 0 - val; /* apply sign if needed */ |
kenjiArai | 2:1397a54382ec | 1232 | *res = val; |
kenjiArai | 2:1397a54382ec | 1233 | return 1; |
kenjiArai | 2:1397a54382ec | 1234 | } |