/*----------------------------------------------------------------------*/
/* FRK-RX62N: FatFsモジュール サンプルプログラム     (C)T.AKAMATSU, 2011  */
/*----------------------------------------------------------------------*/
/* インターフェース付録RX62N基板サンプルプログラム							*/
/* 端末接続ポート: scif.cを参照											*/
/* MMC/SD接続ポート: mmc_rspi.cを参照										*/
/*----------------------------------------------------------------------*/


#include <machine.h>
#include "iodefine.h"
#include "vect.h"
#include "integer.h"
#include "scif.h"
#include "monitor.h"
#include "diskio.h"
#include "ff.h"

#define	PCLK	48000000UL



/*---------------------------------------------------------*/
/* ワークエリア                                            */
/*---------------------------------------------------------*/

DWORD acc_size;				/* ワーク変数 (fsコマンド) */
WORD acc_files, acc_dirs;

FILINFO Finfo;				/* ワーク変数 (fs/flコマンド) */
#if _USE_LFN
char Lfname[_MAX_LFN + 1];
#endif

char Line[256];				/* コンソール入力バッファ */

FATFS Fatfs;				/* ファイルシステムオブジェクト */
FIL File1, File2;			/* ファイルオブジェクト */
DIR Dir;					/* ディレクトリオブジェクト */

BYTE Buff[16384];			/* データバッファ */

volatile UINT Timer;		/* 時間計測用タイマ (1kHz) */




/*---------------------------------------------------------*/
/* 1000Hzインターバルタイマ (CMT0)                         */
/*---------------------------------------------------------*/

void Excep_CMTU0_CMT0(void)		/* ISR: vect.hのインクルードが必要 */
{
	static WORD b;


	/* 時間計測用タイマ */
	Timer++;

	/* LED1を点滅 */
	b++;
	PORT1.DR.BIT.B5 = ((b & 0x40) && (b & 0x680) == 0) ? 0 : 1;

	/* MMCモジュール(mmc_rspi.c)の駆動 */
	disk_timerproc();
}


void delay_ms (					/* ms単位のディレイ */
	UINT ms
)
{
	for (Timer = 0; Timer < ms; ) ;
}



/*---------------------------------------------------------*/
/* クロックの設定とタイマの起動                              */
/*---------------------------------------------------------*/


static
void timer_init (void)
{
	PORT1.DDR.BIT.B5 = 1;			/* P15:LED ON */

	SYSTEM.SCKCR.LONG = 0x01020100;	/* クロックの選択: ICLK=8x, PCLK=4x */

	/* CMT0の初期化(1kHzインターバルタイマ) */
	MSTP_CMT0 = 0;						/* CMT0の動作を許可 */
	CMT0.CMCR.WORD = 0x0040;			/* CMIE=1, CKS=0(PCLK/8) */
	CMT0.CMCOR = PCLK / 8 / 1000 - 1;	/* 分周値の設定 */
	CMT.CMSTR0.BIT.STR0 = 1;			/* CMT0カウント開始 */
	IPR(CMT0, CMI0) = 8;				/* CMT0割り込み優先レベル=8 */
	IEN(CMT0, CMI0) = 1;				/* CMT0割り込み許可 */
}



/*----------------------------------------------------*/
/* 現在時刻取得関数                                   */
/*----------------------------------------------------*/
/* FatFsから呼ばれる下位レイヤ関数の一つで、現在時刻を
/  DWORD値にパッキングして返す。Read only構成では不要。
*/

DWORD get_fattime (void)
{
	/* RTC機能無し。固定値を返す 2011/1/29 0:00:00 */
	return	  ((DWORD)(2011 - 1980) << 25)	/* Y */
			| ((DWORD)1  << 21)				/* M */
			| ((DWORD)29 << 16)				/* D */
			| ((DWORD)0  << 11)				/* H */
			| ((DWORD)0  << 5)				/* M */
			| ((DWORD)0  >> 1);				/* S */
}



