
#include "spi.h"
/* Global define */



u16 *SPI_TxData;
/*********************************************************************
 * @fn      DMA_Tx_Init
 *
 * @brief   Initializes the DMAy Channelx configuration.
 *
 * @param   DMA_CHx - x can be 1 to 7.
 *          ppadr - Peripheral base address.
 *          memadr - Memory base address.
 *          bufsize - DMA channel buffer size.
 *
 * @return  none
 */
void DMA_Tx_Init( DMA_Channel_TypeDef* DMA_CHx)
{
    DMA_InitTypeDef DMA_InitStructure={0};

    RCC_AHBPeriphClockCmd( RCC_AHBPeriph_DMA1, ENABLE );

    DMA_DeInit(DMA_CHx);

    DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&SPI1->DATAR;
    DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)SPI_TxData;
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
    DMA_InitStructure.DMA_BufferSize = 1024;
    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
    DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
    DMA_InitStructure.DMA_Priority = DMA_Priority_High;
    DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
    DMA_Init( DMA_CHx, &DMA_InitStructure );

}




/*******************************************************************************
* Function Name  : HW_SPI_Init
* Description    : AD5231数字电位器.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void CH32V_SPI_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure = {0};
    SPI_InitTypeDef  SPI_InitStructure = {0};

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_SPI1, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
    SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
    SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
    SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
    SPI_InitStructure.SPI_CRCPolynomial = 7;
    SPI_Init(SPI1, &SPI_InitStructure);

    SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx,DISABLE);

    SPI_Cmd(SPI1, ENABLE);

    DMA_Tx_Init( DMA1_Channel3 );

    DMA_Cmd( DMA1_Channel3, DISABLE );
}

void spi_dma_transmit(uint8_t *data,uint16_t len)
{
    DMA1_Channel3->CNTR = len;//发送数据长度
    DMA1_Channel3->MADDR =(uint32_t)data;//数据buff地址
    SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx,ENABLE); //开启 DMA发送
    DMA_Cmd( DMA1_Channel3, ENABLE);//开启DMA发送
    while(DMA_GetFlagStatus(DMA1_FLAG_TC3) == RESET);//等待传输完成

    DMA_ClearFlag( DMA1_FLAG_TC3);
    while((SPI1->STATR & SPI_I2S_FLAG_TXE)== RESET);
    DMA_Cmd( DMA1_Channel3, DISABLE);
    SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx,DISABLE);    //开启USART1 DMA发送
}

/*************************************************
函数功能：硬件SPI口发送或者接收一个字节数据

*************************************************/

/************************************************
函数名称 ： SPI_WriteReadByte
功    能 ： 对SPI写读字节数据
参    数 ： TxData --- 发送的字节数据
返 回 值 ： 读回来的字节数据
作    者 ： strongerHuang
*************************************************/
/*********************************************************************
 * @fn      SPI1_ReadWriteByte
 *
 * @brief   SPI1 read or write one byte.
 *
 * @param   TxData - write one byte data.
 *
 * @return  Read one byte data.
 */
u8 SPI1_ReadWriteByte(u8 TxData)
{
    u8 i = 0;

    while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET)
    {
        i++;
        if(i > 2000)
            return 0;
    }

    SPI_I2S_SendData(SPI1, TxData);
    i = 0;

    while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET)
    {
        i++;
        if(i > 2000)
            return 0;
    }
    return SPI_I2S_ReceiveData(SPI1);
}


//uint8_t SPI1_ReadWriteByte(uint8_t TxData)
//{
//   u8 i = 0;
//   while((SPI1->STATR & SPI_I2S_FLAG_TXE)== RESET)
//   {
//       i++;
//       if(i > 200)
//           return 0;
//   }
//   SPI1->DATAR = TxData;
//   i = 0;
//
//   while((SPI1->STATR & SPI_I2S_FLAG_RXNE)== RESET)
//   {
//       i++;
//       if(i > 200)
//           return 0;
//   }
//   return SPI1->DATAR;;
//}
