cancel
Showing results for 
Search instead for 
Did you mean: 

Why can't I read from SPI Rx buffer?

Gergo Tanyi
Associate II
Posted on November 02, 2017 at 08:52

Hey guys!

I have an STM32 micro as a Master device, and I am communicating with a slave chip through the SPI interface. It works well, so far I can read from the slave's memory cells, or at least I can see the answer on the oscilloscope. But somehow I'm not able to read out this data from the buffer. I attached  the code, I hope somebody can see what I'm missing. I put it in a loop, which quits when theres any data.

#spi
14 REPLIES 14
Posted on November 02, 2017 at 16:08

Naming the device(s) we talk about would surely be helpful.

+1. There should be a 'how to post questions on this forum' FAQ with this being the very first item there.

---

You have to read the bytes out of the Rx buffer even if you don't need them, othewise the SPI receiver overflows (see status register) and stops receiving further bytes. Read the SPI error flags subchapter of the SPI chapter in RM.

Overrun flag (OVR)

An overrun condition occurs when the master or the slave completes the reception of the

next data frame while the read operation of the previous frame from the Rx buffer has not

completed (case RXNE flag is set).

In this case, the content of the Rx buffer is not updated with the new data received. A read

operation from the SPI_DR register returns the frame previously received. All other

subsequently transmitted data are lost.

You may have also incorrectly set

JW

Posted on November 02, 2017 at 15:55

 ... as some STM32 devices have a FIFO implementation, ...

Naming the device(s) we talk about would surely be helpful.

To me it seems that spiReceived value equals to the received byte during your transmission of 0x12, and that's why you get the wrong value. Take a look at TXE and RXNE flag timings in the Reference Manual. Wait for another RXNE flag and see if it resolves your issue.

The following dummy-write (0xFE) supposedly clocks out the answer/value.

Perhaps a scope image of a full sequence ?

Posted on November 02, 2017 at 17:38

Thanks! As a beginner I completely forgot about optimization. The level is 0 though, and volatile didnt help, but at least wecould rule out this error. Im about to check assembler.

Posted on November 02, 2017 at 17:47

That was the problem exactly!! Thank you really, thats just saved me a lot of wasted time. I had to read from the SPI->DR after every transfer, just as you said. And terribly sorry, it's an STM32F103C8T6, I posted this on an other forum where I mentioned the type of the micro, I just copied the body of the message.

Posted on November 02, 2017 at 17:53

Surely this was the problem. Funnily I followed the instructions how to conduct the transmission, reception procedure, and the only step I skipped was reading after the rest of the transmissions. Thank you again indeed!