AnsweredAssumed Answered

STM32F091 : SPI polling issue !

Question asked by camillehad on Nov 17, 2015
Hello all,

I'm concerned about the SPI communication of my STM32F091 micro, since I have got really stuck for a while now. I am using Cube to initialize my software, and HAL libraries to write the code.

When I try to send a byte to an external flash memory, I poll for RXNE status in order to check if data has been received. Here is the code :

uint8_t sFLASH_SendByte (uint8_t byte)
    /*!< Loop while DR register in not emplty */
    while (__HAL_SPI_GET_FLAG (hspi, SPI_FLAG_TXE) == RESET);
    /*!< Send byte through the SPI peripheral */
    HAL_SPI_SendData8 (hspi,  byte);
    /*!< Wait to receive a byte */
    //while (__HAL_SPI_GET_FLAG (hspi, SPI_FLAG_RXNE) == RESET);
    /*!< Return the byte read from the SPI bus */
    return HAL_SPI_ReceiveData8 (hspi);

My problem is that after sending a byte, RXNE is set but, in debug mode, directly reset the line after. Therefore when I get out of the sFLASH_SendByte function, the while loop loops forever. As you can see, I have commented out the while loop so as my code can run, but I am still experiencing problems (that could totally come from another part of the SPI code, the RXNE issue being the first I confronted).

I use a custom sending function which is as follows :

HAL_StatusTypeDef HAL_SPI_SendData8 (SPI_HandleTypeDef *hspi, uint16_t Data)
    /* Process Locked */
    __HAL_LOCK (hspi);
    /* Write in the DR register the data to be sent */
    *((__IO uint8_t*)&hspi->Instance->DR) = Data;
    /* Process Unlocked */
    __HAL_UNLOCK (hspi);
    /* Return OK by default */
    return HAL_OK;

To write this function i took inspiration from various forum posts I went through but also the STM32F0xx_StdPeriph_Driver library.

Indeed, I am using CubeMx and so HAL Library. Hence I also tried sending data by means of HAL_SPI_Transmit, the result being the same (RXNE reset inside the function).

For information, my SPI settings are as follow :

/* SPI1 init function */
void MX_SPI1_Init(void)
  hspi1.Instance = SPI1;
  hspi1.Init.Mode = SPI_MODE_MASTER;
  hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
  hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
  hspi1.Init.NSS = SPI_NSS_SOFT;
  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi1.Init.TIMode = SPI_TIMODE_DISABLED;
  hspi1.Init.CRCPolynomial = 10;

My question is : shouldn't RXNE bit stay SET for a longer time, so as I can decently read it ? I have to precise that I already checked FRXTH bit, that I set so as RXNE is triggered after each byte received. The problem has remained, RXNE is directly "discarded" even though I don't explicitly read it.

I tried working with 8-bit data size, 16-bit data size, changing SPI baud rate...

Beyond that, I am actually wondering if there is not a problem with the HAL library working with F0 family.

Please do not hesitate to send your thoughts.