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


Cortex-M3 simulation problem.

Posted by Roberto Bonacina on September 30, 2009
I am a newbie in ARM and FreeRTOS.

I am trying to execute a simple program that uses FreeRTOS.
The program has 2 tasks coordinated with a binary semaphore.
Each task takes the semaphore, assigns a variable and then releases the semaphore.
My starting point is given by the STM32F103 demos for IAR and Keil. I simplified them, added my tasks and configured to run in simulation mode (I don't have a physical target yet).

Here is the code added in main.c

int main( void )
#ifdef DEBUG


taskCode = 0;


xTaskCreate(vFirstPrintTask, "First_Task", 128, NULL,
xTaskCreate(vSecondPrintTask, "Second_Task", 128, NULL,

/* Start the scheduler. */

/* Will only get here if there was not enough heap space to
create the idle task. */
return 0;

static void vFirstPrintTask(void *pvParameters)
if (xSemaphoreTake(xSemaphore, portMAX_DELAY) != NULL)
taskCode = 1;

if (xSemaphoreGive(xSemaphore) != pdTRUE)
//printf("Semaphore not released by task 1.\n");

static void vSecondPrintTask(void *pvParameters)
if (xSemaphoreTake(xSemaphore, portMAX_DELAY) != NULL)
taskCode = 2;

if (xSemaphoreGive(xSemaphore) != pdTRUE)
//printf("Semaphore not released by task 2.\n");

When I run the program, the semaphore and the tasks are created, then I can see that the second task is executed once. When exiting from this task, the last assembly instruction executed is POP {r4,pc}, then the PC becomes 0x00000000 and a hard fault is generated. The usage fault register has the INVSTATE bit set.
This happens with both IAR and Keil IDEs.

What am I doing wrong?

Could it be possible to add to FreeRTOS a demo like this, in order to make it possible to run it in evaluation IDEs (32K code limited) and in simulation mode?


RE: Cortex-M3 simulation problem.

Posted by Richard Damon on September 30, 2009
functions that implement tasks should not return, and normally have built int them an infinite loop like a while(1){} or for(;;){} loop. your tasks execute the code once and then return which will cause a crash.

It might be nice if FreeRTOS directed tasks that returned to a routine to handle this, but it currently doesn't and returning from a task function will just crash.

RE: Cortex-M3 simulation problem.

Posted by Richard on September 30, 2009
If you wish to terminate a task it is necessary to delete it. The follow structure works:

void vATask( void )
for( ;; )
/* Do some work here. */

if( some situation arises )

/* Task must be deleted before the end of the function is reached. */
vTaskDelete( NULL );


p.s. I wrote the code in a text editor, indented it all, then pasted it into this text box, and it is appearing correctly in the preview pane at least.

[ 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