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
  • If I were to perform a SPIM transaction exactly every 500 us, I would do the following.

    - Use one RTC compare register that publishes a DPPI event.

    - Use one TIMER peripheral configured to perform the START task when the DPPI event from RTC compare is triggered.

    - Use a TIMER compare register, programmed with the < 31.518 microseconds adjustment needed to match 500 microseconds, that triggers a DPPI event. A "shortcut" is programmed as well to stop the TIMER at the same time.

    - Program the SPIM peripheral to listen on the DPPI event from the TIMER to start the transaction.

    - Use an interrupt on the END event from SPIM to process the data, set up for the next SPIM transaction as well as program the timing and DPPI as instructed above for the next transaction to be started at the correct time.

    This method will use the sleep clock and thus very low power between the cpu finished processing the last SPIM transaction result until the RTC wakes up the TIMER.

    Note that the RTC runs at an interval of 1/32768 seconds, which is around 30.518 microseconds. 500 is not divisible by this number, since 500/(1/32768) = 16.384. Therefore a TIMER peripheral must be configured to wait for the last microseconds, to make the result accurate. So, assuming the RTC is turned on all the time, in order to calculate the next rtc and timer compare registers, you need to take into account the previous TIMER peripheral adjustment (before the previous SPIM transaction) and the next TIMER peripheral adjustment (to be applied in the next SPIM transaction). If you are happy with, let's say, performing a SPIM transaction every ~488 or ~519 microseconds instead, you can use the RTC only and skip the TIMER.

    Not sure how to program this with Zephyr in mind though, since I have not used Zephyr.

  • Hi,
    I tested it  without zephyr +/- like you suggested, it is working.
    Looks like integrations with zephyr is the issue.

Reply Children
No Data
Related