#ifndef _SPI_H
#define _SPI_H
#include "user.h"
#define SPI_GPIO1 GPIOD
#define SPI_RESET_PIN GPIO_Pin_4
#define SPI_GPIO2 GPIOB
#define SPI_NSS_PIN GPIO_Pin_4
#define SPI_SCK_PIN GPIO_Pin_5
#define SPI_MOSI_PIN GPIO_Pin_6
#define SPI_MISO_PIN GPIO_Pin_7

#define SPI0_SCS_H 	{GPIO_WriteBit(GPIOD,GPIO_Pin_7,SET);}
#define SPI0_SCS_L  {GPIO_WriteBit(GPIOD,GPIO_Pin_7,RESET);}

//#define SPI0_NSS_H {GPIO_WriteBit(SPI_GPIO2,SPI_NSS_PIN,SET);}
//#define SPI0_NSS_H {GPIO_WriteBit(SPI_GPIO2,SPI_NSS_PIN,SET);}


#define SPI_IO_INIT		{ GPIO_Init(GPIOD,GPIO_Pin_7,GPIO_Mode_Out_PP_High_Fast);GPIO_WriteBit(GPIOD,GPIO_Pin_7,TRUE);\
    GPIO_Init(SPI_GPIO2,SPI_SCK_PIN,GPIO_Mode_Out_PP_High_Fast);\
      GPIO_Init(SPI_GPIO2,SPI_MOSI_PIN,GPIO_Mode_Out_PP_High_Fast);\
        GPIO_Init(SPI_GPIO2,SPI_MISO_PIN,GPIO_Mode_In_PU_No_IT);\
}

#define SPI0_DELAY	{\
asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");\
  asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");\
    asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");\
      asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");\
	asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");\
         asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");\
            asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");\
              asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");asm("NOP;");\
}

enum DataDeal
{
		read=0,
		write=1
};
void SpiInit1( void );
uint8_t SpiInOut( uint8_t outData );
uint8_t spi0ReadWrite(uint8_t reg,uint8_t data,enum DataDeal deal);
void spiTest(void);
#endif
