Re: Drift through k_sleep or k_msleep #api
When I need work to operate on a regular basis and not drift I create a work queue and a timer that is setup to submit the work to the queue on a regular basis. This way it doesn’t matter how long it takes to finish the work, the next work run will still start at exactly the right time. main() calls clock_init() to get this started…
#include <stdio.h> #include <stddef.h> #include <stdint.h> #include <zephyr.h> #include <zephyr/types.h>
#define TIME_INC_FREQUENCY 10 // in secs
void my_work_handler(struct k_work *work) { // do whatever here }
K_WORK_DEFINE(my_work, my_work_handler);
void my_timer_handler(struct k_timer *dummy) { k_work_submit(&my_work); }
K_TIMER_DEFINE(my_timer, my_timer_handler, NULL);
void clock_init(void) { k_timer_start(&my_timer, K_SECONDS(TIME_INC_FREQUENCY), K_SECONDS(TIME_INC_FREQUENCY)); }
Lawrence King Principal Developer +1(416)627-7302
From: users@... <users@...>
On Behalf Of Erik Englund
Sent: Wednesday, February 3, 2021 2:38 PM To: Nikolaus Huber <nikolaus.huber@...> Cc: users@... Subject: Re: [Zephyr-users] Drift through k_sleep or k_msleep #api
One possible solution is to create a periodic timer and post to a semaphore in the timer callback. A thread could simply wait on the semaphore in an endless loop.
Linux clock_nanosleep (mostly used togheter with Preempt-rt patch) would be a nice addition to Zephyr, that would solve these kinds of tasks.
Med vänlig hälsning Erik Englund
Den ons 3 feb. 2021 kl 20:31 skrev Nikolaus Huber <nikolaus.huber@...>:
|
|