diff --git a/ports/cortex-m/atomport.c b/ports/cortex-m/atomport.c index ba4a4bf..49ea73d 100644 --- a/ports/cortex-m/atomport.c +++ b/ports/cortex-m/atomport.c @@ -143,6 +143,11 @@ void archThreadContextInit(ATOM_TCB *tcb_ptr, void *stack_top, struct isr_stack *isr_ctx; struct task_stack *tsk_ctx; + /** + * Enforce initial stack alignment + */ + stack_top = STACK_ALIGN(stack_top, STACK_ALIGN_SIZE); + /** * New threads will be scheduled from an exception handler, so we have to * set up an exception stack frame as well as task stack frame diff --git a/ports/cortex-m/atomport.h b/ports/cortex-m/atomport.h index 1aba5c0..562b84e 100644 --- a/ports/cortex-m/atomport.h +++ b/ports/cortex-m/atomport.h @@ -40,6 +40,10 @@ /* Size of each stack entry / stack alignment size (4 bytes on Cortex-M without FPU) */ #define STACK_ALIGN_SIZE sizeof(uint32_t) +#define ALIGN(x, a) ((x + (typeof(x))(a) - 1) & ~((typeof(x))(a) - 1)) +#define PTR_ALIGN(p, a) ((typeof(p))ALIGN((uint32_t)(p), (a))) +#define STACK_ALIGN(p, a) (typeof(p))((typeof(a))(p) & ~((a) - 1)) + #define POINTER void * #define UINT32 uint32_t