Timer accuracy for micro seconds

Hi,

i defined timer 
K_TIMER_DEFINE(my_timer, my_timer_handler, NULL);

then i am starting  the timer with configuration :

k_timer_start(&my_timer, K_USEC(500), K_USEC(500));
The issue that the 
the handler prints log every 581.5 us instead of 500.
What can be the reason?or how to trigger some handler every 500 us
Thanks
Parents
  • Still have issues if i use counter with timer0

    i set alarm to 500 micro but after 2000 triggers that should print 1000(0.5*2000= 1000 mili)  it prints

    00> I: READ:1748
    00> I: READ:1904
    00> I: READ:1873
    00> I: READ:1751
    00> I: READ:1979
    00> I: READ:1871
    00> I: READ:1784
    00> I: READ:1871

    CONFIG_COUNTER=y
    CONFIG_COUNTER_TIMER0=y

    Here is my code:

    #define THREAD_PRIORITY K_PRIO_PREEMPT(K_HIGHEST_APPLICATION_THREAD_PRIO)
    #define TIMER DT_NODELABEL(timer0)
    #define DELAY 500

    Here is a k_thread_entry_t set_alarm 

    static void set_alarm(void)
    {
        const struct device *const counter_dev = DEVICE_DT_GET(TIMER);
        int err;
    
        printk("Counter alarm sample\n\n");
    
        if (!device_is_ready(counter_dev))
        {
            printk("device not ready.\n");
            return;
        }
    
        counter_start(counter_dev);
    
        alarm_cfg.flags = 0;
        alarm_cfg.ticks = counter_us_to_ticks(counter_dev, DELAY);
        alarm_cfg.callback = test_counter_interrupt_fn;
        alarm_cfg.user_data = &alarm_cfg;
    
        err = counter_set_channel_alarm(counter_dev, ALARM_CHANNEL_ID,
                                        &alarm_cfg);
        printk("Set alarm in %u sec (%u ticks)\n",
               (uint32_t)(counter_ticks_to_us(counter_dev,
                                              alarm_cfg.ticks) /
                          USEC_PER_SEC),
               alarm_cfg.ticks);
    
        if (-EINVAL == err)
        {
            printk("Alarm settings invalid\n");
        }
        else if (-ENOTSUP == err)
        {
            printk("Alarm setting request not supported\n");
        }
        else if (err != 0)
        {
            printk("Error\n");
        }
        while (1)
        {
            static uint16_t c = 0;
            k_sem_take(&timer_semaphore, K_FOREVER);
            static int64_t time = 0;
            if (c == 2000)
            {
                LOG_INF("READ:%lld", k_uptime_delta(&time));
                c = 0;
            }
            if ((c++ % 2) == 0)
            {
    
                struct acc_event *event = new_acc_event();
                EVENT_SUBMIT(event);
            }
        }
    }

    callback that give semaphore to thread loop

    static void test_counter_interrupt_fn(const struct device *counter_dev,
                                          uint8_t chan_id, uint32_t ticks,
                                          void *user_data)
    {
        struct counter_alarm_cfg *config = user_data;
        uint32_t now_ticks;
        uint64_t now_usec;
        int now_sec;
        int err;
    
        k_sem_give(&timer_semaphore);
    
      
    
        err = counter_set_channel_alarm(counter_dev, ALARM_CHANNEL_ID,
                                        user_data);
        if (err != 0)
        {
            printk("Alarm could not be set\n");
        }
    }

    What is wrong?

    Thanks

  • Sorry for late reply maxim. I will try your code snippet right away and see if I can reproduce the same inaccuracies that you see.

Reply Children
No Data
Related