Writing Scheduler/RTOS in XC8

618 views Asked by At

I have an interest in writing a scheduler/RTOS project in XC8 using an enhanced MCU with access to the hardware stack.

I am trying to figure out how to control the creation of the software stacks so each task's software stack will get a certain range in the general purpose ram.

Conceptually this is all easy to program in ASM but I want to be able to write C programs and have the software stacks for each task be put into the right address space.

There doesn't appear to be an option to create a separate software stack for a certain section of code or even create multiple software stacks - how do I do it?

Thanks

1

There are 1 answers

3
Clifford On

Stack switching is the responsibility of teh scheduler,not teh compiler - so you will not find a compiler option for that. You have to implement that in the scheduler you are intending to write - that is in fact most of what a scheduler does.

In an RTOS, switching context involves storing all the registers relating to one thread of execution and replacing them with those of another. This includes replacing the stack-pointer - that is how you switch stacks between threads. A context switch is completed when the program-counter register is loaded effecting a jump to the new thread's last execution point (with all its registers, including the stack-pointer restored.

The context switch itself necessarily involves at least a small amount of assembler code, but much of it may still be written in C, and tasks themselves may be written in C.. A good description of a simple RTOS scheduler is provided in Jean Labrosse's book on μC/OS-II - freely available in PDF. A PIC18 port of μC/OS-II is described here with download.