Cygnal port optimisation.

nobody wrote on Monday, September 18, 2006:

      On the Cygnal port page:"Currently the task context is pushed onto the stack and the stack then copied to XRAM before checking to see if a higher priority task is ready to run. This could be modified so the context is pushed onto the stack, but the stack only copied to XRAM if necessary. If no context switch is required the original task would just pop its context off the stack and continue. This change would make the code very 8051 specific (which is why I have not done it)."

     Can anyone give me some suggestion how to do it?

adarkar9 wrote on Monday, September 18, 2006:

I don’t agree with the argument made on the Cygnal port page.  I have performed the suggested optimization on the Keil Cygnal port that I am working on and it works quite nicely.  I don’t agree with the 8051-specific argument because the modification only affects the port files.  My port of vPortYield() is in assembly because Keil’s C51 does not support the _naked attribute, but I can give you the gist of my change in this untested back-port to C:

/* A place to remember the task running before vTaskSwitchContext(). */
static tskTCB * pxOldTCB;

void vPortYield( void ) _naked
    /* Save the execution context onto the stack. */

    /* Remember what task was running before calling vTaskSwitchContext(). */
    pxOldTCB = pxCurrentTCB;

    /* Call the standard scheduler context switch function. */

    /* Only peform stack copy if a new task will be running. */
    if( pxOldTCB != pxCurrentTCB )
        /* Copy the entire stack to XRAM.  This is necessary as the internal
        RAM is only large enough to hold one stack, and we want one per task. */
        portCOPY_STACK_TO_XRAM( pxOldTCB );

        /* Copy the stack of the task about to execute from XRAM into RAM. */
        portCOPY_XRAM_TO_STACK( pxCurrentTCB );

    /* Restore the context of the task from the stack. */

The idea here is to compare the value of pxCurrentTCB before you execute vTaskSwitchContext() with the value of pxCurrentTCB after you execute vTaskSwitchContext().  You only have to perform the stack copy if these values are different.  You must perform the same modification to vTimer2ISR() as well.

The major modification outside of these functions is to change the macros that copy the stack to and from XRAM to accept as a parameter, the address of the TCB to use for the copy.  Alternatively, you could modify portCOPY_STACK_TO_XRAM() to always work with pxOldTCB and portCOPY_XRAM_TO_STACK() to always work with pxCurrentTCB.  My assembly port does the former because it makes the macros more generic.

David Hawks

adarkar9 wrote on Monday, September 18, 2006:

Please note that my above post does have reentrancy problems.  The use of statically-allocated memory (for pxOldTCB) makes vPortYield() non-reentrant.  As I said, this was an untested back-port to C of my assembly modification.  The value of pxCurrentTCB should be pushed on the stack before calling vTaskSwitchContext() and then popped off the stack into registers for the comparison after the call to vTaskSwitchContext().  This is easily done is assembly, but perhaps not so easily done in C.


nobody wrote on Tuesday, September 19, 2006:

     Thanks alot.

nobody wrote on Wednesday, September 20, 2006:

Can either of you tell me what version of SDCC you are using?

If I use SDCC V2.5.0 the demo program runs on my F124 (with mods for the slower max clock rate) but if I compile with SDCC V2.6.0 the program craters.

And yes, I did notice that the stack pointer needed to be moved.

adarkar9 wrote on Wednesday, September 20, 2006:

I am currently using Keil, not SDCC.  I did get the demo to work using SDCC v2.5.0 before I started my port to Keil, though.  I never tried v2.6.0.