/******************************************************************************/
/** \file lpcd.c
 **
 ** lpcd driver API.
 ** @link Driver Group Some description @endlink
 **
 **   - 2020-07-29  1.0  Ronnie     First version for Device Driver Library of
 **                                 Module.
 **
 ******************************************************************************/
/*******************************************************************************
 * Include files
 ******************************************************************************/
#include "lpcd.h"
#include "fm175xx.h"
#include "user_nfc.h"
#include "mifare_card.h"
#include "type_a.h"
#include "type_b.h"
#include "string.h"

//#define TAG		"lpcd"

struct lpcd_struct Lpcd;

void Lpcd_Set_IRQ_pin(void)
{
	SetReg(ComIEnReg, BIT7);//IRQ引脚反相输出			 
	SetReg(DivIEnReg, BIT7);//IRQ引脚CMOS输出模式（IRQ引脚不需要外接上拉电阻）   
}

//***********************************************
//函数名称：Lpcd_Reset_Status()
//函数功能：LPCD功能复位
//入口参数：
//出口参数：SUCCESS：操作完成 
//***********************************************
bool Lpcd_Reset_Status(void)//LPCD功能复位
{
	Write_Ext_Reg(JREG_LPCD_CTRL1, JBIT_BIT_CTRL_CLR + JBIT_LPCD_RSTN);  //复位LPCD寄存器
	Write_Ext_Reg(JREG_LPCD_CTRL1, JBIT_BIT_CTRL_SET + JBIT_LPCD_RSTN); //复位放开LPCD寄存器
	return true;
}

//***********************************************
//函数名称：Lpcd_Init_Register()
//函数功能：LPCD寄存器初始化
//入口参数：
//出口参数：SUCCESS：操作完成  
//***********************************************
bool Lpcd_Init_Register(void)
{
  	Lpcd_Set_IRQ_pin();
	Write_Ext_Reg(JREG_LPCD_CTRL1,JBIT_BIT_CTRL_SET + JBIT_LPCD_EN);	//使能LPCD功能		
	
    if(LPCD_AUTO_DETECT_TIMES == 1)
	{
        Write_Ext_Reg(JREG_LPCD_CTRL1,JBIT_BIT_CTRL_SET + JBIT_LPCD_SENSE_1);//配置1次检测
	}
    else{
        Write_Ext_Reg(JREG_LPCD_CTRL1,JBIT_BIT_CTRL_CLR + JBIT_LPCD_SENSE_1);//配置3次检测
	}
	
	Write_Ext_Reg(JREG_LPCD_CTRL3,LPCD_MODE << 3);//配置LpcdCtrl3寄存器,LPCD工作模式	
	Write_Ext_Reg(JREG_LPCD_AUTO_WUP_CFG,(LPCD_AUTO_WUP_EN << 3) + LPCD_AUTO_WUP_CFG );//配置Auto_Wup_Cfg寄存器 
                  
	return true;
}

