Skip to main content
Visitor II
April 18, 2024
Solved

How to abort SPI Slave when Master requests for less bytes?

  • April 18, 2024
  • 2 replies
  • 2133 views

Hi,

I have my master and slave applications running on NUCLEO-H563ZI Boards both of which normally receive/transmit  10 bytes of data in each SPI transfer. I use SPI in DMA mode.

However, in one test case, master starts a SPI transaction for only 4 bytes i.e it calls HAL_SPI_TransmitReceive_DMA with Size as 4. So, my master sends clock for only 4 bytes even though the slave has 10 bytes of data. At this point, the Master receives 4 bytes but the HAL_SPI_TxRxCpltCallback on Slave is not called.

I am doing a GPIO Pin toggle in the callback to denote completion of the SPI transfer which is necessary for my slave application. How do I abort the SPI communication in this scenario?

I see there's a HAL_SPI_Abort_IT. Not sure how to use it. Appreciate if you would provide some insight on this.

    This topic has been closed for replies.
    Best answer by Andrew Neil

    But it is how you'd synchronise the Slave to the Master - if you choose to omit it, then you have to devise some other sync scheme.

    The fundamental issue with omitting NSS is, exactly, that you lose this synchronisation!

    2 replies

    Super User
    April 18, 2024

    Hi,

    why not always TransmitReceive 10 bytes (even if only 2 or 4 used, others 0 or FF) ?

    Then no problem with "unknown" size .

    WCareyAuthor
    Visitor II
    April 18, 2024

    My test case is for this specific scenario:

    Slave transmits 10 bytes but Master should send clock only for 4 bytes. In this case I expect, master to receive the requested 4 bytes and spi communication b/w them to be aborted. And, in both Master and Slave callbacks, I have a GPIO Toggle to indicate SPI transfer completion.  

    However, what is happening is master receives 4 bytes and master's gpio toggles but on slave, the callback is not hit and gpio is not toggled.

    How should I abort my slave in a case where master sends less than required clock pulses?

    Super User
    April 18, 2024

    How the slave should know : now only 4 byte coming ?

     

    Maybe you could use just the timeout :

    HAL_SPI_Receive(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout)

    set to 10 ms or so, then you get 10 bytes - or timeout , then you (slave) check, what you got.

    WCareyAuthor
    Visitor II
    April 19, 2024

    No, this is a blocking API and is not suited to my application.