Index: BNEP/user/main.c
===================================================================
--- /YC3121_demo/ModuleDemo/BlueTooth/BNEP/user/main.c	(revision 730)
+++ /YC3121_demo/ModuleDemo/BlueTooth/BNEP/user/main.c	(working copy)
@@ -28,12 +28,9 @@
 #include "yc_bt.h"
 #include "yc_systick.h"
 #include "yc_qspi.h"
-#include "yc_exti.h"
 #include "yc_ipc.h"
 #include "board_config.h"
 
-
-
 /* Private typedef -----------------------------------------------------------*/
 /* Private define ------------------------------------------------------------*/
 #define IPC_UART UART0
@@ -68,7 +65,7 @@
 #endif
 	
 #define IFLASH_NVRAM_ADDR 0x1000000+((512-4)*1024)
-#define NVRAM_LEN 0xaa//每组Nvram长度为34bytes,最多可存储5组配对信息
+#define NVRAM_LEN 170//Nvram 长度170bytes,每组Nvram长度为34bytes,最多可存储5组配对信息
 //#define SPP_FLOWCONTROL
 uint16_t ble_send_handle=0x2a;
 uint8_t bt_cod[3] = {0x24,0x04,0x04};
@@ -226,7 +223,7 @@
 	MyPrintf("bt version=%x\n",BT_GetVersion());
 	
 	qspi_flash_read(IFLASH_NVRAM_ADDR,NvramData,NVRAM_LEN);
-	if(NvramData[0] == 0xff)
+	if(NvramData[34*0] > 0x05||NvramData[34*1] > 0x05||NvramData[34*2] > 0x05||NvramData[34*3] > 0x05||NvramData[34*4] > 0x05)
 	{
 		memset(NvramData,0x00,NVRAM_LEN);
 	}
@@ -461,7 +458,8 @@
 uint8_t bt_master_name[MAX_BT_MASTER_NAME_LEN];
 void BT_Progress()
 {	
-	switch(BT_GetEventOpcode())
+	int eventCmd= BT_GetEventOpcode();
+	switch(eventCmd)
 	{	
 		case DNS_RSP:
 		{
@@ -720,7 +718,7 @@
 					if(BT_SetSppFlowcontrol(1) != TRUE)	MyPrintf("set Flowcontrol failed\n");
 					#endif
 				}
-				else	MyPrintf("read bt data error");
+				else	MyPrintf("read bt data error\r\n");
 			}
 		break;
 			
@@ -743,7 +741,7 @@
 					for(int i=0;i<BleDataLen;i++)	MyPrintf("%02x ",bleData[2+i]);
 					MyPrintf("\n");
 				}
-				else	MyPrintf("read ble data error");
+				else	MyPrintf("read ble data error\r\n");
 			}
 		break;
 
@@ -829,7 +827,7 @@
 					ReserveNV(NvramData);
 				}
 				else	
-					MyPrintf("read Nvram data error");
+					MyPrintf("read Nvram data error\r\n");
 			}
 		break;
 
@@ -847,7 +845,7 @@
 					}
 					MyPrintf("\n");
 				}
-				else	MyPrintf("read CONFIRM_GKEY data error");
+				else	MyPrintf("read CONFIRM_GKEY data error\r\n");
 			
 				if(BT_ConfirmGkey(0) == TRUE)//BT_ConfirmGkey参数为0则继续配对，1则取消配对。
 					MyPrintf("set confirmkey  success");
@@ -874,7 +872,7 @@
 					}
 					MyPrintf("\n");
 				}
-				else	MyPrintf("read Nvram data error");
+				else	MyPrintf("read PAIRING_COMPLETED data error\r\n");
 			}
 		break;
 			
@@ -884,7 +882,7 @@
 				int PairingDataLen=0;
 				uint16_t pairing_status=0;
 				PairingDataLen=BT_ReadBTData(PairingData);
-				if(PairingDataLen>0)
+				if(PairingDataLen==2)
 				{
 					pairing_status |=PairingData[0];
 					pairing_status |=(PairingData[1]<<8);
@@ -911,7 +909,7 @@
 						break;
 					}
 				}
-				else	MyPrintf("read PAIRING_STATE error");
+				else	MyPrintf("read PAIRING_STATE data error\r\n");
 			}
 		break;
 			
@@ -955,7 +953,26 @@
 			}
 		break;
 			
