/*
 * stm32f4xx_stack.c
 *
 *  Created on: Oct 1, 2015
 *      Author: tkl
 */

#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>

#include "stm32f4xx_stack.h"
#include "queue.h"
#include "thread.h"

stack_t *stack_init(
		void (*task_func)(void *),
		void *arg,
		stack_t *stack_start,
		unsigned int stack_size)
{
	stack_t *stk;
	stk = (stack_t *)(stack_start + stack_size);
	stk--;
	*stk = 0x01000000;				// xPSR
	stk--;
	*stk = (stack_t) task_func;		// PC
	stk--;
	*stk = (stack_t) thread_exit;	// LR
	stk -= 5;						// R12, R3, R2 and R1.
	*stk = (stack_t) arg;			// R0
	stk--;
	*stk = 0xfffffffd;
	stk -= 8;						// R11, R10, R9, R8, R7, R6, R5 and R4.

	return stk;
}
