Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Real time embedded FreeRTOS mailing list 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT Training




Loading

Task context at static location

Posted by Artur on September 12, 2007
Hi,

The main idea is to allocate space for task context in the TCB e.g.

typedef struct tskTaskControlBlock
{
#if ( configUSE_STATIC_CONTEXT_FRAME == 1 )
portSTACK_TYPE pxCpuContextFrame[ portCPU_CONTEXT_SIZE ];
#else
volatile portSTACK_TYPE *pxTopOfStack;
#endif
...

Basing on my previous experience with another RTOS and ARM platform I found that such solution has real advantages. It simplify and speeds up context save and restore (at least for the ARM7 platform).

The end results seems quite nice. For my benchmarks I got speedup up to 5% (five tasks with the same priority doing only taskYIELD).
I've also observed speedup of interrupt processing. The time between interrupt generation to the first line of C code in the ISR is shorter over 8% (even 9.5% with compiler optimized code) and time from last line of C code in the ISR to the first instrucion in task is shorter nearly 4%.
Tests was performed on LPC2114 using FreeRTOS 4.4 and GCC 4.2 compiler.

Example code for context save for ARM7:

/* push R0 as we are going to use the register */
STMDB SP!, {R0}
LDR R0, =pxCurrentTCB
LDR R0, [R0]
/* seek to R1 location */
ADD R0, R0, #16
STMIA R0, {R1-R14}^
LDR R1, =ulCriticalNesting
LDR R1, [R1]
MRS R2, SPSR
MOV R3, LR
/* pop R0 */
LDMIA SP!, {R4}
/* seek to SPSR location (begin of the context frame) */
SUB R0, R0, #16
/* R1 = ulCriticalNesting, R2 = SPSR, R3 = LR, R4 = R0 */
STMIA R0, {R1-R4}

Example code for context restore for ARM7:
LDR R0, =pxCurrentTCB
LDR R0, [R0]
/* R1 = ulCriticalNesting, R2 = SPSR */
LDMIA R0!, {R1-R2, LR}
LDR R3, =ulCriticalNesting
STR R1, [R3]
MSR SPSR_fsxc, R2
LDMIA R0, {R0-R14}^
NOP
SUBS PC, LR, #4


I welcome any questions and critique.

BTW> Full patchset is available at request.

Regards,
--
Artur Lipowski

RE: Task context at static location

Posted by sashiono on September 12, 2007
do you have an example, or patchset that could be downloaded somewhere. I'd like to give it a try. What negative effects does/could this patch introduce?

RE: Task context at static location

Posted by sotd on September 12, 2007
Why is it faster to store the context in an array rather than on the stack?

Is this change portable?

RE: Task context at static location

Posted by Artur on September 13, 2007
> do you have an example, or patchset that could be
> downloaded somewhere. I'd like to give it a try.

I will send you a code by e-mail.

> What negative effects does/could this patch introduce?
Well, till now I did not observe any bad things.
It is one of reasons why I wrote on this forum - maybe other people will able to spot weak sides of this idea (especially on other architectures).

Regards,
--
Artur Lipowski

RE: Task context at static location

Posted by Artur on September 13, 2007
> Why is it faster to store the context in an array rather
> than on the stack?

Existing code for context switch on ARM requires retrieving of the SP from different processor mode which complicates things (a little bit). Another (small) speed up comes from the fact that we do not have to store SP twice in (TCB and on the stack).
In fact some speedup is also possible with using existing solution (SP as a part of TCB) and to treat stack as an array.

> Is this change portable?

For current implementation the change is optional and it is just a matter of defining configUSE_STATIC_CONTEXT_FRAME in FreeRTOSConfig.h
Obviously changes for all ARM architectures (including AVR32 ;-) ) are very easy to port for other compilers.
I will look at AVR if it can benefit from this idea.

For other architectures I can only to hope for some feedback from this forum.

Regards,
--
Artur Lipowski


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




Copyright (C) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.


Free TCP/IP and file system demos for the RTOS


Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑


FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

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

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

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

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists