/**
  **********************************************************************************
  *@file    spi_drv.c
  *@author  Product application department
  *@version V1.0
  *@date    2020.02.13
  *@brief   SPI模块DRV层驱动.
  *
  @verbatim
  ===================================================================================
                                  ##### 更新 #####
  ===================================================================================
  [..] 更新时间:
  [..] 更新人:
  [..] 更新内容:
        1. nothing.
  @endverbatim
*/
#include "spi_drv.h"

/**
 *@brief 恢复SPI为默认设置.
 *
 *@param[in] pspi 指向SPI_TypeDef的指针;
 *@return  无;
 */
void DRV_SPI_DeInit(SPI_TypeDef *pspi)
{
    pspi->CR1 = 0x00;
    pspi->CR2 = 0x00;
    pspi->RXFTOCTR = 0x00;
    pspi->TXFTOCTR = 0x00;
    pspi->RXFCR = 0x00;
    pspi->TXFCR = 0x00;
    pspi->ICR = 0x00;
    pspi->DMACR = 0x00;
}
/**
 *@brief 初始化SPI模块.
 *
 *@param[in] pspi 指向SPI_TypeDef的指针;
 *@param[in] Init 指向SPI_InitTypeDef的指针;
 *@return  初始化结果; @ref DRV_SPI_StatusTypeDef
 */
DRV_SPI_StatusTypeDef DRV_SPI_Init(SPI_TypeDef *pspi, SPI_InitTypeDef *Init)
{
    uint8_t reg = 0;
    uint8_t tmp = 0;

    _spi_dis(pspi);
    _spi_dis_it(pspi);

    /*CR0*/
    if(Init->OutputMode == SPI_WIRED_OR_MODE_OPEN_DRAIN)
    {
    	tmp = SPI_WIRED_OR_MODE_OPEN_DRAIN;
    }
    reg = tmp;
    /*CR1*/
    if(Init->Mode == SPI_MASTER)
    {
        reg |= SPI_MASTER;

		/*配置管脚的方向*/
#if 1
        _spi_set_cs_bit(pspi);
        _spi_set_clk_bit(pspi);
        _spi_set_mosi_bit(pspi);
        _spi_pin_dir_input(pspi, SPI_PIN_MISO);
		_spi_pin_dir_output(pspi, SPI_PIN_CS);
		_spi_pin_dir_output(pspi, SPI_PIN_CLK);
		_spi_pin_dir_output(pspi, SPI_PIN_MOSI);
#endif
    }
	else
	{

		_spi_pin_dir_input(pspi, SPI_PIN_CS);
		_spi_pin_dir_input(pspi, SPI_PIN_CLK);
		_spi_pin_dir_input(pspi, SPI_PIN_MOSI);
		_spi_pin_dir_output(pspi, SPI_PIN_MISO);
	}

    if(Init->Cpol == SPI_CPOL_HIGH)
    {
        reg |= SPI_CPOL_HIGH;
    }

    if(Init->Cpha == SPI_CPHA_2EDGE)
    {
        reg |= SPI_CPHA_2EDGE;
    }

    if(Init->Ssoe == ENABLE)
    {
        reg |= SPI_EN_SSOE;
    }

    if(Init->FirstBit == SPI_FIRSTBIT_LSB)
    {
        reg |= SPI_FIRSTBIT_LSB;
    }

    pspi->CR1 = reg;

    reg = 0;
    /*CR2*/
    if(Init->GuradTimeEn == ENABLE)
    {
        reg = Init->GuardTime;
        reg &= 0x3f;
        reg <<= 2;
        _spi_en_guard_time(pspi);
    }
    else
    {
        _spi_dis_guard_time(pspi);
    }

	/*CR2*/
	if(Init->DozeMode == DISABLE)
	{
		reg |= SPI_DOZE_INACTIVE_MODE;
	}

	if(Init->Spc0 == ENABLE)
	{
		reg |= SPI_SPC0_BIDIR_MODE;
	}


    pspi->CR2 = reg;

    /*波持率*/
    if(Init->Mode == SPI_MASTER)
    {
        reg  = 0;
        Init->BaudratePreselectionBits &= 0x07;
        reg  = Init->BaudratePreselectionBits;
        reg <<= 4;
        Init->BaudrateBits &= 0x07;
        reg |= Init->BaudrateBits;
        pspi->BR = reg;
    }

    reg = 0;
    if(Init->Mode == SPI_SLAVE)
	{
		/*高速模式:只针对从机*/
		if(Init->SlaveHighSpeedEn == ENABLE)
		{
			reg |= SPI_EN_HIGH_SPEED;
		}
	}

	/*上拉*/
	if(Init->Pullup == ENABLE)
	{
		reg |= SPI_EN_PULLUP_DEVICE;
	}

	/*驱动能力*/
	if(Init->ReducedDriveCap == ENABLE)
	{
		reg |= SPI_REDUCE_CAP;
	}

	pspi->PURD = reg;

    /*清队fifo*/
    _spi_clr_tx_fifo(pspi);
    _spi_clr_rx_fifo(pspi);

    #if 1
    if(Init->FifoInit.rx_fifo_en == ENABLE)
    {
        _spi_set_rx_fifo_threshold(pspi, Init->FifoInit.rx_fifo_threshold);
        _spi_set_rx_fifo_time_cnt(pspi, Init->FifoInit.rx_fifo_timeout_cnt);
    }
	else
	{
		/*FIFO深度为1*/
	}

    if(Init->FifoInit.tx_fifo_en == ENABLE)
    {
        _spi_set_tx_fifo_threshold(pspi, Init->FifoInit.tx_fifo_threshold);
        _spi_set_tx_fifo_time_cnt(pspi, Init->FifoInit.tx_fifo_timeout_cnt);
    }
	else
	{
		/*深度为1*/
		//_spi_set_tx_fifo_threshold(pspi,0);
	}
    #endif
	
    /**/
    _spi_en(pspi);

    return(DRV_SPI_OK);
}
/**
 *@brief 使能SPI.
 *
 *@param[in] pspi 指向SPI_TypeDef的指针;
 *@return 无;
 */
void DRV_SPI_En(SPI_TypeDef *pspi)
{
    _spi_en(pspi);
}
/**
 *@brief 禁止SPI.
 *
 *@param[in] pspi 指向SPI_TypeDef的指针;
 *@return 无;
 */
void DRV_SPI_Dis(SPI_TypeDef *pspi)
{
    _spi_dis(pspi);
}
/**
 *@brief 使能或禁止SPI.
 *
 *@param[in] pspi 指向SPI_TypeDef的指针;
 *@param[in] NewState 新的状态
 * -ENABLE;
 * -DISABLE;
 *@return 无;
 */
void DRV_SPI_Cmd(SPI_TypeDef *pspi, FunctionalStateTypeDef NewState)
{
    if(NewState ==  ENABLE)
        _spi_en(pspi);
    else
        _spi_dis(pspi);
}
/**
 *@brief 使能SPI中断.
 *
 *@param[in] pspi 指向SPI_TypeDef的指针;
 *@return 无;
 */
void DRV_SPI_ItEn(SPI_TypeDef *pspi)
{

}
/**
 *@brief 主机查询方式发送一字节.
 *
 *@param[in] pspi   指向SPI_TypeDef的指针;
 *@param[in] ToSend 代发送数据
 *@return 发送结果;  @ref DRV_SPI_StatusTypeDef
 */
DRV_SPI_StatusTypeDef DRV_SPI_TransmitByte(SPI_TypeDef *pspi, uint8_t ToSend)
{
    while(pspi->SR & SPI_FLAG_TX_FIFO_FULL);

    pspi->DR = ToSend;

    while((pspi->SR & SPI_FLAG_FINISH) != SPI_FLAG_FINISH);

    _spi_clr_tx_fifo(pspi);

    return(DRV_SPI_OK);
}
/**
 *@brief 主机查询方式收取一字节.
 *
 *@param[in] pspi   指向SPI_TypeDef的指针;
 *@param[in] *Rx    收取数据指针;
 *@return 接收结果;  @ref DRV_SPI_StatusTypeDef
 */
