• AVR Freaks

I2C Check for NACK?

Author
billreinhold
Super Member
  • Total Posts : 186
  • Reward points : 0
  • Joined: 2011/02/10 16:35:50
  • Location: Atlanta, GA
  • Status: offline
2019/06/12 12:44:01 (permalink)
0

I2C Check for NACK?

I'm trying to implement acknowledge polling for a 24LC512 I2C EEPROM, meaning sending a control byte for a Write command and checking for ACK or NACK. 
 
A write command works fine with the device correctly sending ACKs during the transfer. 
 
I then send control bytes as follows and expect a call to DRV_I2C_TransferStatusGet ( i2c_driver_handle_, buffer_handle ) to return DRV_I2C_BUFFER_EVENT_ERROR if a NACK is received, but it does not.  Once the transfer is complete and the NACK has been received, this call returns DRV_I2C_BUFFER_EVENT_COMPLETE.
 
How can I check for a NACK?
 
    DRV_I2C_BUFFER_HANDLE buffer_handle = DRV_I2C_Transmit ( 
         i2c_driver_handle_,
         device_address_,
         NULL,
         0,
         NULL);

 
I'm using the bit-bang version of the Harmony I2C driver with a PIC32MZ2048EFH144.
post edited by billreinhold - 2019/06/12 12:47:00
#1

