/*********************************************************
* HC32F334 Hal drivers
*
***********************************************************/
#include "main.h" 
//#include "hc32f3xx.h"
//#include "hc32f3xx_conf.h"
//#include "hc32_ll_hrpwm.h"
//#include "hc32_ll_gpio.h"
//#include "hc32_ll_def.h"

#define ADC_SAMPLE_TIME            (11U)

#define DMA_MODE_SELECT            0 //0:normal
                                     //1:link pointer

const uint16_t u16Temperature[161] =
{
107	,113	,119	,125	,132	,139	,146	,154	,161	,170	,
178	,187	,196	,205	,215	,226	,236	,247	,258	,270	,
282	,295	,308	,321	,335	,349	,364	,379	,395	,411	,
427	,444	,462	,480	,498	,517	,536	,555	,576	,596	,
617	,638	,660	,682	,705	,728	,751	,775	,799	,823	,
848	,873	,898	,924	,950	,976	,1002	,1029	,1056	,1083	,
1110	,1137	,1165	,1192	,1220	,1248	,1276	,1303	,1331	,1359	,
1387	,1415	,1442	,1470	,1497	,1525	,1552	,1579	,1606	,1633	,
1660	,1686	,1712	,1738	,1764	,1790	,1815	,1840	,1865	,1889	,
1913	,1937	,1960	,1984	,2006	,2029	,2051	,2073	,2095	,2116	,
2137	,2157	,2177	,2197	,2216	,2236	,2254	,2273	,2291	,2309	,
2326	,2343	,2360	,2376	,2392	,2408	,2423	,2438	,2453	,2468	,
2482	,2496	,2509	,2522	,2535	,2548	,2560	,2572	,2584	,2596	,
2607	,2618	,2629	,2640	,2650	,2660	,2670	,2679	,2689	,2698	,
2707	,2716	,2724	,2733	,2741	,2749	,2756	,2764	,2771	,2779	,
2786	,2793	,2799	,2806	,2812	,2819	,2825	,2830	,2836	,2842	,
2847	,

};

uint8_t m_au8RxBuf[64] = {0,0,0};
uint8_t m_au8TxBuf[128] = {'#','0','0'};

/*******************************************************************************
 * @brief  Gpio Init.
 * @note   
 * @param  None
 * @retval None
 *****************************************************************************/
void Gpio_Init(void)
{
    stc_gpio_init_t stcGpioInit;
    //LED configuration
    (void)GPIO_StructInit(&stcGpioInit);
	  stcGpioInit.u16PinAttr = PIN_ATTR_DIGITAL;
    stcGpioInit.u16PinState = PIN_STAT_RST;
    stcGpioInit.u16PinDir = PIN_DIR_OUT;																		
    (void)GPIO_Init(LLC_HAL_LED1_Portx, LLC_HAL_LED1_PIN, &stcGpioInit);
	  (void)GPIO_Init(LLC_HAL_LED2_Portx, LLC_HAL_LED2_PIN, &stcGpioInit);
	   
//	  stcGpioInit.u16PinState = PIN_STAT_SET;
//	  (void)GPIO_Init(GPIO_PORT_A, GPIO_PIN_04, &stcGpioInit);
//	  (void)GPIO_Init(GPIO_PORT_F, GPIO_PIN_02, &stcGpioInit);
//	  	(void)GPIO_Init(GPIO_PORT_C, GPIO_PIN_12, &stcGpioInit);
	  /**************SR driver Enable************************/
	  stcGpioInit.u16PinState = PIN_STAT_SET;
	  (void)GPIO_Init(LLC_HAL_SRA_Portx, LLC_HAL_SRA_PIN, &stcGpioInit);
	  (void)GPIO_Init(LLC_HAL_SRB_Portx, LLC_HAL_SRB_PIN, &stcGpioInit);
	  
	  /****************Relay***********************************/
	  stcGpioInit.u16PinState = PIN_STAT_RST;
	  (void)GPIO_Init(LLC_HAL_RELAY_Portx, LLC_HAL_RELAY_PIN, &stcGpioInit);
	
	  /*******************ADC*********************/
	  stcGpioInit.u16PinAttr = PIN_ATTR_ANALOG;
	
	  (void)GPIO_Init(GPIO_PORT_C, GPIO_PIN_00, &stcGpioInit);
    (void)GPIO_Init(GPIO_PORT_C, GPIO_PIN_01, &stcGpioInit);
    (void)GPIO_Init(GPIO_PORT_C, GPIO_PIN_02, &stcGpioInit);
	  (void)GPIO_Init(GPIO_PORT_C, GPIO_PIN_03, &stcGpioInit);
	  (void)GPIO_Init(GPIO_PORT_C, GPIO_PIN_05, &stcGpioInit);
	  (void)GPIO_Init(GPIO_PORT_B, GPIO_PIN_01, &stcGpioInit);
	  (void)GPIO_Init(GPIO_PORT_B, GPIO_PIN_11, &stcGpioInit);
	  (void)GPIO_Init(GPIO_PORT_A, GPIO_PIN_03, &stcGpioInit);
	   /**********************CMP1***********************/
	   /* Port function configuration for CMP */
    (void)GPIO_StructInit(&stcGpioInit);
    stcGpioInit.u16PinAttr = PIN_ATTR_ANALOG;
//		/* CMP1 compare voltage CMP1_INP2(PA2) */
//    (void)GPIO_Init(GPIO_PORT_A, GPIO_PIN_03, &stcGpioInit); 
    /* CMP1 compare voltage CMP1_INM2(PA7) */
    (void)GPIO_Init(GPIO_PORT_A, GPIO_PIN_07, &stcGpioInit); 
    (void)GPIO_Init(GPIO_PORT_A, GPIO_PIN_02, &stcGpioInit);  		
		/* CMP1_OUT */
    GPIO_SetFunc(GPIO_PORT_A, GPIO_PIN_00, GPIO_FUNC_2);
		
		/*******************CMP2 configuration*********************/
		/* CMP2 compare voltage CMP2_INP2(PB0) */
    (void)GPIO_Init(GPIO_PORT_B, GPIO_PIN_00, &stcGpioInit); 
//    /* CMP2 compare voltage CMP2_INM1(PA4) */
//    (void)GPIO_Init(GPIO_PORT_A, GPIO_PIN_04, &stcGpioInit);  		
//		/* CMP2_OUT */
//    GPIO_SetFunc(GPIO_PORT_A, GPIO_PIN_12, GPIO_FUNC_2);
		
		/*******************CMP3 configuration*********************/
		/* CMP3 compare voltage CMP3_INP1(PA3) */
    //(void)GPIO_Init(GPIO_PORT_A, GPIO_PIN_03, &stcGpioInit); 
		(void)GPIO_Init(GPIO_PORT_B, GPIO_PIN_11, &stcGpioInit); 
    /* CMP3 compare voltage CMP2_INM1(PA4) */
    //(void)GPIO_Init(GPIO_PORT_B, GPIO_PIN_04, &stcGpioInit);  		
		/* CMP3_OUT */
    //GPIO_SetFunc(GPIO_PORT_B, GPIO_PIN_12, GPIO_FUNC_2);
		
	  //HRPWM configuration
	  GPIO_HrpwmPinCmd(HRPWM_1A_PIN, ENABLE);
    GPIO_HrpwmPinCmd(HRPWM_1B_PIN, ENABLE);
	  GPIO_HrpwmPinCmd(HRPWM_2A_PIN, ENABLE);
    GPIO_HrpwmPinCmd(HRPWM_2B_PIN, ENABLE);
		GPIO_HrpwmPinCmd(HRPWM_3A_PIN, ENABLE);
    GPIO_HrpwmPinCmd(HRPWM_3B_PIN, ENABLE);
		GPIO_HrpwmPinCmd(HRPWM_4A_PIN, ENABLE);
    GPIO_HrpwmPinCmd(HRPWM_4B_PIN, ENABLE);
		GPIO_HrpwmPinCmd(HRPWM_5A_PIN, ENABLE);
    GPIO_HrpwmPinCmd(HRPWM_5B_PIN, ENABLE);
		GPIO_HrpwmPinCmd(HRPWM_6A_PIN, ENABLE);
    GPIO_HrpwmPinCmd(HRPWM_6B_PIN, ENABLE);
	
	  //SET_REG32_BIT(CM_GPIO->HRPWM, 0x0FFF);
		
	  /* Configure USART RX/TX pin. */
		GPIO_SetDebugPort(GPIO_PIN_TDI, DISABLE);
    GPIO_SetFunc(USART_RX_PORT, USART_RX_PIN, USART_RX_GPIO_FUNC);
    GPIO_SetFunc(USART_TX_PORT, USART_TX_PIN, USART_TX_GPIO_FUNC);
		
		/***************TIMERA****************************************/
		/***************FAN************************************/
	  //(void)GPIO_Init(LLC_HAL_FAN_Portx, LLC_HAL_FAN_PIN, &stcGpioInit);
	  GPIO_SetFunc(TMRA_PWM_PORT_FAN, TMRA_PWM_PIN_FAN, TMRA_PWM_PIN_FAN_FUNC);   
	  /***************AUX power************************************/
	  GPIO_SetFunc(TMRA_PWM_PORT_AURIXPOWER, TMRA_PWM_PIN_AURIXPOWER, TMRA_PWM_PIN_AURIXPOWER_FUNC); 

    /****************************EMB**************************/
    //EMB_MODE_PORTx 	
//	  /* Set PB7 as EMB1_PORT1**/
//    SET_REG32_BIT(CM_GPIO->PFSRB7,(1 << GPIO_PFSR_BFE_POS));
//		//GPIO_SubFuncCmd(GPIO_PORT_B, GPIO_PIN_07, ENABLE);
//	  /* Set PF3 as EMB1_PORT2**/
//    //SET_REG32_BIT(CM_GPIO->PFSRF3,(1 << GPIO_PFSR_BFE_POS));
//		//GPIO_SubFuncCmd(GPIO_PORT_F, GPIO_PIN_03, ENABLE);
//    /* Set PB8 as EMB1_PORT3**/
//    SET_REG32_BIT(CM_GPIO->PFSRB8,(1 << GPIO_PFSR_BFE_POS));
//		//GPIO_SubFuncCmd(GPIO_PORT_B, GPIO_PIN_08, ENABLE);
//	  /* Set PB9 as EMB1_PORT4**/
//    SET_REG32_BIT(CM_GPIO->PFSRB9,(1 << GPIO_PFSR_BFE_POS));
//		//GPIO_SubFuncCmd(GPIO_PORT_B, GPIO_PIN_09, ENABLE);
//	  /* Set PA13 as EMB1_PORT5**/
//    //SET_REG32_BIT(CM_GPIO->PFSRA13,(1 << GPIO_PFSR_BFE_POS));	

//    CLR_REG32_BIT(CM_GPIO->PSPCR,(1 << GPIO_PSPCR_SPFE_0));
//		CLR_REG32_BIT(CM_GPIO->PSPCR,(1 << GPIO_PSPCR_SPFE_1));
//		CLR_REG32_BIT(CM_GPIO->PSPCR,(1 << GPIO_PSPCR_SPFE_2));
//		CLR_REG32_BIT(CM_GPIO->PSPCR,(1 << GPIO_PSPCR_SPFE_3));
//		CLR_REG32_BIT(CM_GPIO->PSPCR,(1 << GPIO_PSPCR_SPFE_4));

    GPIO_SetFunc(GPIO_PORT_B, GPIO_PIN_07, GPIO_FUNC_5);  //EMB1_Port1<------>PB7
		GPIO_SetFunc(GPIO_PORT_F, GPIO_PIN_03, GPIO_FUNC_5);	//EMB1_Port2<------>PC6
		GPIO_SetFunc(GPIO_PORT_B, GPIO_PIN_08, GPIO_FUNC_5);  //EMB1_Port3<------>PB8
    GPIO_SetFunc(GPIO_PORT_B, GPIO_PIN_09, GPIO_FUNC_5);	//EMB1_Port4<------>PB9	
}


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

