/******************** (C) COPYRIGHT 2020 SONiX *******************************
* COMPANY:			SONiX
* DATE:					2020/06
* AUTHOR:				SA1
* IC:						SN32F780/790
* DESCRIPTION:	CT16B0 related functions.
*____________________________________________________________________________
* REVISION	Date				User		Description
* 1.0				2020/06/24	SA1			First release
*
*____________________________________________________________________________
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS TIME TO MARKET.
* SONiX SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL 
* DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT OF SUCH SOFTWARE
* AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN 
* IN CONNECTION WITH THEIR PRODUCTS.
*****************************************************************************/

/*_____ I N C L U D E S ____________________________________________________*/
#include <SN32F790.h>
#include "CT16.h"
#include "CT16B1.h"

/*_____ D E C L A R A T I O N S ____________________________________________*/
volatile uint32_t iwCT16B1_IrqEvent = 0x00; //The bitmask usage of iwCT16Bn_IrqEvent is the same with CT16Bn_RIS

void CT16B1_Init(void);
void CT16B1_NvicEnable(void);
void CT16B1_NvicDisable(void);

/*_____ D E F I N I T I O N S ______________________________________________*/

/*_____ M A C R O S ________________________________________________________*/

/*_____ F U N C T I O N S __________________________________________________*/