9 Replies Related Threads

    GDA
    Junior Member
    • Total Posts : 78
    • Reward points : 0
    • Joined: 2015/08/31 16:46:40
    • Location: 0
    • Status: offline
    Re: I2C Check for NACK? 2019/06/13 11:00:30 (permalink)
    0
    Interesting.
    You are sending no bytes.  Is that correct?  You are initiating the I2C transaction with the address of the slave, but then sending it no bytes.
     
    Assuming I am not reading something wrong, I think the slave is acknowledging its address, and then, because you have specified 0 data, the master is sending the stop signal.  Thus, the transaction you are commanding is indeed completing successfully.
     
    I'm not sure I have ever tried that.
    You might try sending the Wrong address to the slave.  That should result in a NACK.
     
     
    Having said all that, are you sure you don't want something like this:
     
     const char COMMAND[] = "A"    ;I am not sure what "control byte" you need.
    DRV_2C_BUFFER_HANDLE buffer_handle = DRV_I2C_Transmit (
             i2c_driver_handle_,
             device_address_,
             COMMAND,
             1,
             NULL);
     
    I hope that helps.
    #2
    billreinhold
    Super Member
    • Total Posts : 186
    • Reward points : 0
    • Joined: 2011/02/10 16:35:50
    • Location: Atlanta, GA
    • Status: offline
    Re: I2C Check for NACK? 2019/06/17 16:13:16 (permalink)
    0
    gary.attarian
    You are sending no bytes.  Is that correct?  You are initiating the I2C transaction with the address of the slave, but then sending it no bytes.

     
    Yep, correct, no bytes.  Check out the device datasheet page 12:  
    http://ww1.microchip.com/downloads/en/devicedoc/21754m.pdf 
     
    And it works as advertised -- the EEPROM does not ACK until it's ready from the previous write, so the poll results in a series of control bytes with no ACK and then when it finishes the write cycle and is ready for another operation it returns the ACK. 
     
    But like you said, the master does complete the transaction because there's just the one byte and regardless of ACK or NACK, Harmony returns successfully with no way of determining which.
     
    If I add an address byte, that will correctly return the fail condition, but once it's ready and the ACK is returned, it seems to start the internal EEPROM write cycle so it's not ready again.
     
    It appears the only way to make this work correctly would be to write a custom driver that checks the ACKSTAT bit, but the PIC32MZ has a problem with I2C that necessitates use of the bit bang driver and I'm not going to write my own.  I guess I could look at modifying the Harmony code.
    #3
    ric
    Super Member
    • Total Posts : 22310
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: I2C Check for NACK? 2019/06/17 16:26:33 (permalink)
    0
    gary.attarian
    ..
    Assuming I am not reading something wrong, I think the slave is acknowledging its address, and then, because you have specified 0 data, the master is sending the stop signal. 
    ...

    No, if the slave is busy it will NOT acknowledge its address.
    That is the whole point of "ACK polling".
     

    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!
    #4
    billreinhold
    Super Member
    • Total Posts : 186
    • Reward points : 0
    • Joined: 2011/02/10 16:35:50
    • Location: Atlanta, GA
    • Status: offline
    Re: I2C Check for NACK? 2019/06/17 16:28:21 (permalink)
    0
    But the question is, does Harmony support ACK polling?  Am I missing something?
    #5
    ric
    Super Member
    • Total Posts : 22310
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: I2C Check for NACK? 2019/06/17 16:32:04 (permalink)
    0
    Sorry, don't know. I've never used Harmony.
    I'm just supporting your position that this SHOULD work.
     

    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!
    #6
    GDA
    Junior Member
    • Total Posts : 78
    • Reward points : 0
    • Joined: 2015/08/31 16:46:40
    • Location: 0
    • Status: offline
    Re: I2C Check for NACK? 2019/06/18 10:57:00 (permalink)
    0
    If the slave device does not ACK the address (control byte in your case) then Harmony should report an error.
     
    Are you sure that the slave is NACKING something?
     
    You might be able to simulate something like this by disconnecting the slave and seeing that your transaction is indeed NACKED.  Or less destructively by changing the slave address byte in your write command to something incorrect.
     
    It has been a long time since I looked, but I am pretty sure that the driver will report an error if you write an address which does not get acknowledged by a slave.
     
    I am assuming that you have the appropriate pullups?
    #7
    billreinhold
    Super Member
    • Total Posts : 186
    • Reward points : 0
    • Joined: 2011/02/10 16:35:50
    • Location: Atlanta, GA
    • Status: offline
    Re: I2C Check for NACK? 2019/06/18 11:11:40 (permalink)
    0
    gary.attarianIf the slave device does not ACK the address (control byte in your case) then Harmony should report an error.

     
    I could not agree more.  That is of course exactly what I expected.

    The logic analyzer clearly shows the NACK but Harmony does not report an error unless the operation does not complete.  In this case, since I'm only sending one byte the operation does complete and Harmony does not report an error.
     
    Yes I have the pullups.  As I mentioned in my initial post, a write command works fine with the device correctly sending ACKs during the transfer.  And then the device correctly NACKs the control byte while it's busy with its internal write cycle.  And then it ACKs when the write cycle is complete.  And as far as I can tell Harmony cannot be used to determine when that happens.
    #8
    GDA
    Junior Member
    • Total Posts : 78
    • Reward points : 0
    • Joined: 2015/08/31 16:46:40
    • Location: 0
    • Status: offline
    Re: I2C Check for NACK? 2019/06/18 12:26:46 (permalink)
    0
    Hmmmm.....
     
    Ok, I just took a quick trip down memory lane and looked into the Bit Bang Driver Code.  I think I may have found a bug.  
    If you can try an experiment for me (I don't have a setup right now to test this), please use the DRV_I2C_Transmit_Then_Receive function.  With size 0 receive buffer, obviously.  I'm not certain, but it looks like perhaps the check for the NACK of the address only occurs if there are bytes ready to be transmitted, or if the driver expects to switch to receive mode.
     
    #9
    billreinhold
    Super Member
    • Total Posts : 186
    • Reward points : 0
    • Joined: 2011/02/10 16:35:50
    • Location: Atlanta, GA
    • Status: offline
    Re: I2C Check for NACK? 2019/06/18 17:31:27 (permalink)
    0
    gary.attarian
    ...please use the DRV_I2C_Transmit_Then_Receive function.  

     
    I tried it... it also returns DRV_I2C_BUFFER_EVENT_COMPLETE after a NACK.
    #10
    Jump to:
    © 2019 APG vNext Commercial Version 4.5