+			case -1:
+			//no event
+		break;
+			
 		default:
+		{
+			MyPrintf("unknown event:%02x\r\n",eventCmd);
+			uint8_t unknownData[MAX_BLUETOOTH_DATA_LEN];
+			int unknownDataLen=0;
+			unknownDataLen=BT_ReadBTData(unknownData);
+			if(unknownDataLen>0)
+			{
+				MyPrintf("datalen:%d\r\n",unknownDataLen);
+				for(int i=0;i<unknownDataLen;i++)
+				{
+					MyPrintf("%02X ",unknownData[i]);
+				}
+				MyPrintf("\n");
+			}
+		}
 		break;
 	}
 }
@@ -967,9 +984,5 @@
 	return qspi_flash_write(IFLASH_NVRAM_ADDR,nvram,NVRAM_LEN);
 }
 
-
-
-
-
 /************************ (C) COPYRIGHT Yichip Microelectronics *****END OF FILE****/
 
Index: BT&BLE/prj/MDK/YC3121_Keil.uvoptx
===================================================================
--- /YC3121_demo/ModuleDemo/BlueTooth/BT&BLE/prj/MDK/YC3121_Keil.uvoptx	(revision 730)
+++ /YC3121_demo/ModuleDemo/BlueTooth/BT&BLE/prj/MDK/YC3121_Keil.uvoptx	(working copy)
@@ -224,26 +224,26 @@
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
-  </Group>
-
-  <Group>
-    <GroupName>main</GroupName>
-    <tvExp>1</tvExp>
-    <tvExpOptDlg>0</tvExpOptDlg>
-    <cbSel>0</cbSel>
-    <RteFlg>0</RteFlg>
     <File>
-      <GroupNumber>2</GroupNumber>
+      <GroupNumber>1</GroupNumber>
       <FileNumber>3</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\..\user\main.c</PathWithFileName>
-      <FilenameWithoutPath>main.c</FilenameWithoutPath>
+      <PathWithFileName>..\..\..\..\..\Librarier\core\bt_code.c</PathWithFileName>
+      <FilenameWithoutPath>bt_code.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
+  </Group>
+
+  <Group>
+    <GroupName>main</GroupName>
+    <tvExp>1</tvExp>
+    <tvExpOptDlg>0</tvExpOptDlg>
+    <cbSel>0</cbSel>
+    <RteFlg>0</RteFlg>
     <File>
       <GroupNumber>2</GroupNumber>
       <FileNumber>4</FileNumber>
@@ -251,8 +251,8 @@
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\..\user\bt_code.c</PathWithFileName>
-      <FilenameWithoutPath>bt_code.c</FilenameWithoutPath>
+      <PathWithFileName>..\..\user\main.c</PathWithFileName>
+      <FilenameWithoutPath>main.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
Index: BT&BLE/prj/MDK/YC3121_Keil.uvprojx
===================================================================
--- /YC3121_demo/ModuleDemo/BlueTooth/BT&BLE/prj/MDK/YC3121_Keil.uvprojx	(revision 730)
+++ /YC3121_demo/ModuleDemo/BlueTooth/BT&BLE/prj/MDK/YC3121_Keil.uvprojx	(working copy)
@@ -199,7 +199,7 @@
             <NoZi1>0</NoZi1>
             <NoZi2>0</NoZi2>
             <NoZi3>0</NoZi3>
-            <NoZi4>0</NoZi4>
+            <NoZi4>1</NoZi4>
             <NoZi5>0</NoZi5>
             <Ro1Chk>0</Ro1Chk>
             <Ro2Chk>0</Ro2Chk>
@@ -274,8 +274,8 @@
               </OCR_RVCT3>
               <OCR_RVCT4>
                 <Type>1</Type>
-                <StartAddress>0x0</StartAddress>
-                <Size>0x40000</Size>
+                <StartAddress>0x1000000</StartAddress>
+                <Size>0x80000</Size>
               </OCR_RVCT4>
               <OCR_RVCT5>
                 <Type>1</Type>
@@ -299,8 +299,8 @@
               </OCR_RVCT8>
               <OCR_RVCT9>
                 <Type>0</Type>
-                <StartAddress>0x20000000</StartAddress>
-                <Size>0x20000</Size>
+                <StartAddress>0x20000</StartAddress>
+                <Size>0x10000</Size>
               </OCR_RVCT9>
               <OCR_RVCT10>
                 <Type>0</Type>
