[En-Nut-Discussion] Which stack is used by timer created with NutTimerStart()?

Nathan Moore nategoose at gmail.com
Mon Sep 8 15:47:46 CEST 2014

Hey Ole,
I believe that the stack that is used by timer callbacks is the stack of
the thread that happens
to be running just before the timer is called.  This stack will be in
whatever state that that thread
was in at that time, so it could be small.

There are two fairly straight forward ways to deal with this.

The first is to call NutThreadCreate from within your timer callback to
create a new thread with
a high priority and a large stack that does the processing that you were
attempting to do in your
timer callback.  The thread scheduling happens just after timers are
processed, so if this thread's
priority is sufficiently high it will be the next thread processed so it
won't be delayed much.

The second would be to create the thread ahead of time and have it wait on
an event that the
timer callback posts.  This method has a few benefits, but the best one is
that if this is a repeating
timer (an interval timer or just something that you reschedule several
times due to other things that
happen), then you save the overhead of creating a new thread over and over.
 If you create it early
then you reserve that stack space up early and will avoid the chance that
when the timer expires
there won't be enough contiguous memory to create the new thread.

Another benefit to both of these methods is that you should be able to call
more of the Nut API
functions from within these threads than you would have been able to safely
call from a timer
callback function.  From within the timer callback you have to be very
careful not to call a function
that might also call the scheduler to avoid unintended recursion.

Good luck!

More information about the En-Nut-Discussion mailing list