STM32 HAL SPI 16 bit Transmit
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.
![Alithewise](https://i.stack.imgur.com/ZBCtu.jpg?s=256&g=1)
Comments
-
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 almost 7 yearsThe 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 almost 7 yearsI 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 almost 7 yearsThanks 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.