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:
Thu Jan 14 00:07:07 2021 +0000
Revision:
7:29fece78d927
Parent:
6:7f8bc932cede
run on mbed-os6.6.0

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