6 years, 9 months ago.

[NUCLEO F446RE] Possible issue with SPI

Hi ! I've tryed to send 16 bits message with a F446RE but instead of sending a single packet of 16 clock periods between the falling edge of SS and its rising edge, it sends 2 bursts of 16 periods.

Looking in the mbed official library I've saw that line 359 of stm_api_spi.c :

extract of stm_api_spi.c

int spi_master_write(spi_t *obj, int value)
{
...
    size = (handle->Init.DataSize == SPI_DATASIZE_16BIT) ? 2 : 1;

    /*  Use 10ms timeout */
    ret = HAL_SPI_TransmitReceive(handle,(uint8_t*)&value,(uint8_t*)&Rx,size,HAL_MAX_DELAY);
...
}

Size is set to 2,

But meanwhile the init sets DFF bit of SPI_CR1 (meaning 16 bits transmition). Looking in stm32f4xx_hal_spi.c you can see that you have a "while" statement involving Size :

extract of stm32f4xx_hal_spi.c

  hspi->RxXferCount = Size;
  hspi->RxXferSize  = Size;
...
  hspi->TxXferCount = Size;
  hspi->TxXferSize  = Size;
...
    while ((hspi->TxXferCount > 0U) || (hspi->RxXferCount > 0U))

So just modify the code by swapping 1 and 2... Like this :

extract of stm_api_spi.c

int spi_master_write(spi_t *obj, int value)
{
...
    size = (handle->Init.DataSize == SPI_DATASIZE_16BIT) ? 1 : 2;

    /*  Use 10ms timeout */
    ret = HAL_SPI_TransmitReceive(handle,(uint8_t*)&value,(uint8_t*)&Rx,size,HAL_MAX_DELAY);
...
}

1 Answer

6 years, 8 months ago.

I have been seeing the same thing on an F401RE and have been struggling with it all day. Your solution works very well. Thank you for that.