Index: readme.txt
===================================================================
--- /YC3121_SDK/fw/sdk/yc_7816/readme.txt	(revision 704)
+++ /YC3121_SDK/fw/sdk/yc_7816/readme.txt	(working copy)
@@ -1,16 +1,46 @@
-在使用SCI接口时，需要初始化SCI各IO口引脚，根据实际使用引脚和卡到位检测电平进行初始化。卡到位电平设置为未插卡时IO引脚电平状态 1、低为0.
+在使用SCI接口时，需要初始化SCI各IO口引脚，根据实际使用引脚和卡到位检测电平进行初始化。
+注意一：卡到位电平设置为未插卡时IO引脚电平状态。 1为高电平、低为0低电平.
+
+下面函数在外设初始化时调用。
+void ISO7816_IO_Config(void)
+{
+    sci_pin.clk.gpio_group = GPIOC;
+    sci_pin.clk.gpio_pin = GPIO_Pin_13;
+
+    sci_pin.io.gpio_group = GPIOC;
+    sci_pin.io.gpio_pin = GPIO_Pin_14;
+
+    sci_pin.reset.gpio_group = GPIOC;
+    sci_pin.reset.gpio_pin = GPIO_Pin_15;
+
+    sci_pin.detect.gpio_group = GPIOC;
+    sci_pin.detect.gpio_pin = GPIO_Pin_12;
+	GPIO_Config(GPIOC, GPIO_Pin_12, GPCFG_INPUT);
+    sci_pin.detect_state = 1;
+
+    period_7816 = (CPU_MHZ / 4000000 / 2) - 1;
+    contact_reset_l_cnt = 42000 * (CPU_MHZ / 1000000) / 4;
+    contact_atr_rfb_time_cnt = 47000 * (CPU_MHZ / 1000000) / 4;
+    contact_atr_rab_time_cnt = (20164 * (CPU_MHZ / 1000000) / 4) * 372;
+
+    atr_time_start();
+}
+
+注意二：
+yc_7816.h内宏定义
+量产时应如下：
+//#define DEBUG_7816_EMV          //定义时开启EMV测试时打印交互状态
+//#define DEBUG_7816              //定义时开启打印交互数据
+//#define EMV_L1_TEST             //定义时进行EMV测试，量产代码屏蔽此定义
+
+过检时如下：
+#define DEBUG_7816_EMV          //定义时开启EMV测试时打印交互状态
+//#define DEBUG_7816              //定义时开启打印交互数据
+#define EMV_L1_TEST             //定义时进行EMV测试，量产代码屏蔽此定义
+
+注意三：
+SCI模块用到两个定时器。定时器0用于计时、定时器3用于产生clk。应用代码要避免使用这两个定时器。
+
+注意四：
+量产时开启超时退出功能，时间设置默认为3S.应用固件如开启看门狗建议时间大于等于6S.
 
-下面例子
-sci_pin.clk.gpio_group = GPIOC;
-sci_pin.clk.gpio_pin = GPIO_Pin_13;
-	
-sci_pin.io.gpio_group = GPIOC;
-sci_pin.io.gpio_pin = GPIO_Pin_14;
-	
-sci_pin.reset.gpio_group = GPIOC;
-sci_pin.reset.gpio_pin = GPIO_Pin_15;
-	
-sci_pin.detect.gpio_group = GPIOC;
-sci_pin.detect.gpio_pin = GPIO_Pin_9;
-	
-sci_pin.detect_state = 0;
\ No newline at end of file
Index: yc_7816.h
===================================================================
--- /YC3121_SDK/fw/sdk/yc_7816/yc_7816.h	(revision 704)
+++ /YC3121_SDK/fw/sdk/yc_7816/yc_7816.h	(working copy)
@@ -1,8 +1,8 @@
 /*
 File Name    : yc_iso7816.h
 Author       : Yichip
-Version      : V1.1
-Date         : 2019/12/16
+Version      : V2.0
+Date         : 2020/7/28
 Description  : iso7816 encapsulation.
 */
 
@@ -12,8 +12,9 @@
 #include "yc3121.h"
 #include "yc_gpio.h"
 
