STM32 HAL SPI 16 bit Transmit

16,279

To remove the error, write:

HAL_SPI_Transmit(&hspi2,(uint8_t*)(DataToSend), 2,TIMEOUTSPI);

I noticed that the size of your array is 10 uint16_t DataToSend[10] (which is 20 bytes), maybe you meant:

HAL_SPI_Transmit(&hspi2,(uint8_t*)(DataToSend), 20,TIMEOUTSPI);

You should not get the alignment error mentioned in your link, as you are using the STM32F4 family.

Share:
16,279
Alithewise
Author by

Alithewise

I am a student who likes to code, just a little.

Updated on July 16, 2022

Comments

  • Alithewise
    Alithewise almost 2 years

    I am trying to use HAL_SPI_Transmit(.) for 16 bit data transmission.

    I have configured the SPI using STM32Cube as 16 bit data size

    (with hspi2.Init.DataSize = SPI_DATASIZE_16BIT).

    I tried to send data in 16 bit with:

    uint16_t DataToSend[10]={...};
    
    HAL_SPI_Transmit(&hspi2,DataToSend, 2,TIMEOUTSPI);
    

    But the function HAL_StatusTypeDef HAL_SPI_Transmit(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout) needs specifically for uint8_t*, and it returns the following error:

    error: #167: argument of type "uint16_t *" is incompatible with parameter of type "uint8_t *"

    So how can I send 16 bit data using HAL_SPI_Transmit()?

    I found this link but only the bug was discussed and not the way to use the function. So my question remains.

    I have searched the net without any luck. I am rather new to STM32 so the answer may be obvious to you.

  • Alithewise
    Alithewise almost 7 years
    The reason I have used 2 as Size parameter is that I wanted to send 2 16 bit data. Should I write 4 in that case? @Guillaume
  • Guillaume Michel
    Guillaume Michel almost 7 years
    I have just had a look at the HAL code, and it looks like the size is the number of words. So it seems that you are right, it should be 2 (to send 2 16-bit words)
  • Alithewise
    Alithewise almost 7 years
    Thanks for your answer. I tested it and it worked. But I think the function definition should be changed to support 16 bit data without type conversion.