5 Replies Latest reply on Jan 17, 2018 3:47 PM by user_353361858

    I2C code doesn't work after component update

    frederic.chasse_1669321

      Hi,

       

      I was using the component SCB_P4 3.20 as an I2C component. I updated the component to 4.0, added the timeout value to the functions, as described in the datasheet, but my code doesn't work anymore. I've compared the generated files before and after the change, but there is too much for me to pinpoint the issue. Here is my code :

       

      #define I2C_TIMEOUT_MS          (100)

       

      int I2C_read(unsigned char *buf, int bufferSize) {

          // Read must be passed the following format with expected start, restart,

          // stop to be inserted

          // buf[0]: ADDRESS + W

          // buf[1]: REG ADDRESS

          // buf[2]: ADDRESS + R

       

          int cnt,

              status = bufferSize;

          uint32_t devAddr,

                   rdVal,

                   err;

       

          // Extracting

          devAddr = (uint32_t)((buf[0] >> 1) & 0x7F);

         

          // Sending Start and address + w followed by the register address

          if ( I2C_I2CMasterSendStart(devAddr, 0, I2C_TIMEOUT_MS) != I2C_I2C_MSTR_NO_ERROR ) { return -1; }

          if ( I2C_I2CMasterWriteByte(buf[1], I2C_TIMEOUT_MS) != I2C_I2C_MSTR_NO_ERROR ) { return -1; }

         

          // Sending Restart and reading data

          I2C_I2CMasterReadBuf(devAddr, buf, bufferSize, I2C_I2C_MODE_COMPLETE_XFER | I2C_I2C_MODE_REPEAT_START);

         

          while( !((err = I2C_I2CMasterStatus()) & (I2C_I2C_MSTAT_RD_CMPLT | I2C_I2C_MSTAT_ERR_XFER)) ) {

              CySysPmSleep();

          }

         

          I2C_I2CMasterClearReadBuf();

         

          return status;

      }

       

      int I2C_write(unsigned char *buf, int bufferSize) {

         

          // Write must be passed the following format with expected start and

          // stop to be inserted

          // buf[0]: ADDRESS + W

          // buf[1]: REG ADDRESS

          // buf[2 to 2+bufferSize]: value

       

          int cnt;

          uint32_t devAddr, err;

         

          // Extracting

          devAddr = (uint32_t)((buf[0] >> 1) & 0x7F);

         

          // Sending Start and address + w followed by the register address and then values

          if ( I2C_I2CMasterSendStart(devAddr, 0, I2C_TIMEOUT_MS) != I2C_I2C_MSTR_NO_ERROR ) { return -1; }

          for ( cnt = 1; cnt < bufferSize + 2; cnt++ ) {

              if ( I2C_I2CMasterWriteByte(buf[cnt], I2C_TIMEOUT_MS) != I2C_I2C_MSTR_NO_ERROR ) { return -1; }

          }

          // Stop

          if ( I2C_I2CMasterSendStop(I2C_TIMEOUT_MS) != I2C_I2C_MSTR_NO_ERROR ) { return -1; }

         

          return bufferSize;

      }