DRV_SPI_StatusTypeDef DRV_SPI_ReceiveByte(SPI_TypeDef *pspi, uint8_t *Rx)
{
    _spi_clr_tx_fifo(pspi);

    while(pspi->SR & SPI_FLAG_TX_FIFO_FULL);

    pspi->DR = 0XFF;

    while((pspi->SR & SPI_FLAG_FINISH) != SPI_FLAG_FINISH);

    _spi_clr_tx_fifo(pspi);

    while(pspi->SR & SPI_FLAG_RX_FIFO_EMPTY);

    *Rx = pspi->DR;

    _spi_clr_rx_fifo(pspi);

    return(DRV_SPI_OK);
}
/**
 *@brief 主机查询方式发送数据.
 *
 *@param[in] pspi 指向SPI_TypeDef的指针;
 *@param[in] pBuf 数据指针;
 *@param[in] Size 送数据长度;
 *@return 操作结果; @ref DRV_SPI_StatusTypeDef
 */
DRV_SPI_StatusTypeDef DRV_SPI_TransmitBytes(SPI_TypeDef *pspi, 
                                            uint8_t *pBuf, 
                                            uint32_t Size)
{
    uint32_t loop = 0;

    for(loop = 0; loop < Size; loop++)
    {
        while(pspi->SR & SPI_FLAG_TX_FIFO_FULL);

        pspi->DR = *(pBuf + loop);

        while((pspi->SR & SPI_FLAG_FINISH) != SPI_FLAG_FINISH);

        //_spi_clr_flag_transmission_end(pspi);
    }

    _spi_clr_tx_fifo(pspi);
    _spi_clr_rx_fifo(pspi);

    return(DRV_SPI_OK);
}
/**
 *@brief 主机查询方式接收数据.
 *
 *@param[in] pspi 指向SPI_TypeDef的指针;
 *@param[in] pBuf 数据指针;
 *@param[in] Size 数据长度;
 *@return 操作结果; @ref DRV_SPI_StatusTypeDef
 */
DRV_SPI_StatusTypeDef DRV_SPI_ReceiveBytes(SPI_TypeDef *pspi, uint8_t *pBuf, uint32_t len)
{
    uint32_t loop = 0;

    for(loop = 0; loop < len; loop++)
    {
        while(pspi->SR & SPI_FLAG_TX_FIFO_FULL);

        pspi->DR = 0XFF;

        while((pspi->SR & SPI_FLAG_FINISH) != SPI_FLAG_FINISH);

        _spi_clr_tx_fifo(pspi);

        while(pspi->SR & SPI_FLAG_RX_FIFO_EMPTY);

        *(pBuf + loop) = pspi->DR;

        _spi_clr_rx_fifo(pspi);
    }

    _spi_clr_rx_fifo(pspi);

    return(DRV_SPI_OK);
}
/**
 *@brief 从机查询方式接收数据.
 *
 *@param[in]  pspi 指向SPI_TypeDef的指针;
 *@param[out] pBuf 数据指针;
 *@param[in]  Timeout 超时时间;
 * -0  无限等待;
 * -非0 超时退出;
 *@return 操作结果; @ref DRV_SPI_StatusTypeDef
 */
DRV_SPI_StatusTypeDef DRV_SPI_SlaveReceiveByte(SPI_TypeDef *pspi, 
                                               uint8_t *Rx, 
                                               uint32_t Timeout)
{
    uint32_t loop = 0;
    DRV_SPI_StatusTypeDef DrvStatus = DRV_SPI_OK;

    if(Timeout == 0)
    {
        while(pspi->SR & SPI_FLAG_RX_FIFO_EMPTY);

        *Rx = pspi->DR;

        _spi_clr_rx_fifo(pspi);
    }
    else
    {
        do
        {
            if(Timeout == 0)
            {
                return(DRV_SPI_TIMEOUT);
            }

            Timeout--;
        } while((pspi->SR & SPI_FLAG_RX_FIFO_EMPTY));
    }

    *Rx = pspi->DR;

    return(DRV_SPI_OK);
}
/**
 *@brief 从机查询方式发送数据.
 *
 *@param[in] pspi 指向SPI_TypeDef的指针;
 *@param[in] pBuf 数据指针;
 *@param[in] Timeout 超时时间;
 * -0  无限等待;
 * -非0 超时退出;
 *@return 操作结果; @ref DRV_SPI_StatusTypeDef
 */
