SPI as slave interface hanging after 2 to 3 minutes.

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

SPI as slave interface hanging after 2 to 3 minutes.

1,738 Views
vijith_g1
Contributor II

Hi

I am using  MKE02Z32VFM4 microcontroller for the development of project. I have to use spi interface as master  for accessing data from an ADC. Another spi as slave to receive commands from the master. Currently I am facing an issue i.e. After 2 or 3 minutes slave spi getting hanged on SPI_IsSPRF. My configuration for SPI 1

SPI_ConfigType sSPIConfig1 = {0};

/* initialize SPI1 as slave */
  
    sSPIConfig1.u32BitRate                      = 1000000;
    sSPIConfig1.u32BusClkHz                     = BUS_CLK_HZ;
    sSPIConfig1.sSettings.bShiftLSBFirst        = 0;
    sSPIConfig1.sSettings.bMasterMode           = 0;
    sSPIConfig1.sSettings.bClkPolarityLow       = 1;
    sSPIConfig1.sSettings.bClkPhase1            = 1;
    sSPIConfig1.sSettings.bMasterAutoDriveSS    = 0;
    sSPIConfig1.sSettings.bIntEn                = 1;
    sSPIConfig1.sSettings.bModuleEn             = 1;
    sSPIConfig1.sSettings.bMatchIntEn           = 0; 
    //SPI_ModfEnable(SPI1);
   // SPI_SSOutputDisable(SPI1);
    SPI_Init(SPI1, &sSPIConfig1);
    SPI_SetCallback(SPI1,SPI_Task);

Master SPI spi0 works fine. But slaves causes problem for me.

in interrupt service routine  called this function.

ResultType SPI_Transfer_Wait(SPI_Type *pSPI, SPI_WidthType *pRdBuff, SPI_WidthType *pWrBuff,uint32 uiLength)
{
    ResultType err = SPI_ERR_SUCCESS;
    uint32_t  i;
    if(!uiLength)
    {
        return (err);
    }
   
    for(i = 0; i < uiLength; i++)
    {  
     while(!SPI_IsSPTEF(pSPI))
        {
          if(iCount>=1)
          {
            iCount=0;
            goto SPS;
          }
        }
        SPI_WriteDataReg(pSPI,pWrBuff[i]);
SPS:       
      while(!SPI_IsSPRF(pSPI))
        {
          if(iCount>=1)
          {
             iCount=0;
             goto SOS;
          }
        }  
        pRdBuff[i] = SPI_ReadDataReg(pSPI);   
SOS:            
    }
    
   
    return (err);       
}

Please let me know what change I have to make.

0 Kudos
Reply
5 Replies

1,500 Views
vijith_g1
Contributor II

Hi,

I resolved the hanging issue. Thank you.

But now  I am facing another issue. As a slave  when I was trying to send 4 bytes of data , in master side I was getting only three bytes of data.

For ex :

I was sending from slave :  01 02 03 04

Master receives data as :  00 01 02 03 

After the second read  I will get  in master side as 04 01 02 03.

There is default data 0x00 available in SPI Tx buffer .  when the data arrives from the master , the slave will send the default data ie 0x00.

After that the SPI slave  will send the rest of the data.

Please let me know how will I resolve this issue

0 Kudos
Reply

1,500 Views
Alexis_A
NXP TechSupport
NXP TechSupport

Hi vijith.g@honeywell.com‌,

When the buffer in SPI is empty you will need a dummy byte at the start of your transmission, if you check the driver from the SDK this is implemented in the function SPI_WriteNonBlocking:

while (i < size)
{
    if (buffer != NULL)
    {
#if defined(FSL_FEATURE_SPI_16BIT_TRANSFERS) && FSL_FEATURE_SPI_16BIT_TRANSFERS
        /*16 bit mode*/
        if (base->C2 & SPI_C2_SPIMODE_MASK)
        {
            base->DL = *buffer++;
            base->DH = *buffer++;
        }
        /* 8 bit mode */
        else
        {
            base->DL = *buffer++;
        }
#else
        base->D   = *buffer++;
#endif /* FSL_FEATURE_SPI_16BIT_TRANSFERS */
    }
    /* Send dummy data */
    else
    {
        SPI_WriteData(base, ((uint32_t)g_spiDummyData[instance] << 8 | g_spiDummyData[instance]));
    }
    i += bytesPerFrame;
}

First check if the buffer is empty and if this is the case, it will send a dummy byte.

I will suggest to check the SPI driver from the SDK for more information.

Best Regards,

Alexis Andalon

0 Kudos
Reply

1,500 Views
vijith_g1
Contributor II

Hi,

Thank you for the response.

I already used above logic in my source code. But it did not help.

There is a correction in my previous post.

That is  There is a default data 0x00 available in SPI slave shift register not in SPI TX buffer.  when the data arrives from the  master , the slave will send the default data ie. 0x00. The data which intend to send will write in to the shift register after that only.

In the case of first byte, the data in the SPI shift register not updating after writing in to the SPI slave Tx buffer.

SPI slave Tx Buffer[Write]

      \|/

SPI  slave Shift register    -- >   to master SPI

      \|/

SPI slave Rx buffer [Read]

I want a solution for this issue. Please let me know.

0 Kudos
Reply

1,500 Views
Alexis_A
NXP TechSupport
NXP TechSupport

Hi vijith,

If I understand correctly your statement is right, the default value in the slave Shift register is 0, so you will need to do a dummy send to push this value every time the buffer is empty. That's why you see the last value from the first transmission in your second transmission.

I will suggest to check the SDK example.

Best Regards,

Alexis Andalon

0 Kudos
Reply

1,500 Views
Alexis_A
NXP TechSupport
NXP TechSupport

Dear vijith.g@honeywell.com‌,

At first glance your configuration looks good, I think that your implementing the SPI using polling, I will suggest to check the SDK example from this board, there's an example of how to do it through interruptions, this is very useful if you want to do other operations while its waiting to receive something.

The SDK can be downloaded from the next link:

Welcome | MCUXpresso SDK Builder 

Also, could you check if the clock line is in his idle state? If this is not the case its posible that the slave is the reason of this issue. 

Let me know if this helps you.

Best Regards,

Alexis Andalon

0 Kudos
Reply