I2C master , help me

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

cross mob
docn_292601
Level 2
Level 2
10 replies posted 10 sign-ins 5 replies posted

1.  i call the function  I2C_M_Start();

   

2.  write a data buf to eeprom:

   

          I2C_M_MasterWriteBuf(SLAVE_ADDR, buf, num, I2C_M_MODE_COMPLETE_XFER);
           
            /* Wait till write operation is complete*/
            while (!(I2C_M_MasterStatus() & I2C_M_MSTAT_WR_CMPLT))
            {
                ;
            }

   

            //the buf includes the address of eeprom memory.

   

3. write another data buf to eeprom:

   

            I2C_M_MasterWriteBuf(SLAVE_ADDR, anotherbuf, num1, I2C_M_MODE_COMPLETE_XFER);
           
            /* Wait till write operation is complete*/
            while (!(I2C_M_MasterStatus() & I2C_M_MSTAT_WR_CMPLT))
            {
                ;
            }

   

4.  i call the function  I2C_M_Stopt();

   

my question :

   

2 worked     3 did not work

   

when i commented 2,  the 3 woked.

   

maybe  i was wrong about the mode(now is I2C_M_MODE_COMPLETE_XFER).

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

You should at least check the returned value of the MasterWriteBuf() to see if it completed without an error.

   

 

   

Bob

0 Likes
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

Your EEPROM needs additional time to finish its write. I2C_M_MasterStatus() only waits for the I2C-transfer to be finished - but writing the EEPROM will take more time. And when you try to write too soon, you will get an error.

   

E.g. see the data sheet for a 24LC1025 (http://ww1.microchip.com/downloads/en/DeviceDoc/21941K.pdf ) page 11 for an example. What device are you using?

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Onchip EEPROM takes worst case 20 mS to write a row/block,

   

and is a blocking function when called, so I2C after fill of buffer

   

to be written, has to be stopped from further buffer activity until

   

buffer is completely written to EEPROM.

   

 

   

If you right click EEPROM, find example project, there is one you can

   

add to your design or open in a new deisgn window.

   

 

   

Regards, Dana.

0 Likes
docn_292601
Level 2
Level 2
10 replies posted 10 sign-ins 5 replies posted

my eeprom device is M24256.

   

  i added  CyDelay(50) between 2 and 3.

   

the  3  worked.

   

thank you all very much.

   

谢谢!!!(my english is poor .)

0 Likes
docn_292601
Level 2
Level 2
10 replies posted 10 sign-ins 5 replies posted

   

   

i find  the content above in the datasheet.

   

so i can change CyDelay(50) to CyDelay(10).

   

am i right ? i will test next.

0 Likes
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

You should poll the EEPROM to find out whether it has finished writing or not. See page 18 in the data sheet (http://www.st.com/web/en/resource/technical/document/datasheet/CD00001891.pdf )

0 Likes
docn_292601
Level 2
Level 2
10 replies posted 10 sign-ins 5 replies posted

oh, i see two ways:

   

1. polling the EEPROM to make sure that the sencod buf can be written.

   

2. just Call CyDelay(ms>MAX).

   

the first must be right  , the second now i use maybe right.

   

thanks.

0 Likes
docn_292601
Level 2
Level 2
10 replies posted 10 sign-ins 5 replies posted

oh, i see two ways:

   

1. polling the EEPROM to make sure that the sencod buf can be written.

   

2. just Call CyDelay(ms>MAX).

   

the first must be right  , the second now i use maybe right.

   

thanks.

0 Likes