DRV_SPI_StatusTypeDef DRV_SPI_SlaveTransmitByte(SPI_TypeDef *pspi, uint8_t ToSend, uint32_t Timeout)
{
    uint32_t loop = 0;
    uint32_t TmpTimeout = Timeout;
    DRV_SPI_StatusTypeDef DrvStatus = DRV_SPI_OK;

    if(Timeout == 0)
    {
        while(pspi->SR & SPI_FLAG_TX_FIFO_FULL);

        pspi->DR = ToSend;

        while((pspi->SR & SPI_FLAG_FINISH) != SPI_FLAG_FINISH);
    }
    else
    {
        do
        {
            if(TmpTimeout == 0)
            {
                return(DRV_SPI_TIMEOUT);
            }

            TmpTimeout--;
        } while((pspi->SR & SPI_FLAG_TX_FIFO_FULL));

        pspi->DR = ToSend;

        TmpTimeout  = Timeout;

        do
        {
            if(TmpTimeout == 0)
            {
                return(DRV_SPI_TIMEOUT);
            }

            TmpTimeout--;
        } while((pspi->SR & SPI_FLAG_FINISH) != SPI_FLAG_FINISH);
    }


    return(DRV_SPI_OK);
}
/**
 *@brief 从机查询方式发达数据.
 *
 *@param[in]  pspi 指向SPI_TypeDef的指针;
 *@param[in] pBuf 数据指针;
 *@param[in]  len  数据长度;
 *@param[in]  Timeout 超时时间;
 * -0  无限等待;
 * -非0 超时退出;
 *@return 操作结果; @ref DRV_SPI_StatusTypeDef
 */
DRV_SPI_StatusTypeDef DRV_SPI_SlaveTransmitBytes(SPI_TypeDef *pspi, uint8_t *pBuf, uint32_t len, uint32_t Timeout)
{
    uint32_t loop = 0;
    uint8_t Val = 0;
    uint32_t TmpTimeout = Timeout;
    DRV_SPI_StatusTypeDef DrvStatus = DRV_SPI_OK;

    if(Timeout == 0)
    {
        for(loop = 0; loop < len; loop++)
        {
            //while(pspi->SR & SPI_FLAG_TX_FIFO_FULL);
        	//Val = *(pBuf + loop);
            pspi->DR = *(pBuf + loop);

            while((pspi->SR & SPI_FLAG_FINISH) != SPI_FLAG_FINISH);
        }
    }
    else
    {
        for(loop = 0; loop < len; loop++)
        {
        	TmpTimeout  = Timeout;
            pspi->DR = *(pBuf + loop);

            do
            {
                if(TmpTimeout == 0)
                {
                    return(DRV_SPI_TIMEOUT);
                }

                TmpTimeout--;
            } while((pspi->SR & SPI_FLAG_FINISH) != SPI_FLAG_FINISH);
        }
    }

    return(DRV_SPI_OK);
}
/**
 *@brief 从机查询方式接收数据.
 *
 *@param[in]  pspi 指向SPI_TypeDef的指针;
 *@param[out] pBuf 数据指针;
 *@param[in]  len  数据长度;
 *@param[in]  Timeout 超时时间;
 * -0  无限等待;
 * -非0 超时退出;
 *@return 操作结果; @ref DRV_SPI_StatusTypeDef
 */
DRV_SPI_StatusTypeDef DRV_SPI_SlaveReceiveBytes(SPI_TypeDef *pspi, 
                                                uint8_t *pBuf, 
                                                uint32_t len, 
                                                uint32_t Timeout)
{
    uint32_t loop = 0;
    uint32_t TmpTimeout;
    DRV_SPI_StatusTypeDef DrvStatus = DRV_SPI_OK;

    if(Timeout == 0)
    {
        for(loop = 0; loop < len; loop++)
        {
            while(pspi->SR & SPI_FLAG_RX_FIFO_EMPTY);

            *(pBuf + loop) = pspi->DR;

            _spi_clr_rx_fifo(pspi);
        }
    }
    else
    {
        TmpTimeout = Timeout;

        for(loop = 0; loop < len; loop++)
        {
            do
            {
                if(TmpTimeout == 0)
                {
                    return(DRV_SPI_TIMEOUT);
                }

                TmpTimeout--;
            } while((pspi->SR & SPI_FLAG_RX_FIFO_EMPTY));

            *(pBuf + loop) = pspi->DR;
            TmpTimeout = Timeout;
        }
    }

    return(DRV_SPI_OK);
}
/**
 *@brief 置数据寄存器值.
 *
 *@param[in] pspi 指向SPI_TypeDef的指针;
 *@param[in] val  数据;
 *@return 无
 */
