Index: EcnlProtoTool/trunk/ntshell/fatfs/ff.c
===================================================================
--- EcnlProtoTool/trunk/ntshell/fatfs/ff.c	(revision 434)
+++ EcnlProtoTool/trunk/ntshell/fatfs/ff.c	(revision 435)
@@ -17,8 +17,8 @@
 
 
+#include "shellif.h"
 #include "ff.h"			/* Declarations of FatFs API */
 #include "diskio.h"		/* Declarations of disk I/O functions */
 #include "util/ntstdio.h"
-#include "shellif.h"
 
 /*--------------------------------------------------------------------------
@@ -2617,5 +2617,5 @@
 	/* Get logical drive number */
 #if !FF_FS_READONLY
-	mode &= FA_READ | FA_WRITE | FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW;
+	mode &= FA_READ | FA_WRITE | FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW | FA_OPEN_APPEND;
 	res = find_volume(&dj.fs, &path, (BYTE)(mode & ~FA_READ));
 #else
Index: EcnlProtoTool/trunk/ntshell/fatfs/ff.h
===================================================================
--- EcnlProtoTool/trunk/ntshell/fatfs/ff.h	(revision 434)
+++ EcnlProtoTool/trunk/ntshell/fatfs/ff.h	(revision 435)
@@ -309,4 +309,5 @@
 #define	FA_CREATE_ALWAYS	0x08
 #define	FA_OPEN_ALWAYS		0x10
+#define	FA_OPEN_APPEND		0x30
 #define FA_MODIFIED			0x20
 #define FA_DIRTY			0x40
Index: EcnlProtoTool/trunk/ntshell/fatfs/ffarch.c
===================================================================
--- EcnlProtoTool/trunk/ntshell/fatfs/ffarch.c	(revision 434)
+++ EcnlProtoTool/trunk/ntshell/fatfs/ffarch.c	(revision 435)
@@ -53,4 +53,6 @@
 #include "util/ntstdio.h"
 
+#define SD_CARD_LOG_LEVEL LOG_INFO
+
 #define SIO_PORTID 1
 extern ntstdio_t ntstdio;
@@ -279,4 +281,13 @@
 }
 
+void sdfs_debug(const char *fmt, ...)
+{
+	va_list arp;
+
+	va_start(arp, fmt);
+	syslog(SD_CARD_LOG_LEVEL, fmt, arp);
+	va_end(arp);
+}
+
 int ff_cre_syncobj(BYTE vol, FF_SYNC_t* sobj)
 {
@@ -310,2 +321,18 @@
 	sys_free(mblock);
 }
+
+DWORD get_fattime(void)
+{
+	time_t temp;
+	struct tm _tm;
+
+	temp = rtc_read();
+	gmtime_r(&temp, &_tm);
+
+	return	  ((DWORD)(_tm.tm_year - 1980) << 25)
+		| ((DWORD)_tm.tm_mon << 21)
+		| ((DWORD)_tm.tm_mday << 16)
+		| ((DWORD)_tm.tm_hour << 11)
+		| ((DWORD)_tm.tm_min << 5)
+		| ((DWORD)_tm.tm_sec >> 1);
+}
Index: EcnlProtoTool/trunk/ntshell/fatfs/ffconf.h
===================================================================
--- EcnlProtoTool/trunk/ntshell/fatfs/ffconf.h	(revision 434)
+++ EcnlProtoTool/trunk/ntshell/fatfs/ffconf.h	(revision 435)
@@ -4,4 +4,6 @@
 
 #define FFCONF_DEF 64180	/* Revision ID */
+
+#define FFS_DBG			0
 
 /*---------------------------------------------------------------------------/
@@ -125,4 +127,12 @@
 
 
+#define FF_LFN_BUF		255
+#define FF_SFN_BUF		12
+/* This set of options defines size of file name members in the FILINFO structure
+/  which is used to read out directory items. These values should be suffcient for
+/  the file names to read. The maximum possible length of the read file name depends
+/  on character encoding. When LFN is not enabled, these options have no effect. */
+
+
 #define FF_FS_RPATH	2
 /* This option configures relative path feature.
Index: EcnlProtoTool/trunk/ntshell/fatfs/fftime.c
===================================================================
--- EcnlProtoTool/trunk/ntshell/fatfs/fftime.c	(revision 434)
+++ EcnlProtoTool/trunk/ntshell/fatfs/fftime.c	(revision 435)
@@ -254,18 +254,2 @@
 	return -1;
 }
-
-DWORD get_fattime(void)
-{
-	time_t temp;
-	struct tm _tm;
-
-	temp = rtc_read();
-	gmtime_r(&temp, &_tm);
-
-	return	  ((DWORD)(_tm.tm_year - 1980) << 25)
-		| ((DWORD)_tm.tm_mon << 21)
-		| ((DWORD)_tm.tm_mday << 16)
-		| ((DWORD)_tm.tm_hour << 11)
-		| ((DWORD)_tm.tm_min << 5)
-		| ((DWORD)_tm.tm_sec >> 1);
-}
Index: EcnlProtoTool/trunk/ntshell/fatfs/sdfs.c
===================================================================
--- EcnlProtoTool/trunk/ntshell/fatfs/sdfs.c	(revision 434)
+++ EcnlProtoTool/trunk/ntshell/fatfs/sdfs.c	(revision 435)
@@ -115,9 +115,12 @@
 	*/
 #include "sdfs.h"