/*******************************************************************************
 * @brief  Configure TMR0.
 * @note   In asynchronous clock, If you want to write a TMR0 register, you need to wait for
 *         at least 3 asynchronous clock cycles after the last write operation!
 * @param  None
 * @retval None
 *****************************************************************************/
void Ctrl_TMR0_Config(void)
{
    stc_tmr0_init_t stcTmr0Init;
    stc_irq_signin_config_t stcIrqSignConfig;

    /* Enable timer0 and AOS clock */
    FCG_Fcg2PeriphClockCmd(FCG2_PERIPH_TMR0_1, ENABLE);
    //FCG_Fcg0PeriphClockCmd(FCG0_PERIPH_AOS, ENABLE);

	  #if 1
    /* TIMER0 configuration */
    (void)TMR0_StructInit(&stcTmr0Init);
    stcTmr0Init.u32ClockSrc     = TMR0_CLK_SRC_INTERN_CLK;
    stcTmr0Init.u32ClockDiv     = TMR0_CLK_DIV1;
    stcTmr0Init.u32Func         = TMR0_FUNC_CMP;
    stcTmr0Init.u16CompareValue = (uint16_t)TMR0_FUNC_CMPB_Vaule;
    (void)TMR0_Init(CM_TMR0_1, TMR0_CH_B, &stcTmr0Init);
	 #else
	   WRITE_REG32(CM_TMR0_1->CNTBR, 0UL);		//clear conter
     WRITE_REG32(CM_TMR0_1->CMPBR, TMR0_FUNC_CMPB_Vaule);   //set compera value
     //MODIFY_REG32(CM_TMR0_1->BCONR, ((0x1L << 18) | () | ) );
	
	 #endif
    /* Asynchronous clock source, writing to TMR0 register requires waiting for three asynchronous clocks. */
    //DDL_DelayMS(1U);
    //TMR0_HWStopCondCmd(CM_TMR0_1, TMR0_CH_B, ENABLE);
    /* Asynchronous clock source, writing to TMR0 register requires waiting for three asynchronous clocks. */
    //DDL_DelayMS(1U);
    //TMR0_IntCmd(CM_TMR0_1, TMR0_INT_CMP_B, ENABLE);
    /* Asynchronous clock source, writing to TMR0 register requires waiting for three asynchronous clocks. */
    //DDL_DelayMS(1U);
    //AOS_SetTriggerEventSrc(AOS_TMR0, BSP_KEY1_INT_EVT);
   
//		AOS_SetTriggerEventSrc(AOS_ADC1_0, EVT_SRC_TMR0_1_CMP_B);
//		AOS_SetTriggerEventSrc(AOS_ADC2_0, EVT_SRC_TMR0_1_CMP_B);
		
    /* Interrupt configuration */
//    stcIrqSignConfig.enIntSrc    = INT_SRC_TMR0_1_CMP_B;
//    stcIrqSignConfig.enIRQn      = INT006_IRQn;
//    stcIrqSignConfig.pfnCallback = &TMR0_CompareIrqCallback;
//    (void)INTC_IrqSignIn(&stcIrqSignConfig);
//    NVIC_ClearPendingIRQ(stcIrqSignConfig.enIRQn);
//    NVIC_SetPriority(stcIrqSignConfig.enIRQn, DDL_IRQ_PRIO_DEFAULT);
//    NVIC_EnableIRQ(stcIrqSignConfig.enIRQn);
		
		TMR0_Start(CM_TMR0_1,TMR0_CH_B);
}

