Platform/ECM3532/M3/hw/csp/ecm3532/m3/csp/src/eta_csp_i2c.c
/***************************************************************************/
/**
 *
 * EtaCspI2cTransferPoll - Read and or write to an I2C device.
 *
 * @param iNum I2C instance number
 * @param pui8TxData Pointer to the data to transmit.
 * @param ui32TxLen Transmit length in bytes.
 * @param pui8RxData Point to the received data.
 * @param ui32RxLen Receive length in bytes.
 * @param ui8RestartAfterByte Perform a repeated start after this mayn bytes.
 * @return Return the status.
 *
 ******************************************************************************/
Read and or write to an I2C device.
@param iNum I2C instance number
@param pui8TxData Pointer to the data to transmit.
@param ui32TxLen Transmit length in bytes.
@param pui8RxData Point to the received data.
@param ui32RxLen Receive length in bytes.
@param ui8RestartAfterByte Perform a repeated start after this mayn bytes.
@return Return the status.
Read and or write to an I2C device . @param iNum I2C instance number @param pui8TxData Pointer to the data to transmit . @param ui32TxLen Transmit length in bytes . @param pui8RxData Point to the received data . @param ui32RxLen Receive length in bytes . @param ui8RestartAfterByte Perform a repeated start after this mayn bytes . @return Return the status .
tEtaStatus
EtaCspI2cTransferPoll(tI2cNum iNum, uint8_t *pui8TxData, uint32_t ui32TxLen,
                      uint8_t *pui8RxData, uint32_t ui32RxLen,
                      uint32_t ui8RestartAfterByte)
{
    uint32_t ui32ByteCount, ui32Timeout;
    uint8_t ui8TxByte, ui8RxByte;
    tEtaStatus localI2cStatus;
     Check that the command is acceptable.
    if(ui32TxLen == 0)
    {
#ifdef ETA_CSP_I2C_DEBUG
        ETA_DIAGS_PRINTF("ERROR: i2c_transfer_poll, inst %d, no data " \
                          "(ui32TxLen %d)\r\n", iNum, ui32TxLen);
#endif
        return(eEtaI2cInvalidConfig);
    }
    if(ui32TxLen < ui8RestartAfterByte)
    {
#ifdef ETA_CSP_I2C_DEBUG
        ETA_DIAGS_PRINTF("ERROR: i2c_transfer_poll, inst %d, invalid " \
                          "ui32TxLen %d, ui8RestartAfterByte %d)\r\n", iNum,
                          ui32TxLen, ui8RestartAfterByte);
#endif
        return(eEtaI2cInvalidConfig);
    }
    if(ui32TxLen > 64 * 1024)
    {
#ifdef ETA_CSP_I2C_DEBUG
        ETA_DIAGS_PRINTF("ERROR: i2c_transfer_poll, inst %d exceeds max " \
                          "ui32TxLen=%d\r\n", iNum, ui32TxLen);
#endif
        return(eEtaI2cInvalidConfig);
    }
    if(ui32RxLen > 64 * 1024)
    {
#ifdef ETA_CSP_I2C_DEBUG
        ETA_DIAGS_PRINTF("ERROR: i2c_transfer_poll, inst %d exceeds max " \
                          "ui32RxLen=%d\r\n", iNum, ui32RxLen);
#endif
        return(eEtaI2cInvalidConfig);
    }
     Make sure previous command is done (optional).
    if( ( localI2cStatus = EtaCspI2cDonePoll(iNum) ) )
    {
        return(localI2cStatus);
    }
     Start command (waits for first byte).
    REGN_W1(iNum, I2C_STATUS, START_BUSY, 1);
    ui32ByteCount = 0;
#ifdef ETA_CSP_I2C_DEBUG
    ETA_DIAGS_PRINTF("i2c_transfer_poll beginning tx\r\n");
#endif
    ui32Timeout = 10000;
    while((--ui32Timeout) && (ui32TxLen))
    {
        if( ( localI2cStatus = EtaCspI2cTxReadyPoll(iNum) ) )
        {
            return(localI2cStatus);
        }
        ui8TxByte = *pui8TxData++;
        REG_I2C_TXRX_1BYTE_FIFO_DATA(iNum).V = ui8TxByte;
        ui32TxLen--;
#ifdef ETA_CSP_I2C_DEBUG
        ETA_DIAGS_PRINTF("i2c_transfer_poll tx data sent (%d left)\r\n",
                          ui32TxLen);
#endif
        ui32ByteCount = ui32ByteCount + 1;
        if(ui32ByteCount == ui8RestartAfterByte)
        {
            if( ( localI2cStatus = EtaCspI2cTxReadyPoll(iNum) ) )
            {
                return(localI2cStatus);
            }
            REG_I2C_CMD_FIFO_DATA(iNum).V = I2C_CMD_RESTART;
#ifdef ETA_CSP_I2C_DEBUG
            ETA_DIAGS_PRINTF("i2c_transfer_poll restart issued\r\n");
#endif
        }
    }
     This timeout is for general programming reasons to prevent infinite loop
    if(ui32Timeout == 0)
    {
        return(eEtaFailure);
    }
#ifdef ETA_CSP_I2C_DEBUG
    ETA_DIAGS_PRINTF("i2c_transfer_poll done with tx\r\n");
#endif
     If no rx, end write.
    if(ui32RxLen == 0)
    {
        if( ( localI2cStatus = EtaCspI2cTxReadyPoll(iNum) ) )
        {
            return(localI2cStatus);
        }
        REG_I2C_CMD_FIFO_DATA(iNum).V = I2C_CMD_END_WRITE;
#ifdef ETA_CSP_I2C_DEBUG
        ETA_DIAGS_PRINTF("i2c_transfer_poll corner case with ui32RxLen=0\r\n");
#endif
        return(eEtaSuccess);
    }
    if(ui32RxLen == 1)
    {
#ifdef ETA_CSP_I2C_DEBUG
        ETA_DIAGS_PRINTF("i2c_transfer_poll beginning, ui32RxLen=1\r\n");
#endif
        if( ( localI2cStatus = EtaCspI2cTxReadyPoll(iNum) ) )
        {
            return(localI2cStatus);
        }
        REG_I2C_TXRX_1BYTE_FIFO_DATA(iNum).V = 0xff;
        if( ( localI2cStatus = EtaCspI2cTxReadyPoll(iNum) ) )
        {
            return(localI2cStatus);
        }
        REG_I2C_CMD_FIFO_DATA(iNum).V = I2C_CMD_BEGIN_READ
                                        | I2C_CMD_END_READ;
    }
    else if(ui32RxLen == 2)
    {
#ifdef ETA_CSP_I2C_DEBUG
        ETA_DIAGS_PRINTF("i2c_transfer_poll beginning, ui32RxLen=2\r\n");
#endif
        if( ( localI2cStatus = EtaCspI2cTxReadyPoll(iNum) ) )
        {
            return(localI2cStatus);
        }
        REG_I2C_TXRX_1BYTE_FIFO_DATA(iNum).V = 0xff;
        if( ( localI2cStatus = EtaCspI2cTxReadyPoll(iNum) ) )
        {
            return(localI2cStatus);
        }
        REG_I2C_CMD_FIFO_DATA(iNum).V = I2C_CMD_BEGIN_READ;
        if( ( localI2cStatus = EtaCspI2cTxReadyPoll(iNum) ) )
        {
            return(localI2cStatus);
        }
        REG_I2C_TXRX_1BYTE_FIFO_DATA(iNum).V = 0xff;
        if( ( localI2cStatus = EtaCspI2cTxReadyPoll(iNum) ) )
        {
            return(localI2cStatus);
        }
        REG_I2C_CMD_FIFO_DATA(iNum).V = I2C_CMD_END_READ;
    }
    else
    {
#ifdef ETA_CSP_I2C_DEBUG
        ETA_DIAGS_PRINTF("i2c_transfer_poll beginning, ui32RxLen>2\r\n");
#endif
        if(( localI2cStatus = EtaCspI2cTxReadyPoll(iNum) ) )
        {
            return(localI2cStatus);
        }
        REG_I2C_TXRX_1BYTE_FIFO_DATA(iNum).V = 0xff;
        if( ( localI2cStatus = EtaCspI2cTxReadyPoll(iNum) ) )
        {
            return(localI2cStatus);
        }
        REG_I2C_CMD_FIFO_DATA(iNum).V = I2C_CMD_BEGIN_READ;
        REG_I2C_DUMMY_BYTES(iNum).V = ui32RxLen - 2;
        if( ( localI2cStatus = EtaCspI2cTxReadyPoll(iNum) ) )
        {
            return(localI2cStatus);
        }
        REG_I2C_CMD_FIFO_DATA(iNum).V = I2C_CMD_DUMMYBYTE;
        if( ( localI2cStatus = EtaCspI2cTxReadyPoll(iNum) ) )
        {
            return(localI2cStatus);
        }
        REG_I2C_TXRX_1BYTE_FIFO_DATA(iNum).V = 0xff;
        if( ( localI2cStatus = EtaCspI2cTxReadyPoll(iNum) ) )
        {
            return(localI2cStatus);
        }
        REG_I2C_CMD_FIFO_DATA(iNum).V = I2C_CMD_END_READ;
    }
#ifdef ETA_CSP_I2C_DEBUG
    ETA_DIAGS_PRINTF("i2c_transfer_poll waiting for rx data\r\n");
#endif
    ui32Timeout = 10000;
    while((--ui32Timeout) && (ui32RxLen))
    {
        if( ( localI2cStatus = EtaCspI2cRxDataPoll(iNum) ) )
        {
            return(localI2cStatus);
        }
         Pop data off the read stack.
        ui8RxByte = REG_I2C_TXRX_1BYTE_FIFO_DATA(iNum).V;
        *pui8RxData++ = ui8RxByte;
        ui32RxLen--;
#ifdef ETA_CSP_I2C_DEBUG
        ETA_DIAGS_PRINTF("i2c_transfer_poll rx data received (%d left)\r\n",
                          ui32RxLen);
#endif
    }
#ifdef ETA_CSP_I2C_DEBUG
    ETA_DIAGS_PRINTF("i2c_transfer_poll done waiting for rx data\r\n");
#endif
     This timeout is for general programming reasons to prevent infinite loop
    if(ui32Timeout == 0)
    {
        return(eEtaFailure);
    }
    return(eEtaSuccess);
}
tEtaStatus EtaCspI2cTransferPoll ( tI2cNum iNum , uint8_t * pui8TxData , uint32_t ui32TxLen , uint8_t * pui8RxData , uint32_t ui32RxLen , uint32_t ui8RestartAfterByte ) { uint32_t ui32ByteCount , ui32Timeout ; uint8_t ui8TxByte , ui8RxByte ; tEtaStatus localI2cStatus ; if ( ui32TxLen == 0 ) { #ifdef ETA_CSP_I2C_DEBUG ETA_DIAGS_PRINTF ( " " " \r \n " , iNum , ui32TxLen ) ; #endif return ( eEtaI2cInvalidConfig ) ; } if ( ui32TxLen < ui8RestartAfterByte ) { #ifdef ETA_CSP_I2C_DEBUG ETA_DIAGS_PRINTF ( " " " \r \n " , iNum , ui32TxLen , ui8RestartAfterByte ) ; #endif return ( eEtaI2cInvalidConfig ) ; } if ( ui32TxLen > 64 * 1024 ) { #ifdef ETA_CSP_I2C_DEBUG ETA_DIAGS_PRINTF ( " " " \r \n " , iNum , ui32TxLen ) ; #endif return ( eEtaI2cInvalidConfig ) ; } if ( ui32RxLen > 64 * 1024 ) { #ifdef ETA_CSP_I2C_DEBUG ETA_DIAGS_PRINTF ( " " " \r \n " , iNum , ui32RxLen ) ; #endif return ( eEtaI2cInvalidConfig ) ; } if ( ( localI2cStatus = EtaCspI2cDonePoll ( iNum ) ) ) { return ( localI2cStatus ) ; } REGN_W1 ( iNum , I2C_STATUS , START_BUSY , 1 ) ; ui32ByteCount = 0 ; #ifdef ETA_CSP_I2C_DEBUG ETA_DIAGS_PRINTF ( " \r \n " ) ; #endif ui32Timeout = 10000 ; while ( ( -- ui32Timeout ) && ( ui32TxLen ) ) { if ( ( localI2cStatus = EtaCspI2cTxReadyPoll ( iNum ) ) ) { return ( localI2cStatus ) ; } ui8TxByte = * pui8TxData ++ ; REG_I2C_TXRX_1BYTE_FIFO_DATA ( iNum ) . V = ui8TxByte ; ui32TxLen -- ; #ifdef ETA_CSP_I2C_DEBUG ETA_DIAGS_PRINTF ( " \r \n " , ui32TxLen ) ; #endif ui32ByteCount = ui32ByteCount + 1 ; if ( ui32ByteCount == ui8RestartAfterByte ) { if ( ( localI2cStatus = EtaCspI2cTxReadyPoll ( iNum ) ) ) { return ( localI2cStatus ) ; } REG_I2C_CMD_FIFO_DATA ( iNum ) . V = I2C_CMD_RESTART ; #ifdef ETA_CSP_I2C_DEBUG ETA_DIAGS_PRINTF ( " \r \n " ) ; #endif } } if ( ui32Timeout == 0 ) { return ( eEtaFailure ) ; } #ifdef ETA_CSP_I2C_DEBUG ETA_DIAGS_PRINTF ( " \r \n " ) ; #endif if ( ui32RxLen == 0 ) { if ( ( localI2cStatus = EtaCspI2cTxReadyPoll ( iNum ) ) ) { return ( localI2cStatus ) ; } REG_I2C_CMD_FIFO_DATA ( iNum ) . V = I2C_CMD_END_WRITE ; #ifdef ETA_CSP_I2C_DEBUG ETA_DIAGS_PRINTF ( " \r \n " ) ; #endif return ( eEtaSuccess ) ; } if ( ui32RxLen == 1 ) { #ifdef ETA_CSP_I2C_DEBUG ETA_DIAGS_PRINTF ( " \r \n " ) ; #endif if ( ( localI2cStatus = EtaCspI2cTxReadyPoll ( iNum ) ) ) { return ( localI2cStatus ) ; } REG_I2C_TXRX_1BYTE_FIFO_DATA ( iNum ) . V = 0xff ; if ( ( localI2cStatus = EtaCspI2cTxReadyPoll ( iNum ) ) ) { return ( localI2cStatus ) ; } REG_I2C_CMD_FIFO_DATA ( iNum ) . V = I2C_CMD_BEGIN_READ | I2C_CMD_END_READ ; } else if ( ui32RxLen == 2 ) { #ifdef ETA_CSP_I2C_DEBUG ETA_DIAGS_PRINTF ( " \r \n " ) ; #endif if ( ( localI2cStatus = EtaCspI2cTxReadyPoll ( iNum ) ) ) { return ( localI2cStatus ) ; } REG_I2C_TXRX_1BYTE_FIFO_DATA ( iNum ) . V = 0xff ; if ( ( localI2cStatus = EtaCspI2cTxReadyPoll ( iNum ) ) ) { return ( localI2cStatus ) ; } REG_I2C_CMD_FIFO_DATA ( iNum ) . V = I2C_CMD_BEGIN_READ ; if ( ( localI2cStatus = EtaCspI2cTxReadyPoll ( iNum ) ) ) { return ( localI2cStatus ) ; } REG_I2C_TXRX_1BYTE_FIFO_DATA ( iNum ) . V = 0xff ; if ( ( localI2cStatus = EtaCspI2cTxReadyPoll ( iNum ) ) ) { return ( localI2cStatus ) ; } REG_I2C_CMD_FIFO_DATA ( iNum ) . V = I2C_CMD_END_READ ; } else { #ifdef ETA_CSP_I2C_DEBUG ETA_DIAGS_PRINTF ( " \r \n " ) ; #endif if ( ( localI2cStatus = EtaCspI2cTxReadyPoll ( iNum ) ) ) { return ( localI2cStatus ) ; } REG_I2C_TXRX_1BYTE_FIFO_DATA ( iNum ) . V = 0xff ; if ( ( localI2cStatus = EtaCspI2cTxReadyPoll ( iNum ) ) ) { return ( localI2cStatus ) ; } REG_I2C_CMD_FIFO_DATA ( iNum ) . V = I2C_CMD_BEGIN_READ ; REG_I2C_DUMMY_BYTES ( iNum ) . V = ui32RxLen - 2 ; if ( ( localI2cStatus = EtaCspI2cTxReadyPoll ( iNum ) ) ) { return ( localI2cStatus ) ; } REG_I2C_CMD_FIFO_DATA ( iNum ) . V = I2C_CMD_DUMMYBYTE ; if ( ( localI2cStatus = EtaCspI2cTxReadyPoll ( iNum ) ) ) { return ( localI2cStatus ) ; } REG_I2C_TXRX_1BYTE_FIFO_DATA ( iNum ) . V = 0xff ; if ( ( localI2cStatus = EtaCspI2cTxReadyPoll ( iNum ) ) ) { return ( localI2cStatus ) ; } REG_I2C_CMD_FIFO_DATA ( iNum ) . V = I2C_CMD_END_READ ; } #ifdef ETA_CSP_I2C_DEBUG ETA_DIAGS_PRINTF ( " \r \n " ) ; #endif ui32Timeout = 10000 ; while ( ( -- ui32Timeout ) && ( ui32RxLen ) ) { if ( ( localI2cStatus = EtaCspI2cRxDataPoll ( iNum ) ) ) { return ( localI2cStatus ) ; } ui8RxByte = REG_I2C_TXRX_1BYTE_FIFO_DATA ( iNum ) . V ; * pui8RxData ++ = ui8RxByte ; ui32RxLen -- ; #ifdef ETA_CSP_I2C_DEBUG ETA_DIAGS_PRINTF ( " \r \n " , ui32RxLen ) ; #endif } #ifdef ETA_CSP_I2C_DEBUG ETA_DIAGS_PRINTF ( " \r \n " ) ; #endif if ( ui32Timeout == 0 ) { return ( eEtaFailure ) ; } return ( eEtaSuccess ) ; }
EtaCspI2cTransferPoll - Read and or write to an I2C device.
EtaCspI2cTransferPoll - Read and or write to an I2C device .
// // Check that the command is acceptable. // // // Make sure previous command is done (optional). // // // Start command (waits for first byte). // // This timeout is for general programming reasons to prevent infinite loop // // If no rx, end write. // // // Pop data off the read stack. // // This timeout is for general programming reasons to prevent infinite loop