//***********************************************
//函数名称：Lpcd_Set_Timer()
//函数功能：LPCD设置Timer
//入口参数：
//出口参数：SUCCESS：操作完成  
//***********************************************
bool Lpcd_Set_Timer(void)
{
	Lpcd.Timer1 = LPCD_TIMER1_CFG & 0x0F;//TIMER1 = 0x01~0x0F
	Lpcd.Timer2 = LPCD_TIMER2_CFG & 0x1F;//TIMER2 = 0x01~0x1F
	Lpcd.Timer3 = LPCD_TIMER3_CFG & 0x1F;//TIMER3 = 0x03~0x1F
	Lpcd.TimerVmid = LPCD_TIMER_VMID_CFG & 0x1F;
    if (Lpcd.Timer3 > 0xF) //Timer3Cfg用到5bit，选择16分频
    {
        Lpcd.Timer3_Offset = 0x05;
		Lpcd.Timer3_Div  = 2;			//16分频
		Lpcd.Fullscale_Value =  ((Lpcd.Timer3 - 1)<<3) - Lpcd.Timer3_Offset;	
    }
	else if(Lpcd.Timer3 > 0x7) //Timer3Cfg用到4bit，选择8分频
	{
        Lpcd.Timer3_Offset = 0x0E;
		Lpcd.Timer3_Div  = 1;			//8分频
		Lpcd.Fullscale_Value =  ((Lpcd.Timer3 - 1)<<4) - Lpcd.Timer3_Offset;	
	}
	else{
        Lpcd.Timer3_Offset = 0x1F;
		Lpcd.Timer3_Div  = 0;			//4分频
		Lpcd.Fullscale_Value =  ((Lpcd.Timer3 - 1)<<5) - Lpcd.Timer3_Offset;	
	}
	Lpcd.Calibration.Range_L = LPCD_THRESHOLD;// 
    Lpcd.Calibration.Range_H = Lpcd.Fullscale_Value - LPCD_THRESHOLD;// 
	Write_Ext_Reg(JREG_LPCD_T1CFG, (Lpcd.Timer3_Div <<4) + Lpcd.Timer1);//配置Timer1Cfg寄存器
	Write_Ext_Reg(JREG_LPCD_T2CFG, Lpcd.Timer2);//配置Timer2Cfg寄存器
	Write_Ext_Reg(JREG_LPCD_T3CFG, Lpcd.Timer3);//配置Timer3Cfg寄存器	
    Write_Ext_Reg(JREG_LPCD_VMIDBD_CFG, Lpcd.TimerVmid);//配置VmidBdCfg寄存器
    return true;
}

//***********************************************
//函数名称：Lpcd_Set_Gain()
//函数功能：LPCD设置幅度包络信号的放大、衰减增益
//入口参数：
//出口参数：SUCCESS：操作完成  
//***********************************************
bool Lpcd_Set_Gain(uint8_t lpcd_gain)
{
	lpcd_gain &= 0x1F;
	Write_Ext_Reg(JREG_LPCD_CTRL4, lpcd_gain);
  	return true;
}

//***********************************************
//函数名称：Lpcd_Set_Threshold()
//函数功能：LPCD设置触发阈值
//入口参数：
//出口参数：SUCCESS：操作完成  
//***********************************************
uint8_t Lpcd_Set_Threshold(uint8_t lpcd_threshold_min, uint8_t lpcd_threshold_max)
{
	uint8_t temp;
       
	if(lpcd_threshold_max < lpcd_threshold_min)
	{
		temp = lpcd_threshold_min;
		lpcd_threshold_min = lpcd_threshold_max;
		lpcd_threshold_max = temp;
	}
  
  	Write_Ext_Reg(JREG_LPCD_THRESHOLD_MIN_L,(lpcd_threshold_min & 0x3F));//写入THRESHOLD_Min阈值低6位
  	Write_Ext_Reg(JREG_LPCD_THRESHOLD_MIN_H,(lpcd_threshold_min>>6));//写入THRESHOLD_Min阈值高2位
	Write_Ext_Reg(JREG_LPCD_THRESHOLD_MAX_L,(lpcd_threshold_max & 0x3F));//写入THRESHOLD_Max阈值低6位
	Write_Ext_Reg(JREG_LPCD_THRESHOLD_MAX_H,(lpcd_threshold_max>>6));//写入THRESHOLD_Max阈值高2位
        
  	return true;
}

//***********************************************
//函数名称：Lpcd_Set_Drive()
//函数功能：LPCD设置输出驱动
//入口参数：
//出口参数：SUCCESS：操作完成  
//***********************************************
uint8_t Lpcd_Set_Driver(uint8_t lpcd_cwp,uint8_t lpcd_cwn,uint8_t lpcd_tx2_en)
{
    uint8_t reg_data;
	GetReg(VersionReg, &reg_data);
    if(reg_data == 0x88)//V03版本芯片
	{
        if(lpcd_cwn > 1)
		{
            lpcd_cwn = 1;
		}
        lpcd_cwn &= 0x01;
        lpcd_cwp &= 0x07;
        Write_Ext_Reg(JREG_LPCD_CTRL2, ((lpcd_tx2_en << 4) + (lpcd_cwn << 3) + lpcd_cwp));//V03版本芯片
	}
    if(reg_data == 0x89)//V03以上版本芯片
	{
		lpcd_cwn &= 0x0F;
		lpcd_cwp &= 0x07;
		Write_Ext_Reg(JREG_LPCD_CTRL2,((lpcd_tx2_en<<4) + lpcd_cwp));//V03以上版本芯片
		SetReg(GsNOnReg, lpcd_cwn << 4); //V03以上版本芯片
	}
  	return true;
}

