rtel wrote on Monday, November 22, 2004:
Ugliest of all the ports primarily because the compiler is not re-enterant so a whole chunk of global memory has to be saved as part of the context switch.
prvLowInterrupt() is the interrupt service routine. The compatibility mode is used meaning that only one interrupt priority exists. prvLowInterrupt() simple polls all the peripherals that could have caused the interrupt and call the appropriate ISR routine. It first checks to see if the interrupt was caused by the timer and if so calls the Tick ISR, it then checks to see if it was caused by a character Rx and if so calls the serial port Rx interrupt etc.
prvTickISR() is the function that implements the tick ISR (strangely), it is called by prvLowInterrupt() when the interrupt was generated by the timer.
prvTickISR() follows the same sequence as all the tick ISRs in all the ports, namely:
- Save the current context
- Call vTaskIncrementTick() which checks to see if any tasks are woken by the tick ISR.
- If the preemptive scheduler is being used, vTaskSwitchContext() is called to switch to the highest priority task that is ready to run. If the preemptive scheduler is not called this step is skipped as a context switch can only be caused by a call to portYIELD().
- Finally restores the current context which may be different to the context saved if vTaskSwitchContext() switched in another task. This way the ISR will return to the highest priority task.
The tricky bit is the saving and restoring of the context which is very C18 specific. The compiler uses two of the indirect memory registers as stack pointer and frame pointer. Therefore the POSTINC0 calls is effectively pushing variables onto the stack.
1) First some registers are pushed so they get stored in their original state these get modified in the next step so this must happen first.
2) Next the interrupt status flags are stored this is what causes the registers to get modified in step 2.
3) Next all the other necessary CPU registers are stored.
4) Followed by the global memory regions used by the compiler (.tempdata and MATH_DATA sections yuk).
5) Next the hardware stack is saved onto the software stack.
6) The size of the hardware stack is variable, so following the hardware stack values, the number of values pushed onto the stack also gets stored.
7) Finally the new stack top is saved into the TCB.
Hopefully this will help you follow the comments in the code. As I said though this is very specific to the C18 compiler. This is also the trickiest of all the ports. It may be helpful to get the evaluation version of the C18 compiler and step through the code to help understanding.