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 Aug 17 03:29:11 2019 +0000
Revision:
3:4d7be22641c1
Parent:
0:342895df3f8f
Child:
4:6d6ea85642ed
modified main.cpp. added check_revision.cpp. run on latest revision.

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