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

Using app_timer_cnt_get(), get zero!

Hi there, I have done search and know there are several similar questions, but unfortunately I didn't find any useful answer after carefully reading through them, thus I decide to post a new question, and wish it could help others in the future.

As suggested by the answers under the similar questions, I have called app_timer_start(), and the timer is of APP_TIMER_MODE_REPEATED. I did debug, and traced to the function rtc1_counter_get(), but the NRF_RTC1->COUNTER is ZERO.

Could anyone give me any hints?

The background is I am studying the example in this timer tutorial, and want to obtain the elapsed time between two events with function app_timer_cnt_diff_compute The main() function is as follows,

int main(void)

 {

SEGGER_RTT_ConfigUpBuffer(0,NULL,NULL,0,SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL);

// Request LF clock.
lfclk_request();

// Initialize the application timer module.
APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);

// Configure GPIO's.
gpio_config();

// Create application timers.
create_timers();



SEGGER_RTT_WriteString(0, "SEGGER Real-Time-Terminal: \r\n\r\n");


// Main loop.
while (true)
{
    // Wait for interrupt.
    __WFI(); // take care, may change!
}
}

The relevant code is as follows, where gpio_config() calls nrf_drv_gpiote_in_init, which further calls function gpiote_event_handler.

static void gpio_config()
{
.......
// Configure input pins for buttons, with separate event handlers for each button.
err_code = nrf_drv_gpiote_in_init(BUTTON_1_PIN, &in_config, gpiote_event_handler);
APP_ERROR_CHECK(err_code);
.......


 }

 //Button event handler.
 void gpiote_event_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
 {

  uint32_t watchdog_timer;
  uint32_t app_timer_cur;
  uint32_t app_timer_diff;


uint32_t err_code;
static uint32_t timeout = 0;
switch (pin)
{
case BUTTON_1_PIN:
    err_code = app_timer_start(m_led_a_timer_id, APP_TIMER_TICKS(200, APP_TIMER_PRESCALER), NULL);
    APP_ERROR_CHECK(err_code);
    /* Set watchdog timer */
    app_timer_cnt_get(&watchdog_timer);

    break;
case BUTTON_2_PIN:
    err_code = app_timer_stop(m_led_a_timer_id);
    APP_ERROR_CHECK(err_code);

    /* Check for a watchdog timeout */
    app_timer_cnt_get(&app_timer_cur);
    app_timer_cnt_diff_compute(app_timer_cur, watchdog_timer, &app_timer_diff);
    SEGGER_RTT_printf(0, "app_timer_diff is: %u \r\n", app_timer_diff);

    break;
case BUTTON_3_PIN:
    // Start single shot timer. Increase the timeout with 1 second every time.
    timeout += 1000;
    err_code = app_timer_start(m_led_b_timer_id, APP_TIMER_TICKS(timeout, APP_TIMER_PRESCALER), NULL);
    APP_ERROR_CHECK(err_code);
    break;
case BUTTON_4_PIN:
    nrf_drv_gpiote_out_set(LED_2_PIN);
    break;
default:
    break;
}
}

Many thanks!

Parents Reply Children
No Data
Related