/******************** (C) COPYRIGHT 2021 SONiX *******************************
* COMPANY:	SONiX
* DATE:			2021/02
* AUTHOR:		SA1
* IC:				SN32F780/790
*____________________________________________________________________________
*	REVISION	Date				User		Description
*	1.0				2020/06/24	SA1			1. First release
*	1.2				2021/02/26	SA1			1. Fix typing erros in CMP.h
*																2. Upadte CMP_initfunction.
*
*____________________________________________________________________________
* 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 "CMP.h"

/*_____ D E C L A R A T I O N S ____________________________________________*/

/*_____ D E F I N I T I O N S ______________________________________________*/
#define	CMP0_ENABLE					1		//0: Disable, 1: Enable
#define	CMP1_ENABLE					1		//0: Disable, 1: Enable
#define	CMP2_ENABLE					1		//0: Disable, 1: Enable
#define	CMP_VIREF_ENABLE		1		//0: Disable, 1: Enable

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

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

/*****************************************************************************
* Function		: CMP0_IRQHandler
* Description	: ISR of CMP0 interrupt
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
__irq void CMP0_IRQHandler(void)
{
	SN_CMP->IC |= mskCMP_CM0IC_ENABLE;
}

/*****************************************************************************
* Function		: CMP1_IRQHandler
* Description	: ISR of CMP1 interrupt
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
__irq void CMP1_IRQHandler(void)
{
	SN_CMP->IC |= mskCMP_CM1IC_ENABLE;
}

/*****************************************************************************
* Function		: CMP2_IRQHandler
* Description	: ISR of CMP2 interrupt
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
__irq void CMP2_IRQHandler(void)
{
	SN_CMP->IC |= mskCMP_CM2IC_ENABLE;
}

/*****************************************************************************
* Function		: CMP_init
* Description	: Initialization of CMP0/CMP1/CMP2
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void CMP_init(void)
{
	__CMP_ENABLE_CMPHCLK;

#if CMP_VIREF_ENABLE
	CMP_VIREF_Enable(mskCMP_CMPIREF_VDD);
#endif

#if CMP0_ENABLE
	SN_CMP->CTRL |= (mskCMP_CM0PREF_CM0P2 | mskCMP_CM0NREF_VIREF0 | mskCMP_CM0RS_VIREF_M01D32 | mskCMP_CM0O1_ENABLE | mskCMP_CM0G_FALLING);
	SN_CMP->IE |= mskCMP_CM0IE_ENABLE;
	NVIC_EnableIRQ(CMP0_IRQn);
	__CMP0_ENABLE;
#endif
	
#if CMP1_ENABLE
	SN_CMP->CTRL |= (mskCMP_CM1PREF_CM1P0 | mskCMP_CM1NREF_VIREF1 | mskCMP_CM1RS_VIREF_M15D32 | mskCMP_CM1O2_ENABLE | mskCMP_CM1G_RISING);
	SN_CMP->IE |= mskCMP_CM1IE_ENABLE;
	NVIC_EnableIRQ(CMP1_IRQn);
	__CMP1_ENABLE;
#endif
	
#if CMP2_ENABLE
	SN_CMP->CTRL1 |= (mskCMP_CM2PREF_CM2P1 | mskCMP_CM2NREF_VIREF2 | mskCMP_CM2RS_VIREF_M31D32 | mskCMP_CM2O3_ENABLE | mskCMP_CM2G_RISING);
	SN_CMP->IE |= mskCMP_CM2IE_ENABLE;
	NVIC_EnableIRQ(CMP2_IRQn);
	__CMP2_ENABLE;
#endif
}

/*****************************************************************************
* Function		: CMP_VIREF_init
* Description	: Initialization of CMP0/1/2 
* Input				: mskCMP_CMPIREF_VDD ,mskCMP_CMPIREF_1P5V, mskCMP_CMPIREF_2V, mskCMP_CMPIREF_3V
* Output			: None
* Return			: None
* Note				: None
*****************************************************************************/
void CMP_VIREF_Enable(uint32_t VIREF)
{
	SN_CMP->VIREF = (VIREF | mskCMP_CMPIREFEN_ENABLE);
}
