SPI Slave - number of received bytes

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

SPI Slave - number of received bytes

3,163 Views
peto_frnka
Contributor II

Hello,

I am new in development with Kinetis microcontroller (MK66FN2M0VMD18) and I am trying to create an application which read not specified number of bytes (SPI frame) from SPI Master using an interrupt. I was able to implement basic functionality because I went through the downloaded examples. 

void SPI0_DriverIRQHandler(void)
{
   while(DSPI_GetStatusFlags(SPI0) & kDSPI_RxFifoDrainRequestFlag)
   {   
      slaveRxData[counter++] = DSPI_ReadData(SPI0);

      DSPI_ClearStatusFlags(SPI0, kDSPI_RxFifoDrainRequestFlag);
   }
}

Slave receives valid SPI frame and stores it to the slaveRxData buffer. Comparing to the examples, interrupt is disabled and receive process is completed when number of received bytes is equal to an expected value, but there is no expected number of bytes in my application.

When should I disable interrupt and how can I detect when SPI does not receive any SPI frame ? I would expect that SPI status register SPI0_SR has busy flag.

Thanks a lot !    

Tags (2)
7 Replies

2,426 Views
JHinkle
Senior Contributor I

What I was suggesting was to enable the pin used on the slave for SPI chip select to fire an interrupt in the Slave when it is being de-asserted.

I'm not sure if your device will allow that but some will since the pin's interrupt capability is associated with the pin's state even though the pin's function is assigned to SPI Chip Select.

I've done this in the past and it works very well if the two capabilities are allowed at the same time.

2,426 Views
JHinkle
Senior Contributor I

If the Master is using the Slave's "enable" pin to encompass the "complete" message packet (not just every byte) then you can have the enable pin fire an interrupt on the edge of the enable signal when the message packet is complete.

2,426 Views
Ray_V
Contributor V

The slave's enable pin is controlled by the master (or the master's driver).

So, it still needs to know when all bytes have been retrieved so it can stop sending "dummy bytes" and de-assert the slave's enable pin.

2,426 Views
Ray_V
Contributor V

In SPI, the slave can't tell the master that it has data and can't initiate a transfer.

The master needs to clock-in any data from the slave, so if the master has no data to send, but there is data to receive, the application needs to send some "dummy" data to clock-in the data from the slave. So in essence it (the application/driver) needs to know how many bytes to retrieve.

For Example:

A DIGI 900MHz XBee module can communicate via SPI. It does have a pin indicating when it has data. This pin can be used by the application to know when to stop sending "dummy" data.

If there is no access to this pin, the XBee packet needs to be analyzed to know when the full packet is received.

But normally if the slave can have data without the master requesting it, the slave needs some way outside the SPI interface to let you know (e.g. the "Data Available" pin).

You don't give any details as to your SPI slave or why you don't know how many bytes to expect, but I would say that in your case you need to analyze the data received to know when all the bytes have been received.

Hope this helps.

2,426 Views
peto_frnka
Contributor II

Hi Raymundo Velarde,

thanks a lot for your reply ! As you wrote, it looks that it is necessary to analyze received data in slave and then process the data so I need to redesign my application.

thanks for the suggestions.

0 Kudos
Reply

2,426 Views
peto_frnka
Contributor II

Hi Kerry Zhou,

Thank you for your reply, I really appreciate it.

I understand that it is not necessary to disable the interrupt what partially answer my question. As you wrote, received data are processed in the main function but there is no "flag" containing information that receive process is complete or SPI is not busy in the SPI periphery registers. I am struggling with described questions because I am trying to process unexpected number of bytes in the following way:

static uint32_t ReceivedFramesNum;

 void ProcessRxData()

{

   while(!IsRxComlete())

   {}

   ReceivedFramesNum++;

   if(slaveRxData[0] == 0x25)

   {

      // Do something - this an example

   }

}

The function IsRxComlete() should return true if receive process is complete or SPI periphery is not busy. The problem is that SPI periphery registers do not contain any information about described state and I was not able to find any solution for that.

Thank you !

0 Kudos
Reply

2,426 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi Peter Frnka,

   If you don't have the expected number of bytes in your application, you even don't need to disable the interrupt.

   Please note, the SPI receive interrupt only happens when the SPI slave receive the data, if no data, the SPI receive interrupt won't be happen.

  So, you don't need to disable the interrupt.

  You can process the data in the main function directly.

  Wish it helps you!

If you still have question about it, please kindly let me know.


Have a great day,
Kerry

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------