#include "AnalogChannelCtrl_2d5g_base.h"
#include "delay.h"

#include "CommandsDefine.h"
#include "ProjectConfig.h"
#include "usart.h"
u8 IsChannelSetting;
u32 Low32HC595;
u32 High8HC595;
//u32 DAC1_CMD_offset;
//u32 DAC2_CMD_offset;
//u32 DAC3_CMD_offset;
//u16 DAC_CMDs[3][12];
//u32 DAC1_CMD_bias;
//u32 DAC2_CMD_bias;
//u32 DAC3_CMD_bias;
//u32 Low32HC595_new;
//u32 High8HC595_new;

//��ʼ��IIC5675
void IIC5675_Init(void)
{
    GPIO_InitTypeDef  GPIO_InitStructure;

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);//ʹ��GPIOBʱ��

    //GPIOB8,B9��ʼ������
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12 ;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//��ͨ���ģʽ
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//�������
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//����
    GPIO_Init(GPIOB, &GPIO_InitStructure);//��ʼ��
		 
    IIC5675_SCL=1;
    IIC5675_SDA=1;
	 
		GPIO_SetBits(GPIOD,GPIO_Pin_0); 

		GPIO_SetBits(GPIOB,GPIO_Pin_13); 

		GPIO_SetBits(GPIOA,GPIO_Pin_2); 
    
}
//����IIC5675��ʼ�ź�
void IIC5675_Start(void)
{
    SDA5675_OUT();     //SDA5675�����
    IIC5675_SDA=1;
    IIC5675_SCL=1;
    delay_us(4);
    IIC5675_SDA=0;//START:when CLK is high,DATA change form high to low
    delay_us(4);
    IIC5675_SCL=0;//ǯסI2C���ߣ�׼�����ͻ��������
}
//����IIC5675ֹͣ�ź�
void IIC5675_Stop(void)
{
    SDA5675_OUT();//SDA5675�����
    IIC5675_SCL=0;
    IIC5675_SDA=0;//STOP:when CLK is high DATA change form low to high
    delay_us(4);
    IIC5675_SCL=1;
    IIC5675_SDA=1;//����I2C���߽����ź�
    delay_us(4);
}
//�ȴ�Ӧ���źŵ���
//����ֵ��1������Ӧ��ʧ��
//        0������Ӧ��ɹ�
u8 IIC5675_Wait_Ack(void)
{
    u8 ucErrTime=0;
    SDA5675_IN();      //SDA5675����Ϊ����
    IIC5675_SDA=1;
    delay_us(1);
    IIC5675_SCL=1;
    delay_us(1);
    while(READ_SDA5675)
    {
        ucErrTime++;
        if(ucErrTime>250)
        {
            IIC5675_Stop();
            return 1;
        }
    }
    IIC5675_SCL=0;//ʱ�����0
    return 0;
}
//����ACKӦ��
void IIC5675_Ack(void)
{
    IIC5675_SCL=0;
    SDA5675_OUT();
    IIC5675_SDA=0;
    delay_us(2);
    IIC5675_SCL=1;
    delay_us(2);
    IIC5675_SCL=0;
}
//������ACKӦ��
void IIC5675_NAck(void)
{
    IIC5675_SCL=0;
    SDA5675_OUT();
    IIC5675_SDA=1;
    delay_us(2);
    IIC5675_SCL=1;
    delay_us(2);
    IIC5675_SCL=0;
}
//IIC5675����һ���ֽ�

void IIC5675_Send_Byte(u8 txd)
{
    u8 t;
    SDA5675_OUT();
    IIC5675_SCL=0;//����ʱ�ӿ�ʼ���ݴ���
    for(t=0; t<8; t++)
    {
        IIC5675_SDA=(txd&0x80)>>7;
        txd<<=1;
        delay_us(2);   //��TEA5767��������ʱ���Ǳ����
        IIC5675_SCL=1;
        delay_us(2);
        IIC5675_SCL=0;
        delay_us(2);
    }
}
//��1���ֽڣ�ack=1ʱ������ACK��ack=0������nACK
u8 IIC5675_Read_Byte(unsigned char ack)
{
    unsigned char i,receive=0;
    SDA5675_IN();//SDA5675����Ϊ����
    for(i=0; i<8; i++ )
    {
        IIC5675_SCL=0;
        delay_us(2);
        IIC5675_SCL=1;
        receive<<=1;
        if(READ_SDA5675)receive++;
        delay_us(1);
    }
    if (!ack)
        IIC5675_NAck();//����nACK
    else
        IIC5675_Ack(); //����ACK
    return receive;
}
 

