Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


Cygnal port optimisation.

Posted by Nobody/Anonymous on 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?

RE: Cygnal port optimisation.

Posted by David Hawks on 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. */

/* 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

RE: Cygnal port optimisation.

Posted by David Hawks on 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.


RE: Cygnal port optimisation.

Posted by Nobody/Anonymous on September 19, 2006
Thanks alot.

RE: Cygnal port optimisation.

Posted by Nobody/Anonymous on 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.

RE: Cygnal port optimisation.

Posted by David Hawks on 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.


[ Back to the top ]    [ About FreeRTOS ]    [ Privacy ]    [ Sitemap ]    [ ]

Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

NXP tweet showing LPC5500 (ARMv8-M Cortex-M33) running FreeRTOS.

Meet Richard Barry and learn about running FreeRTOS on RISC-V at FOSDEM 2019

Version 10.1.1 of the FreeRTOS kernel is available for immediate download. MIT licensed.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


FreeRTOS and other embedded software careers at AWS.

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers


STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner