cancel
Showing results for 
Search instead for 
Did you mean: 

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

WCarey
Associate III

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.

1 ACCEPTED SOLUTION

Accepted Solutions

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!

View solution in original post

7 REPLIES 7
AScha.3
Chief III

Hi,

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

Then no problem with "unknown" size .

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

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?

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.

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

Do you use the NSS (aka Chip Select) signal?

That's what should signal the slave that the Master has ended the transaction.

 

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

WCarey_0-1713501435508.png

The STM H5  manual says, SS pin is optional for single master and single slave.

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!