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 MPU stack pointer alignment

Posted by Joshua Napoli on January 29, 2013
In FreeRTOS-7.3.0, xTaskCreateRestricted gives an assertion failure when checking stack pointer alignment after pxPortInitialiseStack:

ASSERTION FAILED at vendor\FreeRTOS\FreeRTOS\tasks.c:560: ( ( ( unsigned long ) pxNewTCB->pxTopOfStack & ( unsigned long ) ( 0x0007 ) ) == 0UL )

I checked that the stack base and length passed in are both well-aligned. I'm doubtful about the first line of pxPortInitializeStack, "pxTopOfStack--". The initial decrement skips the topmost word of the stack and results in an odd number of words being pushed.


RE: Cortex-M3 MPU stack pointer alignment

Posted by Richard on January 30, 2013
xTaskCreateRestricted() calls xTaskGenericCreate(), which is the only function that actually creates tasks (but is never called directly as it is not part of the public API).

The assert in question passes with the Cortex-M3 port, but a quick look at the code shows why it fails with the Cortex-M3 MPU code. The MPU version has one more item on the context stack, meaning the end stack alignment is different to that with the non MPU version (the assert was added after the development of the MPU port).

The MPU version therefore ends up with 4 byte alignment, instead of the required 8. In the vast majority of cases this is not going to effect your code. The only time it has ever been known to cause a problem on Cortex-M ports is when printf()'ing a floating point number, as in "printf( "%f", a_float_variable)" - in which case 0.0 was always printed out instead of the real value. It is conceivable, although I have never tested it, that using 64-bit numbers with compiler optimisation might also result in incorrect value.

At the top of FreeRTOS/Source/portable/GCC/ARM_CM3_MPU/port.c you will find the function pxPortInitialiseStack(), as referenced in your email. At the top of that function you will see the line pxTopOfStack--; Please change that to instead read "pxTopOfStack -= 2;" and let me know if everything is still ok, and the assertion passes.

[Do *not* just delete the "pxTopOfStack--;" line as otherwise the stack pointer will end up pointing outside of the defined stack region when the first task starts]

Thanks and regards.

RE: Cortex-M3 MPU stack pointer alignment

Posted by Richard on January 31, 2013
I've just tested this now and the stack alignment in the release code is *correct* and the additional line should not be added to port.c.

Instead, the following can be added to portmacro.h (I have done it in the main line code now) to allow you to use the configASSERT() macro.

/* There are an uneven number of items on the initial stack, so 
portALIGNMENT_ASSERT_pxCurrentTCB() will trigger false positive asserts. */
#define portALIGNMENT_ASSERT_pxCurrentTCB ( void )


RE: Cortex-M3 MPU stack pointer alignment

Posted by Joshua Napoli on February 5, 2013
Thank you! I'll clear the stack alignment assertion, as you suggest.

[ 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