/*******************************************************************************
 * @brief  Configure EMB0.
 * @note   
 * @param  None
 * @retval None
 *         PB7 <---------->EMB1_PORT1<--------->LLC output ovp
 *         PF3 <---------->EMB1_PORT2<--------->LLC output ocp
 *         PB8 <---------->EMB1_PORT3<--------->PhaseB LLC Primary Current 1 ocp
 *         PB9 <---------->EMB1_PORT4<--------->PhaseA LLC Primary Current 2 ocp
 *         EMB0<----------->EMB1_PORT1<--------->LLC output ovp
 *                          EMB1_PORT2<--------->LLC output ocp
 *                          EMB1_PORT4<--------->PhaseA LLC Primary Current 2 ocp
 *         EMB1<----------->EMB1_PORT1<--------->LLC output ovp
 *                          EMB1_PORT2<--------->LLC output ocp
 *                          EMB1_PORT3<--------->PhaseB LLC Primary Current 1 ocp
 *****************************************************************************/
void EMB_Init(void)
{
	 stc_emb_hrpwm_init_t pstcEmbInit;
	 FCG_Fcg2PeriphClockCmd(FCG2_PERIPH_EMB, ENABLE);
	 
	 
	 EMB_HRPWM_StructInit(&pstcEmbInit);
	 //EMB0 configuration
	 pstcEmbInit.stcPort.stcPort1.u32PortLevel  = EMB_PORT1_DETECT_LVL_HIGH;    //EMB1_Port high
	 pstcEmbInit.stcPort.stcPort1.u32PortFilterDiv = EMB_PORT1_FILTER_CLK_DIV1;                       //filter clock source select 
	 pstcEmbInit.stcPort.stcPort1.u32PortFilterState = EMB_PORT1_FILTER_ENABLE; //EMB1_Port filter enable
	 pstcEmbInit.stcPort.stcPort1.u32PortState  = EMB_PORT1_ENABLE;             //EMB1_Port enable
	
	 pstcEmbInit.stcPort.stcPort2.u32PortLevel  = EMB_PORT2_DETECT_LVL_HIGH;    //EMB1_Port high
	 pstcEmbInit.stcPort.stcPort2.u32PortFilterDiv = EMB_PORT2_FILTER_CLK_DIV1;                       //filter clock source select 
	 pstcEmbInit.stcPort.stcPort2.u32PortFilterState = EMB_PORT2_FILTER_ENABLE; //EMB1_Port filter enable
	 pstcEmbInit.stcPort.stcPort2.u32PortState  = EMB_PORT2_ENABLE;             //EMB1_Port enable
	
	 pstcEmbInit.stcPort.stcPort4.u32PortLevel  = EMB_PORT4_DETECT_LVL_HIGH;    //EMB1_Port high
	 pstcEmbInit.stcPort.stcPort4.u32PortFilterDiv = EMB_PORT4_FILTER_CLK_DIV1;                       //filter clock source select 
	 pstcEmbInit.stcPort.stcPort4.u32PortFilterState = EMB_PORT4_FILTER_ENABLE; //EMB1_Port filter enable
	 pstcEmbInit.stcPort.stcPort4.u32PortState  = EMB_PORT4_ENABLE;             //EMB1_Port enable
	
	 pstcEmbInit.stcCmp.u32Cmp1State = EMB_CMP1_ENABLE;    //
	 pstcEmbInit.stcCmp.u32Cmp3State = EMB_CMP3_ENABLE;
	 EMB_HRPWM_Init(CM_EMB0,&pstcEmbInit);
	
	 //EMB2 configuration
	 EMB_HRPWM_StructInit(&pstcEmbInit);
	 
	 pstcEmbInit.stcPort.stcPort1.u32PortLevel  = EMB_PORT1_DETECT_LVL_HIGH;    //EMB1_Port high
	 pstcEmbInit.stcPort.stcPort1.u32PortFilterDiv = EMB_PORT1_FILTER_CLK_DIV1;                       //filter clock source select 
	 pstcEmbInit.stcPort.stcPort1.u32PortFilterState = EMB_PORT1_FILTER_ENABLE; //EMB1_Port filter enable
	 pstcEmbInit.stcPort.stcPort1.u32PortState  = EMB_PORT1_ENABLE;             //EMB1_Port enable
	
	 pstcEmbInit.stcPort.stcPort2.u32PortLevel  = EMB_PORT2_DETECT_LVL_HIGH;    //EMB1_Port high
	 pstcEmbInit.stcPort.stcPort2.u32PortFilterDiv = EMB_PORT2_FILTER_CLK_DIV1;                       //filter clock source select 
	 pstcEmbInit.stcPort.stcPort2.u32PortFilterState = EMB_PORT2_FILTER_ENABLE; //EMB1_Port filter enable
	 pstcEmbInit.stcPort.stcPort2.u32PortState  = EMB_PORT2_ENABLE;             //EMB1_Port enable
	 
	 pstcEmbInit.stcPort.stcPort3.u32PortLevel  = EMB_PORT3_DETECT_LVL_HIGH;    //EMB1_Port high
	 pstcEmbInit.stcPort.stcPort3.u32PortFilterDiv = EMB_PORT3_FILTER_CLK_DIV1;                       //filter clock source select 
	 pstcEmbInit.stcPort.stcPort3.u32PortFilterState = EMB_PORT3_FILTER_ENABLE; //EMB1_Port filter enable
	 pstcEmbInit.stcPort.stcPort3.u32PortState  = EMB_PORT3_ENABLE;             //EMB1_Port enable
	 
	 pstcEmbInit.stcCmp.u32Cmp2State = EMB_CMP2_ENABLE;    //
	 pstcEmbInit.stcCmp.u32Cmp3State = EMB_CMP3_ENABLE;
	 EMB_HRPWM_Init(CM_EMB1,&pstcEmbInit);
	 
	 //EMB release configuration
	 EMB_SetReleasePwmCond(CM_EMB0, EMB_EVT_PORT1, EMB_RELEASE_PWM_COND_FLAG_ZERO);
	 EMB_SetReleasePwmCond(CM_EMB0, EMB_EVT_PORT2, EMB_RELEASE_PWM_COND_FLAG_ZERO);
	 EMB_SetReleasePwmCond(CM_EMB0, EMB_EVT_PORT4, EMB_RELEASE_PWM_COND_FLAG_ZERO);
	 EMB_SetReleasePwmCond(CM_EMB0, EMB_EVT_CMP,   EMB_RELEASE_PWM_COND_FLAG_ZERO);
	 
	 EMB_SetReleasePwmCond(CM_EMB1, EMB_EVT_PORT1, EMB_RELEASE_PWM_COND_FLAG_ZERO);
	 EMB_SetReleasePwmCond(CM_EMB1, EMB_EVT_PORT2, EMB_RELEASE_PWM_COND_FLAG_ZERO);
	 EMB_SetReleasePwmCond(CM_EMB1, EMB_EVT_PORT3, EMB_RELEASE_PWM_COND_FLAG_ZERO);
	 EMB_SetReleasePwmCond(CM_EMB1, EMB_EVT_CMP,   EMB_RELEASE_PWM_COND_FLAG_ZERO);
	 //EMB clear flag
	 EMB_ClearStatus(CM_EMB0, EMB_FLAG_PORT1);
	 EMB_ClearStatus(CM_EMB0, EMB_FLAG_PORT2);
	 EMB_ClearStatus(CM_EMB0, EMB_FLAG_PORT4);
	 EMB_ClearStatus(CM_EMB0, EMB_FLAG_CMP1);
	 EMB_ClearStatus(CM_EMB0, EMB_FLAG_CMP3);
	 
	 EMB_ClearStatus(CM_EMB1, EMB_FLAG_PORT1);
	 EMB_ClearStatus(CM_EMB1, EMB_FLAG_PORT2);
	 EMB_ClearStatus(CM_EMB1, EMB_FLAG_PORT3);
	 EMB_ClearStatus(CM_EMB1, EMB_FLAG_CMP2);
	 EMB_ClearStatus(CM_EMB1, EMB_FLAG_CMP3);
	 
}