@@ -321,7 +321,7 @@
             <PlainCh>0</PlainCh>
             <Ropi>0</Ropi>
             <Rwpi>0</Rwpi>
-            <wLevel>2</wLevel>
+            <wLevel>1</wLevel>
             <uThumb>0</uThumb>
             <uSurpInc>0</uSurpInc>
             <uC99>1</uC99>
@@ -392,6 +392,11 @@
               <FileType>1</FileType>
               <FilePath>..\..\..\..\..\Librarier\core\system.c</FilePath>
             </File>
+            <File>
+              <FileName>bt_code.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>..\..\..\..\..\Librarier\core\bt_code.c</FilePath>
+            </File>
           </Files>
         </Group>
         <Group>
@@ -402,11 +407,6 @@
               <FileType>1</FileType>
               <FilePath>..\..\user\main.c</FilePath>
             </File>
-            <File>
-              <FileName>bt_code.c</FileName>
-              <FileType>1</FileType>
-              <FilePath>..\..\user\bt_code.c</FilePath>
-            </File>
           </Files>
         </Group>
         <Group>
Index: BT&BLE/user/main.c
===================================================================
--- /YC3121_demo/ModuleDemo/BlueTooth/BT&BLE/user/main.c	(revision 730)
+++ /YC3121_demo/ModuleDemo/BlueTooth/BT&BLE/user/main.c	(working copy)
@@ -28,11 +28,13 @@
 #include "yc_bt.h"
 #include "yc_systick.h"
 #include "yc_qspi.h"
+#include "yc_ipc.h"
+#include "board_config.h"
 
 /* Private typedef -----------------------------------------------------------*/
 /* Private define ------------------------------------------------------------*/
-#define uartBaud 115200
-#define PRINT_UART UART0
+#define IPC_UART UART0
+//#define UART_TO_IPC
 
 /* Private macro -------------------------------------------------------------*/
 /* Private variables ---------------------------------------------------------*/
@@ -61,9 +63,8 @@
 uint8_t ble_Characteristic_uuid_flow_ctrl[] = {0x18,0x10,0x18,0x03,0xa6,0x28,0x5e,0xd8,0xec,0x91,0x1c,0x48,0xa3,0xac,0x43,0x53,0x53,0x49,0x01,0x00};	
 
 	
-	
-#define IFLASH_NVRAM_ADDR 0x1000000+0x40000
-#define NVRAM_LEN 170//Nvram 长度170bytes,每个设备信息34个字节.
+#define IFLASH_NVRAM_ADDR 0x1000000+((512-4)*1024)
+#define NVRAM_LEN 170//Nvram 长度170bytes,每组Nvram长度为34bytes,最多可存储5组配对信息
 //#define SPP_FLOWCONTROL
 uint16_t ble_send_handle=0x2a;
 uint8_t bt_cod[3] = {0x24,0x04,0x04};
@@ -72,14 +73,27 @@
 	
 //write flash 
 uint8_t   ReserveNV(uint8_t* nvram);
+extern tick SystickCount;
+#ifdef UART_TO_IPC
+HCI_TypeDef HCI_Tx;
+HCI_TypeDef HCI_Rx;
+uint8_t ipc_tx_buf[256];
+uint8_t ipc_rx_buf[256];
+#endif
 
