Index: LPM_SLEEP/lpm_demo/user/main.c
===================================================================
--- /YC3121_demo/ModuleDemo/LPM_SLEEP/lpm_demo/user/main.c	(revision 843)
+++ /YC3121_demo/ModuleDemo/LPM_SLEEP/lpm_demo/user/main.c	(working copy)
@@ -33,6 +33,9 @@
 #include "yc_power.h"
 #include "yc_ssc.h"
 #include "board_config.h"
+#include "misc.h"
+#include "yc_rtc.h"
+
 
 /* Private typedef -----------------------------------------------------------*/
 /* Private define ------------------------------------------------------------*/
@@ -42,11 +45,17 @@
 
 /* Private macro -------------------------------------------------------------*/
 /* Private variables ---------------------------------------------------------*/
+extern uint8_t bt_lpm_mode;
 /* Private function prototypes -----------------------------------------------*/
 void UART_Configuration(void);
+void NVIC_Configuration(void);
+void BT_ConfigerInit(Boolean bleinitflag);
+void Power_off_test(void);
+static void Printf_Menu(void);
+void RTC_Configuration(void);
 
-
-
+//uint8_t connect_data[8] = {0x28, 0x00, 0x28, 0x00, 0x00, 0x00, 0xf4, 0x01};
+uint8_t connect_data[8] = {0x08, 0x89, 0x01, 0x90, 0x00, 0x00, 0x2c, 0x01};
 // byte0： uuid length
 //byte1-16: uuid
 // 49535343-FE78-4AE5-8FA9-9FAFD205E455
@@ -72,8 +81,8 @@
 
 uint16_t ble_send_handle=0x2a;
 uint8_t bt_cod[3] = {0x24,0x04,0x04};
-uint8_t NvramData[NVRAM_LEN]={0};
-
+uint8_t NvramData[NVRAM_LEN]= {0};
+uint8_t BT_WAKEUP_FLAG = 0;
 
 //write flash
 uint8_t   ReserveNV(uint8_t* nvram);
@@ -84,28 +93,33 @@
 uint8_t ipc_tx_buf[256];
 uint8_t ipc_rx_buf[256];
 #endif
-
+void HARD_FAULT_IRQHandler()
+{
+    soft_reset();
+}
 void BT_IRQHandler()
 {
-	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));
+    BT_WAKEUP_FLAG = 1;
+    bt_lpm_mode=0;
+    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));
 }
 
 void SYSTICK_IRQHandler()
 {
-	SystickCount++;
-	if(SystickCount>=TICK_MAX_VALUE)	SystickCount=0;
+    SystickCount++;
+    if(SystickCount>=TICK_MAX_VALUE)	SystickCount=0;
 }
 
 
@@ -113,12 +127,12 @@
 static uint8_t hex_lookup[16] = { '0', '1', '2', '3', '4', '5', '6', '7','8','9', 'a', 'b', 'c', 'd', 'e', 'f' };
 static void array2hex(uint8_t* in, int inlen, uint8_t* out)
 {
-	int i;
-	for (i = 0; i < inlen; ++i)
-	{
-		out[2 * i] = hex_lookup[in[inlen-1-i] >> 4];
-		out[2 * i + 1] = hex_lookup[in[inlen-1-i] & 0x0f];
-	}
+    int i;
+    for (i = 0; i < inlen; ++i)
+    {
+        out[2 * i] = hex_lookup[in[inlen-1-i] >> 4];
+        out[2 * i + 1] = hex_lookup[in[inlen-1-i] & 0x0f];
+    }
 }
 
 void genBleAdvData(uint8_t *ble_name,int ble_name_len,uint8_t *ble_mac,uint8_t *ble_adv_data)
@@ -131,7 +145,7 @@
     //name
     ble_adv_data[index++]=(ble_name_len+1);
     ble_adv_data[index++]=0x09;//name flag
-    for(int i=0;i<ble_name_len;i++)
+    for(int i=0; i<ble_name_len; i++)
     {
         ble_adv_data[index++]=ble_name[i];
     }
@@ -140,7 +154,7 @@
     ble_adv_data[index++]=0xff;//manufacturer info flag
     ble_adv_data[index++]=0x00;
     ble_adv_data[index++]=0x00;
-    for(int i=0;i<6;i++)
+    for(int i=0; i<6; i++)
     {
         ble_adv_data[index++]=ble_mac[5-i];
     }
@@ -152,103 +166,162 @@
 }
 
 
