cancel
Showing results for 
Search instead for 
Did you mean: 

I2C: where is data from HAL_I2C_Master_Receive_IT()?

Garcia.Steve
Associate II
Posted on January 08, 2016 at 23:50

Hi,

I'm attempting to read an I2C device withHAL_I2C_Master_Receive_IT(). A logic analyzer shows the data coming across the I2C bus, but where does it go? This is a st32f405, IAR, and CubeMx generated code. Given the logic analyzer results, it seems the I2C bus and CubeMx code is fundamentally working.

uint8_t value8;<
br
><
br
>value8 = 0x79;<
br
>halResult = HAL_I2C_Master_Receive_IT(&hi2c1, (uint16_t)FUEL_GAUGE_I2C_ADDRESS, &value8, (uint16_t)1);<
br
>

As I read the HAL documentation, the value read from I2C should be placed at value8. It's not. What am I missing? A more complete code block is below. The initial write is to configure the I2C slave for the following read. Everything appears correct on the logic analyzer.

uint16_t tmpValue;<
br
> uint8_t value8;<
br
> HAL_StatusTypeDef halResult;<
br
> <
br
> aTxBuffer[0] = 0x06; // cmd<
br
> aTxBuffer[1] = 0x07; // cmd<
br
> <
br
> halResult = HAL_I2C_Master_Transmit_IT(&hi2c1, (uint16_t)FUEL_GAUGE_I2C_ADDRESS, (uint8_t*)&aTxBuffer[0], (uint16_t)2);<
br
> if (halResult != HAL_OK)<
br
> {<
br
> return false;<
br
> }<
br
> <
br
> // Wait for the end of the transfer<
br
> //<
br
> while (HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY)<
br
> {<
br
> }<
br
> <
br
> HAL_Delay(70);<
br
> <
br
> // Read result<
br
> //<
br
> value8 = 0x79;<
br
> <
br
> halResult = HAL_I2C_Master_Receive_IT(&hi2c1, (uint16_t)FUEL_GAUGE_I2C_ADDRESS, &value8, (uint16_t)1);<
br
> if (halResult != HAL_OK)<
br
> {<
br
> return false;<
br
> }<
br
><
br
> tmpValue = (uint16_t)value8;<
br
> *value = tmpValue;<
br
> <
br
> return true;<
br
>

The result of this block is value8 remains unchanged by the call to HAL_I2C_Master_Receive_IT. I've tried various lengths for the I2C read and the result is consistent. Thre received data doesn't end up where the HAL documentation seems to say it should. Steve #stm32f405-hal-i2c
3 REPLIES 3
Garcia.Steve
Associate II
Posted on January 08, 2016 at 23:55

Seems I didn't do too well with pasting the code blocks.

The first simple code block is:

uint8_t     value8;

value8 = 0x79;

halResult = HAL_I2C_Master_Receive_IT(&hi2c1, (uint16_t)FUEL_GAUGE_I2C_ADDRESS, &value8, (uint16_t)1);

The larger code block is:

    uint16_t tmpValue;

    uint8_t     value8;

    HAL_StatusTypeDef halResult;

    

    aTxBuffer[0] = 0x06;    // cmd

    aTxBuffer[1] = 0x07;    // cmd

    

    halResult = HAL_I2C_Master_Transmit_IT(&hi2c1, (uint16_t)FUEL_GAUGE_I2C_ADDRESS, (uint8_t*)&aTxBuffer[0], (uint16_t)2);

    if (halResult != HAL_OK)

    {

        return false;

    }

    

    //Wait for the end of the transfer

    // 

    while (HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY)

    {

    } 

    

    HAL_Delay(70);

    

    // Read result

    // 

    

    value8 = 0x79;

    

    halResult = HAL_I2C_Master_Receive_IT(&hi2c1, (uint16_t)FUEL_GAUGE_I2C_ADDRESS, &value8, (uint16_t)1);

    if (halResult != HAL_OK)

    {

        return false;

    }

    tmpValue = (uint16_t)value8;

    *value = tmpValue;

    

    return true;

Posted on January 09, 2016 at 02:46

Be careful if the variable you are asking it to write under interrupt is a local/auto variable, in a routine you will leave.

If it's filling under interrupt, I presume the functions are non-blocking, and that the value will not be there until the interrupt actually occurs. You leave immediately, is it even done at that point?

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Garcia.Steve
Associate II
Posted on January 09, 2016 at 04:59

Of course!

Thank you so much, Clive.  I really appreciate it!

Steve