/*DSA_PE43711
* PB0:  CH1_DSA_CS
* PB1:  CH2_DSA_CS
* PB2:  CH3_DSA_CS
* PB3:  CH4_DSA_CS
* PB4:  DATA
* PB5:  CLK
*/
// ����˥������������
void DSA_PE43711(u8 addrDev,u8 data,u8 CHx_DSA_CSn)
{
    u8 i=0;
    unsigned char addr1 = addrDev;

    GPIOPin csGpioPin;  //SPIƬѡ�ܽ�

    GPIOPin dataGpioPin;  //SPI���ݹܽ�
    dataGpioPin.gpioType = GPIOD;
    dataGpioPin.pinIndex = GPIO_Pin_11;

    GPIOPin clkGpioPin;  //SPIʱ�ӹܽ�
    clkGpioPin.gpioType = GPIOD;
    clkGpioPin.pinIndex = GPIO_Pin_13;

    //Ƭѡ��ʼ,����
    switch(CHx_DSA_CSn)
    {
        case CH1_DSA_CS :
        {
            csGpioPin.gpioType = GPIOD;
            csGpioPin.pinIndex = GPIO_Pin_12;
            break;
        }
        case CH2_DSA_CS :
        {
            csGpioPin.gpioType = GPIOE;
            csGpioPin.pinIndex = GPIO_Pin_12;
			break;
        }
        case CH3_DSA_CS  :
        {
            csGpioPin.gpioType = GPIOA;
            csGpioPin.pinIndex = GPIO_Pin_1;
			break;
        }
        case CH4_DSA_CS  :
        {
            csGpioPin.gpioType = GPIOE;
            csGpioPin.pinIndex = GPIO_Pin_4;
			break;
        }
        default:
                break;
    }
    GPIO_ResetBits(csGpioPin.gpioType, csGpioPin.pinIndex);//��Ҫ�����ӳ�
    delay_us(2);
		
    //����6401����43711��ʽ����
    if(addr1 != 0)
    {
        for( i = 0; i < 8; i++ )
        {
            u8 j = (addr1 & 0x80);

            if(j == 0x80)
            {
                GPIO_SetBits(dataGpioPin.gpioType, dataGpioPin.pinIndex);//��Ҫ�����ӳ�
            }
            else
            {
                GPIO_ResetBits(dataGpioPin.gpioType, dataGpioPin.pinIndex);//��Ҫ�����ӳ�
            }
            delay_us(2);

            GPIO_SetBits(clkGpioPin.gpioType, clkGpioPin.pinIndex);//��Ҫ�����ӳ�
            delay_us(2);
            GPIO_ResetBits(clkGpioPin.gpioType, clkGpioPin.pinIndex);//��Ҫ�����ӳ�
            delay_us(2);

            addr1 =(addr1<<1);
        }
            
        for( i = 0; i < 8; i++ )
        {
            u8 j=( data & 0x80);

            if(j == 0x80)
            {
                GPIO_SetBits(dataGpioPin.gpioType, dataGpioPin.pinIndex);//��Ҫ�����ӳ�
            }
            else
            {
                GPIO_ResetBits(dataGpioPin.gpioType, dataGpioPin.pinIndex);//��Ҫ�����ӳ�
            }
            delay_us(2);

            GPIO_SetBits(clkGpioPin.gpioType, clkGpioPin.pinIndex);//��Ҫ�����ӳ�
            delay_us(2);
            GPIO_ResetBits(clkGpioPin.gpioType, clkGpioPin.pinIndex);//��Ҫ�����ӳ�
            delay_us(2);

            data=(data<<1);
        }
    }
    else
    {
        for( i=0; i<8; i++ )
        {
            u8	j=(data&0x01);

            if(j == 0x01)
            {
                GPIO_SetBits(dataGpioPin.gpioType, dataGpioPin.pinIndex);//��Ҫ�����ӳ�
            }
            else
            {
                GPIO_ResetBits(dataGpioPin.gpioType, dataGpioPin.pinIndex);//��Ҫ�����ӳ�
            }
            delay_us(2);

            GPIO_SetBits(clkGpioPin.gpioType, clkGpioPin.pinIndex);//��Ҫ�����ӳ�
            delay_us(2);
            GPIO_ResetBits(clkGpioPin.gpioType, clkGpioPin.pinIndex);//��Ҫ�����ӳ�
            delay_us(2);

            data=(data>>1);
        }
    }

    //Ƭѡ���������ߵ�
    GPIO_SetBits(csGpioPin.gpioType, csGpioPin.pinIndex);//��Ҫ�����ӳ�
    delay_us(2);
    GPIO_ResetBits(csGpioPin.gpioType, csGpioPin.pinIndex);//��Ҫ�����ӳ�
    delay_us(2);
}