/*--------------------------------------------------------------------------*/
/* モニタ                                                                   */
/*--------------------------------------------------------------------------*/


int xstrlen (const char *str)
{
	int n = 0;

	while (*str++) n++;
	return n;
}


char *xstrcpy (char* dst, const char* src)
{
	char c, *d = dst;

	do {
		c = *src++;
		*d++ = c;
	} while (c);

	return dst;
}


void *xmemset (void *p, int c, int sz)
{
	char *pf = (char*)p;

	while (sz--) *pf++ = (char)c;
	return p;
}


char *xstrchr (const char *str, int c)
{
	while (*str) {
		if (*str == (char)c) return (char*)str;
		str++;
	}
	return 0;
}


static
FRESULT scan_files (	/* ディレクトリの中を調べる */
	char* path			/* パス名ワークエリアへのポインタ */
)
{
	DIR dirs;
	FRESULT fr;
	BYTE i;
	char *fn;


	fr = f_opendir(&dirs, path);	/* ディレクトリを開く */
	if (fr == FR_OK) {
		i = xstrlen(path);
		while (((fr = f_readdir(&dirs, &Finfo)) == FR_OK) && Finfo.fname[0]) {	/* ディレクトリ項目読み出し*/
			if (_FS_RPATH && Finfo.fname[0] == '.') continue;	/* ドットエントリは棄てる */
#if _USE_LFN
			fn = *Finfo.lfname ? Finfo.lfname : Finfo.fname;	/* LFNがあるときはそれを使う */
#else
			fn = Finfo.fname;	/* 非LFN構成では常にSFN */
#endif
			if (Finfo.fattrib & AM_DIR) {	/* ディレクトリ */
				acc_dirs++;									/* ディレクトリ数++ */
				*(path+i) = '/'; xstrcpy(path+i+1, fn);		/* そのディレクトリの中をさらに辿る */
				fr = scan_files(path);
				*(path+i) = '\0';
				if (fr != FR_OK) break;
			} else {						/* ファイル */
			/*	xprintf("%s/%s\n", path, fn); */
				acc_files++;								/* ファイル数++ */
				acc_size += Finfo.fsize;					/* ファイルサイズの積算(バイト単位) */
			}
		}
	}

	return fr;
}




static
void put_rc (		/* ファイル関数のリザルトコードを省略形とともに表示 */
	FRESULT rc
)
{
	const char *str =
		"OK\0" "DISK_ERR\0" "INT_ERR\0" "NOT_READY\0" "NO_FILE\0" "NO_PATH\0"
		"INVALID_NAME\0" "DENIED\0" "EXIST\0" "INVALID_OBJECT\0" "WRITE_PROTECTED\0"
		"INVALID_DRIVE\0" "NOT_ENABLED\0" "NO_FILE_SYSTEM\0" "MKFS_ABORTED\0" "TIMEOUT\0"
		"LOCKED\0" "NOT_ENOUGH_CORE\0" "TOO_MANY_OPEN_FILES\0";
	FRESULT i;

	for (i = FR_OK; i != rc && *str; i++) {
		while (*str++) ;
	}
	xprintf("rc=%u FR_%s\n", (UINT)rc, str);
}


