Skip to main content
Explorer
August 30, 2024
Solved

This question is about HAL_SPI_TransmitReceive

  • August 30, 2024
  • 1 reply
  • 731 views

This question is about HAL_SPI_TransmitReceive.
If a slave starts sending 8 bytes of data after the 4th byte sent by the master, is it possible to use HAL_SPI_TransmitReceive without a dummy (ignore) buffer?
We will be the master side.

Our assumption is that a dummy (ignore) buffer will be created.

HAL_SPI_TransmitReceive(&hspi1, sbuf, rbuf, 12, 1000)

sbuf[12] = { A, B, C, D, Dummy, Dummy, Dummy, Dummy, Dummy, Dummy, Dummy }
rbuf[12] = { Dummy, Dummy, Dummy, Dummy, A, B, C, D, E, F, G, H }
Is this perception wrong?

    This topic has been closed for replies.
    Best answer by TDK

    Both buffers need to be the total transaction length. They will not be created automatically. However, you can use the same buffer to transmit and receive, which will get you what you want.

    uint8_t buf[12] = {A, B, C, D};
    HAL_SPI_TransmitReceive(&hspi1, buf, buf, 12, HAL_MAX_DELAY)
    uint8_t x = buf[5];
    etc...

    You could also break it up into two transactions.

    uint8_t sbuf[4] = {A, B, C, D};
    HAL_SPI_Transmit(&hspi1, sbuf, 4, HAL_MAX_DELAY)
    uint8_t rbuf[8] = {0};
    HAL_SPI_Receive(&hspi1, rbuf, 8, HAL_MAX_DELAY)
    uint8_t x = rbuf[0];
    etc...

     

    1 reply

    TDKAnswer
    Super User
    August 30, 2024

    Both buffers need to be the total transaction length. They will not be created automatically. However, you can use the same buffer to transmit and receive, which will get you what you want.

    uint8_t buf[12] = {A, B, C, D};
    HAL_SPI_TransmitReceive(&hspi1, buf, buf, 12, HAL_MAX_DELAY)
    uint8_t x = buf[5];
    etc...

    You could also break it up into two transactions.

    uint8_t sbuf[4] = {A, B, C, D};
    HAL_SPI_Transmit(&hspi1, sbuf, 4, HAL_MAX_DELAY)
    uint8_t rbuf[8] = {0};
    HAL_SPI_Receive(&hspi1, rbuf, 8, HAL_MAX_DELAY)
    uint8_t x = rbuf[0];
    etc...

     

    Explorer
    August 30, 2024

    Thank you for your quick reply. It was extremely helpful.