Index: asp3_tinet_ecnl_rx/trunk/ntshell/src/echonet_app_config.h
===================================================================
--- asp3_tinet_ecnl_rx/trunk/ntshell/src/echonet_app_config.h	(revision 359)
+++ asp3_tinet_ecnl_rx/trunk/ntshell/src/echonet_app_config.h	(revision 364)
@@ -40,4 +40,8 @@
 
 #define ECHONET_TASK_STACK_SIZE	1024	/* ECNL UDPタスク用スタック領域のサイズ */
+
+#define ECHONET_API_DATAQUEUE_COUNT 5		/* ECHONET Lite API用データキュー */
+#define ECHONET_SVC_DATAQUEUE_COUNT 5		/* ECHONET Lite サービス処理タスク用データキュー */
+#define ECHONET_UDP_DATAQUEUE_COUNT 5		/* ECHONET Lite UDP通信処理用データキュー */
 
 /*
Index: asp3_tinet_ecnl_rx/trunk/ntshell/src/fdtable.c
===================================================================
--- asp3_tinet_ecnl_rx/trunk/ntshell/src/fdtable.c	(revision 359)
+++ asp3_tinet_ecnl_rx/trunk/ntshell/src/fdtable.c	(revision 364)
@@ -45,5 +45,8 @@
 #include "target_syssvc.h"
 #ifndef NTSHELL_NO_SOCKET
+#include <tinet_defs.h>
 #include <tinet_config.h>
+#include <net/net.h>
+#include <net/net_endian.h>
 #include <netinet/in.h>
 #include <netinet/in_itron.h>
@@ -55,7 +58,9 @@
 #include <net/net.h>
 #include <net/if_var.h>
+#include <netinet/udp.h>
 #include <netinet/udp_var.h>
 #include <netinet/tcp.h>
 #include <netinet/tcp_var.h>
+#include <net/net_buf.h>
 #endif
 #include "ff.h"
@@ -69,5 +74,5 @@
 #define IO_TYPE_DIR		3
 #define IO_TYPE_TCP		4
-#define IO_TYPE_UDP		5
+#define IO_TYPE_UDP	5
 
 static struct _IO_FILE fd_table[8 * sizeof(FLGPTN)] = {
@@ -85,4 +90,5 @@
 			continue;
 
+		memset(fp, 0, sizeof(struct _IO_FILE));
 		fp->fd = fd;
 		fp->type = type;
@@ -280,4 +286,15 @@
 #endif
 
+void memor(void *dst, void *src, size_t len)
+{
+	uint8_t *d = (uint8_t *)dst;
+	uint8_t *s = (uint8_t *)src;
+	uint8_t *e = &s[len];
+
+	while (s < e) {
+		*d++ |= *s++;
+	}
+}
+
 struct fd_events {
 	int count;
@@ -304,20 +321,35 @@
 	}
 
-	memcpy(&evts.readfds, rfds, sizeof(fd_set));
-	memcpy(&evts.writefds, wfds, sizeof(fd_set));
-	memcpy(&evts.errorfds, efds, sizeof(fd_set));
+	if (rfds != NULL)
+		memcpy(&evts.readfds, rfds, sizeof(fd_set));
+	else
+		memset(&evts.readfds, 0, sizeof(fd_set));
+	if (wfds != NULL)
+		memcpy(&evts.writefds, wfds, sizeof(fd_set));
+	else
+		memset(&evts.writefds, 0, sizeof(fd_set));
+	if (efds != NULL)
+		memcpy(&evts.errorfds, efds, sizeof(fd_set));
+	else
+		memset(&evts.errorfds, 0, sizeof(fd_set));
 	evts.count = 0;
 
 	ret = shell_get_evts(&evts, tmout);
+	if (rfds != NULL)
+		memset(rfds, 0, sizeof(fd_set));
+	if (wfds != NULL)
+		memset(wfds, 0, sizeof(fd_set));
+	if (efds != NULL)
+		memset(efds, 0, sizeof(fd_set));
 	if (ret == E_OK) {
-		memcpy(rfds, &evts.readfds, sizeof(fd_set));
-		memcpy(wfds, &evts.writefds, sizeof(fd_set));
-		memcpy(efds, &evts.errorfds, sizeof(fd_set));
+		if (rfds != NULL)
+			memor(rfds, &evts.readfds, sizeof(fd_set));
+		if (wfds != NULL)
+			memor(wfds, &evts.writefds, sizeof(fd_set));
+		if (efds != NULL)
+			memor(efds, &evts.errorfds, sizeof(fd_set));
 		return evts.count;
 	}
 	if (ret == E_TMOUT) {
-		memset(rfds, 0, sizeof(fd_set));
-		memset(wfds, 0, sizeof(fd_set));
-		memset(efds, 0, sizeof(fd_set));
 		return 0;
 	}
@@ -441,4 +473,6 @@
 	struct _IO_FILE *fp = tcpid_to_fd(cepid);
 	FLGPTN flgptn = 0;
+	ER ret;
+	int len;
 
 	if (fp == NULL)
@@ -449,5 +483,39 @@
 
 	switch (fncd) {
+	case TFN_TCP_RCV_BUF:
+		len = *(int *)p_parblk;
+		if (len <= 0)
+			return E_OK;
+
+		ret = wai_sem(SEM_FILEDESC);
+		if (ret < 0) {
+			syslog(LOG_ERROR, "wai_sem => %d", ret);
+		}
+		fp->psock->len += len;
+		ret = sig_sem(SEM_FILEDESC);
+		if (ret < 0) {
+			syslog(LOG_ERROR, "sig_sem => %d", ret);
+		}
+
+		if (fp->readevt_w == fp->readevt_r) fp->readevt_w++;
+
+		set_flg(FLG_SELECT_WAIT, flgptn);
+		return E_OK;
+
 	case TFN_TCP_RCV_DAT:
+		len = *(int *)p_parblk;
+		if (len <= 0)
+			return E_OK;
+
+		ret = wai_sem(SEM_FILEDESC);
+		if (ret < 0) {
+			syslog(LOG_ERROR, "wai_sem => %d", ret);
+		}
+		fp->psock->len += len;
+		ret = sig_sem(SEM_FILEDESC);
+		if (ret < 0) {
+			syslog(LOG_ERROR, "sig_sem => %d", ret);
+		}
+
 		if (fp->readevt_w == fp->readevt_r) fp->readevt_w++;
 
@@ -484,4 +552,5 @@
 	struct _IO_FILE *fp = udpid_to_fd(cepid);
 	FLGPTN flgptn = 0;
+	int len;
 
 	if (fp == NULL)
@@ -492,6 +561,47 @@
 
 	switch (fncd) {
+	case TEV_UDP_RCV_DAT:
+	{
+		T_UDP_RCV_DAT_PARA *udppara = (T_UDP_RCV_DAT_PARA *)p_parblk;
+		len = udppara->len;
+		if (len <= 0)
+			return E_OK;
+
+		ER ret = wai_sem(SEM_FILEDESC);
+		if (ret < 0) {
+			syslog(LOG_ERROR, "wai_sem => %d", ret);
+		}
+		fp->psock->len = len;
+		if (fp->psock->input != NULL) {
+			ret = rel_net_buf(fp->psock->input);
+			if (ret < 0) {
+				syslog(LOG_ERROR, "rel_net_buf => %d", ret);
+			}
+		}
+		fp->psock->input = udppara->input;
+		fp->psock->buf = GET_UDP_SDU(udppara->input, udppara->off);
+		memset(&fp->psock->raddr4, 0, sizeof(fp->psock->raddr4));
+		fp->psock->raddr4.sin_family = AF_INET;
+		fp->psock->raddr4.sin_port = htons(udppara->rep4.portno);
+		fp->psock->raddr4.sin_addr.s_addr = htonl(udppara->rep4.ipaddr);
+		udppara->input->flags |= NB_FLG_NOREL_IFOUT;
+		ret = sig_sem(SEM_FILEDESC);
+		if (ret < 0) {
+			syslog(LOG_ERROR, "sig_sem => %d", ret);
+		}
+
+		if (fp->readevt_w == fp->readevt_r) fp->readevt_w++;
+
+		set_flg(FLG_SELECT_WAIT, flgptn);
+		return E_OK;
+	}
 	case TFN_UDP_CRE_CEP:
+		return E_OK;
+
 	case TFN_UDP_RCV_DAT:
+		len = *(int *)p_parblk;
+		if (len <= 0)
+			return E_OK;
+
 		if (fp->readevt_w == fp->readevt_r) fp->readevt_w++;
 
@@ -524,101 +634,149 @@
 ER shell_get_evts(struct fd_events *evts, TMO tmout)
 {
-	ER ret;
-	FLGPTN waitptn, flgptn = 0, readfds = 0, writefds = 0;
-	struct _IO_FILE *fp = NULL;
 	int count = 0;
-
-	stdio_update_evts();
+	SYSTIM prev, now;
+
+	get_tim(&prev);
+
+	for (;;) {
+		ER ret;
+		FLGPTN waitptn, flgptn, readfds = 0, writefds = 0;
+		struct _IO_FILE *fp = NULL;
+
+		stdio_update_evts();
 
 #ifndef NTSHELL_NO_SOCKET
-	waitptn = *((FLGPTN *)&evts->errorfds);
+		waitptn = *((FLGPTN *)&evts->errorfds);
 #else
-	waitptn = *((FLGPTN *)&evts->readfds) | *((FLGPTN *)&evts->errorfds);
+		waitptn = *((FLGPTN *)&evts->readfds) | *((FLGPTN *)&evts->errorfds);
 #endif
-	for (int fd = 0; fd < fd_table_count; fd++) {
-		fp = &fd_table[fd];
+		for (int fd = 0; fd < fd_table_count; fd++) {
+			fp = &fd_table[fd];
 
 #ifndef NTSHELL_NO_SOCKET
-		if (FD_ISSET(fd, &evts->readfds)) {
-			if ((fp->type == IO_TYPE_TCP)
-				&& (GET_TCP_CEP(fp->psock->cepid)->rwbuf_count > 0)) {
-				FD_SET(fd, (fd_set *)&readfds);
-				count++;
-				if (fp->readevt_w == fp->readevt_r) fp->readevt_r--;
+			if (FD_ISSET(fd, &evts->readfds)) {
+				if ((fp->type == IO_TYPE_TCP) && (fp->psock->cepid != 0)) {
+					if (fp->psock->len == 0) {
+						ret = tcp_rcv_buf(fp->psock->cepid, &fp->psock->input, TMO_NBLK);
+						if ((ret != E_WBLK) && (ret != E_OBJ) && (ret < 0)) {
+							syslog(LOG_ERROR, "tcp_rcv_buf => %d", ret);
+							//return ret;
+						}
+						if (ret > 0) {
+							ret = wai_sem(SEM_FILEDESC);
+							if (ret < 0) {
+								syslog(LOG_ERROR, "wai_sem => %d", ret);
+							}
+							fp->psock->len += ret;
+							ret = sig_sem(SEM_FILEDESC);
+							if (ret < 0) {
+								syslog(LOG_ERROR, "sig_sem => %d", ret);
+							}
+						}
+					}
+					else ret = 1;
+					if (ret > 0) {
+						FD_SET(fd, (fd_set *)&readfds);
+						count++;
+						if (fp->readevt_w == fp->readevt_r) fp->readevt_r--;
+					}
+				}
+				else if ((fp->type == IO_TYPE_UDP) && (fp->psock->cepid != 0)) {
+					if (fp->psock->input != NULL) {
+						FD_SET(fd, (fd_set *)&readfds);
+						count++;
+						if (fp->readevt_w == fp->readevt_r) fp->readevt_r--;
+					}
+				}
+				else {
+					FD_SET(fd, (fd_set *)&waitptn);
+				}
 			}
-			else {
-				FD_SET(fd, (fd_set *)&waitptn);
+#endif
+			if (FD_ISSET(fd, &evts->writefds)) {
+				if (fp->writeevt_w == fp->writeevt_r) {
+					FD_SET(fd, (fd_set *)&writefds);
+					count++;
+					if (fp->writeevt_w == fp->writeevt_r) fp->writeevt_r--;
+				}
+				else {
+					FD_SET(fd, (fd_set *)&waitptn);
+				}
 			}
 		}
-#endif
-		if (FD_ISSET(fd, &evts->writefds)) {
-			if (fp->writable) {
-				FD_SET(fd, (fd_set *)&writefds);
+		memset(evts, 0, sizeof(*evts));
+
+		if (waitptn == 0) {
+			memcpy(&evts->readfds, &readfds, sizeof(evts->readfds));
+			memcpy(&evts->writefds, &writefds, sizeof(evts->writefds));
+			evts->count = count;
+			return E_OK;
+		}
+		else if ((readfds | writefds) != 0) {
+			set_flg(FLG_SELECT_WAIT, (readfds | writefds));
+		}
+
+		/* イベント待ち */
+		flgptn = 0;
+		ret = twai_flg(FLG_SELECT_WAIT, waitptn, TWF_ORW, &flgptn, tmout);
+		if (ret != E_OK) {
+			if (ret != E_TMOUT) {
+				syslog(LOG_ERROR, "twai_flg => %d", ret);
+				return ret;
+			}
+
+			stdio_flgptn(&flgptn);
+
+			if (flgptn == 0)
+				return E_TMOUT;
+		}
+		flgptn &= waitptn;
+
+		/* 受け取ったフラグのみクリア */
+		ret = clr_flg(FLG_SELECT_WAIT, ~flgptn);
+		if (ret != E_OK) {
+			syslog(LOG_ERROR, "clr_flg => %d", ret);
+		}
+
+		count = 0;
+		for (int fd = 0; fd < fd_table_count; fd++) {
+			if (!FD_ISSET(fd, (fd_set *)&waitptn))
+				continue;
+
+			fp = &fd_table[fd];
+
+			if (fp->readevt_w != fp->readevt_r) {
+				fp->readevt_r++;
+				FD_SET(fd, &evts->readfds);
 				count++;
 			}
-			else {
-				FD_SET(fd, (fd_set *)&waitptn);
+			if (fp->writeevt_w != fp->writeevt_r) {
+				fp->writeevt_r++;
+				fp->writable = 1;
 			}
-		}
-	}
-	memset(evts, 0, sizeof(*evts));
-
-	if (waitptn == 0) {
-		memcpy(&evts->readfds, &readfds, sizeof(evts->readfds));
-		memcpy(&evts->writefds, &writefds, sizeof(evts->writefds));
-		evts->count = count;
-		return E_OK;
-	}
-	else if ((readfds | writefds) != 0){
-		set_flg(FLG_SELECT_WAIT, (readfds | writefds));
-	}
-
-	/* イベント待ち */
-	ret = twai_flg(FLG_SELECT_WAIT, waitptn, TWF_ORW, &flgptn, tmout);
-	if (ret != E_OK) {
-		if (ret != E_TMOUT) {
-			syslog(LOG_ERROR, "twai_flg => %d", ret);
-			return ret;
-		}
-
-		stdio_flgptn(&flgptn);
-
-		if (flgptn == 0)
+			if (fp->writable) {
+				FD_SET(fd, &evts->writefds);
+				count++;
+			}
+			if (fp->errorevt_w != fp->errorevt_r) {
+				fp->errorevt_r++;
+				FD_SET(fd, &evts->errorfds);
+				count++;
+			}
+		}
+
+		if (count > 0)
+			break;
+
+		get_tim(&now);
+
+		SYSTIM elapse = now - prev;
+		if (elapse > tmout)
 			return E_TMOUT;
-	}
-	flgptn &= waitptn;
-
-	/* 受け取ったフラグのみクリア */
-	ret = clr_flg(FLG_SELECT_WAIT, ~flgptn);
-	if (ret != E_OK) {
-		syslog(LOG_ERROR, "clr_flg => %d", ret);
-	}
-
-	count = 0;
-	for (int fd = 0; fd < fd_table_count; fd++) {
-		if (!FD_ISSET(fd, (fd_set *)&waitptn))
-			continue;
-
-		fp = &fd_table[fd];
-
-		if (fp->readevt_w != fp->readevt_r) {
-			fp->readevt_r++;
-			FD_SET(fd, &evts->readfds);
-			count++;
-		}
-		if (fp->writeevt_w != fp->writeevt_r) {
-			fp->writeevt_r++;
-			fp->writable = 1;
-		}
-		if (fp->writable) {
-			FD_SET(fd, &evts->writefds);
-			count++;
-		}
-		if (fp->errorevt_w != fp->errorevt_r) {
-			fp->errorevt_r++;
-			FD_SET(fd, &evts->errorfds);
-			count++;
-		}
-	}
+
+		prev = now;
+		tmout -= elapse;
+	}
+
 	evts->count = count;
 
@@ -676,9 +834,4 @@
 }
 
-ssize_t shell_send(int fd, const void *buf, size_t len, int flags)
-{
-	return -ENOMEM;
-}
-
 ssize_t shell_sendto(int fd, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t alen)
 {
@@ -691,10 +844,5 @@
 }
 
