Can not using function contain while loop in setup

Hi, I'm facing an weird problem when I use an function that is contain while loop

void GPS_init_usingWhile(char* configCommand, char *expectedResponse) {
    NRF_LOG_INFO("call GPS init using while");
    while(1){
        NRF_LOG_INFO("in while loop");
    }
}

int main(void){
 
  bool erase_bonds;
  log_init();
  NRF_LOG_INFO("Debug logging for UART over RTT started.");
  timers_init();
  timer_uarte_init();
  uarte_init();
  app_timer_start(m_app_timer_id, CONFIG_UART_INTERVAL, NULL);
  buttons_leds_init(&erase_bonds);
  power_management_init();
  ble_stack_init();
  gap_params_init();
  gatt_init();
  services_init();
  advertising_init();
  conn_params_init();
  advertising_start();

  ret_code_t err_code;
  app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(500, LED_2);
  pwm1_cfg.pin_polarity[1] = APP_PWM_POLARITY_ACTIVE_HIGH;
  err_code = app_pwm_init(&PWM1, &pwm1_cfg, pwm_ready_callback);
  APP_ERROR_CHECK(err_code);
  app_pwm_enable(&PWM1);
  firstInit = true;
  uarteInit_status = true;
  uarteUninit_status = false;
  firstUninit = false;
  timerTimeout_flag = false;
  //GPS_init_usingWhile(GPS_RMC,RMC_response);

  for (;;) {
    idle_state_handle();
    }
}

When I uncommented the 35th line => call GPS_init_usingWhile(GPS_RMC,RMC_response);  => here is my debug terminal

Here is when I commented the 35th => don't call GPS_init_usingWhile(GPS_RMC,RMC_response); => here is my debug terminal

=> so why I can not call the while loop GPS_init_usingWhile(GPS_RMC,RMC_response) ???

Parents
  • Hello,

    I think there is a misunderstanding here. If you uncomment GPS_init_using_While(...), you should see that you are actually inside that while loop. If you press the "pause" button when you are debugging, you should see that this is the case.

    The thing is that when you are in this while loop (that you never exit, because of the while (1), then the nRF52832 will never reach idle_state_handle(), which will actually process and print the log over the UART.

    So either you need to call it only a limited amount of times

    void GPS_init_usingWhile(char* configCommand, char *expectedResponse) {
        uint16_t counter=0;
        NRF_LOG_INFO("call GPS init using while");
        while(1){
            NRF_LOG_INFO("in while loop");
            counter++;
            if (counter > 5)
            {
                break;
            }
        }
    }

    Or you can disable deferred logging in sdk_config.h by setting NRF_LOG_DEFERRED to 0. This will cause the nRF to process the logging during runtime, and not in idle_state_handle().

    Best regards,

    Edvin

  • Hi Edvin, can you explain more clearly about idle_state_handle() what is purpose of this function?
    I think it check if nRF have no anything to handle and keep nRF sleep.

Reply Children
  • Those are the two main things to do in sleep. 

    By default, the examples use Deferred logging in order to not spend too much time on the logging in the application. This is to get closer to the behavior when you turn off the log later (so that the timing in the application is not too much different in a release FW compared to the debug FW). This means that you would need to process the log somewhere, and this is done in the idle_state_handle().

    After the log is processed (if the log is enabled), the device will go to sleep to save power.

    Best regards,

    Edvin

Related