/******************** (C) COPYRIGHT 2013 SONiX *******************************
* COMPANY:			SONiX
* DATE:					2013/04
* AUTHOR:				SA1
* IC:						SN32F100
* DESCRIPTION:	Comparator related functions.
*____________________________________________________________________________
* REVISION	Date				User		Description
*	1.0				2013/04/19	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 <SN32F100.h>
#include "system_SN32F100.h"
#include "Comparator.h"

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

/*_____ 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		: Comparator_Init 
* Description	: Initialization of Comparator function
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void Comparator_Init(void)
{
	SN_SYS1->AHBCLKEN_b.CMPCLKEN = 1;					//Enable clock for Comparator	 						
	
	SN_SYS1->APBCP0_b.CMPPRE = 0; 						//Comparator clock source prescaler
	
	NVIC_EnableIRQ(CMP_IRQn);
}

/*****************************************************************************
* Function		: Comparator
* Description	: Start Comparator 
* Input			  : channel - CH_P20, CH_P21, ..., CH_P37
*				        IE - Interrupt enabled or not (ENABLE, DISABLE)
* Output		  : None
* Return		  : None
* Note			  : None
*****************************************************************************/
void Comparator_Channel(uint16_t channel, uint16_t edge, uint16_t IE)
{
	SN_CMP->CMPM &= ~0x1FF;
	
	SN_CMP->CMP_IE &= ~3;
	
	switch (channel)
	{
		case CH_P20:
			break;
		case CH_P21:
			SN_CMP->CMPM |= CH_P21;
			break;
		case CH_P22:
			SN_CMP->CMPM |= CH_P22;
			break;
		case CH_P23:
			SN_CMP->CMPM |= CH_P23;
			break;
		case CH_P24:
			SN_CMP->CMPM |= CH_P24;
			break;
		case CH_P25:
			SN_CMP->CMPM |= CH_P25;
			break;
		case CH_P26:
			SN_CMP->CMPM |= CH_P26;
			break;
		case CH_P27:
			SN_CMP->CMPM |= CH_P27;
			break;
		case CH_P28:
			SN_CMP->CMPM |= CH_P28;
			break;
		case CH_P29:
			SN_CMP->CMPM |= CH_P29;
			break;
		case CH_P210:
			SN_CMP->CMPM |= CH_P210;
			break;
		case CH_P211:
			SN_CMP->CMPM |= CH_P211;
			break;
		case CH_P212:
			SN_CMP->CMPM |= CH_P212;
			break;
		case CH_P213:
			SN_CMP->CMPM |= CH_P213;
			break;
		case CH_P214:
			SN_CMP->CMPM |= CH_P214;
			break;
		case CH_P215:
			SN_CMP->CMPM |= CH_P215;
			break;
		case CH_P30:
			SN_CMP->CMPM |= CH_P30;
			break;
		case CH_P31:
			SN_CMP->CMPM |= CH_P31;
			break;
		case CH_P32:
			SN_CMP->CMPM |= CH_P32;
			break;
		case CH_P33:
			SN_CMP->CMPM |= CH_P33;
			break;
		case CH_P34:
			SN_CMP->CMPM |= CH_P34;
			break;
		case CH_P35:
			SN_CMP->CMPM |= CH_P35;
			break;
		case CH_P36:
			SN_CMP->CMPM |= CH_P36;
			break;
		case CH_P37:
			SN_CMP->CMPM |= CH_P37;
			break;		
	}

	SN_CMP->CMPM |= (edge << 8);	
	
	if (IE == ENABLE)
		SN_CMP->CMP_IE |= IE;	//Enable Comparator edge trigger interrupt 
	else
		SN_CMP->CMP_IE &= IE;
}

/*****************************************************************************
* Function		: Comparator_Enable
* Description	: Enable/Disable Comparator 
* Input			: en - ENABLE, DISABLE
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void Comparator_Enable(uint16_t en)
{
	if (en == ENABLE)
		SN_CMP->CMPM |= (en << 31);			//Enable Comparator
	else
		SN_CMP->CMPM &= ~(en << 31);			//Disble Comparator
}

/*****************************************************************************
* Function		: Comparator_Output
* Description	: P3.8 pin exchanges to comparator output pin
* Input			: en - ENABLE, DISABLE
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void Comparator_Output(uint16_t en)
{
	if (en == ENABLE)
		SN_CMP->CMPM |= (en << 9);			//P3.8 pin exchanges to comparator output pin
	else
		SN_CMP->CMPM &= ~0x200;			//P3.8 pin is GPIO mode
}

/*****************************************************************************
* Function		: CMP_IRQHandler
* Description	: ISR of Comparator interrupt
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
__irq void CMP_IRQHandler(void)
{
	if(SN_CMP->CMP_RIS_b.CMPGIF == 1)
	{	
		SN_GPIO0->DATA ^= 0x01 ;
	
		SN_CMP->CMP_IC_b.CMPGIC = 1;	
	}	
}	
















