run on mbed-os6.6.0

NumSD Card pinModule pinPin NamePort NameSPI
1CD/DAT3CSD10PB6-
2CMD/DISDID11PA_7MOSI
3VSS1GNDGND--
4VDD3.3V3V3--
5CLKCLKD13PA_5SCLK
6VSS2GNDGND--
7DAT0/DOSDOD12PA_6MISO
8DAT1----
9DAT2----

電源+/-を含め合計6本の結線で接続します。
Nucleoシリーズは、ボード上のLED(プログラムでLED1)がD13に接続されSPIモード設定時にSCLKとなる構成が多いので、LED1制御を止める必要があります。

For DISCO-F769NI
https://os.mbed.com/users/kenjiArai/code/DISCO-F769NI_BD_SD_Card_Control/
For DISCO-F469NI
https://os.mbed.com/users/kenjiArai/code/DISCO-F469NI_BD_SD_Card_Control/

Committer:
kenjiArai
Date:
Sat Apr 07 01:33:25 2018 +0000
Revision:
0:342895df3f8f
Child:
3:4d7be22641c1
SD Card control on Mbed OS5

Who changed what in which revision?

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