cancel
Showing results for 
Search instead for 
Did you mean: 

I2C problem

tran
Associate II
Posted on September 24, 2009 at 18:07

I2C problem

5 REPLIES 5
tran
Associate II
Posted on May 17, 2011 at 13:23

Hello all,

I have one question about I2C programme. When i program I2C , i try to use the code example in the STM32 library, and i have somthing like this one:

I2C_GenerateSTART(I2C_EE, ENABLE);

/* Test on EV5 and clear it */

while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_MODE_SELECT));

/* Send EEPROM address for write */

I2C_Send7bitAddress(I2C_EE, EEPROM_ADDRESS, I2C_Direction_Transmitter); /* Test on EV6 and clear it */

while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

The problem of this code is : when we try to send 1 byte to the slave, if there is some problem in the communication ( or another thing )that make us can't receive the ACK bit.

When we havent the ACK bit, the function ''while'' will block my programme ( its so rude :( ).

If i delete this function, the communication I2C won't wofk after.

Do you have any solution to this problem??

Thank you very much.

guyvo67
Associate II
Posted on May 17, 2011 at 13:23

hi,

At the slave you can test on this event:

I2C_EVENT_SLAVE_ACK_FAILURE

At the master you can test capture via interrupt error handler:

if (I2C_GetITStatus(I2C1, I2C_IT_AF))

{

I2C_ClearITPendingBit(I2C1, I2C_IT_AF);

I2C_GenerateSTOP(I2C1, ENABLE);

]

I think if you use these you can work around your problem of communication failures.

-G

tran
Associate II
Posted on May 17, 2011 at 13:23

Hi,

My device is always working like a master, im trying to work with the interrupt error handle too, but it hasn't worked yet :(

Does someone know to assure the reliability of a communication I2C?

How can we be sure that the data we send or receive is correct?

Thx all,

guyvo67
Associate II
Posted on May 17, 2011 at 13:23

hi,

You can use :

Packet error checking:

A PEC calculator has been implemented to improve the reliability of communication. The PEC is calculated by using the C(x) = x8 + x2 + x + 1 CRC-8 polynomial serially on each bit.

see page 639 of RM0008 to read the details.

-G

[ This message was edited by: guyvo67 on 14-09-2009 09:06 ]

tomas23
Associate II
Posted on May 17, 2011 at 13:23

Three hints on using I2C:

1. check single bits in I2C->SR and CR, don't use the library status functions (my personal hint)

2. use timeouts in every loop, where any trouble may occur (esp. when communicating with something external to STM32!)

3. use I2C interrupts or DMA, if possible