AnsweredAssumed Answered

Strange SPI receive routine in Cube example code

Question asked by waclawek.jan on Mar 13, 2017
Latest reply on Mar 23, 2017 by waclawek.jan

This is supposed to receive one byte in SPI bidirectional mode ([STM32Cube_FW_L4_V1.5.0]\Drivers\BSP\STM32L476G-Discovery\stm32l476g_discovery.c):

#if defined(__ICCARM__)
#pragma optimize=none
#endif
/**
  * @brief  Receives a Byte from the SPI bus.
  * @retval The received byte value
  */
static uint8_t SPIx_Read(void)
{
  uint8_t receivedbyte;
    __HAL_SPI_ENABLE(&SpiHandle);
    __DSB();
    __DSB();
    __DSB();
    __DSB();
    __DSB();
    __DSB();
    __DSB();
    __DSB();
     __HAL_SPI_DISABLE(&SpiHandle);

  while((SpiHandle.Instance->SR & SPI_FLAG_RXNE) != SPI_FLAG_RXNE);
  /* read the received data */
  receivedbyte = *(__IO uint8_t *)&SpiHandle.Instance->DR;
 
  /* Wait for the BSY flag reset */
  while((SpiHandle.Instance->SR & SPI_FLAG_BSY) == SPI_FLAG_BSY);
 
  return receivedbyte;
}

 

Can ST please comment on:

  • Why the #pragma? What in this code is supposed to be not-optimized-out? Would ICC optimize out the repeated __DSB()s (sounds not very plausible but one never knows, I don't have ICC)? How about other compilers?
  • Why the 8 __DSB()? Is this supposed to be a time waste? Is there something wrong with __NOP()?
  • The check for RXNE and BUSY after __HAL_SPI_DISABLE() indicates that the __DSB()s are intended to provide a way to "start issuing clocks" but disable SPI soon enough so that clocks only for one frame are issued. Correct?
  • If above point is true, what if an interrupt happens in middle of the __DSB()s?
  • Is the number of __DSB()s dependent on SPI baudrate?
  • Where is all this documented in the RM, exactly?

 

And while at commenting on STM32 SPI issues, can ST please comment also on this and this and this threads?

 

Thanks,

 

Jan Waclawek

Outcomes