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

APP_TIMER not working with while

Hi,

I am trying to use APP_TIMER to detect if a sensor isn't responding to I2C Commands. I created and started the timer with the following steps. The project is based on Thread "mtd_coap_client" project.

  1. Enabled Low-Frequency clock,
  2. Created a timer and started it.
  3. Send a command to a sensor on I2C Bus and wait for it to Acknowledge. Sleep during wait.

Here's the code:

//Read the Datasheet for more inforamtion on Configuration Register.
  uint8_t transmitBuffer[3] = {OPT3004_REGISTER_CONFIGURATION, 0xCC, 0x09};
  TWITransmitCompleted = false;
  startTimeoutTimer(1000);
  ret_code_t err_code = nrf_drv_twi_tx(&sensorsTWIInstance, OPT3004_DEVICE_ADDRESS, transmitBuffer, 3, false);
  if(err_code != NRF_SUCCESS) {
    NRF_LOG_ERROR("OPT3004 -> OPT3004Initialisation. Error while setting config register. Error: %d", err_code);
    NRF_LOG_FLUSH();

    return err_code;
  }
  
  while(TWITransmitCompleted == false && timeoutTimerElapsed == false) {
    // Make sure the event register is cleared
    __SEV();
    __WFE();
    //Enter SYSTEM ON Sleep Mode
    __WFE();
  }

The problem I am facing is, if I have:

while(TWITransmitCompleted == false && timeoutTimerElapsed == false) {
    // Make sure the event register is cleared
    __SEV();
    __WFE();
    //Enter SYSTEM ON Sleep Mode
    __WFE();
  }
,

The control never goes to timeout handler. If I remove while(), control goes to timeout handler.

Can you please help me understand what's causing this?

Parents
  • Hello,

    So you are waiting for a timeout or the TWITransmitCompleted, but none of them occurs. 

    Where do you call this piece of code from? Is it in an interrupt or the main() function? If it is in an interrupt, which one?

    What variable types are TWITransmitCompleted and timeoutTimerElapsed? And where are they declared?

    My initial suspicions are:

    1: Either you are calling this from an interrupt with equal or higher priority than the app_timer and the TWI, so that the timer/TWI interrupts are not processed, and this is where you are supposed to set the timeoutTimerElapsed and TWITransmitCompleted to true.

    2: Or you have not declared TWITransmitCompleted or timeoutTimerElapsed as volatile variables, and due to optimization in the compiler, they assume that these are not changed from elsewhere. If this is the case, try to declare them as volatile types:

    volatile bool timeoutTimerElapsed = false;
    volatile bool TWITransmitCompleted = false;

    Best regards,

    Edvin

Reply
  • Hello,

    So you are waiting for a timeout or the TWITransmitCompleted, but none of them occurs. 

    Where do you call this piece of code from? Is it in an interrupt or the main() function? If it is in an interrupt, which one?

    What variable types are TWITransmitCompleted and timeoutTimerElapsed? And where are they declared?

    My initial suspicions are:

    1: Either you are calling this from an interrupt with equal or higher priority than the app_timer and the TWI, so that the timer/TWI interrupts are not processed, and this is where you are supposed to set the timeoutTimerElapsed and TWITransmitCompleted to true.

    2: Or you have not declared TWITransmitCompleted or timeoutTimerElapsed as volatile variables, and due to optimization in the compiler, they assume that these are not changed from elsewhere. If this is the case, try to declare them as volatile types:

    volatile bool timeoutTimerElapsed = false;
    volatile bool TWITransmitCompleted = false;

    Best regards,

    Edvin

Children
Related