/// @brief ͨ��595��ʹ�ܵ�DSA����
/// @param addrDev 
/// @param data 
void DSA2_PE43711(u8 addrDev,u8 data)
{
    u8 i=0;
    unsigned char addr1 = addrDev;

    GPIOPin dataGpioPin;  //SPI���ݹܽ�
    dataGpioPin.gpioType = GPIOD;
    dataGpioPin.pinIndex = GPIO_Pin_11;

    GPIOPin clkGpioPin;  //SPIʱ�ӹܽ�
    clkGpioPin.gpioType = GPIOD;
    clkGpioPin.pinIndex = GPIO_Pin_13;

    //����6401����43711��ʽ����
    if(addr1 != 0)
    {
        for( i = 0; i < 8; i++ )
        {
            u8 j = (addr1 & 0x80);

            if(j == 0x80)
            {
                GPIO_SetBits(dataGpioPin.gpioType, dataGpioPin.pinIndex);//��Ҫ�����ӳ�
            }
            else
            {
                GPIO_ResetBits(dataGpioPin.gpioType, dataGpioPin.pinIndex);//��Ҫ�����ӳ�
            }
            delay_us(2);

            GPIO_SetBits(clkGpioPin.gpioType, clkGpioPin.pinIndex);//��Ҫ�����ӳ�
            delay_us(2);
            GPIO_ResetBits(clkGpioPin.gpioType, clkGpioPin.pinIndex);//��Ҫ�����ӳ�
            delay_us(2);

            addr1 =(addr1<<1);
        }
            
        for( i = 0; i < 8; i++ )
        {
            u8 j=( data & 0x80);

            if(j == 0x80)
            {
                GPIO_SetBits(dataGpioPin.gpioType, dataGpioPin.pinIndex);//��Ҫ�����ӳ�
            }
            else
            {
                GPIO_ResetBits(dataGpioPin.gpioType, dataGpioPin.pinIndex);//��Ҫ�����ӳ�
            }
            delay_us(2);

            GPIO_SetBits(clkGpioPin.gpioType, clkGpioPin.pinIndex);//��Ҫ�����ӳ�
            delay_us(2);
            GPIO_ResetBits(clkGpioPin.gpioType, clkGpioPin.pinIndex);//��Ҫ�����ӳ�
            delay_us(2);

            data=(data<<1);
        }
    }
    else
    {
        for( i=0; i<8; i++ )
        {
            u8	j=(data&0x01);

            if(j == 0x1)
            {
                GPIO_SetBits(dataGpioPin.gpioType, dataGpioPin.pinIndex);//��Ҫ�����ӳ�
            }
            else
            {
                GPIO_ResetBits(dataGpioPin.gpioType, dataGpioPin.pinIndex);//��Ҫ�����ӳ�
            }
            delay_us(2);

            GPIO_SetBits(clkGpioPin.gpioType, clkGpioPin.pinIndex);//��Ҫ�����ӳ�
            delay_us(2);
            GPIO_ResetBits(clkGpioPin.gpioType, clkGpioPin.pinIndex);//��Ҫ�����ӳ�
            delay_us(2);

            data=(data>>1);
        }
    }
}

