1
votes

I am trying to loopback the SPI bus on my STM32F0 (with a discovery board, MISO pin connected to MOSI pin).

I am following the tutorial Discovering the STM32 Microcontroller, (edition January 18, 2014), Exercise 6.1 : SPILoopback.

The STM32 is configured as the master.

To send a byte to MOSI pin, the author wrote :

SPI_I2S_SendData (SPIx, *tbuf++);

where :

  • SPIx is the SPI bus I want to send data
  • tbuf is the uint8 (in other words an unsigned char ...) I want to send on the bus

To receive this byte from MISO pin, he wrote :

while (SPI_I2S_GetFlagStatus (SPIx, SPI_I2S_FLAG_RXNE) == RESET);
if (rbuf)
{
    *rbuf++ = SPI_I2S_ReceiveData(SPIx);
...
...

The flag SPI_I2S_FLAG_RXNE should be SET since I send a data to MOSI pin, and since MOSI pin is connected to MISO pin.

My problem is :

I never go out from the while loop (the SPI_I2S_FLAG_RXNE is never SET, even if I look through the debugger.

(I see CLK and MOSI being alive on my logic analyser, so I'm sure my byte goes out from an electrical point of view.)

It is like the STM32 never received the byte, even if MISO pin is connected on MOSI pin ...

Why ?

1

1 Answers

1
votes

It is because on STM32F0, the flag SPI_I2S_FLAG_RXNE is set by default only when 16 bits (so 2 bytes) are received on MISO pin.

If you send 2 bytes and then do you while loop, you should exit of the loop very quickly.

The Reference Manual said (Section SPI status flags) :

The RXNE flag is set depending on the FRXTH bit value in the SPIx_CR2 register:

  • If FRXTH is set, RXNE goes high and stays high until the RXFIFO level is greater or equal to 1/4 (8-bit).

  • If FRXTH is cleared, RXNE goes high and stays high until the RXFIFO level is greater than or equal to 1/2 (16-bit).

So, if you want the flag SPI_I2S_FLAG_RXNE to be set when only 1 byte is received, you should use the following function :

SPI_RxFIFOThresholdConfig (SPIx, SPI_RxFIFOThreshold_QF);
  • SPI_RxFIFOThreshold_QF: RXNE event is generated if the FIFO level is greater or equal to 1/4.
  • SPI_RxFIFOThreshold_HF: RXNE event is generated if the FIFO level is greater or equal to 1/2. (default value)