For ARM EABI the required stack alignment is 8 bytes. This has recently been addressed in V 6.0.0 and onwards by providing a check on any malloced stack pointer. And while this alignment works fine (on most platforms >= 32bit, check previous post: “EABI Stack Alignment on ARM7”), the problem is what happens next.
In task.c line i have the following code:
pxTopOfStack = pxNewTCB->pxStack + ( usStackDepth - ( unsigned short ) 1 ); pxTopOfStack = ( portSTACK_TYPE * ) ( ( ( uintptr_t ) pxTopOfStack ) & ( ( uintptr_t ) ~portBYTE_ALIGNMENT_MASK ) ); /* Check the alignment of the calculated top of stack is correct. */ configASSERT( ( ( ( uintptr_t ) pxTopOfStack & ( uintptr_t ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
(Notice that I replaced unsigned long with uintptr_t in order to make compiling on 8-bit AVR working. To fix previously said bug). The assertion here does not fail.
This ensures that the top of stack pointer is now aligned.
Then there is a call:
pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
And now there is a second alignment assertion, which fails:
/* Check the alignment of the initialised stack. */ configASSERT( ( ( ( uintptr_t ) pxNewTCB->pxTopOfStack & ( uintptr_t ) portBYTE_ALIGNMENT_MASK ) == 0UL ) ); assertion "( ( ( uintptr_t ) pxNewTCB->pxTopOfStack & ( uintptr_t ) portBYTE_ALIGNMENT_MASK ) == 0UL )" failed: file "../src/freertos/tasks.c", line 508, function: xTaskGenericCreate Exit 1
This is because the ARM7tdmi based port stacks an uneven number of elements (17) to the task stack. This does not mean that the task stack itself will be unaligned because the original top of stack was stored in the task context itself. So the second assertion does nothing to ensure that the stack of the task is aligned. Rather it checks if the size of the stored context is even or odd…
I added a pxTopOfStack-; to the beginning of initializeStack to make it stack 18 elements, and now the assertion does not fail, and the code also works…
Is this going to be a future requirements that all ports must initialize the task context with a number of elements which corresponds to the stack alignment?