/*******************************************************************************
 * @brief  ADC configuration.
 * @note   
 * @param  None
 * @retval None
 *         PC0<----------->ADC12_IN10<------------>PhaseA temperature<----------------------------->regular ADC1_3 
 *         PC1<----------->ADC12_IN11<------------>PhaseB temperature<------------->regular ADC2_2 
 *         PC2<----------->ADC12_IN12<------------>PhaseA LLC Primary Current <---->regular ADC2_1 
 *         PC3<----------->ADC12_IN13<------------>PhaseB LLC Primary Current<--------------------->regular ADC1_1     
 *         PA3<----------->ADC1_IN03<------------->LLC output current<----------------------------->regular ADC1_0
 *         PC5<----------->ADC12_IN15<------------>LLC input voltage<------------------------------>regular ADC1_2
 *         PB1<----------->ADC123_IN9<------------>LLC output voltage<------------->regular ADC2_0
 *****************************************************************************/
void ADCx_Init(void)
{
	  stc_adc_init_t stcAdcInit;

    /* 1. Enable ADC peripheral clock. */
    FCG_Fcg3PeriphClockCmd(FCG3_PERIPH_ADC1 | FCG3_PERIPH_ADC2 | FCG3_PERIPH_ADC3, ENABLE);
    FCG_Fcg0PeriphClockCmd(FCG0_PERIPH_AOS, ENABLE);
	
    /* 2. Modify the default value depends on the application. */
    (void)ADC_StructInit(&stcAdcInit);
    stcAdcInit.u16ScanMode = ADC_MD_SEQA_SINGLESHOT;

    /* 3. Initializes ADC. */
    (void)ADC_Init(CM_ADC1, &stcAdcInit);
    (void)ADC_Init(CM_ADC2, &stcAdcInit);
    (void)ADC_Init(CM_ADC3, &stcAdcInit);
    /* 4. ADC channel configuration. */
    /* 4.1 Set the ADC pin to analog input mode. */
    //AdcSetPinAnalogMode();
#if 0
    /* 4.2 Enable ADC channels and set sample time. */
    ADC_ChCmd(CM_ADC1, ADC_SEQ_A, ADC_CH3, ENABLE);
    ADC_ChCmd(CM_ADC1, ADC_SEQ_A, ADC_CH13, ENABLE);
    ADC_ChCmd(CM_ADC1, ADC_SEQ_A, ADC_CH15, ENABLE);
    ADC_ChCmd(CM_ADC1, ADC_SEQ_A, ADC_CH10, ENABLE);
		
    //ADC_ChCmd(CM_ADC2, ADC_SEQ_A, ADC_CH9, ENABLE);
    ADC_ChCmd(CM_ADC2, ADC_SEQ_A, ADC_CH12, ENABLE);
    ADC_ChCmd(CM_ADC2, ADC_SEQ_A, ADC_CH11, ENABLE);
		//ADC_ChCmd(CM_ADC2, ADC_SEQ_A, ADC_CH15, ENABLE);
		
		ADC_ChCmd(CM_ADC3, ADC_SEQ_A, ADC_CH9, ENABLE);

    ADC_SetSampleTime(CM_ADC1, ADC_CH3, ADC_SAMPLE_TIME);
    ADC_SetSampleTime(CM_ADC1, ADC_CH13, ADC_SAMPLE_TIME);
    ADC_SetSampleTime(CM_ADC1, ADC_CH15, ADC_SAMPLE_TIME);
		ADC_SetSampleTime(CM_ADC1, ADC_CH10, ADC_SAMPLE_TIME);
		
    ADC_SetSampleTime(CM_ADC2, ADC_CH9, ADC_SAMPLE_TIME);
    ADC_SetSampleTime(CM_ADC2, ADC_CH12, ADC_SAMPLE_TIME);
    ADC_SetSampleTime(CM_ADC2, ADC_CH11, ADC_SAMPLE_TIME);	
//		ADC_SetSampleTime(CM_ADC2, ADC_CH15, ADC_SAMPLE_TIME);

    ADC_SetSampleTime(CM_ADC3, ADC_CH9, ADC_SAMPLE_TIME);
#else

	  ADC_ChRemap(CM_ADC1, ADC_CH0, ADC1_PIN_PA3);
	  ADC_ChRemap(CM_ADC1, ADC_CH1, ADC1_PIN_PC3);
	  ADC_ChRemap(CM_ADC1, ADC_CH2, ADC1_PIN_PC5);
	  ADC_ChRemap(CM_ADC1, ADC_CH3, ADC1_PIN_PC0);
		
	  ADC_ChRemap(CM_ADC2, ADC_CH4, ADC2_PIN_PB1);
	  ADC_ChRemap(CM_ADC2, ADC_CH5, ADC2_PIN_PC2);
	  ADC_ChRemap(CM_ADC2, ADC_CH6, ADC2_PIN_PC1);
		
    /* 4.2 Enable ADC channels and set sample time. */
		ADC_ChCmd(CM_ADC1, ADC_SEQ_A, ADC_CH0, ENABLE);
		ADC_ChCmd(CM_ADC1, ADC_SEQ_A, ADC_CH1, ENABLE);
		ADC_ChCmd(CM_ADC1, ADC_SEQ_A, ADC_CH2, ENABLE);
		ADC_ChCmd(CM_ADC1, ADC_SEQ_A, ADC_CH3, ENABLE);
				
		ADC_ChCmd(CM_ADC2, ADC_SEQ_A, ADC_CH4, ENABLE);
		ADC_ChCmd(CM_ADC2, ADC_SEQ_A, ADC_CH5, ENABLE);
		ADC_ChCmd(CM_ADC2, ADC_SEQ_A, ADC_CH6, ENABLE);

    ADC_SetSampleTime(CM_ADC1, ADC_CH0, ADC_SAMPLE_TIME);
    ADC_SetSampleTime(CM_ADC1, ADC_CH1, ADC_SAMPLE_TIME);
    ADC_SetSampleTime(CM_ADC1, ADC_CH2, ADC_SAMPLE_TIME);
		ADC_SetSampleTime(CM_ADC1, ADC_CH3, ADC_SAMPLE_TIME);
		
    ADC_SetSampleTime(CM_ADC2, ADC_CH4, ADC_SAMPLE_TIME);
    ADC_SetSampleTime(CM_ADC2, ADC_CH5, ADC_SAMPLE_TIME);
    ADC_SetSampleTime(CM_ADC2, ADC_CH6, ADC_SAMPLE_TIME);

#endif
    /**********************ADC trigger configuration**********/
    ADC_TriggerConfig(CM_ADC1, ADC_SEQ_A, ADC_HARDTRIG_EVT0);
		ADC_TriggerConfig(CM_ADC2, ADC_SEQ_A, ADC_HARDTRIG_EVT0);
		//ADC_TriggerConfig(CM_ADC3, ADC_SEQ_A, ADC_HARDTRIG_EVT0);
		
		AOS_SetTriggerEventSrc(AOS_ADC1_0, EVT_SRC_TMR0_1_CMP_B);
		AOS_SetTriggerEventSrc(AOS_ADC2_0, EVT_SRC_TMR0_1_CMP_B);
		//AOS_SetTriggerEventSrc(AOS_ADC3_0, EVT_SRC_HRPWM_1_SCMP_A);
		
		/**********************ADC interrupt configuration**********/
		ADC_IntCmd(CM_ADC1,ADC_INT_EOCA,ENABLE);
		
		NVIC_ClearPendingIRQ(ADC1_IRQn);
    NVIC_SetPriority(ADC1_IRQn, DDL_IRQ_PRIO_04);
    NVIC_EnableIRQ(ADC1_IRQn);
		
    ADC_TriggerCmd(CM_ADC1, ADC_SEQ_A, ENABLE);	
    ADC_TriggerCmd(CM_ADC2, ADC_SEQ_A, ENABLE);			
		ADC_TriggerCmd(CM_ADC3, ADC_SEQ_A, ENABLE);		
}

