#include "SPI.h"
void SpiInit1( void )
{
    CLK_PeripheralClockConfig(CLK_Peripheral_SPI1,ENABLE);
    SPI_IO_INIT;
    SPI_DeInit(SPI1);
    SPI0_DELAY;
    SPI_Init(SPI1,SPI_FirstBit_MSB,SPI_BaudRatePrescaler_16,SPI_Mode_Master,SPI_CPOL_Low,SPI_CPHA_1Edge,SPI_Direction_2Lines_FullDuplex,SPI_NSS_Soft,0);
    SPI_Cmd(SPI1,ENABLE);
//    SPI1->CR2 = 0x01;
//    SPI1->CR1 = 0x5C;
//    SPI1->CR1 |= 0x40;
}

uint8_t SpiInOut( uint8_t outData )
{
  u8 cc=0;
    /* Send SPIy data */
    //SPI_SendData( SPI1, outData );
    SPI1->DR = outData;
    while(!(SPI1->SR&0x02));
    while(!(SPI1->SR&0x01));
    cc= SPI1->DR;//SPI_ReceiveData( SPI1 );
    SPI1->SR &= ~0x01;
    return cc;
}
uint8_t regTemp=0,dataTemp=0,temp3=0;
uint8_t spi0ReadWrite(uint8_t reg,uint8_t data,enum DataDeal deal)
{
		SPI0_SCS_L;
		SPI0_DELAY;
		dataTemp = data;
		regTemp = reg;
		if(deal==write)
		{
				regTemp |= 0x80;
		}
		else if(deal==read)
		{
				regTemp &= 0x7F;
				dataTemp = 0xff;
		}
		SpiInOut(regTemp);
		temp3 =  SpiInOut(dataTemp);
		SPI0_SCS_H;
		return temp3;
}
uint8_t temp2;
void spiTest(void)
{
      if(DelayTime)return;
      DelayTime = 1;
      temp2 = spi0ReadWrite(REG_LR_VERSION,0xff,read);;//SpiInOut(0xa5);
      SPI0_DELAY;
      UsartPrint(&temp2,1);
}