// ����DAC ƫ�õ���,I2C Send
void DAC_AD5675(unsigned long data,unsigned int DACx_CS)
{
    u8 device_addr = 0;
    u8 port = (data>>16)&0xF;
    u8 cmd = (3<<4)|port;
    u16 sendData = data & 0xFFFF;
    GPIOPin ldacGpioPin;  //Ƭѡ�ܽ�

    switch(DACx_CS)
    {
        case DAC1:
        {			
            device_addr = 12<<1; //0b1100
            ldacGpioPin.gpioType = GPIOD;			 
            ldacGpioPin.pinIndex = GPIO_Pin_0;			 
            break;
        }
        case DAC2:
        {		
            device_addr = 13<<1; //0b1101
            ldacGpioPin.gpioType = GPIOB;			 
            ldacGpioPin.pinIndex = GPIO_Pin_13;		 
            break;
        }
        case DAC3:
        {				 
            device_addr = 14<<1; //0b1110
            ldacGpioPin.gpioType = GPIOA;			 
            ldacGpioPin.pinIndex = GPIO_Pin_2;	
            break; 
        }
        default:
            return;
    }
    IIC5675_Start();
    IIC5675_Send_Byte(device_addr);
    
    if (IIC5675_Wait_Ack() != 0)
    {
        goto cmd_fail;	
    }
    IIC5675_Send_Byte(cmd);
    if (IIC5675_Wait_Ack() != 0)
    {
        goto cmd_fail;	
    }
		
    IIC5675_Send_Byte((sendData>>8)&0xff); 
    if (IIC5675_Wait_Ack() != 0)
    {
        goto cmd_fail;	
    }
	
    IIC5675_Send_Byte(sendData&0xff);
    if (IIC5675_Wait_Ack() != 0)
    {
        goto cmd_fail;	
    }
    IIC5675_Stop();//����һ��ֹͣ����
	
    //����Ƭѡ
    GPIO_ResetBits(ldacGpioPin.gpioType,ldacGpioPin.pinIndex); 
    delay_us(5);
    GPIO_SetBits(ldacGpioPin.gpioType,ldacGpioPin.pinIndex); 
    delay_ms(1);
    return;

    cmd_fail:
        IIC5675_Stop();//����һ��ֹͣ����
}

// ����DAC ƫ�õ���
void DAC_AD5628(unsigned long data,unsigned int DACx_CS)
{
    u8 i=0;
    u8 j=0;
    unsigned long DAC_data_bit_status=0;

    GPIOPin csGpioPin;  //SPIƬѡ�ܽ�

    GPIOPin dataGpioPin;  //SPI���ݹܽ�
    dataGpioPin.gpioType = GPIOB;
    dataGpioPin.pinIndex = GPIO_Pin_11;
    
    GPIOPin clkGpioPin;  //SPIʱ�ӹܽ�
    clkGpioPin.gpioType = GPIOB;
    clkGpioPin.pinIndex = GPIO_Pin_12;

    //Ƭѡ��ʼ�����͸ߵ�
    switch(DACx_CS)
    {
        case DAC1:
        {
            csGpioPin.gpioType = GPIOD;
            csGpioPin.pinIndex = GPIO_Pin_0;
            break;
        }
        case DAC2:
        {
            csGpioPin.gpioType = GPIOB;
            csGpioPin.pinIndex = GPIO_Pin_13;
        }
        case DAC3:
        {
            csGpioPin.gpioType = GPIOA;
            csGpioPin.pinIndex = GPIO_Pin_2;
        }
        default:
                break;
    }
    GPIO_ResetBits(csGpioPin.gpioType, csGpioPin.pinIndex);//��Ҫ�����ӳ�
    delay_us(1);
    GPIO_SetBits(csGpioPin.gpioType, csGpioPin.pinIndex);//��Ҫ�����ӳ�
    delay_us(1);
    GPIO_ResetBits(csGpioPin.gpioType, csGpioPin.pinIndex);//��Ҫ�����ӳ�
    delay_us(1);

    //����SPI���ݺ�ʱ��
    GPIO_SetBits(dataGpioPin.gpioType, dataGpioPin.pinIndex);
    GPIO_SetBits(clkGpioPin.gpioType, clkGpioPin.pinIndex);
    delay_us(1);
    GPIO_ResetBits(dataGpioPin.gpioType, dataGpioPin.pinIndex);
    delay_us(1);
    GPIO_ResetBits(clkGpioPin.gpioType, clkGpioPin.pinIndex);
    delay_us(1);

    for	 (i=0; i<4; i++)
    {
        for( j=0; j<8; j++ )
        {
            DAC_data_bit_status = (data & 0x80000000);
            if(DAC_data_bit_status)
            {
                GPIO_SetBits(dataGpioPin.gpioType, dataGpioPin.pinIndex);
            }
            else
            {
                GPIO_ResetBits(dataGpioPin.gpioType, dataGpioPin.pinIndex);
            }
            delay_us(1);

            GPIO_SetBits(clkGpioPin.gpioType, clkGpioPin.pinIndex);
            delay_us(1);
            GPIO_ResetBits(clkGpioPin.gpioType, clkGpioPin.pinIndex);
            delay_us(2);
            data = (data<<1);
        }

        GPIO_ResetBits(dataGpioPin.gpioType, dataGpioPin.pinIndex);
        delay_us(1);
        GPIO_SetBits(clkGpioPin.gpioType, clkGpioPin.pinIndex);
        delay_us(1);
        GPIO_ResetBits(clkGpioPin.gpioType, clkGpioPin.pinIndex);
        delay_us(2);


    }
    GPIO_SetBits(clkGpioPin.gpioType, clkGpioPin.pinIndex);
    delay_us(1);
    GPIO_SetBits(dataGpioPin.gpioType, dataGpioPin.pinIndex);
    delay_us(1);
 
    //Ƭѡ����������
    GPIO_SetBits(csGpioPin.gpioType, csGpioPin.pinIndex); 
    delay_us(1);
}

