#ifndef XTDRIVER_H
#define XTDRIVER_H

#include "base/define.h"
#include "hardware/gpio_cfg.h"
#include "hardware/adc_cfg.h"
#include "base/delay.h"
#include "hardware/power.h"
#include <rtthread.h>



#define XTBUS_IDLE						XTBUS_W_1			
#define XTBUS_RWIDLE					XTBUS_WR_1

#define XT_Read_AD_Sample_C  120

#define XT_ADC_SAMPLE_TIME 		589u //采样 XT_Read_AD_Sample_C 所需时间



#define XTREGISTERVOLTAGE							80
#define XTCOMMHIGHVOLTAGE							100						//10V 1200米干线可以，1400米干线电压不够
#define XTCHARGEINSTRUCTIONVOLTAGE					160	
#define XTCOMMMEDIUMVOLTAGE							80
#define XTCHARGEVOLTAGE								260						//充电20V 1200米电压不够， 充电18V 1200米分档充能充满
																				
#define XTBIT0 										0
#define XTBIT1 										1

#define XTFIRSTDELAY								138
#define XTFIRSTCYCLE								108
#define XTSECONDDELAY								128
#define	XTSECONDCYCLE								118
#define XTTHRIDDELAY								123
#define XTTHRIDCYCLE								118
#define XTOTHERDELAY								123
#define	XTOTHERCYCLE								113


#define XT_DAT_PLUS_T               100
#define XT_BIT_DELAY								180

#define XT_TRIM_DELAY								293
#define XTTRIMCYCLE									693

//#define XTFIRSTDELAY								140			//1200米低温可能芯片识别不出来指令
//#define XTFIRSTCYCLE								140
//#define XTSECONDDELAY								110
//#define	XTSECONDCYCLE								110
//#define XTTHRIDDELAY								110
//#define XTTHRIDCYCLE								110
//#define XTOTHERDELAY								110
//#define	XTOTHERCYCLE								110
//#define XTBITDELAY									180
//#define XTTRIMDELAY									293
//#define XTTRIMCYCLE									693


#define XTCmdAndSquareUs							700		//指令与方波的时间间隔
#define XTTimOverflowUs								100			//TIM定时器溢出时间
#define XTSquareLowTimNum							12			//方波低电平TIM溢出数       这个时间让电容充电 100us后才会开始充电		//原值7 越长反馈幅度越大，最大15
#define XTSquareCollectTimNum						1			//方波高电平内采样TIM溢出数
#define XTSquareHighTimNum							7			//方波高电平TIM溢出数		确保比芯片反馈完多100us，最小7，最大15，短点好

#define XTCollectResistance							2			//2 - R10_4ma_54ma_MC    3-R1_33ma_540ma_MC
#define XTCollectADCChannel							AN_MAL_CH	//AN_UA_CH     AN_MAL_CH

#define XTCollectIncrementValue						150			//最小200mV		低温下扫描间隔长一点70ms，300ms一个，短了电压会不够 抢反馈
#define XTCollectSumValue							1500			
#define XTSingleCollectAckValue						800			//单发检测
#define XTConfirmAckCount							10

#define XTCollectTimerPrescaler						72			//1us
#define XTCollectTimerOverflowUs					600			//600us        //450us		//要将反馈全部包进去，反馈开始时间最晚在边沿后350us
#define XTSingleCollectTimerPrescaler				7200		//0.1ms
#define XTSingleCollectTimerOverflowMs				500			//50ms
#define XTSingleCollectAckTimerOverflowCnt			20000			//2s //单发充电完成
#define XTWriteBitTimerOverflowCnt					500			//50ms 指令应答超时

#define XT_ADC_BUF_LEN               140

//************************************************************************//
#define XT_CMDA_WRITE_FIELD					0xEC		//写现场值
#define XT_CMDA_READ_FIELD					0xEF		//读现场值
#define XT_CMDA_READ_SHELL					0xF4		//读管壳码
#define XT_CMDA_READ_PARAM					0xF1		//读测量参数值
#define XT_CMDA_MEASURE_RES					0xF8		//测量药头电阻
#define XT_CMDA_MEASURE_CAP					0xDB		//测量储能电容
#define XT_CMDA_CHARGE						  0xDD		//单发充电

#define XT_CMDB_CLEAR_READH					0x1F		//清除已读状态高8位
#define XT_CMDB_CLEAR_READL					0x2A		//清除已读状态低8位
#define XT_CMDB_FIREH						    0x3C		//起爆高8位
#define XT_CMDB_FIREL						    0xC8		//起爆低8位
#define XT_CMDB_INITH						    0x1A		//初始化寄存器高8位
#define XT_CMDB_INITL						    0x61		//初始化寄存器低8位
#define XT_CMDB_RESETH						  0x19		//软件复位高8位
#define XT_CMDB_RESETL						  0x66		//软件复位低8位
#define XT_CMDB_READ_UIDH					  0x33		//扫描高8位
#define XT_CMDB_READ_UIDL					  0x34		//扫描低8位
#define XT_CMDB_TRIM_FASTH					0x2E		//快速延期标定高8位
#define XT_CMDB_TRIM_FASTL					0x2D		//快速延期标定低8位
#define XT_CMDB_TRIM_COMPLETEH			0x2D		//完整延期标定高8位
#define XT_CMDB_TRIM_COMPLETEL			0x33		//完整延期标定低8位
#define XT_CMDB_CHECK_ONLINEH				0x2B		//在线检测高8位
#define XT_CMDB_CHECK_ONLINEL				0x4B		//在线检测低8位
#define XT_CMDB_CHECK_BMIDH					0x28		//验证起爆密码高8位
#define XT_CMDB_CHECK_BMIDL					0x7F		//验证起爆密码低8位
#define XT_CMDB_CHECK_FIRE_ALLH			0x27		//获取全部验证状态高8位
#define XT_CMDB_CHECK_FIRE_ALLL			0x52		//获取全部验证状态低8位
#define XT_CMDB_CHECK_FIRE_EACHH		0x24		//获取逐发验证状态高8位
#define XT_CMDB_CHECK_FIRE_EACHL		0x55		//获取逐发验证状态低8位
#define XT_CMDB_SET_CURRENTH				0x42		//设置反馈电流大小
#define XT_CMDB_CHARGE						  0x41		//广播充电
#define XT_CMDB_CHECK_CONFIG				0x70		//检查配置区

