Get Current Time with kernel Timer

Hello,

I like to get the current time with a kernel_timer. So I use the function k_uptime_get().

However,with each call the timer starts again from the beginning (from 0).Is there a method how i can bypass that it starts again from 0.

Parents
  • Hello,

    k_uptime_get() does not reset when you use it. Is it saying "0" every time? I'm guessing youre looking at it with a printk or LOG_XXX function. If so it's probably just not printing the 64 bit number correctly. For instance,

    while(1)
    {
        printk("%d\n", k_uptime_get());
        k_msleep(1000);
    }

    Would print "0" every time because the %d is not expecting a 64 bit number. It's only printing the upper 32 bits which is 0 until 4,294,967,295 milliseconds have elapsed.

    If you do this instead:

    while(1)
    {
        printk("%"PRIu64"\n", k_uptime_get());
        k_msleep(1000);
    }

    Then it will print the correct values. Or you could use this if you dont need all 64 bits:

    while(1)
    {
        printk("%d\n", k_uptime_get_32());
        k_msleep(1000);
    }

  • thank you for your answer. 

    enclosed you can see my output. As you can see I have output currenttime. However, with both the system restarts, so that the same number is printed out. After "Tasks_sleep_mode" my system ist entering the IDLE Thread and is waking up with an interrupt. In my Interrupt i am printing the current time. Does the timer starts again after getting out of the IDLE Thread?

  • It should not restart because of exiting the idle thread. That is interesting. Are you able to post your code? Are you using any power savings modes or something?

  • If you turn the ticket  to privat, it would be better and than I can post a part of my code.

    Is it possible?

  • Hi Tal and Louis,

    Thank you a lot for the answers Louis, we appreciate that the community is helping out!

    Tal, DevZone is a public forum as you know, and not all that answers are Nordic Employees.
    Louis is not a Nordic employee, so if I make the case private, he can no longer see the case.

    Instead of making the case private, I suggest that you take one of our samples, for example zephyr/samples/hello_world and re-create the issue in this  sample.
    This has two advantages:

    1. You will be able to share code in a public case.
    2. Creating a minimal sample is a good way to narrow down what went wrong

    If you do so, I will try the sample and see if I can figure out what is wrong.
    And if Louis want to, he can have a look as well.

    Does that work for you?

    Regards,
    Sigurd Hellevik

Reply
  • Hi Tal and Louis,

    Thank you a lot for the answers Louis, we appreciate that the community is helping out!

    Tal, DevZone is a public forum as you know, and not all that answers are Nordic Employees.
    Louis is not a Nordic employee, so if I make the case private, he can no longer see the case.

    Instead of making the case private, I suggest that you take one of our samples, for example zephyr/samples/hello_world and re-create the issue in this  sample.
    This has two advantages:

    1. You will be able to share code in a public case.
    2. Creating a minimal sample is a good way to narrow down what went wrong

    If you do so, I will try the sample and see if I can figure out what is wrong.
    And if Louis want to, he can have a look as well.

    Does that work for you?

    Regards,
    Sigurd Hellevik

Children
Related