static
char HelpMsg[] = {
	"md <address> [<len>] - Dump system memory\n"
	"\n"
	"dd [<lba>] - Dump secrtor\n"
	"di - Initialize disk\n"
	"ds - Show disk status\n"
	"\n"
	"bd <ofs> - Dump working buffer\n"
	"be <ofs> [<data>] ... - Edit working buffer\n"
	"br <lba> [<num>] - Read disk into working buffer\n"
	"bw <lba> [<num>] - Write working buffer into disk\n"
	"bf <val> - Fill working buffer\n"
	"\n"
	"fi - Force initialized the volume\n"
	"fs - Show volume status\n"
	"fl [<path>] - Show a directory\n"
	"fo <mode> <file> - Open a file\n"
	"fc - Close the file\n"
	"fe <ofs> - Move fp\n"
	"fd <len> - Read and dump the file\n"
	"fr <len> - Read the file\n"
	"fw <len> <val> - Write to the file\n"
	"fn <object name> <new name> - Rename an object\n"
	"fu <object name> - Unlink an object\n"
	"fv - Truncate the file at current fp\n"
	"fk <dir name> - Create a directory\n"
	"fa <atrr> <mask> <object name> - Change object attribute\n"
	"ft <year> <month> <day> <hour> <min> <sec> <object name> - Change timestamp of an object\n"
	"fx <src file> <dst file> - Copy a file\n"
	"fg <path> - Change current directory\n"
	"fq - Show current dir path\n"
	"fm <rule> <cluster size> - Create file system\n"
	"fz [<len>] - Change R/W block length for fr/fw/fx command\n"
	"\n"
};


static
void show_disk_status (		/* ドライブステータスの表示 */
	BYTE drv
)
{
	DWORD dw;
	WORD w;
	BYTE b, buf[64];
	char *ty, *am;


	if (disk_ioctl(drv, GET_SECTOR_COUNT, &dw) == RES_OK)
		xprintf("Drive size: %lu sectors\n", dw);

	if (disk_ioctl(drv, GET_SECTOR_SIZE, &w) == RES_OK)
		xprintf("Sector size: %u\n", w);

	if (disk_ioctl(drv, GET_BLOCK_SIZE, &dw) == RES_OK)
		xprintf("Erase block size: %lu sectors\n", dw);

	if (disk_ioctl(drv, MMC_GET_TYPE, &b) == RES_OK) {
		ty = "Unknown"; am = "";
		if (b & CT_MMC) ty = "MMC";
		if (b & CT_SD1) ty = "SDv1";
		if (b & CT_SD2) {
			ty = "SDv2";
			am = (b & CT_BLOCK) ? "(Block)" : "(Byte)";
		}
		xprintf("Card type: %s%s\n", ty, am);
	}

	if (disk_ioctl(drv, MMC_GET_CSD, buf) == RES_OK) {
		xputs("CSD:\n");
		put_dump(buf, 0, 16);
	}

	if (disk_ioctl(drv, MMC_GET_CID, buf) == RES_OK) {
		xputs("CID:\n");
		put_dump(buf, 0, 16);
	}

	if (disk_ioctl(drv, MMC_GET_OCR, buf) == RES_OK) {
		xputs("OCR:\n");
		put_dump(buf, 0, 4);
	}

	if (disk_ioctl(drv, MMC_GET_SDSTAT, buf) == RES_OK) {
		xputs("SD Status:\n");
		for (w = 0; w < 64; w += 16)
			put_dump(buf+w, w, 16);
	}
}





static
void show_fs_status (	/* ボリュームステータスの表示 */
	char *path
)
{
	FRESULT res;
	DWORD dw;
	FATFS *fs;
	static const BYTE ft[] = {0, 12, 16, 32};


	res = f_getfree(path, &dw, &fs);
	if (res == FR_OK) {
		xprintf("FAT type = FAT%u\n", ft[fs->fs_type & 3]);
		xprintf("Bytes/Cluster = %lu\nNumber of FATs = %u\n", fs->csize * 512UL, fs->n_fats);
		if (fs->fs_type != FS_FAT32) xprintf("Root DIR entries = %u\n", fs->n_rootdir);
		xprintf("Sectors/FAT = %lu\nNumber of clusters = %lu\n", fs->fsize, fs->n_fatent - 2);
		xprintf("FAT start (lba) = %lu\nDIR start (lba,clustor) = %lu\nData start (lba) = %lu\n...",
				fs->fatbase, fs->dirbase, fs->database);

		acc_size = acc_files = acc_dirs = 0;
		res = scan_files(path);
		if (res == FR_OK) {
			xprintf("\r   \n%u files, %lu bytes.\n%u folders.\n"
					"%lu KB total disk space.\n%lu KB available.\n",
					acc_files, acc_size, acc_dirs,
					(fs->n_fatent - 2) * (fs->csize / 2), dw * (fs->csize / 2)
			);
		} else {
			 put_rc(res);
		}
	} else {
		 put_rc(res);
	}
}