//***********************************************
//函数名称：Lpcd_Set_Reference()
//函数功能：LPCD设置基准信号的充电电流与充电电容
//入口参数：
//出口参数：SUCCESS：操作完成  
//***********************************************
bool Lpcd_Set_Reference(uint8_t lpcd_bias_current, uint8_t lpcd_reference)
{
	lpcd_reference &= 0x7F;
	lpcd_bias_current &= 0x07;
	Write_Ext_Reg(JREG_LPCD_BIAS_CURRENT, ((lpcd_reference & 0x40)>>1) + (lpcd_bias_current & 0x07));       
	Write_Ext_Reg(JREG_LPCD_ADC_REFERECE, (lpcd_reference & 0x3F));
	return true;
}
//***********************************************
//函数名称：Lpcd_Calibrate_Reference()
//函数功能：LPCD设置校准基准电压
//入口参数：
//出口参数：SUCCESS：校准成功；ERROR：校准失败
//***********************************************
bool Lpcd_Calibrate_Reference(void)
{
    bool result = false;      
    Lpcd_Reset_Status();//校准基准电压前，先复位状态机
    for(uint8_t i = 4;i < 0x7F; i++)
    {
		Lpcd.Calibration.Reference = 0 + i;//
		Lpcd_Set_Reference(LPCD_BIAS_CURRENT,Lpcd.Calibration.Reference);//
		
		Lpcd.Calibration.Gain_Index = 10;//
		Lpcd_Set_Gain(LPCD_GAIN[Lpcd.Calibration.Gain_Index]);//
		
		Lpcd_Set_Driver(7,15,1);//
		Lpcd_Get_Calibration_Value(&Lpcd.Calibration.Value);
		// if(Lpcd_Debug_Info)
		// {
		// 	ESP_LOGI(TAG, "-> Refference = 0x%x",i); //Uart_Send_Msg("\r\n");
		// 	ESP_LOGI(TAG, "; Lpcd.Calibration.Value = 0x%x",Lpcd.Calibration.Value);
		// }
		if((Lpcd.Calibration.Value == 0)&&(Lpcd.Calibration.Reference != 0))
		{
//			ESP_LOGI(TAG, "Calibra Reference Success!\r\n");	
			result = true;
			break;//
		}
		if((Lpcd.Calibration.Value == 0)&&(Lpcd.Calibration.Reference == 0))
		{
//			ESP_LOGI(TAG, "Calibra Reference Error!\r\n");	
			result = false; //
			break;
		}
    }
   	return result;
}

