Callback function on I2C master/slave transfer complete

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 want a callback function that is called once the I2C master is done sending all the bytes. I came across 2 options that I maybe able to use -

I2C_1_I2C_ISR_ExitCallback() or I2C_1_SetMasterInterruptMode(I2C_1_INTR_MASTER_I2C_STOP) or I2C_1_I2C_MSTAT_RD_CMPLT;

What is the difference between the two? And which one should I be using?

Also, I need to set some flags indicating slave's status. For this I also came across I2C_1_I2C_SSTAT_RD_CMPLT (& other flag bits), I2C_1_I2C_SlaveCompleteCallback and I2C_1_SetSlaveInterruptMode. What is the differnce between all these methods?

0 Likes
1 Solution
Hari
Moderator
Moderator
Moderator
750 replies posted 500 replies posted 250 solutions authored

Hello SaGh_4441651

The callback function (I2C_1_I2C_ISR_ExitCallback()) is the correct method to check. The I2C component will return I2C_1_I2C_MSTAT_RD_CMPLT for I2C_1_I2CMasterStatus() once the read is complete. Hence, the callback function must include an if statement like

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

{

}

Another method is to poll I2C_1_I2C_MSTAT_RD_CMPLT to check if the read is complete. This can be done by continuously checking the return status of I2C_1_I2CMasterStatus().

However, interrupt method is a better solution.

Thanks and regards

Hari

View solution in original post

5 Replies
Hari
Moderator
Moderator
Moderator
750 replies posted 500 replies posted 250 solutions authored

Hello SaGh_4441651

The callback function (I2C_1_I2C_ISR_ExitCallback()) is the correct method to check. The I2C component will return I2C_1_I2C_MSTAT_RD_CMPLT for I2C_1_I2CMasterStatus() once the read is complete. Hence, the callback function must include an if statement like

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

{

}

Another method is to poll I2C_1_I2C_MSTAT_RD_CMPLT to check if the read is complete. This can be done by continuously checking the return status of I2C_1_I2CMasterStatus().

However, interrupt method is a better solution.

Thanks and regards

Hari

Hey,

Thank you for your response. So I if want to check the same for a slave device, should I be using the same callback and then instead check for I2C_1_I2C_SSTAT_RD_CMPLT? How do I check if the slave is busy i.e. with the flag I2C_1_I2C_SSTAT_WR_BUSY? Do I have to poll this flag to check slave's status? Because I am assuming the callback is called once the ISR is done executing.

0 Likes
Hari
Moderator
Moderator
Moderator
750 replies posted 500 replies posted 250 solutions authored

Hi SaGh_4441651

Yes. Depending on the slave application, I2C_1_I2C_SSTAT_RD_CMPLT or I2C_1_I2C_SSTAT_WR_CMPLT must be checked to see if the slave is free.

You can either poll (in main itself) or you can check the condition and wait in the end of ISR in callback function.

Thanks and regards

Harigovind

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

I would recommend not to wait in an interrupt handler, system might stall.

Bob

0 Likes
Hari
Moderator
Moderator
Moderator
750 replies posted 500 replies posted 250 solutions authored

Yes. This is true. The implementation suggested was to check the interrupt cause - I2C_1_I2C_SSTAT_RD_CMPLT  or I2C_1_I2C_SSTAT_WR_CMPLT- using conditional statements.

Thanks and regards

Hari

0 Likes