/* Reflow Oven Controller
*
* Copyright (C) 2020  Mario Hüttel <mario.huettel@gmx.net>
*
* This file is part of the Reflow Oven Controller Project.
*
* The reflow oven controller is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* GDSII-Converter is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the reflow oven controller project.
* If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef __STACK_CHECK_H__
#define __STACK_CHECK_H__

#include <stdint.h>

#define STACK_CHECK_MIN_HEAP_GAP 16UL

int32_t stack_check_get_usage();

int32_t stack_check_get_free();

static inline int stack_check_collision()
{
	int ret = 0;
	int32_t free_space = stack_check_get_free();

	if ((unsigned int)free_space < STACK_CHECK_MIN_HEAP_GAP) {
			ret = -1;
	}

	return ret;
}

static inline uint32_t read_stack_pointer()
{
	uint32_t stack_pointer;

	__asm__ __volatile__ ("mov %0, sp\n\t" : "=r"(stack_pointer) : : );

	return stack_pointer;
}

#endif /* __STACK_CHECK_H__ */