//***********************************************
//函数名称：Lpcd_Scan_Gain()
//函数功能：LPCD在各级放大、衰减增益的条件下进行校准操作，得到11个校准数据与10个校准差值
//入口参数：
//出口参数：SUCCESS：操作完成  
//***********************************************
//***********************************************
//函数名称：Lpcd_Calibrate_Driver()
//函数功能：LPCD设置校准输出驱动
//入口参数：
//出口参数：SUCCESS：操作完成  
//***********************************************
bool Lpcd_Calibrate_Driver(void)
{
    Lpcd.Calibration.Gain_Index = LPCD_GAIN_INDEX ;//      
	for(uint8_t j = 0; j < 12; j++)//
	{ 
		Lpcd_Set_Gain(LPCD_GAIN[Lpcd.Calibration.Gain_Index]);// 设置Gain
    	for(uint8_t i = 2; i < 7; i++ )//
       	{
			Lpcd.Calibration.Driver_Index = i;
			Lpcd_Set_Driver(LPCD_P_DRIVER[Lpcd.Calibration.Driver_Index],LPCD_N_DRIVER[Lpcd.Calibration.Driver_Index],LPCD_TX2RF_EN);//
			Lpcd_Get_Calibration_Value(&Lpcd.Calibration.Value);//设置Driver
			// if(Lpcd_Debug_Info)
			// {				 
			// 	ESP_LOGI(TAG, "-> Driver = 0x%x",i);
			// 	ESP_LOGI(TAG, "Lpcd.Calibration.Value = 0x%x",Lpcd.Calibration.Value);
			// } 
       		if((Lpcd.Calibration.Value > Lpcd.Calibration.Range_L)&&(Lpcd.Calibration.Value < Lpcd.Calibration.Range_H))
         	{
//           if((Lpcd.Calibration.Value - LPCD_THRESHOLD > 0 )&&((Lpcd.Calibration.Value + LPCD_THRESHOLD) < Lpcd.Fullscale_Value))//
				if((Lpcd.Calibration.Value - (Lpcd.Fullscale_Value>>2) > 0 )&&((Lpcd.Calibration.Value + (Lpcd.Fullscale_Value>>2) ) < Lpcd.Fullscale_Value))//
				{
					Lpcd.Calibration.Threshold_Max = Lpcd.Calibration.Value + LPCD_THRESHOLD;
					Lpcd.Calibration.Threshold_Min = Lpcd.Calibration.Value - LPCD_THRESHOLD;
						 
//              		ESP_LOGI(TAG, "-> Lpcd.Calibration.Value = 0x%x",Lpcd.Calibration.Value);
//					ESP_LOGI(TAG, "Lpcd.Fullscale_Value = 0x%x",Lpcd.Fullscale_Value);
//              		ESP_LOGI(TAG, "-> Lpcd.Calibration.Threshold_Max = 0x%x",Lpcd.Calibration.Threshold_Max); 
//              		ESP_LOGI(TAG, "Lpcd.Calibration.Threshold_Min = 0x%x",Lpcd.Calibration.Threshold_Min);
               		Lpcd_Set_Threshold(Lpcd.Calibration.Threshold_Min,Lpcd.Calibration.Threshold_Max);
					// if(Lpcd_Debug_Test)
					// {
					// 	while(1)//
					// 	{
					// 		Lpcd_Get_Calibration_Value(&Lpcd.Calibration.Value);//
					// 		ESP_LOGI(TAG, "-> Lpcd.Calibration.Value2 = "); ESP_LOGI(TAG, "0x%x",Lpcd.Calibration.Value);ESP_LOGI(TAG, "\r\n");
					// 	}
					// }
//              		ESP_LOGI(TAG, "Calibra Driver Success!\r\n");	        
					return true;
				}
			}	 
		}
			 
//			  if(Lpcd.Calibration.Value > Lpcd.Calibration.Range_H)//
		if(Lpcd.Calibration.Value > (Lpcd.Fullscale_Value - (Lpcd.Fullscale_Value>>1)))//
        {
          	if(Lpcd.Calibration.Gain_Index == 11)
			{
            	break;//
			}
          	else{
            	Lpcd.Calibration.Gain_Index++;
			}
//           	ESP_LOGI(TAG, "Gain_Index++\r\n");
        }
//      if(Lpcd.Calibration.Value < Lpcd.Calibration.Range_L)//
		if(Lpcd.Calibration.Value < (Lpcd.Fullscale_Value>>2))//
        {     
          	if(Lpcd.Calibration.Gain_Index == 0)
			{
            	break;//
			}
          	else{
             	Lpcd.Calibration.Gain_Index--;
			}
//           	ESP_LOGI(TAG, "Gain_Index--\r\n");
        }
				
	}		
//    ESP_LOGI(TAG, "Calibra Driver Error !\r\n");
    return false;
}

//***********************************************
//函数名称：WaitForLpcdIrq()
//函数功能：等待LPCD中断
//入口参数：IrqType
//出口参数：SUCCESS：读取操作完成   ERROR：超时退出
//***********************************************
bool Lpcd_WaitFor_Irq(uint8_t IrqType)
{
	uint8_t ExtRegData;
	uint8_t TimeOutCount;
	
	TimeOutCount = 0;
	ExtRegData = Read_Ext_Reg(JREG_LPCD_IRQ);

	for(TimeOutCount = LPCD_CALIBRA_IRQ_TIMEOUT; TimeOutCount > 0; TimeOutCount--)
	{
		Delay_Ms(1);	//延时
		ExtRegData = Read_Ext_Reg(JREG_LPCD_IRQ);	
		if(ExtRegData & IrqType)
			return true; 
	}        
	return false;
	
}