+#include "diskio.h"
 //#include "mbed_debug.h"
 #include "mbed_wait_api.h"
-
-#define debug(str, ...)
-#define debug_if(cond, strt, ...) if(cond){ debug(str, __ARGS__); }
+#include "util/ntstdio.h"
+
+void sdfs_debug(const char *fmt, ...);
+#define debug(...) sdfs_debug(__VA_ARGS__)
+#define debug_if(cond, ...) if (cond) { sdfs_debug(__VA_ARGS__); }
 
 static int sdfs_initialise_card_v1(sdfs_t *obj);
@@ -134,5 +137,5 @@
 #define SD_DBG             0
 
-sdfs_init(sdfs_t *obj, PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name)
+void sdfs_init(sdfs_t *obj, PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name)
 {
 	obj->name = name;
@@ -144,4 +147,5 @@
     obj->_init_sck = 100000;
     obj->_transfer_sck = 1000000;
+	obj->_is_initialized = STA_NOINIT;
 }
 
@@ -154,10 +158,4 @@
 #define R1_PARAMETER_ERROR      (1 << 6)
 
-// Types
-#define SDCARD_FAIL 0 //!< v1.x Standard Capacity
-#define SDCARD_V1   1 //!< v2.x Standard Capacity
-#define SDCARD_V2   2 //!< v2.x High Capacity
-#define SDCARD_V2HC 3 //!< Not recognised as an SD Card
-
 int sdfs_initialise_card(sdfs_t *obj)
 {
@@ -173,5 +171,5 @@
 	if (sdfs__cmd(obj, 0, 0) != R1_IDLE_STATE) {
 		debug("No disk, or could not put SD card in to SPI idle state\n");
-		return SDCARD_FAIL;
+		return CT_FAIL;
 	}
 
@@ -186,5 +184,5 @@
 	else {
 		debug("Not in idle state after sending CMD8 (not an SD card?)\n");
-		return SDCARD_FAIL;
+		return CT_FAIL;
 	}
 }
@@ -192,50 +190,82 @@
 static int sdfs_initialise_card_v1(sdfs_t *obj)
 {
+	sdfs__cmd(obj, 55, 0);
+	if (sdfs__cmd(obj, 41, 0) <= 1) {
+		obj->cdv = 512;
+		debug_if(SD_DBG, "\n\rInit: SEDCARD_V1\n\r");
+		for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
+			sdfs__cmd(obj, 55, 0);
+			if (sdfs__cmd(obj, 41, 0) == 0) {
+				break;
+			}
+		}
+		return CT_SD1;
+	}
+	else {
+		for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
+			if (sdfs__cmd(obj, 1, 0) == 0)
+				break;
+		}
+		return CT_MMC;
+	}
+	//if (!Timer1 || sdfs__cmd(obj, 16, 512) != 0)	/* Set block length: 512 */
+	//	ty = 0;
+
+	debug("Timeout waiting for v1.x card\n");
+	return CT_FAIL;
+}
+
+static int sdfs_initialise_card_v2(sdfs_t *obj)
+{
+	int n;
+	uint8_t ocr[4];
+
+	/* Get 32 bit return value of R7 resp */
+	for (n = 0; n < 4; n++) ocr[n] = spi_master_write(&obj->_spi, 0xFF);
 	for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
 		sdfs__cmd(obj, 55, 0);
-		if (sdfs__cmd(obj, 41, 0) == 0) {
-			obj->cdv = 512;
-			debug_if(SD_DBG, "\n\rInit: SEDCARD_V1\n\r");
-			return SDCARD_V1;
-		}
-	}
-
-	debug("Timeout waiting for v1.x card\n");
-	return SDCARD_FAIL;
-}
-
-static int sdfs_initialise_card_v2(sdfs_t *obj)
-{
-	for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
+		if (sdfs__cmd(obj, 41, 0x40000000) == 0) {
+			if (sdfs__cmd58(obj) == 0) {
+				for (n = 0; n < 4; n++) ocr[n] = spi_master_write(&obj->_spi, 0xFF);
+				debug_if(SD_DBG, "\n\rInit: SDCARD_V2\n\r");
+				obj->cdv = 1;
+				return (ocr[0] & 0x40) ? CT_SD2 | CT_BLOCK : CT_SD2;
+			}
+		}
 		wait_ms(50);
-		sdfs__cmd58(obj);
-		sdfs__cmd(obj, 55, 0);
-		if (sdfs__cmd(obj, 41, 0x40000000) == 0) {
-			sdfs__cmd58(obj);
-			debug_if(SD_DBG, "\n\rInit: SDCARD_V2\n\r");
-			obj->cdv = 1;
-			return SDCARD_V2;
-		}
 	}
 
 	debug("Timeout waiting for v2.x card\n");
