Nick Gammon wrote:
Quote from: jfhaugh on Today at 05:11:08 PM
Cool! So, in my ISR I save the flags and PC in a task state structure, the load the previous saved flags from a different task state structure, stack the PC, and RETI?
Just remind me of how you are going to handle stack-based variables.
Point of information ... wouldn't all those context switch areas and stacks for multiple tasks eat up a
lot of the lowly 2KB RAM space of the mega328?
FWIW and JFTHOI, last year I wrote a rather largish program to test the idea of using the state-machine
scheme I outlined above to handle multiple program levels and multiple tasks. (it was on a 40-MHZ PIC24
and not Arduino, however).
The nice thing is you don't have context switch areas or RAM usage explosion, and by coding the
state-machines well, you still can get pretty good, though not perfect, time-scheduling. I did use
interrupts for the time-critical stuff.
The state-machines can be used for both timed-coded and non-time-coded tasks. If the task is
time-coded [as the OP's original loops], then the overhead is near zero when the task timeout
does not occur. Jump in, check time, return. I'd guess it's much much less than the
context-switching overhead in a preemptive multitasker.
If the task is non-time-coded and takes a fair amount of processing to complete, then the onus is on
the coder to limit the amount of processing done on each entry into the task. One can have many
successive states in the task and do a limited amount of processing on each entry into the task.
All in all, I was amazed at how well the idea worked, and how much simpler it was than implementing
a preemptive context-switching multitasker might have been.
Comments?