static
void auto_test (void)	/* 自動テスト */
{
	char *p;
	BYTE res, buf[512];
	UINT i, c;


	for (;;) {
		xputs("\n**** Insert a memory card or Type any key to start monitor ****\n");
		c = 50;
		do {
			delay_ms(10);
			if (scif_test()) {
				scif_get();
				xputc('\n');
				return;
			}
			if (disk_status(0) & STA_NODISK)
				c = 50;
		} while (--c);
		xputs("Card detected.\nInitializing...");

		if (disk_initialize(0) & STA_NOINIT) {
			xputs("failed.\n\n");
			continue;
		}
		xputs("succeeded.\n");

		xputs("\n\n---- Physical drive status ---------------------------------------------\n\n");

		show_disk_status(0);

		xputs("\n\n---- Physical sector 0 (MBR) dump ------------------------------------\n\n");

		res = disk_read(0, buf, 0, 1);
		if (res == RES_OK) {
			for (i = 0; i < 0x200; i += 16)
				put_dump(buf+i, i, 16);
		} else {
			xprintf("rc=%u\n", res);
		}

		xputs("\n\n---- FAT volume status ----------------------------------------------\n\n");

		f_mount(0, &Fatfs);
		p = (char*)buf;
		*p = 0;
		show_fs_status(p);

		xputs("\n\n**** Remove memory card ****\n");
		c = 50;
		do {
			delay_ms(10);
			if (!(disk_status(0) & STA_NODISK))
				c = 50;
		} while (--c);
	}
}



