5 Replies Latest reply on Apr 9, 2019 6:20 AM by luonc_4149601

    How to Write more than 1 byte command in I2C

    luonc_4149601

      Greetings to everyone.

      Like the title of the discussion says, i have to write a 16 bit command in a sensor using I2C interface.

      The structure of the message should be (according to sensor datasheet):

       

      S+Slave_addr+W+ACK+8bit Command(MSB)+ACK+8bit Command(LSB)+ACK

       

      I've tried this code:

      Error myI2C_Write(uint8_t SlaveAddr, uint16_t command){

          Error error;

          static cy_en_scb_i2c_status_t tmp;

       

          tmp=I2C_MasterSendStart(SlaveAddr,CY_SCB_I2C_WRITE_XFER,0);

          if (tmp!=CY_SCB_I2C_SUCCESS){

              error=ERROR_ALL;

              return error;

          }

          tmp=I2C_MasterWriteByte(command>>8,0);

          if (tmp!=CY_SCB_I2C_SUCCESS){

              error=ERROR_ALL;

              return error;

          }

          uint8_t cmd2=command&0xFF;

          tmp=I2C_MasterWriteByte(cmd2,0);

         

          if (tmp!=CY_SCB_I2C_SUCCESS){

              error=ERROR_ALL;

              return error;

          }

         

          tmp=I2C_MasterSendStop(0);

          CyDelayUs(2000);

          if (tmp!=CY_SCB_I2C_SUCCESS){

              error=ERROR_ALL;

              return error;

          }

         

         

          return error=ERROR_NONE;

      };

       

      But after first command byte succeded, the second one return with "CY_SCB_I2C_MASTER_MANUAL_NAK"

       

      I've also tried the high-level function "Cy_SCB_I2C_MasterWrite(I2C_HW,&masterTransferCfg,&I2C_context)"

      with:

      uint8_t buffer[2];

          buffer[0]=0x36;

          buffer[1]=0x15;

         

         

          masterTransferCfg.buffer=buffer;

          masterTransferCfg.bufferSize=2U;

          error=Cy_SCB_I2C_MasterWrite(I2C_HW,&masterTransferCfg,&I2C_context);

       

      but this function doesn't seem to work at all.

      In my debug panel i always see that the command is only 0x36 and not both.

      Any suggestion?

      Thanks in advance

      Luca