Strange SPI receive routine in Cube example code
Posted on March 13, 2017 at 00:37
This is supposed to receive one byte in SPI bidirectional mode ([STM32Cube_FW_L4_V1.5.0]\Drivers\BSP\STM32L476G-Discovery\stm32l476g_discovery.c):
&sharpif defined(__ICCARM__)
&sharppragma optimize=none
&sharpendif
/**
* @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 &sharppragma? 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
https://community.st.com/message/139780-re-stm32f072-spi-bidirectional-mode-strange-behavior?commentID=139780&et=watches.email.thread&sharpcomment-139780 [linkfix Feb.2019 https://community.st.com/s/question/0D50X00009XkZNhSAN/stm32f072-spi-bidirectional-mode-strange-behavior ]
and
https://community.st.com/0D50X00009XkW6HSAV
and
https://community.st.com/0D50X00009XkaArSAJ
threads?
Thanks,
Jan Waclawek
null