+
 /**
   * @brief  Main program
-  * @param  None
   * @retval None
+  * @param  None
   */
 
-void flash_powerdown(void);
-void BT_ConfigerInit(uint32_t bleinitflag);
-void Power_off_test(void);
-
-extern uint8_t bt_lpm_mode;
-uint32_t bpk_buf[32] = {0};
-#define  FLAG_LEN   1
 int main(void)
 {
-	UART_Configuration();
+    int rtccnt_back = 0;
+    UART_Configuration();
+    NVIC_Configuration();
+
     MyPrintf("YC3121 Sleep Mode Demo V1.0!\n");
-    SSC_LPMKeyRead(bpk_buf,FLAG_LEN,0);
+
+    Printf_Menu();
+
     SysTick_Config(CPU_MHZ/100);
-    BT_ConfigerInit(bpk_buf[0]);
+    BT_ConfigerInit(BT_IsNeedInitBt());
+
+    int uartretval = 0;
 
-    if(bpk_buf[0] == TRUE)             /*为TRUE 蓝牙不初始化唤醒，唤醒后清0*/
+    while (1)
     {
-        bpk_buf[0] = FALSE;
-        SSC_LPMKeyWrite(bpk_buf,FLAG_LEN,0);
-    }
-    MyPrintf("====================================\n");
-    MyPrintf(" Please input the following numbers to configure the sleep mode !\n");
-    MyPrintf(" 1: M0 Sleep and BT Deep Sleep\n");
-    MyPrintf(" 2: M0 Div Freqency and BT Deep sleep!\n");
-    MyPrintf(" 3: M0 Sleep and BT Light Sleep!\n");
-    MyPrintf("====================================\n");
-    uint8_t uartretval;
-	while (1)
-	{
         if(UART_IsRXFIFONotEmpty(UART0))
         {
             uartretval = UART_ReceiveData(UART0);
             switch (uartretval)
             {
-            case '1':
-                MyPrintf(" M0 Sleep and BT Deep Sleep....\n");
-                Disable_Trng();
+            case '1':                                                     //********************************************************
+                MyPrintf(" M0 Deep Sleep and BT Deep Sleep....\n");       //case功能：M0 深睡，BT 深睡
+                Disable_Trng();                                           //唤醒方式：GPIO 唤醒（不支持 RTC 定时唤醒，BT 唤醒）
                 GPIO_Unused_Pd();
-                GPIO_Config(GPIOA, GPIO_Pin_1, GPCFG_PD);
-                GPIO_Config(GPIOA, GPIO_Pin_0, GPCFG_PD);
-                GPIO_Config(GPIOA, GPIO_Pin_14, GPCFG_PD);
-                GPIO_Config(GPIOA, GPIO_Pin_15, GPCFG_PD);
-                CM0_Sleep(10,0,1<<1,0,1);                   //定时10S,IO33输入高唤醒
+                GPIO_Config(GPIOA, GPIO_Pin_1, PULL_DOWN);
+                GPIO_Config(GPIOA, GPIO_Pin_0, PULL_DOWN);
+                GPIO_Config(GPIOA, GPIO_Pin_14, PULL_DOWN);
+                GPIO_Config(GPIOA, GPIO_Pin_15, PULL_DOWN);
+
+                CM0_Sleep(0, GPIO_Pin_5, 0, 0, 2);
                 break;
+
             case '2':
-                MyPrintf(" M0 Div Freqency and BT Deep sleep....\n");
-                SYSCTRL_HCLKConfig(SYSCTRL_HCLK_Div8);          //降频
+                MyPrintf(" M0 Deep Sleep and BT Light Sleep....\n");      //********************************************************
+                Disable_Trng();                                           //case功能：M0 深睡，BT 浅睡，在此环境下，蓝牙可以维持连接
+                GPIO_Unused_Pd();                                         //唤醒方式：GPIO 唤醒，可配置高电平唤醒或者低电平唤醒
+                GPIO_Config(GPIOA, GPIO_Pin_1, PULL_DOWN);                //          BT 唤醒，BT 有事件上报就会唤醒，比如蓝牙连接和断开
+                GPIO_Config(GPIOA, GPIO_Pin_0, PULL_DOWN);                //          RTC 定时唤醒，单位为 100ms，此时由 BT 定时唤醒
+                GPIO_Config(GPIOA, GPIO_Pin_14, PULL_DOWN);
+                GPIO_Config(GPIOA, GPIO_Pin_15, PULL_DOWN);
+
+                CM0_Sleep(20, GPIO_Pin_5, 0, 0, 1);                       //定时 2s 唤醒(单位100ms)，IO5 输入高唤醒
+                break;
+
+            case '3':
+                MyPrintf(" M0 Deep Sleep and BT Runing....\n");           //*******************************************************
+                Disable_Trng();                                           //case功能：M0 深睡，BT 正常运行
+                GPIO_Unused_Pd();                                         //唤醒方式：GPIO 唤醒，可配置高电平唤醒或者低电平唤醒
+                GPIO_Config(GPIOA, GPIO_Pin_1, PULL_DOWN);                //          RTC 定时唤醒，单位为 100ms，此时由 BT 定时唤醒
+                GPIO_Config(GPIOA, GPIO_Pin_0, PULL_DOWN);
+                GPIO_Config(GPIOA, GPIO_Pin_14, PULL_DOWN);
+                GPIO_Config(GPIOA, GPIO_Pin_15, PULL_DOWN);
+
+                CM0_Sleep(20, GPIO_Pin_5, 0, 0, 0);                        //定时 2s 唤醒(单位100ms),IO5输入高唤醒
+                break;
+
+            case '4':
+                MyPrintf(" M0 Light Sleep and BT Deep Sleep....\n");      //********************************************
+                //case功能：M0 降频，BT 深睡
+                SYSCTRL_HCLKConfig(SYSCTRL_HCLK_Div8);                    //此功能下 M0 降频，可以通过检测 GPIO 的状态和读取 RTC 的计数值来恢复睡前状态
                 Disable_Trng();
                 GPIO_Unused_Pd();
-                GPIO_Config(GPIOA, GPIO_Pin_1, GPCFG_PD);
-                GPIO_Config(GPIOA, GPIO_Pin_0, GPCFG_PD);
-                GPIO_Config(GPIOA, GPIO_Pin_14, GPCFG_PD);
-                GPIO_Config(GPIOA, GPIO_Pin_15, GPCFG_PD);
+                GPIO_Config(GPIOA, GPIO_Pin_1, PULL_DOWN);
+                GPIO_Config(GPIOA, GPIO_Pin_0, PULL_DOWN);
+                GPIO_Config(GPIOA, GPIO_Pin_14, PULL_DOWN);
+                GPIO_Config(GPIOA, GPIO_Pin_15, PULL_DOWN);
                 SYSCTRL_AHBPeriphClockCmd(SYSCTRL_AHBPeriph_INTR | SYSCTRL_AHBPeriph_SHA | \
-                            SYSCTRL_AHBPeriph_CRC | SYSCTRL_AHBPeriph_PWM | \
-                            SYSCTRL_AHBPeriph_WDT | SYSCTRL_AHBPeriph_USB | \
-                            SYSCTRL_AHBPeriph_SPI | SYSCTRL_AHBPeriph_DES | \
-                            SYSCTRL_AHBPeriph_RSA | SYSCTRL_AHBPeriph_ASE | \
-                            SYSCTRL_AHBPeriph_7816 | SYSCTRL_AHBPeriph_SM4 | \
-                            SYSCTRL_AHBPeriph_7811 | SYSCTRL_AHBPeriph_ADC7811 | \
-                            SYSCTRL_AHBPeriph_UART | \
-                            SYSCTRL_AHBPeriph_CP, DISABLE);
+                                          SYSCTRL_AHBPeriph_CRC | SYSCTRL_AHBPeriph_PWM | \
+                                          SYSCTRL_AHBPeriph_WDT | SYSCTRL_AHBPeriph_USB | \
+                                          SYSCTRL_AHBPeriph_SPI | SYSCTRL_AHBPeriph_DES | \
+                                          SYSCTRL_AHBPeriph_RSA | SYSCTRL_AHBPeriph_ASE | \
+                                          SYSCTRL_AHBPeriph_7816 | SYSCTRL_AHBPeriph_SM4 | \
+                                          SYSCTRL_AHBPeriph_7811 | SYSCTRL_AHBPeriph_ADC7811 | \
+                                          SYSCTRL_AHBPeriph_CP, DISABLE);
+
                 BT_Hibernate();
+                rtccnt_back = RTC_GetRefRegister();
+                MyPrintf("trc = %d\r\n", rtccnt_back);
                 while(1)
                 {
-                    if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_1) == 1)        //配置IO33输入高唤醒
+                    if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_5) || ((RTC_GetRefRegister() - rtccnt_back) >= 5))    //配置 IO5 输入高恢复，RTC定时 5S 恢复
                     {
-                        IpcInit();
                         SYSCTRL_AHBPeriphClockCmd(SYSCTRL_AHBPeriph_INTR | \
-                        SYSCTRL_AHBPeriph_UART | SYSCTRL_AHBPeriph_CP, ENABLE);
+                                                  SYSCTRL_AHBPeriph_UART | SYSCTRL_AHBPeriph_CP, ENABLE);
                         GPIO_Config(UART0_TX_PORT, UART0_TX_PIN, UART0_TXD);
                         GPIO_Config(UART0_RX_PORT, UART0_RX_PIN, UART0_RXD);
                         SYSCTRL_HCLKConfig(SYSCTRL_HCLK_Div2);
