• AVR Freaks

Hot!Problem to send stop bit I2C to eeprom

Author
dobrosoft
Starting Member
  • Total Posts : 76
  • Reward points : 0
  • Joined: 2019/05/20 04:44:55
  • Location: 0
  • Status: offline
2020/01/30 02:57:01 (permalink)
0

Problem to send stop bit I2C to eeprom

Hello. I'm using P˜IC32MZ2048EFH100 and eeprom 24LC64 and Harmony V2.
 
After some starting problems (PLIB_I2C not works!) I create functions for communicating with setting I2C registers.
 
Now I can write and read bytes from EEPROM. But I have problem with send stop bit to EEPROM. When I'll do it, next communication is not possible. When I not send stop bit in finishing and close I2C, next reading/writing works,
 
What can cause this?
 
Here are functions for comunication
 
void i2c_master_idle(void){

while(I2C1STATbits.TRSTAT);
return;
}
void i2c_master_start(void){

i2c_master_idle();
I2C1CONbits.SEN = 1;
while(I2C1CONbits.SEN);
}
void i2c_master_stop(void){
i2c_master_idle();
I2C1CONbits.PEN = 1;
while(I2C1CONbits.PEN);
}
void i2c_master_restart(void){
i2c_master_idle();
I2C1CONbits.RSEN = 1;
while(I2C1CONbits.RSEN);
}
void i2c_master_ack_nack(int val){
i2c_master_idle();
I2C1CONbits.ACKDT = val;
while(I2C1CONbits.ACKEN);
}
bool i2c_master_send(uint8_t byte){
I2C1TRN = byte;
while (I2C1STATbits.TRSTAT);  
bool ackstat = I2C1STATbits.ACKSTAT; 
if(ackstat)
{
return false;
} else {
return true;
}
}
uint8_t i2c_master_recv(void){
//memset(bRxTemp_Buf,0,sizeof(bRxTemp_Buf));
I2C1CONbits.RCEN = 1;
while(!I2C1STATbits.RBF);
return I2C1RCV;
}
uint16_t readEEPROMdword (uint16_t adresa){

PLIB_I2C_Enable(MY_I2C_ID);
adresa1 = (uint8_t)(adresa >> 8);
adresa2 = (uint8_t)adresa;
uint16_t datahl;
uint8_t datah, datal;
i2c_master_start();
while(!i2c_master_send(slaveAddress)); //when send stop bit, next communication hang here - i2c_master_send write always false
while(!i2c_master_send(adresa1));
while(!i2c_master_send(adresa2));
i2c_master_restart();
while(!i2c_master_send(slaveAddress | 0x01));
datah = i2c_master_recv();
i2c_master_ack_nack(0);
datal = i2c_master_recv();
i2c_master_ack_nack(1);
for(delay=0;delay<10;delay++);
//i2c_master_stop();    //When uncomment this, next communication is not possible
PLIB_I2C_Disable( MY_I2C_ID );

datahl = datah;
datahl = datahl << 8;
datahl |= datal;

return datahl;
}
#1

5 Replies Related Threads

    ric
    Super Member
    • Total Posts : 26973
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Problem to send stop bit I2C to eeprom 2020/01/30 03:14:43 (permalink)
    0
    This
    void i2c_master_ack_nack(int val){
       i2c_master_idle();
       I2C1CONbits.ACKDT = val;
      while(I2C1CONbits.ACKEN);
    }

    Is missing one instruction, should be
    void i2c_master_ack_nack(int val){
      i2c_master_idle();
      I2C1CONbits.ACKDT = val;
      I2C1CONbits.ACKEN = 1;    // start ACK cycle
      while(I2C1CONbits.ACKEN);    // wait until ACK cycle completes
    }

     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    dobrosoft
    Starting Member
    • Total Posts : 76
    • Reward points : 0
    • Joined: 2019/05/20 04:44:55
    • Location: 0
    • Status: offline
    Re: Problem to send stop bit I2C to eeprom 2020/01/30 06:43:22 (permalink)
    0
    Thank you !
    #3
    dobrosoft
    Starting Member
    • Total Posts : 76
    • Reward points : 0
    • Joined: 2019/05/20 04:44:55
    • Location: 0
    • Status: offline
    Re: Problem to send stop bit I2C to eeprom 2020/01/30 08:11:01 (permalink)
    0
    I implement it, but I have similar second problem. When I write data to EEPROM and next I will immediately read it, in first send while(!i2c_master_send(slaveAddress)); in read byte sequence it hangs again. I2C EEPROM never send ACK.
     
    When I debug it - pause after write byte and then play next, it will not hangs, EEPROM will send ACK after send i2c address, ...
     
    post edited by dobrosoft - 2020/01/30 08:26:57
    #4
    ric
    Super Member
    • Total Posts : 26973
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Problem to send stop bit I2C to eeprom 2020/01/30 12:12:30 (permalink)
    0
    That's because the external EEPROM needs about 2ms to complete the write.
    It will NOT ACK any commands until it has finished writing. This is all documented in the EEPROM datasheet.
    You must either:
    [1] Add a manual delay longer than the slowest possible write time documented in the EEPROM datasheet, or
    [2] Do "ACK polling", where you loop sending dummy commands to the EEPROM until you get an ACK back.

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #5
    dobrosoft
    Starting Member
    • Total Posts : 76
    • Reward points : 0
    • Joined: 2019/05/20 04:44:55
    • Location: 0
    • Status: offline
    Re: Problem to send stop bit I2C to eeprom 2020/01/30 23:30:35 (permalink)
    0
    Thank you, yes, it is in data sheet, I didn't read it. My mistake.
    #6
    Jump to:
    © 2020 APG vNext Commercial Version 4.5