void DRV_SPI_SetDR(SPI_TypeDef *pspi, uint8_t val)
{
    pspi->DR = val;
}
/**
 *@brief 获取数据寄存器值.
 *
 *@param[in]  pspi 指向SPI_TypeDef的指针;
 *@param[out] pval 数据指针;
 *@return 无
 */
void DRV_SPI_GetDR(SPI_TypeDef *pspi, uint8_t *pval)
{
    *pval = pspi->DR;
}
/**
  *@brief 获取标志状态.
  *
  *@param[in]  pspi 指向SPI_TypeDef的指针;
  *@param[in]  FlagIndex SPI标志索引;
  	*- SPI_FLAG_INDEX_RXFEMP
	*- SPI_FLAG_INDEX_RXFFULL
	*- SPI_FLAG_INDEX_XFEMP
	*- SPI_FLAG_INDEX_TXFFFUL
	*- SPI_FLAG_INDEX_MODE
	*- SPI_FLAG_INDEX_EOTF
	*- SPI_FLAG_INDEX_FLOST
	*- SPI_FLAG_INDEX_SPIF
	*- SPI_FLAG_INDEX_RXFSER
	*- SPI_FLAG_INDEX_RXFUDF
	*- SPI_FLAG_INDEX_RXFOVFM
	*- SPI_FLAG_INDEX_RXFTO
	*- SPI_FLAG_INDEX_TXFSER
	*- SPI_FLAG_INDEX_TXFUDF
	*- SPI_FLAG_INDEX_TXFOVFM
	*- SPI_FLAG_INDEX_TXFTO 
  *@param[out] pflag 标志状态
  *- SET;
  *- REST;
  *@retval 当前状态 @ref DRV_SPI_StatusTypeDef
  */
DRV_SPI_StatusTypeDef DRV_SPI_GetFlag(SPI_TypeDef *pspi,SPI_FlagIndexTypeDef FlagIndex,uint8_t*pflag)
{

	if((FlagIndex>= SPI_FLAG_INDEX_RXFEMP) && (FlagIndex<= SPI_FLAG_INDEX_SPIF))
	{
		if(pspi->SR &(1<<FlagIndex))
			*pflag = SET;
		else
			*pflag = RESET;
	}
	else if((FlagIndex>= SPI_FLAG_INDEX_RXFSER) && (FlagIndex<= SPI_FLAG_INDEX_TXFTO))
	{
        if(pspi->SRH &(1<<(FlagIndex-8)))
			*pflag = SET;
		else
			*pflag = RESET;
	}
	else
	{
		return(DRV_SPI_ERROR);
	}
	
	return(DRV_SPI_OK);
}
/**
  *@brief 清除标志状态.
  *
  *@param[in]  pspi 指向SPI_TypeDef的指针;
  *@param[in]  FlagIndex SPI标志索引;
  	*- SPI_FLAG_INDEX_RXFEMP
	*- SPI_FLAG_INDEX_RXFFULL
	*- SPI_FLAG_INDEX_XFEMP
	*- SPI_FLAG_INDEX_TXFFFUL
	*- SPI_FLAG_INDEX_MODE
	*- SPI_FLAG_INDEX_EOTF
	*- SPI_FLAG_INDEX_FLOST
	*- SPI_FLAG_INDEX_SPIF
	*- SPI_FLAG_INDEX_RXFSER
	*- SPI_FLAG_INDEX_RXFUDF
	*- SPI_FLAG_INDEX_RXFOVFM
	*- SPI_FLAG_INDEX_RXFTO
	*- SPI_FLAG_INDEX_TXFSER
	*- SPI_FLAG_INDEX_TXFUDF
	*- SPI_FLAG_INDEX_TXFOVFM
	*- SPI_FLAG_INDEX_TXFTO 
  *@retval 当前状态 @ref DRV_SPI_StatusTypeDef
  */

