Index: asp3_tinet_ecnl_rx/trunk/ntshell/fatfs/ffarch.c
===================================================================
--- asp3_tinet_ecnl_rx/trunk/ntshell/fatfs/ffarch.c	(revision 337)
+++ asp3_tinet_ecnl_rx/trunk/ntshell/fatfs/ffarch.c	(revision 359)
@@ -54,5 +54,65 @@
 #include "mbed_api.h"
 
-//extern ntstdio_t ntstdio;
+extern ntstdio_t ntstdio;
+
+enum ffarch_state_t {
+	FFS_IDLE,
+	FFS_RETRY_WAIT,
+};
+
+enum ffarch_state_t ffarch_state;
+volatile int ffarch_timer;
+int ffarch_retry_count;
+uint8_t mmc_rspi_prev_status;
+
+static void ffarch_initialize();
+
+/*
+ * FatFSタスク
+ */
+void ffarch_task(intptr_t exinf)
+{
+	ER ret, ret2;
+	SYSTIM prev, now;
+	int timer;
+
+	ffarch_initialize();
+
+	ret2 = get_tim(&now);
+	if (ret2 != E_OK){
+		ntstdio_printf(&ntstdio, "[ffarch] get_tim error: %s",
+			itron_strerror(ret2));
+		return;
+	}
+
+	for(;;){
+		prev = now;
+
+		/* タイマー取得 */
+		timer = ffarch_get_timer();
+
+		/* 待ち */
+		ret = tslp_tsk(timer);
+		if ((ret != E_OK) && (ret != E_TMOUT)) {
+			ntstdio_printf(&ntstdio, "[ffarch] tslp_tsk error: %s %d",
+				itron_strerror(ret), timer);
+			break;
+		}
+
+		ret2 = get_tim(&now);
+		if (ret2 != E_OK) {
+			ntstdio_printf(&ntstdio, "[ffarch] get_tim error: %s",
+				itron_strerror(ret2));
+			break;
+		}
+
+		/* 時間経過 */
+		int elapse = now - prev;
+		ffarch_progress(elapse);
+
+		/* タイムアウト処理 */
+		ffarch_timeout();
+	}
+}
 
 FATFS RomDisk;
@@ -88,29 +148,26 @@
 	//sdfs_init(&sdfs, P8_5, P8_6, P8_3, P8_4, "sd");
 
-	result = SD_begin() ? 0 : -1;
-	if (result == 0) {
-		printf("SD card (1:) OK!\n");
+	ffarch_state = FFS_IDLE;
+	ffarch_timer = 0;
+	ffarch_retry_count = 3;
+	mmc_rspi_prev_status = STA_NODISK;
+
+	ffarch_timeout();
+
+	act_tsk(FFARCH_TASK);
+
+	if (romdisk_init()) {
+		ntstdio_printf(&ntstdio, "ROM disk (0:) OK!\n");
 	}
 	else {
-		printf("SD card (1:) NG!\n");
-	}
+		ntstdio_printf(&ntstdio, "ROM disk (0:) NG!\n");
+	}
+
+	serial_ctl_por(SIO_PORTID, IOCTL_FCSND | IOCTL_FCRCV);
+}
+
+void ffarch_initialize()
+{
 	sta_cyc(MMC_RSPI_CYC);
-
-	if (romdisk_init())
-		result = 0;
-
-	if (result == 0) {
-		printf("ROM disk (0:) OK!\n");
-	}
-	else {
-		printf("ROM disk (0:) NG!\n");
-	}
-
-	/* uploadディレクトリを作成しておく */
-	f_mkdir("1:/upload");
-
-	serial_ctl_por(SIO_PORTID, IOCTL_FCSND | IOCTL_FCRCV);
-
-	return result;
 }
 
@@ -216,4 +273,81 @@
 	}
 	return RES_PARERR;
+}
+
+void mmc_rspi_status_changed(DSTATUS newst)
+{
+	if (ffarch_state == FFS_IDLE) {
+		mmc_rspi_prev_status = mmc_rspi_status();
+		ffarch_timer = 0;
+		wup_tsk(FFARCH_TASK);
+	}
+}
+
+int ffarch_get_timer()
+{
+	return ffarch_timer;
+}
+
+void ffarch_progress(int elapse)
+{
+	if (ffarch_timer != TMO_FEVR) {
+		ffarch_timer -= elapse;
+		if (ffarch_timer < 0) {
+			ffarch_timer = 0;
+		}
+	}
+}
+
+void ffarch_timeout()
+{
+	if (ffarch_timer != 0)
+		return;
+
+	switch (ffarch_state) {
+	case FFS_RETRY_WAIT:
+		if (ffarch_retry_count == 0) {
+			ntstdio_printf(&ntstdio, "SD card (1:) initialize tired...\n");
+
+			ffarch_state = FFS_IDLE;
+			ffarch_timer = TMO_FEVR;
+			ffarch_retry_count = 3;
+			break;
+		}
+		ffarch_retry_count--;
+		//goto case FFS_IDLE;
+	case FFS_IDLE:
+ 		/* SDカードが入れられた場合 */
+ 		if (((mmc_rspi_prev_status & STA_NODISK) != 0) && ((mmc_rspi_status() & STA_NODISK) == 0)) {
+			ntstdio_printf(&ntstdio, "SD card initializing ...\n");
+
+			Sd.FatFs.fs_type = 0;
+			if (SD_begin()) {
+				ntstdio_printf(&ntstdio, "SD card (1:) OK!\n");
+
+				/* uploadディレクトリを作成しておく */
+				f_mkdir("1:/upload");
+
+				ffarch_state = FFS_IDLE;
+				ffarch_timer = TMO_FEVR;
+				ffarch_retry_count = 3;
+			}
+			else {
+				ntstdio_printf(&ntstdio, "SD card (1:) NG!\n");
+				ffarch_state = FFS_RETRY_WAIT;
+				ffarch_timer = 1000 * 1000;
+			}
+		}
+		/* SDカードが抜かれた場合 */
+		else if (((mmc_rspi_prev_status & STA_NODISK) == 0) && ((mmc_rspi_status() & STA_NODISK) != 0)) {
+			ntstdio_printf(&ntstdio, "SD card unmount\n");
+
+			f_mount(&Sd.FatFs, "1:", 0);
+			ffarch_state = FFS_IDLE;
+			ffarch_timer = TMO_FEVR;
+			ffarch_retry_count = 3;
+		}
+		mmc_rspi_prev_status = mmc_rspi_status();
+		break;
+	}
 }
 
