I2C master write

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
SaGh_4441651
Level 3
Level 3
First like received First like given

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?

0 Likes
1 Solution
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

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

View solution in original post

0 Likes
5 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

Bob

0 Likes

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

[This is a PSOC4 device]

0 Likes
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

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

0 Likes

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.

0 Likes
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

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

0 Likes