cancel
Showing results for 
Search instead for 
Did you mean: 

F4 Series SPI HAL Data Size Bug

Enforcer83
Associate

I am getting a warning during compile of an incompatible pointer type when I call the SPI TransmitReceive function as shown below.Compiler warningCompiler warning

I am passing the function an unsigned 16 bit integer array.  I have configured the SPI to use 16 data bits, as shown below in CubeMX.

.STM32CubeMX_SPI.png

While digging into the HAL functions, I noticed that all the Transmit and Receive functions were expecting a pointer to an unsigned 8 bit value, shown below.  If the SPI peripheral is suppose to be able to accept either 8 or 16 data bits, wouldn't the expected type be the maximum size it can accept or two different sets of functions for either 8 data bits or 16 data bits?

STM32CubeIDE F4 SPI_HAL.png

 

3 REPLIES 3
TDK
Guru

Cast the pointer to uint8_t* when you pass it:

HAL_SPI_TransmitReceive(adc->_spiHandle, (uint8_t*) txBuf, (uint8_t*) rxBuf, 8, HAL_MAX_DELAY);

 

There's no way to accept two different types as an argument in C. They could have made it a void pointer, but that has its own issues.

If you feel a post has answered your question, please click "Accept as Solution".

@Enforcer83 wrote:

While digging into the HAL functions, ...


You need to dig a little deeper.

You'll see that, inside the function, it checks whether the SPI is configured for 8 or 16 bits, and behaves accordingly

 


@Enforcer83 wrote:

wouldn't the expected type be the maximum size it can accept


No - then it wouldn't work for 8-bit data (it would skip every other byte).

16-bit data is just pairs of 8-bit bytes - so uint8_t is used as the "lowest common denominator".

Pavel A.
Evangelist III

Maybe even cast to void* right in the invocation

HAL_SPI_TransmitReceive(adc->_spiHandle, (void*) txBuf, (void*) rxBuf, ...)