4 Replies Latest reply on May 3, 2020 9:57 PM by YatheeshK_36

    Best way to send USB data over I2C with FX2LP

    DaWi_3430881

      Hello,

       

        I am using the bulkloop example project to build a USB to I2C data bridge. I am using the example that includes the fxlpi2c.c and fx2lpi2c.h files. I currently have an I2C display connected to the FX2LP and working. I have been trying to figure out how to take USB data from EP2 in the TD_Poll() function from EXTAUTODAT1 and send it out over I2C.  I've tried the code below but it doesn't seem to work. In USB Control Center I can transmit a byte fine.

       

       

      void TD_Poll(void)              // Called repeatedly while the device is idle
      {
        WORD i;
        WORD count;
        BYTE __xdata buf;
      
      
      //  FX2LPI2C_XmitString("Packet of Byte Count = ");
        // FX2LPI2C_XmitHex4(count);
        // FX2LPI2C_XmitString(" being moved to EP6\r\n");
      //  FX2LPI2C_XmitChar('R');
      
      
        if(!(EP2468STAT & bmEP2EMPTY))
        { // check EP2 EMPTY(busy) bit in EP2468STAT (SFR), core set's this bit when FIFO is empty
           if(!(EP2468STAT & bmEP6FULL))
           {  // check EP6 FULL(busy) bit in EP2468STAT (SFR), core set's this bit when FIFO is full
              APTR1H = MSB( &EP2FIFOBUF );
              APTR1L = LSB( &EP2FIFOBUF );
      
      
              AUTOPTRH2 = MSB( &EP6FIFOBUF );
              AUTOPTRL2 = LSB( &EP6FIFOBUF );
      
      
              count = (EP2BCH << 8) + EP2BCL;
      
      
              // loop EP2OUT buffer data to EP6IN
              for( i = 0x0000; i < count; i++ )
              {
                 // setup to transfer EP2OUT buffer to EP6IN buffer using AUTOPOINTER(s)
                 EXTAUTODAT2 = EXTAUTODAT1;
              }
              EP6BCH = EP2BCH;  
              SYNCDELAY;  
              EP6BCL = EP2BCL;        // arm EP6IN
              SYNCDELAY;                    
              EP2BCL = 0x80;          // re(arm) EP2OUT
      
      
            buf = (BYTE __xdata)EXTAUTODAT1;
              EZUSB_WriteI2C(0x3C, 1 , &buf);
      
      
           }
        }
      

       

      Thanks,

      Dave

        • 1. Re: Best way to send USB data over I2C with FX2LP
          YatheeshK_36

          Hello Dave,

           

          EXTAUTODAT1 will contain the data pointed by auto-pointers APTR1H and APTR1L.

          The auto-pointers will increment every read or write to EXTAUTODAT1. Hence in your code once the for loop is completed the  the auto-pointers will be pointing to the data after the EP2FIFOBUF Buffer, which can be a junk data. In the code the EZUSB_WriteI2C is trying to print the data byte after the EP2FIFOBUF. Please pass a valid buffer pointer to the *dat function parameter.

          for example, if you want to print the first byte of EP2FIFOBUF:

          BYTE xdata buff = &EP2FIFOBUF;

          EZUSB_WriteI2C(0x3C,0x01, buff);

           

          If you want to send the entire EP2 Buffer data over I2C, you can use the EZUSB_WriteI2C(BYTE addr, BYTE length, BYTE xdata *dat)

          function with the pointer to the starting address of the data in the "dat" parameter and the length to be written in the length parameter.

          example:

          EZUSB_WriteI2C(0x3C, count, &EP2FIFOBUF)

          Please note that, you need to select the register address by writing the address of the register to the I2C slave before writing the data values.

           

           

           

          OR,

          You can also use a for loop to write each byte one by one.

          Please refer to the Vendax firmware from the FX2LP DVK which uses vendor commands to write to large EEPROM through I2C.

          The vendax firmware has 2 functions to write to EEPROM.

           

          void EEPROMWriteByte(WORD addr, BYTE value)

          void EEPROMRead(WORD addr, BYTE length, BYTE xdata *buf)

           

           

           

          Please let me know what you want to transmit through the I2C.

           

          Thanks,

          Yatheesh

          • 2. Re: Best way to send USB data over I2C with FX2LP
            DaWi_3430881

            Hi Yatheesh,

             

              Thanks for responding so quickly. I am new to FX2LP coding. I've implemented the following code for testing.

             

            void TD_Poll(void)              // Called repeatedly while the device is idle
            {
            
            WORD count
            BYTE __xdata buff = &EP2FIFOBUF;
            
              if(!(EP2468STAT & bmEP2EMPTY)) 
              { // check EP2 EMPTY(busy) bit in EP2468STAT (SFR), core set's this bit when FIFO is empty
                 count = (EP2BCH << 8) + EP2BCL;
                 EZUSB_WriteI2C(0x3C, count , buff);
            
                 EP2BCL = 0x80;          // re(arm) EP2OUT
              }
            }
            

             

            For line 05. above I get the following error:

            ../bulkloop.c:123: error 47: indirections to different types assignment  

            from type 'volatile-unsigned-char [1024] xdata* xdata'

            to type 'unsigned-char xdata'

            and for line 10. I get:

             

            ../bulkloop.c:136: warning 154: converting integral to pointer without a cast

            from type 'unsigned-char xdata'

            to type 'unsigned-char xdata* fixed'

             

            It does output something on the I2C lines but not the correct data. I'm trying to figure out how to fix these errors.

             

            What I'm trying to do is send I2C commands from the USB side to several I2C slave devices.

             

            Thanks,

            Dave

            • 3. Re: Best way to send USB data over I2C with FX2LP
              DaWi_3430881

              I figured it out.

               

              I needed to change line 5 to:

               

              BYTE __xdata buff = &EP2FIFOBUF;

              • 4. Re: Best way to send USB data over I2C with FX2LP
                YatheeshK_36

                Hello Dave,

                 

                Hope the I2C is working as expected. Please let me know if there are anymore queries.

                 

                Thanks,

                Yatheesh