-	return SDCARD_FAIL;
+	return CT_FAIL;
 }
 
 int sdfs_initialize(sdfs_t *obj)
 {
-	obj->_is_initialized = sdfs_initialise_card(obj);
-	if (obj->_is_initialized == 0) {
+	obj->_is_initialized |= STA_NOINIT;
+
+	obj->_card_type = sdfs_initialise_card(obj);
+	if (obj->_card_type == 0) {
 		debug("Fail to initialize card\n");
 		return 1;
 	}
-	debug_if(SD_DBG, "init card = %d\n", obj->_is_initialized);
+	debug_if(SD_DBG, "init card = %x\n", obj->_is_initialized);
+
 	obj->_sectors = sdfs__sd_sectors(obj);
+
+	if (sdfs__cmdx(obj, 10, 0) != 0		/* READ_CID */
+		|| sdfs__read(obj, obj->_cid, 16) != 0) {
+		obj->_card_type = CT_FAIL;
+		debug("Fail to read cid\n");
+		return 1;
+	}
 
 	// Set block length to 512 (CMD16)
 	if (sdfs__cmd(obj, 16, 512) != 0) {
+		obj->_card_type = CT_FAIL;
 		debug("Set 512-byte block timed out\n");
 		return 1;
 	}
+
+	obj->_is_initialized &= ~STA_NOINIT;
 
 	// Set SCK for data transfer
@@ -246,5 +276,5 @@
 int sdfs_write(sdfs_t *obj, const uint8_t *buffer, uint32_t block_number, uint32_t count)
 {
-	if (!obj->_is_initialized) {
+	if (obj->_is_initialized & (STA_NOINIT | STA_PROTECT)) {
 		return -1;
 	}
@@ -266,5 +296,5 @@
 int sdfs_read(sdfs_t *obj, uint8_t *buffer, uint32_t block_number, uint32_t count)
 {
-	if (!obj->_is_initialized) {
+	if (obj->_is_initialized & STA_NOINIT) {
 		return -1;
 	}
@@ -287,5 +317,5 @@
 {
 	// FATFileSystem::disk_status() returns 0 when initialized
-	if (obj->_is_initialized) {
+	if ((obj->_is_initialized & STA_NOINIT) == 0) {
 		return 0;
 	}
@@ -294,5 +324,5 @@
 	}
 }
-int sdfs_sync(sdfs_t *obj) { return 0; }
+
 uint64_t sdfs_sectors(sdfs_t *obj) { return obj->_sectors; }
 
@@ -477,5 +507,5 @@
 {
 	uint32_t c_size, c_size_mult, read_bl_len;
-	uint32_t block_len, mult, blocknr, capacity;
+	uint32_t mult, blocknr;
 	uint32_t hc_c_size;
 	uint64_t blocks;
@@ -487,6 +517,5 @@
 	}
 
-	uint8_t csd[16];
-	if (sdfs__read(obj, csd, 16) != 0) {
+	if (sdfs__read(obj, obj->_csd, 16) != 0) {
 		debug("Couldn't read csd response from disk\n");
 		return 0;
@@ -498,24 +527,24 @@
 	// read_bl_len   : csd[83:80] - the *maximum* read block length
 
-	int csd_structure = sdfs_ext_bits(obj, csd, 127, 126);
+	int csd_structure = sdfs_ext_bits(obj, obj->_csd, 127, 126);
 
 	switch (csd_structure) {
 	case 0:
 		obj->cdv = 512;
-		c_size = sdfs_ext_bits(obj, csd, 73, 62);
-		c_size_mult = sdfs_ext_bits(obj, csd, 49, 47);
-		read_bl_len = sdfs_ext_bits(obj, csd, 83, 80);
-
-		block_len = 1 << read_bl_len;
+		c_size = sdfs_ext_bits(obj, obj->_csd, 73, 62);
+		c_size_mult = sdfs_ext_bits(obj, obj->_csd, 49, 47);
+		read_bl_len = sdfs_ext_bits(obj, obj->_csd, 83, 80);
+
+		obj->_block_len = 1 << read_bl_len;
 		mult = 1 << (c_size_mult + 2);
 		blocknr = (c_size + 1) * mult;
-		capacity = blocknr * block_len;
-		blocks = capacity / 512;
-		debug_if(SD_DBG, "\n\rSDCard\n\rc_size: %d \n\rcapacity: %ld \n\rsectors: %lld\n\r", c_size, capacity, blocks);
+		obj->_capacity = blocknr * obj->_block_len;
+		blocks = obj->_capacity / 512;
+		debug_if(SD_DBG, "\n\rSDCard\n\rc_size: %d \n\rcapacity: %ld \n\rsectors: %lld\n\r", c_size, obj->_capacity, blocks);
 		break;
 
 	case 1:
 		obj->cdv = 1;
-		hc_c_size = sdfs_ext_bits(obj, csd, 63, 48);
+		hc_c_size = sdfs_ext_bits(obj, obj->_csd, 63, 48);
 		blocks = (hc_c_size + 1) * 1024;
 		debug_if(SD_DBG, "\n\rSDHC Card \n\rhc_c_size: %d\n\rcapacity: %lld \n\rsectors: %lld\n\r", hc_c_size, blocks * 512, blocks);
@@ -528,2 +557,113 @@
 	return blocks;
 }
+
+static
+int wait_ready(sdfs_t *obj, uint32_t wt)
+{
+	/* Wait until card goes ready or timeout */
+	for (int i = 0; i < wt; i++) {
+		if (spi_master_write(&obj->_spi, 0xFF) == 0xFF)
+			/* Card goes ready */
+			return 1;
+	}
+
+	/* Timeout occured */
+	return 0;
+}
+
+static
+void deselect(sdfs_t *obj)
+{
+	/* Set CS# high */
+	gpio_write(&obj->_cs, 0);
+
+	/* Dummy clock (force DO hi-z for multiple slave SPI) */
+	spi_master_write(&obj->_spi, 0xFF);
+}
+
+static
+int select(sdfs_t *obj)
+{
+	/* Set CS# low */
+	gpio_write(&obj->_cs, 1);
+
+	/* Dummy clock (force DO enabled) */
+	spi_master_write(&obj->_spi, 0xFF);
+
+	/* Wait for card ready */
+	if (wait_ready(obj, 500))
+		return 0;
+
+	deselect(obj);
+
+	/* Failed to select the card due to timeout */
+	return -1;
+}
+
+int sdfs_sync(sdfs_t *obj)
+{
+	int ret = select(obj);
+	deselect(obj);
+	return ret;
+}
+
+int sdfs_trim(sdfs_t *obj, uint32_t st, uint32_t ed)
+{
+	int res = -1;
+
+	for(;;) {
+		/* Check if the card is SDC */
+		if (!(obj->_card_type & CT_SDC))
+			break;
+		/* Check if sector erase can be applied to the card */
+		if (!(obj->_csd[0] >> 6) && !(obj->_csd[10] & 0x40))
+			break;
+		/* Load sector block */
+		if (!(obj->_card_type & CT_BLOCK)) {
+			st *= 512; ed *= 512;
+		}
+		/* Erase sector block */
+		if ((sdfs__cmd(obj, 32, st) == 0)
+			&& (sdfs__cmd(obj, 33, ed) == 0)
+			&& (sdfs__cmd(obj, 38, 0) == 0)
+			&& wait_ready(obj, 30000))
+			res = 0;
+	}
+
+	return res;
+}
+
+int sdfs_get_ocr(sdfs_t *obj, uint8_t buff[4])
+{
+	int res = -1, n;
+
+	/* READ_OCR */
+	if (sdfs__cmd58(obj) == 0) {
+		for (n = 4; n; n--) {
+			*buff++ = spi_master_write(&obj->_spi, 0xFF);
+		}
+		res = 0;
+	}
+
+	deselect(obj);
+
+	return res;
+}
+
+int sdfs_get_sdstat(sdfs_t *obj, uint8_t buff[64])
+{
+	int res = -1;
+
+	sdfs__cmd(obj, 55, 0);
+
+	/* SD_STATUS */
+	if (sdfs__cmd(obj, 13, 0) == 0) {
+		spi_master_write(&obj->_spi, 0xFF);
+		if (sdfs__read(obj, buff, 64) == 0)
+			return 0;
+	}
+
+	deselect(obj);
+
+	return res;
+}
Index: EcnlProtoTool/trunk/ntshell/fatfs/sdfs.h
===================================================================
--- EcnlProtoTool/trunk/ntshell/fatfs/sdfs.h	(revision 434)
+++ EcnlProtoTool/trunk/ntshell/fatfs/sdfs.h	(revision 435)
@@ -34,4 +34,12 @@
 #endif
 
+/* MMC card type flags (MMC_GET_TYPE) */
+#define CT_FAIL		0x00
+#define CT_MMC		0x01		/* MMC ver 3 */
+#define CT_SD1		0x02		/* SD ver 1 */
+#define CT_SD2		0x04		/* SD ver 2 */
+#define CT_SDC		(CT_SD1|CT_SD2)	/* SD */
+#define CT_BLOCK	0x08		/* Block addressing */
+
 /** Access the filesystem on an SD Card using SPI
 	*
@@ -51,4 +59,9 @@
 typedef struct sdfs_s{
 	const char* name;
+	volatile uint8_t _is_initialized;
+	uint8_t _card_type;
+	uint8_t _csd[16], _cid[16];
+	uint64_t _capacity;
+	uint64_t _block_len;
 	uint64_t _sectors;
 	spi_t _spi;
@@ -57,5 +70,4 @@
     uint32_t _init_sck;
     uint32_t _transfer_sck;
-	int _is_initialized;
 } sdfs_t;
 
@@ -68,5 +80,5 @@
 	* @param name The name used to access the virtual filesystem
 	*/
-sdfs_init(sdfs_t *obj, PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name);
+void sdfs_init(sdfs_t *obj, PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name);
 
 int sdfs_initialize(sdfs_t *obj);
@@ -76,4 +88,7 @@
 int sdfs_sync(sdfs_t *obj);
 uint64_t sdfs_sectors(sdfs_t *obj);
+int sdfs_trim(sdfs_t *obj, uint32_t st, uint32_t ed);
+int sdfs_get_ocr(sdfs_t *obj, uint8_t buff[4]);
+int sdfs_get_sdstat(sdfs_t *obj, uint8_t buff[64]);
 
 #ifdef __cplusplus
Index: EcnlProtoTool/trunk/ntshell/ntshell/usrcmd.c
===================================================================
--- EcnlProtoTool/trunk/ntshell/ntshell/usrcmd.c	(revision 434)
+++ EcnlProtoTool/trunk/ntshell/ntshell/usrcmd.c	(revision 435)
@@ -330,5 +330,5 @@
 
 	while ((res == FR_OK) && (fno.fname[0] != 0)) {
-		if (pattern_p != NULL && (fno.fattrib & AM_DIR) && ((fno.fname[0] == '.') ? (pattern_p[0] == '.') : 1)) {/* FATFS_DIR とパターンマッチしている場合は FATFS_DIR 内部を ls する */
+		if (pattern_p != NULL && (fno.fattrib & AM_DIR) && ((fno.fname[0] == '.') ? (pattern_p[0] == '.') : 1)) {/* DIR とパターンマッチしている場合は DIR 内部を ls する */
 #if FF_USE_LFN
 			fn = *fno.lfname ? fno.lfname : fno.fname;
@@ -374,5 +374,7 @@
 	f_closedir(&dir);
 
+#if FF_USE_LFN
 	if (lfn			!= NULL) ff_memfree(lfn);
+#endif
 	if (path_backup != NULL) ff_memfree(path_backup);
 }
Index: EcnlProtoTool/trunk/ntshell/src/main.h
===================================================================
--- EcnlProtoTool/trunk/ntshell/src/main.h	(revision 434)
+++ EcnlProtoTool/trunk/ntshell/src/main.h	(revision 435)
@@ -1,6 +1,6 @@
 /*
- *  TOPPERS ECHONET Lite Communication Middleware
+ *  TOPPERS PROJECT Home Network Working Group Software
  * 
- *  Copyright (C) 2014-2017 Cores Co., Ltd. Japan
+ *  Copyright (C) 2014-2019 Cores Co., Ltd. Japan
  * 
  *  上記著作権者は，以下の(1)～(4)の条件を満たす場合に限り，本ソフトウェ
