2024-12-23 04:47 PM
We are using HAL_SPI_TransmitReceive to transmit and receive SPI data.
We would like to cancel HAL_SPI_TransmitReceive midway through. Is this possible? Also, how should We write it?
2024-12-23 05:23 PM
Not really, not by design. It's a blocking function, it doesn't return until it's done and there is no method of cancelling it halfway through.
You could possibly modify the software state machine to simulate completion in an interrupt, but it's not really designed for that and you'd have to trace all the logic. Better option would be to write your own HAL_SPI_TransmitReceive with a flag to cancel it which you check each iteration.
2024-12-23 06:22 PM
Thank you for your answer.
I see, that's right.
Is this the same idea when using HAL_SPI_TransmitReceive_IT, which is not blocking?
2024-12-23 07:34 PM
Either HAL_SPI_Abort or HAL_SPI_Abort_IT can be used to abort nonblocking (IT and DMA-based) SPI transactions.
/**
* @brief Abort ongoing transfer (blocking mode).
* @PAram hspi SPI handle.
* @note This procedure could be used for aborting any ongoing transfer (Tx and Rx),
* started in Interrupt or DMA mode.
* This procedure performs following operations :
* - Disable SPI Interrupts (depending of transfer direction)
* - Disable the DMA transfer in the peripheral register (if enabled)
* - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
* - Set handle State to READY
* @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
* @retval HAL status
*/
HAL_StatusTypeDef HAL_SPI_Abort(SPI_HandleTypeDef *hspi)