-extern tick SystickCount;;
 void BT_IRQHandler()
 {
-	//MyPrintf("BT_IRQHandler\n");
 	while(IPC_have_data())
 	{
+		#ifdef UART_TO_IPC
+		if(TRUE==IPC_ReadBtData(&HCI_Rx))
+		{
+			UART_SendBuf(IPC_UART,(uint8_t*)&HCI_Rx,3);
+			UART_SendBuf(IPC_UART,HCI_Rx.p_data,HCI_Rx.DataLen);
+		}
+		#else
 		BT_ParseBTData();
+		#endif
 	}
 
 	BT_CONFIG &= (~(1<<BT_INIT_FLAG));
@@ -88,6 +102,7 @@
 void SYSTICK_IRQHandler()
 {
 	SystickCount++;
+	if(SystickCount>=TICK_MAX_VALUE)	SystickCount=0;
 }
 
 
@@ -111,26 +126,30 @@
   */
 int main(void)
 {
-	uint8_t bt_name[17] = "YC3121demo";
-	uint8_t bt_addr[6] =  {0x00,0x00,0x00,0x33,0x22,0x11};
+	uint8_t bt_name[15] = "YC3121bt";
+	uint8_t bt_addr[6] =  {0x00,0x00,0x00,0x33,0x22,0x10};
 	uint8_t chip_id[6]={0};
 	uint16_t temp_handle;
 
 	int UARTCMD = 0;
 	uint8_t getbtstatus = 0;
 	int i = 0;
+	#ifdef UART_TO_IPC
+	HCI_Tx.p_data=ipc_tx_buf;
+	HCI_Rx.p_data=ipc_rx_buf;
+	#endif
 	UART_Configuration();
-
-	SysTick_Config(CPU_MHZ/1000);
+	MyPrintf("bt ble demo\n");
+	SysTick_Config(CPU_MHZ/100);
 	BT_Init();
 	enable_intr(INTR_BT);
 	
 	read_chipid(chip_id);
-	array2hex(chip_id+3,3,bt_name+10);
-	bt_name[16]='\0';
+	array2hex(chip_id,3,bt_name+8);
+	bt_name[14]='\0';
 	memcpy(bt_addr,chip_id,3);
 
-	MyPrintf("bt ble demo\n");
+	#ifndef UART_TO_IPC
 	if(BT_SetBleName(bt_name,sizeof(bt_name)-1)==TRUE) 
 		MyPrintf("SetBleName_suc ble name:%s\n",bt_name);
 	else 
@@ -198,11 +217,11 @@
 	
 	MyPrintf("bt version=%x\n",BT_GetVersion());
 	
-
-	memset(NvramData,0xff,NVRAM_LEN);
 	qspi_flash_read(IFLASH_NVRAM_ADDR,NvramData,NVRAM_LEN);
-	//nvram 包含5个设备信息
-	
+	if(NvramData[34*0] > 0x05||NvramData[34*1] > 0x05||NvramData[34*2] > 0x05||NvramData[34*3] > 0x05||NvramData[34*4] > 0x05)
+	{
+		memset(NvramData,0x00,NVRAM_LEN);
+	}
 	i=0;
 	if(BT_SetNVRAM(NvramData,NVRAM_LEN) == TRUE)
 	{
@@ -213,10 +232,30 @@
 	}
 	else
 		MyPrintf("set nvram failed\n");
+		#endif
 
 	while (1)
 	{
+		//uart cmd to ipc
+		#ifdef UART_TO_IPC
+		if(UART_IsRXFIFONotEmpty(IPC_UART))
+		{
+			HCI_Tx.type = UART_ReceiveData(IPC_UART);
+			while(!UART_IsRXFIFONotEmpty(IPC_UART));
+			HCI_Tx.opcode = UART_ReceiveData(IPC_UART);
+			while(!UART_IsRXFIFONotEmpty(IPC_UART));
+			HCI_Tx.DataLen = UART_ReceiveData(IPC_UART);
+			i=0;
+			while(i<HCI_Tx.DataLen)
+			{
+				while(!UART_IsRXFIFONotEmpty(IPC_UART));
+				HCI_Tx.p_data[i++] = UART_ReceiveData(IPC_UART);
+			}
+			IPC_TxData(&HCI_Tx);
+		}
+		#else
 		BT_Progress();
+		#endif
 	}
 }
 
@@ -230,8 +269,8 @@
 	UART_InitTypeDef UART_InitStruct;
 
 	/* Configure serial ports 0 RX and TX for IO. */
-	GPIO_Config(GPIOA, GPIO_Pin_1, UART0_TXD);
-	GPIO_Config(GPIOA, GPIO_Pin_0, UART0_RXD);
+	GPIO_Config(UART0_TX_PORT, UART0_TX_PIN, UART0_TXD);
+	GPIO_Config(UART0_RX_PORT, UART0_RX_PIN, UART0_RXD);
 
 	/* USARTx configured as follow:
   - BaudRate = 115200 baud  
@@ -241,7 +280,11 @@
   - Hardware flow control disabled (RTS and CTS signals)
   - Receive and transmit enabled
   */
-	UART_InitStruct.BaudRate = uartBaud;			//Configure serial port baud rate, the baud rate defaults to 128000.
+	#ifdef UART_TO_IPC
+	UART_InitStruct.BaudRate = 115200;			//Configure serial port baud rate, the baud rate defaults to 128000.
+	#else
+	UART_InitStruct.BaudRate = UARTBAUD;
+	#endif
 	UART_InitStruct.DataBits = Databits_8b;
 	UART_InitStruct.StopBits = StopBits_1;
 	UART_InitStruct.Parity = Parity_None;
@@ -261,7 +304,8 @@
 
 void BT_Progress()
 {	
-	switch(BT_GetEventOpcode())
+	int eventCmd= BT_GetEventOpcode();
+	switch(eventCmd)
 	{	
 		case BT_DATA:
 			{
@@ -283,7 +327,7 @@
 					if(BT_SetSppFlowcontrol(1) != TRUE)	MyPrintf("set Flowcontrol failed\n");
 					#endif
 				}
-				else	MyPrintf("read bt data error");
+				else	MyPrintf("read bt data error\r\n");
 			}
 		break;
 			
@@ -306,7 +350,7 @@
 					for(int i=0;i<BleDataLen;i++)	MyPrintf("%02x ",bleData[2+i]);
 					MyPrintf("\n");
 				}
-				else	MyPrintf("read ble data error");
+				else	MyPrintf("read ble data error\r\n");
 			}
 		break;
 