+                        SysTick_Config(CPU_MHZ/100);
+                        BT_ConfigerInit(BT_IsNeedInitBt());
                         MyPrintf("Wake up !\n");
                         break;
                     }
                 }
                 break;
-            case '3':
-                MyPrintf(" M0 Sleep and BT Light Sleep....\n");
+            case '5':
+                MyPrintf(" M0 Light Sleep and BT Light Sleep....\n"); //**********************************************
+                if(BT_SetWakeupRtc(50) == TRUE)                       //case功能：M0 降频，BT 浅睡，BT此时可以维持连接
+                    MyPrintf("BT_SetWakeupRtc success\n");            //唤醒方式：GPIO 唤醒，RTC 唤醒
+                else
+                    MyPrintf("BT_SetWakeupRtc fail\n");
+
+                if( BT_SetWakeupGpio(0, GPIO_Pin_5, 0) == TRUE)
+                {
+                    MyPrintf("BT_SetWakeupGpio success\n");
+                }
+                else
+                {
+                    MyPrintf("BT_SetWakeupGpio fail\n");
+                }
+
+                SYSCTRL_HCLKConfig(SYSCTRL_HCLK_Div8);
+                Disable_Trng();
                 GPIO_Unused_Pd();
-                GPIO_Config(GPIOA, GPIO_Pin_1, GPCFG_PD);
-                GPIO_Config(GPIOA, GPIO_Pin_0, GPCFG_PD);
-                GPIO_Config(GPIOA, GPIO_Pin_14, GPCFG_PD);
-                GPIO_Config(GPIOA, GPIO_Pin_15, GPCFG_PD);
-                BT_SetLpmMode(0x01);                        //设置蓝牙低功耗
-                BT_SetLEAdvInterval(0x640);                 //设置广播间隔
-                bpk_buf[0] = TRUE;
-                SSC_LPMKeyWrite(bpk_buf,FLAG_LEN,0);
-                CM0_Sleep(0,0,0,0,0);
+                GPIO_Config(GPIOA, GPIO_Pin_1, PULL_DOWN);
+                GPIO_Config(GPIOA, GPIO_Pin_0, PULL_DOWN);
+                GPIO_Config(GPIOA, GPIO_Pin_14, PULL_DOWN);
+                GPIO_Config(GPIOA, GPIO_Pin_15, PULL_DOWN);
+                GPIO_Config(GPIOA, GPIO_Pin_2, PULL_DOWN);
+                SYSCTRL_AHBPeriphClockCmd(SYSCTRL_AHBPeriph_INTR | SYSCTRL_AHBPeriph_SHA | \
+                                          SYSCTRL_AHBPeriph_CRC | SYSCTRL_AHBPeriph_PWM | \
+                                          SYSCTRL_AHBPeriph_WDT | SYSCTRL_AHBPeriph_USB | \
+                                          SYSCTRL_AHBPeriph_SPI | SYSCTRL_AHBPeriph_DES | \
+                                          SYSCTRL_AHBPeriph_RSA | SYSCTRL_AHBPeriph_ASE | \
+                                          SYSCTRL_AHBPeriph_7816 | SYSCTRL_AHBPeriph_SM4 | \
+                                          SYSCTRL_AHBPeriph_7811 | SYSCTRL_AHBPeriph_ADC7811 | \
+                                          SYSCTRL_AHBPeriph_CP, DISABLE);
+                BT_WAKEUP_FLAG = 0;
+                BT_SetLpmMode(0x01);
+                IPC_dishold_bt();
+                while(1)
+                {
+                    if(BT_WAKEUP_FLAG)                               //蓝牙产生事件唤醒
+                    {
+                        BT_WAKEUP_FLAG = 0;
+                        SYSCTRL_AHBPeriphClockCmd(SYSCTRL_AHBPeriph_INTR | \
+                                                  SYSCTRL_AHBPeriph_UART | SYSCTRL_AHBPeriph_CP, ENABLE);
+                        GPIO_Config(UART0_TX_PORT, UART0_TX_PIN, UART0_TXD);
+                        GPIO_Config(UART0_RX_PORT, UART0_RX_PIN, UART0_RXD);
+                        SYSCTRL_HCLKConfig(SYSCTRL_HCLK_Div2);
+                        SysTick_Config(CPU_MHZ/100);
+                        BT_ConfigerInit(BT_IsNeedInitBt());
+                        MyPrintf("Wake up !\n");
+                        break;
+                    }
+                }
                 break;
