cancel
Showing results for 
Search instead for 
Did you mean: 

This question is about HAL_SPI_TransmitReceive

pass3master
Senior

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?

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

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...

 

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

View solution in original post

2 REPLIES 2
TDK
Guru

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...

 

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

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