/*****************************************************************************
* Function		: CT16B1_IRQHandler
* Description	: ISR of CT16B1 interrupt
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
__irq void CT16B1_IRQHandler(void)
{
	uint32_t iwRisStatus;

	iwRisStatus = SN_CT16B1->RIS;	//Save the interrupt status.

	//Before checking the status, always re-check the interrupt enable register first.
	//In practice, user might use only one or two timer interrupt source. 
	//Ex: Enable only MR0IE and MR3IE ==> No check on MR1IE, MR2IE, and CAP0IE is necessary.
  //User can add the directive pair of "#if 0" and "#endif" pair 
	//to COMMENT the un-used parts to reduce ISR overheads and ROM usage.
	
	//Check the status in oder.
	//MR0
	if (SN_CT16B1->MCTRL_b.MR0IE)				//Check if MR0 IE enables?
	{
		if(iwRisStatus & mskCT16B1_MR0IF)				
		{
			iwCT16B1_IrqEvent |= mskCT16B1_MR0IF;				
			SN_CT16B1->IC = mskCT16B1_MR0IC;	//Clear MR0 match interrupt status
		}
	}
	//MR1
	if (SN_CT16B1->MCTRL_b.MR1IE)				//Check if MR1 IE enables?
	{
		if (iwRisStatus & mskCT16B1_MR1IF)		
		{
			iwCT16B1_IrqEvent |= mskCT16B1_MR1IF;			
			SN_CT16B1->IC = mskCT16B1_MR1IC;	//Clear MR1 match interrupt status
		}
	}
	//MR2
	if (SN_CT16B1->MCTRL_b.MR2IE)				//Check if MR2 IE enables?
	{
		if (iwRisStatus & mskCT16B1_MR2IF)		
		{
			iwCT16B1_IrqEvent |= mskCT16B1_MR2IF;		
			SN_CT16B1->IC = mskCT16B1_MR2IC;	//Clear MR2 match interrupt status
		}
	}
	//MR3
	if (SN_CT16B1->MCTRL_b.MR3IE)				//Check if MR3 IE enables?
	{	
		if (iwRisStatus & mskCT16B1_MR3IF)	
		{
			iwCT16B1_IrqEvent |= mskCT16B1_MR3IF;		
			SN_CT16B1->IC = mskCT16B1_MR3IC;	//Clear MR3 match interrupt status
		}
	}
	//MR4
	if (SN_CT16B1->MCTRL_b.MR4IE)				//Check if MR4 IE enables?
	{	
		if (iwRisStatus & mskCT16B1_MR4IF)	
		{
			iwCT16B1_IrqEvent |= mskCT16B1_MR4IF;		
			SN_CT16B1->IC = mskCT16B1_MR4IC;	//Clear MR4 match interrupt status
		}
	}
	//MR5
	if (SN_CT16B1->MCTRL_b.MR5IE)				//Check if MR5 IE enables?
	{	
		if (iwRisStatus & mskCT16B1_MR5IF)	
		{
			iwCT16B1_IrqEvent |= mskCT16B1_MR5IF;		
			SN_CT16B1->IC = mskCT16B1_MR5IC;	//Clear MR5 match interrupt status
		}
	}
	//MR6
	if (SN_CT16B1->MCTRL_b.MR6IE)				//Check if MR6 IE enables?
	{	
		if (iwRisStatus & mskCT16B1_MR6IF)	
		{
			iwCT16B1_IrqEvent |= mskCT16B1_MR6IF;		
			SN_CT16B1->IC = mskCT16B1_MR6IC;	//Clear MR6 match interrupt status
		}
	}
	//MR7
	if (SN_CT16B1->MCTRL_b.MR7IE)				//Check if MR7 IE enables?
	{	
		if (iwRisStatus & mskCT16B1_MR7IF)	
		{
			iwCT16B1_IrqEvent |= mskCT16B1_MR7IF;		
			SN_CT16B1->IC = mskCT16B1_MR7IC;	//Clear MR7 match interrupt status
		}
	}
	//MR8
	if (SN_CT16B1->MCTRL_b.MR8IE)				//Check if MR8 IE enables?
	{	
		if (iwRisStatus & mskCT16B1_MR8IF)	
		{
			iwCT16B1_IrqEvent |= mskCT16B1_MR8IF;		
			SN_CT16B1->IC = mskCT16B1_MR8IC;	//Clear MR8 match interrupt status
		}
	}
	//MR9
	if (SN_CT16B1->MCTRL_b.MR9IE)				//Check if MR9 IE enables?
	{	
		if (iwRisStatus & mskCT16B1_MR9IF)	
		{
			iwCT16B1_IrqEvent |= mskCT16B1_MR9IF;		
			SN_CT16B1->IC = mskCT16B1_MR9IC;	//Clear MR9 match interrupt status
		}
	}
	//MR10
	if (SN_CT16B1->MCTRL2_b.MR10IE)				//Check if MR10 IE enables?
	{	
		if (iwRisStatus & mskCT16B1_MR10IF)	
		{
			iwCT16B1_IrqEvent |= mskCT16B1_MR10IF;		
			SN_CT16B1->IC = mskCT16B1_MR10IC;	//Clear MR10 match interrupt status
		}
	}
	//MR11
	if (SN_CT16B1->MCTRL2_b.MR11IE)				//Check if MR11 IE enables?
	{	
		if (iwRisStatus & mskCT16B1_MR11IF)	
		{
			iwCT16B1_IrqEvent |= mskCT16B1_MR11IF;		
			SN_CT16B1->IC = mskCT16B1_MR11IC;	//Clear MR11 match interrupt status
		}
	}
	//MR12
	if (SN_CT16B1->MCTRL2_b.MR12IE)				//Check if MR12 IE enables?
	{	
		if (iwRisStatus & mskCT16B1_MR12IF)	
		{
			iwCT16B1_IrqEvent |= mskCT16B1_MR12IF;		
			SN_CT16B1->IC = mskCT16B1_MR12IC;	//Clear MR12 match interrupt status
		}
	}
	//CAP0
	if (SN_CT16B1->CAPCTRL_b.CAP0IE)		//Check if CAP0 IE enables?
	{
		if (iwRisStatus & mskCT16B1_CAP0IF)	//CAP0
		{
			iwCT16B1_IrqEvent |= mskCT16B1_CAP0IF;
			SN_CT16B1->IC = mskCT16B1_CAP0IC;	//Clear CAP0 interrupt status
		}	
	}
}

/*****************************************************************************
* Function		: CT16B1_Init
* Description	: Initialization of CT16B1 timer
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void CT16B1_Init(void)
{
	__CT16B1_ENABLE;
}

/*****************************************************************************
* Function		: CT16B1_NvicEnable
* Description	: Enable CT16B1 timer interrupt
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void CT16B1_NvicEnable(void)
{
	NVIC_ClearPendingIRQ(CT16B1_IRQn);
	NVIC_EnableIRQ(CT16B1_IRQn);
}

/*****************************************************************************
* Function		: CT16B1_NvicDisable
* Description	: Enable CT16B1 timer interrupt
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void CT16B1_NvicDisable(void)
{
	NVIC_DisableIRQ(CT16B1_IRQn);
}
