5 Replies Latest reply on Oct 11, 2019 6:37 PM by MoTa_728816

    I2C master write

    SaGh_4441651

      I configured a master I2C using the SCB block. I am trying to write some data to the slave using the function I2C_1_I2CMasterWriteBuf(). The following is my code snippet -

       

      if(I2C_1_I2C_MSTR_NO_ERROR == I2C_1_I2CMasterWriteBuf(addr, buffer, size, I2C_1_I2C_MODE_COMPLETE_XFER ))

        {

          while( 0u == (I2C_1_I2CMasterStatus() & I2C_1_I2C_MSTAT_WR_CMPLT ))

          {

            /*Wait*/

            CyDelay(3);

          }

         

          if(0u == (I2C_1_I2CMasterStatus() & I2C_1_I2C_MSTAT_ERR_XFER ))

          {

            if(I2C_1_I2CMasterGetWriteBufSize() == size)

            {

              result = ERROR;

            }

          }

        }

      bytes_sent = I2C_1_I2CMasterGetWriteBufSize();

       

      At this point I2C_1_I2CMasterGetWriteBufSize() is always returning 1. What can be the error? Shouldn't this function return a number that is equal to size if the transfer was successful?

        • 1. Re: I2C master write
          JoMe_264151

          Following the datasheet  I2C_1_I2CMasterGetWriteBufSize() returns the number of bytes still in the buffer to be sent.

           

          Bob

          • 2. Re: I2C master write
            SaGh_4441651

            The data sheet says this - Returns the number of bytes that have been transferred with the SCB_I2CMasterWriteBuf() function

             

            [This is a PSOC4 device]

            • 3. Re: I2C master write
              MoTa_728816

              Hi,

               

              I think that calling I2C_1_I2CMasterWriteBuf() requests "size" byte of transfer

              and the I2C_1_I2CMasterStatus() could be I2C_I2C_MSTAT_WR_CMPLT

              for each byte sent.

               

              So the first while loop exit at the first byte sent.

              If you want to wait for the completion of the buffer sent

              you may need to use a loop something like

               

                   while(I2C_1_SCB_I2CMasterWriteBuf() != size)  {

                                      ;

                    }

               

              moto

              • 4. Re: I2C master write
                SaGh_4441651

                When you said this - I2C_1_I2CMasterWriteBuf() requests "size" byte of transfer, do you mean the function  I2C_1_I2CMasterGetWriteBufSize() instead?

                 

                Because I2C_1_I2CMasterWriteBuf() is the function that actually writes an entire buffer of data to a slave device.

                • 5. Re: I2C master write
                  MoTa_728816

                  Hi,

                   

                  Since I2C_1_I2CMasterWriteBuf() can take long time if the buffer is big,

                  I think the function returns just after submitting the request.

                   

                  So it will be the programmer's responsibility to monitor how much of the buffer has been sent.

                   

                  In your case you monitored I2C_Status(), I imagined that the status of component gets to be complete

                  in each transaction, so your while loop exit at the first byte sent.

                   

                  I think that the ..WriteBufSize() must be "the size - 1" at that time.

                   

                  So I proposed to use while loop to wait until the size get to 0.

                   

                  moto