Olin Lathrop wrote: > Harold Hallikainen wrote: >> So... that's the way I did multitasking, but not on a PIC! On the >> PIC, I just have a state variable for each task. Each task always >> starts with a jump table to get to where it left off. Not as clean as >> storing the PC... > > You can implement restricted cooperative multitasking on a PIC 16, with the > restriction being that TASK_YIELD be a macro that may only be invoked from > top level code (not in any subroutines), and that the data stack be empty at > the time. See the CMD module of the HOS project at > http://www.embedinc.com/pic for an example. The way I do it usually is to make every task (or better, the more complex tasks) a state machine, with yields (i.e. returns to the main loop) where appropriate, and call them all in a fixed sequence from the top level main loop (round robin). Since that "round robin" is very fast (all individual tasks run at max short activities between their yields) this becomes almost preemptive -- if you allow the maximum main loop time as latency. And if you keep that low, a cooperative model is not really that different from a "real" preemptive one. One important advantage of a real preemptive system is that you isolate the tasks from each other, and have the means to tame a rogue task. This is important in a multi-user system like Unix, where different tasks are controlled by different users. But in an embedded system where the designer is the only one controlling all tasks, a rogue task (that is, a task that takes too long between cooperative yields) is just a bug that needs to be fixed. After that, you're back to your pseudo-preemptive model again. The other advantage is that once you have all the infrastructure of task scheduling and inter-task communication up, some things (related to timing) become quite comfortable to program. But then, we're talking PICs here... once you're looking for that level of comfort, it's probably time to look at ARMs or similar :) OTOH, by the same margin that it becomes more comfortable, it becomes more unpredictable. Testing a complex real-time system with a preemptive scheduler and various levels of task priorities for odd failure modes is not easy. Gerhard -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist