This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

TWI Optimization Issue?

Hello!  

This is my first time using a Nordic product and I'm still learning the basics.  I am adapting the TWI Sensor example and I'm trying to read data back from an accelerometer.  Unfortunately, it's jumping out of a function before it should (and running right past the breakpoints).  I assume it's an optimization setting but I wasn't sure.  

Below is my basic code:

bool ADXL355_check_status(int address)
{
   ret_code_t err_code;

    m_xfer_done = false;

    uint8_t reg[1] = {WHO_AM_I_Register};
    err_code = nrf_drv_twi_tx(&m_twi, address, reg, sizeof(reg), false);
    APP_ERROR_CHECK(err_code);
    while (m_xfer_done == false);

    m_xfer_done = false;

    err_code = nrf_drv_twi_rx(&m_twi, address, &m_sample, sizeof(m_sample));  <----- Jumps FROM here
    APP_ERROR_CHECK(err_code);     <-----------------  Never makes it to this line

    if(m_sample==WHO_AM_I_RESPONSE)
        return true;
    else
        return false;
}


/**
 * @brief Function for main application entry.
 */
int main(void)
{
    APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    NRF_LOG_DEFAULT_BACKENDS_INIT();

    NRF_LOG_INFO("\r\nTWI sensor example started.");
    NRF_LOG_FLUSH();
    twi_init();
    //LM75B_set_mode();
    ADXL355_check_status(ADXL355_I2C_ADDRESS);

    while (true)
    {
        nrf_delay_ms(500);            <---------------------- Jumps TO here

        do
        {
            __WFE();
        }while (m_xfer_done == false);

        //read_sensor_data();
        NRF_LOG_FLUSH();
    }
}

As I noted above, it runs:

     err_code = nrf_drv_twi_rx(&m_twi, address, &m_sample, sizeof(m_sample));

and never makes it to:

     "APP_ERROR_CHECK(err_code); " line.  

It jumps straight down to:
     
     nrf_delay_ms(500);

The good news is it looks like it's actually sending and receiving the data correctly.  Unfortunately, it's not getting to the data check at the end to provide the expected bool output.

Any idea what I can do to resolve this?  Any help you can provide would be greatly appreciated.  Thanks!

Parents
  • Hmm, it looks like that was indeed the issue.  I'm kind of surprised the check would be optimized out. 

    I do have a related question though.  It has to do with this section of the code:

        if(m_sample==WHO_AM_I_RESPONSE)
            return true;
        else
            return false;

    So, my "WHO_AM_I_RESPONSE" is setup as:

        #define WHO_AM_I_RESPONSE   0x1D

    I can confirm that my m_sample is being returned as a uint8_t with a value of 0x1D.  When I'm comparing those, I'm finding the value of the who am I response is something like 0x0000000000000000000001D or something along those lines.  To resolve this issue, I cast my #define variable so that now my code looks like this:

        if(m_sample==(uint8_t)WHO_AM_I_RESPONSE)
            return true;
        else
            return false;

    I can hover over m_sample and see the value is 0x1d and do the same with the cast variable of the who am I response (which also provides 0x1d) but find that it still returns false. 

    I decided to change the who am I response from a #define to:

        uint8_t WHO_AM_I_RESPONSE = 0x1d;

    in case the issue is related to comparing a #define (even if it's cast properly) to a declared variable but still got the same issue.  Then I cast m_sample to a uint8_t (just in case).  That ALSO failed. 

    So now, I'm changing my optimization setting from 0 to none and still no luck. 

    Is there something wrong with my if statement that I'm not seeing?  If both sides are 0x1d, I don't see how it's possible to be returning false in any of these cases.  Any help you can give me would be greatly appreciated!  Thanks!

Reply
  • Hmm, it looks like that was indeed the issue.  I'm kind of surprised the check would be optimized out. 

    I do have a related question though.  It has to do with this section of the code:

        if(m_sample==WHO_AM_I_RESPONSE)
            return true;
        else
            return false;

    So, my "WHO_AM_I_RESPONSE" is setup as:

        #define WHO_AM_I_RESPONSE   0x1D

    I can confirm that my m_sample is being returned as a uint8_t with a value of 0x1D.  When I'm comparing those, I'm finding the value of the who am I response is something like 0x0000000000000000000001D or something along those lines.  To resolve this issue, I cast my #define variable so that now my code looks like this:

        if(m_sample==(uint8_t)WHO_AM_I_RESPONSE)
            return true;
        else
            return false;

    I can hover over m_sample and see the value is 0x1d and do the same with the cast variable of the who am I response (which also provides 0x1d) but find that it still returns false. 

    I decided to change the who am I response from a #define to:

        uint8_t WHO_AM_I_RESPONSE = 0x1d;

    in case the issue is related to comparing a #define (even if it's cast properly) to a declared variable but still got the same issue.  Then I cast m_sample to a uint8_t (just in case).  That ALSO failed. 

    So now, I'm changing my optimization setting from 0 to none and still no luck. 

    Is there something wrong with my if statement that I'm not seeing?  If both sides are 0x1d, I don't see how it's possible to be returning false in any of these cases.  Any help you can give me would be greatly appreciated!  Thanks!

Children
Related