/*******************************************************************************
* Copyright (C) 2019 China Micro Semiconductor Limited Company. All Rights Reserved.
*
* This software is owned and published by:
* CMS LLC, No 2609-10, Taurus Plaza, TaoyuanRoad, NanshanDistrict, Shenzhen, China.
*
* BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
* BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
*
* This software contains source code for use with CMS
* components. This software is licensed by CMS to be adapted only
* for use in systems utilizing CMS components. CMS shall not be
* responsible for misuse or illegal use of this software for devices not
* supported herein. CMS is providing this software "AS IS" and will
* not be responsible for issues arising from incorrect user implementation
* of the software.
*
* This software may be replicated in part or whole for the licensed use,
* with the restriction that this Disclaimer and Copyright notice must be
* included with each copy of this software, whether used in part or whole,
* at all times.
*/

/****************************************************************************/
/** \file demo_epwm.c
**
**	History:
**	
*****************************************************************************/
/****************************************************************************/
/*	include files
*****************************************************************************/
#include "demo_epwm.h"

/****************************************************************************/
/*	Local pre-processor symbols/macros('#define')
*****************************************************************************/

/****************************************************************************/
/*	Global variable definitions(declared in header file with 'extern')
*****************************************************************************/

/****************************************************************************/
/*	Local type definitions('typedef')
*****************************************************************************/

/****************************************************************************/
/*	Local variable  definitions('static')
*****************************************************************************/


/****************************************************************************/
/*	Local function prototypes('static')
*****************************************************************************/

/****************************************************************************/
/*	Function implementation - global ('extern') and local('static')
*****************************************************************************/

/*****************************************************************************
 ** \brief	EPWM_CMP_Trigger_ADCB
 **			设置EPWM CMP模式
 ** \param [in] none
 ** \return  none
 ** \note	
*****************************************************************************/
void EPWM_CMP_Trigger_ADCB(void)
{
	/*
	(1)设置EPWM运行模式
	*/
	EPWM_ConfigRunMode(  EPWM_COUNT_DOWN 		| 			/*向下计数模式(边沿对齐)*/
						 EPWM_OCU_SYMMETRIC 	|			/*对称模式*/
						 EPWM_WFG_COMPLEMENTARYK   |		/*互补模式*/
						 EPWM_OC_INDEPENDENT);				/*独立输出模式*/
	/*
	(2)设置EPWM时钟周期与死区
	*/
	SYS_EnablePeripheralClk(SYS_CLK_EPWM_MSK);				/*开启EPWM时钟*/
	
	EPWM_ConfigChannelClk( EPWM0, EPWM_CLK_DIV_1);			/*设置EPWM0时钟为APB时钟的1分频*/
	EPWM_ConfigChannelClk( EPWM2, EPWM_CLK_DIV_1);			/*设置EPWM2时钟为APB时钟的1分频*/

	EPWM_ConfigChannelPeriod(EPWM0,  4800);					/*EPWM0通道的周期 = 4800/Fepwm = 100us*/
	EPWM_ConfigChannelPeriod(EPWM2,  4800);					/*EPWM2通道的周期 = 4800/Fepwm = 100us*/

	EPWM_ConfigChannelSymDuty(EPWM0, 1200);					/*EPWM0通道的占空比 = 25%*/
	EPWM_ConfigChannelSymDuty(EPWM2, 2400);					/*EPWM2通道的占空比 = 50%*/	

	EPWM_DisableDeadZone(EPWM_CH_0_MSK | EPWM_CH_1_MSK |
						 EPWM_CH_2_MSK | EPWM_CH_3_MSK);	/*关闭死区*/

	/*
	(3)设置EPWM反向输出
	*/
	EPWM_DisableReverseOutput( EPWM_CH_0_MSK | EPWM_CH_1_MSK |
							   EPWM_CH_2_MSK | EPWM_CH_3_MSK );				/*关闭反相输出*/

	/*
	(4)设置EPWM加载方式
	*/
	EPWM_EnableAutoLoadMode(EPWM_CH_0_MSK | EPWM_CH_2_MSK );	/*设置为自动加载模式*/
	EPWM_ConfigLoadAndIntMode(EPWM0, EPWM_EACH_PERIOD_ZERO);			
	EPWM_ConfigLoadAndIntMode(EPWM2, EPWM_EACH_PERIOD_ZERO);		/*加载点设置为每个周期点和零点*/
/*
	(5)设置EPWM比较计数器
	*/
	EPWM_ConfigCompareTriger(EPWM_CMPTG_0,EPWM_CMPTG_FALLING,EPWM_CMPTG_EPWM0,2000);	/*计数比较器0设置为EPWM0的向下计数比较点，比较点值为2000*/
	EPWM_ConfigCompareTriger(EPWM_CMPTG_1,EPWM_CMPTG_FALLING,EPWM_CMPTG_EPWM0,4000);	/*计数比较器1设置为EPWM0的向下计数比较点，比较点值为4000*/	
	/*
	(6)设置中断
	*/
	EPWM_EnablePeriodInt( EPWM_CH_0_MSK);				/*开启周期点中断*/
	EPWM_EnableDownCmpInt( EPWM_CH_0_MSK);				/*开启向下比较点中断*/
	EPWM_EnableZeroInt( EPWM_CH_0_MSK);					/*开启零点中断*/
	
	NVIC_EnableIRQ(EPWM_IRQn);
	/*
	(7)设置优先级
	*/	
	NVIC_SetPriority(EPWM_IRQn,3);					/*优先级0~3， 0最高、3最低*/
	
	/*
	(8)设置IO口输出
	*/	
	SYS_SET_IOCFG(IOP44CFG, SYS_IOCFG_P44_EPWM3);	
	SYS_SET_IOCFG(IOP45CFG, SYS_IOCFG_P45_EPWM2);	
	SYS_SET_IOCFG(IOP46CFG, SYS_IOCFG_P46_EPWM1);	
	SYS_SET_IOCFG(IOP47CFG, SYS_IOCFG_P47_EPWM0);
	
	EPWM_EnableOutput(EPWM_CH_0_MSK | EPWM_CH_1_MSK |
					  EPWM_CH_2_MSK | EPWM_CH_3_MSK );

	/*
	(9)开启EPWM
	*/		
	EPWM_Start(EPWM_CH_0_MSK | EPWM_CH_2_MSK );
	
	
	/*
	(8)设置GPIO输出
	*/
	SYS_SET_IOCFG(IOP12CFG, SYS_IOCFG_P12_GPIO);	
	GPIO_CONFIG_IO_MODE(GPIO1,GPIO_PIN_2,GPIO_MODE_OUTPUT);		/*指示周期中断*/
	GPIO1->DO_f.P2 = 0;	
	
	SYS_SET_IOCFG(IOP13CFG, SYS_IOCFG_P13_GPIO);	
	GPIO_CONFIG_IO_MODE(GPIO1,GPIO_PIN_3,GPIO_MODE_OUTPUT);		/*指示比较点中断*/
	GPIO1->DO_f.P3 = 0;		
	
	SYS_SET_IOCFG(IOP14CFG, SYS_IOCFG_P14_GPIO);	
	GPIO_CONFIG_IO_MODE(GPIO1,GPIO_PIN_4,GPIO_MODE_OUTPUT);		/*指示比较点中断*/
	GPIO1->DO_f.P4 = 0;		
	
}


