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.

C6657 ZYNQ7020 SPI problem

Hello,guys.
We met a problem when debug C6657 and Zynq7020 by SPI.
The test project is based on SPI.pjt from STK_C6657 downloaded from this forum.
C6657MySpi_20160801.rar
We modify the configuration as follow:

/*data format for loopback test*/
SPI_Data_Format meDataFormat =
{
    /*.delayBetweenTrans_ns = */0,
    /*.ShifDirection        = */SPI_MSB_SHIFT_FIRST,
    /*.disable_CS_timing    = */1,
    /*.clockPolarity        = */0,
    /*.clockPhase           = */1,
    /*.clockSpeedKHz        = */1000,
    /*.wordLength           = */8
};

SPI_Transfer_Param meTransferParam =
{
    0,     /*Chip select number*/
    3,  /*select one of the 4 SPI formats*/
    SPI_CS_NO_HOLD,//fl //SPI_CS_NO_LAST_HOLD, /*hold CS between multiple words*/
    FALSE,  /*Enable the delay counter at the end of the current transaction*/
    1   /*number of bytes per SPI word*/
};

DSP can send data to ZYNQ. Code as below:

/*Since SPI TX and RX shares same clock and control signals, SPI TX and RX
actually happens at the same time even you only want to TX or RX. But,
application may only care for TX or RX, or part of the TX and RX words.
This function only TX "numTxByte" valid data from the "firstTxByte",
the valid data are from the "txBuf";
only RX "numRxByte" valid data from the "firstRxByte",
the valid data are stored to the "rxBuf".
Return number of successful words*/
/// only transfer data from txBuf[firstTxByte] to txBuf[firstTxByte+numTxByte] // fl, 2016-08-01
Uint32 KeyStone_SPI_TxRx_me(Uint8 * txBuf, Uint32 firstTxByte,
    Uint32 numTxByte, Uint8 * rxBuf, Uint32 firstRxByte, Uint32 numRxByte,
    SPI_Transfer_Param * transfer)
{
    Int32 i;
    Int32 length;
    Uint32 txData, rxData;
    Uint32 byteOfWord, dataFormat, lastDataFormat;

    Uint8 *tempTxBuf=txBuf+firstTxByte; // fl
    Uint8 *tempRxBuf=rxBuf+firstRxByte; // fl

    //spiRegs

    /*total transfer word length is the max of the lastRxWord and lastTxWord*/
    /* // fl
    if((firstTxByte+numTxByte)>(firstRxByte+numRxByte))
        length= (firstTxByte+numTxByte);
    else
        length= (firstRxByte+numRxByte);
    */

    /*------data format: higher 16 bit of SPIDAT1------*/
    KeyStone_SPIDAT1_format(transfer, &dataFormat, &lastDataFormat);

    byteOfWord= transfer->byteOfWord;

    /// fl, 2016-08-01
    if(byteOfWord==1)
    {
        for(i=0; i<numTxByte-byteOfWord; i+=byteOfWord)
        {
            txData= *tempTxBuf;
            txData |= dataFormat;

            /* Wait for room in TX buffer */
            if(0==KeyStone_SPI_wait_flag(CSL_SPI_SPIFLG_TXINTFLG_MASK, CSL_SPI_SPIFLG_TXINTFLG_MASK))
                return 0;

            /* Send the word */
            spiRegs->SPIDAT1 = txData;

            /* Wait for data in RX buffer */
            if(0==KeyStone_SPI_wait_flag(CSL_SPI_SPIFLG_RXINTFLG_MASK, CSL_SPI_SPIFLG_RXINTFLG_MASK))
                return i;

            /* Read the next word */
            rxData = spiRegs->SPIBUF&0xFFFF;

            *tempRxBuf = rxData;

            tempTxBuf+=byteOfWord;
            tempRxBuf+=byteOfWord;
        }
        { // the last data
            txData= *tempTxBuf;
            txData |= lastDataFormat;

            /* Wait for room in TX buffer */
            if(0==KeyStone_SPI_wait_flag(CSL_SPI_SPIFLG_TXINTFLG_MASK, CSL_SPI_SPIFLG_TXINTFLG_MASK))
                return 0;

            /* Send the word */
            spiRegs->SPIDAT1 = txData;

            /* Wait for data in RX buffer */
            if(0==KeyStone_SPI_wait_flag(CSL_SPI_SPIFLG_RXINTFLG_MASK, CSL_SPI_SPIFLG_RXINTFLG_MASK))
                return i;

            /* Read the next word */
            rxData = spiRegs->SPIBUF&0xFFFF;

            *tempRxBuf = rxData;

            tempTxBuf+=byteOfWord;
            tempRxBuf+=byteOfWord;
        }

        return numTxByte;
    }
    else //(byteOfWord==2)
    {
        for(i=0; i<numTxByte-byteOfWord; i+=byteOfWord)
        {
            txData= *(Uint16 *)tempTxBuf;
            txData |= dataFormat;

            /* Wait for room in TX buffer */
            if(0==KeyStone_SPI_wait_flag(CSL_SPI_SPIFLG_TXINTFLG_MASK, CSL_SPI_SPIFLG_TXINTFLG_MASK))
                return 0;

            /* Send the word */
            spiRegs->SPIDAT1 = txData;

            /* Wait for data in RX buffer */
            if(0==KeyStone_SPI_wait_flag(CSL_SPI_SPIFLG_RXINTFLG_MASK, CSL_SPI_SPIFLG_RXINTFLG_MASK))
                return i;

            /* Read the next word */
            rxData = spiRegs->SPIBUF&0xFFFF;

            *(Uint16 *)tempRxBuf = rxData;

            tempTxBuf+=byteOfWord;
            tempRxBuf+=byteOfWord;
        }
        { // the last data
            txData= *(Uint16 *)tempTxBuf;
            txData |= lastDataFormat;

            /* Wait for room in TX buffer */
            if(0==KeyStone_SPI_wait_flag(CSL_SPI_SPIFLG_TXINTFLG_MASK, CSL_SPI_SPIFLG_TXINTFLG_MASK))
                return 0;

            /* Send the word */
            spiRegs->SPIDAT1 = txData;

            /* Wait for data in RX buffer */
            if(0==KeyStone_SPI_wait_flag(CSL_SPI_SPIFLG_RXINTFLG_MASK, CSL_SPI_SPIFLG_RXINTFLG_MASK))
                return i;

            /* Read the next word */
            rxData = spiRegs->SPIBUF&0xFFFF;

            *(Uint16 *)tempRxBuf = rxData;

            tempTxBuf+=byteOfWord;
            tempRxBuf+=byteOfWord;
        }

        return numTxByte;
    }
}