/*****************************************************************
* ADC2_IRQn_Handler(void)
*
*
******************************************************************/
//void  ADC2_Handler(void)
//{
//	  uint16_t u16AdcVal;
//    u16AdcVal = ADC_GetValue(CM_ADC1, ADC_CH9);//todo	
//}

/**********************************************
 * @brief  Event port initialize
 * @param  None
 * @retval None
 **********************************************/
void EventPort_Init(void)
{
    stc_ep_init_t stcEPInit;
    (void)EP_StructInit(&stcEPInit);

    //FCG_Fcg0PeriphClockCmd(PWC_FCG0_AOS, ENABLE);
    GPIO_SetFunc(EVT_PORT_3, EVT_PIN_13, GPIO_FUNC_26);

    stcEPInit.enPinState        = EVT_PIN_RESET;
    stcEPInit.u32PinDir         = EP_DIR_OUT;
    stcEPInit.u32PinTriggerOps  = EP_OPS_TOGGLE;
    (void)EP_Init(EVT_PORT_3, EVT_PIN_13, &stcEPInit);

    AOS_SetTriggerEventSrc(AOS_EVTPORT34, EVT_SRC_TMR0_1_CMP_B);
}

/*********************************************************
 * @brief  TimerA configuration.
 * @param  None
 * @retval None
 *********************************************************/
void TmrAConfig(void)
{
    stc_tmra_init_t stcTmraInit;
    stc_tmra_pwm_init_t stcPwmInit;

    /* 1. Enable TimerA peripheral clock. */
    FCG_Fcg2PeriphClockCmd(TMRA_PERIPH_CLK, ENABLE);

    /* 2. Set a default initialization value for stcTmraInit. */
    (void)TMRA_StructInit(&stcTmraInit);

    /* 3. Modifies the initialization values depends on the application. */
    stcTmraInit.sw_count.u8CountMode = TMRA_MD;
    stcTmraInit.sw_count.u8CountDir  = TMRA_DIR;
    stcTmraInit.u32PeriodValue = TMRA_PERIOD_VAL;
    (void)TMRA_Init(TMRA_UNIT, &stcTmraInit);

    /* 4. Set the comparison reference value. */
#if (APP_FUNC == APP_FUNC_NORMAL_SINGLE_PWM)
    (void)TMRA_PWM_StructInit(&stcPwmInit);
    stcPwmInit.u32CompareValue = TMRA_PWM_CMP_AURIXPOWER_VAL;
	  stcPwmInit.u16StopPolarity = TMRA_PWM_LOW;
	  stcPwmInit.u16StartPolarity = TMRA_PWM_LOW;
		stcPwmInit.u16CompareMatchPolarity = TMRA_PWM_LOW;
		stcPwmInit.u16PeriodMatchPolarity = TMRA_PWM_HIGH;
    (void)TMRA_PWM_Init(TMRA_UNIT, TMRA_PWM_CH_AURIXPOWER, &stcPwmInit);
    TMRA_PWM_OutputCmd(TMRA_UNIT, TMRA_PWM_CH_AURIXPOWER, ENABLE);
		
		stcPwmInit.u32CompareValue = TMRA_PWM_CMP_FAN_VAL;
		GPIO_SetFunc(TMRA_PWM_PORT_FAN, TMRA_PWM_PIN_FAN, TMRA_PWM_PIN_FAN_FUNC);
    (void)TMRA_PWM_Init(TMRA_UNIT, TMRA_PWM_CH_FAN, &stcPwmInit);
		
//		TMRA_CompareBufCmd(TMRA_UNIT, TMRA_PWM_CH_AURIXPOWER, ENABLE);//enable shadown
//		TMRA_SetCompareBufCond(TMRA_UNIT, TMRA_PWM_CH_AURIXPOWER, TMRA_BUF_TRANS_COND_VALLEY);//prd update
		
    TMRA_PWM_OutputCmd(TMRA_UNIT, TMRA_PWM_CH_FAN, ENABLE);
    
#elif (APP_FUNC == APP_FUNC_SINGLE_EDGE_ALIGNED_PWM)
    (void)TMRA_PWM_StructInit(&stcPwmInit);
    stcPwmInit.u32CompareValue = TMRA_PWMX_CMP_VAL;
    GPIO_SetFunc(TMRA_PWMX_PORT, TMRA_PWMX_PIN, TMRA_PWMX_PIN_FUNC);
    (void)TMRA_PWM_Init(TMRA_UNIT, TMRA_PWMX_CH, &stcPwmInit);
    TMRA_PWM_OutputCmd(TMRA_UNIT, TMRA_PWMX_CH, ENABLE);

    (void)TMRA_PWM_StructInit(&stcPwmInit);
    stcPwmInit.u32CompareValue = TMRA_PWMY_CMP_VAL;
    GPIO_SetFunc(TMRA_PWMY_PORT, TMRA_PWMY_PIN, TMRA_PWMY_PIN_FUNC);
    (void)TMRA_PWM_Init(TMRA_UNIT, TMRA_PWMY_CH, &stcPwmInit);
    TMRA_PWM_OutputCmd(TMRA_UNIT, TMRA_PWMY_CH, ENABLE);

#elif (APP_FUNC == APP_FUNC_TOW_EDGE_SYMMETRIC_PWM)
    (void)TMRA_PWM_StructInit(&stcPwmInit);
    stcPwmInit.u32CompareValue        = TMRA_PWMX_CMP_VAL;
    stcPwmInit.u16StartPolarity       = TMRA_PWM_HIGH;
    stcPwmInit.u16StopPolarity        = TMRA_PWM_HIGH;
    stcPwmInit.u16PeriodMatchPolarity = TMRA_PWM_HOLD;
    GPIO_SetFunc(TMRA_PWMX_PORT, TMRA_PWMX_PIN, TMRA_PWMX_PIN_FUNC);
    (void)TMRA_PWM_Init(TMRA_UNIT, TMRA_PWMX_CH, &stcPwmInit);
    TMRA_PWM_OutputCmd(TMRA_UNIT, TMRA_PWMX_CH, ENABLE);

    stcPwmInit.u32CompareValue  = TMRA_PWMY_CMP_VAL;
    stcPwmInit.u16StartPolarity = TMRA_PWM_LOW;
    stcPwmInit.u16StopPolarity  = TMRA_PWM_LOW;
    GPIO_SetFunc(TMRA_PWMY_PORT, TMRA_PWMY_PIN, TMRA_PWMY_PIN_FUNC);
    (void)TMRA_PWM_Init(TMRA_UNIT, TMRA_PWMY_CH, &stcPwmInit);
    TMRA_PWM_OutputCmd(TMRA_UNIT, TMRA_PWMY_CH, ENABLE);
#endif

  /* Starts TimerA. */
    TMRA_Start(TMRA_UNIT);
}