@@ -342,7 +386,7 @@
 					ReserveNV(NvramData);//将最新的5个设备信息（NVRAM） 写入flash ,
 				}
 				else	
-					MyPrintf("read Nvram data error");
+					MyPrintf("read Nvram data error\r\n");
 			}
 		break;
 
@@ -360,7 +404,7 @@
 					}
 					MyPrintf("\n");
 				}
-				else	MyPrintf("read CONFIRM_GKEY data error");
+				else	MyPrintf("read CONFIRM_GKEY data error\r\n");
 			
 				if(BT_ConfirmGkey(0) == TRUE)//BT_ConfirmGkey参数为0则继续配对，1则取消配对。
 					MyPrintf("set confirmkey  success");
@@ -387,18 +431,74 @@
 					}
 					MyPrintf("\n");
 				}
-				else	MyPrintf("read Nvram data error");
+				else	MyPrintf("read PAIRING_COMPLETED data error\r\n");
+			}
+		break;
+			
+		case PAIRING_STATE:
+			{
+				uint8_t PairingData[MAX_BLUETOOTH_DATA_LEN];
+				int PairingDataLen=0;
+				uint16_t pairing_status=0;
+				PairingDataLen=BT_ReadBTData(PairingData);
+				if(PairingDataLen==2)
+				{
+					pairing_status |=PairingData[0];
+					pairing_status |=(PairingData[1]<<8);
+					switch(pairing_status)
+					{
+						case 0x0001:
+							MyPrintf("BT Pairing Success\r\n");
+						break;
+						
+						case 0x0101:
+							MyPrintf("BT Pairing fail\r\n");
+						break;
+						
+						case 0x0080:
+							MyPrintf("BLE Pairing Success\r\n");
+						break;
+						
+						case 0x0180:
+							MyPrintf("BLE Pairing fail\r\n");
+						break;
+						
+						default:
+							MyPrintf("unknown pairing_status[%04x]\r\n",pairing_status);
+						break;
+					}
+				}
+				else	MyPrintf("read PAIRING_STATE data error\r\n");
 			}
 		break;
 			
+		case -1:
+			//no event
+		break;
+			
 		default:
+		{
+			MyPrintf("unknown event:%02x\r\n",eventCmd);
+			uint8_t unknownData[MAX_BLUETOOTH_DATA_LEN];
+			int unknownDataLen=0;
+			unknownDataLen=BT_ReadBTData(unknownData);
+			if(unknownDataLen>0)
+			{
+				MyPrintf("datalen:%d\r\n",unknownDataLen);
+				for(int i=0;i<unknownDataLen;i++)
+				{
+					MyPrintf("%02X ",unknownData[i]);
+				}
+				MyPrintf("\n");
+			}
+		}
 		break;
 	}
 }
 
 uint8_t   ReserveNV(uint8_t* nvram)
 {
-	MyPrintf("new nvram data,updata to flash ");
+	MyPrintf("new nvram data,updata to flash\r\n");
 	qspi_flash_sectorerase(IFLASH_NVRAM_ADDR);
 	return qspi_flash_write(IFLASH_NVRAM_ADDR,nvram,NVRAM_LEN);
 }