And DSP receive function is as below:

/// SPI DSP receive test (fl, 2016-08-03)
Uint32 KeyStone_SPI_Rx_me(Uint8 * txBuf, Uint32 firstTxByte,
    Uint32 numTxByte, Uint8 * rxBuf, Uint32 firstRxByte, Uint32 numRxByte,
    SPI_Transfer_Param * transfer)
{
    Int32 i;
    Int32 length;
    Uint32 txData, rxData;
    Uint32 byteOfWord, dataFormat, lastDataFormat;

    Uint8 *tempTxBuf=txBuf+firstTxByte; // fl
    Uint8 *tempRxBuf=rxBuf+firstRxByte; // fl

    //spiRegs

    /*total transfer word length is the max of the lastRxWord and lastTxWord*/
    /* // fl
    if((firstTxByte+numTxByte)>(firstRxByte+numRxByte))
        length= (firstTxByte+numTxByte);
    else
        length= (firstRxByte+numRxByte);
    */

    /*------data format: higher 16 bit of SPIDAT1------*/
    KeyStone_SPIDAT1_format(transfer, &dataFormat, &lastDataFormat);

    byteOfWord= transfer->byteOfWord;

    /// fl, 2016-08-01
    if(byteOfWord==1)
    {
        for(i=0; i<numRxByte; i+=byteOfWord)
        {
            txData = 0x12;
            txData |= dataFormat;

            /* Wait for room in TX buffer */
            if(0==KeyStone_SPI_wait_flag(CSL_SPI_SPIFLG_TXINTFLG_MASK, CSL_SPI_SPIFLG_TXINTFLG_MASK))
                return 0;

            /* Send the word */
            spiRegs->SPIDAT1 = txData;


            /* Wait for data in RX buffer */
            //if(0==KeyStone_SPI_wait_flag(CSL_SPI_SPIFLG_RXINTFLG_MASK, CSL_SPI_SPIFLG_RXINTFLG_MASK))
            //    return i;
            /// wait forever
            while(0==KeyStone_SPI_wait_flag(CSL_SPI_SPIFLG_RXINTFLG_MASK, CSL_SPI_SPIFLG_RXINTFLG_MASK));

            /* Read the next word */
            rxData = spiRegs->SPIBUF&0xFFFF;
            *tempRxBuf = rxData;
            tempRxBuf+=byteOfWord;
        }

        return numRxByte;
    }
    else //(byteOfWord==2)
    {
        for(i=0; i<numRxByte; i+=byteOfWord)
        {
            txData = 0x12;
            txData |= dataFormat;

            /* Wait for room in TX buffer */
            if(0==KeyStone_SPI_wait_flag(CSL_SPI_SPIFLG_TXINTFLG_MASK, CSL_SPI_SPIFLG_TXINTFLG_MASK))
                return 0;

            /* Send the word */
            spiRegs->SPIDAT1 = txData;


            /* Wait for data in RX buffer */
            //if(0==KeyStone_SPI_wait_flag(CSL_SPI_SPIFLG_RXINTFLG_MASK, CSL_SPI_SPIFLG_RXINTFLG_MASK))
            //    return i;
            /// wait forever
            while(0==KeyStone_SPI_wait_flag(CSL_SPI_SPIFLG_RXINTFLG_MASK, CSL_SPI_SPIFLG_RXINTFLG_MASK));

            /* Read the next word */
            rxData = spiRegs->SPIBUF&0xFFFF;
            *(Uint16 *)tempRxBuf = rxData;
            tempRxBuf+=byteOfWord;
        }

        return numRxByte;
    }
}

We can't receive the right data.
For example, when ZYNQ writes a 0xAA(10101010b), while DSP receives 0x52(01010010b)

Please help, thanks!

see SPI_me_test() function in the attached C6657MySpi_20160801.pjt