// λ�ƼĴ����� ���Ƽ̵�����ģ�⿪�ء���������
void CD_4094(unsigned long data,unsigned char data_bit)
{
    unsigned char i=0;
    unsigned long ShiftReg_data_bit_status=0;

    GPIOPin csGpioPin;  //SPIƬѡ�ܽ�
    csGpioPin.gpioType = GPIOB;
    csGpioPin.pinIndex = GPIO_Pin_15;
    
    GPIOPin dataGpioPin;  //SPI���ݹܽ�
    dataGpioPin.gpioType = GPIOC;
    dataGpioPin.pinIndex = GPIO_Pin_12;
    
    GPIOPin clkGpioPin;  //SPIʱ�ӹܽ�
    clkGpioPin.gpioType = GPIOB;
    clkGpioPin.pinIndex = GPIO_Pin_14;

    if(data_bit == 1)
    {
        for( i=0; i<8; i++ )
        {
            ShiftReg_data_bit_status=((unsigned char)data & 0x80);
            if(ShiftReg_data_bit_status)
            {
                GPIO_SetBits(dataGpioPin.gpioType,dataGpioPin.pinIndex);//��Ҫ�����ӳ�
                delay_us(1);
            }
            else
            {
                GPIO_ResetBits(dataGpioPin.gpioType,dataGpioPin.pinIndex);//��Ҫ�����ӳ�
                delay_us(1);
            }
            GPIO_SetBits(clkGpioPin.gpioType,clkGpioPin.pinIndex);//��Ҫ�����ӳ�
            delay_us(1);
            GPIO_ResetBits(clkGpioPin.gpioType,clkGpioPin.pinIndex);//��Ҫ�����ӳ�
            delay_us(1);
            data=(data<<1);
        }
    }
    else if (data_bit==4)
    {
        for( i=0; i<32; i++ )
        {
            ShiftReg_data_bit_status=(data&0x80000000);
            if(ShiftReg_data_bit_status)//data_bit32=1
            {
                GPIO_SetBits(dataGpioPin.gpioType,dataGpioPin.pinIndex);//��Ҫ�����ӳ�
                delay_us(1);
            }
            else
            {
                GPIO_ResetBits(dataGpioPin.gpioType,dataGpioPin.pinIndex);//��Ҫ�����ӳ�
                delay_us(1);
            }
            GPIO_SetBits(clkGpioPin.gpioType,clkGpioPin.pinIndex);//��Ҫ�����ӳ�
            delay_us(1);
            GPIO_ResetBits(clkGpioPin.gpioType,clkGpioPin.pinIndex);//��Ҫ�����ӳ�
            delay_us(1);
            data=(data<<1);
        }
        GPIO_SetBits(csGpioPin.gpioType,csGpioPin.pinIndex);//��Ҫ�����ӳ�
        delay_us(1);
        GPIO_ResetBits(csGpioPin.gpioType,csGpioPin.pinIndex);//��Ҫ�����ӳ�
        delay_us(1);
    }
 
}
