Skip to main content
typo
Source Link
HeyYO
  • 744
  • 5
  • 12

I think you misunderstood the functionality of HAL_SPI_TransmitReceive function. It doesn't first transmit tx buffer then receive to rx buffer. It does both at the same time. After all SPI is a full duplex protocol. It can transmit (from MOSI) and receive (MISO) at the same time.

To achieve thatwhat you want in a single function call you should use buffers of size 2. Like this:

    HAL_SPI_TransmitReceive(&hspi1, &tx, &rx, 2, 10000);

It doesn't matter what is in the second byte of TX buffer (tx[1]) or what you read in the first byte of RX buffer (rx[0]). Just make sure you put correct data to tx[0] and take received data from rx[1] and ignore the rest.

PS: I haven't checked the datasheet of the slave device you mentioned. Some devices start sending data at the very first clock cycle. Make sure you understand protocol correctly.

I think you misunderstood the functionality of HAL_SPI_TransmitReceive function. It doesn't first transmit tx buffer then receive to rx buffer. It does both at the same time. After all SPI is a full duplex protocol. It can transmit (from MOSI) and receive (MISO) at the same time.

To achieve that you want in a single function call you should use buffers of size 2. Like this:

    HAL_SPI_TransmitReceive(&hspi1, &tx, &rx, 2, 10000);

It doesn't matter what is in the second byte of TX buffer (tx[1]) or what you read in the first byte of RX buffer (rx[0]). Just make sure you put correct data to tx[0] and take received data from rx[1] and ignore the rest.

PS: I haven't checked the datasheet of the slave device you mentioned. Some devices start sending data at the very first clock cycle. Make sure you understand protocol correctly.

I think you misunderstood the functionality of HAL_SPI_TransmitReceive function. It doesn't first transmit tx buffer then receive to rx buffer. It does both at the same time. After all SPI is a full duplex protocol. It can transmit (from MOSI) and receive (MISO) at the same time.

To achieve what you want in a single function call you should use buffers of size 2. Like this:

    HAL_SPI_TransmitReceive(&hspi1, &tx, &rx, 2, 10000);

It doesn't matter what is in the second byte of TX buffer (tx[1]) or what you read in the first byte of RX buffer (rx[0]). Just make sure you put correct data to tx[0] and take received data from rx[1] and ignore the rest.

PS: I haven't checked the datasheet of the slave device you mentioned. Some devices start sending data at the very first clock cycle. Make sure you understand protocol correctly.

Source Link
HeyYO
  • 744
  • 5
  • 12

I think you misunderstood the functionality of HAL_SPI_TransmitReceive function. It doesn't first transmit tx buffer then receive to rx buffer. It does both at the same time. After all SPI is a full duplex protocol. It can transmit (from MOSI) and receive (MISO) at the same time.

To achieve that you want in a single function call you should use buffers of size 2. Like this:

    HAL_SPI_TransmitReceive(&hspi1, &tx, &rx, 2, 10000);

It doesn't matter what is in the second byte of TX buffer (tx[1]) or what you read in the first byte of RX buffer (rx[0]). Just make sure you put correct data to tx[0] and take received data from rx[1] and ignore the rest.

PS: I haven't checked the datasheet of the slave device you mentioned. Some devices start sending data at the very first clock cycle. Make sure you understand protocol correctly.