2024-09-24 04:42 AM
I am getting a warning during compile of an incompatible pointer type when I call the SPI TransmitReceive function as shown below.
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.
.
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?
2024-09-24 05:23 AM
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.
2024-09-24 07:00 AM
@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".
2024-09-24 07:25 AM
Maybe even cast to void* right in the invocation
HAL_SPI_TransmitReceive(adc->_spiHandle, (void*) txBuf, (void*) rxBuf, ...)