2022-03-04 08:27 PM
I am coding STM32H743 MCU with CubeMX generated library.
I have 1 SPI Master and 1 Slave. Both configured for a full SPI with both lines of Tx and Rx.
Master is calling blocking HAL_SPI_Transmit() or HAL_SPI_Receive() but never simultaneous HAL_SPI_TransmitReceive() . Yet the library converts any call to the HAL_SPI_TransmitReceive(). Slave configured in non-blocking IT mode to catch any direction transmission.
Does this mean that either one-directional transaction from Master will always trigger both Slave's callbacks HAL_SPI_TxCpltCallback() and HAL_SPI_RxCpltCallback() as well ?
In other words if Master Transmits only, then Slave not only receives it but also completes transmission of the same back to Master over the shifting loop? So both catchup Callbacks are triggered in the Slave? Meaning the Slave side cannot distinguish Master's Transmit-only command from Master's TransmitReceive command?
Am I missing something ?
Thanks in advance to anyone for clarification .
Solved! Go to Solution.
2022-03-05 03:51 AM
The callback will be based on what call is performed on the slave side.
HAL_SPI_Transmit_IT -> HAL_SPI_TxCpltCallback
HAL_SPI_Receive_IT -> HAL_SPI_RxCpltCallback
HAL_SPI_TransmitReceive_IT -> HAL_SPI_TxRxCpltCallback
I recommend always using TransmitReceive for both sides.
2022-03-04 10:55 PM
In a full-duplex, single master + single slave SPI setup, a transfer will effectively swap the data between master and slave. So, yes: the slave cannot distinguish a master HAL_SPI_Transmit() from a HAL_SPI_TransmitReceive(). This holds independently of HAL implementation, just by looking at the wires: MOSI and MISO share the same SCLK.
If you don't call the bidir TransmitReceive functions, dummy data will be used. Thinks that HAL will not call the CpltCallback you haven't asked for, but I'm not sure. But, this is easy to check by debug-stepping into the source code and setting few breakpoints.
hth
KnarfB
2022-03-05 03:51 AM
The callback will be based on what call is performed on the slave side.
HAL_SPI_Transmit_IT -> HAL_SPI_TxCpltCallback
HAL_SPI_Receive_IT -> HAL_SPI_RxCpltCallback
HAL_SPI_TransmitReceive_IT -> HAL_SPI_TxRxCpltCallback
I recommend always using TransmitReceive for both sides.
2022-03-06 01:19 PM
Thank you both Knarf and TDK , very helpful advises. I confirmed while debugging that each directional type of SPI calls will trigger only the respective Callback but not the other even though both lines Tx and Rx are connected and SCLK is shifting data on both lines. This boils down to the importance of maintaining the State-Event machine in the Slave to prepare a relevant transmission when Master's clock start ticking ... Now I realized the advantage of using HAL_SPI_TransmitReceive_IT() for my Slave as TDK recommended.
Thank you'll again