/*********************************************************
 * @brief  USART2 configuration.
 * @param  None
 * @retval None
 *********************************************************/
void USART2Config(void)
{
	 stc_usart_uart_init_t stcUartInit;
	
    /* Enable peripheral clock */
    USART_FCG_ENABLE();

    /* Initialize UART. */
    (void)USART_UART_StructInit(&stcUartInit);
    stcUartInit.u32ClockDiv = USART_CLK_DIV64;
    stcUartInit.u32CKOutput = USART_CK_OUTPUT_ENABLE;
    stcUartInit.u32Baudrate = USART_BAUDRATE;
    stcUartInit.u32OverSampleBit = USART_OVER_SAMPLE_8BIT;
	  USART_UART_Init(USART_UNIT, &stcUartInit, NULL);
	
	  //SET_REG32_BIT(USART_UNIT->CR1,0x00000088);
	  /* Enable TX && RX && RX interrupt function */
    //USART_FuncCmd(USART_UNIT, (USART_TX), ENABLE);
    
}


/**********************************************************
 * @brief  Initialize DMA.
 * @param  None
 * @retval int32_t:
 *           - LL_OK:                   Initialize successfully.
 *           - LL_ERR_INVD_PARAM:       Initialization parameters is invalid.
 **************************************************************/
void DMA_Config(void)
{
    int32_t i32Ret;
    stc_dma_init_t stcDmaInit;
    stc_dma_llp_init_t stcDmaLlpInit;
    //stc_irq_signin_config_t stcIrqSignConfig;
	  stc_dma_repeat_init_t pstcDmaRepeatInit;
    static stc_dma_llp_descriptor_t stcLlpDesc;
 
    /* DMA&AOS FCG enable */
    RX_DMA_FCG_ENABLE();
    TX_DMA_FCG_ENABLE();
    FCG_Fcg0PeriphClockCmd(FCG0_PERIPH_AOS, ENABLE);

    /* USART_RX_DMA */

    (void)DMA_StructInit(&stcDmaInit);
    stcDmaInit.u32IntEn = DMA_INT_DISABLE;
    stcDmaInit.u32BlockSize = 1UL;
    stcDmaInit.u32TransCount = 45;
    stcDmaInit.u32DataWidth = DMA_DATAWIDTH_8BIT;
    stcDmaInit.u32DestAddr = (uint32_t)m_au8RxBuf;
    stcDmaInit.u32SrcAddr = (uint32_t)(&USART_UNIT->RDR);
    stcDmaInit.u32SrcAddrInc = DMA_SRC_ADDR_FIX;
    stcDmaInit.u32DestAddrInc = DMA_DEST_ADDR_INC;
    i32Ret = DMA_Init(RX_DMA_UNIT, RX_DMA_CH, &stcDmaInit);
		
		DMA_RepeatStructInit(&pstcDmaRepeatInit);
		pstcDmaRepeatInit.u32DestCount = 45;
		pstcDmaRepeatInit.u32Mode = DMA_RPT_DEST;
		pstcDmaRepeatInit.u32SrcCount = 0;
    DMA_RepeatInit(RX_DMA_UNIT, RX_DMA_CH, &pstcDmaRepeatInit);
		
#if 0
		if (LL_OK == i32Ret) 
		{
        (void)DMA_LlpStructInit(&stcDmaLlpInit);
        stcDmaLlpInit.u32State = DMA_LLP_ENABLE;
        stcDmaLlpInit.u32Mode  = DMA_LLP_WAIT;
        stcDmaLlpInit.u32Addr  = (uint32_t)&stcLlpDesc;
        (void)DMA_LlpInit(RX_DMA_UNIT, RX_DMA_CH, &stcDmaLlpInit);

        stcLlpDesc.SARx   = stcDmaInit.u32SrcAddr;
        stcLlpDesc.DARx   = stcDmaInit.u32DestAddr;
        stcLlpDesc.DTCTLx = (stcDmaInit.u32TransCount << DMA_DTCTL_CNT_POS) | (stcDmaInit.u32BlockSize << DMA_DTCTL_BLKSIZE_POS);;
        stcLlpDesc.LLPx   = (uint32_t)&stcLlpDesc;
        stcLlpDesc.CHCTLx = stcDmaInit.u32SrcAddrInc | stcDmaInit.u32DestAddrInc | stcDmaInit.u32DataWidth |  \
                            stcDmaInit.u32IntEn      | stcDmaLlpInit.u32State    | stcDmaLlpInit.u32Mode;

        DMA_ReconfigLlpCmd(RX_DMA_UNIT, RX_DMA_CH, ENABLE);
        DMA_ReconfigCmd(RX_DMA_UNIT, ENABLE);
        AOS_SetTriggerEventSrc(RX_DMA_RECONF_TRIG_SEL, RX_DMA_RECONF_TRIG_EVT_SRC);

//        stcIrqSignConfig.enIntSrc = RX_DMA_TC_INT_SRC;
//        stcIrqSignConfig.enIRQn  = RX_DMA_TC_IRQn;
//        stcIrqSignConfig.pfnCallback = &RX_DMA_TC_IrqCallback;
//        (void)INTC_IrqSignIn(&stcIrqSignConfig);
//        NVIC_ClearPendingIRQ(stcIrqSignConfig.enIRQn);
//        NVIC_SetPriority(stcIrqSignConfig.enIRQn, DDL_IRQ_PRIO_DEFAULT);
//        NVIC_EnableIRQ(stcIrqSignConfig.enIRQn);

        AOS_SetTriggerEventSrc(RX_DMA_TRIG_SEL, RX_DMA_TRIG_EVT_SRC);

        DMA_Cmd(RX_DMA_UNIT, ENABLE);
        //DMA_TransCompleteIntCmd(RX_DMA_UNIT, RX_DMA_TC_INT, ENABLE);
        (void)DMA_ChCmd(RX_DMA_UNIT, RX_DMA_CH, ENABLE);
    }
	#endif
	
	  AOS_SetTriggerEventSrc(RX_DMA_TRIG_SEL, RX_DMA_TRIG_EVT_SRC);
		DMA_Cmd(RX_DMA_UNIT, ENABLE);
		//DMA_TransCompleteIntCmd(RX_DMA_UNIT, RX_DMA_TC_INT, ENABLE);
	  (void)DMA_ChCmd(RX_DMA_UNIT, RX_DMA_CH, ENABLE);
	
    /* USART_TX_DMA */
    (void)DMA_StructInit(&stcDmaInit);
    stcDmaInit.u32IntEn = DMA_INT_DISABLE;
    stcDmaInit.u32BlockSize = 1UL;
    stcDmaInit.u32TransCount = 128;
    stcDmaInit.u32DataWidth = DMA_DATAWIDTH_8BIT;
    stcDmaInit.u32DestAddr = (uint32_t)(&USART_UNIT->TDR);
    stcDmaInit.u32SrcAddr = (uint32_t)m_au8TxBuf;
    stcDmaInit.u32SrcAddrInc = DMA_SRC_ADDR_INC;
    stcDmaInit.u32DestAddrInc = DMA_DEST_ADDR_FIX;
		
    DMA_Init(TX_DMA_UNIT, TX_DMA_CH, &stcDmaInit);
		AOS_SetTriggerEventSrc(TX_DMA_TRIG_SEL, TX_DMA_TRIG_EVT_SRC);
		DMA_Cmd(TX_DMA_UNIT, ENABLE);
		DMA_ChCmd(TX_DMA_UNIT, TX_DMA_CH, ENABLE);

    DMA_RepeatStructInit(&pstcDmaRepeatInit);
		pstcDmaRepeatInit.u32DestCount = 0;
		pstcDmaRepeatInit.u32Mode = DMA_RPT_SRC;
		pstcDmaRepeatInit.u32SrcCount = 128;	
    DMA_RepeatInit(TX_DMA_UNIT, TX_DMA_CH, &pstcDmaRepeatInit);
    /* Enable TX && RX && RX interrupt function */
    USART_FuncCmd(USART_UNIT, (USART_RX|USART_TX), ENABLE);
		//SET_REG32_BIT(USART_UNIT->CR1,0x00000088);
    //return i32Ret;
}

