This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

delay loops in CSL SPI code

SPI_read and SPI_write in the c55xx_csl code contain delay loops before polling the flags indicating that the character is complete.

What are these delays doing there, and do the arbitrary loop counts of 100 work over all possible clock speeds for the C5515?

  • Hi,

    That delay is not necessary.

    Regards,

    Hyun

  • Well actually I find that when I remove them, my SPI sniffer doesn't see valid transactions.

    I'm doing a write followed by a read in the same transaction, so I'm not quite using the CSL code exactly as written.

    Also, why does the loop check for character not complete AND busy.  Shouldn't it be OR?

    The SPI user's guide describes it as poll for complete, and then poll for not busy.

  • I am having a similar issue when I remove the for(delay = 0; delay < 100; delay++);   in the SPI_read function . (see code below). With the code removed the first character that gets read in is correct. and all subsequent characters are always 0.

    The problem appears to be in the spi device that I'm using. I hooked up a scope to look at the SPI lines and the external slave device responds with the first character after teh chip select is asserted, but it does not respond with any additional characters. My slave device is a TotalPhase Aardvark adapter that is connected to my PC.

    If the master has a delay of 13.8 usec between each character then the slave responds correctly. I have posted a question to Totalphase to get their assistance with this issue.

    The test code that I'm running is as follows:

    #define SPI_TEST_BUFLEN 32
    static unsigned char bufr[SPI_TEST_BUFLEN];

    void
    SpiTest(void) {  
      volatile Uint16 delay;
      Uint16 nBytes = 8;

       // read test
       for (;;) {
       
           for (delay; = 0 ; delay; < 10000 ; delay;++) {
             // Delay between tests
           }
           result = SPI_read(mhSpiCs0 , (Uint16*) bufr, nBytes);
       } 

     

    _____CSL   2.10 source code________

    CSL_Status SPI_read (CSL_SpiHandle hSpi,
         Uint16 *readBuffer,
         Uint16 bufLen)
    {
     Int16  getWLen;
     volatile Uint16  bufIndex;
     Int16  spiStatusReg;
     volatile Int16  spiBusyStatus;
     volatile Int16  spiWcStaus;
     volatile Uint16 delay;

     bufIndex = 0;
     if((NULL == readBuffer) || (0 == bufLen))
     {
      return (CSL_ESYS_INVPARAMS);
     }

     /* Read the word length form the register */
     getWLen = CSL_FEXT(CSL_SPI_REGS->SPICMD2, SPI_SPICMD2_CLEN) + 1;

     if(getWLen >= SPI_MAX_WORD_LEN)
     {
      return (CSL_ESYS_INVPARAMS);
     }

     /* Read Word length set by the user */
     while(bufIndex < bufLen)
     {
      /* Set command for reading buffer */
      CSL_FINS(CSL_SPI_REGS->SPICMD2, SPI_SPICMD2_CMD,
              CSL_SPI_SPICMD2_CMD_READ);

      for(delay = 0; delay < 100; delay++);

      do
      {
       spiStatusReg = CSL_SPI_REGS->SPISTAT1;
       spiBusyStatus = (spiStatusReg & CSL_SPI_SPISTAT1_BSY_MASK);
       spiWcStaus = (spiStatusReg & CSL_SPI_SPISTAT1_CC_MASK);
      }while((spiBusyStatus == CSL_SPI_SPISTAT1_BSY_BUSY) &&
        (spiWcStaus != CSL_SPI_SPISTAT1_CC_MASK));

      if(getWLen == 16)
      {
       readBuffer[bufIndex] = CSL_SPI_REGS->SPIDR1;
       bufIndex++;
      }
      else if(getWLen == 8)
      {
       readBuffer[bufIndex] = (CSL_SPI_REGS->SPIDR1 & 0xFF);
       bufIndex++;
      }
     }
     return (CSL_SOK);
    }