AnsweredAssumed Answered

SPI interface STM-32

Question asked by bharath.r on Feb 17, 2015

I have been trying to interface between cc3200 and tdc gp-22,in which i got example code for tdc gp-22 which is written for STM32F105_VC microprocessor.

 

The attached function is used for " Reads n bytes from an address in GP22". In this function i cant understand the part from "compulsory reads to DR and SR to clear OVR so incoming next data is saved" which is specified as comments.Why SPI_I2S_GetFlagStatus(bus_type, SPI_I2S_FLAG_TXE); is used again. Whats the use of sending 0x00FF and recieving bus_type and whats the use of setting "while (SPI_I2S_GetFlagStatus(bus_type, SPI_I2S_FLAG_RXNE)==0)"

 

OR

 

can somebody explain how actually the spi interfacing takes place as i cant able to clearly understand the process going on between transmission and reciever buffer .Why is transmitter buffer is checked empty and reciver buffer is checked as not empty??. CAN SOMEONE HELP PLS?!!

 

float gp22_read_n_bytes(void *bus_type, uint8_t n_bytes, uint8_t read_opcode,
                   uint8_t read_addr, uint8_t fractional_bits)
{
  uint32_t    Result_read = 0;
  float       Result = 0;
  uint8_t     read_opcode_addr = read_opcode | read_addr;
 
  //.............. Result = n Byte = n x 8 bits......................
  if (bus_type==SPI1 | bus_type==SPI2)
  {
       // Deactivating Reset SPIx
       if (bus_type==SPI1) GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_RESET);
       if (bus_type==SPI2) GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_RESET);
       
       SPI_I2S_SendData(bus_type, read_opcode_addr);  // READ OPCODE + Address
        
       while (SPI_I2S_GetFlagStatus(bus_type, SPI_I2S_FLAG_TXE)==RESET) {};
  Simple_delay_750ns((void*)10); // important delay (16) at SPI freq.=750kHz
        
       //Compulsory reads to DR and SR to clear OVR,
       //so that next incoming data is saved
       SPI_I2S_ReceiveData(bus_type);                     // To clear OVR
       SPI_I2S_GetFlagStatus(bus_type, SPI_I2S_FLAG_TXE); // To clear OVR
 
         //Reading byte1
         SPI_I2S_SendData(bus_type, 0x00FF);  // DUMMY WRITE
         // Wait until RX buffer is not empty, then read the received data
         while (SPI_I2S_GetFlagStatus(bus_type, SPI_I2S_FLAG_RXNE)==0) {}
         Result_read = SPI_I2S_ReceiveData(bus_type); //  Read
 
       for (int n = 1; n < n_bytes; n++)
       {      
         //Reading byte2 .. byte.n
         SPI_I2S_SendData(bus_type, 0x00FF);  // DUMMY WRITE
         // Wait until RX buffer is not empty, then read the received data
         while (SPI_I2S_GetFlagStatus(bus_type, SPI_I2S_FLAG_RXNE)==0) {}
 
         Result_read = Result_read<<8;
         Result_read |= SPI_I2S_ReceiveData(bus_type); //  Read
       }
 
       // Reset to device SPIx
       if (bus_type==SPI1) GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_SET);
       if (bus_type==SPI2) GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_SET);
  }
   
  Result = Result_read / pow(2, fractional_bits);
 
  return Result;
}

Outcomes