bool Lpcd_Get_Calibration_Value(uint8_t *value)
{
	bool result;
      	    
	Write_Ext_Reg(JREG_LPCD_MISC, BFL_JBIT_CALIBRATE_VMID_EN);//使能VMID电源，BFL_JBIT_AMP_EN_SEL = 1 提前使能AMP	Lpcd_Reset_Status();  //清除CalibraIRq标志
  Write_Ext_Reg(JREG_LPCD_CTRL1, JBIT_BIT_CTRL_CLR + JBIT_LPCD_CALIBRATE_EN);//关闭校准模式
	Write_Ext_Reg(JREG_LPCD_CTRL1, JBIT_BIT_CTRL_SET + JBIT_LPCD_CALIBRATE_EN);//使能校准模式
	
	result = Lpcd_WaitFor_Irq(JBIT_CALIBRATE_IRQ);//等待校准结束中断      

  Clear_BitMask(JREG_LPCD_MISC,BFL_JBIT_CALIBRATE_VMID_EN);//关闭VMID电源 
	
	Write_Ext_Reg(JREG_LPCD_CTRL1,JBIT_BIT_CTRL_CLR + JBIT_LPCD_CALIBRATE_EN);//关闭校准模式
	
	Lpcd_Get_Value(&*value);
	
  Lpcd_Reset_Status();  //清除CalibraIRq标志

	return result;
}


//***********************************************
//函数名称：Lpcd_Get_Value()
//函数功能：读取LPCD的数值
//入口参数： *value
//出口参数：SUCCESS：读取操作完成   ERROR:操作超时
//***********************************************
bool Lpcd_Get_Value(uint8_t *value)
{
	uint8_t ExtRegData;
	ExtRegData = Read_Ext_Reg(JREG_LPCD_ADC_RESULT_H);//读取幅度信息，高2位
	*value = ((ExtRegData & 0x3) << 6);

	ExtRegData = Read_Ext_Reg(JREG_LPCD_ADC_RESULT_L);//读取幅度信息，低6位

	*value += (ExtRegData & 0x3F);  

	return true;
}

//***********************************************
//函数名称：Lpcd_IRQ_Event()
//函数功能：LPCD中断处理
//入口参数：
//出口参数：
//***********************************************
void Lpcd_IRQ_Event(void)
{
	uint8_t reg;
	Lpcd_Calibration_Event();
//	if(Lpcd.Irq & JBIT_CARD_IN_IRQ)//如果有卡片中断，进入读卡操作
//        {
					 Lpcd_Set_Driver(LPCD_P_DRIVER[Lpcd.Calibration.Driver_Index],LPCD_N_DRIVER[Lpcd.Calibration.Driver_Index],LPCD_TX2RF_EN);//配置LPCD输出驱动
					 //Lpcd_Set_Reference(LPCD_BIAS_CURRENT,Lpcd.Calibration.Reference);
					 //Lpcd_Set_Gain(LPCD_GAIN[Lpcd.Calibration.Gain_Index]);
					 Lpcd_Set_IRQ_pin();	
//				}   
	Lpcd.Irq = 0;//清除LPCD中断标志
		
	return;	
}

void Lpcd_Calibration_Restore(void)
{
	Lpcd.Calibration.Reference = Lpcd.Calibration_Backup.Reference;
	Lpcd.Calibration.Gain_Index = Lpcd.Calibration_Backup.Gain_Index;
  Lpcd.Calibration.Driver_Index = Lpcd.Calibration_Backup.Driver_Index;
//	ESP_LOGI(TAG, "-> Restore Success!\r\n");		
	return;
}