DRV_SPI_StatusTypeDef DRV_SPI_ClrFlag(SPI_TypeDef *pspi,SPI_FlagIndexTypeDef FlagIndex)
{
    uint8_t TmpStatus = 0;

	if((FlagIndex>= SPI_FLAG_INDEX_RXFEMP) && (FlagIndex<= SPI_FLAG_INDEX_SPIF))
	{
		TmpStatus = pspi->SR;
		switch(FlagIndex)
		{
		    case SPI_FLAG_INDEX_FLOST:
			case SPI_FLAG_INDEX_EOTF:
			{
				pspi->SR |= (1<<FlagIndex);
				break;
			}
			default:break;
		}
	}
	else if((FlagIndex>= SPI_FLAG_INDEX_RXFSER) && (FlagIndex<= SPI_FLAG_INDEX_TXFTO))
	{
        TmpStatus = pspi->SRH;
		FlagIndex -= 8;
		switch(FlagIndex)
		{
		    case SPI_FLAG_INDEX_RXFUDF:
			case SPI_FLAG_INDEX_RXFOVFM:
			case SPI_FLAG_INDEX_TXFUDF:
			case SPI_FLAG_INDEX_TXFOVFM:
			case SPI_FLAG_INDEX_TXFTO:									
			{
				pspi->SR |= (1<<FlagIndex);
				break;
			}
			default:break;
		}
	}
	else
	{
		return(DRV_SPI_ERROR);
	}

	return(DRV_SPI_OK);
}
/**
  *@brief 获取标志状态.
  *
  *@param[in]  pspi 指向SPI_TypeDef的指针;
  *@param[in]  FlagIndex SPI标志索引;
  	*- SPI_FLAG_INDEX_RXFEMP
	*- SPI_FLAG_INDEX_RXFFULL
	*- SPI_FLAG_INDEX_XFEMP
	*- SPI_FLAG_INDEX_TXFFFUL
	*- SPI_FLAG_INDEX_MODE
	*- SPI_FLAG_INDEX_EOTF
	*- SPI_FLAG_INDEX_FLOST
	*- SPI_FLAG_INDEX_SPIF
	*- SPI_FLAG_INDEX_RXFSER
	*- SPI_FLAG_INDEX_RXFUDF
	*- SPI_FLAG_INDEX_RXFOVFM
	*- SPI_FLAG_INDEX_RXFTO
	*- SPI_FLAG_INDEX_TXFSER
	*- SPI_FLAG_INDEX_TXFUDF
	*- SPI_FLAG_INDEX_TXFOVFM
	*- SPI_FLAG_INDEX_TXFTO 
  *@param[in] staus 标志状态
  *- SET;
  *- REST;
  @param[in] timeout 超时时间;
  *@retval 当前状态 @ref DRV_SPI_StatusTypeDef
  */
DRV_SPI_StatusTypeDef  DRV_SPI_WaitonFlagTimeout(SPI_TypeDef *pspi,
                                            SPI_FlagIndexTypeDef index,
                                            FlagStatusTypeDef status,
                                            uint32_t timeout)
{
	FlagStatusTypeDef tmp_flag = RESET;

    if(timeout == 0)
    {
        do
        {
            DRV_SPI_GetFlag(pspi, index, &tmp_flag);
        } while(tmp_flag != SET);
    }
    else
    {
        if(status == SET)
        {
            DRV_SPI_GetFlag(pspi, index, &tmp_flag);

            while(tmp_flag != SET)
            {
                timeout--;

                if(timeout == 0)
                {
                    return(DRV_SPI_TIMEOUT);
                }

                DRV_SPI_GetFlag(pspi, index, &tmp_flag);
            }
        }
        else if(status == RESET)
        {
            DRV_SPI_GetFlag(pspi, index, &tmp_flag);

            while(tmp_flag != RESET)
            {
                timeout--;

                if(timeout == 0)
                {
                    return(DRV_SPI_TIMEOUT);
                }

                DRV_SPI_GetFlag(pspi, index, &tmp_flag);
            }
        }
    }

    return DRV_SPI_OK;
    
}

