DA1468x high resolution timer

⚠️
Hi there.. thanks for coming to the forums. Exciting news! we’re now in the process of moving to our new forum platform that will offer better functionality and is contained within the main Dialog website. All posts and accounts have been migrated. We’re now accepting traffic on the new forum only - please POST any new threads at https://www.dialog-semiconductor.com/support . We’ll be fixing bugs / optimising the searching and tagging over the coming days.
6 posts / 0 new
Last post
herrkaefer
Offline
Last seen: 1 year 4 months ago
Joined: 2017-12-16 15:38
DA1468x high resolution timer

Hi Dialog,

I need a high resolution timer to record time elapsed between two GPIO events. Error <100 microseconds is required.

My explorations so far:

1) TIMER1 precision (31.25 microseconds) should be OK. But with `OS_GET_TICK_COUNT()` I can only get 2ms interval.

2) TIMER1->CAPTIM_TIMER_VAL_REG will give me a value loop from 0 to 65535, or about 2ms interval. So I can't use it directly to measure time difference larger than 2ms.

3) Some functions in "hw_timer1.h" e.g. `hw_timer1_get_count`, `hw_timer1_get_value` seems to simply return "TIMER1->CAPTIM_TIMER_VAL_REG"

4) I also see functions like `hw_timer1_get_event1_trigger` `hw_timer1_get_event1_gpio`.  I am not sure how to use them.

 

Can you show me the best way to do this? With TIMER1 or other timer? Any documentation or code sample would be very helpful. Thanks!

Device: 
PM_Dialog
Offline
Last seen: 9 months 1 day ago
Staff
Joined: 2018-02-08 11:03
Hi herrkaefer,

Hi herrkaefer,

You can use timer1 in order to capture 2 gpio events, but you could not achieve more than 2ms interval. Please notice that the timer1 is used for the FreeRTOS systick and the system will be destabilized or stop working if it is disrupted. The Timer1 is the only timer that stays alive when the DA1468x is in sleep mode and it is operating at sleep clock. For capturing two GPIO events, you could use only the Timer1, but the period will be up to 2msec.

Thanks, PM_Dialog

herrkaefer
Offline
Last seen: 1 year 4 months ago
Joined: 2017-12-16 15:38
Thanks for your reply!

Thanks for your reply!

For "period will be up to 2msec", do you mean the time difference between two GPIO events should not larger than 2ms? Or the error of single event measurement is 2ms?

Our two events can be up to 9msec difference, so 2msec interval is not enough :-( And I think I need a more general timer.

My application never goes into sleep mode, so I can use other high requency timer if available.

Good news is I can keep looking at the timer so if it overflows I can get noticed. Could you please give comments on the following two versions of my microsecond time functions? (They don't work well for me so there should be something wrong with them. )

 

- Version 1: 

static inline uint32_t get_time_us() {
    static uint32_t time_us = 0;
    static uint16_t last_count = 0, current_count;

    current_count = TIMER1->CAPTIM_TIMER_VAL_REG;

    if (current_count >= last_count)
        time_us += (current_count - last_count) * 30;
    else
        time_us += ((UINT16_MAX - last_count) + current_count) * 30;
    last_count = current_count;

    return time_us;
}

 

- Version 2: Timer1 has a CAPTIM_TIMER_HVAL_REG register and hw_timer1_get_value() uses it.

static inline uint32_t get_time_us() {
    static uint32_t time_us = 0;
    uint32_t count = hw_timer1_get_value();
    time_us += count * 30;
    return time_us;
}

 

Is there any working demo similar to mine that I can use as a reference? Thanks!

PM_Dialog
Offline
Last seen: 9 months 1 day ago
Staff
Joined: 2018-02-08 11:03
Hi herrkaefer,

Hi herrkaefer,

Let me check it and I will get back to you as soon as possible.

Thanks, PM_Dialog

herrkaefer
Offline
Last seen: 1 year 4 months ago
Joined: 2017-12-16 15:38
Thanks very much!

Thanks very much!

 

Actually I need to precisely measure a waveform duration (several millisecond) on one GPIO pin. I tried to use Timer1's GPIO trigger and interrupt to capture two triggers, one for HW_TIMER1_TRIGGER_RISING and one for HW_TIMER1_TRIGGER_FALLING. To do this, I have to define `dg_configUSE_HW_TIMER1 1` and `dg_configUSER_CAN_USE_TIMER1 1`. However, there are compiling errors of "sys_power_mgr.c". It seems functions in sys_power_mgr.c require `dg_configUSER_CAN_USE_TIMER1` to be zero.

Could you show me the reason, or a working demo which can use timer1's GPIO event capture function? Thanks!

 

PM_Dialog
Offline
Last seen: 9 months 1 day ago
Staff
Joined: 2018-02-08 11:03
Hi herrkaefer,

Hi herrkaefer,

Let’s provide you more inputs I got for the capturing procedure. You are not able to capture 2 (external) events with the usage of Software Timer0 because this timer has not the appropriate hardware capability.  You could not use Software Timer2 as discussed in a previous ticket. The only Software timer which has hardware with capturing capability is Timer1, but it is used from the RTOS. To do so, I would correct the previous statement and you cannot use it for capturing functionality. The only way to capture two events is to configure the Wake-up controller and use timestamps. So, you could configure the WKUP controller for getting 2 events (EVT1, EVT2) triggered and you could use timestamps with the following ways:

  1. Software Timer0 as you did in the attached code, in order to capture the EVT1 and EV2 and calculate their difference. The limitation is that the system should be always in ACTIVE mode, as the Timer0 is shut down when in sleep mode. The configurations are correct, and you are not missing any clock cycle.

 

  1. OS_timer() which can be used in sleep mode but the limitation is the very low granularity (2msec)

 

 

  1. rtc_get() in order to count the number of Low Power clocks and calculate the difference between EVT1 and EVT2. It can be used in sleep of course.

Thanks, PM_Dialog