void Lpcd_Calibration_Backup(void)
{
	Lpcd.Calibration_Backup.Reference = Lpcd.Calibration.Reference;
	Lpcd.Calibration_Backup.Gain_Index = Lpcd.Calibration.Gain_Index;
  Lpcd.Calibration_Backup.Driver_Index = Lpcd.Calibration.Driver_Index;
//	ESP_LOGI(TAG, "-> Bakckup Success!\r\n");		
	return;
}
//***********************************************
//函数名称：Lpcd_Calibration_Event()
//函数功能：LCPD校准程序
//入口参数：无
//出口参数：SUCCESS:校准成功；ERROR：校准失败
//***********************************************
bool Lpcd_Calibration_Event(void)
{
	bool result;
//  CD_SetTPD();                                                        /* 再拉高                       */
//  mDelay(2);
	Lpcd_Init_Register();		//

	Lpcd_Set_Timer();
	Lpcd_Set_Aux(LPCD_AUX_EN);	//??AUX????

//	ESP_LOGI(TAG, "Start Calibration!\r\n");    
	Lpcd_Reset_Status();
	result = Lpcd_Calibrate_Reference();
  if(result == false)
	{
      	return false;
	}
   
	result = Lpcd_Calibrate_Driver();
  if (result == true)		
 	{	
//		ESP_LOGI(TAG, "Calibration Success!\r\n");
        return true;
	} 
	else
	{
//		ESP_LOGI(TAG, "Calibration Error!\r\n");
		Lpcd_Calibration_Restore();//恢复校准参数
	}	 
     
	return false;
}

//***********************************************
//函数名称：Lpcd_Set_Mode()
//函数功能：LCPD工作模式设置
//入口参数：mode = 1:开启 mode = 0 :关闭
//出口参数：
//***********************************************
void Lpcd_Set_Mode(uint8_t mode)
{
  if(mode == 1)
	{
// 		SetReg(ComIEnReg, 0x80);                                    /* */
//         SetReg(DivIEnReg, 0x80);
//         SetReg(ComIrqReg,0x7f); 
//         Lpcd_Set_IE(1);//FM175XX打开LPCD中断               
//         NFC_HardPowerdown(0);//进入LPCD模式	 
//        	ESP_LOGI(TAG, "LPCD Mode Entered!\r\n");  
//         vTaskDelay(10 / portTICK_PERIOD_MS);				
// //			  NVIC_EnableIRQ(PIN_INT1_IRQn);               
		unsigned int sw = 15;
		unsigned char delta = 0x34;

		WriteRawRC(0x01,0x0F);	//
		WriteRawRC(0x14, 0x83); // Tx2CW = 1 ,continue
		WriteRawRC(0x37, 0x5e); //	
		WriteRawRC(0x3c, delta);//Delta[3:0]
		WriteRawRC(0x3d, 0x18);		//休眠时间	200MS
		// WriteRawRC(0x3d, 0x0d); //休眠时间	100MS
		WriteRawRC(0x3e, 0x80 | sw);//探测时间
		WriteRawRC(0x37, 0x00); // 关闭私有寄存器保护开关
		#if 1
		WriteRawRC(0x37, 0x5a);//打开私有寄存器保护开关
		WriteRawRC(0x38, 0xf0);//设置LPCD 发射功率
		WriteRawRC(0x39, 0x3f);//设置LPCD 发射功率
		// WriteRawRC(0x31, 0xa1);
		WriteRawRC(0x33, 0xa0);//调整步长,20,60,A0,E0
		WriteRawRC(0x36, 0x80);
		WriteRawRC(0x37, 0x00);//关闭私有寄存器保护开关
		#endif
		ClearBitMask(0x02, BIT7);//IRQ引脚反相输出
		WriteRawRC(0x03, 0xA0);	//打开卡探测中断,IRQ 为CMOS 输出
		WriteRawRC(0x01, 0x10); //PCD soft powerdown						                       
	}
  else{               
		NFC_HardPowerdown(1);//退出LPCD模式	
    Lpcd_Set_IE(0);//FM75XX关闭LPCD中断
//    ESP_LOGI(TAG, "LPCD Mode Exited!\r\n");  
	}     
  return;
}

