AnsweredAssumed Answered

Reading SPI port - first byte?

Question asked by Snewo on Apr 8, 2014
Latest reply on Sep 18, 2015 by liang.jami
Maybe some of you with SPI experience can spot this one easily. I have a SPI slave device connected to my STM8L152. I watch the clock and data stream to the device requesting a read of a register, and then I can see the proper bit stream returned on the MISO line. However, I cannot get the STM8L's RX buffer to read the thing correctly. In fact, it rarely changes no matter what the bit pattern I see on the line. I have tried both phase polarities, and I am confident the clock polarity should be "idle low". Here is the short section of code, using only standard library calls, following the protocol as I understand it to be from the User's Manual (pg 549, "full duplex"). I break on a line following the last and read the returned result. Any suggestions are welcome. Thanks.

 //send byte through the SPI peripheral
  SPI_SendData(SPI1, 0x42);
    //loop while DR register in not empty
  while (SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET);
    //send redundant byte through the SPI peripheral (clocks in response to first read)
  SPI_SendData(SPI1, 0x42);
    //wait to receive a byte
  while (SPI_GetFlagStatus(SPI1, SPI_FLAG_RXNE) == RESET);

  // Return the byte read from the SPI bus
  spiByte = SPI_ReceiveData(SPI1);