/*! ----------------------------------------------------------------------------
 * @file	mutex.c
 * @brief	IRQ interface / mutex implementation
 * @version 5.0
 * @date 18/05/2017
 */

#include "deca_device_api.h"
#include "interrupts.h"
#include "boards.h"

/*! ------------------------------------------------------------------------------------------------------------------
 * Function: decamutexon()
 *
 * Description: This function should disable interrupts. This is called at the start of a critical section
 * It returns the irq state before disable, this value is used to re-enable in decamutexoff call
 *
 * Note: The body of this function is defined in deca_mutex.c and is platform specific
 *
 * input parameters:	
 *
 * output parameters
 *
 * returns the state of the DW1000 interrupt
 */
decaIrqStatus_t decamutexon(void)           
{
	decaIrqStatus_t s = 1;//port_GetEXT_IRQStatus();

	if(s) 
	{
		// Disable External interrupt (interupts from decawave)
		GPIO_INT_disable(PIN_DECA_INT);
	}	
	
	return s ;   // return state before disable, value is used to re-enable in decamutexoff call
}

/*! ------------------------------------------------------------------------------------------------------------------
 * Function: decamutexoff()
 *
 * Description: This function should re-enable interrupts, or at least restore their state as returned(&saved) by decamutexon 
 * This is called at the end of a critical section
 *
 * Note: The body of this function is defined in deca_mutex.c and is platform specific
 *
 * input parameters:	
 * @param s - the state of the DW1000 interrupt as returned by decamutexon
 *
 * output parameters
 *
 * returns the state of the DW1000 interrupt
 */
void decamutexoff(decaIrqStatus_t s)        // put a function here that re-enables the interrupt at the end of the critical section
{
	//need to check the port state as we can't use level sensitive interrupt on the STM ARM
	if(s) 
	{
		// Enable External interrupt (interupts from decawave)
		GPIO_INT_enable(PIN_DECA_INT);
	}
}
