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


use local variables in coroutine

Posted by ares.qi on July 4, 2006
hello richard

Do you kown protothread by Adam Dunkels? It's similar to coroutine and is more mature, i think. May be we can expand coroutine based on that.

I have a way to use local variables in each coroutines.

1. we add a field of void * pVar in corCRCB and the initial value is NULL
2. when we enter a coroutine and if pVar is NULL , then we enter the coroutine first time , so we can malloc some space for our local variables. But how many space is machine and compiler dependent.
3. all local variable must use pointers which can point to a space return by malloc.

for example

void vACoRoutineFunction( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )

//define local variables here
//do it before crSTART
int *a,*b; //we need 2 int variables
void **c; //we need 1 void* variable

void *pMalloc

if (xHandle->pVar == NULL)
pMalloc = pvPortMalloc( (sizeof)int + (sizeof)int + (sizeof)(void *) );
if (pMalloc)
a = pMalloc;
b = pMalloc +1 //in 32bit machine
c = pMalloc +1 //in 32bit machine
xHandle->pVar = pMalloc;

pMalloc = xHandle->pVar;
a = pMalloc;
b = pMalloc +1 //in 32bit machine
c = pMalloc +1 //in 32bit machine

// Co-routines must start with a call to crSTART().
crSTART( xHandle );

for( ;; )
*a = 10;
// It is fine to make a blocking call here,
crDELAY( xHandle, 10 );

//we can use local variables now
if (*a == 10)


// Co-routines must end with a call to crEND().

RE: use local variables in coroutine

Posted by Richard on July 4, 2006
I have seen older versions of protothreads, but not the latest.

Your idea is quite neat, but I'm not sure if it has many advantages over declaring the variables static in an array, as per the examples. Both ways will convert to code that uses a pointer offset, but the array version will be more portable (probably) with respect to data sizes and byte alignment of pointers.


[ 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