need help to write variable delay function for different event

Thread Starter

bug13

Joined Feb 13, 2012
2,002
Hi guys

In my embedded system, I usually have a timer to keep time, and I usually have a many different tasks to run in different interval in the main loop. That's how I usually do it:
Code:
while(1)
{
  time_now = get_now();

  if (( time_now - time_xyz_task ) > time_1s )
    do_task_xyz();

  if (( time_now - time_abc_taks ) > time_100ms )
    do_taks_abc();

  // another task ...
  // another task ...
}
It's OK to only have a few task, what if I have 20 different task need to run at different interval, is that a way to write a function like this:
Code:
  schedule( run_every_1s, some_task_1()); 
  schedule( run_every_2s, some_task_2()); 
  // another task ...
  // another task ...
  schedule( run_every_ns, some_task_n());
Thanks guys
 

dannyf

Joined Sep 13, 2015
2,197
It is certainly doable. How to do it will depend on the kind of compromises or complexity you are willing to take.

The simplest would be to keep a system tick that advances periodically. At its advancement, you check for scheduled tasks against event tick. Once an event is due to execute, set a flag for its execution and advance the event tick.

You can encompass all of those parameters in a strict so you can pass that strict to an update function that will do it all for you .

Or you can run a rtos.

BTW, your code isn't overflow safe.
 

ErnieM

Joined Apr 24, 2011
8,377
I believe he means you could overflow your call stack with so many things going on at the same time. It is an understated problem with small processors with limited memory that can lead to troublesome hard to reproduce almost random errors and crashes.

What I do see in your first cut is you complete one task before beginning another, which keeps the stack as small as possible.

What I don't see is any sort of prioritizing the routines. If the 1 sec interval is running do you stop it for the one running every millisecond? If you do... Up pops the overflow problem.

Writing direct code for multiple events may be cleaner than some general routine where events are registered and certainly takes less ram for more space for the stack.

A general routine is possible as you can create a structure to save the time interval and a function pointer, and call the pointer when the time is expired.
 

Thread Starter

bug13

Joined Feb 13, 2012
2,002
I believe he means you could overflow your call stack with so many things going on at the same time. It is an understated problem with small processors with limited memory that can lead to troublesome hard to reproduce almost random errors and crashes.

What I do see in your first cut is you complete one task before beginning another, which keeps the stack as small as possible.

What I don't see is any sort of prioritizing the routines. If the 1 sec interval is running do you stop it for the one running every millisecond? If you do... Up pops the overflow problem.

Writing direct code for multiple events may be cleaner than some general routine where events are registered and certainly takes less ram for more space for the stack.

A general routine is possible as you can create a structure to save the time interval and a function pointer, and call the pointer when the time is expired.
Thank your for your replay, that help a lot!!
 

John P

Joined Oct 14, 2008
2,025
That code is perfectly protected against stack overflow, because it's all based on polling, with no interrupts at all.

I wouldn't call it "overflow", but there might be another vulnerability. What if (let's say) the 1-second task takes a long time to complete, and while it's working, the 100-msec task becomes due, and doesn't get performed, and then the 100-msec interval comes around again? Ultimately you can't prevent this kind of thing except by getting a faster processor or more than one processor, but I'd say you need to be sure that it won't happen (make sure the tasks are all brief) or have a plan in place for what the result will be if it does occur. Which tasks might block others, and how bad are the consequences?
 
Top