+
             default:
                 break;
             }
@@ -257,9 +330,27 @@
         BT_Progress();
         /*芯片下电*/
         Power_off_test();
-	}
+    }
 }
 
+
+void NVIC_Configuration(void)
+{
+    NVIC_EnableIRQ(BT_IRQn);
+    NVIC_SetPriority(BT_IRQn, 1);
+}
+
+void RTC_Configuration(void)
+{
+    RTC_Calibration();
+    RTC_SetWakeUpCounter(0xffffffff);
+    RTC_SetRefRegister(0);
+
+    RTC_Config(ENABLE);
+    RTC_ClearITPendingBit();
+}
+
+
 /**
   * @brief  Serial port 0 initialization function.
   * @param  None
@@ -267,47 +358,49 @@
   */
 void UART_Configuration(void)
 {
-	UART_InitTypeDef UART_InitStruct;
+    UART_InitTypeDef UART_InitStruct;
 
-	/* Configure serial ports 0 RX and TX for IO. */
-	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
-  - Word Length = 8 Bits
-  - Stop Bit = 1 Stop Bit
-  - Parity = No Parity
-  - Hardware flow control disabled (RTS and CTS signals)
-  - Receive and transmit enabled
-  */
-	#ifdef UART_TO_IPC
-	UART_InitStruct.BaudRate = UART_TO_IPC_BAUDRATE;			//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;
-	UART_InitStruct.FlowCtrl = FlowCtrl_None;
-	UART_InitStruct.Mode = Mode_duplex;
+    /* Configure serial ports 0 RX and TX for IO. */
+    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
+    - Word Length = 8 Bits
+    - Stop Bit = 1 Stop Bit
+    - Parity = No Parity
+    - Hardware flow control disabled (RTS and CTS signals)
+    - Receive and transmit enabled
+    */
+#ifdef UART_TO_IPC
+    UART_InitStruct.BaudRate = UART_TO_IPC_BAUDRATE;			//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;
+    UART_InitStruct.FlowCtrl = FlowCtrl_None;
+    UART_InitStruct.Mode = Mode_duplex;
 
-	UART_Init(UART0, &UART_InitStruct);
+    UART_Init(UART0, &UART_InitStruct);
 }
 
 
