Platform/ECM3532/DSP/hw/csp/ecm3532/src/eta_csp_spi.c
/***************************************************************************/
/**
 *
 * EtaCspSpiTransferPoll - Read and or write to an SPI device.
 *
 * @param pui8TxData Pointer to the data to transmit.
 * @param ui16TxLen Transmit length in bytes.
 * @param pui8RxData Point to the received data.
 * @param ui16RxLen Receive length in bytes.
 * @param spiChipSel Chip Select to use
 * @param iSpiSequence Sequence field allowing multiple functions in a single
 * SPI command
 * @return Return the status.
 *
 ******************************************************************************/
Read and or write to an SPI device.
@param pui8TxData Pointer to the data to transmit.
@param ui16TxLen Transmit length in bytes.
@param pui8RxData Point to the received data.
@param ui16RxLen Receive length in bytes.
@param spiChipSel Chip Select to use
@param iSpiSequence Sequence field allowing multiple functions in a single
SPI command
@return Return the status.
Read and or write to an SPI device . @param pui8TxData Pointer to the data to transmit . @param ui16TxLen Transmit length in bytes . @param pui8RxData Point to the received data . @param ui16RxLen Receive length in bytes . @param spiChipSel Chip Select to use @param iSpiSequence Sequence field allowing multiple functions in a single SPI command @return Return the status .
tEtaStatus
EtaCspSpiTransferPoll(uint8_t *pui8TxData, uint16_t ui16TxLen,
                      uint8_t *pui8RxData, uint16_t ui16RxLen,
                      tSpiChipSel spiChipSel, tSpiSequence iSpiSequence)
{
    tEtaStatus localStatus;
    uint32_t ui32Timeout;
    uint8_t tx_byte;
    uint8_t rx_byte;
 if (ui32RxLen > (64*1024)-1)
 etaPrintf ("ERROR: EtaCspSpiTransferPoll exceeded max ui32RxLen of
 (64*1024)-1 with %d", ui32RxLen);
     If it is first, start command
    if(iSpiSequence & eSpiSequenceFirstOnly)
    {
         Make sure previous command is done (optional)
        if(localStatus = EtaCspSpiDonePoll())
        {
            return(localStatus);
        }
         Start command
        REG_SPI2_STATUS = BM_SPI2_STATUS_START_BUSY |
                          (spiChipSel << BP_SPI2_STATUS_CS_SEL) |
                          BM_SPI2_STATUS_HOLD_CS;
    }
     If this is not the start of the command wait till
     dummy bytes registers are 0. If this is the start
     of the command these are 0 anyway.
    while(REG_SPI2_RXDROP_CNT)
    {
    }
    while(REG_SPI2_TX_DUMMY_BYTE_CNT)
    {
    }
     Deal with TX first
     We'll drop ui16TxLen of data (garbage receive data during TX)
    REG_SPI2_RXDROP_CNT = ui16TxLen;
    ui32Timeout = 10000;
    while((--ui32Timeout) && (ui16TxLen))
    {
        if(localStatus = EtaCspSpiTxReadyPoll())
        {
            return(localStatus);
        }
        tx_byte = *pui8TxData++;
        REG_SPI2_TXRX_1BYTE_FIFO_DATA = tx_byte;
        ui16TxLen--;
    }
    if(ui32Timeout == 0)
    {
        return(1);
    }
     Now deal with RX.
     Write number of dummy bytes to send to get rx data we want
    REG_SPI2_TX_DUMMY_BYTE_CNT = ui16RxLen;
     If it is Last, end command
    if(iSpiSequence & eSpiSequenceLastOnly)
    {
         At this point, all TX is in the queue. Remove hold
        REG_SPI2_STATUS = 0;
    }
    ui32Timeout = 10000;
     1 Byte version
    while((--ui32Timeout) && (ui16RxLen))
    {
        localStatus = EtaCspSpiRxDataPoll();
        if(localStatus)
        {
            return(localStatus);
        }
         pop data off the read stack
        rx_byte = REG_SPI2_TXRX_1BYTE_FIFO_DATA;
        *pui8RxData++ = rx_byte;
         If we have data to send, ignore receive data.
        ui16RxLen--;
    }
     This ui32Timeout is for general programming reasons to prevent infinite
     loop
    if(ui32Timeout == 0)
    {
        return(eEtaFailure);
    }
    return(eEtaSuccess);
}
tEtaStatus EtaCspSpiTransferPoll ( uint8_t * pui8TxData , uint16_t ui16TxLen , uint8_t * pui8RxData , uint16_t ui16RxLen , tSpiChipSel spiChipSel , tSpiSequence iSpiSequence ) { tEtaStatus localStatus ; uint32_t ui32Timeout ; uint8_t tx_byte ; uint8_t rx_byte ; if ( iSpiSequence & eSpiSequenceFirstOnly ) { if ( localStatus = EtaCspSpiDonePoll ( ) ) { return ( localStatus ) ; } REG_SPI2_STATUS = BM_SPI2_STATUS_START_BUSY | ( spiChipSel << BP_SPI2_STATUS_CS_SEL ) | BM_SPI2_STATUS_HOLD_CS ; } while ( REG_SPI2_RXDROP_CNT ) { } while ( REG_SPI2_TX_DUMMY_BYTE_CNT ) { } REG_SPI2_RXDROP_CNT = ui16TxLen ; ui32Timeout = 10000 ; while ( ( -- ui32Timeout ) && ( ui16TxLen ) ) { if ( localStatus = EtaCspSpiTxReadyPoll ( ) ) { return ( localStatus ) ; } tx_byte = * pui8TxData ++ ; REG_SPI2_TXRX_1BYTE_FIFO_DATA = tx_byte ; ui16TxLen -- ; } if ( ui32Timeout == 0 ) { return ( 1 ) ; } REG_SPI2_TX_DUMMY_BYTE_CNT = ui16RxLen ; if ( iSpiSequence & eSpiSequenceLastOnly ) { REG_SPI2_STATUS = 0 ; } ui32Timeout = 10000 ; while ( ( -- ui32Timeout ) && ( ui16RxLen ) ) { localStatus = EtaCspSpiRxDataPoll ( ) ; if ( localStatus ) { return ( localStatus ) ; } rx_byte = REG_SPI2_TXRX_1BYTE_FIFO_DATA ; * pui8RxData ++ = rx_byte ; ui16RxLen -- ; } if ( ui32Timeout == 0 ) { return ( eEtaFailure ) ; } return ( eEtaSuccess ) ; }
EtaCspSpiTransferPoll - Read and or write to an SPI device.
EtaCspSpiTransferPoll - Read and or write to an SPI device .
// Impossible if we use 16 bit lengths... // but keep incase we standardize to 32 bit lengths. // if (ui32TxLen > (64*1024)-1) // { // #ifdef ETA_CSP_SPI_DEBUG // etaPrintf ("ERROR: EtaCspSpiTransferPoll exceeded max ui32TxLen of // (64*1024)-1 with %d", ui32TxLen); // #endif // return eEtaSpiInvalidConfig; // } // // if (ui32RxLen > (64*1024)-1) // { // #ifdef ETA_CSP_SPI_DEBUG // etaPrintf ("ERROR: EtaCspSpiTransferPoll exceeded max ui32RxLen of // (64*1024)-1 with %d", ui32RxLen); // #endif // return eEtaSpiInvalidConfig; // } // If it is first, start command // Make sure previous command is done (optional) // Start command // // If this is not the start of the command wait till // dummy bytes registers are 0. If this is the start // of the command these are 0 anyway. // // Deal with TX first // We'll drop ui16TxLen of data (garbage receive data during TX) // Now deal with RX. // Write number of dummy bytes to send to get rx data we want // If it is Last, end command // At this point, all TX is in the queue. Remove hold // 1 Byte version // pop data off the read stack // If we have data to send, ignore receive data. // This ui32Timeout is for general programming reasons to prevent infinite // loop