Skip to main content
Tung Nguyen
Associate II
July 7, 2018
Solved

NACK when reading a register through I2C communication

  • July 7, 2018
  • 2 replies
  • 2930 views
Posted on July 07, 2018 at 04:41

Hi, 

I'm having trouble reading a register in a slave through I2C communication. 

Slave: bq25895 

http://www.ti.com/lit/ds/symlink/bq25895.pdf

In main:

while (1){

   HAL_StatusTypeDef status

=

HAL_OK

;

   uint16_t

return_value

=

0

;

   status

=

HAL_I2C_Mem_Read

(&

hi2c1

,

0xD4

,

(

uint16_t

)(0x07)

,

I2C_MEMADD_SIZE_8BIT

,

&

return_value

,

1

,

1000

);

   

if

(

status

!=

HAL_OK

)

{

printf ('Error');

}

   else {printf ('No error');}

}

Problem: The I2C_FLAG_AF is set since there is no ACK from the slave. 

Tracing the problem down: 

/* Send Slave Address and Memory Address */

if (I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, Timeout, tickstart) != HAL_OK)

/* Wait until TXIS flag is set */

if (I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)

/* Check if a NACK is detected */

if (I2C_IsAcknowledgeFailed(hi2c, Timeout, Tickstart) != HAL_OK)

/* Check if NACK is set inside of 

I2C_IsAcknowledgeFailed

() */

if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)

How do I fix this problem? 

I'm new to this whole thing, so I appreciate any helps. 

Thanks. 

#nack #bq25895
    This topic has been closed for replies.
    Best answer by Tung Nguyen
    Posted on July 08, 2018 at 17:29

    Hey guys,

    Thanks. I figured out the problem. It weren't the software problem, but the hardware. Apparently, the master board had some problem with it, so there was no ACK. I changed to an application board from TI and got the ACK. I'll talk to an electrical engineer on Monday to fix the old board. 

    Appreciate all your helps. 

    2 replies

    David Littell
    Senior II
    July 7, 2018
    Posted on July 07, 2018 at 13:41

    Top-of-the-head questions:

    0. Are the required pull-up resistors installed on the I2C signals?

    1. Do you know for certain that your call to HAL_I2C_Mem_Read() results in I2C activity?  (via a scope or logic analyzer)

    2. Section 8.3.1 in the bq Data Sheet indicates the device wakes up in 'Default Mode' and requires an I2C write and some bit twiddling to get (and stay) in 'Host Mode'.  Are you doing this write?  (I didn't study the Data Sheet to try to determine if I2C reads attempted before it's in Host Mode will be NACKed or not - that kind of annoyance would be just like TI.)

    Tung Nguyen
    Associate II
    July 7, 2018
    Posted on July 07, 2018 at 19:53

    0. Yes. On the master (STM32F746), there are pull-up resistors installed on the SDA and SCL pin before connecting to the slave (bq25895). 

    1.  What steps do you need to go through to set up the scope to measure I2C? How do you know if your measurement indicate I2C activity? 

    2. No, I did not do the write. I did not see any mentions about putting the master in host mode before reading through I2C. But good point, let me confirm with TI.

    Thanks David.  

    T J
    Senior III
    July 8, 2018
    Posted on July 08, 2018 at 01:45

    Use the scope trigger on the clock SCLK pin.

    then try to show both SDA and SCLK at the same time.

    sometimes you have to twiddle the HOLD-OFF to longer than the byte time...

    are you shifting the address up one bit ?

    The first  time I used IIC on STM, the whole thing was running in a few hours... ST has done a great job really...

    Tung Nguyen
    Tung NguyenAuthorBest answer
    Associate II
    July 8, 2018
    Posted on July 08, 2018 at 17:29

    Hey guys,

    Thanks. I figured out the problem. It weren't the software problem, but the hardware. Apparently, the master board had some problem with it, so there was no ACK. I changed to an application board from TI and got the ACK. I'll talk to an electrical engineer on Monday to fix the old board. 

    Appreciate all your helps. 

    Tan Nguyen
    Visitor II
    December 10, 2019

    Hi Tung Nguyen,

    I also get stuck same you, but I use BQ7411 and no ACK when I debug at I2C_IsAcknowledgeFailed. Could you send me the code example?

    Thank you.