Page 1 of 1

Convert timer value to wall time

Posted: Sun Jun 11, 2017 12:32 pm
by tomkcook
I'm setting up a timer like this:

Code: Select all

    timer_config_t tconf;
    tconf.alarm_en = false;
    tconf.auto_reload = false;
    tconf.counter_dir = TIMER_COUNT_UP;
    tconf.counter_en = true;
    tconf.divider = 1;
    timer_init(TIMER_GROUP_1, TIMER_0, &tconf);
    timer_set_counter_value(TIMER_GROUP_1, TIMER_0, 0);
    timer_start(TIMER_GROUP_1, TIMER_0);
When I read the timer counter (with timer_get_counter_value) how can I convert this to a timer (eg in ms)? I'm currently using:

Code: Select all

    uint64_t value;
    timer_get_counter_value(TIMER_GROUP_1, TIMER_0, &value);
    uint64_t t = value / (TIMER_BASE_CLK / 1000);
But this produces the wrong result. It's hard to say exactly how much wrong, but it's a factor of three or four out. On my system, TIMER_BASE_CLK = 80,000,000 - is that the base frequency of the timer?

Thanks for any help,
Tom

Re: Convert timer value to wall time

Posted: Mon Jun 12, 2017 8:29 am
by tomkcook
Further experimentation suggests that the code above is out by a factor of exactly two. It's not exactly clear why, but it seems that TIMER_BASE_CLK is twice the maximum timer clock frequency.

Re: Convert timer value to wall time

Posted: Mon Jun 12, 2017 9:31 am
by ESP_igrr
It looks like you are setting clock divider to 1. This means that the clock frequency will be 1/2 of the APB_CLK frequency.
Quoting the technical reference manual,
Screen Shot 2017-06-12 at 17.29.41.png
Screen Shot 2017-06-12 at 17.29.41.png (88.98 KiB) Viewed 5449 times