need help to write variable delay function for different event

Discussion in 'Programmer's Corner' started by bug13, May 31, 2016.

  1. bug13

    Thread Starter Senior Member

    Feb 13, 2012
    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 (Text):
    1. while(1)
    2. {
    3.   time_now = get_now();
    5.   if (( time_now - time_xyz_task ) > time_1s )
    6.     do_task_xyz();
    8.   if (( time_now - time_abc_taks ) > time_100ms )
    9.     do_taks_abc();
    11.   // another task ...
    12.   // another task ...
    13. }
    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 (Text):
    2.   schedule( run_every_1s, some_task_1());
    3.   schedule( run_every_2s, some_task_2());
    4.   // another task ...
    5.   // another task ...
    6.   schedule( run_every_ns, some_task_n());
    Thanks guys
  2. dannyf

    Well-Known Member

    Sep 13, 2015
    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.
  3. bug13

    Thread Starter Senior Member

    Feb 13, 2012
    You mean my code in my first post? Can you tell me more? I have no idea my code is not overflow safe.thanks
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    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.
  5. bug13

    Thread Starter Senior Member

    Feb 13, 2012
    Thank your for your replay, that help a lot!!
  6. John P

    AAC Fanatic!

    Oct 14, 2008
    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?