cancel
Showing results for 
Search instead for 
Did you mean: 

How does HAL_I2C_IsDeviceReady work?

Alessio Caligiuri
Associate II
Posted on October 19, 2017 at 20:38

Hi all,

I'm dealing with an I2C memory connected to a STM32F401 MCU and I'd like to check at runtime if the device is ready (or if something is wrong, e.g. with wirings).

I'm trying to use the HAL_I2C_IsDeviceReady function but reading the ST documentation I can't understand completely how it works. So, what is the difference between the ' trials' and the ' timeout' parameters?

In many examples, I found something like this:

while(HAL_I2C_IsDeviceReady(&hi2c1, EEPROM_I2C_ADDRESS,1, HAL_MAX_DELAY));�?�?

but this is a blocking statement in the case of the device isn't working properly (e.g., not power supplied) and so I'm not satisfied with this solution.

Can anyone help me?

Many thanks!

Alessio

#i2c #hal_i2c_isdeviceready
3 REPLIES 3
john doe
Lead
Posted on October 19, 2017 at 22:06

trials is how many times you want it to try

timeout is how long you want it to wait on each try, i believe in ms if i remember the manual correctly

if you read the code for the function, all it does is send the address and wait for the ACK from the slave.

Posted on October 19, 2017 at 22:37

Dear John, thanks for your reply!

There is a point that I don't understand: how ACK is detected. If I'm not wrong, the ACK is detected by the master during the ninth SCL clock cycle, just after it has sent the address (8 clock cycles). So the master shouldn't wait for the ACK but instead recognize a NACK if ACK is not provided by the slave on the ninth clock cycle.

I don't understand why to wait on each trial. So, what is the purpose of timeout?

Many thanks!

Alessio

Posted on October 21, 2017 at 20:59

what is a NACK?  its the slave pulling the data line down.  the pull-up resistors as part of the i2c spec would pull the lines UP not DOWN.  A NACK isn't a non-response, its an actual, active response by the slave.  If the device is not responding, it cannot overcome the pull-up resistors to pull the line low.  Therefore, if there is no device, or device is not responding, the timeout is required.

anyway, the relevant code is in stm32f4xx_hal_i2c.c which should be in your Drivers/STM32F4xx_HAL_Driver/Src/ directory

the HAL is pretty well commented