-ssize_t shell_recv(int fd, void *buf, size_t len, int flags)
-{
-	return -ENOMEM;
-}
-
-ssize_t shell_recvfrom(int fd, void *buf, size_t len, int flags, struct sockaddr *__restrict addr, socklen_t *__restrict alen)
+ssize_t shell_recvfrom(int fd, void *__restrict buf, size_t len, int flags, struct sockaddr *__restrict addr, socklen_t *__restrict alen)
 {
 	return -ENOMEM;
Index: asp3_tinet_ecnl_rx/trunk/ntshell/src/mbed_api.c
===================================================================
--- asp3_tinet_ecnl_rx/trunk/ntshell/src/mbed_api.c	(revision 359)
+++ asp3_tinet_ecnl_rx/trunk/ntshell/src/mbed_api.c	(revision 364)
@@ -1,7 +1,7 @@
 /*
  *  TOPPERS ECHONET Lite Communication Middleware
- * 
- *  Copyright (C) 2014-2017 Cores Co., Ltd. Japan
- * 
+ *
+ *  Copyright (C) 2014-2018 Cores Co., Ltd. Japan
+ *
  *  上記著作権者は，以下の(1)～(4)の条件を満たす場合に限り，本ソフトウェ
  *  ア（本ソフトウェアを改変したものを含む．以下同じ）を使用・複製・改
@@ -26,5 +26,5 @@
  *      由に基づく請求からも，上記著作権者およびTOPPERSプロジェクトを
  *      免責すること．
- * 
+ *
  *  本ソフトウェアは，無保証で提供されているものである．上記著作権者お
  *  よびTOPPERSプロジェクトは，本ソフトウェアに関して，特定の使用目的
@@ -32,5 +32,5 @@
  *  アの利用により直接的または間接的に生じたいかなる損害に関しても，そ
  *  の責任を負わない．
- * 
+ *
  *  @(#) $Id$
  */
@@ -42,4 +42,7 @@
 #include "rx630.h"
 #include "ff.h"
+#include "target_stddef.h"
+#include "target_kernel_impl.h"
+#include "scif.h"
 
  /* PWM出力(490Hz) */
@@ -64,4 +67,7 @@
 void gpio_init(gpio_t *obj, PinName pin)
 {
+	obj->pin = pin;
+	obj->dir = PIN_INPUT;
+	obj->mode = PullDefault;
 }
 
@@ -83,35 +89,37 @@
 	/* 汎用入出力ポートに設定 */
 	switch (pin) {
-	case 0:
+	case D0:
 		/* P21 */
 		sil_wrb_mem(PORT2_PMR_ADDR, sil_reb_mem(PORT2_PMR_ADDR) & ~PORT_PMR_B1_BIT);
 		break;
-	case 1:
+	case D1:
 		/* P20 */
 		sil_wrb_mem(PORT2_PMR_ADDR, sil_reb_mem(PORT2_PMR_ADDR) & ~PORT_PMR_B0_BIT);
 		break;
-	case 2:
+	case D2:
 		/* P22 */
 		sil_wrb_mem(PORT2_PMR_ADDR, sil_reb_mem(PORT2_PMR_ADDR) & ~PORT_PMR_B2_BIT);
 		break;
-	case 3:
+	case D3:
 		/* P23 */
 		sil_wrb_mem(PORT2_PMR_ADDR, sil_reb_mem(PORT2_PMR_ADDR) & ~PORT_PMR_B3_BIT);
 		break;
-	case 4:
+	case D4:
 		/* P24 */
 		sil_wrb_mem(PORT2_PMR_ADDR, sil_reb_mem(PORT2_PMR_ADDR) & ~PORT_PMR_B4_BIT);
 		break;
-	case 5:
+	case D5:
 		/* P25 */
 		sil_wrb_mem(PORT2_PMR_ADDR, sil_reb_mem(PORT2_PMR_ADDR) & ~PORT_PMR_B5_BIT);
 		break;
-	case 6:
+	case D6:
 		/* P32 */
 		sil_wrb_mem(PORT3_PMR_ADDR, sil_reb_mem(PORT3_PMR_ADDR) & ~PORT_PMR_B2_BIT);
 		break;
-	case 7:
+	case D7:
 		/* P33 */
 		sil_wrb_mem(PORT3_PMR_ADDR, sil_reb_mem(PORT3_PMR_ADDR) & ~PORT_PMR_B3_BIT);
+		break;
+	default:
 		break;
 	}
@@ -124,5 +132,5 @@
 	switch (pin) {
 		/* P21/TIOCA3 */
-	case 0:
+	case D0:
 		if (gpio) {
 			/* P21端子機能制御レジスタ P21とする */
@@ -136,5 +144,5 @@
 		break;
 		/* P20/TIOCB3 */
-	case 1:
+	case D1:
 		if (gpio) {
 			/* P20端子機能制御レジスタ P20とする */
@@ -148,5 +156,5 @@
 		break;
 		/* P22/TIOCC3 */
-	case 2:
+	case D2:
 		if (gpio) {
 			/* P22端子機能制御レジスタ P22とする */
@@ -160,5 +168,5 @@
 		break;
 		/* P23/TIOCD3 */
-	case 3:
+	case D3:
 		if (gpio) {
 			/* P23端子機能制御レジスタ P23とする */
@@ -172,5 +180,5 @@
 		break;
 		/* P24/TIOCB4 */
-	case 4:
+	case D4:
 		if (gpio) {
 			/* P24端子機能制御レジスタ P24とする */
@@ -184,5 +192,5 @@
 		break;
 		/* P25/TIOCA4 */
-	case 5:
+	case D5:
 		if (gpio) {
 			/* P25端子機能制御レジスタ P25とする */
@@ -196,5 +204,5 @@
 		break;
 		/* P32/TIOCC0 */
-	case 6:
+	case D6:
 		if (gpio) {
 			/* P32端子機能制御レジスタ P32とする */
@@ -208,5 +216,5 @@
 		break;
 		/* P33/TIOCD0 */
-	case 7:
+	case D7:
 		if (gpio) {
 			/* P33端子機能制御レジスタ P33とする */
@@ -218,4 +226,6 @@
 		}
 		result = E_OK;
+		break;
+	default:
 		break;
 	}
@@ -228,43 +238,45 @@
 		switch (pin) {
 			/* P21/TIOCA3 */
-		case 0:
+		case D0:
 			sil_wrb_mem(PORT2_PDR_ADDR, sil_reb_mem(PORT2_PDR_ADDR) | PORT_PDR_B1_BIT);
 			sil_wrb_mem(PORT2_PMR_ADDR, sil_reb_mem(PORT2_PMR_ADDR) | PORT_PMR_B1_BIT);
 			break;
 			/* P20/TIOCB3 */
-		case 1:
+		case D1:
 			sil_wrb_mem(PORT2_PDR_ADDR, sil_reb_mem(PORT2_PDR_ADDR) | PORT_PDR_B0_BIT);
 			sil_wrb_mem(PORT2_PMR_ADDR, sil_reb_mem(PORT2_PMR_ADDR) | PORT_PMR_B0_BIT);
 			break;
 			/* P22/TIOCC3 */
-		case 2:
+		case D2:
 			sil_wrb_mem(PORT2_PDR_ADDR, sil_reb_mem(PORT2_PDR_ADDR) | PORT_PDR_B2_BIT);
 			sil_wrb_mem(PORT2_PMR_ADDR, sil_reb_mem(PORT2_PMR_ADDR) | PORT_PMR_B2_BIT);
 			break;
 			/* P23/TIOCD3 */
-		case 3:
+		case D3:
 			sil_wrb_mem(PORT2_PDR_ADDR, sil_reb_mem(PORT2_PDR_ADDR) | PORT_PDR_B3_BIT);
 			sil_wrb_mem(PORT2_PMR_ADDR, sil_reb_mem(PORT2_PMR_ADDR) | PORT_PMR_B3_BIT);
 			break;
 			/* P24/TIOCB4 */
-		case 4:
+		case D4:
 			sil_wrb_mem(PORT2_PDR_ADDR, sil_reb_mem(PORT2_PDR_ADDR) | PORT_PDR_B4_BIT);
 			sil_wrb_mem(PORT2_PMR_ADDR, sil_reb_mem(PORT2_PMR_ADDR) | PORT_PMR_B4_BIT);
 			break;
 			/* P25/TIOCA4 */
-		case 5:
+		case D5:
 			sil_wrb_mem(PORT2_PDR_ADDR, sil_reb_mem(PORT2_PDR_ADDR) | PORT_PDR_B5_BIT);
 			sil_wrb_mem(PORT2_PMR_ADDR, sil_reb_mem(PORT2_PMR_ADDR) | PORT_PMR_B5_BIT);
 			break;
 			/* P32/TIOCC0 */
-		case 6:
+		case D6:
 			sil_wrb_mem(PORT3_PDR_ADDR, sil_reb_mem(PORT3_PDR_ADDR) | PORT_PDR_B2_BIT);
 			sil_wrb_mem(PORT3_PMR_ADDR, sil_reb_mem(PORT3_PMR_ADDR) | PORT_PMR_B2_BIT);
 			break;
 			/* P33/TIOCD0 */
-		case 7:
+		case D7:
 			sil_wrb_mem(PORT3_PDR_ADDR, sil_reb_mem(PORT3_PDR_ADDR) | PORT_PDR_B3_BIT);
 			sil_wrb_mem(PORT3_PMR_ADDR, sil_reb_mem(PORT3_PMR_ADDR) | PORT_PMR_B3_BIT);
 			break;
+		default:
+			break;
 		}
 	}
@@ -280,57 +292,57 @@
 
 	switch (obj->pin) {
-	case 0:
+	case D0:
 		/* P21 */
 		pdr = PORT2_PDR_ADDR; bit = PORT_PDR_B1_BIT;
 		break;
-	case 1:
+	case D1:
 		/* P20 */
 		pdr = PORT2_PDR_ADDR; bit = PORT_PDR_B0_BIT;
 		break;
-	case 2:
+	case D2:
 		/* P22 */
 		pdr = PORT2_PDR_ADDR; bit = PORT_PDR_B2_BIT;
 		break;
-	case 3:
+	case D3:
 		/* P23 */
 		pdr = PORT2_PDR_ADDR; bit = PORT_PDR_B3_BIT;
 		break;
-	case 4:
+	case D4:
 		/* P24 */
 		pdr = PORT2_PDR_ADDR; bit = PORT_PDR_B4_BIT;
 		break;
-	case 5:
+	case D5:
 		/* P25 */
 		pdr = PORT2_PDR_ADDR; bit = PORT_PDR_B5_BIT;
 		break;
-	case 6:
+	case D6:
 		/* P32 */
 		pdr = PORT3_PDR_ADDR; bit = PORT_PDR_B2_BIT;
 		break;
-	case 7:
+	case D7:
 		/* P33 */
 		pdr = PORT3_PDR_ADDR; bit = PORT_PDR_B3_BIT;
 		break;
-	case 8:
+	case D8:
 		/* PC2 */
 		pdr = PORTC_PDR_ADDR; bit = PORT_PDR_B2_BIT;
 		break;
-	case 9:
+	case D9:
 		/* PC3 */
 		pdr = PORTC_PDR_ADDR; bit = PORT_PDR_B3_BIT;
 		break;
-	case 10:
+	case D10:
 		/* PC4 */
 		pdr = PORTC_PDR_ADDR; bit = PORT_PDR_B4_BIT;
 		break;
-	case 11:
+	case D11:
 		/* PC6 */
 		pdr = PORTC_PDR_ADDR; bit = PORT_PDR_B6_BIT;
 		break;
-	case 12:
+	case D12:
 		/* PC7 */
 		pdr = PORTC_PDR_ADDR; bit = PORT_PDR_B7_BIT;
 		break;
-	case 13:
+	case D13:
 		/* PC5 */
 		pdr = PORTC_PDR_ADDR; bit = PORT_PDR_B5_BIT;
@@ -349,4 +361,6 @@
 		sil_wrb_mem(pdr, sil_reb_mem(pdr) | bit);
 		break;
+	default:
+		break;
 	}
 
@@ -387,57 +401,57 @@
 
 	switch (obj->pin) {
-	case 0:
+	case D0:
 		/* P21 */
 		podr = PORT2_PODR_ADDR; bit = PORT_PODR_B1_BIT;
 		break;
-	case 1:
+	case D1:
 		/* P20 */
 		podr = PORT2_PODR_ADDR; bit = PORT_PODR_B0_BIT;
 		break;
-	case 2:
+	case D2:
 		/* P22 */
 		podr = PORT2_PODR_ADDR; bit = PORT_PODR_B2_BIT;
 		break;
-	case 3:
+	case D3:
 		/* P23 */
 		podr = PORT2_PODR_ADDR; bit = PORT_PODR_B3_BIT;
 		break;
-	case 4:
+	case D4:
 		/* P24 */
 		podr = PORT2_PODR_ADDR; bit = PORT_PODR_B4_BIT;
 		break;
-	case 5:
+	case D5:
 		/* P25 */
 		podr = PORT2_PODR_ADDR; bit = PORT_PODR_B5_BIT;
 		break;
-	case 6:
+	case D6:
 		/* P32 */
 		podr = PORT3_PODR_ADDR; bit = PORT_PODR_B2_BIT;
 		break;
-	case 7:
+	case D7:
 		/* P33 */
 		podr = PORT3_PODR_ADDR; bit = PORT_PODR_B3_BIT;
 		break;
-	case 8:
+	case D8:
 		/* PC2 */
 		podr = PORTC_PODR_ADDR; bit = PORT_PODR_B2_BIT;
 		break;
-	case 9:
+	case D9:
 		/* PC3 */
 		podr = PORTC_PODR_ADDR; bit = PORT_PODR_B3_BIT;
 		break;
-	case 10:
+	case D10:
 		/* PC4 */
 		podr = PORTC_PODR_ADDR; bit = PORT_PODR_B4_BIT;
 		break;
-	case 11:
+	case D11:
 		/* PC6 */
 		podr = PORTC_PODR_ADDR; bit = PORT_PODR_B6_BIT;
 		break;
-	case 12:
+	case D12:
 		/* PC7 */
 		podr = PORTC_PODR_ADDR; bit = PORT_PODR_B7_BIT;
 		break;
-	case 13:
+	case D13:
 		/* PC5 */
 		podr = PORTC_PODR_ADDR; bit = PORT_PODR_B5_BIT;
@@ -463,57 +477,57 @@
 
 	switch (obj->pin) {
-	case 0:
+	case D0:
 		/* P21 */
 		pidr = PORT2_PIDR_ADDR; bit = PORT_PIDR_B1_BIT;
 		break;
-	case 1:
+	case D1:
 		/* P20 */
 		pidr = PORT2_PIDR_ADDR; bit = PORT_PIDR_B0_BIT;
 		break;
-	case 2:
+	case D2:
 		/* P22 */
 		pidr = PORT2_PIDR_ADDR; bit = PORT_PIDR_B2_BIT;
 		break;
-	case 3:
+	case D3:
 		/* P23 */
 		pidr = PORT2_PIDR_ADDR; bit = PORT_PIDR_B3_BIT;
 		break;
-	case 4:
+	case D4:
 		/* P24 */
 		pidr = PORT2_PIDR_ADDR; bit = PORT_PIDR_B4_BIT;
 		break;
-	case 5:
+	case D5:
 		/* P25 */
 		pidr = PORT2_PIDR_ADDR; bit = PORT_PIDR_B5_BIT;
 		break;
-	case 6:
+	case D6:
 		/* P32 */
 		pidr = PORT3_PIDR_ADDR; bit = PORT_PIDR_B2_BIT;
 		break;
-	case 7:
+	case D7:
 		/* P33 */
 		pidr = PORT3_PIDR_ADDR; bit = PORT_PIDR_B3_BIT;
 		break;
-	case 8:
+	case D8:
 		/* PC2 */
 		pidr = PORTC_PIDR_ADDR; bit = PORT_PIDR_B2_BIT;
 		break;
-	case 9:
+	case D9:
 		/* PC3 */
 		pidr = PORTC_PIDR_ADDR; bit = PORT_PIDR_B3_BIT;
 		break;
-	case 10:
+	case D10:
 		/* PC4 */
 		pidr = PORTC_PIDR_ADDR; bit = PORT_PIDR_B4_BIT;
 		break;
-	case 11:
+	case D11:
 		/* PC6 */
 		pidr = PORTC_PIDR_ADDR; bit = PORT_PIDR_B6_BIT;
 		break;
-	case 12:
+	case D12:
 		/* PC7 */
 		pidr = PORTC_PIDR_ADDR; bit = PORT_PIDR_B7_BIT;
 		break;
-	case 13:
+	case D13:
 		/* PC5 */
 		pidr = PORTC_PIDR_ADDR; bit = PORT_PIDR_B5_BIT;
@@ -581,36 +595,38 @@
 
 	switch (obj->pin) {
-	case 14:
+	case A0:
 		/* *value = sil_reh_mem(S12AD_ADDR0_ADDR); */
 		result = (arduino_ad_avelage[0] / (arduino_ad_table_count));
 		break;
-	case 15:
+	case A1:
 		/* *value = sil_reh_mem(S12AD_ADDR1_ADDR); */
 		result = (arduino_ad_avelage[1] / (arduino_ad_table_count));
 		break;
-	case 16:
+	case A2:
 		/* *value = sil_reh_mem(S12AD_ADDR2_ADDR); */
 		result = (arduino_ad_avelage[2] / (arduino_ad_table_count));
 		break;
-	case 17:
+	case A3:
 		/* *value = sil_reh_mem(S12AD_ADDR3_ADDR); */
 		result = (arduino_ad_avelage[3] / (arduino_ad_table_count));
 		break;
-	case 18:
+	case A4:
 		/* *value = sil_reh_mem(S12AD_ADDR4_ADDR); */
 		result = (arduino_ad_avelage[4] / (arduino_ad_table_count));
 		break;
-	case 19:
+	case A5:
 		/* *value = sil_reh_mem(S12AD_ADDR5_ADDR); */
 		result = (arduino_ad_avelage[5] / (arduino_ad_table_count));
 		break;
-	case 20:
+	case A6:
 		/* *value = sil_reh_mem(S12AD_ADDR6_ADDR); */
 		result = (arduino_ad_avelage[6] / (arduino_ad_table_count));
 		break;
-	case 21:
+	case A7:
 		/* *value = sil_reh_mem(S12AD_ADDR7_ADDR); */
 		result = (arduino_ad_avelage[7] / (arduino_ad_table_count));
 		break;
+	default:
+		return 0;
 	}
 
@@ -677,7 +693,7 @@
 	/* 波形出力レベルの選択 */
 	sil_wrb_mem(TPU0_TIORL_ADDR, (5 << TPU_TIORL_IOC_OFFSET) | (5 << TPU_TIORL_IOD_OFFSET));
-	sil_wrb_mem(TPU3_TIORH_ADDR, (5 << TPU_TIORL_IOA_OFFSET) | (5 << TPU_TIORL_IOB_OFFSET));
+	sil_wrb_mem(TPU3_TIORH_ADDR, (5 << TPU_TIORH_IOA_OFFSET) | (5 << TPU_TIORH_IOB_OFFSET));
 	sil_wrb_mem(TPU3_TIORL_ADDR, (5 << TPU_TIORL_IOC_OFFSET) | (5 << TPU_TIORL_IOD_OFFSET));
-	sil_wrb_mem(TPU4_TIOR_ADDR, (5 << TPU_TIORL_IOA_OFFSET) | (5 << TPU_TIORL_IOB_OFFSET));
+	sil_wrb_mem(TPU4_TIOR_ADDR, (5 << TPU_TIOR_IOA_OFFSET) | (5 << TPU_TIOR_IOB_OFFSET));
 	/* TGRyの設定 */
 	sil_wrh_mem(TPU0_TGRA_ADDR, TPU_BASE_COUNTER);
@@ -714,96 +730,961 @@
 }
 
+typedef enum {
+	sciSerial,
+	sciI2C,
+	sciSPIM,
+	sciSPIS,
+} sci_mode_t;
+
+void sci_enable(void *base_address, sci_mode_t mode)
+{
+	/*
+	 *  モジュールストップ機能の設定
+	 */
+	sil_wrh_mem(SYSTEM_PRCR_ADDR, (uint16_t)0xA502);	/* 書込み許可 */
+	sil_wrw_mem(SYSTEM_MSTPCRB_ADDR,
+		sil_rew_mem(SYSTEM_MSTPCRB_ADDR) & ~SCI_MSTPCRB_BIT(base_address));
+	sil_wrh_mem(SYSTEM_PRCR_ADDR, (uint16_t)0xA500);	/* 書込み禁止 */
+
+	/* 送受信禁止, SCKn端子は入出力ポートとして使用 */
+	sil_wrb_mem(SCI_SCR(base_address), 0x00U);
+
+	switch ((uint32_t)base_address) {
+	/* TXD/SDA/MOSI:P20, RXD/SCL/MISO:P21, SCK:P22 */
+	case SCI0_BASE:
+		switch (mode) {
+		case sciSerial:
+			/* HIGH状態に設定 */
+			sil_wrb_mem(PORT2_PODR_ADDR,
+				sil_reb_mem(PORT2_PODR_ADDR) | PORT_PODR_B0_BIT | PORT_PODR_B1_BIT);
+			/* TX出力/RX入力ポート設定 */
+			sil_wrb_mem(PORT2_PDR_ADDR, (sil_reb_mem(PORT2_PDR_ADDR) | PORT_PDR_B0_BIT) & ~PORT_PDR_B1_BIT);
+			/* プルアップ無効 */
+			sil_wrb_mem(PORT2_PCR_ADDR, sil_reb_mem(PORT2_PCR_ADDR) & ~(PORT_PCR_B0_BIT | PORT_PCR_B1_BIT));
+			/* RX CMOS */
+			sil_wrb_mem(PORT2_ODR0_ADDR,
+				(sil_reb_mem(PORT2_ODR0_ADDR) & ~(PORT_ODR_Pm0_MASK | PORT_ODR_Pm1_MASK))
+				| (PORT_ODR_CMOS << PORT_ODR_Pm0_OFFSET) | (PORT_ODR_CMOS << PORT_ODR_Pm1_OFFSET));
+			/* 周辺機能として使用 */
+			sil_wrb_mem(PORT2_PMR_ADDR, sil_reb_mem(PORT2_PMR_ADDR) | PORT_PMR_B0_BIT | PORT_PMR_B1_BIT);
+			break;
+		case sciI2C:
+			/* HIGH状態に設定 */
+			sil_wrb_mem(PORT2_PODR_ADDR,
+				sil_reb_mem(PORT2_PODR_ADDR) | PORT_PODR_B0_BIT | PORT_PODR_B1_BIT);
+			/* SCL,SDA 出力ポート設定 */
+			sil_wrb_mem(PORT2_PDR_ADDR, sil_reb_mem(PORT2_PDR_ADDR) | PORT_PDR_B0_BIT | PORT_PDR_B1_BIT);
+			/* プルアップ有効 */
+			sil_wrb_mem(PORT2_PCR_ADDR, sil_reb_mem(PORT2_PCR_ADDR) | PORT_PCR_B0_BIT | PORT_PCR_B1_BIT);
+			/* Nチャネルオープンドレイン */
+			sil_wrb_mem(PORT2_ODR0_ADDR,
+				(sil_reb_mem(PORT2_ODR0_ADDR) & ~(PORT_ODR_Pm0_MASK | PORT_ODR_Pm1_MASK))
+				| (PORT_ODR_NCH_OPEN_DRAIN << PORT_ODR_Pm0_OFFSET) | (PORT_ODR_NCH_OPEN_DRAIN << PORT_ODR_Pm1_OFFSET));
+			/* 周辺機能として使用 */
+			sil_wrb_mem(PORT2_PMR_ADDR, sil_reb_mem(PORT2_PMR_ADDR) | PORT_PMR_B0_BIT | PORT_PMR_B1_BIT);
+			break;
+		case sciSPIM:
+			/* HIGH状態に設定 */
+			sil_wrb_mem(PORT2_PODR_ADDR,
+				sil_reb_mem(PORT2_PODR_ADDR) | PORT_PODR_B0_BIT | PORT_PODR_B1_BIT | PORT_PODR_B2_BIT);
+			/* MOSI出力/MISO入力/SCK入出力ポート設定 */
+			sil_wrb_mem(PORT2_PDR_ADDR, (sil_reb_mem(PORT2_PDR_ADDR) | PORT_PDR_B0_BIT | PORT_PDR_B2_BIT) & ~PORT_PDR_B1_BIT);
+			/* プルアップ無効 */
+			sil_wrb_mem(PORT2_PCR_ADDR, sil_reb_mem(PORT2_PCR_ADDR) & ~(PORT_PCR_B0_BIT | PORT_PCR_B1_BIT | PORT_PCR_B2_BIT));
+			/* MISO CMOS */
+			sil_wrb_mem(PORT2_ODR0_ADDR,
+				(sil_reb_mem(PORT2_ODR0_ADDR) & ~(PORT_ODR_Pm0_MASK | PORT_ODR_Pm1_MASK | PORT_ODR_Pm2_MASK))
+				| (PORT_ODR_CMOS << PORT_ODR_Pm0_OFFSET) | (PORT_ODR_CMOS << PORT_ODR_Pm1_OFFSET) | (PORT_ODR_CMOS << PORT_ODR_Pm2_OFFSET));
+			/* 周辺機能として使用 */
+			sil_wrb_mem(PORT2_PMR_ADDR, sil_reb_mem(PORT2_PMR_ADDR) | PORT_PMR_B0_BIT | PORT_PMR_B1_BIT | PORT_PMR_B2_BIT);
+			break;
+		}
+		/* 書き込みプロテクトレジスタの設定 PFSWEビットへの書き込みを許可 */
+		sil_wrb_mem(MPC_PWPR_ADDR, 0x00);
+		/* 書き込みプロテクトレジスタの設定 PxxFSレジスタへの書き込みを許可 */
+		sil_wrb_mem(MPC_PWPR_ADDR, 0x40);
+
+		/* P20端子機能制御レジスタ TXD0/SMOSI0/SSDA0とする */
+		sil_wrb_mem(MPC_P20PFS_ADDR, 0x0A);
+		/* P21端子機能制御レジスタ RXD0/SMISO0/SSCL0とする */
+		sil_wrb_mem(MPC_P21PFS_ADDR, 0x0A);
+		if (mode == sciSPIM) {
+			/* P22端子機能制御レジスタ SCK0とする */
+			sil_wrb_mem(MPC_P22PFS_ADDR, 0x0A);
+		}
+
+		/* 書き込みプロテクトレジスタの設定 書き込みを禁止 */
+		sil_wrb_mem(MPC_PWPR_ADDR, 0x80);
+		break;
+	/* TXD/SDA/MOSI:P50, RXD/SCL/MISO:P52, SCK:P51 */
+	case SCI2_BASE:
+		switch (mode) {
+		case sciSerial:
+			/* HIGH状態に設定 */
+			sil_wrb_mem(PORT5_PODR_ADDR,
+				sil_reb_mem(PORT5_PODR_ADDR) | PORT_PODR_B0_BIT | PORT_PODR_B2_BIT);
+			/* TX出力/RX入力ポート設定 */
+			sil_wrb_mem(PORT5_PDR_ADDR, (sil_reb_mem(PORT5_PDR_ADDR) | PORT_PDR_B0_BIT) & ~PORT_PDR_B2_BIT);
+			/* プルアップ無効 */
+			sil_wrb_mem(PORT5_PCR_ADDR, sil_reb_mem(PORT5_PCR_ADDR) & ~(PORT_PCR_B0_BIT | PORT_PCR_B2_BIT));
+			/* RX CMOS */
+			sil_wrb_mem(PORT5_ODR0_ADDR,
+				(sil_reb_mem(PORT5_ODR0_ADDR) & ~(PORT_ODR_Pm0_MASK | PORT_ODR_Pm2_MASK))
+				| (PORT_ODR_CMOS << PORT_ODR_Pm0_OFFSET) | (PORT_ODR_CMOS << PORT_ODR_Pm2_OFFSET));
+			/* 周辺機能として使用 */
+			sil_wrb_mem(PORT5_PMR_ADDR, sil_reb_mem(PORT5_PMR_ADDR) | PORT_PMR_B0_BIT | PORT_PMR_B2_BIT);
+			break;
+		case sciI2C:
+			/* HIGH状態に設定 */
+			sil_wrb_mem(PORT5_PODR_ADDR,
+				sil_reb_mem(PORT5_PODR_ADDR) | PORT_PODR_B0_BIT | PORT_PODR_B2_BIT);
+			/* SCL,SDA 出力ポート設定 */
+			sil_wrb_mem(PORT5_PDR_ADDR, sil_reb_mem(PORT5_PDR_ADDR) | PORT_PDR_B0_BIT | PORT_PDR_B2_BIT);
+			/* プルアップ有効 */
+			sil_wrb_mem(PORT5_PCR_ADDR, sil_reb_mem(PORT5_PCR_ADDR) | PORT_PCR_B0_BIT | PORT_PCR_B2_BIT);
+			/* Nチャネルオープンドレイン */
+			sil_wrb_mem(PORT5_ODR0_ADDR,
+				(sil_reb_mem(PORT5_ODR0_ADDR) & ~(PORT_ODR_Pm0_MASK | PORT_ODR_Pm2_MASK))
+				| (PORT_ODR_NCH_OPEN_DRAIN << PORT_ODR_Pm0_OFFSET) | (PORT_ODR_NCH_OPEN_DRAIN << PORT_ODR_Pm2_OFFSET));
+			/* 周辺機能として使用 */
+			sil_wrb_mem(PORT5_PMR_ADDR, sil_reb_mem(PORT5_PMR_ADDR) | PORT_PMR_B0_BIT | PORT_PMR_B2_BIT);
+			break;
+		case sciSPIM:
+			/* HIGH状態に設定 */
+			sil_wrb_mem(PORT5_PODR_ADDR,
+				sil_reb_mem(PORT5_PODR_ADDR) | PORT_PODR_B0_BIT | PORT_PODR_B1_BIT | PORT_PODR_B2_BIT);
+			/* MOSI出力/MISO入力/SCK入出力ポート設定 */
+			sil_wrb_mem(PORT5_PDR_ADDR, (sil_reb_mem(PORT5_PDR_ADDR) | PORT_PDR_B0_BIT | PORT_PDR_B1_BIT) & ~PORT_PDR_B2_BIT);
+			/* プルアップ無効 */
+			sil_wrb_mem(PORT5_PCR_ADDR, sil_reb_mem(PORT5_PCR_ADDR) & ~(PORT_PCR_B0_BIT | PORT_PCR_B1_BIT | PORT_PCR_B2_BIT));
+			/* MISO CMOS */
+			sil_wrb_mem(PORT5_ODR0_ADDR,
+				(sil_reb_mem(PORT5_ODR0_ADDR) & ~(PORT_ODR_Pm0_MASK | PORT_ODR_Pm1_MASK | PORT_ODR_Pm2_MASK))
+				| (PORT_ODR_CMOS << PORT_ODR_Pm0_OFFSET) | (PORT_ODR_CMOS << PORT_ODR_Pm1_OFFSET) | (PORT_ODR_CMOS << PORT_ODR_Pm2_OFFSET));
+			/* 周辺機能として使用 */
+			sil_wrb_mem(PORT5_PMR_ADDR, sil_reb_mem(PORT5_PMR_ADDR) | PORT_PMR_B0_BIT | PORT_PMR_B1_BIT | PORT_PMR_B1_BIT | PORT_PMR_B2_BIT);
+			break;
+		}
+		/* 書き込みプロテクトレジスタの設定 PFSWEビットへの書き込みを許可 */
+		sil_wrb_mem(MPC_PWPR_ADDR, 0x00);
+		/* 書き込みプロテクトレジスタの設定 PxxFSレジスタへの書き込みを許可 */
+		sil_wrb_mem(MPC_PWPR_ADDR, 0x40);
+
+		/* P50端子機能制御レジスタ SSDA2とする */
+		sil_wrb_mem(MPC_P50PFS_ADDR, 0x0A);
+		/* P52端子機能制御レジスタ SSCL2とする */
+		sil_wrb_mem(MPC_P52PFS_ADDR, 0x0A);
+		if (mode == sciSPIM) {
+			/* P51端子機能制御レジスタ SCK2とする */
+			sil_wrb_mem(MPC_P51PFS_ADDR, 0x0A);
+		}
+
+		/* 書き込みプロテクトレジスタの設定 書き込みを禁止 */
+		sil_wrb_mem(MPC_PWPR_ADDR, 0x80);
+		break;
+	/* TXD/SDA/MOSI:P32, RXD/SCL/MISO:P33, SCK:P34 */
+	case SCI6_BASE:
+		switch (mode) {
+		case sciSerial:
+			/* HIGH状態に設定 */
+			sil_wrb_mem(PORT3_PODR_ADDR,
+				sil_reb_mem(PORT3_PODR_ADDR) | PORT_PODR_B2_BIT | PORT_PODR_B3_BIT);
+			/* TX出力/RX入力ポート設定 */
+			sil_wrb_mem(PORT3_PDR_ADDR, (sil_reb_mem(PORT3_PDR_ADDR) | PORT_PDR_B2_BIT) & ~PORT_PDR_B3_BIT);
+			/* プルアップ無効 */
+			sil_wrb_mem(PORT3_PCR_ADDR, sil_reb_mem(PORT3_PCR_ADDR) & ~(PORT_PCR_B2_BIT | PORT_PCR_B3_BIT));
+			/* RX CMOS */
+			sil_wrb_mem(PORT3_ODR0_ADDR,
+				(sil_reb_mem(PORT3_ODR0_ADDR) & ~(PORT_ODR_Pm2_MASK | PORT_ODR_Pm3_MASK))
+				| (PORT_ODR_CMOS << PORT_ODR_Pm2_OFFSET) | (PORT_ODR_CMOS << PORT_ODR_Pm3_OFFSET));
+			/* 周辺機能として使用 */
+			sil_wrb_mem(PORT3_PMR_ADDR, sil_reb_mem(PORT3_PMR_ADDR) | PORT_PMR_B2_BIT | PORT_PMR_B3_BIT);
+			break;
+		case sciI2C:
+			/* HIGH状態に設定 */
+			sil_wrb_mem(PORT3_PODR_ADDR,
+				sil_reb_mem(PORT3_PODR_ADDR) | PORT_PODR_B2_BIT | PORT_PODR_B3_BIT);
+			/* SCL,SDA 出力ポート設定 */
+			sil_wrb_mem(PORT3_PDR_ADDR, sil_reb_mem(PORT3_PDR_ADDR) | PORT_PDR_B2_BIT | PORT_PDR_B3_BIT);
+			/* プルアップ有効 */
+			sil_wrb_mem(PORT3_PCR_ADDR, sil_reb_mem(PORT3_PCR_ADDR) | PORT_PCR_B2_BIT | PORT_PCR_B3_BIT);
+			/* Nチャネルオープンドレイン */
+			sil_wrb_mem(PORT3_ODR0_ADDR,
+				(sil_reb_mem(PORT3_ODR0_ADDR) & ~(PORT_ODR_Pm2_MASK | PORT_ODR_Pm3_MASK))
+				| (PORT_ODR_NCH_OPEN_DRAIN << PORT_ODR_Pm2_OFFSET) | (PORT_ODR_NCH_OPEN_DRAIN << PORT_ODR_Pm3_OFFSET));
+			/* 周辺機能として使用 */
+			sil_wrb_mem(PORT3_PMR_ADDR, sil_reb_mem(PORT3_PMR_ADDR) | PORT_PMR_B2_BIT | PORT_PMR_B3_BIT);
+			break;
+		case sciSPIM:
+			/* HIGH状態に設定 */
+			sil_wrb_mem(PORT3_PODR_ADDR,
+				sil_reb_mem(PORT3_PODR_ADDR) | PORT_PODR_B2_BIT | PORT_PODR_B3_BIT | PORT_PODR_B4_BIT);
+			/* MOSI出力/MISO入力/SCK入出力ポート設定 */
+			sil_wrb_mem(PORT3_PDR_ADDR, (sil_reb_mem(PORT3_PDR_ADDR) | PORT_PDR_B2_BIT | PORT_PDR_B4_BIT) & ~PORT_PDR_B3_BIT);
+			/* プルアップ無効 */
+			sil_wrb_mem(PORT3_PCR_ADDR, sil_reb_mem(PORT3_PCR_ADDR) & ~(PORT_PCR_B2_BIT | PORT_PCR_B3_BIT | PORT_PCR_B4_BIT));
+			/* MISO CMOS */
+			sil_wrb_mem(PORT3_ODR0_ADDR,
+				(sil_reb_mem(PORT3_ODR0_ADDR) & ~(PORT_ODR_Pm2_MASK | PORT_ODR_Pm3_MASK | PORT_ODR_Pm4_MASK))
+				| (PORT_ODR_CMOS << PORT_ODR_Pm2_OFFSET) | (PORT_ODR_CMOS << PORT_ODR_Pm3_OFFSET) | (PORT_ODR_CMOS << PORT_ODR_Pm4_OFFSET));
+			/* 周辺機能として使用 */
+			sil_wrb_mem(PORT3_PMR_ADDR, sil_reb_mem(PORT3_PMR_ADDR) | PORT_PMR_B2_BIT | PORT_PMR_B3_BIT | PORT_PMR_B4_BIT);
+			break;
+		}
+
+		/* 書き込みプロテクトレジスタの設定 PFSWEビットへの書き込みを許可 */
+		sil_wrb_mem(MPC_PWPR_ADDR, 0x00);
+		/* 書き込みプロテクトレジスタの設定 PxxFSレジスタへの書き込みを許可 */
+		sil_wrb_mem(MPC_PWPR_ADDR, 0x40);
+
+		/* P32端子機能制御レジスタ TXD6/SMOSI6/SSDA6とする */
+		sil_wrb_mem(MPC_P32PFS_ADDR, 0x0A);
+		/* P33端子機能制御レジスタ RXD6/SMISO6/SSCL6とする */
+		sil_wrb_mem(MPC_P33PFS_ADDR, 0x0A);
+		if (mode == sciSPIM) {
+			/* P34端子機能制御レジスタ SCK6とする */
+			sil_wrb_mem(MPC_P34PFS_ADDR, 0x0A);
+		}
+
+		/* 書き込みプロテクトレジスタの設定 書き込みを禁止 */
+		sil_wrb_mem(MPC_PWPR_ADDR, 0x80);
+		break;
+	/* TXD/SDA/MOSI:PC7, RXD/SCL/MISO:PC6, SCK:PC5 */
+	case SCI8_BASE:
+		switch (mode) {
+		case sciSerial:
+			/* HIGH状態に設定 */
+			sil_wrb_mem(PORTC_PODR_ADDR,
+				sil_reb_mem(PORTC_PODR_ADDR) | PORT_PODR_B6_BIT | PORT_PODR_B7_BIT);
+			/* TX出力/RX入力ポート設定 */
+			sil_wrb_mem(PORTC_PDR_ADDR, (sil_reb_mem(PORTC_PDR_ADDR) | PORT_PDR_B6_BIT) & ~PORT_PDR_B7_BIT);
+			/* プルアップ無効 */
+			sil_wrb_mem(PORTC_PCR_ADDR, sil_reb_mem(PORTC_PCR_ADDR) & ~(PORT_PCR_B6_BIT | PORT_PCR_B7_BIT));
+			/* RX CMOS */
+			sil_wrb_mem(PORTC_ODR0_ADDR,
+				(sil_reb_mem(PORTC_ODR0_ADDR) & ~(PORT_ODR_Pm6_MASK | PORT_ODR_Pm7_MASK))
+				| (PORT_ODR_CMOS << PORT_ODR_Pm6_OFFSET) | (PORT_ODR_CMOS << PORT_ODR_Pm7_OFFSET));
+			/* 周辺機能として使用 */
+			sil_wrb_mem(PORTC_PMR_ADDR, sil_reb_mem(PORTC_PMR_ADDR) | PORT_PMR_B6_BIT | PORT_PMR_B7_BIT);
+			break;
+		case sciI2C:
+			/* HIGH状態に設定 */
+			sil_wrb_mem(PORTC_PODR_ADDR,
+				sil_reb_mem(PORTC_PODR_ADDR) | PORT_PODR_B6_BIT | PORT_PODR_B7_BIT);
+			/* SCL,SDA 出力ポート設定 */
+			sil_wrb_mem(PORTC_PDR_ADDR, sil_reb_mem(PORTC_PDR_ADDR) | PORT_PDR_B6_BIT | PORT_PDR_B7_BIT);
+			/* プルアップ有効 */
+			sil_wrb_mem(PORTC_PCR_ADDR, sil_reb_mem(PORTC_PCR_ADDR) | PORT_PCR_B6_BIT | PORT_PCR_B7_BIT);
+			/* Nチャネルオープンドレイン */
+			sil_wrb_mem(PORTC_ODR0_ADDR,
+				(sil_reb_mem(PORTC_ODR0_ADDR) & ~(PORT_ODR_Pm6_MASK | PORT_ODR_Pm7_MASK))
+				| (PORT_ODR_NCH_OPEN_DRAIN << PORT_ODR_Pm6_OFFSET) | (PORT_ODR_NCH_OPEN_DRAIN << PORT_ODR_Pm7_OFFSET));
+			/* 周辺機能として使用 */
+			sil_wrb_mem(PORTC_PMR_ADDR, sil_reb_mem(PORTC_PMR_ADDR) | PORT_PMR_B6_BIT | PORT_PMR_B7_BIT);
+			break;
+		case sciSPIM:
+			/* HIGH状態に設定 */
+			sil_wrb_mem(PORTC_PODR_ADDR,
+				sil_reb_mem(PORTC_PODR_ADDR) | PORT_PODR_B7_BIT | PORT_PODR_B6_BIT | PORT_PODR_B5_BIT);
+			/* MOSI出力/MISO入力/SCK入出力ポート設定 */
+			sil_wrb_mem(PORTC_PDR_ADDR, (sil_reb_mem(PORTC_PDR_ADDR) | PORT_PDR_B5_BIT | PORT_PDR_B7_BIT) & ~PORT_PDR_B6_BIT);
+			/* プルアップ無効 */
+			sil_wrb_mem(PORTC_PCR_ADDR, sil_reb_mem(PORTC_PCR_ADDR) & ~(PORT_PCR_B5_BIT | PORT_PCR_B7_BIT | PORT_PCR_B6_BIT));
+			/* MISO CMOS */
+			sil_wrb_mem(PORTC_ODR0_ADDR,
+				(sil_reb_mem(PORTC_ODR0_ADDR) & ~(PORT_ODR_Pm5_MASK | PORT_ODR_Pm6_MASK | PORT_ODR_Pm7_MASK))
+				| (PORT_ODR_CMOS << PORT_ODR_Pm5_OFFSET) | (PORT_ODR_CMOS << PORT_ODR_Pm6_OFFSET) | (PORT_ODR_CMOS << PORT_ODR_Pm7_OFFSET));
+			/* 周辺機能として使用 */
+			sil_wrb_mem(PORTC_PMR_ADDR, sil_reb_mem(PORTC_PMR_ADDR) | PORT_PMR_B5_BIT | PORT_PMR_B6_BIT | PORT_PMR_B7_BIT);
+			break;
+		}
+
+		/* 書き込みプロテクトレジスタの設定 PFSWEビットへの書き込みを許可 */
+		sil_wrb_mem(MPC_PWPR_ADDR, 0x00);
+		/* 書き込みプロテクトレジスタの設定 PxxFSレジスタへの書き込みを許可 */
+		sil_wrb_mem(MPC_PWPR_ADDR, 0x40);
+
+		/* PC7端子機能制御レジスタ TXD8/SMOSI8/SSDA8とする */
+		sil_wrb_mem(MPC_PC7PFS_ADDR, 0x0A);
+		/* PC6端子機能制御レジスタ RXD8/SMISO8/SSCL8とする */
+		sil_wrb_mem(MPC_PC6PFS_ADDR, 0x0A);
+		if (mode == sciSPIM) {
+			/* PC5端子機能制御レジスタ SCK8とする */
+			sil_wrb_mem(MPC_PC5PFS_ADDR, 0x0A);
+		}
+
+		/* 書き込みプロテクトレジスタの設定 書き込みを禁止 */
+		sil_wrb_mem(MPC_PWPR_ADDR, 0x80);
+		break;
+	}
+}
+
+void sci_disable(void *base_address)
+{
+	/* 送受信禁止, SCKn端子は入出力ポートとして使用 */
+	sil_wrb_mem(SCI_SCR(base_address), 0x00U);
+
+	/*
+	 *  モジュールストップ機能の設定
+	 */
+	sil_wrh_mem(SYSTEM_PRCR_ADDR, (uint16_t)0xA502);	/* 書込み許可 */
+	sil_wrw_mem(SYSTEM_MSTPCRB_ADDR,
+		sil_rew_mem(SYSTEM_MSTPCRB_ADDR) | SCI_MSTPCRB_BIT(base_address));
+	sil_wrh_mem(SYSTEM_PRCR_ADDR, (uint16_t)0xA500);	/* 書込み禁止 */
+}
+
+void sci_set_frequency(void *base_address, int hz)
+{
+	uint_t	cnt, brr, clk;
+
+	cnt = (FREQ_PCLK * 1000) / (16 * hz);
+	if (cnt < 512)
+		clk = 0;
+	else if (cnt < 4 * 512)
+		clk = 1;
+	else if (cnt < 16 * 512)
+		clk = 2;
+	else if (cnt < 64 * 512)
+		clk = 3;
+	else
+		return;
+
+	brr = cnt / (1 << (2 * clk));
+	brr = (brr / 2) + (brr % 2) - 1;
+
+	/* SMRに送信／ 受信フォーマットを設定 */
+	/* クロック選択ビット(SMR.CKS[1:0]ビットを設定)、それ以外のビットを“0”に設定します。 */
+	sil_wrb_mem(SCI_SMR(base_address), clk);
+
+	/* SCMRレジスタはSDIRビットを“1”、SINVビットを“0”、SMIFビットを“0”に設定します。 */
+	sil_wrb_mem(SCI_SCMR(base_address),
+		(sil_reb_mem(SCI_SCMR(base_address)) & ~(SINV | SMIF)) | SDIR);
+
+	/* ビットレートを設定 */
+	sil_wrb_mem(SCI_BRR(base_address), brr);
+}
+
 void serial_init(serial_t *obj, PinName tx, PinName rx)
 {
+#ifdef TOPPERS_GRCITRUS
+	if ((rx == D0) && (tx == D1)) {
+		obj->base_address = (void *)SCI0_BASE;
+		obj->rxi_intno = INT_SCI0_RXI;
+		obj->tei_intno = INT_SCI0_TEI;
+	}
+	else if ((rx == D5) && (tx == D6)) {
+		obj->base_address = (void *)SCI2_BASE;
+		obj->rxi_intno = INT_SCI2_RXI;
+		obj->tei_intno = INT_SCI2_TEI;
+	}
+	else if ((rx == D7) && (tx == D8)) {
+		obj->base_address = (void *)SCI6_BASE;
+		obj->rxi_intno = INT_SCI6_RXI;
+		obj->tei_intno = INT_SCI6_TEI;
+	}
+	else if ((rx == D11) && (tx == D12)) {
+		obj->base_address = (void *)SCI8_BASE;
+		obj->rxi_intno = INT_SCI8_RXI;
+		obj->tei_intno = INT_SCI8_TEI;
+	}
+	else {
+		obj->base_address = NULL;
+		return;
+	}
+#else
+	if ((rx == D0) && (tx == D1)) {
+		obj->base_address = (void *)SCI0_BASE;
+		obj->rxi_intno = INT_SCI0_RXI;
+		obj->tei_intno = INT_SCI0_TEI;
+	}
+	else if ((rx == D6) && (tx == D7)) {
+		obj->base_address = (void *)SCI6_BASE;
+		obj->rxi_intno = INT_SCI6_RXI;
+		obj->tei_intno = INT_SCI6_TEI;
+	}
+	else if ((rx == D11) && (tx == D12)) {
+		obj->base_address = (void *)SCI8_BASE;
+		obj->rxi_intno = INT_SCI8_RXI;
+		obj->tei_intno = INT_SCI8_TEI;
+}
+	else {
+		obj->base_address = NULL;
+		return;
+	}
+#endif // TOPPERS_GRCITRUS
+	/* SCI有効 */
+	sci_enable(obj->base_address, sciSerial);
+
+	/* SIMR1.IICMビットを“0”に設定 */
+	sil_wrb_mem(SCI_SIMR1(obj->base_address),
+		sil_reb_mem(SCI_SIMR1(obj->base_address)) & ~IICM);
+
+	/* SPMR.CKPH, CKPOLビットを“0”に設定 */
+	sil_wrb_mem(SCI_SPMR(obj->base_address),
+		sil_reb_mem(SCI_SPMR(obj->base_address)) & ~(CKPH | CKPOL));
+
+	sci_set_frequency(obj->base_address, 9600);
+
+	/* SMRに送信／ 受信フォーマットを設定 */
+	serial_format(obj, 8, ParityNone, 1);
+
+	/* 送受信許可 */
+	sil_wrb_mem(SCI_SCR(obj->base_address),
+		(sil_reb_mem(SCI_SCR(obj->base_address)) | RE | TE));
 }
 
 void serial_free(serial_t *obj)
 {
+	/* SCI無効 */
+	sci_disable(obj->base_address);
+
+	obj->base_address = NULL;
 }
 
 void serial_baud(serial_t *obj, int baudrate)
 {
+	uint8_t scr = sil_reb_mem(SCI_SCR(obj->base_address));
+
+	/* 送受信禁止, SCKn端子は入出力ポートとして使用 */
+	sil_wrb_mem(SCI_SCR(obj->base_address), 0x00U);
+
+	sci_set_frequency(obj->base_address, baudrate);
+
+	/* 送受信許可 */
+	sil_wrb_mem(SCI_SCR(obj->base_address), scr);
 }
 
 void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits)
 {
+	uint8_t smr = sil_reb_mem(SCI_SMR(obj->base_address));
+
+	smr &= ~(PE | PM | CHR | STOP);
+
+	switch (parity) {
+	case ParityNone:
+		break;
+	case ParityOdd:
+		smr |= PE | PM;
+		break;
+	case ParityEven:
+		smr |= PE;
+		break;
+	default:
+		break;
+	}
+
+	switch (data_bits) {
+	case 7:
+		smr |= CHR;
+		break;
+	case 8:
+		break;
+	default:
+		break;
+	}
+
+	switch (stop_bits) {
+	case 1:
+		break;
+	case 2:
+		smr |= STOP;
+		break;
+	default:
+		break;
+	}
+
+	sil_wrb_mem(SCI_SMR(obj->base_address), smr);
 }
 
 int serial_getc(serial_t *obj)
 {
+	uint8_t ssr;
+	int c;
+
+	for (;;) {
+		/* SSR.ORER, PER, FERフラグをリード */
+		ssr = sil_reb_mem(SCI_SSR(obj->base_address));
+
+		/* 受信エラーが発生したとき */
+		if ((ssr & (ORER | PER | FER)) != 0) {
+			/* オーバランエラー */
+			if ((ssr & ORER) != 0) {
+				/* RDRレジスタをリード */
+				c = (int)sil_reb_mem(SCI_RDR(obj->base_address));
+			}
+			/* フレーミングエラー */
+			if ((ssr & FER) != 0) {
+			}
+			/* パリティエラー */
+			if ((ssr & PER) != 0) {
+			}
+		}
+
+		/* SSR.ORER, PER, FERフラグを“0”に設定 */
+		sil_wrb_mem(SCI_SSR(obj->base_address), ssr & ~(ORER | PER | FER));
+
+		/* SSR.ORER, PER, FERフラグをリード */
+		ssr = sil_reb_mem(SCI_SSR(obj->base_address));
+
+		/* RXI割り込み */
+		if (serial_readable(obj)) {
+			clear_int(obj->rxi_intno);
+			break;
+		}
+
+		dly_tsk(10);
+	}
+
+	/* RDRレジスタをリード */
+	c = (int)sil_reb_mem(SCI_RDR(obj->base_address));
+
+	return c;
+}
+
+void serial_putc(serial_t *obj, int c)
+{
+	while (serial_writable(obj))
+		dly_tsk(10);
+
+	clear_int(obj->tei_intno);
+
+	sil_wrb_mem(SCI_TDR(obj->base_address), (char)c);
+}
+
+int serial_readable(serial_t *obj)
+{
+	return probe_int(obj->rxi_intno);
+}
+
+int serial_writable(serial_t *obj)
+{
+	return probe_int(obj->tei_intno);
+}
+
+uart_irq_handler sci_irq_handler[7];
+
+void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id)
+{
+	int no = SCI_NUM(obj->base_address);
+
+	sci_irq_handler[no] = handler;
+}
+
+void i2c_init(i2c_t *obj, PinName sda, PinName scl)
+{
+	if ((sda == I2C_SDA) && (scl == I2C_SCL)) {
+#ifdef TOPPERS_GRCITRUS
+		obj->base_address = (void *)SCI6_BASE;
+		obj->rxi_intno = INT_SCI6_RXI;
+		obj->txi_intno = INT_SCI6_TXI;
+		obj->tei_intno = INT_SCI6_TEI;
+#else
+		obj->base_address = (void *)SCI2_BASE;
+		obj->rxi_intno = INT_SCI2_RXI;
+		obj->txi_intno = INT_SCI2_TXI;
+		obj->tei_intno = INT_SCI2_TEI;
+#endif
+	}
+	else {
+		obj->base_address = NULL;
+		return;
+	}
+
+	/* SCI有効 */
+	sci_enable(obj->base_address, sciI2C);
+
+	/* SIMR3.IICSDAS[1:0](b5-b4), IICSCLS[1:0](b7-b6)ビットを“11b”に設定 */
+	sil_wrb_mem(SCI_SIMR3(obj->base_address), 0xF0U);
+
+	sci_set_frequency(obj->base_address, 100000);
+
+	/* SEMR、SNFR、SIMR1、SIMR2、SPMRレジスタに値を設定します。 */
+	/* SEMRレジスタは、NFENビットを設定します。 */
+	sil_wrb_mem(SCI_SEMR(obj->base_address), NFEN);
+
+	/* SNFRレジスタは、NFCS[2:0]ビットを設定します。*/
+	sil_wrb_mem(SCI_SNFR(obj->base_address), 1);
+
+	/* SIMR1レジスタは、IICMビットを“1”に設定し、IICDL[4:0]ビ	ットを設定します。 */
+	sil_wrb_mem(SCI_SIMR1(obj->base_address), IICM | (3 << 3));
+
+	/* SIMR2レジスタは、IICACKT、IICCSCビットを“1”に設定し、IICINTMビットを設定します。 */
+	sil_wrb_mem(SCI_SIMR2(obj->base_address), IICACKT | IICCSC | IICINTM);
+
+	/* SPMRレジスタは、全ビット“0”に設定します。 */
+	sil_wrb_mem(SCI_SPMR(obj->base_address), 0);
+
+	/* 送受信許可 */
+	sil_wrb_mem(SCI_SCR(obj->base_address),
+		(sil_reb_mem(SCI_SCR(obj->base_address)) | RE | TE));
+}
+
+void i2c_frequency(i2c_t *obj, int hz)
+{
+	uint8_t scr = sil_reb_mem(SCI_SCR(obj->base_address));
+
+	/* 送受信禁止, SCKn端子は入出力ポートとして使用 */
+	sil_wrb_mem(SCI_SCR(obj->base_address), 0x00U);
+
+	sci_set_frequency(obj->base_address, hz);
+
+	/* 送受信許可 */
+	sil_wrb_mem(SCI_SCR(obj->base_address), scr);
+}
+
+int i2c_start(i2c_t *obj)
+{
+	if (obj->base_address == NULL)
+		return 0;
+
+	/* SIMR3.IICSTAREQビットを“1”に設定すると同時に、
+	 * SIMR3.IICSCLS[1:0],IICSDAS[1:0]ビットを“01b”に設定 */
+	sil_wrb_mem(SCI_SIMR3(obj->base_address), IICSTAREQ | (1 << 4) | (1 << 6));
+
 	return 0;
 }
 
-void serial_putc(serial_t *obj, int c)
-{
-}
-
-int serial_readable(serial_t *obj)
-{
+int i2c_restart(i2c_t *obj)
+{
+	if (obj->base_address == NULL)
+		return 0;
+
+	sil_wrb_mem(SCI_SIMR3(obj->base_address), IICRSTAREQ | (1 << 4) | (1 << 6));
+
 	return 0;
 }
 
-int serial_writable(serial_t *obj)
-{
+int i2c_stop(i2c_t *obj)
+{
+	if (obj->base_address == NULL)
+		return 0;
+
+	/* SIMR3.IICSTPREQビットを“1”に設定すると同時に、
+	 * SIMR3.IICSCLS[1:0],IICSDAS[1:0]ビットを“01b”に設定 */
+	sil_wrb_mem(SCI_SIMR3(obj->base_address), IICSTPREQ | (1 << 4) | (1 << 6));
+
 	return 0;
 }
 
-void i2c_init(i2c_t *obj, PinName sda, PinName scl)
-{
-}
-
-void i2c_frequency(i2c_t *obj, int hz)
-{
-}
-
-int i2c_start(i2c_t *obj)
-{
+int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
+{
+	if (obj->base_address == NULL)
+		return 0;
+
+	/* 送信割り込み要求を禁止 */
+	sil_wrb_mem(SCI_SCR(obj->base_address),
+		(sil_reb_mem(SCI_SCR(obj->base_address)) & ~(RIE)) | TEIE | TIE);
+
+	/* 開始条件の生成 */
+	i2c_start(obj);
+
+	/* STI(TEI)割り込み発生確認 */
+	while (!probe_int(obj->tei_intno))
+		dly_tsk(2);
+	clear_int(obj->tei_intno);
+
+	/* SIMR3.IICSTIFフラグを“0”に設定し、 */
+	sil_wrb_mem(SCI_SIMR3(obj->base_address),
+		sil_reb_mem(SCI_SIMR3(obj->base_address)) & ~IICSTIF);
+	/* SIMR3.IICSCLS[1:0], IICSDAS[1:0]ビットを“00b”に設定 */
+	sil_wrb_mem(SCI_SIMR3(obj->base_address),
+		sil_reb_mem(SCI_SIMR3(obj->base_address)) & ~((3 << 4) | (3 << 6)));
+
+	/* TDRレジスタにスレーブアドレス、R/Wビットをライト */
+	sil_wrb_mem(SCI_TDR(obj->base_address), address | 1);
+
+	/* TXI割り込み発生確認 */
+	while (!probe_int(obj->txi_intno))
+		dly_tsk(10);
+	clear_int(obj->txi_intno);
+
+	/* スレーブデバイスからのACKを確認 */
+	if ((sil_reb_mem(SCI_SISR(obj->base_address)) & IICACKR) == 0) {
+		/* SIMR2.IICACKTビットを“0”に設定 */
+		sil_wrb_mem(SCI_SIMR2(obj->base_address), sil_reb_mem(SCI_SIMR2(obj->base_address)) & ~IICACKT);
+		/* SCR.RIEビットを“1”に設定 */
+		sil_wrb_mem(SCI_SCR(obj->base_address), sil_reb_mem(SCI_SCR(obj->base_address)) | RIE);
+
+		for (int i = 0; i < length; i++) {
+			/* 次データが最終 */
+			if (i == length - 1) {
+				/* SIMR2.IICACKTビットを“1”に設定 */
+				sil_wrb_mem(SCI_SIMR2(obj->base_address), sil_reb_mem(SCI_SIMR2(obj->base_address)) | IICACKT);
+			}
+
+			/* TDRレジスタにダミーデータ“FFh”をライト */
+			sil_wrb_mem(SCI_TDR(obj->base_address), 0xFF);
+
+			/* RXI割り込み発生確認 */
+			while (!probe_int(obj->rxi_intno))
+				dly_tsk(10);
+			clear_int(obj->rxi_intno);
+
+			/* RDRレジスタの受信データをリード */
+			*data = sil_reb_mem(SCI_RDR(obj->base_address));
+			data++;
+
+			/* TXI割り込み発生確認 */
+			while (!probe_int(obj->txi_intno))
+				dly_tsk(10);
+			clear_int(obj->txi_intno);
+		}
+	}
+
+	if (stop) {
+		/* 停止条件の生成 */
+		i2c_stop(obj);
+
+		/* STI(TEI)割り込み発生確認 */
+		while (!probe_int(obj->tei_intno))
+			dly_tsk(2);
+		clear_int(obj->tei_intno);
+
+		/* SIMR3.IICSTIFフラグを“0”に設定し、*/
+		sil_wrb_mem(SCI_SIMR3(obj->base_address),
+			sil_reb_mem(SCI_SIMR3(obj->base_address)) & ~IICSTIF);
+		/* SIMR3.IICSCLS[1:0], IICSDAS[1:0]ビットを“11b”に設定 */
+		sil_wrb_mem(SCI_SIMR3(obj->base_address),
+			sil_reb_mem(SCI_SIMR3(obj->base_address)) | ((3 << 4) | (3 << 6)));
+	}
+
+	return length;
+}
+
+int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
+{
+	if (obj->base_address == NULL)
+		return 0;
+
+	/* 受信割り込み要求を禁止 */
+	sil_wrb_mem(SCI_SCR(obj->base_address),
+		(sil_reb_mem(SCI_SCR(obj->base_address)) & ~(RIE)) | TEIE | TIE);
+
+	/* 開始条件の生成 */
+	i2c_start(obj);
+
+	/* STI(TEI)割り込み発生確認 */
+	while (!probe_int(obj->tei_intno))
+		dly_tsk(2);
+	clear_int(obj->tei_intno);
+
+	/* SIMR3.IICSTIFフラグを“0”に設定し、 */
+	sil_wrb_mem(SCI_SIMR3(obj->base_address),
+		sil_reb_mem(SCI_SIMR3(obj->base_address)) & ~IICSTIF);
+	/* SIMR3.IICSCLS[1:0], IICSDAS[1:0]ビットを“00b”に設定 */
+	sil_wrb_mem(SCI_SIMR3(obj->base_address),
+		sil_reb_mem(SCI_SIMR3(obj->base_address)) & ~((3 << 4) | (3 << 6)));
+
+	/* TDRレジスタにスレーブアドレス、R/Wビットをライト */
+	sil_wrb_mem(SCI_TDR(obj->base_address), address);
+
+	/* TXI割り込み発生確認 */
+	while (!probe_int(obj->txi_intno))
+		dly_tsk(10);
+	clear_int(obj->txi_intno);
+
+	/* スレーブデバイスからのACKを確認 */
+	if ((sil_reb_mem(SCI_SISR(obj->base_address)) & IICACKR) == 0) {
+
+		for (int i = 0; i < length; i++) {
+			/* TDRレジスタにスレーブアドレス、R/Wビットをライト */
+			sil_wrb_mem(SCI_TDR(obj->base_address), *data);
+			data++;
+
+			/* TXI割り込み発生確認 */
+			while (!probe_int(obj->txi_intno))
+				dly_tsk(10);
+			clear_int(obj->txi_intno);
+		}
+	}
+
+	if (stop) {
+		/* 停止条件の生成 */
+		i2c_stop(obj);
+
+		/* STI(TEI)割り込み発生確認 */
+		while (!probe_int(obj->tei_intno))
+			dly_tsk(2);
+		clear_int(obj->tei_intno);
+
+		/* SIMR3.IICSTIFフラグを“0”に設定し、*/
+		sil_wrb_mem(SCI_SIMR3(obj->base_address),
+			sil_reb_mem(SCI_SIMR3(obj->base_address)) & ~IICSTIF);
+		/* SIMR3.IICSCLS[1:0], IICSDAS[1:0]ビットを“11b”に設定 */
+		sil_wrb_mem(SCI_SIMR3(obj->base_address),
+			sil_reb_mem(SCI_SIMR3(obj->base_address)) | ((3 << 4) | (3 << 6)));
+	}
+
+	return length;
+}
+
+int i2c_byte_read(i2c_t *obj, int last)
+{
+	if (obj->base_address == NULL)
+		return 0;
+
 	return 0;
 }
 
-int i2c_stop(i2c_t *obj)
-{
+int i2c_byte_write(i2c_t *obj, int data)
+{
+	if (obj->base_address == NULL)
+		return 0;
+
 	return 0;
 }
 
-int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
-{
-	return 0;
-}
-
-int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
-{
-	return 0;
-}
-
-int i2c_byte_read(i2c_t *obj, int last)
-{
-	return 0;
-}
-
-int i2c_byte_write(i2c_t *obj, int data)
-{
-	return 0;
-}
-
 void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel)
 {
+	if ((mosi == D11) && (miso == D12) && (sclk == D13) && ((ssel == D10) || (ssel == NC))) {
+		obj->base_address = (void *)SCI8_BASE;
+		obj->txi_intno = INT_SCI8_TXI;
+		obj->rxi_intno = INT_SCI8_RXI;
+		obj->tei_intno = INT_SCI8_TEI;
+	}
+	else {
+		obj->base_address = NULL;
+		return;
+	}
+
+	/* SCI有効 */
+	sci_enable(obj->base_address, (ssel == D10) ? sciSPIS : sciSPIM);
+
+	/* SIMR1.IICMビットを“0”に設定 */
+	sil_wrb_mem(SCI_SIMR1(obj->base_address),
+		sil_reb_mem(SCI_SIMR1(obj->base_address)) & ~IICM);
+
+	if (ssel == NC) {
+		/* SPMR.SSE, CTSE, MSS, MFF, CKPH, CKPOLビットを“0”に設定 */
+		sil_wrb_mem(SCI_SPMR(obj->base_address), 0);
+	}
+	else {
+		/* SPMR, CTSE, MSS, MFF, CKPH, CKPOLビットを“0”に設定 */
+		/* SPMR.SSEビットを“1”に設定 */
+		sil_wrb_mem(SCI_SPMR(obj->base_address), SSE);
+	}
+
+	/* SCMR.SMIF ビットを“0”に設定 */
+	sil_wrb_mem(SCI_SCMR(obj->base_address),
+		sil_reb_mem(SCI_SCMR(obj->base_address)) & ~SMIF);
+
+	/* SMRに送信／ 受信フォーマットをクロック同期式モードで動作で設定 */
+	sil_wrb_mem(SCI_SMR(obj->base_address), CM);
+
+	sci_set_frequency(obj->base_address, 1000000);
+
+	/* 送受信許可 */
+	sil_wrb_mem(SCI_SCR(obj->base_address),
+		(sil_reb_mem(SCI_SCR(obj->base_address)) | RE | TE));
 }
 
 void spi_free(spi_t *obj)
 {
+	/* SCI無効 */
+	sci_disable(obj->base_address);
+
+	obj->base_address = NULL;
 }
 
 void spi_format(spi_t *obj, int bits, int mode, int slave)
 {
+	int polarity = (mode & 0x2) ? 1 : 0;
+	int phase = (mode & 0x1) ? 1 : 0;
+	uint8_t spmr;
+
+	if ((bits != 8) || (mode < 0) || (mode > 3)) {
+		return;
+	}
+
+	/* 送受信禁止, SCKn端子は入出力ポートとして使用 */
+	sil_wrb_mem(SCI_SCR(obj->base_address), 0x00U);
+
+	spmr = sil_reb_mem(SCI_SPMR(obj->base_address));
+
+	/* クロック極性反転あり/なし */
+	if (polarity)
+		spmr |= CKPOL;
+	else
+		spmr &= ~CKPOL;
+
+	/* クロック位相反転あり/なし */
+	if (phase)
+		spmr |= CKPH;
+	else
+		spmr &= ~CKPH;
+
+	if (slave) {
+		/* SCKn端子はクロック入力として使用 */
+		sil_wrb_mem(SCI_SCR(obj->base_address), 
+			(sil_reb_mem(SCI_SCR(obj->base_address)) & ~CKE) | 2);
+
+		/* SPMR.MSSビットを“1”に設定 */
+		sil_wrb_mem(SCI_SPMR(obj->base_address), spmr | MSS);
+	}
+	else {
+		/* SCKn端子はクロック出力として使用 */
+		sil_wrb_mem(SCI_SCR(obj->base_address),
+			sil_reb_mem(SCI_SCR(obj->base_address)) & ~CKE);
+
+		/* SPMR.MSSビットを“0”に設定 */
+		sil_wrb_mem(SCI_SPMR(obj->base_address), spmr & ~MSS);
+	}
+
+	/* 送受信許可 */
+	sil_wrb_mem(SCI_SCR(obj->base_address),
+		(sil_reb_mem(SCI_SCR(obj->base_address)) | RE | TE));
 }
 
 void spi_frequency(spi_t *obj, int hz)
 {
+	uint8_t scr = sil_reb_mem(SCI_SCR(obj->base_address));
+
+	/* 送受信禁止, SCKn端子は入出力ポートとして使用 */
+	sil_wrb_mem(SCI_SCR(obj->base_address), 0x00U);
+
+	sci_set_frequency(obj->base_address, hz);
+
+	/* 送受信許可 */
+	sil_wrb_mem(SCI_SCR(obj->base_address), scr);
 }
 
 int spi_master_write(spi_t *obj, int value)
 {
-	return 0;
+	/* TXI割り込み発生確認 */
+	while (!probe_int(obj->txi_intno))
+		;
+
+	/* 送信データをライト */
+	sil_wrb_mem(SCI_TDR(obj->base_address), (char)value);
+
+	do {
+		uint8_t ssr = sil_reb_mem(SCI_SSR(obj->base_address));
+		if ((ssr & ORER) != 0) {
+			ssr &= ~ORER;
+			sil_wrb_mem(SCI_SSR(obj->base_address), ssr);
+			return -1;
+		}
+	}
+	/* RXI割り込み発生確認 */
+	while (!probe_int(obj->rxi_intno));
+
+	/* 受信データをリード */
+	return sil_reb_mem(SCI_RDR(obj->base_address));
+}
+
+int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length,
+	char *rx_buffer, int rx_length, char write_fill)
+{
+	int total = (tx_length > rx_length) ? tx_length : rx_length;
+
+	for (int i = 0; i < total; i++) {
+		char out = (i < tx_length) ? tx_buffer[i] : write_fill;
+		char in = spi_master_write(obj, out);
+		if (i < rx_length) {
+			rx_buffer[i] = in;
+		}
+	}
+
+	return total;
 }
 
@@ -858,2 +1739,129 @@
 	sil_wrb_mem((uint8_t *)RTC_RSECCNT_ADDR, DecToBcd(tm.tm_sec));
 }
+
+void sci_rxi_handler(uint32_t id)
+{
+	uart_irq_handler hdr = sci_irq_handler[id];
+	if (hdr == NULL)
+		return;
+
+	hdr(id, RxIrq);
+}
+
+void sci_txi_handler(uint32_t id)
+{
+	uart_irq_handler hdr = sci_irq_handler[id];
+	if (hdr == NULL)
+		return;
+
+	hdr(id, TxIrq);
+}
+
+void sci_tei_handler(int no)
+{
+}
+
+void sci0_rxi_handler(void)
+{
+	sci_rxi_handler(0);
+}
+
+void sci0_txi_handler(void)
+{
+	sci_txi_handler(0);
+}
+
+void sci0_tei_handler(void)
+{
+	sci_tei_handler(0);
+}
+
+void sci1_rxi_handler(void)
+{
+	sci_rxi_handler(1);
+}
+
+void sci1_txi_handler(void)
+{
+	sci_txi_handler(1);
+}
+
+void sci1_tei_handler(void)
+{
+	sci_tei_handler(1);
+}
+
+void sci2_rxi_handler(void)
+{
+	sci_rxi_handler(2);
+}
+
+void sci2_txi_handler(void)
+{
+	sci_txi_handler(2);
+}
+
+void sci2_tei_handler(void)
+{
+	sci_tei_handler(2);
+}
+
+void sci3_rxi_handler(void)
+{
+	sci_rxi_handler(3);
+}
+
+void sci3_txi_handler(void)
+{
+	sci_txi_handler(3);
+}
+
+void sci3_tei_handler(void)
+{
+	sci_tei_handler(3);
+}
+
+void sci4_rxi_handler(void)
+{
+	sci_rxi_handler(4);
+}
+
+void sci4_txi_handler(void)
+{
+	sci_txi_handler(4);
+}
+
+void sci4_tei_handler(void)
+{
+	sci_tei_handler(4);
+}
+
+void sci5_rxi_handler(void)
+{
+	sci_rxi_handler(5);
+}
+
+void sci5_txi_handler(void)
+{
+	sci_txi_handler(5);
+}
+
+void sci5_tei_handler(void)
+{
+	sci_tei_handler(5);
+}
+
+void sci6_rxi_handler(void)
+{
+	sci_rxi_handler(6);
+}
+
+void sci6_txi_handler(void)
+{
+	sci_txi_handler(6);
+}
+
+void sci6_tei_handler(void)
+{
+	sci_tei_handler(6);
+}
Index: asp3_tinet_ecnl_rx/trunk/ntshell/src/mbed_api.cfg
===================================================================
--- asp3_tinet_ecnl_rx/trunk/ntshell/src/mbed_api.cfg	(revision 364)
+++ asp3_tinet_ecnl_rx/trunk/ntshell/src/mbed_api.cfg	(revision 364)
@@ -0,0 +1,120 @@
+/*
+ *  TOPPERS ECHONET Lite Communication Middleware
+ * 
+ *  Copyright (C) 2018 Cores Co., Ltd. Japan
+ * 
+ *  上記著作権者は，以下の(1)～(4)の条件を満たす場合に限り，本ソフトウェ
+ *  ア（本ソフトウェアを改変したものを含む．以下同じ）を使用・複製・改
+ *  変・再配布（以下，利用と呼ぶ）することを無償で許諾する．
+ *  (1) 本ソフトウェアをソースコードの形で利用する場合には，上記の著作
+ *      権表示，この利用条件および下記の無保証規定が，そのままの形でソー
+ *      スコード中に含まれていること．
+ *  (2) 本ソフトウェアを，ライブラリ形式など，他のソフトウェア開発に使
+ *      用できる形で再配布する場合には，再配布に伴うドキュメント（利用
+ *      者マニュアルなど）に，上記の著作権表示，この利用条件および下記
+ *      の無保証規定を掲載すること．
+ *  (3) 本ソフトウェアを，機器に組み込むなど，他のソフトウェア開発に使
+ *      用できない形で再配布する場合には，次のいずれかの条件を満たすこ
+ *      と．
+ *    (a) 再配布に伴うドキュメント（利用者マニュアルなど）に，上記の著
+ *        作権表示，この利用条件および下記の無保証規定を掲載すること．
+ *    (b) 再配布の形態を，別に定める方法によって，TOPPERSプロジェクトに
+ *        報告すること．
+ *  (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
+ *      害からも，上記著作権者およびTOPPERSプロジェクトを免責すること．
+ *      また，本ソフトウェアのユーザまたはエンドユーザからのいかなる理
+ *      由に基づく請求からも，上記著作権者およびTOPPERSプロジェクトを
+ *      免責すること．
+ * 
+ *  本ソフトウェアは，無保証で提供されているものである．上記著作権者お
+ *  よびTOPPERSプロジェクトは，本ソフトウェアに関して，特定の使用目的
+ *  に対する適合性も含めて，いかなる保証も行わない．また，本ソフトウェ
+ *  アの利用により直接的または間接的に生じたいかなる損害に関しても，そ
+ *  の責任を負わない．
+ * 
+ *  @(#) $Id$
+ */
+
+/*
+ *  mbed_apiのシステムコンフィギュレーションファイル
+ */
+#include "shellif.h"
+#include "mbed_api.h"
+
+/* CSI0 */
+
+//DEF_INH(INT_SCI0_RXI, { TA_HLNG, sci0_rxi_handler });
+//CFG_INT(INT_SCI0_RXI, { TA_NULL, -1 });
+
+//DEF_INH(INT_SCI0_TXI, { TA_HLNG, sci0_txi_handler });
+//CFG_INT(INT_SCI0_TXI, { TA_NULL, -1 });
+
+//DEF_INH(INT_SCI0_TEI, { TA_HLNG, sci0_tei_handler });
+//CFG_INT(INT_SCI0_TEI, { TA_NULL, -1 });
+
+/* CSI1 */
+
+DEF_INH(INT_SCI1_RXI, { TA_HLNG, sci1_rxi_handler });
+CFG_INT(INT_SCI1_RXI, { TA_NULL, -1 });
+
+DEF_INH(INT_SCI1_TXI, { TA_HLNG, sci1_txi_handler });
+CFG_INT(INT_SCI1_TXI, { TA_NULL, -1 });
+
+DEF_INH(INT_SCI1_TEI, { TA_HLNG, sci1_tei_handler });
+CFG_INT(INT_SCI1_TEI, { TA_NULL, -1 });
+
+/* CSI2 */
+
+DEF_INH(INT_SCI2_RXI, { TA_HLNG, sci2_rxi_handler });
+CFG_INT(INT_SCI2_RXI, { TA_NULL, -1 });
+
+DEF_INH(INT_SCI2_TXI, { TA_HLNG, sci2_txi_handler });
+CFG_INT(INT_SCI2_TXI, { TA_NULL, -1 });
+
+DEF_INH(INT_SCI2_TEI, { TA_HLNG, sci2_tei_handler });
+CFG_INT(INT_SCI2_TEI, { TA_NULL, -1 });
+
+/* CSI3 */
+
+DEF_INH(INT_SCI3_RXI, { TA_HLNG, sci3_rxi_handler });
+CFG_INT(INT_SCI3_RXI, { TA_NULL, -1 });
+
+DEF_INH(INT_SCI3_TXI, { TA_HLNG, sci3_txi_handler });
+CFG_INT(INT_SCI3_TXI, { TA_NULL, -1 });
+
+DEF_INH(INT_SCI3_TEI, { TA_HLNG, sci3_tei_handler });
+CFG_INT(INT_SCI3_TEI, { TA_NULL, -1 });
+
+/* CSI4 */
+
+DEF_INH(INT_SCI4_RXI, { TA_HLNG, sci4_rxi_handler });
+CFG_INT(INT_SCI4_RXI, { TA_NULL, -1 });
+
+DEF_INH(INT_SCI4_TXI, { TA_HLNG, sci4_txi_handler });
+CFG_INT(INT_SCI4_TXI, { TA_NULL, -1 });
+
+DEF_INH(INT_SCI4_TEI, { TA_HLNG, sci4_tei_handler });
+CFG_INT(INT_SCI4_TEI, { TA_NULL, -1 });
+
+/* CSI5 */
+
+DEF_INH(INT_SCI5_RXI, { TA_HLNG, sci5_rxi_handler });
+CFG_INT(INT_SCI5_RXI, { TA_NULL, -1 });
+
+DEF_INH(INT_SCI5_TXI, { TA_HLNG, sci5_txi_handler });
+CFG_INT(INT_SCI5_TXI, { TA_NULL, -1 });
+
+DEF_INH(INT_SCI5_TEI, { TA_HLNG, sci5_tei_handler });
+CFG_INT(INT_SCI5_TEI, { TA_NULL, -1 });
+
+/* CSI6 */
+
+DEF_INH(INT_SCI6_RXI, { TA_HLNG, sci6_rxi_handler });
+CFG_INT(INT_SCI6_RXI, { TA_NULL, -1 });
+
+DEF_INH(INT_SCI6_TXI, { TA_HLNG, sci6_txi_handler });
+CFG_INT(INT_SCI6_TXI, { TA_NULL, -1 });
+
+DEF_INH(INT_SCI6_TEI, { TA_HLNG, sci6_tei_handler });
+CFG_INT(INT_SCI6_TEI, { TA_NULL, -1 });
+
Index: asp3_tinet_ecnl_rx/trunk/ntshell/src/mbed_api.h
===================================================================
--- asp3_tinet_ecnl_rx/trunk/ntshell/src/mbed_api.h	(revision 359)
+++ asp3_tinet_ecnl_rx/trunk/ntshell/src/mbed_api.h	(revision 364)
@@ -2,5 +2,5 @@
  *  TOPPERS ECHONET Lite Communication Middleware
  * 
- *  Copyright (C) 2014-2017 Cores Co., Ltd. Japan
+ *  Copyright (C) 2014-2018 Cores Co., Ltd. Japan
  * 
  *  上記著作権者は，以下の(1)～(4)の条件を満たす場合に限り，本ソフトウェ
@@ -38,4 +38,7 @@
 #define _MBED_API_H_
 
+#define DEVICE_SPI
+#define DEVICE_I2C
+
 void mbed_api_init(void);
 
@@ -44,9 +47,37 @@
 
 typedef enum {
+	D0, /* P21 */
+	D1, /* P20 */
+	D2, /* P22 */
+	D3, /* P23 */
+	D4, /* P24 */
+	D5, /* P25 */
+	D6, /* P32 */
+	D7, /* P33 */
+	D8, /* PC2 */
+	D9, /* PC3 */
+	D10, /* PC4 */
+	D11, /* PC6 */
+	D12, /* PC7 */
+	D13, /* PC5 */
+
+	A0, /* AN0 */
+	A1, /* AN1 */
+	A2, /* AN2 */
+	A3, /* AN3 */
+	A4, /* AN4 */
+	A5, /* AN5 */
+	A6, /* AN6 */
+	A7, /* AN7 */
+
+	I2C_SCL, /* P52 */
+	I2C_SDA, /* P50 */
+
 	PIN_LED0,
 	PIN_LED1,
 	PIN_LED2,
 	PIN_LED3,
-	PIN_BUTTON0,
+	PIN_SW,
+
 	NC = (int)0xFFFFFFFF
 } PinName;
@@ -56,5 +87,5 @@
 #define LED_BLUE		PIN_LED2
 #define LED_USER		PIN_LED3
-#define USER_BUTTON0	PIN_BUTTON0
+#define USER_BUTTON0	PIN_SW
 
 typedef enum {
@@ -64,9 +95,9 @@
 
 typedef enum {
-	PullUp = 0,
+	PullUp = 2,
 	PullDown = 3,
-	PullNone = 2,
+	PullNone = 0,
 	OpenDrain = 4,
-	PullDefault = PullDown
+	PullDefault = PullNone
 } PinMode;
 
@@ -114,25 +145,30 @@
 
 typedef enum {
-    ParityNone = 0,
-    ParityOdd = 1,
-    ParityEven = 2,
-    ParityForced1 = 3,
-    ParityForced0 = 4
+	ParityNone = 0,
+	ParityOdd = 1,
+	ParityEven = 2,
+	ParityForced1 = 3,
+	ParityForced0 = 4
 } SerialParity;
 
 typedef enum {
-    FlowControlNone,
-    FlowControlRTS,
-    FlowControlCTS,
-    FlowControlRTSCTS
+	RxIrq,
+	TxIrq
+} SerialIrq;
+
+typedef enum {
+	FlowControlNone,
+	FlowControlRTS,
+	FlowControlCTS,
+	FlowControlRTSCTS
 } FlowControl;
 
 typedef struct serial_s {
-	int dummy;
+	void *base_address;
+	int rxi_intno;
+	int tei_intno;
 } serial_t;
 
-typedef struct spi_s {
-	int dummy;
-} spi_t;
+typedef void (*uart_irq_handler)(uint32_t id, SerialIrq event);
 
 void serial_init(serial_t *obj, PinName tx, PinName rx);
@@ -144,6 +180,11 @@
 int serial_readable(serial_t *obj);
 int serial_writable(serial_t *obj);
+void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id);
 
 typedef struct i2c_s {
+	void *base_address;
+	int rxi_intno;
+	int txi_intno;
+	int tei_intno;
 	unsigned char rxdata[16];
 } i2c_t;
@@ -158,4 +199,13 @@
 int i2c_byte_write(i2c_t *obj, int data);
 
+#define SPI_FILL_CHAR         (0xFF)
+
+typedef struct spi_s {
+	void *base_address;
+	int rxi_intno;
+	int txi_intno;
+	int tei_intno;
+} spi_t;
+
 void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel);
 void spi_free(spi_t *obj);
@@ -163,4 +213,6 @@
 void spi_frequency(spi_t *obj, int hz);
 int spi_master_write(spi_t *obj, int value);
+int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length,
+	char *rx_buffer, int rx_length, char write_fill);
 
 void rtc_init(void);
@@ -170,3 +222,31 @@
 void rtc_write(time_t t);
 
+void sci0_rxi_handler(void);
+void sci0_txi_handler(void);
+void sci0_tei_handler(void);
+
+void sci1_rxi_handler(void);
+void sci1_txi_handler(void);
+void sci1_tei_handler(void);
+
+void sci2_rxi_handler(void);
+void sci2_txi_handler(void);
+void sci2_tei_handler(void);
+
+void sci3_rxi_handler(void);
+void sci3_txi_handler(void);
+void sci3_tei_handler(void);
+
+void sci4_rxi_handler(void);
+void sci4_txi_handler(void);
+void sci4_tei_handler(void);
+
+void sci5_rxi_handler(void);
+void sci5_txi_handler(void);
+void sci5_tei_handler(void);
+
+void sci6_rxi_handler(void);
+void sci6_txi_handler(void);
+void sci6_tei_handler(void);
+
 #endif /* _MBED_API_H_ */
Index: asp3_tinet_ecnl_rx/trunk/ntshell/src/net_misc.h
===================================================================
--- asp3_tinet_ecnl_rx/trunk/ntshell/src/net_misc.h	(revision 359)
+++ asp3_tinet_ecnl_rx/trunk/ntshell/src/net_misc.h	(revision 364)
@@ -41,5 +41,5 @@
 /* スタックサイズ */
 
-#define NET_MISC_STACK_SIZE	1024		/* NTP クライアントタスク（IPv4）のスタックサイズ	*/
+#define NET_MISC_STACK_SIZE	2048		/* NTP クライアントタスク（IPv4）のスタックサイズ	*/
 
 /* 優先度 */
Index: asp3_tinet_ecnl_rx/trunk/ntshell/src/netcmd.c
===================================================================
--- asp3_tinet_ecnl_rx/trunk/ntshell/src/netcmd.c	(revision 359)
+++ asp3_tinet_ecnl_rx/trunk/ntshell/src/netcmd.c	(revision 364)
@@ -239,6 +239,6 @@
 int usrcmd_ping(int argc, char **argv)
 {
-	int	tmo, size;
-	char	apip = DEFAULT_API_PROTO;
+	int tmo = 3, size = 64;
+	char apip = DEFAULT_API_PROTO;
 	char *line = argv[1];
 #if defined(SUPPORT_INET6)
@@ -262,4 +262,7 @@
 
 #if defined(SUPPORT_INET6) && defined(SUPPORT_INET4)
+	if (argc <= 2)
+		return 0;
+
 	if ('0' <= *line && *line <= '9') {
 		if (*line == '6')
@@ -269,4 +272,9 @@
 		line++;
 	}
+
+	line = argv[2];
+#else
+	if (argc <= 1)
+		return 0;
 #endif	/* of #if defined(SUPPORT_INET6) && defined(SUPPORT_INET4) */
 
@@ -276,15 +284,15 @@
 	}
 
-	line = argv[2];
-	if ('0' <= *line && *line <= '9')
-		line = get_int(&tmo, line);
-	else
-		tmo = 3;
-
-	line = argv[3];
-	if ('0' <= *line && *line <= '9')
-		line = get_int(&size, line);
-	else
-		size = 64;
+	if (argc > 2) {
+		line = argv[2];
+		if ('0' <= *line && *line <= '9')
+			line = get_int(&tmo, line);
+
+		if (argc > 3) {
+			line = argv[3];
+			if ('0' <= *line && *line <= '9')
+				line = get_int(&size, line);
+		}
+	}
 
 #if defined(SUPPORT_INET6)
@@ -337,4 +345,8 @@
 {
 	ER ret;
+
+	if (argc < 2)
+		return 0;
+
 	if (ntlibc_strcmp(argv[1], "rel") == 0) {
 		ret = dhcp4c_rel_info();
@@ -348,4 +360,5 @@
 		dhcp4c_info();
 	}
+
 	return 0;
 }
Index: asp3_tinet_ecnl_rx/trunk/ntshell/src/ntp_cli.c
===================================================================
--- asp3_tinet_ecnl_rx/trunk/ntshell/src/ntp_cli.c	(revision 359)
+++ asp3_tinet_ecnl_rx/trunk/ntshell/src/ntp_cli.c	(revision 364)
@@ -265,5 +265,5 @@
 
 	nc->buf[0] = '\0';
-#ifndef _MSC_VER
+#ifdef __NEED_struct_timespec
 	if (ctime_r(&tp.tv_sec, nc->buf) != NULL)
 #else
Index: asp3_tinet_ecnl_rx/trunk/ntshell/src/ntshell_main.c
===================================================================
--- asp3_tinet_ecnl_rx/trunk/ntshell/src/ntshell_main.c	(revision 359)
+++ asp3_tinet_ecnl_rx/trunk/ntshell/src/ntshell_main.c	(revision 364)
@@ -60,4 +60,5 @@
 #include "socket_stub.h"
 #include "usb_hbth.h"
+#include "mbed_api.h"
 
 extern int bt_bnep_mode;
@@ -69,9 +70,9 @@
 const struct utsname host_name = {
 	"TOPPERS/ASP3",
-    TARGET_NAME,
-    "3.2.0",
-    "3.2.0",
-    TARGET_NAME,
-    "toppers.jp"
+	TARGET_NAME,
+	"3.2.0",
+	"3.2.0",
+	TARGET_NAME,
+	"toppers.jp"
 };
 
@@ -88,7 +89,29 @@
 jmp_buf process_exit;
 void sys_init(void);
-
-void ntshell_task_init()
-{
+NTSHELL_SERIAL_READ ntshell_serial_read = 0;
+NTSHELL_SERIAL_WRITE ntshell_serial_write = 0;
+void *ntshell_serial_extobj;
+
+unsigned char ntstdio_xi(struct ntstdio_t *handle)
+{
+	unsigned char buf[1];
+	ntshell_serial_read((char *)buf, 1, ntshell_serial_extobj);
+	return buf[0];
+}
+
+void ntstdio_xo(struct ntstdio_t *handle, unsigned char c)
+{
+	char buf[1];
+	buf[0] = c;
+	ntshell_serial_write(buf, 1, ntshell_serial_extobj);
+}
+
+void ntshell_task_init(NTSHELL_SERIAL_READ func_read,
+	NTSHELL_SERIAL_WRITE func_write, void *extobj)
+{
+	ntshell_serial_read = func_read;
+	ntshell_serial_write = func_write;
+	ntshell_serial_extobj = extobj;
+
 	sys_init();
 
@@ -126,4 +149,7 @@
 	int result = 0;
 	int found = 0;
+
+	if (*args == 0)
+		return result;
 
 	if (strcmp((const char *)args[1], "help") == 0) {
@@ -295,5 +321,5 @@
 int shell_clock_getres(clockid_t clk_id, struct timespec *res)
 {
-	if (clk_id != CLOCK_REALTIME)
+	if ((clk_id != CLOCK_REALTIME) && (clk_id != CLOCK_MONOTONIC))
 		return -EINVAL;
 
@@ -308,5 +334,5 @@
 	SYSTIM now = 0;
 
-	if (clk_id != CLOCK_REALTIME)
+	if ((clk_id != CLOCK_REALTIME) && (clk_id != CLOCK_MONOTONIC))
 		return -EINVAL;
 
@@ -320,8 +346,16 @@
 int shell_clock_settime(clockid_t clk_id, const struct timespec *tp)
 {
-	if (clk_id != CLOCK_REALTIME)
+	if ((clk_id != CLOCK_REALTIME) && (clk_id != CLOCK_MONOTONIC))
 		return -EINVAL;
 
-	rtc_write(tp->tv_sec);
+	SYSTIM time;
+	ER ret;
+
+	time = (tp->tv_sec * 1000000ll) + (tp->tv_nsec / 1000ll);
+
+	ret = set_tim(time);
+	if (ret != E_OK) {
+		return -EPERM;
+	}
 
 	return 0;
@@ -356,5 +390,5 @@
 }
 
-struct sigaction sigtable[6];
+struct sigaction sigtable[7];
 
 int shell_sigaction(int sig, const struct sigaction *restrict sa, struct sigaction *restrict old)
@@ -381,4 +415,7 @@
 		sat = &sigtable[5];
 		break;
+	case SIGPIPE:
+		sat = &sigtable[6];
+		break;
 	default:
 		return -EINVAL;
Index: asp3_tinet_ecnl_rx/trunk/ntshell/src/ntshell_main.cfg
===================================================================
--- asp3_tinet_ecnl_rx/trunk/ntshell/src/ntshell_main.cfg	(revision 359)
+++ asp3_tinet_ecnl_rx/trunk/ntshell/src/ntshell_main.cfg	(revision 364)
@@ -43,3 +43,4 @@
 CRE_TSK(NTSHELL_TASK, { TA_NULL, 0, ntshell_task, NTSHELL_PRIORITY, NTSHELL_STACK_SIZE, NULL });
 CRE_FLG(FLG_SELECT_WAIT, { TA_WMUL, 0x00 });
+CRE_SEM(SEM_FILEDESC, { TA_TPRI, 1, 1 });
 CRE_SEM(SEM_MALLOC, { TA_TPRI, 1, 1 });
Index: asp3_tinet_ecnl_rx/trunk/ntshell/src/ntshell_main.h
===================================================================
--- asp3_tinet_ecnl_rx/trunk/ntshell/src/ntshell_main.h	(revision 359)
+++ asp3_tinet_ecnl_rx/trunk/ntshell/src/ntshell_main.h	(revision 364)
@@ -49,4 +49,5 @@
 
 #include "usrcmd.h"
+#include "core/ntshell.h"
 
 /*
@@ -54,5 +55,5 @@
  */
 
-#define NTSHELL_PRIORITY	4		/* ntshellタスクの優先度 */
+#define NTSHELL_PRIORITY	8		/* ntshellタスクの優先度 */
 
 #define NTSHELL_STACK_SIZE	8192	/* ntshellタスクのスタック領域のサイズ */
@@ -75,10 +76,7 @@
 extern PRI main_task_priority;
 
-struct ntstdio_t;
-unsigned char ntstdio_xi(struct ntstdio_t *handle);
-void ntstdio_xo(struct ntstdio_t *handle, unsigned char c);
-
 /* ntshellタスク初期化 */
-void ntshell_task_init();
+void ntshell_task_init(NTSHELL_SERIAL_READ func_read,
+	NTSHELL_SERIAL_WRITE func_write, void *extobj);
 
 /* ntshellタスク */
Index: asp3_tinet_ecnl_rx/trunk/ntshell/src/shellif.h
===================================================================
--- asp3_tinet_ecnl_rx/trunk/ntshell/src/shellif.h	(revision 359)
+++ asp3_tinet_ecnl_rx/trunk/ntshell/src/shellif.h	(revision 364)
@@ -39,11 +39,4 @@
 
 #ifdef _MSC_VER
-#define _CRT_NO_TIME_T
-#define tm msc_tm
-#include <time.h>
-#include <errno.h>
-#include <limits.h>
-#include <stdlib.h>
-#undef tm
 #undef NULL
 #endif
@@ -133,4 +126,8 @@
 #endif
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 // time.h
 #define CLOCK_REALTIME 0
@@ -179,10 +176,9 @@
 int shell_access(const char *path, int mode);
 // signal.h
-int shell_sigprocmask(int how, const sigset_t *restrict set, sigset_t *restrict old);
-int shell_sigaction(int sig, const struct sigaction *restrict sa, struct sigaction *restrict old);
+int shell_sigprocmask(int how, const sigset_t *__restrict set, sigset_t *__restrict old);
+int shell_sigaction(int sig, const struct sigaction *__restrict sa, struct sigaction *__restrict old);
 // socket.h
 int shell_socket(int, int, int);
 int shell_connect(int, const struct sockaddr *, socklen_t);
-ssize_t shell_send(int, const void *, size_t, int);
 ssize_t shell_sendto(int, const void *, size_t, int, const struct sockaddr *, socklen_t);
 ssize_t shell_sendmsg(int, const struct msghdr *, int);
@@ -190,5 +186,4 @@
 int shell_listen(int, int);
 int shell_accept(int, struct sockaddr *__restrict, socklen_t *__restrict);
-ssize_t shell_recv(int, void *, size_t, int);
 ssize_t shell_recvfrom(int, void *__restrict, size_t, int, struct sockaddr *__restrict, socklen_t *__restrict);
 ssize_t shell_recvmsg(int, struct msghdr *, int);
@@ -263,6 +258,6 @@
 int shell_pipe2(int [2], int);
 int shell_ppoll(struct pollfd *, nfds_t, const struct timespec *,const sigset_t *);
-int shell_pselect6(int, fd_set *restrict, fd_set *restrict,
-	fd_set *restrict, const struct timespec *restrict, const sigset_t *restrict);
+int shell_pselect6(int, fd_set *__restrict, fd_set *__restrict,
+	fd_set *__restrict, const struct timespec *__restrict, const sigset_t *__restrict);
 int shell_setpgid(pid_t, pid_t);
 int shell_setsid();
@@ -308,5 +303,4 @@
 #endif
 
-struct tm *gmtime_r(const time_t *t, struct tm *tm);
 void *malloc(size_t size);
 void *calloc(size_t size, size_t count);
@@ -314,3 +308,7 @@
 void free(void *ptr);
 
+#ifdef __cplusplus
+}
+#endif
+
 #endif /* SHELLIF_H */
Index: asp3_tinet_ecnl_rx/trunk/ntshell/src/socket_stub.c
===================================================================
--- asp3_tinet_ecnl_rx/trunk/ntshell/src/socket_stub.c	(revision 359)
+++ asp3_tinet_ecnl_rx/trunk/ntshell/src/socket_stub.c	(revision 364)
@@ -42,5 +42,8 @@
 #include <sil.h>
 #include "syssvc/syslog.h"
+#include <tinet_defs.h>
 #include <tinet_config.h>
+#include <net/net.h>
+#include <net/net_endian.h>
 #include <netinet/in.h>
 #include <netinet/in_itron.h>
@@ -53,4 +56,5 @@
 #include <net/if_var.h>
 #include <netinet/udp_var.h>
+#include <net/net_buf.h>
 //#include <netinet/tcp_var.h>
 #include <netapp/resolver.h>
@@ -58,4 +62,5 @@
 #include "ff.h"
 #include "socket_stub.h"
+#include "kernel_cfg.h"
 
 #define SOCKET_TIMEOUT 2000000
@@ -181,4 +186,7 @@
 			fp->psock->buf_size = 512 + 512;
 			fp->psock->buf = malloc(fp->psock->buf_size);
+#ifdef _DEBUG
+			memset(fp->psock->buf, 0, fp->psock->buf_size);
+#endif
 			T_TCP_CCEP ccep = { 0, fp->psock->buf, 512, &fp->psock->buf[512], 512, (FP)socket_tcp_callback };
 			ret = tcp_cre_cep(cepid, &ccep);
@@ -279,4 +287,7 @@
 			fp->psock->buf_size = 512 + 512;
 			fp->psock->buf = malloc(fp->psock->buf_size);
+#ifdef _DEBUG
+			memset(fp->psock->buf, 0, fp->psock->buf_size);
+#endif
 			T_TCP_CCEP ccep = { 0, fp->psock->buf, 512, &fp->psock->buf[512], 512, (FP)socket_tcp_callback };
 			ret = tcp_cre_cep(cepid, &ccep);
@@ -298,5 +309,4 @@
 			return -EHOSTUNREACH;
 		}
-		fp->writable = 1;
 		break;
 	}
@@ -322,5 +332,5 @@
 	}
 
-	memcpy(&fp->psock, &lfp->psock, sizeof(fp->psock));
+	memcpy(fp->psock, lfp->psock, offsetof(socket_t, buf_size));
 
 	ER ret;
@@ -335,4 +345,7 @@
 			fp->psock->buf_size = 512 + 512;
 			fp->psock->buf = malloc(fp->psock->buf_size);
+#ifdef _DEBUG
+			memset(fp->psock->buf, 0, fp->psock->buf_size);
+#endif
 			T_TCP_CCEP ccep = { 0, fp->psock->buf, 512, &fp->psock->buf[512], 512, (FP)socket_tcp_callback };
 			ret = tcp_cre_cep(cepid, &ccep);
@@ -346,4 +359,5 @@
 		else {
 			cepid = lfp->psock->cepid;
+			fp->handle = cepid;
 			lfp->handle = tmax_tcp_cepid + lfp->psock->repid;
 			lfp->psock->cepid = 0;
@@ -356,5 +370,4 @@
 			return -ENOMEM;
 		}
-		fp->writable = 1;
 		struct sockaddr_in *raddr = &fp->psock->raddr4;
 		memset(raddr, 0, sizeof(*raddr));
@@ -384,5 +397,5 @@
 }
 
-ssize_t shell_send(int fd, const void *buf, size_t len, int flags)
+ssize_t shell_sendto(int fd, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t alen)
 {
 	SOCKET *fp = fd_to_fp(fd);
@@ -396,5 +409,8 @@
 		switch (fp->psock->type) {
 		case SOCK_STREAM: {
-			fp->writable = 0;
+			if ((addr != NULL) || (alen != 0)) {
+				return -EISCONN;
+			}
+
 			if (flags & MSG_OOB) {
 				ret = tcp_snd_oob(fp->psock->cepid, (void *)buf, len, SOCKET_TIMEOUT);
@@ -404,53 +420,16 @@
 			}
 			else {
-				ret = tcp_snd_dat(fp->psock->cepid, (void *)buf, len, SOCKET_TIMEOUT);
-				if (ret < 0) {
-					return -ECOMM;
-				}
-			}
-			break;
-		}
-		case SOCK_DGRAM: {
-			return -EINVAL;
-		}
-		}
-		break;
-	}
-	case AF_INET6: {
-		return -EAFNOSUPPORT;
-	}
-	}
-
-	return ret;
-}
-
-ssize_t shell_sendto(int fd, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t alen)
-{
-	SOCKET *fp = fd_to_fp(fd);
-	if (fp == NULL) {
-		return -EBADF;
-	}
-
-	int ret = 0;
-	switch (fp->psock->family) {
-	case AF_INET: {
-		switch (fp->psock->type) {
-		case SOCK_STREAM: {
-			if ((addr != NULL) && (alen != 0)) {
-				return -EINVAL;
-			}
-
-			fp->writable = 0;
-			if (flags & MSG_OOB) {
-				ret = tcp_snd_oob(fp->psock->cepid, (void *)buf, len, SOCKET_TIMEOUT);
-				if (ret < 0) {
-					return -ECOMM;
-				}
-			}
-			else {
-				ret = tcp_snd_dat(fp->psock->cepid, (void *)buf, len, SOCKET_TIMEOUT);
-				if (ret < 0) {
-					return -ECOMM;
-				}
+				for (;;) {
+					ret = tcp_snd_dat(fp->psock->cepid, (void *)buf, len, SOCKET_TIMEOUT);
+					if (ret < 0) {
+						if (ret == E_TMOUT)
+							return -ETIME;
+						return -ECOMM;
+					}
+					len -= ret;
+					if (len <= 0)
+						break;
+					buf = (const void *)&((uint8_t *)buf)[ret];
+				} 
 			}
 			break;
@@ -458,5 +437,5 @@
 		case SOCK_DGRAM: {
 			int sz = alen;
-			if (sz < 8) {
+			if ((addr == NULL) || (sz < 8)) {
 				return -EINVAL;
 			}
@@ -465,9 +444,8 @@
 			memcpy(raddr, addr, sz);
 			T_IPV4EP rep = { ntohl(raddr->sin_addr.s_addr), ntohs(raddr->sin_port) };
-			fp->writable = 0;
 			ret = udp_snd_dat(fp->psock->cepid, &rep, (void *)buf, len,
-				(fp->psock->flags & O_NONBLOCK) ? 0 : SOCKET_TIMEOUT);
+				(fp->psock->flags & O_NONBLOCK) ? TMO_POL : SOCKET_TIMEOUT);
 			if (ret < 0) {
-				return -ECOMM;
+				return (ret == E_TMOUT) ? -ETIME : -ECOMM;
 			}
 			break;
@@ -490,5 +468,5 @@
 }
 
-ssize_t shell_recv(int fd, void *buf, size_t len, int flags)
+ssize_t shell_recvfrom(int fd, void *__restrict buf, size_t len, int flags, struct sockaddr *__restrict addr, socklen_t *__restrict alen)
 {
 	SOCKET *fp = fd_to_fp(fd);
@@ -505,74 +483,127 @@
 				ret = tcp_rcv_oob(fp->psock->cepid, buf, len);
 				if (ret < 0) {
+					syslog(LOG_ERROR, "tcp_rcv_oob => %d", ret);
 					return -ECOMM;
 				}
 			}
 			else {
-				ret = tcp_rcv_dat(fp->psock->cepid, buf, len, TMO_FEVR);
+				int rsz, tmp;
+				if (fp->psock->input == NULL) {
+					ret = wai_sem(SEM_FILEDESC);
+					if (ret < 0) {
+						syslog(LOG_ERROR, "wai_sem => %d", ret);
+					}
+					fp->psock->len = 0;
+					ret = sig_sem(SEM_FILEDESC);
+					if (ret < 0) {
+						syslog(LOG_ERROR, "sig_sem => %d", ret);
+					}
+					ret = tcp_rcv_buf(fp->psock->cepid, &fp->psock->input, TMO_FEVR);
+					if (ret < 0) {
+						syslog(LOG_ERROR, "tcp_rcv_buf => %d", ret);
+						return -ECOMM;
+					}
+					rsz = ret;
+				}
+				else
+					rsz = fp->psock->len;
+				tmp = rsz;
+				if (rsz > len)
+					rsz = len;
+				if (rsz >= 0) {
+					memcpy(buf, fp->psock->input, rsz);
+					ret = wai_sem(SEM_FILEDESC);
+					if (ret < 0) {
+						syslog(LOG_ERROR, "wai_sem => %d", ret);
+					}
+					fp->psock->len = tmp - rsz;
+					ret = sig_sem(SEM_FILEDESC);
+					if (ret < 0) {
+						syslog(LOG_ERROR, "sig_sem => %d", ret);
+					}
+					if (tmp - rsz == 0) {
+						fp->psock->input = NULL;
+					}
+					else
+						fp->psock->input = (void *)&((uint8_t *)fp->psock->input)[rsz];
+					ret = tcp_rel_buf(fp->psock->cepid, rsz);
+					if ((ret != E_OBJ) && (ret < 0)) {
+						syslog(LOG_ERROR, "tcp_rel_buf => %d", ret);
+						//return -ECOMM;
+					}
+				}
+				ret = rsz;
+			}
+			break;
+		}
+		case SOCK_DGRAM: {
+			struct sockaddr_in *raddr = &fp->psock->raddr4;
+			int rsz;
+			ret = wai_sem(SEM_FILEDESC);
+			if (ret < 0) {
+				syslog(LOG_ERROR, "wai_sem => %d", ret);
+			}
+			T_NET_BUF *input = fp->psock->input;
+			if (input == NULL) {
+				ret = sig_sem(SEM_FILEDESC);
 				if (ret < 0) {
-					return -ECOMM;
-				}
-			}
-			break;
-		}
-		case SOCK_DGRAM: {
-			T_IPV4EP rep = { 0, 0 };
-			ret = udp_rcv_dat(fp->psock->cepid, &rep, buf, len, (fp->psock->flags & O_NONBLOCK) ? 0 : TMO_FEVR);
-			if (ret < 0) {
-				return -ECOMM;
-			}
-		}
-		}
-		break;
-	}
-	case AF_INET6: {
-		return -EAFNOSUPPORT;
-	}
-	}
-
-	return ret;
-}
-
-ssize_t shell_recvfrom(int fd, void *buf, size_t len, int flags, struct sockaddr *__restrict addr, socklen_t *__restrict alen)
-{
-	SOCKET *fp = fd_to_fp(fd);
-	if (fp == NULL) {
-		return -EBADF;
-	}
-
-	int ret = 0;
-	switch (fp->psock->family) {
-	case AF_INET: {
-		switch (fp->psock->type) {
-		case SOCK_STREAM: {
-			if (flags & MSG_OOB) {
-				ret = tcp_rcv_oob(fp->psock->cepid, buf, len);
+					syslog(LOG_ERROR, "sig_sem => %d", ret);
+				}
+
+				T_IPV4EP rep = { 0, 0 };
+				ret = udp_rcv_dat(fp->psock->cepid, &rep, buf, len,
+					(fp->psock->flags & O_NONBLOCK) ? TMO_POL : SOCKET_TIMEOUT);
 				if (ret < 0) {
-					return -ECOMM;
+					syslog(LOG_ERROR, "udp_rcv_buf => %d", ret);
+					return (ret == E_TMOUT) ? -ETIME : -ECOMM;
+				}
+				rsz = ret;
+				if ((addr != NULL) && (alen != NULL)) {
+					ret = wai_sem(SEM_FILEDESC);
+					if (ret < 0) {
+						syslog(LOG_ERROR, "wai_sem => %d", ret);
+					}
+					int sz = *alen;
+					memset(raddr, 0, sizeof(fp->psock->raddr4));
+					raddr->sin_family = AF_INET;
+					raddr->sin_port = htons(rep.portno);
+					raddr->sin_addr.s_addr = htonl(rep.ipaddr);
+					if (sz > sizeof(fp->psock->raddr4))
+						sz = sizeof(fp->psock->raddr4);
+					memcpy(addr, raddr, sz);
+					*alen = sz;
+					ret = sig_sem(SEM_FILEDESC);
+					if (ret < 0) {
+						syslog(LOG_ERROR, "sig_sem => %d", ret);
+					}
 				}
 			}
 			else {
-				ret = tcp_rcv_dat(fp->psock->cepid, buf, len, TMO_FEVR);
+				rsz = fp->psock->len;
+				void *pbuf = fp->psock->buf;
+				fp->psock->input = NULL;
+				fp->psock->len = 0;
+				fp->psock->buf = NULL;
+				if ((addr != NULL) && (alen != NULL)) {
+					int sz = *alen;
+					if (sz > sizeof(fp->psock->raddr4))
+						sz = sizeof(fp->psock->raddr4);
+					memcpy(addr, raddr, sz);
+					*alen = sz;
+				}
+				ret = sig_sem(SEM_FILEDESC);
 				if (ret < 0) {
-					return -ECOMM;
-				}
-			}
-			break;
-		}
-		case SOCK_DGRAM: {
-			T_IPV4EP rep = { 0, 0 };
-			ret = udp_rcv_dat(fp->psock->cepid, &rep, buf, len, TMO_FEVR);
-			if (ret < 0) {
-				return -ECOMM;
-			}
-			int sz = *alen;
-			struct sockaddr_in raddr;
-			memset(&raddr, 0, sizeof(raddr));
-			raddr.sin_family = AF_INET;
-			raddr.sin_port = htons(rep.portno);
-			raddr.sin_addr.s_addr = htonl(rep.ipaddr);
-			if (sz > sizeof(raddr))
-				sz = sizeof(raddr);
-			memcpy(addr, &raddr, sz);
+					syslog(LOG_ERROR, "sig_sem => %d", ret);
+				}
+				if (rsz > len)
+					rsz = len;
+				memcpy(buf, pbuf, rsz);
+				ret = rel_net_buf(input);
+				if (ret < 0) {
+					syslog(LOG_ERROR, "rel_net_buf => %d", ret);
+					//return -ECOMM;
+				}
+			}
+			ret = rsz;
 		}
 		}
@@ -830,8 +861,5 @@
 int tcp_fd_close(struct _IO_FILE *fp)
 {
-	ER ret;
-
-	free(fp->psock->buf);
-	fp->psock->buf = NULL;
+	ER ret, ret2;
 
 	switch (fp->psock->family) {
@@ -844,12 +872,11 @@
 			}
 			ret = tcp_cls_cep(cepid, (fp->psock->repid != 0) ? 0 : SOCKET_TIMEOUT);
-			if (ret < 0) {
-				//return -1;
-			}
-			ret = tcp_del_cep(cepid);
+			ret2 = tcp_del_cep(cepid);
+			free(fp->psock->buf);
+			fp->psock->buf = NULL;
 			delete_tcp_fd(cepid);
 			delete_id(tcp_cepid_table, tcp_cepid_table_count, cepid);
-			if (ret < 0) {
-				return -EINVAL;
+			if ((ret < 0) || (ret2 < 0)) {
+				return (ret == E_TMOUT) ? -ETIME : -EINVAL;
 			}
 		}
@@ -857,4 +884,6 @@
 			ID repid = fp->psock->repid;
 			ret = tcp_del_rep(repid);
+			free(fp->psock->buf);
+			fp->psock->buf = NULL;
 			delete_tcp_fd(tmax_tcp_cepid + repid);
 			delete_id(tcp_repid_table, tcp_repid_table_count, repid);
@@ -864,4 +893,6 @@
 		}
 		else {
+			free(fp->psock->buf);
+			fp->psock->buf = NULL;
 			return -EINVAL;
 		}
@@ -878,10 +909,10 @@
 size_t tcp_fd_read(struct _IO_FILE *fp, unsigned char *dst, size_t dstsz)
 {
-	return shell_recv(fp->fd, dst, dstsz, 0);
+	return shell_recvfrom(fp->fd, dst, dstsz, 0, NULL, NULL);
 }
 
 size_t tcp_fd_write(struct _IO_FILE *fp, const unsigned char *src, size_t srcsz)
 {
-	return shell_send(fp->fd, src, srcsz, 0);
+	return shell_sendto(fp->fd, src, srcsz, 0, NULL, 0);
 }
 
@@ -901,11 +932,10 @@
 	ID cepid;
 
-	free(fp->psock->buf);
-	fp->psock->buf = NULL;
-
 	switch (fp->psock->family) {
 	case AF_INET: {
 		cepid = fp->psock->cepid;
 		ret = udp_del_cep(cepid);
+		//free(fp->psock->buf);
+		//fp->psock->buf = NULL;
 		delete_udp_fd(cepid);
 		delete_id(udp_cepid_table, udp_cepid_table_count, cepid);
@@ -925,10 +955,10 @@
 size_t udp_fd_read(struct _IO_FILE *fp, unsigned char *dst, size_t dstsz)
 {
-	return shell_recv(fp->fd, dst, dstsz, 0);
+	return shell_recvfrom(fp->fd, dst, dstsz, 0, NULL, NULL);
 }
 
 size_t udp_fd_write(struct _IO_FILE *fp, const unsigned char *src, size_t srcsz)
 {
-	return shell_send(fp->fd, src, srcsz, 0);
+	return shell_sendto(fp->fd, src, srcsz, 0, NULL, 0);
 }
 
Index: asp3_tinet_ecnl_rx/trunk/ntshell/src/socket_stub.h
===================================================================
--- asp3_tinet_ecnl_rx/trunk/ntshell/src/socket_stub.h	(revision 359)
+++ asp3_tinet_ecnl_rx/trunk/ntshell/src/socket_stub.h	(revision 364)
@@ -99,4 +99,6 @@
 	int buf_size;
 	unsigned char *buf;
+	void *input;
+	int len;
 } socket_t;
 
Index: asp3_tinet_ecnl_rx/trunk/ntshell/src/syscall.c
===================================================================
--- asp3_tinet_ecnl_rx/trunk/ntshell/src/syscall.c	(revision 359)
+++ asp3_tinet_ecnl_rx/trunk/ntshell/src/syscall.c	(revision 364)
@@ -172,4 +172,5 @@
 
 long SYS_futex() {
+	int futex(int *uaddr, int op, int val, const struct timespec *timeout, int *uaddr2, int val3);
 	return no_implement("futex\n");
 }
@@ -280,12 +281,9 @@
 
 long SYS_recv(long a, long b, long c, long d) {
-	return shell_recv((int)a, (void *)b, (size_t)c, (int)d);
+	return shell_recvfrom((int)a, (void *)b, (size_t)c, (int)d, NULL, NULL);
 }
 
 long SYS_recvfrom(long a, long b, long c, long d, long e, long f) {
-	if (a == 0)
-		return shell_recv(a, (void *)b, (size_t)c, (int)d);
-	else
-		return shell_recvfrom(a, (void *)b, (size_t)c, (int)d, (struct sockaddr *)e, (socklen_t *)f);
+	return shell_recvfrom(a, (void *)b, (size_t)c, (int)d, (struct sockaddr *)e, (socklen_t *)f);
 }
 
@@ -315,5 +313,5 @@
 
 long SYS_send(long a, long b, long c, long d) {
-	return shell_send((int)a, (const void *)b, (size_t)c, (int)d);
+	return shell_sendto((int)a, (const void *)b, (size_t)c, (int)d, NULL, 0);
 }
 
@@ -323,8 +321,5 @@
 
 long SYS_sendto(long a, long b, long c, long d, long e, long f) {
-	if (a == 0)
-		return shell_send(a, (const void *)b, (size_t)c, (int)d);
-	else
-		return shell_sendto(a, (const void *)b, (size_t)c, (int)d, (const struct sockaddr *)e, (socklen_t)f);
+	return shell_sendto(a, (const void *)b, (size_t)c, (int)d, (const struct sockaddr *)e, (socklen_t)f);
 }
 
@@ -392,2 +387,57 @@
 	return shell_writev((int)a, (const struct iovec *)b, (size_t)c);
 }
+
+long SYS_dup()
+{
+	return no_implement("dup\n");
+}
+
+long SYS_dup2()
+{
+	return no_implement("dup2\n");
+}
+
+long SYS_pipe()
+{
+	return no_implement("pipe\n");
+}
+
+long SYS_readlink()
+{
+	return no_implement("readlink\n");
+}
+
+long SYS_symlink()
+{
+	return no_implement("symlink\n");
+}
+
+long SYS_umask()
+{
+	return no_implement("umask\n");
+}
+
+long SYS_execve()
+{
+	return no_implement("execve\n");
+}
+
+long SYS_fork()
+{
+	return no_implement("fork\n");
+}
+
+long SYS_wait4()
+{
+	return no_implement("wait4\n");
+}
+
+long SYS_socketpair()
+{
+	return no_implement("socketpair\n");
+}
+
+long SYS_flock()
+{
+	return no_implement("flock\n");
+}
