2

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.

Bence Kaulics
  • 7,066
  • 7
  • 33
  • 63
Alithewise
  • 25
  • 1
  • 1
  • 7

1 Answers1

4

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.

Guillaume Michel
  • 1,189
  • 8
  • 14
  • 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 – Alithewise Aug 24 '17 at 08:58
  • 1
    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) – Guillaume Michel Aug 24 '17 at 09:54
  • 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. – Alithewise Aug 24 '17 at 20:24