Quality RTOS & Embedded Software

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


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 ]    [ 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.

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