#define XT_CMDC_WRITE_UID					  0x1E		//写UID
#define XT_CMDC_WRITE_BMID					0x1D		//写起爆密码
#define XT_CMDC_WRITE_SHELL					0x1B		//写管壳码
#define XT_CMDC_WRITE_FIELD					0x18		//写现场值_C
#define XT_CMDC_WRITE_CONFIG				0x14		//写配置区
#define XT_CMDC_WRITE_PARAM					0x12		//写测量参数值
#define XT_CMDC_MEASURE_CAP					0x11		//测量储能电容_C
#define XT_CMDC_LOCK						    0x3E		//锁定
#define XT_CMDC_READ_PARAM					0x37		//读测量参数值_C
#define XT_CMDC_READ_CONFIG					0x34		//读配置区
#define XT_CMDC_READ_SHELL					0x2C		//读管壳码_C
#define XT_CMDC_MEASURE_RES					0x2F		//测量药头电阻_C


extern volatile uint16_t XT_FreeBackMaxCur,XT_FreeBackMinCur,XT_FreeBackMaxTime,XT_FreeBackMinTime,XT_CommEndMaxCur;
extern volatile uint8_t XT_EnFreeBack_Test,XT_CommEnd_CurEn;

//************************************************************************//
/*
@brief 通信反码信息初始化
*/
void XT_FreeBack_Prapare(uint8_t enable_flag);
void XT_SendSquare(uint32_t num);
uint8_t XT_Power_Init(void);
void XT_BUS_CTRL( uint8_t NewState );
uint8_t XT_Write_Field(uint8_t *uid, uint8_t uidlen, uint8_t *field);
uint8_t XT_Read_Field(uint8_t *uid, uint8_t uidlen, uint8_t *field);
uint8_t XT_Read_Shell(uint8_t *uid, uint8_t uidlen, uint8_t *shell);
uint8_t XT_Read_Param(uint8_t *uid, uint8_t uidlen, uint8_t *param);
uint8_t XT_Measure_Res(uint8_t *uid, uint8_t uidlen, uint8_t *adcvalue);
uint8_t XT_Measure_Cap(uint8_t *uid, uint8_t uidlen, uint8_t level, uint32_t *chargetime);

uint8_t XT_Charge_One(uint8_t *uid,uint8_t uidlen, uint8_t level,uint16_t busvoltage, uint8_t isgetack);
void XT_Clear_Read(void);
void XT_Fire(void);
uint8_t XT_FireandCheck(uint8_t *delay);
uint8_t XT_MeasureFire(uint32_t *delay)	;
void XT_Init(uint8_t regdata);
void XT_Reset(void);
uint8_t XT_Read_Uid(uint8_t *uid, uint8_t uidlen);
void XT_Trim_Fast(void);
void XT_Trim_Faset_Cycle(uint16_t cycle,uint16_t duty);
void XT_Trim_Complete(uint32_t delay_max);
void XT_Trim_Complete_Cycle(uint16_t cycle,uint16_t duty, uint32_t delay_max);
uint8_t XT_Check_Online(uint16_t index_max, uint8_t *online);
uint8_t XT_Check_Bmid(uint8_t *bmid, uint8_t bmidlen);
uint8_t XT_Check_Fire_All(void);
uint8_t XT_Check_Fire_Each(uint32_t index_max, uint8_t *check);
void XT_Set_Current(uint8_t level);
void XT_Charge_All(uint8_t level);
uint8_t XT_Check_Config(uint8_t *config);
uint8_t XT_Write_Uid(uint8_t *uid, uint8_t uidlen);
uint8_t XT_Write_Bmid(uint8_t *bmid, uint8_t bmidlen);
uint8_t XT_Write_Shell(uint8_t *shell);
uint8_t XT_Write_Field_All(uint8_t *field);
uint8_t XT_Write_Config(uint8_t *config);
uint8_t XT_Write_Param(uint8_t *param);
uint8_t XT_Measure_Cap_All(uint8_t level, uint32_t *chargetime);
void XT_Lock(void);
uint8_t XT_Read_Param_All(uint8_t *param);
uint8_t XT_Read_Config(uint8_t *config);
uint8_t XT_Read_Shell_All(uint8_t *shell);
uint8_t XT_Measure_Res_All(uint8_t *adcvalue);
uint8_t XT_Get_UID_R(uint8_t *uid, uint8_t uidlen);
uint8_t XT_Get_R(void);

uint8_t XT_Get_UID_C(uint8_t *uid, uint8_t uidlen, uint8_t level, uint32_t time_out,float *CapacitanceValue) ;
uint8_t XT_Get_C(uint8_t level, uint32_t time_out, float *CapacitanceValue);

#endif