-#define DEBUG_7816_EMV
-#define DEBUG_7816
+//#define DEBUG_7816_EMV          //瀹氫箟鏃跺紑鍚疎MV娴嬭瘯鏃舵墦鍗颁氦浜掔姸鎬
+//#define DEBUG_7816              //瀹氫箟鏃跺紑鍚墦鍗颁氦浜掓暟鎹
+//#define EMV_L1_TEST             //瀹氫箟鏃惰繘琛孍MV娴嬭瘯锛岄噺浜т唬鐮佸睆钄芥瀹氫箟
 
 typedef struct
 {
@@ -33,12 +34,15 @@
 extern SCI_PIN_Def sci_pin;
 
 #define 	TIM0_CNT    TIM_CNT(0)
+#define     SYSCTRL_HWCTRL_VAL  SYSCTRL_HWCTRL(0)     
 
 #define 	ISO7816_TIMx  TIM3
 
 #define 	CONTACT_RESET_L_CNT				contact_reset_l_cnt        //(40000-45000) 澶嶄綅寮曡剼淇濇寔浣庣數骞虫寔缁椂闂
 #define 	CONTACT_ATR_RFB_TIME_CNT		contact_atr_rfb_time_cnt        //42019(400-40000)	绛夊緟IC鍥炲簲绗竴涓狝TR瀛楄妭鐨勬渶澶ф椂闂
 #define 	CONTACT_ATR_RAB_TIME_CNT		contact_atr_rab_time_cnt  //19200etu鍐呭浣嶄俊鎭簲绛斿畬鎴(TS - LAST)
+#define     CONTACT_OVER_TIME_CNT           contact_over_time_cnt       //閲忎骇涓渶澶х殑鎺ユ敹銆佸彂閫佽秴鏃舵椂闂达紝璁剧疆涓3S
+#define     CONTACT_OVER_TIME_VAL           3                           //閲忎骇涓渶澶х殑鎺ユ敹銆佸彂閫佽秴鏃舵椂闂达紝璁剧疆涓3S
 
 #define 	ATR_SIZE_MAX                0x33
 #define	 	REC_SIZE_MAX                0x107 //5+256+2
@@ -52,6 +56,7 @@
 #define 	OK                          0
 #define 	NOK                         1
 
+#define     ISO7816_POWER_OFF           0x4E
 #define     ISO7816_ATR_ERROR           0x4F
 #define     ISO7816_ATR_LEN_ERROR       0x50
 #define     ISO7816_ATR_TCK_ERROR       0x51
@@ -61,6 +66,7 @@
 #define     ISO7816_OPERATE_MODE_ERROR  0x57
 #define     ISO7816_PARA_ERROR          0x59
 #define     ISO7816_REC_TIMEOUT         0x5A
+#define     ISO7816_SEND_TIMEOUT        0x5B
 #define     ISO7816_ERR_NUM_OVER        0x5C
 #define     ISO7816_T1_TRANSFER_ERROR   0x60
 
@@ -202,6 +208,9 @@
 extern uint32_t contact_reset_l_cnt;
 extern uint32_t contact_atr_rfb_time_cnt;
 extern uint32_t contact_atr_rab_time_cnt;
+extern uint32_t contact_over_time_cnt;
+
+extern volatile unsigned int time0_cnt_last;
 
 void atr_time_start(void);
 uint16_t ISO7816_OperateSelect(uint8_t OperateMode, uint8_t vcard_set);
Index: yc_7816.c
===================================================================
--- /YC3121_SDK/fw/sdk/yc_7816/yc_7816.c	(revision 704)
+++ /YC3121_SDK/fw/sdk/yc_7816/yc_7816.c	(working copy)
@@ -1,8 +1,8 @@
 /*
 File Name    : yc_iso7816.c
 Author       : Yichip
-Version      : V1.1
-Date         : 2019/12/16
+Version      : V2.0
+Date         : 2020/7/28
 Description  : iso7816 encapsulation.
 */
 
@@ -45,6 +45,7 @@
 uint32_t contact_reset_l_cnt = 0;
 uint32_t contact_atr_rfb_time_cnt = 0;
 uint32_t contact_atr_rab_time_cnt = 0;
+uint32_t contact_over_time_cnt = 0;
 
 //璺烼1鏈夊叧鐨勫弬鏁
 st_T1_Parameters g_T1;
@@ -141,6 +142,8 @@
 void atr_time_start(void)
 {
     timer0_enable(0xffffffff);
+
+    contact_over_time_cnt = CONTACT_OVER_TIME_VAL*CPU_MHZ;
 }
 
 uint16_t ISO7816_RecAtrBytes(uint8_t *RxBuf, uint16_t RxLen)
@@ -854,8 +857,15 @@
         {
             return ISO7816_CARD_STUTES_ERROR;
         }
-
-        ret = ISO7816_cold_reset(vcard_set);
+        if ((SYSCTRL_HWCTRL_VAL & 8) == 0)
+        {
+            ret = ISO7816_cold_reset(vcard_set);
+        }
+        else
+        {
+            ISO7816_warm_reset();
+            ret = OK;
+        }
         if (ret != OK)
         {
             return ISO7816_CARD_STUTES_ERROR;
Index: yc_7816_T0.c
===================================================================
--- /YC3121_SDK/fw/sdk/yc_7816/yc_7816_T0.c	(revision 704)
+++ /YC3121_SDK/fw/sdk/yc_7816/yc_7816_T0.c	(working copy)
@@ -13,7 +13,7 @@
     }
 
     SCI7816_CWT = terminal_rec_cwt;
-
+    time0_cnt_last = TIM0_CNT;
     do
     {
         if (detect_io_state() == sci_pin.detect_state)
@@ -29,6 +29,21 @@
             return ISO7816_ERR_NUM_OVER;
         }
 
+        if ((SYSCTRL_HWCTRL_VAL & 8) == 0)
+        {
+            return ISO7816_POWER_OFF;
+        }
+
+        #ifndef EMV_L1_TEST
+        if ((time0_cnt_last - TIM0_CNT) >=contact_over_time_cnt)
+        {
+            #ifdef DEBUG_7816
+			MyPrintf("REC_TIMEOUT\n");
+			#endif
+            return ISO7816_REC_TIMEOUT;
+        }
+        #endif
+
         if (SCI7816_STAT & 1)
         {
             *(RxBuf + RemainLen) = SCI7816_DATA;
@@ -60,6 +75,7 @@
 #endif
     delay_ms(2);
 
+    time0_cnt_last = TIM0_CNT;
     SCI7816_CWT = 0;						//quick startup send
     while (SCI7816_STAT & 8)				//dummy
     {
@@ -67,6 +83,20 @@
         {
             return ISO7816_CARD_ABSENT;
         }
+        if ((SYSCTRL_HWCTRL_VAL & 8) == 0)
+        {
+            return ISO7816_POWER_OFF;
+        }
+
+        #ifndef EMV_L1_TEST
+        if ((time0_cnt_last - TIM0_CNT) >=contact_over_time_cnt)
+        {
+            #ifdef DEBUG_7816
+			MyPrintf("SEND_TIMEOUT1\n");
+			#endif
+            return ISO7816_SEND_TIMEOUT;
+        }
+        #endif
     }
 
     for (i = 0; i < Len; i++)
@@ -88,6 +118,21 @@
             {
                 return ISO7816_CARD_ABSENT;
             }
+
+            if ((SYSCTRL_HWCTRL_VAL & 8) == 0)
+            {
+                return ISO7816_POWER_OFF;
+            }
+
+            #ifndef EMV_L1_TEST
+            if ((time0_cnt_last - TIM0_CNT) >=contact_over_time_cnt)
+            {
+                #ifdef DEBUG_7816
+                MyPrintf("SEND_TIMEOUT2\n");
+                #endif
+                return ISO7816_SEND_TIMEOUT;
+            }
+            #endif
         }
 
 #ifdef DEBUG_7816
@@ -101,6 +146,21 @@
         {
             return ISO7816_CARD_ABSENT;
         }
+
+        if ((SYSCTRL_HWCTRL_VAL & 8) == 0)
+        {
+            return ISO7816_POWER_OFF;
+        }
+
+        #ifndef EMV_L1_TEST
+        if ((time0_cnt_last - TIM0_CNT) >=contact_over_time_cnt)
+        {
+            #ifdef DEBUG_7816
+			MyPrintf("SEND_TIMEOUT3\n");
+			#endif
+            return ISO7816_SEND_TIMEOUT;
+        }
+        #endif
     }
     SCI7816_CWT = (terminal_rec_cwt | (1 << 24));
 
