#include "hw.h"

/* Private variables ---------------------------------------------------------*/
volatile bool blockingDmaFlag;

void SpiInit(void)
{
	  // 使能SPI1和GPIOA时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 | RCC_APB2Periph_GPIOA, ENABLE);

    // 配置SPI1的引脚
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin = RADIO_MOSI_PIN | RADIO_MISO_PIN| RADIO_SCK_PIN;  // SCK, MISO, MOSI
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  // 复用推挽输出
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(RADIO_MOSI_PORT, &GPIO_InitStructure);
 
  
    GPIO_SetBits(RADIO_MOSI_PORT,RADIO_MOSI_PIN|RADIO_MISO_PIN|RADIO_SCK_PIN); //初始上拉输出

    // 配置SPI1
    SPI_InitTypeDef SPI_InitStructure;
	
		SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;	//双线全双工
		SPI_InitStructure.SPI_Mode = SPI_Mode_Master;						//SPI为主机模式
		SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;					//传输的数据长度为8位
		SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;							//SPI总线空闲时，sck线为低电平
		SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;						//在SCK的偶数边沿采样数据
		SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;							//NSS软件控制模式
		SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;	//时钟分频值，此处为8分频
		SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;					//数据高位先行
		SPI_InitStructure.SPI_CRCPolynomial = 7;						//CRC校验多项式设置，数据传输的最后一个字节出加上此处的7，以完成CRC校验
		SPI_Init(SPI1, &SPI_InitStructure);
	
		SPI_Cmd(SPI1, ENABLE);
}

void SpiDeInit( void )
{
    SPI_Cmd(SPI1,DISABLE);
	
		RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1,ENABLE);
		RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1,DISABLE);
}


#define WAIT_FOR_BLOCKING_FLAG         while( blockingDmaFlag ) { }


/*!
 * @brief Sends txBuffer and receives rxBuffer
 *
 * @param [IN] txBuffer Byte to be sent
 * @param [OUT] rxBuffer Byte to be sent
 * @param [IN] size Byte to be sent
 */
void SpiInOut( uint8_t *txBuffer, uint8_t *rxBuffer, uint16_t size )
{
//      while (!(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == SET)) {
//        // 等待 RXNE 标志
//    }
//    SPI1->DR;
    
    
    for (uint16_t i = 0; i < size; i++) {
        // 等待发送缓冲区为空
        while (!(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == SET)) {}

        // 写入数据到DR寄存器
        SPI1->DR = txBuffer[i];

        // 等待接收缓冲区非空
        while (!(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == SET)) {}

        // 读取数据
        rxBuffer[i] = SPI1->DR;
    }
}

void SpiIn( uint8_t *txBuffer, uint16_t size )
{
  
    uint8_t data = 0;
  
    for (uint16_t i = 0; i < size; i++)
    {
//        // 等待发送缓冲区为空
//        while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET) {}

//        // 写入数据到DR寄存器
//        SPI1->DR = txBuffer[i];

//        // 等待接收缓冲区非空
//        while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET) {}
//          
//        data = SPI1->DR;
      
      while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
      SPI_I2S_SendData(SPI1, txBuffer[i]);
	
      while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
      data = SPI_I2S_ReceiveData(SPI1);

    }
}