/**********************************************************
 * @brief  Usart2_TxEnable DMA.
 * @param  None
 * @retval int32_t:
 **************************************************************/
void Usart2_TxEnable(void)
{
   //if(SET == m_enRxFrameEnd) 
	 {
		    //USART_FuncCmd(USART_UNIT, USART_TX, DISABLE);
		 
				DMA_SetSrcAddr(TX_DMA_UNIT, TX_DMA_CH, (uint32_t)m_au8TxBuf);

				DMA_SetTransCount(TX_DMA_UNIT, TX_DMA_CH, 128);

				(void)DMA_ChCmd(TX_DMA_UNIT, TX_DMA_CH, ENABLE);

				USART_FuncCmd(USART_UNIT, USART_TX, ENABLE);
				//m_enRxFrameEnd = RESET;
   }
}

void Usart2_RxEnable(void)
{
   //if(SET == m_enRxFrameEnd) 
	 {
//				DMA_SetDestAddr(RX_DMA_UNIT, RX_DMA_CH, (uint32_t)m_au8RxBuf);

//				DMA_SetTransCount(RX_DMA_UNIT, RX_DMA_CH, 32);

		      //USART_FuncCmd(USART_UNIT, USART_RX, ENABLE);
		 
				  (void)DMA_ChCmd(RX_DMA_UNIT, RX_DMA_CH, ENABLE);

				

				//m_enRxFrameEnd = RESET;
   }
}


/*************************************************
 * @brief  Configure CMP.
 * @param  None
 * @retval None
 *************************************************/
void LLC_CmpConfig(void)
{
    stc_cmp_init_t stcCmpInit;
    //stc_gpio_init_t stcGpioInit;

    /* Enable peripheral Clock */
    FCG_Fcg3PeriphClockCmd(FCG3_PERIPH_CMP1_2, ENABLE);
	  FCG_Fcg3PeriphClockCmd(FCG3_PERIPH_CMP3, ENABLE);

    /* Clear structure */
    (void)CMP_StructInit(&stcCmpInit);
	
	  /**************************CMP3---Output OCP********************/
    stcCmpInit.u16PositiveInput = (uint16_t)CMP_POSITIVE_INP1;  /* PA3 */
    stcCmpInit.u16NegativeInput = (uint16_t)CMP_NEGATIVE_INM5;  /* DA2O1 voltage */
    stcCmpInit.u16OutPolarity = CMP_OUT_INVT_OFF;
    stcCmpInit.u16OutDetectEdge = CMP_DETECT_EDGS_RISING;
    stcCmpInit.u16OutFilter = CMP_OUT_FILTER_CLK;
    stcCmpInit.u16HystVoltage = CMP_HYST_VOLT_NONE;
    (void)CMP_NormalModeInit(CM_CMP3, &stcCmpInit);

	  /* Enable interrupt if need */
    //CMP_IntCmd(CM_CMP1, ENABLE);
	
	  /******************CMP2-Phase A resonant current ********************************/
	  stcCmpInit.u16PositiveInput = (uint16_t)CMP_POSITIVE_INP1;  
    stcCmpInit.u16NegativeInput = (uint16_t)CMP_NEGATIVE_INM4;/* DA1O1 voltage */ 
	  (void)CMP_NormalModeInit(CM_CMP2, &stcCmpInit);
	
	  /* Enable interrupt if need */
    //CMP_IntCmd(CM_CMP2, ENABLE);
		
	  /***********************CMP1 Phase B resonant current ********************************/
	  stcCmpInit.u16PositiveInput = (uint16_t)CMP_POSITIVE_INP1;  
    stcCmpInit.u16NegativeInput = (uint16_t)CMP_NEGATIVE_INM3;/* DA2O1 voltage */ 
	  (void)CMP_NormalModeInit(CM_CMP1, &stcCmpInit);
	   
		 
    /* Enable CMP output */
    CMP_CompareOutCmd(CM_CMP1, ENABLE);
    /* Enable VCOUT */
    CMP_PinVcoutCmd(CM_CMP1, ENABLE);			

    /* Enable CMP output */
    CMP_CompareOutCmd(CM_CMP2, ENABLE);
    /* Enable VCOUT */
    CMP_PinVcoutCmd(CM_CMP2, ENABLE);	
		
		/* Enable CMP output */
    CMP_CompareOutCmd(CM_CMP3, ENABLE);
		/* Enable VCOUT */
    CMP_PinVcoutCmd(CM_CMP3, ENABLE);	
}

/*************************************************
 * @brief  Configure DAC.
 * @param  None
 * @retval None
 *************************************************/
