9 Replies Latest reply on Jan 31, 2017 7:18 AM by lee.carter

    How do I get 400Khz i2c module operation?

    lee.carter


      Description Hello? 
      I'd like to get 400Khz i2c module operation in the FX3. 

      as the below is my i2c code, Can you let me know what am I supposed to do to get the 400KHZ i2c operation? 

      ////////////////////////////////////////////////////////////// 
      /* I2c initialization for EEPROM programming. */ 
      CyU3PReturnStatus_t 
      CyFxI2cInit (uint16_t pageLen) 

      CyU3PI2cConfig_t i2cConfig; 
      //CyU3PDmaChannelConfig_t dmaConfig; 
      CyU3PReturnStatus_t status = CY_U3P_SUCCESS; 

      /* Initialize and configure the I2C master module. */ 
      status = CyU3PI2cInit (); 
      if (status != CY_U3P_SUCCESS) 

      CyU3PDebugPrint (4, "I2C configuration failed!\n"); 
      return status; 


      /* Start the I2C master block. The bit rate is set at 100KHz. 
      * The data transfer is done via DMA. */ 
      CyU3PMemSet ((uint8_t *)&i2cConfig, 0, sizeof(i2cConfig)); 
      i2cConfig.bitRate = 100000;//CY_FX_USBI2C_I2C_BITRATE; 
      i2cConfig.busTimeout = 0xFFFFFFFF; 
      i2cConfig.dmaTimeout = 0xFFFF; 
      i2cConfig.isDma = CyFalse; 

      status = CyU3PI2cSetConfig (&i2cConfig, NULL); 
      if (status != CY_U3P_SUCCESS) 

      CyU3PDebugPrint (4, "I2C configuration failed!\n"); 
      return status; 


      /* Now create the DMA channels required for read and write. */ 
      //CyU3PMemSet ((uint8_t *)&dmaConfig, 0, sizeof(dmaConfig)); 
      //dmaConfig.size = pageLen; 
      /* No buffers need to be allocated as this will be used 
      * only in override mode. */ 
      /* 
      dmaConfig.count = 0; 
      dmaConfig.prodAvailCount = 0; 
      dmaConfig.dmaMode = CY_U3P_DMA_MODE_BYTE; 
      dmaConfig.prodHeader = 0; 
      dmaConfig.prodFooter = 0; 
      dmaConfig.consHeader = 0; 
      dmaConfig.notification = 0; 
      dmaConfig.cb = NULL; 
      */ 
      /* Create a channel to write to the EEPROM. */ 
      /* 
      dmaConfig.prodSckId = CY_U3P_CPU_SOCKET_PROD; 
      dmaConfig.consSckId = CY_U3P_LPP_SOCKET_I2C_CONS; 
      status = CyU3PDmaChannelCreate (&glI2cTxHandle, 
      CY_U3P_DMA_TYPE_MANUAL_OUT, &dmaConfig); 
      if (status != CY_U3P_SUCCESS) 

      return status; 

      */ 
      /* Create a channel to read from the EEPROM. */ 
      /* 
      dmaConfig.prodSckId = CY_U3P_LPP_SOCKET_I2C_PROD; 
      dmaConfig.consSckId = CY_U3P_CPU_SOCKET_CONS; 
      status = CyU3PDmaChannelCreate (&glI2cRxHandle, 
      CY_U3P_DMA_TYPE_MANUAL_IN, &dmaConfig); 

      if (status == CY_U3P_SUCCESS) 

      glI2cPageSize = pageLen; 

      */ 
      return status; 

      ////////////////////////////////////////////////////////////// 
      CyU3PReturnStatus_t 
      CyFxUsbI2cTransfer ( 
      uint16_t byteAddress, 
      uint8_t devAddr, 
      uint16_t byteCount, 
      uint8_t *buffer, 
      CyBool_t isRead) 

      CyU3PDmaBuffer_t buf_p; 
      CyU3PI2cPreamble_t preamble; 
      uint16_t pageCount = (byteCount / glI2cPageSize); 
      CyU3PReturnStatus_t status = CY_U3P_SUCCESS; 

      if (byteCount == 0) 

      return CY_U3P_SUCCESS; 


      if ((byteCount % glI2cPageSize) != 0) 

      pageCount ++; 


      CyU3PDebugPrint (2, "I2C access - dev: 0x%x, address: 0x%x, size: 0x%x, pages: 0x%x.\r\n", 
      devAddr, byteAddress, byteCount, pageCount); 

      /* Update the buffer address. */ 
      buf_p.buffer = buffer; 
      buf_p.status = 0; 

      while (pageCount != 0) 

      if (isRead) 

      /* Update the preamble information. */ 
      preamble.length = 4; 
      preamble.buffer[0] = devAddr; 
      preamble.buffer[1] = (uint8_t)(byteAddress >> 8); 
      preamble.buffer[2] = (uint8_t)(byteAddress & 0xFF); 
      preamble.buffer[3] = (devAddr | 0x01); 
      preamble.ctrlMask = 0x0004; 

      buf_p.size = glI2cPageSize; 
      buf_p.count = glI2cPageSize; 

      status = CyU3PI2cSendCommand (&preamble, glI2cPageSize, CyTrue); 
      if (status != CY_U3P_SUCCESS) 

      return status; 

      status = CyU3PDmaChannelSetupRecvBuffer (&glI2cRxHandle, &buf_p); 
      if (status != CY_U3P_SUCCESS) 

      return status; 

      status = CyU3PDmaChannelWaitForCompletion(&glI2cRxHandle, 
      CY_FX_USB_I2C_TIMEOUT); 
      if (status != CY_U3P_SUCCESS) 

      return status; 


      else /* Write */ 

      /* Update the preamble information. */ 
      preamble.length = 3; 
      preamble.buffer[0] = devAddr; 
      preamble.buffer[1] = (uint8_t)(byteAddress >> 8); 
      preamble.buffer[2] = (uint8_t)(byteAddress & 0xFF); 
      preamble.ctrlMask = 0x0000; 

      buf_p.size = glI2cPageSize; 
      buf_p.count = (byteCount > glI2cPageSize) ? glI2cPageSize : byteCount; 

      status = CyU3PDmaChannelSetupSendBuffer (&glI2cTxHandle, 
      &buf_p); 
      if (status != CY_U3P_SUCCESS) 

      return status; 

      status = CyU3PI2cSendCommand (&preamble, ((byteCount > glI2cPageSize) ? glI2cPageSize : byteCount), CyFalse); 
      if (status != CY_U3P_SUCCESS) 

      return status; 

      status = CyU3PDmaChannelWaitForCompletion(&glI2cTxHandle, 
      CY_FX_USB_I2C_TIMEOUT); 
      if (status != CY_U3P_SUCCESS) 

      return status; 



      /* Update the parameters */ 
      byteAddress += glI2cPageSize; 
      buf_p.buffer += glI2cPageSize; 
      byteCount -= glI2cPageSize; 
      pageCount --; 

      /* Need a delay between write operations. */ 
      CyU3PThreadSleep (10); 


      return CY_U3P_SUCCESS; 


      /////////////////////////////////////////////////////////////