Arduino_Core_STM32 icon indicating copy to clipboard operation
Arduino_Core_STM32 copied to clipboard

Patch, SPI.transfer16()

Open drmcnelson opened this issue 1 year ago • 4 comments

Implement SPI.transfer16() as a true 16 bit transfer

drmcnelson avatar Sep 23 '24 12:09 drmcnelson

Why did you removed defined(STM32U0xx) ?

uzi18 avatar Sep 23 '24 20:09 uzi18

Actually, I did not remove it either.

It was not in my version of that file. I will make the correction.

drmcnelson avatar Sep 24 '24 12:09 drmcnelson

Hi @drmcnelson Sorry for the delay looking at this PR.

In fact there is a mistake. Using LL_SPI_TransmitData16 is not enough to properly transfer 16bit. https://github.com/stm32duino/Arduino_Core_STM32/blob/c1ee060cc54ea4909ad20aad13b8f8939ebf96e3/libraries/SPI/src/utility/spi_com.c#L310

It requires to configure the data size to SPI_DATASIZE_16BIT.

So it should require to manage it. Several way:

  1. Adding below code in spi_transfer16 (same for the spi_transfer with LL_SPI_DATAWIDTH_8BIT
  if (LL_SPI_GetDataWidth(_SPI) != LL_SPI_DATAWIDTH_16BIT) {
    /* Change data width to 16 bit */
    LL_SPI_Disable(_SPI);
    LL_SPI_SetDataWidth(_SPI, LL_SPI_DATAWIDTH_16BIT);
    LL_SPI_Enable(_SPI);
  }

This add at least 1 check per transfer.

  1. Kept transfer16 as it is and add a 2 new API, one to change the size and one to call LL_SPI_TransmitData16.

  2. Add an API to change the data size before calling the transfer16 but this will break all Arduino sketches.

  3. maybe other way...

fpistm avatar Oct 21 '25 14:10 fpistm

@fpistm Wonderful, thank you, and thank for you looking at it.

How about a variable transfer size? Some ADCs are 18 bit (or larger) and some are daisy-chainable, so that transfers could be several times 18 bits (or larger). I have been doing this by hand for an acoustic imaging array, but with the new world of 32 bit processors and higher precision preripherals, maybe it's time to upgrade the SPI interface for everybody.

drmcnelson avatar Oct 21 '25 16:10 drmcnelson