Skip to main content
xpp07
Associate III
September 20, 2018
Question

Reading a 16-bit word via SPI

  • September 20, 2018
  • 4 replies
  • 1137 views

In order to read a 16-bit word from a current sensor, is it correct to say:

HAL_SPI_Receive(&hspi1, &SPIRx[0], 1, 1); //?

SPIRx is declared as uint16_t SPIRx[1];

I told CubeMx that the data size for SPI1 is 16 bits

    This topic has been closed for replies.

    4 replies

    Tesla DeLorean
    Guru
    September 20, 2018

    Probably,

    Try looking at the source for HAL_SPI_Receive() function, usually specific comments about parameters/usage.

    Seem to recall the length parameter being units of transfer size, so 1 for a single word.

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    xpp07
    xpp07Author
    Associate III
    September 20, 2018

    I'm confused because the definition says:

    HAL_SPI_Receive(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout),

    The second parameter uint8_t *pData, but I have the data as 16 bits. Does it affect?

    Tesla DeLorean
    Guru
    September 20, 2018

    Isn't it the same routine whether you have the SPI in 8 or 16-bit mode?

    I think (void *) would have been better for the general case, but the pointer can be recast.

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    xpp07
    xpp07Author
    Associate III
    September 20, 2018

    If I do as above, it throws this warning:

    passing argument 2 of 'HAL_SPI_Receive' from incompatible pointer type [-Wincompatible-pointer-types]

    So, the pointer has to be 8 bits. Seems like I got my wires crossed.