//***********************************************
//函数名称：Lpcd_Get_IRQ()
//函数功能：LPCD读取中断标志
//入口参数：*irq IRQ标志
//出口参数:
//***********************************************
void Lpcd_Get_IRQ(uint8_t *irq)
{
	*irq = Read_Ext_Reg(JREG_LPCD_IRQ);//读取LPCD中断寄存器

//	ESP_LOGI(TAG, "Lpcd.Irq = 0x%x",*irq);
	if(Lpcd.Irq & JBIT_CARD_IN_IRQ)
	{
		Lpcd_Get_Value(&Lpcd.Value);
//		ESP_LOGI(TAG, "Lpcd.Value = 0x%x",Lpcd.Value);
	}
	Lpcd_Reset_Status();//清除FM175XX中断状态
	return;
}
//***********************************************
//函数名称：Lpcd_Set_IE()
//函数功能：LPCD中断引脚设置
//入口参数：mode = 1:开启 mode = 0 ：关闭
//出口参数：SUCCESS：操作完成
//***********************************************
bool Lpcd_Set_IE(uint8_t mode)
{
    if(mode == 1)
	{
       	Write_Ext_Reg(JREG_LPCD_CTRL1,JBIT_BIT_CTRL_SET + JBIT_LPCD_IE);//打开LPCD中断					
	}
    else{        
        Write_Ext_Reg(JREG_LPCD_CTRL1,JBIT_BIT_CTRL_CLR + JBIT_LPCD_IE);//关闭LPCD中断			
	}
    return true;
}

//***********************************************
//函数名称：Lpcd_Set_Aux()
//函数功能：LPCD测试通道功能
//入口参数：mode = 1:开启 mode = 0 ：关闭
//出口参数：SUCCESS：操作完成
//***********************************************
bool Lpcd_Set_Aux(uint8_t mode)
{
	if (mode == 1) 
	{
		Set_BitMask(0x39,BIT7);			
		Clear_BitMask(0x3C,BIT1);	
		Clear_BitMask(0x3D,(BIT2 | BIT1 | BIT0));				
		Clear_BitMask(0x3E,(BIT1 | BIT0));
		SetReg(AnalogTestReg,0x00);
		Write_Ext_Reg(0x13,BIT0);//Aux1(pin19) = V_demodulation包络检波信号输出
		Write_Ext_Reg(0x14,BIT1);//Aux2(pin20)= Reference电压信号输出
		Write_Ext_Reg(0x15,0x00);//D2输出LPCD_OUT
		Write_Ext_Reg(0x16,(BIT4 | BIT3 ));//D1(pin25) = T3 ； D2(pin26) = LPCD_OUT    
		SetReg(TestPinEnReg,0x86);//使能D1，D2 输出             
	}
	else{   
		Write_Ext_Reg(0x13,0x00);
		Write_Ext_Reg(0x14,0x00);
    Write_Ext_Reg(0x15,0x00);	
    Write_Ext_Reg(0x16,0x00);	
    SetReg(TestPinEnReg,0x80);//关闭D1；D2 输出
	}
	return true;
}

/*********************************************************************************************************
** Function name:       TyteA_Read()
** Descriptions:        ISO14443A协议测试
** input parameters:    无
** output parameters:   无
** Returned value:      无
*********************************************************************************************************/
bool TyteA_Read(uint8_t *picc_atqa, uint8_t *picc_uid)
{
    uint8_t statues = true;
    uint8_t picc_sak[3];                                                /* 储存卡片应答信息             */
    FM175X_SoftReset();                                                	/* FM175xx软件复位              */
    Set_Rf(3);                                                        	/* 打开双天线                   */
    Pcd_ConfigISOType(0);                                             	/* ISO14443寄存器初始化         */
	memset((void *)picc_uid, 0, 15);
	Delay_Ms(1);
    statues = TypeA_CardActive(picc_atqa, picc_uid, picc_sak);    			/* 激活卡片                     */
    if(statues == true) 
	{
		TypeA_Halt(0);                                          					/* 睡眠卡片                     */                       
    }
	Set_Rf(0);  
	SetReg(ComIEnReg, 0x80);                                    /* */
    SetReg(DivIEnReg, 0x80);
    SetReg(ComIrqReg, 0x7f);                                    /* */ 	
    return statues;
}

/******************************************************************************
 * EOF (not truncated)
 ******************************************************************************/