int main (void)
{
	char *ptr, *ptr2;
	long p1, p2, p3;
	BYTE dr;
	FRESULT fr;
	UINT s1, s2, cnt, blen = sizeof(Buff);
	DWORD ofs = 0, sect = 0;
	FATFS *fs;


	timer_init();			/* クロックの設定とタイマの起動 */
	scif_init();			/* scif.cの初期化 */

	xdev_in(scif_get);		/* monitor.cの入出力をscif.cに結合 */
	xdev_out(scif_put);

	/* 起動メッセージ */
	delay_ms(10);
	xputs("\nFatFs module test monitor for FRK-RN62N\n");
	xputs(_USE_LFN ? "LFN Enabled" : "LFN Disabled");
	xprintf(", Code page: %u/ANSI\n", _CODE_PAGE);

#if _USE_LFN
	Finfo.lfname = Lfname;
	Finfo.lfsize = sizeof(Lfname);
#endif

	auto_test();	/* 自動テストの実行 */

	for (;;) {
		xputc('>');
		xgets(Line, sizeof(Line));

		ptr = Line;
		switch (*ptr++) {
		case '?' :		/* ? - Show usage */
			xputs(HelpMsg);
			break;

		case 'm' :
			switch (*ptr++) {
			case 'd' :	/* md <address> [<count>] - Dump memory */
				if (!xatoi(&ptr, &p1)) break;
				if (!xatoi(&ptr, &p2)) p2 = 128;
				for (ptr=(char*)p1; p2 >= 16; ptr += 16, p2 -= 16)
					put_dump((BYTE*)ptr, (UINT)ptr, 16);
				if (p2) put_dump((BYTE*)ptr, (UINT)ptr, p2);
				break;
			}
			break;

		case 'd' :
			switch (*ptr++) {
			case 'd' :	/* dd [<lba>] - Dump secrtor */
				if (!xatoi(&ptr, &p2)) p2 = sect;
				dr = disk_read(0, Buff, p2, 1);
				if (dr) { xprintf("rc=%d\n", (WORD)dr); break; }
				sect = p2 + 1;
				xprintf("Sector:%lu\n", p2);
				for (ptr=(char*)Buff, ofs = 0; ofs < 0x200; ptr+=16, ofs+=16)
					put_dump((BYTE*)ptr, ofs, 16);
				break;

			case 'i' :	/* di - Initialize disk */
				xprintf("rc=%d\n", (WORD)disk_initialize(0));
				break;

			case 's' :	/* ds - Show disk status */
				show_disk_status(0);
				break;
			}
			break;

		case 'b' :
			switch (*ptr++) {
			case 'd' :	/* bd <addr> - Dump R/W buffer */
				if (!xatoi(&ptr, &p1)) break;
				for (ptr=(char*)&Buff[p1], ofs = p1, cnt = 32; cnt; cnt--, ptr+=16, ofs+=16)
					put_dump((BYTE*)ptr, ofs, 16);
				break;

			case 'e' :	/* be <addr> [<data>] ... - Edit R/W buffer */
				if (!xatoi(&ptr, &p1)) break;
				if (xatoi(&ptr, &p2)) {
					do {
						Buff[p1++] = (BYTE)p2;
					} while (xatoi(&ptr, &p2));
					break;
				}
				for (;;) {
					xprintf("0x%04X: 0x%02X-", (WORD)(p1), (WORD)Buff[p1]);
					xgets(Line, sizeof(Line));
					ptr = Line;
					if (*ptr == '.') break;				/* Exit */
					if (*ptr < ' ') { p1++; continue; }	/* Skip */
					if (xatoi(&ptr, &p2))
						Buff[p1++] = (BYTE)p2;
					else
						xputs("???\n");
				}
				break;

			case 'r' :	/* br <lba> [<num>] - Read disk into R/W buffer */
				if (!xatoi(&ptr, &p2)) break;
				if (!xatoi(&ptr, &p3)) p3 = 1;
				xprintf("rc=%u\n", (WORD)disk_read(0, Buff, p2, p3));
				break;

			case 'w' :	/* bw <lba> [<num>] - Write R/W buffer into disk */
				if (!xatoi(&ptr, &p2)) break;
				if (!xatoi(&ptr, &p3)) p3 = 1;
				xprintf("rc=%u\n", (WORD)disk_write(0, Buff, p2, p3));
				break;

			case 'f' :	/* bf <val> - Fill working buffer */
				if (!xatoi(&ptr, &p1)) break;
				xmemset(Buff, (BYTE)p1, sizeof(Buff));
				break;

			}
			break;

		case 'f' :
			switch (*ptr++) {

			case 'i' :	/* fi - Force initialized the logical drive */
				put_rc(f_mount(0, &Fatfs));
				break;

			case 's' :	/* fs - Show logical drive status */
				ptr = Line;
				*ptr = 0;
				show_fs_status(ptr);
				break;

			case 'l' :	/* fl [<path>] - Directory listing */
				while (*ptr == ' ') ptr++;
				fr = f_opendir(&Dir, ptr);
				if (fr) { put_rc(fr); break; }
				p1 = s1 = s2 = 0;
				for(;;) {
					fr = f_readdir(&Dir, &Finfo);
					if ((fr != FR_OK) || !Finfo.fname[0]) break;
					if (Finfo.fattrib & AM_DIR) {
						s2++;
					} else {
						s1++; p1 += Finfo.fsize;
					}
					xprintf("%c%c%c%c%c %u/%02u/%02u %02u:%02u %9lu  %-12s  %s\n",
							(Finfo.fattrib & AM_DIR) ? 'D' : '-',
							(Finfo.fattrib & AM_RDO) ? 'R' : '-',
							(Finfo.fattrib & AM_HID) ? 'H' : '-',
							(Finfo.fattrib & AM_SYS) ? 'S' : '-',
							(Finfo.fattrib & AM_ARC) ? 'A' : '-',
							(Finfo.fdate >> 9) + 1980, (Finfo.fdate >> 5) & 15, Finfo.fdate & 31,
							(Finfo.ftime >> 11), (Finfo.ftime >> 5) & 63,
							Finfo.fsize, Finfo.fname,
#if _USE_LFN
							Lfname);
#else
							"");
#endif
				}
				xprintf("%4u File(s),%10lu bytes total\n%4u Dir(s)", s1, p1, s2);
				if (f_getfree(ptr, (DWORD*)&p1, &fs) == FR_OK)
					xprintf(", %10lu bytes free\n", p1 * fs->csize * 512);
				break;

			case 'o' :	/* fo <mode> <file> - Open a file */
				if (!xatoi(&ptr, &p1)) break;
				while (*ptr == ' ') ptr++;
				put_rc(f_open(&File1, ptr, (BYTE)p1));
				break;

			case 'c' :	/* fc - Close a file */
				put_rc(f_close(&File1));
				break;

			case 'e' :	/* fe - Seek file pointer */
				if (!xatoi(&ptr, &p1)) break;
				fr = f_lseek(&File1, p1);
				put_rc(fr);
				if (fr == FR_OK)
					xprintf("fptr=%lu(0x%lX)\n", File1.fptr, File1.fptr);
				break;

			case 'd' :	/* fd <len> - read and dump file from current fp */
				if (!xatoi(&ptr, &p1)) break;
				ofs = File1.fptr;
				while (p1) {
					if ((UINT)p1 >= 16) { cnt = 16; p1 -= 16; }
					else 				{ cnt = p1; p1 = 0; }
					fr = f_read(&File1, Buff, cnt, &cnt);
					if (fr != FR_OK) { put_rc(fr); break; }
					if (!cnt) break;
					put_dump(Buff, ofs, cnt);
					ofs += 16;
				}
				break;

			case 'r' :	/* fr <len> - read file */
				if (!xatoi(&ptr, &p1)) break;
				p2 = 0;
				Timer = 0;
				while (p1) {
					if ((UINT)p1 >= blen) {
						cnt = blen; p1 -= blen;
					} else {
						cnt = p1; p1 = 0;
					}
					fr = f_read(&File1, Buff, cnt, &s2);
					if (fr != FR_OK) { put_rc(fr); break; }
					p2 += s2;
					if (cnt != s2) break;
				}
				xprintf("%lu bytes read with %lu kB/sec.\n", p2, Timer ? (p2 / Timer) : 0);
				break;

			case 'w' :	/* fw <len> <val> - write file */
				if (!xatoi(&ptr, &p1) || !xatoi(&ptr, &p2)) break;
				xmemset(Buff, (BYTE)p2, blen);
				p2 = 0;
				Timer = 0;
				while (p1) {
					if ((UINT)p1 >= blen) {
						cnt = blen; p1 -= blen;
					} else {
						cnt = p1; p1 = 0;
					}
					fr = f_write(&File1, Buff, cnt, &s2);
					if (fr != FR_OK) { put_rc(fr); break; }
					p2 += s2;
					if (cnt != s2) break;
				}
				xprintf("%lu bytes written with %lu kB/sec.\n", p2, Timer ? (p2 / Timer) : 0);
				break;

			case 'n' :	/* fn <old_name> <new_name> - Change file/dir name */
				while (*ptr == ' ') ptr++;
				ptr2 = xstrchr(ptr, ' ');
				if (!ptr2) break;
				*ptr2++ = 0;
				while (*ptr2 == ' ') ptr2++;
				put_rc(f_rename(ptr, ptr2));
				break;

			case 'u' :	/* fu <name> - Unlink a file or dir */
				while (*ptr == ' ') ptr++;
				put_rc(f_unlink(ptr));
				break;

			case 'v' :	/* fv - Truncate file */
				put_rc(f_truncate(&File1));
				break;

			case 'k' :	/* fk <name> - Create a directory */
				while (*ptr == ' ') ptr++;
				put_rc(f_mkdir(ptr));
				break;

			case 'a' :	/* fa <atrr> <mask> <name> - Change file/dir attribute */
				if (!xatoi(&ptr, &p1) || !xatoi(&ptr, &p2)) break;
				while (*ptr == ' ') ptr++;
				put_rc(f_chmod(ptr, p1, p2));
				break;

			case 't' :	/* ft <year> <month> <day> <hour> <min> <sec> <name> - Change timestamp */
				if (!xatoi(&ptr, &p1) || !xatoi(&ptr, &p2) || !xatoi(&ptr, &p3)) break;
				Finfo.fdate = ((p1 - 1980) << 9) | ((p2 & 15) << 5) | (p3 & 31);
				if (!xatoi(&ptr, &p1) || !xatoi(&ptr, &p2) || !xatoi(&ptr, &p3)) break;
				Finfo.ftime = ((p1 & 31) << 11) | ((p2 & 63) << 5) | ((p3 >> 1) & 31);
				put_rc(f_utime(ptr, &Finfo));
				break;

			case 'x' : /* fx <src_name> <dst_name> - Copy file */
				while (*ptr == ' ') ptr++;
				ptr2 = xstrchr(ptr, ' ');
				if (!ptr2) break;
				*ptr2++ = 0;
				while (*ptr2 == ' ') ptr2++;
				xprintf("Opening \"%s\"", ptr);
				fr = f_open(&File1, ptr, FA_OPEN_EXISTING | FA_READ);
				xputc('\n');
				if (fr) {
					put_rc(fr);
					break;
				}
				xprintf("Creating \"%s\"", ptr2);
				fr = f_open(&File2, ptr2, FA_CREATE_ALWAYS | FA_WRITE);
				xputc('\n');
				if (fr) {
					put_rc(fr);
					f_close(&File1);
					break;
				}
				xprintf("Copying file...");
				Timer = 0;
				p1 = 0;
				for (;;) {
					fr = f_read(&File1, Buff, blen, &s1);
					if (fr || s1 == 0) break;   /* error or eof */
					fr = f_write(&File2, Buff, s1, &s2);
					p1 += s2;
					if (fr || s2 < s1) break;   /* error or disk full */
				}
				xprintf("%lu bytes copied with %lu kB/sec.\n", p1, p1 / Timer);
				f_close(&File1);
				f_close(&File2);
				break;
#if _FS_RPATH >= 1
			case 'g' :	/* fg <path> - Change current directory */
				while (*ptr == ' ') ptr++;
				put_rc(f_chdir(ptr));
				break;
#if _FS_RPATH >= 2
			case 'q' :	/* fq - Show current dir path */
				fr = f_getcwd(Line, sizeof(Line));
				if (fr)
					put_rc(fr);
				else
					xprintf("%s\n", Line);
				break;
#endif
#endif
#if _USE_MKFS
			case 'm' :	/* fm <partition rule> <cluster size> - Create file system */
				if (!xatoi(&ptr, &p2) || !xatoi(&ptr, &p3)) break;
				xprintf("The card will be formatted. Are you sure? (Y/n)=");
				xgets(ptr, sizeof(Line));
				if (*ptr == 'Y')
					put_rc(f_mkfs(0, (BYTE)p2, (WORD)p3));
				break;
#endif
			case 'z' :	/* fz [<rw size>] - Change R/W length for fr/fw/fx command */
				if (xatoi(&ptr, &p1) && p1 >= 1 && p1 <= sizeof(Buff))
					blen = p1;
				xprintf("blen=%u\n", blen);
				break;
			}
			break;

		}
	}
}