void DACx_ConversionInit(void)
{
	 stc_dac_init_t stInit;
	
    /* Enable DAC peripheral clock. */
    FCG_Fcg3PeriphClockCmd(PWC_FCG3_DAC1, ENABLE);
	  FCG_Fcg3PeriphClockCmd(PWC_FCG3_DAC2, ENABLE);

    //Set GPIO pin for sine wave output
//    stc_gpio_init_t stcGpioInit;
//    (void)GPIO_StructInit(&stcGpioInit);
//    stcGpioInit.u16PinAttr = PIN_ATTR_ANALOG;
//    (void)GPIO_Init(GPIO_PORT_A, GPIO_PIN_04, &stcGpioInit);
//    (void)GPIO_Init(GPIO_PORT_A, GPIO_PIN_05, &stcGpioInit);
//    (void)GPIO_Init(GPIO_PORT_A, GPIO_PIN_06, &stcGpioInit);
	
    /* Init DAC by default value: source from data register and output enabled*/
    DAC_DeInit(CM_DAC1);
    DAC_DeInit(CM_DAC2);
    (void)DAC_StructInit(&stInit);
	
    (void)DAC_Init(CM_DAC1, DAC_CH1, &stInit);
    (void)DAC_Init(CM_DAC1, DAC_CH2, &stInit);
	  (void)DAC_Init(CM_DAC2, DAC_CH1, &stInit);

    DAC_DataRegAlignConfig(CM_DAC1, DAC_DATA_ALIGN_RIGHT);
		DAC_DataRegAlignConfig(CM_DAC2, DAC_DATA_ALIGN_RIGHT);
		
#ifdef DAC_ADP_ENABLE
    /* Set ADC first */
    /* Enable ADC peripheral clock. */
    FCG_Fcg3PeriphClockCmd(PWC_FCG3_ADC1 | PWC_FCG3_ADC2 | PWC_FCG3_ADC3, ENABLE);
    if (CM_ADC1->STR == 0U) {
        if (CM_ADC2->STR == 0U) {
            if (CM_ADC3->STR == 0U) {
                DAC_ADCPrioConfig(DAC_UNIT, DAC_ADP_SEL, ENABLE);
                DAC_ADCPrioCmd(DAC_UNIT, ENABLE);
            }
        }
    }
#endif

    DAC_ADCPrioConfig(CM_DAC1, DAC_ADP_SEL_ALL, ENABLE);
		DAC_ADCPrioConfig(CM_DAC2, DAC_ADP_SEL_ALL, ENABLE);
    DAC_ADCPrioCmd(CM_DAC1, ENABLE);
		DAC_ADCPrioCmd(CM_DAC2, ENABLE);
										
    //primary ocp configuration
    DAC_SetDualChData(CM_DAC1, 3103, 3013);//2482
		//DAC_SetDualChData(CM_DAC1, 450, 450);//2482
    //output current ocp
		DAC_SetChData(CM_DAC2, DAC_CH1, 3972);  //94A
		
		DAC_OutputCmd(CM_DAC1,DAC_CH1,DISABLE);
		DAC_OutputCmd(CM_DAC1,DAC_CH2,DISABLE);
		DAC_OutputCmd(CM_DAC2,DAC_CH1,DISABLE);
		
		//DAC start 
		DAC_StartDualCh(CM_DAC1);
		//
		DAC_Start(CM_DAC2,DAC_CH1);
		
		//wait for DAC output stable
		DDL_DelayUS(100);
}

/*************************************************
 * @brief  WDT configuration.
 * @param  None
 * @retval None
 ************************************************/
void WDT_Config(void)
{
    stc_wdt_init_t stcWdtInit;

    /* WDT configuration */
    stcWdtInit.u32CountPeriod   = WDT_CNT_PERIOD16384;
    stcWdtInit.u32ClockDiv      = WDT_CLK_DIV1024;
    stcWdtInit.u32RefreshRange  = WDT_RANGE_0TO100PCT;
    stcWdtInit.u32LPMCount      = WDT_LPM_CNT_STOP;
    stcWdtInit.u32ExceptionType = WDT_EXP_TYPE_RST;
    (void)WDT_Init(&stcWdtInit);
}

/*************************************************
 * @brief  Calibration Read.
 * @param  None
 * @retval None
 ************************************************/
void CalibrationRead(void)
{
	uint32_t u32Addr = EFM_SECTOR_ADDR(31);
	uint32_t u32Tempdata = 0;
	//Vout calibration gain
	(void)EFM_ReadByte(u32Addr, (uint8_t *)&u32Tempdata, sizeof(u32Tempdata));
	if(u32Tempdata != 0xFFFFFFFF)
	{
		int32VoutCalibGainQ11 = (int32_t)(u32Tempdata);
	}

	//Vout calibration offsent
	u32Addr += 4;
	(void)EFM_ReadByte(u32Addr, (uint8_t *)&u32Tempdata, sizeof(u32Tempdata));
	if(u32Tempdata != 0xFFFFFFFF)
	{
		int32VoutCalibOffsentQ11 = (int32_t)(u32Tempdata);
	}
	
	//Iout calibration gain
	u32Addr += 4;
	(void)EFM_ReadByte(u32Addr, (uint8_t *)&u32Tempdata, sizeof(u32Tempdata));
	if(u32Tempdata != 0xFFFFFFFF)
	{
		int32IoutCalibGainQ11 = (int32_t)(u32Tempdata);
	}
	//Iout calibration offsent
	u32Addr += 4;
	(void)EFM_ReadByte(u32Addr, (uint8_t *)&u32Tempdata, sizeof(u32Tempdata));
	if(u32Tempdata != 0xFFFFFFFF)
	{
		int32IoutCalibOffsentQ11 = (int32_t)(u32Tempdata);
	}
	
	//Vin calibration gain
	u32Addr += 4;
	(void)EFM_ReadByte(u32Addr, (uint8_t *)&u32Tempdata, sizeof(u32Tempdata));
	if(u32Tempdata != 0xFFFFFFFF)
	{
		int32VinCalibGainQ11 = (int32_t)(u32Tempdata);
	}
	//Vin calibration offsent
	u32Addr += 4;
	(void)EFM_ReadByte(u32Addr, (uint8_t *)&u32Tempdata, sizeof(u32Tempdata));
	if(u32Tempdata != 0xFFFFFFFF)
	{
		int32VinCalibOffsentQ11 = (int32_t)(u32Tempdata);
	}
}

/*************************************************
 * @brief  Calibration Write.
 * @param  None
 * @retval None
 ************************************************/
void CalibrationWrite(void)
{
	  uint32_t u32Addr = 0x0;
	  /* EFM_FWMC write enable */
    EFM_FWMC_Cmd(ENABLE);
    /* Sector 10 disables write protection */
    (void)EFM_SingleSectorOperateCmd(31, ENABLE);
    /* Erase sector 10. sector 10: 0x00014000~0x00015FFF */
    u32Addr = EFM_SECTOR_ADDR(31);
    (void)EFM_SectorErase(u32Addr);
	 
	  //Vout calibration gain
	  EFM_ProgramWord(u32Addr, int32VoutCalibGainQ11);
	  //Vout calibration offsent
	  u32Addr += 4;
	  EFM_ProgramWord(u32Addr, int32VoutCalibOffsentQ11);
	  //Vout calibration gain
	
	  //Iout calibration gain
	  u32Addr += 4;
	  EFM_ProgramWord(u32Addr, int32IoutCalibGainQ11);
	  //Iout calibration offsent
	  u32Addr += 4;
	  EFM_ProgramWord(u32Addr, int32IoutCalibOffsentQ11);
	  
		//Vin calibration gain
	  u32Addr += 4;
	  EFM_ProgramWord(u32Addr, int32VinCalibGainQ11);
	  //Vin calibration offsent
	  u32Addr += 4;
	  EFM_ProgramWord(u32Addr, int32VinCalibOffsentQ11);		
}

int16_t  TemperatureBaseLookUpTable(uint16_t u16ADCTemp)
{
		int16_t s16TempValue = 25;
	  int16_t s16leftIndex = 0;
	  int16_t s16rightIndex = 160;
	  int16_t s16MiddleIndex = 0;
	
	  if(u16ADCTemp < 107)
		{
			s16TempValue = -10;
			return s16TempValue;
		}
		
		if(u16ADCTemp > 2847)
		{
			s16TempValue = 150;
			return s16TempValue;
		}
		
		while(s16leftIndex <= s16rightIndex)
		{
			s16MiddleIndex = s16leftIndex + ((s16rightIndex - s16leftIndex) >> 1);
			if(u16Temperature[s16MiddleIndex] > u16ADCTemp )
			{
				s16rightIndex = s16MiddleIndex - 1;
			}
			else if(u16Temperature[s16MiddleIndex] < u16ADCTemp)
			{
				 s16leftIndex = s16MiddleIndex + 1;
			}
			else
			{
				 s16TempValue = -10 + s16MiddleIndex;
				 return s16TempValue;
			}		
		}
		s16TempValue = -10 + s16MiddleIndex;
		return s16TempValue;
		
}
