Platform/ECM3532/M3/hw/csp/ecm3532/m3/csp/src/eta_csp_i2c.c
/***************************************************************************/
/**
 *
 *  EtaCspI2cProcessIntRxData - Process I2C Interrupt for RX Data Available
 *
 *  @param iNum the I2C number.
 *
 ******************************************************************************/
Process I2C Interrupt for RX Data Available
@param iNum the I2C number.
Process I2C Interrupt for RX Data Available @param iNum the I2C number .
tEtaStatus EtaCspI2cProcessIntRxData (tI2cNum iNum)
{
  uint8_t space_in_fifo;
  uint32_t rx_word;
  #ifdef ETA_CSP_I2C_INT_DEBUG
     ETA_DIAGS_PRINTF("DETAILED_I2C:entered EtaCspI2cProcessIntRxData\r\n");
  #endif
  if ((g_pEtaCspI2cCmd[iNum]->bCmdActive == 0) || (g_pEtaCspI2cCmd[iNum]->ui32RxLen == 0))
  {
#ifdef ETA_CSP_I2C_DEBUG
    ETA_DIAGS_PRINTF("ERROR: received i2c_int_rx but command is done. bCmdActive=%x  ui32RxLen=%x\r\n", g_pEtaCspI2cCmd[iNum]->bCmdActive, g_pEtaCspI2cCmd[iNum]->ui32RxLen);
#endif
    return eEtaI2cCmdSequenceErr;
  }
  if (ETA_CSP_I2C_EN_GREEDY_INT) 
  {
    space_in_fifo = (MEMIO8(REG_I2C_RX_FIFO_DEBUG_ADDR (iNum)) & BM_I2C_RX_FIFO_DEBUG_ENTRIES);
  }
  else
  {
    space_in_fifo = g_pEtaCspI2cCmd[iNum]->ui8RxIntSize; 
    if (space_in_fifo > g_pEtaCspI2cCmd[iNum]->ui32RxLen) space_in_fifo = g_pEtaCspI2cCmd[iNum]->ui32RxLen;
  }
  #ifdef ETA_CSP_I2C_INT_DEBUG
     ETA_DIAGS_PRINTF("DETAILED_I2C:I2C RX data interrupt. space_in_fifo:%d\r\n", space_in_fifo);
  #endif
  do
  {
    if (space_in_fifo >= 4)
    {
      rx_word = REG_I2C_RX_4BYTE_FIFO_DATA(iNum).V;
      #ifdef ETA_CSP_I2C_INT_DEBUG
       ETA_DIAGS_PRINTF("DETAILED_I2C:I2C RX 4data interrupt. rx:%x rxlen:%d space:%d\r\n", rx_word, g_pEtaCspI2cCmd[iNum]->ui32RxLen, space_in_fifo);
      #endif
      *g_pEtaCspI2cCmd[iNum]->pui8RxData++ = ((rx_word >> 0) & 0xff);
      *g_pEtaCspI2cCmd[iNum]->pui8RxData++ = ((rx_word >> 8) & 0xff);
      *g_pEtaCspI2cCmd[iNum]->pui8RxData++ = ((rx_word >> 16) & 0xff);
      *g_pEtaCspI2cCmd[iNum]->pui8RxData++ = ((rx_word >> 24) & 0xff);
      g_pEtaCspI2cCmd[iNum]->ui32RxLen -= 4;
      space_in_fifo -= 4;
    }
    else if (space_in_fifo >= 2)
    {
      rx_word = MEMIO16(REG_I2C_RX_2BYTE_FIFO_DATA_ADDR (iNum));
      #ifdef ETA_CSP_I2C_INT_DEBUG
       ETA_DIAGS_PRINTF("DETAILED_I2C:I2C RX 2data interrupt. rx:%x rxlen:%d space:%d\r\n", rx_word, g_pEtaCspI2cCmd[iNum]->ui32RxLen, space_in_fifo);
      #endif
      *g_pEtaCspI2cCmd[iNum]->pui8RxData++ = ((rx_word >> 0) & 0xff);
      *g_pEtaCspI2cCmd[iNum]->pui8RxData++ = ((rx_word >> 8) & 0xff);
      g_pEtaCspI2cCmd[iNum]->ui32RxLen -= 2;
      space_in_fifo -= 2;
    }
    else
    {
      rx_word = MEMIO8(REG_I2C_TXRX_1BYTE_FIFO_DATA_ADDR (iNum));
      #ifdef ETA_CSP_I2C_INT_DEBUG
        ETA_DIAGS_PRINTF("DETAILED_I2C:I2C RX 1data interrupt. rx:%x rxlen:%d space:%d\r\n", rx_word, g_pEtaCspI2cCmd[iNum]->ui32RxLen, space_in_fifo);
      #endif
      *g_pEtaCspI2cCmd[iNum]->pui8RxData++ = ((rx_word >> 0) & 0xff);
      g_pEtaCspI2cCmd[iNum]->ui32RxLen --;
      space_in_fifo --;
    }
    if (g_pEtaCspI2cCmd[iNum]->ui32RxLen == 0) 
    {
      REG_I2C_INT_EN(iNum).V = BM_I2C_INT_EN_CMD_DONE | BM_I2C_INT_EN_UNACK_EN;
      #ifdef ETA_CSP_I2C_INT_DEBUG
       ETA_DIAGS_PRINTF("DETAILED_I2C:Last I2C RX data interrupt\r\n");
      #endif
    }
    if ((space_in_fifo < 4) & (g_pEtaCspI2cCmd[iNum]->ui32RxLen >= 4))
    {
      break; 
    }
    if ((space_in_fifo < 2) & (g_pEtaCspI2cCmd[iNum]->ui32RxLen >= 2))
    {
      break; 
    }
    if (g_pEtaCspI2cCmd[iNum]->ui32RxLen == 0)
    {
      break; 
    }
  }
  while (space_in_fifo);
  if (g_pEtaCspI2cCmd[iNum]->ui8RxIntSize > g_pEtaCspI2cCmd[iNum]->ui32RxLen)
    REG_I2C_CONFIG(iNum).BF.RX_FIFO_INT_LEVEL = g_pEtaCspI2cCmd[iNum]->ui32RxLen;
    #ifdef ETA_CSP_I2C_INT_DEBUG
     ETA_DIAGS_PRINTF("DETAILED_I2C:exiting EtaCspI2cProcessIntRxData\r\n");
    #endif
  return eEtaSuccess;
}
tEtaStatus EtaCspI2cProcessIntRxData ( tI2cNum iNum ) { uint8_t space_in_fifo ; uint32_t rx_word ; #ifdef ETA_CSP_I2C_INT_DEBUG ETA_DIAGS_PRINTF ( " \r \n " ) ; #endif if ( ( g_pEtaCspI2cCmd [ iNum ] -> bCmdActive == 0 ) || ( g_pEtaCspI2cCmd [ iNum ] -> ui32RxLen == 0 ) ) { #ifdef ETA_CSP_I2C_DEBUG ETA_DIAGS_PRINTF ( " \r \n " , g_pEtaCspI2cCmd [ iNum ] -> bCmdActive , g_pEtaCspI2cCmd [ iNum ] -> ui32RxLen ) ; #endif return eEtaI2cCmdSequenceErr ; } if ( ETA_CSP_I2C_EN_GREEDY_INT ) { space_in_fifo = ( MEMIO8 ( REG_I2C_RX_FIFO_DEBUG_ADDR ( iNum ) ) & BM_I2C_RX_FIFO_DEBUG_ENTRIES ) ; } else { space_in_fifo = g_pEtaCspI2cCmd [ iNum ] -> ui8RxIntSize ; if ( space_in_fifo > g_pEtaCspI2cCmd [ iNum ] -> ui32RxLen ) space_in_fifo = g_pEtaCspI2cCmd [ iNum ] -> ui32RxLen ; } #ifdef ETA_CSP_I2C_INT_DEBUG ETA_DIAGS_PRINTF ( " \r \n " , space_in_fifo ) ; #endif do { if ( space_in_fifo >= 4 ) { rx_word = REG_I2C_RX_4BYTE_FIFO_DATA ( iNum ) . V ; #ifdef ETA_CSP_I2C_INT_DEBUG ETA_DIAGS_PRINTF ( " \r \n " , rx_word , g_pEtaCspI2cCmd [ iNum ] -> ui32RxLen , space_in_fifo ) ; #endif * g_pEtaCspI2cCmd [ iNum ] -> pui8RxData ++ = ( ( rx_word >> 0 ) & 0xff ) ; * g_pEtaCspI2cCmd [ iNum ] -> pui8RxData ++ = ( ( rx_word >> 8 ) & 0xff ) ; * g_pEtaCspI2cCmd [ iNum ] -> pui8RxData ++ = ( ( rx_word >> 16 ) & 0xff ) ; * g_pEtaCspI2cCmd [ iNum ] -> pui8RxData ++ = ( ( rx_word >> 24 ) & 0xff ) ; g_pEtaCspI2cCmd [ iNum ] -> ui32RxLen -= 4 ; space_in_fifo -= 4 ; } else if ( space_in_fifo >= 2 ) { rx_word = MEMIO16 ( REG_I2C_RX_2BYTE_FIFO_DATA_ADDR ( iNum ) ) ; #ifdef ETA_CSP_I2C_INT_DEBUG ETA_DIAGS_PRINTF ( " \r \n " , rx_word , g_pEtaCspI2cCmd [ iNum ] -> ui32RxLen , space_in_fifo ) ; #endif * g_pEtaCspI2cCmd [ iNum ] -> pui8RxData ++ = ( ( rx_word >> 0 ) & 0xff ) ; * g_pEtaCspI2cCmd [ iNum ] -> pui8RxData ++ = ( ( rx_word >> 8 ) & 0xff ) ; g_pEtaCspI2cCmd [ iNum ] -> ui32RxLen -= 2 ; space_in_fifo -= 2 ; } else { rx_word = MEMIO8 ( REG_I2C_TXRX_1BYTE_FIFO_DATA_ADDR ( iNum ) ) ; #ifdef ETA_CSP_I2C_INT_DEBUG ETA_DIAGS_PRINTF ( " \r \n " , rx_word , g_pEtaCspI2cCmd [ iNum ] -> ui32RxLen , space_in_fifo ) ; #endif * g_pEtaCspI2cCmd [ iNum ] -> pui8RxData ++ = ( ( rx_word >> 0 ) & 0xff ) ; g_pEtaCspI2cCmd [ iNum ] -> ui32RxLen -- ; space_in_fifo -- ; } if ( g_pEtaCspI2cCmd [ iNum ] -> ui32RxLen == 0 ) { REG_I2C_INT_EN ( iNum ) . V = BM_I2C_INT_EN_CMD_DONE | BM_I2C_INT_EN_UNACK_EN ; #ifdef ETA_CSP_I2C_INT_DEBUG ETA_DIAGS_PRINTF ( " \r \n " ) ; #endif } if ( ( space_in_fifo < 4 ) & ( g_pEtaCspI2cCmd [ iNum ] -> ui32RxLen >= 4 ) ) { break ; } if ( ( space_in_fifo < 2 ) & ( g_pEtaCspI2cCmd [ iNum ] -> ui32RxLen >= 2 ) ) { break ; } if ( g_pEtaCspI2cCmd [ iNum ] -> ui32RxLen == 0 ) { break ; } } while ( space_in_fifo ) ; if ( g_pEtaCspI2cCmd [ iNum ] -> ui8RxIntSize > g_pEtaCspI2cCmd [ iNum ] -> ui32RxLen ) REG_I2C_CONFIG ( iNum ) . BF . RX_FIFO_INT_LEVEL = g_pEtaCspI2cCmd [ iNum ] -> ui32RxLen ; #ifdef ETA_CSP_I2C_INT_DEBUG ETA_DIAGS_PRINTF ( " \r \n " ) ; #endif return eEtaSuccess ; }
EtaCspI2cProcessIntRxData - Process I2C Interrupt for RX Data Available
  @param iNum the I2C number.
EtaCspI2cProcessIntRxData - Process I2C Interrupt for RX Data Available @param iNum the I2C number .
// This allows for burst writes, which is more efficient, but not requires an extra read. // Do work that interrupt said we have space for // If we have data to send, ignore receive data. // Just received last byte. // If we drop low enough that we cannot bulk transfer, exit loop, wait for next int. // If we drop low enough that we cannot bulk transfer, exit loop, wait for next int. // Exit loop, we're done.