Showing results for 
Search instead for 
Did you mean: 

SPI Master RXWNE never sets at high speed

Associate III

Hi Experts

I have a situation with STMH7 as a master on the SPI3 bus with two slaves (both RM44),

I am using polling on master and chip select lines to select individual slaves.

Problem is at speed greater than 5 Mbps the RXWNE flag never sets and code always stuck at following line of code and never gets inside this if, and RxXferCount never decreases.

if (((hspi->Instance->SR & (SPI_FLAG_RXWNE | SPI_FLAG_FRLVL)) != 0UL) && (initial_RxXferCount > 0UL))

But at low speed everything works fine. Could you please throw some light what could be the possible cause of the behavior.

Thanks for your constant support.

Have a lovely day ahead, and please let me know if you need more information.

Following is the complete code for your reference

/* Wait until RXWNE/FRLVL flag is reset */
      if (((hspi->Instance->SR & (SPI_FLAG_RXWNE | SPI_FLAG_FRLVL)) != 0UL) && (initial_RxXferCount > 0UL))
        if ((hspi->Instance->SR & SPI_FLAG_RXWNE) != 0UL)
          *((uint32_t *)hspi->pRxBuffPtr) = *((__IO uint32_t *)&hspi->Instance->RXDR);
          hspi->pRxBuffPtr += sizeof(uint32_t);
          hspi->RxXferCount -= (uint16_t)4UL;
          initial_RxXferCount = hspi->RxXferCount;
        else if ((hspi->Instance->SR & SPI_FLAG_FRLVL) > SPI_RX_FIFO_1PACKET)
#if defined (__GNUC__)
          *((uint16_t *)hspi->pRxBuffPtr) = *prxdr_16bits;
          *((uint16_t *)hspi->pRxBuffPtr) = *((__IO uint16_t *)&hspi->Instance->RXDR);
#endif /* __GNUC__ */
          hspi->pRxBuffPtr += sizeof(uint16_t);
          hspi->RxXferCount -= (uint16_t)2UL;
          initial_RxXferCount = hspi->RxXferCount;
          *((uint8_t *)hspi->pRxBuffPtr) = *((__IO uint8_t *)&hspi->Instance->RXDR);
          hspi->pRxBuffPtr += sizeof(uint8_t);
          initial_RxXferCount = hspi->RxXferCount;

Associate III

Any suggestion would be appreciated following is the initialization code

      hspi3.Instance = SPI3;
      hspi3.Init.Mode = SPI_MODE_MASTER;
      hspi3.Init.Direction = SPI_DIRECTION_2LINES;
      hspi3.Init.DataSize = SPI_DATASIZE_8BIT;
      hspi3.Init.CLKPolarity = SPI_POLARITY_LOW;
      hspi3.Init.CLKPhase = SPI_PHASE_1EDGE;
      hspi3.Init.NSS = SPI_NSS_SOFT;
      hspi3.Init.FirstBit = SPI_FIRSTBIT_MSB;
      hspi3.Init.TIMode = SPI_TIMODE_DISABLE;
      hspi3.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
      hspi3.Init.CRCPolynomial = 0x0;
      hspi3.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
      hspi3.Init.NSSPolarity = SPI_NSS_POLARITY_LOW;
      hspi3.Init.FifoThreshold = SPI_FIFO_THRESHOLD_01DATA;
      hspi3.Init.TxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
      hspi3.Init.RxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
      hspi3.Init.MasterSSIdleness = SPI_MASTER_SS_IDLENESS_00CYCLE;
      hspi3.Init.MasterInterDataIdleness = SPI_MASTER_INTERDATA_IDLENESS_00CYCLE;
      hspi3.Init.MasterReceiverAutoSusp = SPI_MASTER_RX_AUTOSUSP_DISABLE;
      hspi3.Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_ENABLE;
      hspi3.Init.IOSwap = SPI_IO_SWAP_DISABLE;
	  hspi3.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;


Did you find a solution? I'm having the same issue. Tried stm32h7xx_hal_driver versions v1.10.0 and v1.11.1, both with the same result. 


I ran this to ground.

Interestingly, the problem is that the pins are not initialized at the proper speed. They are at speed Low by default which clearly isn't enough for the fastest SPI speeds. Changing them to Very High fixes the problem.

You can modify the setting here:


This isn't a HAL bug, but a CubeMX code generation bug. CubeMX knows the SPI data rate, so it should be setting these pins appropriately.


Also interestingly, for the F4 family, it sets SPI pins at Very High by default. So definitely an inconsistency at the least.

@Patrice LF @Amel NASRI can you look into? Not sure who to tag here.


(also just note that throwing a logic analyzer or scope on the line would have quickly showed the issue)

If you feel a post has answered your question, please click "Accept as Solution".

You're absolutely right, weird that it does not automatically choose a higher speed. Even setting to Medium worked fine for the speed I was looking for. Thank you for the response!