/*
 * stm32f4xx_isr.c
 *
 *  Created on: Nov 1, 2015
 *      Author: tkl
 */

#include <stdint.h>

#include "stm32f4xx.h"
#include "stm32f4xx_low_power.h"
#include "stm32f4xx_isr.h"

#define MAX_SYSCALL_INTERRUPT_PRIORITY 	(5 << (8 - __NVIC_PRIO_BITS))

__attribute__((naked)) uint32_t set_interrupt_mask(void)
{
	__asm volatile												\
	(															\
		"	mrs r0, basepri									\n" \
		"	mov r1, %0										\n"	\
		"	msr basepri, r1									\n" \
		"	bx lr											\n" \
		:: "i" (MAX_SYSCALL_INTERRUPT_PRIORITY) : "r0", "r1"	\
	);

	return 0;
}

__attribute__((naked)) void clear_interrupt_mask(uint32_t new_mask_value)
{
	__asm volatile				\
	(							\
		"	msr basepri, r0	\n"	\
		"	bx lr			\n" \
		:::"r0"					\
	);

	(void) new_mask_value;
}


void enter_isr(void)
{
	exit_low_power();
	set_interrupt_mask();
}

void exit_isr(void)
{
	*((volatile unsigned long *) 0xe000ed04) = 0x10000000;
	clear_interrupt_mask(0);
}