/**
 *@brief SPI管脚用作GPIO初始化.
 *
 *@param pspi[in] pspi 指向I2C_TypeDef结构体的指针;
 *@param Init[in] Init 初始化结构体指针;
 *@return DRV_SPI_StatusTypeDef @ref DRV_I2C_StatusTypeDef
 */
DRV_SPI_StatusTypeDef DRV_SPI_GPIOInit(SPI_TypeDef *pspi, GPIO_InitTypeDef *Init)
{
    /*方向*/
    if(Init->Dir == GPIO_DIR_OUT)
    {
        _spi_pin_dir_output(pspi, Init->Pin);

        #if 0

        /*影响全部的4个管脚*/
        if(Init->OutputMode == GPIO_OUTPUT_MODE_CMOS)
        {
            _spi_pin_output_cmos(pspi);
        }
        else if(Init->OutputMode == GPIO_OUTPUT_MODE_CMOS)
        {
            _spi_pin_output_opendrain(pspi);
        }
        else
        {
            return(DRV_SPI_ERROR);
        }

        #endif
    }
    else if(Init->Dir == GPIO_DIR_IN)
    {
        _spi_pin_dir_input(pspi, Init->Pin);
    }
    else if(Init->Dir == GPIo_DIR_TRIGATE)
    {
        /*用作输主之后IOCONTROL部分禁止输入*/
        _spi_pin_dir_input(pspi, Init->Pin);
        /**/
    }

    /*上下拉影响全部4个管脚*/

    /**/
    return(DRV_SPI_OK);
}
/**
 *@brief SPI管脚恢复初始设置.
 *
 *@param pspi[in] pspi 指向SPI_TypeDef结构体的指针;
 *@param Init[in] pin  SPI的管脚;
 *- SPI_PIN_CS;
 *- SPI_PIN_CLK;
 *- SPI_PIN_MOSI;
 *- SPI_PIN_MISO;
 *@return DRV_SPI_StatusTypeDef @ref DRV_SPI_StatusTypeDef
 */
void DRV_SPI_GPIODeInit(SPI_TypeDef *pspi, uint8_t pin)
{

}
/**
 *@brief SPI管脚输出指定电平.
 *
 *@param pspi[in] pspi 指向SPI_TypeDef结构体的指针;
 *@param Init[in] pin  SPI的管脚;
 *- SPI_PIN_CS;
 *- SPI_PIN_CLK;
 *- SPI_PIN_MOSI;
 *- SPI_PIN_MISO;
 *@return 无
 */
void DRV_SPI_WritePin(SPI_TypeDef *pspi, uint8_t pin, GPIO_PinStateTypeDef state)
{
    if(state == RESET)
    {
        _spi_reset_pin_bit(pspi, pin);
    }
    else
    {
        _spi_set_pin_bit(pspi, pin);
    }
}
/**
 *@brief 获取SPI管脚电平.
 *
 *@param pspi[in] pspi 指向SPI_TypeDef结构体的指针;
 *@param Init[in] pin  SPI的管脚;
 *- SPI_PIN_CS;
 *- SPI_PIN_CLK;
 *- SPI_PIN_MOSI;
 *- SPI_PIN_MISO;
 *@return 管脚电平状态 @ref @BitActionTypeDef
 */
BitActionTypeDef DRV_SPI_ReadPin(SPI_TypeDef *pspi, uint8_t pin)
{
    if(_spi_get_pin_bit(pspi, pin))
    {
        return(SET);
    }
    else
    {
        return(RESET);
    }
}
/**
 *@brief SPI管脚电平翻转.
 *
 *@param pspi[in] pspi 指向SPI_TypeDef结构体的指针;
 *@param Init[in] pin  SPI的管脚;
 *- SPI_PIN_CS;
 *- SPI_PIN_CLK;
 *- SPI_PIN_MOSI;
 *- SPI_PIN_MISO;
 *@return 无
 */
void DRV_SPI_TogglePin(SPI_TypeDef *pspi, uint8_t pin)
{
    pspi->DDR &= ~(1 << pin);
}
