Question

I am working with a small cooperative OS (Salvo), in C, for an embedded target (MSP430).

I have to implement a command handler with a large number of non-reusable implementation codes. Those codes may include context switches, using the OS. During a context switch, the callstack is NOT saved, so I must remain at the same stack level. I am doing things this way, by including c files:

TaskA()
{
    while(1)
    {
         command_code = OS_WaitMsgQ()

         switch(command_code)
             case 0x01:
             {
                 #include CSC0x01_Name_of_the_command.c
             }
             case 0x02:
             {
                 #include CSC0x02_Name_of_the_command.c
                     //OSContextSwitch()?
             }
             //100+ cases like this...
    }
}

This is working right now, but with this approach I loose most of the feature of my IDE when writing command specific codes, and I don't know if it will be manageable when the code will grow.

Is there a better solution?

Also, those command specific codes may use chunks of code that are reusable but that also contain context switches.

Is there something better than:

  • copy and past?
  • an involved and perilous multi-line macro?
  • saving and restoring the stack using assembly for those common functions?

Or, which one is the lesser evil?

Was it helpful?

Solution

You should be able to write each of your commands as inline functions and call them as regular functions. This is a much nicer approach than what you've done, it just leaves it up to the compiler to generate the right code for you. A mess of #include like that will be in no way maintainable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top