Convert timer value to wall time

tomkcook
Posts: 5
Joined: Sun Jun 11, 2017 12:25 pm

Convert timer value to wall time

Postby tomkcook » Sun Jun 11, 2017 12:32 pm

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

tomkcook
Posts: 5
Joined: Sun Jun 11, 2017 12:25 pm

Re: Convert timer value to wall time

Postby tomkcook » Mon Jun 12, 2017 8:29 am

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.

ESP_igrr
Posts: 2072
Joined: Tue Dec 01, 2015 8:37 am

Re: Convert timer value to wall time

Postby ESP_igrr » Mon Jun 12, 2017 9:31 am

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 5451 times

Who is online

Users browsing this forum: Majestic-12 [Bot] and 132 guests