text: #include "stm32_spi.h"
  #include "stm32f2xx.h"
  
  int spi1_init(void)
  {	
      SPI_InitTypeDef SPI_InitStructure;
      GPIO_InitTypeDef GPIO_InitStruct;  
  
      RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
    
    	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
  	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  	GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
      GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
      GPIO_Init(GPIOA, &GPIO_InitStruct);
     	  
      GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);  
      GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);	
      GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);
    
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
      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_8;
      SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
      SPI_InitStructure.SPI_CRCPolynomial = 7;
      SPI_Init(SPI1, &SPI_InitStructure);
      SPI_Cmd(SPI1, ENABLE);
      
      return 0;
  }
  
  int spi3_init(void)
  {
  	GPIO_InitTypeDef GPIO_InitStructure;
  	SPI_InitTypeDef SPI_InitStructure;
  
  	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
      RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);
  
  	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
  	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
      GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  	GPIO_Init(GPIOB, &GPIO_InitStructure);
  
      GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF_SPI3);	
      GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_SPI3);
      GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_SPI3);
  
  	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_64;
  	SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  	SPI_InitStructure.SPI_CRCPolynomial = 7;
  	SPI_Init(SPI3, &SPI_InitStructure);
  	SPI_Cmd(SPI3, ENABLE);
  
      return 0;
  }
  
  int spi2_init(void)
  {
  	GPIO_InitTypeDef GPIO_InitStructure;
  	SPI_InitTypeDef SPI_InitStructure;
  
  	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
      RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
  
  	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
  	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
      GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  	GPIO_Init(GPIOB, &GPIO_InitStructure);
  
      GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_SPI2);	
      GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_SPI2);
      GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_SPI2);
      
  	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(SPI2, &SPI_InitStructure);
  	SPI_Cmd(SPI2, ENABLE);
  
      return 0;
  }
  
  /**
    * @mode
    * 	SPI_MODE_REG
    * 	SPI_MODE_LIB
    * @note
    * 	I don't know why the stmlib not work while read ad7705,
    * 	so, I reserve the register type read for it.
    */
  uint16_t spi_send_byte(SPI_TypeDef *SPIx, uint8_t data, int mode)
  {
  	u16 retry = 0;
  
  	if (mode) {
  		while (!SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE)) {
  			if (retry++ > 20000)
  				return 0;
  		}
  
  		SPI_I2S_SendData(SPIx, data);
  		
  		retry = 0;
  		while (!SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE)) {
  			if (retry++ > 20000)
  				return 0;
  		}
  
  		return SPI_I2S_ReceiveData(SPIx);
  	} else {
  		while ((SPIx->SR & 1 << 1) == 0) {
  			if (retry++ > 20000)
  				return 0;
  		}
  		
  		SPIx->DR = data;
  
  		retry = 0;
  		while ((SPIx->SR & 1 << 0) == 0) {
  			if(retry++ > 20000)
  				return 0;
  		}
  
  		return SPIx->DR;
  	}
  }
  
  