#ifndef __ANALOGCHANNELCTRL_2D5G_H
#define __ANALOGCHANNELCTRL_2D5G_H
#include "sys.h"
//////////////////////////////////////////////////////////////////////////////
// 			2203/5/X GHz_ljw �ϲ���
// 			2203/6/13 GHz_ljw 5675��д
//////////////////////////////////////////////////////////////////////////////

/////////////////////////////IIC///////////////////////////////////////////
//IO��������
#define SDA5675_IN()  {GPIOB->MODER&=~(3<<(11*2));GPIOB->MODER|=0<<11*2;}	//PB ����ģʽ
#define SDA5675_OUT() {GPIOB->MODER&=~(3<<(11*2));GPIOB->MODER|=1<<11*2;} //PB ���ģʽ
//IO��������	 
#define IIC5675_SCL    PBout(12) //SCL
#define IIC5675_SDA    PBout(11) //SDA5675	 
#define READ_SDA5675   PBin(11)  //����SDA5675 

#define EXTER_DAC_SENSIT_DEFAULT 0x7FFF
#define EXTER_DAC_POSTION_DEFAULT 0x7FFF

//DATA里面的参数偏移及大小定义
#define OFFSET_IN_DATA_FG_START 0
#define OFFSET_IN_DATA_FG_SIZE 1
#define OFFSET_IN_DATA_GAIN_START (OFFSET_IN_DATA_FG_START + OFFSET_IN_DATA_FG_SIZE)
#define OFFSET_IN_DATA_GAIN_SIZE 2
#define OFFSET_IN_DATA_ATT_START (OFFSET_IN_DATA_GAIN_START + OFFSET_IN_DATA_GAIN_SIZE)
#define OFFSET_IN_DATA_ATT_SIZE 1 
#define OFFSET_IN_DATA_COUPLING_START (OFFSET_IN_DATA_ATT_START + OFFSET_IN_DATA_ATT_SIZE)
#define OFFSET_IN_DATA_COUPLING_SIZE 1
#define OFFSET_IN_DATA_HIGH_LOW_START (OFFSET_IN_DATA_COUPLING_START + OFFSET_IN_DATA_COUPLING_SIZE)
#define OFFSET_IN_DATA_HIGH_LOW_SIZE 1
#define OFFSET_IN_DATA_BIAS_START (OFFSET_IN_DATA_HIGH_LOW_START + OFFSET_IN_DATA_HIGH_LOW_SIZE)
#define OFFSET_IN_DATA_BIAS_SIZE 2
#define OFFSET_IN_DATA_MOVE_START (OFFSET_IN_DATA_BIAS_START + OFFSET_IN_DATA_BIAS_SIZE)
#define OFFSET_IN_DATA_MOVE_SIZE 2
#define OFFSET_IN_DATA_BANDWIDTH_START (OFFSET_IN_DATA_MOVE_START + OFFSET_IN_DATA_MOVE_SIZE)
#define OFFSET_IN_DATA_BANDWIDTH_SIZE 2

#define PACKAGE_CHANNELSET_DATA_BYTES (OFFSET_IN_DATA_BANDWIDTH_START + OFFSET_IN_DATA_BANDWIDTH_SIZE)
#define PACKAGE_CHANNELSET_OFFSET_DATA_BYTES 4
#define PACKAGE_CHANNELSET_INDEX_BYTES 2

#define PACKAGE_CHANNELSET_CRC32_BYTES 4
#define PACKAGE_CHANNELSET_FRAMEID_BYTES 2
#define PACKAGE_CHANNELSET_CHECK_AREA_BYTES 6
#define PACKAGE_CHANNELSET_MAX_BYTES		(PACKAGE_CHANNELSET_INDEX_BYTES+MAX_CHANNEL_NUM*PACKAGE_CHANNELSET_DATA_BYTES+PACKAGE_CHANNELSET_CHECK_AREA_BYTES+PACKAGE_CHANNELSET_FRAMEID_BYTES) 
#define MAX_CHANNEL_NUM 4
//IIC���в�������
void IIC5675_Init(void);                //��ʼ��IIC��IO��				 
void IIC5675_Start(void);				//����IIC��ʼ�ź�
void IIC5675_Stop(void);	  			//����IICֹͣ�ź�
void IIC5675_Send_Byte(u8 txd);			//IIC����һ���ֽ�
u8 IIC5675_Read_Byte(unsigned char ack);//IIC��ȡһ���ֽ�
u8 IIC5675_Wait_Ack(void); 				//IIC�ȴ�ACK�ź�
void IIC5675_Ack(void);					//IIC����ACK�ź�
void IIC5675_NAck(void);				//IIC������ACK�ź�

void IIC5675_Write_One_Byte(u8 daddr,u8 addr,u8 data);
u8 IIC5675_Read_One_Byte(u8 daddr,u8 addr);	 
//////////////////////////////////////////////////////////////////////////////

#define CH1_InPut 		GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_11) //PC11
#define CH2_InPut 		GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_8)	//PD8
#define CH3_InPut 		GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_3) //PA3
#define CH4_InPut 		GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_5) //PE5

enum  DSAx
{
	CH1_DSA_CS=1,
	CH2_DSA_CS,
	CH3_DSA_CS,
	CH4_DSA_CS
};

enum DACx
{
	DAC1=1,
	DAC2,
	DAC3
};

typedef struct GPIOPin {
	GPIO_TypeDef * gpioType;
	uint16_t pinIndex;
}GPIOPin;

#ifndef RUNNING_WHERE_AT_BOOT	
extern void DAC_AD5675(unsigned long data,unsigned int DACx_CS);
extern void DAC_AD5628(unsigned long data,unsigned int DACx_CS);
extern void DAC_AD5668(unsigned long data,unsigned int DACx_CS);
extern void DSA_PE43711(u8 addrDev,u8 data,u8 CHx_DSA_CSn);
extern void DSA2_PE43711(u8 addrDev,u8 data);
extern void CD_4094(unsigned long data,unsigned char data_bit);
#endif
 
extern void ExterDACSensit(u16 dac);
extern void ExterDACPostion(u16 dac);

extern unsigned char TRIG_ShiftReg_CMD_temp;
extern unsigned long CH_ShiftReg_CMD_temp;
extern unsigned char CH1_PROTECT_EN;
extern unsigned char CH2_PROTECT_EN;
extern unsigned char CH3_PROTECT_EN;
extern unsigned char CH4_PROTECT_EN;

extern u32 Low32HC595;
extern u32 High8HC595;
extern u32 Low32HC595_new;
extern u32 High8HC595_new;
extern u8 IsChannelSetting;

extern u32 DAC1_CMD_offset;
extern u32 DAC2_CMD_offset;
extern u32 DAC3_CMD_offset;
//extern u16 DAC_CMDs[3][12];
//extern u32 DAC1_CMD_bias;
//extern u32 DAC2_CMD_bias;
//extern u32 DAC3_CMD_bias;
void AnalogChProtect_GPIO_Init(void);
void AnalogChCtrl_GPIO_Init(void);

#endif
