cstm32spistm32f4discoverystm32f4

STM32 HAL SPI 16 bit Transmit


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.


Solution

  • 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.