#include "AnalogChannelCtrl_8g.h"
#include "delay.h"
#include <stdio.h>

//DAC输出Port的定义
#define	DAC_VOUT0 0 
#define	DAC_VOUT1 1 
#define	DAC_VOUT2 2 
#define	DAC_VOUT3 3 
#define	DAC_VOUT4 4 
#define	DAC_VOUT5 5 
#define	DAC_VOUT6 6 
#define	DAC_VOUT7 7 

u8 DacPortInfoTable[4][4][2] = 
{
    //          FGN                 FGP                     FDN                     FDP
    { {DAC1, DAC_VOUT0},  {DAC3, DAC_VOUT0},  {DAC1, DAC_VOUT2},  {DAC1, DAC_VOUT1}}, // ch1
    { {DAC1, DAC_VOUT7},  {DAC3, DAC_VOUT1},  {DAC1, DAC_VOUT5},  {DAC1, DAC_VOUT6}}, // ch2
    { {DAC2, DAC_VOUT0},  {DAC3, DAC_VOUT2},  {DAC2, DAC_VOUT2},  {DAC2, DAC_VOUT1}}, // ch3
    { {DAC2, DAC_VOUT7},  {DAC3, DAC_VOUT3},  {DAC2, DAC_VOUT5},  {DAC2, DAC_VOUT6}}, // ch4
};

u32 GetDACConfigWord_8G(ParamsDAC_8G params,u8* dacIndex)
{
	u32 ConfigWord = 0x10300000;

    *dacIndex = DacPortInfoTable[params.chnlId-1][(u8)params.type][0];
    u8 portIndex = DacPortInfoTable[params.chnlId-1][(u8)params.type][1];
	ConfigWord |= (portIndex << 16);
	ConfigWord |= params.data;

	return ConfigWord;
}

void GetDsaSpiLine(u8 chnlId, enum DsaIndex_8G dsaIndex, SpiLines* spiLines)
{
    GPIOPin* csLine = spiLines->csLine; 
	GPIOPin* dataLine = spiLines->dataLine; 
	GPIOPin* clkLine = spiLines->clkLine; 

    //初始用使用DSA0_8G的dataLine，clkLine
    dataLine->gpioType = GPIOD;
    dataLine->pinIndex = GPIO_Pin_13;
    clkLine->gpioType = GPIOD;
    clkLine->pinIndex = GPIO_Pin_11;

    switch (chnlId)
    {
        case 1:
            csLine->gpioType = GPIOD;
            csLine->pinIndex = GPIO_Pin_12;
            if(dsaIndex == DSA1_8G){
                dataLine->gpioType = GPIOC;
                dataLine->pinIndex = GPIO_Pin_10;
                clkLine->gpioType = GPIOC;
                clkLine->pinIndex = GPIO_Pin_4;
            }
            break;
        case 2:
            csLine->gpioType = GPIOE;
            csLine->pinIndex = GPIO_Pin_12;
            if(dsaIndex == DSA1_8G){
                dataLine->gpioType = GPIOD;
                dataLine->pinIndex = GPIO_Pin_9;
                clkLine->gpioType = GPIOC;
                clkLine->pinIndex = GPIO_Pin_5;
            }
            break;
        case 3:
            csLine->gpioType = GPIOA;
            csLine->pinIndex = GPIO_Pin_1;
            if(dsaIndex == DSA1_8G){
                dataLine->gpioType = GPIOE;
                dataLine->pinIndex = GPIO_Pin_9;
                clkLine->gpioType = GPIOB;
                clkLine->pinIndex = GPIO_Pin_0;
            }
						break;
        case 4:
            csLine->gpioType = GPIOE;
            csLine->pinIndex = GPIO_Pin_4;
            if(dsaIndex == DSA1_8G){
                dataLine->gpioType = GPIOC;
                dataLine->pinIndex = GPIO_Pin_0;
                clkLine->gpioType = GPIOB;
                clkLine->pinIndex = GPIO_Pin_1;
            }
						break;
        default:
            spiLines = NULL;
            break;  
    }
}

void DSA_PE43711_8G(u8 addrDev,u8 data,SpiLines* spiLines)
{
    u8 i=0;
    unsigned char addr1 = addrDev;
    
    GPIO_ResetBits(spiLines->csLine->gpioType, spiLines->csLine->pinIndex);//发送数据之前，片选拉低
    delay_us(2);
		
    //发送数据
    if(addr1 != 0)
    {
        for( i = 0; i < 8; i++ )
        {
            u8 j = (addr1 & 0x80);

            if(j == 0x80)
            {
                GPIO_SetBits(spiLines->dataLine->gpioType, spiLines->dataLine->pinIndex);
            }
            else
            {
                GPIO_ResetBits(spiLines->dataLine->gpioType, spiLines->dataLine->pinIndex);
            }
            delay_us(2);

            GPIO_SetBits(spiLines->clkLine->gpioType, spiLines->clkLine->pinIndex);
            delay_us(2);
            GPIO_ResetBits(spiLines->clkLine->gpioType, spiLines->clkLine->pinIndex);
            delay_us(2);

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

            if(j == 0x80)
            {
                GPIO_SetBits(spiLines->dataLine->gpioType, spiLines->dataLine->pinIndex);
            }
            else
            {
                GPIO_ResetBits(spiLines->dataLine->gpioType, spiLines->dataLine->pinIndex);
            }
            delay_us(2);

            GPIO_SetBits(spiLines->clkLine->gpioType, spiLines->clkLine->pinIndex);
            delay_us(2);
            GPIO_ResetBits(spiLines->clkLine->gpioType, spiLines->clkLine->pinIndex);
            delay_us(2);

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

            if(j == 0x01)
            {
                GPIO_SetBits(spiLines->dataLine->gpioType, spiLines->dataLine->pinIndex);
            }
            else
            {
                GPIO_ResetBits(spiLines->dataLine->gpioType, spiLines->dataLine->pinIndex);
            }
            delay_us(2);

            GPIO_SetBits(spiLines->clkLine->gpioType, spiLines->clkLine->pinIndex);
            delay_us(2);
            GPIO_ResetBits(spiLines->clkLine->gpioType, spiLines->clkLine->pinIndex);
            delay_us(2);

            data=(data>>1);
        }
    }

    //发送数据之后，片选拉高，拉低
    GPIO_SetBits(spiLines->csLine->gpioType, spiLines->csLine->pinIndex);
    delay_us(2);
    GPIO_ResetBits(spiLines->csLine->gpioType, spiLines->csLine->pinIndex);
    delay_us(2);
}