-/**
-  * @brief  deal with data from bt core event
-  * @param  None
-  * @retval None
-  */
-
 
-void flash_powerdown(void)
+static void Printf_Menu(void)
 {
-    ((void(*)())(0x4e9c+1))();// m0 goto sleep
+    MyPrintf("=========================================\n");
+    MyPrintf("1: M0 Deep Sleep and BT Deep Sleep!\n");
+    MyPrintf("2: M0 Deep Sleep and BT Light Sleep!\n");
+    MyPrintf("3: M0 Deep Sleep and BT Runing!\n");
+    MyPrintf("4: M0 Light Sleep and BT Deep Sleep!\n");
+    MyPrintf("5: M0 Light Sleep and BT Light Sleep!\n");
+    MyPrintf("=========================================\n");
 }
 
+
+
 void Power_off_test(void)
 {
     uint8_t ret;
@@ -325,38 +418,43 @@
     }
 }
 
-void BT_ConfigerInit(uint32_t bleinitflag)
+void BT_ConfigerInit(Boolean bleinitflag)
 {
-	uint8_t bt_name[15] = "YC3121bt";
-	uint8_t bt_addr[6] =  {0x00,0x00,0x00,0x33,0x22,0x10};
-	uint8_t chip_id[6]={0};
-    uint8_t ble_adv_data[31]={0x02,0x01,0x02,0x00};
-	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
-    if(bleinitflag != TRUE)
+    uint16_t rtc_sexmax = 0;
+    uint8_t bt_name[15] = "YC3121bt";
+    uint8_t bt_addr[6] =  {0x00,0x00,0x00,0x33,0x22,0x10};
+    uint8_t chip_id[6]= {0};
+    uint8_t ble_adv_data[31]= {0x02,0x01,0x02,0x00};
+    uint16_t temp_handle;
+
+    int i = 0;
+#ifdef UART_TO_IPC
+    HCI_Tx.p_data=ipc_tx_buf;
+    HCI_Rx.p_data=ipc_rx_buf;
+#endif
+    if(bleinitflag == TRUE)
     {
+        RTC_Configuration();
+        rtc_sexmax = lpm_read(LPM_SECMAX);
+        lpm_write((int*)(0xf8480), rtc_sexmax);
         BT_Init();
         enable_intr(INTR_BT);
     }
     else
     {
+        rtc_sexmax = lpm_read((int*)(0xf8480));
+        RTC_Set_SecMax(rtc_sexmax);
         BT_WakeupInit();
     }
 
-	read_chipid(chip_id);
-	array2hex(chip_id,3,bt_name+8);
-	bt_name[14]='\0';
-	memcpy(bt_addr,chip_id,3);
-    if(bleinitflag != TRUE)
+    read_chipid(chip_id);
+    array2hex(chip_id,3,bt_name+8);
+    bt_name[14]='\0';
+    memcpy(bt_addr,chip_id,3);
+
+    if(bleinitflag == TRUE)
     {
-        #ifndef UART_TO_IPC
+#ifndef UART_TO_IPC
         if(BT_SetBleName(bt_name,sizeof(bt_name)-1)==TRUE)
             MyPrintf("SetBleName_suc ble name:%s\n",bt_name);
         else
@@ -387,12 +485,6 @@
         else
             MyPrintf("set COD failed\n");
 
-        if(BT_SetLpmMode(0x00) == TRUE) //设置低功耗模式
-            MyPrintf("set lpm mode sucess\n");
-        else
-            MyPrintf("set lpm mode failed\n");
-
-
         if(BT_DeleteService() == TRUE) //  删除用户自定义服务
             MyPrintf("delete service sucess\n");
         else
@@ -433,12 +525,12 @@
         else
             MyPrintf("BT_SetLEScanData failed\n");
 
-        if(BT_SetVisibility(0x00,0x00,0x01) == TRUE)
+        if(BT_SetVisibility(FALSE, FALSE, TRUE) == TRUE)
             MyPrintf("SetVisibility sucess\n");
         else
             MyPrintf("SetVisibility failed\n");
 
-        BT_SetLEAdvInterval(0x40);
+        BT_SetLEAdvInterval(0x640);             //设置广播间隔1S
 
         MyPrintf("bt version=%x\n",BT_GetVersion());
 
@@ -457,215 +549,215 @@
         }
         else
             MyPrintf("set nvram failed\n");
-        #else
+#else
         MyPrintf("uart baud rate is:%d\n",UART_TO_IPC_BAUDRATE);
-        #endif
+#endif
     }
 }
 
 
 void BT_Progress()
 {
-	int eventCmd= BT_GetEventOpcode();
+    int eventCmd= BT_GetEventOpcode();
     if(eventCmd != 0xFFFFFFFF)
         MyPrintf("eventCmd = 0x%x\n",eventCmd);
-	switch(eventCmd)
-	{
-		case BT_DATA:
-			{
-				uint8_t i = 0;
-				uint8_t btData[MAX_BLUETOOTH_DATA_LEN];
-				int BtDataLen=0;
-				BtDataLen=BT_ReadBTData(btData);
-				if(BtDataLen>0)
-				{
-					MyPrintf("received %d bytes bt data:\n",BtDataLen);
-					for(int i=0;i<BtDataLen;i++)	MyPrintf("%02x ",btData[i]);
-					MyPrintf("\n");
-
-					BT_SendSppData(btData,BtDataLen);
-					MyPrintf("send %d bytes bt data:\n",BtDataLen);
-					for(int i=0;i<BtDataLen;i++)	MyPrintf("%02x ",btData[i]);
-					MyPrintf("\n");
-					#if (MEM_SPP_FLOWCTRL_FLAG_VALUE==0x01)
-					if(BT_SetSppFlowcontrol(1) != TRUE)	MyPrintf("set Flowcontrol failed\n");
-					#endif
-				}
-				else	MyPrintf("read bt data error\r\n");
-			}
-		break;
-
-		case BLE_DATA:
-			{
-				uint8_t bleData[MAX_BLUETOOTH_DATA_LEN]={0};
-				int BleDataLen=0;
-				BleDataLen=BT_ReadBTData(bleData);
-				if(BleDataLen>0)
-				{
-					MyPrintf("received %d bytes ble data[handle:%02x %02x]:\n",BleDataLen-2,bleData[0],bleData[1]);
-					for(int i=0;i<(BleDataLen-2);i++)	MyPrintf("%02x ",bleData[2+i]);
-					MyPrintf("\n");
-
-					//add handle
-					bleData[0]=(ble_send_handle&0xff);
-					bleData[1]=((ble_send_handle>>8)&0xff);
-					BT_SendBleData(bleData,BleDataLen);
-					MyPrintf("send %d bytes ble data[%02x %02x]:\n",BleDataLen-2,bleData[0],bleData[1]);
-					for(int i=0;i<(BleDataLen-2);i++)	MyPrintf("%02x ",bleData[2+i]);
-					MyPrintf("\n");
-				}
-				else	MyPrintf("read ble data error\r\n");
-			}
-		break;
-
-		case BT_CONNECTED:
-			MyPrintf("bt connected\n");
-		break;
-
-		case BLE_CONNECTED:
-			MyPrintf("ble connected\n");
-		break;
-
-		case BT_DISCONNECTED:
-			MyPrintf("bt disconnected\n");
-		break;
-
-		case BLE_DISCONNECTED:
-			MyPrintf("ble disconnected\n");
-		break;
-
-		case NVRAM_DATA:
-			{
-				uint8_t NvramData[MAX_BLUETOOTH_DATA_LEN];
-				int NvramDataLen=0;
-				NvramDataLen=BT_ReadBTData(NvramData);
-				if(NvramDataLen>0)
-				{
-					MyPrintf("receive %dbytes Nvramdata:",NvramDataLen);
-					for(int i=0;i<NvramDataLen;i++)
-					{
-						MyPrintf("%02X ",NvramData[i]);
-					}
-					MyPrintf("\n");
-					ReserveNV(NvramData);//将最新的5个设备信息（NVRAM） 写入flash ,
-				}
-				else
-					MyPrintf("read Nvram data error\r\n");
-			}
-		break;
-
-		case CONFIRM_GKEY:
-			{
-				uint8_t GkeyData[MAX_BLUETOOTH_DATA_LEN];
-				int GkeyDataLen=0;
-				GkeyDataLen=BT_ReadBTData(GkeyData);
-				if(GkeyDataLen>0)
-				{
-					MyPrintf("receive %dbytes CONFIRM_GKEY:",GkeyDataLen);
-					for(int i=0;i<GkeyDataLen;i++)
-					{
-						MyPrintf("%02X ",GkeyData[i]);
-					}
-					MyPrintf("\n");
-				}
-				else	MyPrintf("read CONFIRM_GKEY data error\r\n");
-
-				if(BT_ConfirmGkey(0) == TRUE)//BT_ConfirmGkey参数为0则继续配对，1则取消配对。
-					MyPrintf("set confirmkey  success");
-				else
-					MyPrintf("set confirmkey  failed\n");
-		}
-		break;
-
-		case PASSKEY:
-			MyPrintf("request Passkey");
-		break;
-
-		case PAIRING_COMPLETED:
-			{
-				uint8_t PairingData[MAX_BLUETOOTH_DATA_LEN];
-				int PairingDataLen=0;
-				PairingDataLen=BT_ReadBTData(PairingData);
-				if(PairingDataLen>0)
-				{
-					MyPrintf("receive %dbytes paring data:",PairingDataLen);
-					for(int i=0;i<PairingDataLen;i++)
-					{
-						MyPrintf("%02X ",PairingData[i]);
-					}
-					MyPrintf("\n");
-				}
-				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;
-	}
+    switch(eventCmd)
+    {
+    case BT_DATA:
+    {
+        uint8_t btData[MAX_BLUETOOTH_DATA_LEN];
+        int BtDataLen=0;
+        BtDataLen=BT_ReadBTData(btData);
+        if(BtDataLen>0)
+        {
+            MyPrintf("received %d bytes bt data:\n",BtDataLen);
+            for(int i=0; i<BtDataLen; i++)	MyPrintf("%02x ",btData[i]);
+            MyPrintf("\n");
+
+            BT_SendSppData(btData,BtDataLen);
+            MyPrintf("send %d bytes bt data:\n",BtDataLen);
+            for(int i=0; i<BtDataLen; i++)	MyPrintf("%02x ",btData[i]);
+            MyPrintf("\n");
+#if (MEM_SPP_FLOWCTRL_FLAG_VALUE==0x01)
+            if(BT_SetSppFlowcontrol(1) != TRUE)	MyPrintf("set Flowcontrol failed\n");
+#endif
+        }
+        else	MyPrintf("read bt data error\r\n");
+    }
+    break;
+
+    case BLE_DATA:
+    {
+        uint8_t bleData[MAX_BLUETOOTH_DATA_LEN]= {0};
+        int BleDataLen=0;
+        BleDataLen=BT_ReadBTData(bleData);
+        if(BleDataLen>0)
+        {
+            MyPrintf("received %d bytes ble data[handle:%02x %02x]:\n",BleDataLen-2,bleData[0],bleData[1]);
+            for(int i=0; i<(BleDataLen-2); i++)	MyPrintf("%02x ",bleData[2+i]);
+            MyPrintf("\n");
+
+            //add handle
+            bleData[0]=(ble_send_handle&0xff);
+            bleData[1]=((ble_send_handle>>8)&0xff);
+            BT_SendBleData(bleData,BleDataLen);
+            MyPrintf("send %d bytes ble data[%02x %02x]:\n",BleDataLen-2,bleData[0],bleData[1]);
+            for(int i=0; i<(BleDataLen-2); i++)	MyPrintf("%02x ",bleData[2+i]);
+            MyPrintf("\n");
+        }
+        else	MyPrintf("read ble data error\r\n");
+    }
+    break;
+
+    case BT_CONNECTED:
+        MyPrintf("bt connected\n");
+        break;
+
+    case BLE_CONNECTED:
+        MyPrintf("ble connected\n");
+        break;
+
+    case BT_DISCONNECTED:
+        MyPrintf("bt disconnected\n");
+        break;
+
+    case BLE_DISCONNECTED:
+        MyPrintf("ble disconnected\n");
+        break;
+
+    case NVRAM_DATA:
+    {
+        uint8_t NvramData[MAX_BLUETOOTH_DATA_LEN];
+        int NvramDataLen=0;
+        NvramDataLen=BT_ReadBTData(NvramData);
+        if(NvramDataLen>0)
+        {
+            MyPrintf("receive %dbytes Nvramdata:",NvramDataLen);
+            for(int i=0; i<NvramDataLen; i++)
+            {
+                MyPrintf("%02X ",NvramData[i]);
+            }
+            MyPrintf("\n");
+            ReserveNV(NvramData);//将最新的5个设备信息（NVRAM） 写入flash ,
+        }
+        else
+            MyPrintf("read Nvram data error\r\n");
+    }
+    break;
+
+    case CONFIRM_GKEY:
+    {
+        uint8_t GkeyData[MAX_BLUETOOTH_DATA_LEN];
+        int GkeyDataLen=0;
+        GkeyDataLen=BT_ReadBTData(GkeyData);
+        if(GkeyDataLen>0)
+        {
+            MyPrintf("receive %dbytes CONFIRM_GKEY:",GkeyDataLen);
+            for(int i=0; i<GkeyDataLen; i++)
+            {
+                MyPrintf("%02X ",GkeyData[i]);
+            }
+            MyPrintf("\n");
+        }
+        else	MyPrintf("read CONFIRM_GKEY data error\r\n");
+
+        if(BT_ConfirmGkey(0) == TRUE)//BT_ConfirmGkey参数为0则继续配对，1则取消配对。
+            MyPrintf("set confirmkey  success");
+        else
+            MyPrintf("set confirmkey  failed\n");
+    }
+    break;
+
+    case PASSKEY:
+        MyPrintf("request Passkey");
+        break;
+
+    case PAIRING_COMPLETED:
+    {
+        uint8_t PairingData[MAX_BLUETOOTH_DATA_LEN];
+        int PairingDataLen=0;
+        PairingDataLen=BT_ReadBTData(PairingData);
+        if(PairingDataLen>0)
+        {
+            MyPrintf("receive %dbytes paring data:",PairingDataLen);
+            for(int i=0; i<PairingDataLen; i++)
+            {
+                MyPrintf("%02X ",PairingData[i]);
+            }
+            MyPrintf("\n");
+        }
+        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\r\n");
-	qspi_flash_sectorerase(IFLASH_NVRAM_ADDR);
-	return qspi_flash_write(IFLASH_NVRAM_ADDR,nvram,NVRAM_LEN);
+    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);
 }
 
+
 /************************ (C) COPYRIGHT Yichip Microelectronics *****END OF FILE****/
 
