cancel
Showing results for 
Search instead for 
Did you mean: 

How to cancel HAL_SPI_TransmitReceive

pass3master
Senior

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?

3 REPLIES 3
TDK
Guru

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.

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

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?

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)

 

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