/**
 *******************************************************************************
 * @file  template/source/main.c
 * @brief Main program template for the Device Driver Library.
 @verbatim
   Change Logs:
   Date             Author          Notes
   xxxx-xx-xx       CDT             First version
 @endverbatim
 *******************************************************************************
 * Copyright (C) 2022-2023, Xiaohua Semiconductor Co., Ltd. All rights reserved.
 *
 * This software component is licensed by XHSC under BSD 3-Clause license
 * (the "License"); You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at:
 *                    opensource.org/licenses/BSD-3-Clause
 *
 *******************************************************************************
 */

/*******************************************************************************
 * Include files
 ******************************************************************************/
#include "main.h"
#include "ev_hc32f334_lqfp64.h"

/**
 * @addtogroup HC32F334_DDL_Examples
 * @{
 */

/**
 * @addtogroup LL_Templates
 * @{
 */

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

/*******************************************************************************
 * Local pre-processor symbols/macros ('#define')
 ******************************************************************************/
#define  OPENLOOP_ENABLE     (0) //0:close loop 1:open loop
#define  CURR_LIMIT_ENABLE   (0) //0:current limit disable 1:current limit enable
#define  VOUT_LIMIT_ENABLE   (0) //0:Vout Limit disable    1:vout

//system state machine
#define   SYS_INIT_MODE     ((uint16_t)(0))
#define   SYS_STABDY_MODE   ((uint16_t)(1))
#define   SYS_WORK_MODE     ((uint16_t)(2))
#define   SYS_FAULT_MODE    ((uint16_t)(4))

//system timer
#define TASK_300US_CNT    ((uint32_t)(30))
#define TASK_1MS_CNT      ((uint32_t)(105))
#define TASK_5MS_CNT      ((uint32_t)(510))

//
//#define VOLT_SLEW_STEP    ((int32_t)(4))
//#define CURR_SLEW_STEP    ((int32_t)(10))

////
//#define CTRL_VLOOP_CNT    	(16)
//#define CTRL_RESOANANT_CNT	(16)



//controller output
#define  VLOOPCTRLMAX   (int32_t)(1000);
#define  VLOOPCTRLMIN   (int32_t)(200);
#define  ILOOPCTRLMAX   (int32_t)(1000);
#define  ILOOPCTRLMIN   (int32_t)(200);

#define  SR_DUTY_VOLT_MAX        ((uint16_t)(37))
#define  SR_DUTY_CURR_MAX        ((uint16_t)(55))
#define  VOUT_FRELIMIT_MAX       ((uint16_t)(84))

const uint16_t SR_LookUpTable[SR_DUTY_VOLT_MAX][SR_DUTY_CURR_MAX] =
{
  //Vout = 42V
  {25600,25600,47145,48792,48792,49167,49167,49167,49167,49167,
	 49352,49352,49352,49352,49352,49612,49612,49612,49612,49612,
   49967,49967,49967,49967,49967,50327,50327,50327,50327,50327,
   50693,50693,50693,50693,50693,50996,50996,50996,50996,50996,
   51440,51440,51440,51440,51440,51717,51717,51717,51717,51717,
   52032,52032,52032,52032,52032,
  },
};

#define SR_VOLT_BASE_ADC            ((int32_t)(2369))  //42V/0.5V
#define SR_CURR_BASE_ADC            ((int32_t)(217))   //7A/1.0A
#define SR_CURR_K_Q11               (CONSTIoutScaleQ11)
#define SR_VOLT_K_Q11               (CONSTVoutScaleQ11 * 2)
/*******************************************************************************
 * Global variable definitions (declared in header file with 'extern')
 ******************************************************************************/

/*******************************************************************************
 * Local function prototypes ('static')
 ******************************************************************************/
void Gpio_Init(void);
/*******************************************************************************
 * Local variable definitions ('static')
 ******************************************************************************/

//output voltage limit table TODO
const uint32_t Vout_LimitTable[VOUT_FRELIMIT_MAX] =
{
  200,200,200,200,200,200,200,200,200,200,200,200,
  200,200,200,200,200,200,200,200,200,200,200,200,
  200,200,200,200,200,200,200,200,200,200,200,200,
  200,200,200,200,200,200,200,200,200,200,200,200,
  200,200,200,200,200,200,200,200,200,200,200,200,
  200,200,200,200,200,200,200,200,200,200,200,200,
  200,200,200,200,200,200,200,200,200,200,200,200,
};

//SR duty table TODO


//ADC value
int32_t  int32VoutADCQ0_test     = 0;
int32_t  int32VoutADCQ0     = 0;
int32_t  int32IoutADCQ0     = 0;
int32_t  int32VinADCQ0      = 0;
int32_t  int32PriCurr1ADCQ0 = 0;
int32_t  int32PriCurr2ADCQ0 = 0;
int16_t  int16Temp1ADCQ0    = 0;
int16_t  int16Temp2ADCQ0    = 0;

//filter and calibration
//int32_t  int32VoutFilterADCQ0     = 0;
//int32_t  int32IoutFilterADCQ0     = 0;
int32_t  int32IoutFilterADCQ0_PRE = 0;
//int32_t  int32VinFilterADCQ0      = 0;
//int32_t  int32PriCurr1FilterADCQ0 = 0;
//int32_t  int32PriCurr2FilterADCQ0 = 0;


//int32_t  int32VoutCalibADCQ0     = 0;
//int32_t  int32IoutCalibADCQ0     = 0;
//int32_t  int32VinCalibADCQ0      = 0;
int32_t  int32PriCurr1CalibADCQ0 = 0;
int32_t  int32PriCurr2CalibADCQ1 = 0;

//real value
int32_t  int32VoutRtdbQ11   = 0;
int32_t  int32IoutRtdbQ11   = 0;
int32_t  int32VinRtdbQ11    = 0;
int32_t  int32PriCurr1RtdbQ11 = 0;
int32_t  int32PriCurr2RtdbQ11 = 0;


//ADC->Real
int32_t  int32VoutScaleQ11          = CONSTVoutScaleQ11;   //0.017725
int32_t  int32IoutScaleQ11          = CONSTIoutScaleQ11;   //
int32_t  int32VinScaleQ11           = CONSTVinScaleQ11;  //
int32_t  int32PriCurr1ScaleQ11      = CONSTPriCurr1ScaleQ11; //TODO
int32_t  int32PriCurr2ScaleQ11      = CONSTPriCurr2ScaleQ11; //TODO

//isr
//uint16_t u16IsrDivdeCnt    = 0;
//int32_t  s32SumPriCurr1     = 0;
//int32_t  s32SumPriCurr2     = 0;



//calibration parameter
int32_t  int32VoutCalibGainQ11 = 2048;
int32_t  int32VoutCalibOffsentQ11 = 0;
int32_t  int32IoutCalibGainQ11 = 2048;
int32_t  int32IoutCalibOffsentQ11 = 0;
int32_t  int32VinCalibGainQ11 = 2048;
int32_t  int32VinCalibOffsentQ11 = 0;
int32_t  int32PriCurr1CalibGainQ11 = 2048;
int32_t  int32PriCurr1CalibOffsentQ11 = 0;
int32_t  int32PriCurr2CalibGainQ11 = 2048;
int32_t  int32PriCurr2CalibOffsentQ11 = 0;

//output set
//int32_t  int32VoutSetADCQ0  = 3017;//2708; //2395;//2708;  //base on ADC(48V * SCAL)
//int32_t  int32IoutSetADCQ0  = CURRENT_SET_MAX_Q0;   //base on ADC(62.5A * SCAL)
//int32_t  int32VoutSlewQ0  = 0; //used for controller;
//int32_t  int32IoutSlewQ0  = 1552; //used for controller;
//int32_t  int32IoutLimitQ0 = CURRENT_SET_MAX_Q0;
//int32_t  int32FreLimitQ0  = FRE_CTRL_MAX;   //default is 80KHZ
//int32_t  int32FreLimitIndex = 0;

//fault flag
uint32_t u32Faultflag = 0;

//Power on
//uint16_t SysPowerONState = 0;
//uint16_t u16CtrlSrOpenIndex = 0;

//controller parameter
//voltage loop parameter
//typedef struct
//{
//    int32_t int32B0;
//    int32_t int32B1;
//    int32_t int32B2;
//    int32_t int32A1;
//    int32_t int32A2;
//    int32_t int32X0;
//    int32_t int32X1;
//    int32_t int32X2;
//    int32_t int32Y1;
//    int32_t int32Y2;
//    int32_t int32Y0;
//    int32_t int32Y0_REM;
//    int32_t int32Max;
//    int32_t int32Min;
//}ctrlloopDef;

////
//ctrlloopDef VloopCtrl;
//ctrlloopDef CloopCtrl;


//int32_t int32VoltErr = 0;          //voltage loop error
//int32_t int32CurrErr = 0;          //current loop error

//int32_t int32VoltErr_0 = 0;          //voltage loop error
//int32_t int32VoltErr_1 = 0;          //voltage loop error
//int32_t int32VoltErr_2 = 0;          //voltage loop error
//int32_t int32VoltErr_3 = 0;          //voltage loop error
//int32_t int32VoltErr_4 = 0;          //voltage loop error
//int32_t int32VoltErr_5 = 0;          //voltage loop error
//int32_t int32VoltErr_6 = 0;          //voltage loop error
//int32_t int32VoltErr_7 = 0;          //voltage loop error
//int32_t int32VoltErr_Pre = 0;        //voltage loop error

int32_t int32VoltAdcBuffer[CTRL_VLOOP_CNT] = {0,0,0,0,0,0,0,0,/*0,0,0,0,0,0,0,0*/};
//int32_t int32PriCurr1AdcBuffer[CTRL_RESOANANT_CNT] = {0,0,0,0,0,0,0,0/*,0,0,0,0,0,0,0,0*/};
//int32_t int32PriCurr2AdcBuffer[CTRL_RESOANANT_CNT] = {0,0,0,0,0,0,0,0/*,0,0,0,0,0,0,0,0*/};

//uint16_t u16VoltStoreIndex = 0;
//uint16_t u16BM_EnableCounter = 0; 
int32_t int32VoltAdcSum = 0;
int32_t int32VoltAvage = 0;

//int32_t int32PriCurr1Avage = 0;
//int32_t int32PriCurr2Avage = 0;

//int32_t int32PriCurrErr = 0;       //resonant current error
//int32_t int32PriCurrCtrlOut = 0;   //

//int32_t int32CtrlOut = 0;
//int32_t int32EpwmPrdUpdate = 0;

//SR duty
int32_t int32Epwm3SRDuty_A   = 0;
int32_t int32Epwm3SRDuty_B   = 0;

int32_t int32Epwm6SRDuty_A   = 0;
int32_t int32Epwm6SRDuty_B   = 0;

int32_t int32SrVoltIndex = 0;
int32_t int32SrCurrIndex = 0;
int32_t int32SrCurrIndex_Pre = 0;

//EPWM Phase shift
int32_t int32Epwm2PhaseShift  = 2;
int32_t int32Epwm3PhaseShift  = 2;
int32_t int32Epwm4PhaseShift  = 100;
int32_t int32Epwm5PhaseShift  = 100;
int32_t int32Epwm6PhaseShift  = 100;

int32_t int32EPWM1DT_UP       = UINTx_DT_UP_MIN;
int32_t int32EPWM1DT_DOWN     = UINTx_DT_DOWN_MIN;

int32_t int32EPWM4DT_UP       = UINTx_DT_UP_MIN;
int32_t int32EPWM4DT_DOWN     = UINTx_DT_DOWN_MIN;

//int32_t int32P1Phase = 2;          //P1 phase shift
//int32_t int32P2Phase = 2;          //P2 Phase shift

//
//uint16_t u16BurstModeFlag = 0;
//uint16_t u16SrModeFlag    = 0;
//uint16_t u16SrModeFlagPre = 0;
//uint16_t u16OneTimeFlag   = 0;
//system parameter
uint16_t u16SysSM     = SYS_INIT_MODE;   //state machine
uint16_t u16WorkMode   = 0;               //0:normal; 1:Test mode
//system task timers
uint32_t u32SystimerCnt = 0;            //
uint32_t u32SysCurrTimerCnt = 0;        //
uint32_t u32SysTask300USCnt = 0;        //
uint32_t u32SysTask1MSCnt = 0;          //
uint32_t u32SysTask5MSCnt = 0;          //

uint16_t u16timerCnt = 0;
uint32_t u32SysTimer1SCnt = 0;

//work enable
uint16_t u8WorkEnable = 0;               //0:power off ,1 power on
uint16_t u8WorkEnable_USART = 0x01;

//uint16_t u16WorkEnableFlag = 0;        //0:disable,1 enable
//typedef struct
//{
//    uint16_t    u16WorkEnableFlag         : 1;  //work enable flag
//    uint16_t    u16SoftStartFinishFlag    : 1;  //soft start finish
//    uint16_t    RSVD                      : 14; //RSVD
//}SysFlagStructDef;

//typedef union
//{
//    SysFlagStructDef bits;
//    uint16_t   all;
//}SysFlagUnionDef;

//SysFlagUnionDef sysFlagRtdb = {0x0};

uint32_t  LedtimeCnt = 0;
uint32_t  systimercnt = 0;
//u32 u32Hrtim1Cmp   = 51327;
uint32_t u32HrPWMDTU1    = UINT1_DT_UP_VALUE;
uint32_t u32HrPWMDTU2    = UINT2_DT_UP_VALUE;
//uint32_t u32HrPWMDTU3    = 256;
uint32_t u32HrPWMDTU4    = UINT4_DT_UP_VALUE;
uint32_t u32HrPWMDTU5    = UINT5_DT_UP_VALUE;
//uint32_t u32HrPWMDTU6    = 256;

uint32_t u32HrPWMDTD1    = UINT1_DT_DOWN_VALUE;
uint32_t u32HrPWMDTD2    = UINT2_DT_DOWN_VALUE;
//uint32_t u32HrPWMDTD3    = 512;
uint32_t u32HrPWMDTD4    = UINT4_DT_DOWN_VALUE;
uint32_t u32HrPWMDTD5    = UINT5_DT_DOWN_VALUE;
//uint32_t u32HrPWMDTD6    = 512;

//uint32_t u32HrpwmPeriod       = UINT1_PRD_VALUE;

uint32_t u32HrpwmPhase1       = UINT1_PHASE_1;
uint32_t u32HrpwmPhase2       = UINT1_PHASE_2;
uint32_t u32HrpwmPhase3       = UINT1_PHASE_3;
uint32_t u32HrpwmPhase4       = UINT1_PHASE_4;
uint32_t u32HrpwmPhase5       = UINT1_PHASE_5;

uint32_t s32DeltPhase1        = 0;
uint32_t s32DeltPhase2        = 0;

uint32_t u32HrpwmCMPA           = (UINT1_PRD_VALUE >> 1);
uint32_t u32HrpwmCMPB           = (UINT1_PRD_VALUE >> 1);
//uint32_t u32HrpwmCMPE           = (UINT1_PRD_VALUE >> 1);
//uint32_t u32HrpwmCMPF           = (UINT1_PRD_VALUE >> 1);
//uint32_t u32HrpwmCMPSA           = 18290;
//uint32_t u32HrpwmCMPSB           = 21948;

uint32_t u32HrpwmCMPA_U3           = UINT1_DT_UP_VALUE;
uint32_t u32HrpwmCMPB_U3           = (UINT1_PRD_VALUE >> 1);
uint32_t u32HrpwmCMPE_U3           = (UINT1_PRD_VALUE >> 1) + UINT1_DT_DOWN_VALUE;
uint32_t u32HrpwmCMPF_U3           = UINT1_PRD_VALUE;

uint32_t u32HrpwmCMPA_U4           = UINT1_DT_UP_VALUE;
uint32_t u32HrpwmCMPB_U4           = (UINT1_PRD_VALUE >> 1);
uint32_t u32HrpwmCMPE_U4           = (UINT1_PRD_VALUE >> 1) + UINT1_DT_DOWN_VALUE;
uint32_t u32HrpwmCMPF_U4           = UINT1_PRD_VALUE;

//uint32_t  s32SRDuty                = (UINT1_PRD_VALUE >> 1);

uint16_t u16FanDuty                = 5;
uint16_t u16FANPrd                 = 10;
uint16_t u16DebugDelayCNt          = 0;

uint16_t u16PowerON_flag           = 0;
uint16_t u16EepromWriteEnable      = 0;
int32_t  s32EepromTempData         = 0;

//int16_t  s16TemptureArray[2][16]   = {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},
//                                      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,}
//                                     };

int16_t  s16TempPhaseA = 25;
int16_t  s16TempPhaseB = 25;
int16_t  s16TempPhaseAADC = 0;
int16_t  s16TempPhaseBADC = 0;
int16_t  s16TempPhaseASum = 0;
int16_t  s16TempPhaseBSum = 0;
uint16_t u16TempStoreCounter = 0;
/*******************************************************************************
 * Function implementation - global ('extern') and local ('static')
 ******************************************************************************/
//function
void SysStateMachineMag(void);
void HwFualtCheck(void);
void SwFualtCheck(void);
static inline void PowerOnConfig(void);
void ControllerInit(void);
void LedDriverMang(void);
void LogoDataUpdate(uint8_t *txdatabuffer);
//void LLC_CTRL_Loop(void);
/*******************************************************************************
 * @brief  Main function of template project
 * @param  None
 * @retval int32_t return value, if needed
 ******************************************************************************/
int32_t main(void)
{   
	  /* Unlock peripherals or registers */
    LL_PERIPH_WE(LL_PERIPH_SEL);
    /* Configure BSP */
    BSP_CLK_Init();	

    //CalibrationWrite();
	
    CalibrationRead();
  
    /* gpio init*/ 
	  Gpio_Init();
	  
    /********Event port**********************************/
	  //EventPort_Init(); 

    /****************ADC configuration*******************/
	  ADCx_Init();  
	  
	  /****************DAC configuration*******************/
	  DACx_ConversionInit();
	  /****************DAC configuration*******************/
	  LLC_CmpConfig();
	
	  //HRTIM configuration
		HRTIM_Config();					
			
		//EMB configuration
		EMB_Init();
		
		//TMRA configuration
		TmrAConfig();
		
		//USART configuration
		USART2Config();
		
		//DMA configuration
		DMA_Config();
		
		//Application initionation
		ControllerInit();
		
		//Ctrl timer configuration
		Ctrl_TMR0_Config();
		
		WDT_Config();
		
		/* First reload counter to start WDT */
    WDT_FeedDog();
		
	  /* MCU Peripheral registers write protected */
		//LL_PERIPH_WP(EXAMPLE_PERIPH_WP);
		
		for (;;) 
		{
				u32SysCurrTimerCnt = u32SystimerCnt;

        if((u32SysTask300USCnt + TASK_300US_CNT) <= u32SysCurrTimerCnt) //task300us
        {
            u32SysTask300USCnt = u32SysCurrTimerCnt;
            //HWREG(GPIODATA_BASE + GPIO_O_GPASET) = GPIO_GPASET_GPIO27;
            //ILLLC_HAL_PWMDriverEnable(1,0);
            //soft start
            if(sysFlagRtdb.bits.u16WorkEnableFlag == 1)
            {
                //voltage soft start
                if(int32VoutSlewQ0 < int32VoutSetADCQ0)
                {
                    int32VoutSlewQ0 += VOLT_SLEW_STEP;
                    if(int32VoutSlewQ0 >= int32VoutSetADCQ0)
                    {
                        int32VoutSlewQ0 = int32VoutSetADCQ0;
                    }
//										else if(int32VoutSlewQ0 < 300)
//										{
//											  int32VoutSlewQ0 = 300;
//										}
                }
                else
                {
                    int32VoutSlewQ0 -= VOLT_SLEW_STEP;
                    if(int32VoutSlewQ0 < int32VoutSetADCQ0)
                    {
                       int32VoutSlewQ0 = int32VoutSetADCQ0;
                    }
                }

#if 0
                //current soft start
                if(int32IoutSlewQ0 < int32IoutSetADCQ0)
                {
                    int32IoutSlewQ0 += CURR_SLEW_STEP;
                    if(int32IoutSlewQ0 > int32IoutSetADCQ0)
                    {
                        int32IoutSlewQ0 = int32IoutSetADCQ0;
                    }
                }
                else
                {
                    int32IoutSlewQ0 -= CURR_SLEW_STEP;
                    if(int32IoutSlewQ0 < int32IoutSetADCQ0)
                    {
                        int32IoutSlewQ0 = int32IoutSetADCQ0;
                    }
                }
#else
                int32IoutSlewQ0 = int32IoutSetADCQ0;
#endif
            }
            else
            {
                int32VoutSlewQ0 = int32VoutADCQ0;
							  //int32VoutSlewQ0 = 0;
                int32IoutSlewQ0 = 0;
            }

            //HW fault check
            HwFualtCheck();
            //HWREG(GPIODATA_BASE + GPIO_O_GPACLEAR) = GPIO_GPASET_GPIO27;
        }

        if((u32SysTask1MSCnt + TASK_1MS_CNT) <= u32SysCurrTimerCnt) //task1MS
        {
             u32SysTask1MSCnt = u32SysCurrTimerCnt;
//             HWREG(GPIODATA_BASE + GPIO_O_GPASET) = GPIO_GPASET_GPIO27;
#if OPENLOOP_ENABLE == 1
             if(SysPowerONState  >= 1)
             {
                 if(u16DebugDelayCNt < 20)
                 {
                     u16DebugDelayCNt++;
                 }
                 else if(u16DebugDelayCNt == 20)
                 {

                     //ILLLC_HAL_PWMDriverEnable(1,1);
                     //ILLLC_HAL_SRDriverEnable(1,1);
									   LLC_PWM_ENABLE_IDLEMODE_ALL;
                     u16DebugDelayCNt = 21;
                 }
             }
#endif

             //GPIO_togglePin(27);
             //real time value
             int32VoutRtdbQ11 = int32VoutCalibADCQ0 * int32VoutScaleQ11;
             int32IoutRtdbQ11 = int32IoutCalibADCQ0 * int32IoutScaleQ11 + CONSTIoutOffsetQ11;
             int32VinRtdbQ11  = int32VinCalibADCQ0  * int32VinScaleQ11;
						 
             if(DMA_GetTransCompleteStatus(TX_DMA_UNIT, TX_DMA_TC_FLAG))
						 {
							  DMA_ClearTransCompleteStatus(TX_DMA_UNIT, TX_DMA_TC_FLAG);
								USART_FuncCmd(USART_UNIT, (USART_TX), DISABLE);
						 }
             //Power on set and configuration
             PowerOnConfig();
//             HWREG(GPIODATA_BASE + GPIO_O_GPACLEAR) = GPIO_GPASET_GPIO27;
						 
             s16TempPhaseASum += int16Temp1ADCQ0;
						 s16TempPhaseBSum += int16Temp2ADCQ0;
						 u16TempStoreCounter++;
						 if(u16TempStoreCounter > 16)
						 {
							 u16TempStoreCounter = 0;
							 s16TempPhaseAADC = (s16TempPhaseASum >> 4);
							 s16TempPhaseBADC = (s16TempPhaseBSum >> 4);
							 s16TempPhaseASum = 0;
							 s16TempPhaseBSum = 0;
						 }
        }

        if((u32SysTask5MSCnt + TASK_5MS_CNT) <= u32SysCurrTimerCnt) //task5MS
        {
             u32SysTask5MSCnt = u32SysCurrTimerCnt;
						 
             //USART 
						 if(DMA_GetTransCompleteStatus(RX_DMA_UNIT,RX_DMA_TC_FLAG))
						 {
								//DMA_ClearTransCompleteStatus(RX_DMA_UNIT, RX_DMA_TC_FLAG);
                
							  if(m_au8RxBuf[3] == '1')
								{ 
									if(m_au8RxBuf[8] == '1')
									{
										u8WorkEnable_USART = 0x1;
									}
									else
									{
										u8WorkEnable_USART = 0x0;
									}
								}
								else if(m_au8RxBuf[3] == '2')
								{
									if(m_au8RxBuf[8] == '1') //vout and iout calibration
									{
										s32EepromTempData = 0;
										s32EepromTempData += m_au8RxBuf[14] - '0';
										s32EepromTempData *= 10;
										s32EepromTempData += m_au8RxBuf[15] - '0';
										s32EepromTempData *= 10;
										s32EepromTempData += m_au8RxBuf[16] - '0';
										s32EepromTempData *= 10;
										s32EepromTempData += m_au8RxBuf[17] - '0';
										
										int32VoutCalibGainQ11 = s32EepromTempData;
										
										s32EepromTempData = 0;
										s32EepromTempData += m_au8RxBuf[21] - '0';
										s32EepromTempData *= 10;
										s32EepromTempData += m_au8RxBuf[22] - '0';
										s32EepromTempData *= 10;
										s32EepromTempData += m_au8RxBuf[23] - '0';
										s32EepromTempData *= 10;
										s32EepromTempData += m_au8RxBuf[24] - '0';
										s32EepromTempData *= 10;
										s32EepromTempData += m_au8RxBuf[25] - '0';
										s32EepromTempData *= 10;
										s32EepromTempData += m_au8RxBuf[26] - '0';									
										int32VoutCalibOffsentQ11 = s32EepromTempData;
										if(m_au8RxBuf[20] == '-')	
										{
											int32VoutCalibOffsentQ11 = -s32EepromTempData;
										}
										
										s32EepromTempData = 0;
										s32EepromTempData += m_au8RxBuf[32] - '0';
										s32EepromTempData *= 10;
										s32EepromTempData += m_au8RxBuf[33] - '0';
										s32EepromTempData *= 10;
										s32EepromTempData += m_au8RxBuf[34] - '0';
										s32EepromTempData *= 10;
										s32EepromTempData += m_au8RxBuf[35] - '0';
										int32IoutCalibGainQ11 = s32EepromTempData;
										
										s32EepromTempData = 0;
										s32EepromTempData += m_au8RxBuf[39] - '0';
										s32EepromTempData *= 10;
										s32EepromTempData += m_au8RxBuf[40] - '0';
										s32EepromTempData *= 10;
										s32EepromTempData += m_au8RxBuf[41] - '0';
										s32EepromTempData *= 10;
										s32EepromTempData += m_au8RxBuf[42] - '0';
										s32EepromTempData *= 10;
										s32EepromTempData += m_au8RxBuf[43] - '0';
										s32EepromTempData *= 10;
										s32EepromTempData += m_au8RxBuf[44] - '0';									
										int32IoutCalibOffsentQ11 = s32EepromTempData;
										if(m_au8RxBuf[38] == '-')	
										{
											int32IoutCalibOffsentQ11 = -s32EepromTempData;
										}
									}
									else if(m_au8RxBuf[8] == '2')//vin calibration
									{
										s32EepromTempData = 0;
										s32EepromTempData += m_au8RxBuf[14] - '0';
										s32EepromTempData *= 10;
										s32EepromTempData += m_au8RxBuf[15] - '0';
										s32EepromTempData *= 10;
										s32EepromTempData += m_au8RxBuf[16] - '0';
										s32EepromTempData *= 10;
										s32EepromTempData += m_au8RxBuf[17] - '0';
										int32VinCalibGainQ11 = s32EepromTempData;
										
										s32EepromTempData = 0;
										s32EepromTempData += m_au8RxBuf[21] - '0';
										s32EepromTempData *= 10;
										s32EepromTempData += m_au8RxBuf[22] - '0';
										s32EepromTempData *= 10;
										s32EepromTempData += m_au8RxBuf[23] - '0';
										s32EepromTempData *= 10;
										s32EepromTempData += m_au8RxBuf[24] - '0';
										s32EepromTempData *= 10;
										s32EepromTempData += m_au8RxBuf[25] - '0';
										s32EepromTempData *= 10;
										s32EepromTempData += m_au8RxBuf[26] - '0';							
										int32VinCalibOffsentQ11 = s32EepromTempData;
										if(m_au8RxBuf[20] == '-')	
										{
											int32VinCalibOffsentQ11 = -s32EepromTempData;
										}
									}
									
									if(m_au8RxBuf[7] == '1')
									{
										u16EepromWriteEnable |= 0x01;
									}									
								}
									
								//u8WorkEnable_USART = 0x1;
								DMA_ClearTransCompleteStatus(RX_DMA_UNIT, RX_DMA_TC_FLAG);
								DMA_SetTransCount(RX_DMA_UNIT, RX_DMA_CH, 45);
                (void)DMA_ChCmd(RX_DMA_UNIT, RX_DMA_CH, ENABLE);							 						 
						 }

						 
#if CURR_LIMIT_ENABLE == 1
             //current limit base on output voltage
             if(sysFlagRtdb.bits.u16SoftStartFinishFlag)
             {
            	 int32IoutLimitQ0 = ((CURR_LIMIT_K_Q11 * int32VoutCalibADCQ0) >> 11) + CURR_LIMIT_B_Q0;
            	 if(int32IoutLimitQ0 > CURRENT_SET_MAX_Q0)
            	 {
            		 int32IoutLimitQ0 = CURRENT_SET_MAX_Q0;
            	 }
            	 else if(int32IoutLimitQ0 < CURRENT_SET_MIN_Q0)
            	 {
            		 int32IoutLimitQ0 = CURRENT_SET_MIN_Q0;
            	 }
             }
             else
             {
            	 int32IoutLimitQ0 = CURRENT_SET_MAX_Q0;
             }
#else
             int32IoutLimitQ0 = CURRENT_SET_MAX_Q0;
#endif
             if(int32IoutLimitQ0 < int32IoutSetADCQ0)
             {
            	 int32IoutSetADCQ0 = int32IoutLimitQ0;
             }
             //Sw fault check
             SwFualtCheck();
//             //system state machine
#if OPENLOOP_ENABLE == 0
             SysStateMachineMag();
#endif
             LedDriverMang();
             //
						 
						 //
						 WDT_FeedDog();
						 
						 //
						 u16timerCnt++;
						 if(u16timerCnt == 50)
						 {
							  s16TempPhaseA = TemperatureBaseLookUpTable(s16TempPhaseAADC);
							  s16TempPhaseB = TemperatureBaseLookUpTable(s16TempPhaseBADC);
						 }
						 else if(u16timerCnt == 100)
						 {
							 LogoDataUpdate(&m_au8TxBuf[0]);							 
						 }
             //1S task
             else if(u16timerCnt >= 200)
             {
                u16timerCnt = 0;
                u32SysTimer1SCnt++;
							  if((u16EepromWriteEnable & 0x1) == 0x1)
								{
									 u16EepromWriteEnable &= 0xFFFE;
									 WDT_FeedDog();
									 CalibrationWrite();
								}
							  Usart2_TxEnable();
             }
        }
    }
}


/*******************************************************
 * @brief  TMR0 compare interrupt callback function.
 * @param  None
 * @retval None
 *******************************************************/
void ADC1_Handler(void)
{
	  //CM_GPIO->POSRC = (1 << 12);
	
#if 0
	  //read ADC value
    int32IoutADCQ0     = READ_REG32(CM_ADC1->DR3);//ADC_GetValue(CM_ADC1,ADC_CH14);
	  int16Temp1ADCQ0    = READ_REG32(CM_ADC1->DR10);//ADC_GetValue(CM_ADC1,ADC_CH12);
    int32PriCurr1ADCQ0 = READ_REG32(CM_ADC1->DR13);//ADC_GetValue(CM_ADC1,ADC_CH13);
	  int32VinADCQ0      = READ_REG32(CM_ADC1->DR15);//ADC_GetValue(CM_ADC1,ADC_CH15);//2800;
	  //int32VinADCQ0      = 2805;
    int32VoutADCQ0     = READ_REG32(CM_ADC3->DR9);//ADC_GetValue(CM_ADC2,ADC_CH9);
	  //int32VoutADCQ0     = READ_REG32(CM_ADC2->DR9);//ADC_GetValue(CM_ADC2,ADC_CH9);
    int16Temp2ADCQ0    = READ_REG32(CM_ADC2->DR11);//ADC_GetValue(CM_ADC1,ADC_CH12);
	  int32PriCurr2ADCQ0 = READ_REG32(CM_ADC2->DR12);//ADC_GetValue(CM_ADC1,ADC_CH12);
#else
    int32IoutADCQ0     = READ_REG32(CM_ADC1->DR0);//ADC_GetValue(CM_ADC1,ADC_CH14);
	  int32PriCurr1ADCQ0 = READ_REG32(CM_ADC1->DR1);//ADC_GetValue(CM_ADC1,ADC_CH13);
	  int32VinADCQ0      = READ_REG32(CM_ADC1->DR2);//ADC_GetValue(CM_ADC1,ADC_CH15);//2800;
	  int16Temp1ADCQ0    = READ_REG32(CM_ADC1->DR3);//ADC_GetValue(CM_ADC1,ADC_CH12);
    //int32VinADCQ0      = 3400;

	  //int32VinADCQ0      = 2805;
    int32VoutADCQ0     = READ_REG32(CM_ADC2->DR4);//ADC_GetValue(CM_ADC2,ADC_CH9);
	  //int32VoutADCQ0     = READ_REG32(CM_ADC2->DR9);//ADC_GetValue(CM_ADC2,ADC_CH9);
    int16Temp2ADCQ0    = READ_REG32(CM_ADC2->DR6);//ADC_GetValue(CM_ADC1,ADC_CH12);
	  int32PriCurr2ADCQ0 = READ_REG32(CM_ADC2->DR5);//ADC_GetValue(CM_ADC1,ADC_CH12);

#endif	
//	  int32VoutADCQ0 = 0;
//	  int32IoutADCQ0 = 0;
//	  int32VinADCQ0  = 2800;
    //filer
    int32VoutFilterADCQ0 = (int32VoutFilterADCQ0 * VoutLPFCoff_B_Q16 +  int32VoutADCQ0 * VoutLPFCoff_A_Q16) >> 16;
	  //int32VoutFilterADCQ0 = int32VoutADCQ0;
    int32IoutFilterADCQ0 = (int32IoutFilterADCQ0 * IoutLPFCoff_B_Q16 +  int32IoutADCQ0 * IoutLPFCoff_A_Q16) >> 16;
    int32VinFilterADCQ0 =  (int32VinFilterADCQ0  * VinLPFCoff_B_Q16  +  int32VinADCQ0  * VinLPFCoff_A_Q16) >> 16;
    int32PriCurr1FilterADCQ0 = (int32PriCurr1FilterADCQ0 * PriCurrLPFCoff_B_Q16 +  int32PriCurr1ADCQ0 * PriCurrLPFCoff_A_Q16) >> 16;
    int32PriCurr2FilterADCQ0 = (int32PriCurr2FilterADCQ0 * PriCurrLPFCoff_B_Q16 +  int32PriCurr2ADCQ0 * PriCurrLPFCoff_A_Q16) >> 16;

//    s32SumPriCurr1 += int32PriCurr1FilterADCQ0;
//    s32SumPriCurr2 += int32PriCurr2FilterADCQ0;

    //calibration
    int32VoutCalibADCQ0 = ((int32VoutFilterADCQ0 * int32VoutCalibGainQ11) >> 11) + int32VoutCalibOffsentQ11;
    int32IoutCalibADCQ0 = ((int32IoutFilterADCQ0 * int32IoutCalibGainQ11) >> 11) + int32IoutCalibOffsentQ11;
		if(int32IoutCalibADCQ0 < 0)
		{
			int32IoutCalibADCQ0 = 0;
		}
    int32VinCalibADCQ0  = ((int32VinFilterADCQ0  * int32VinCalibGainQ11) >> 11)  + int32VinCalibOffsentQ11;

		VloopCtrl.int32B0 = ((int32VinCalibADCQ0 * CTRL_KP_GAIN_Q16 + CTRL_KP_OFFSENT_Q16) >> 8);
		if(VloopCtrl.int32B0 >= 12032)
		{
			VloopCtrl.int32B0 = 12032;
		}
		else if(VloopCtrl.int32B0 <= 9856)
		{
			VloopCtrl.int32B0 = 9856;
		}
		
		
    LLC_CTRL_Loop();
    

   int32IoutFilterADCQ0_PRE = int32IoutFilterADCQ0;

#if OPENLOOP_ENABLE == 1

        //int32EpwmPrdUpdate = (int32VoutFilterADCQ0 >> 2);
        int32EpwmPrdUpdate  = VLOOP_CTRL_MAX;
        if(int32EpwmPrdUpdate < BURST_CTRL_VALUE)
        {
            int32EpwmPrdUpdate = BURST_CTRL_VALUE;
        }
        else if(int32EpwmPrdUpdate > VLOOP_CTRL_MAX)
        {
            int32EpwmPrdUpdate = VLOOP_CTRL_MAX;
        }

        int32P1Phase = 0;
				int32P2Phase = 0;
				
				s32SRDuty = int32EpwmPrdUpdate >> 1;

//        int32Epwm3SRDuty_A = int32EpwmPrdUpdate >> 1;
//        int32Epwm3SRDuty_B = int32EpwmPrdUpdate >> 1;

//        int32Epwm6SRDuty_A = int32EpwmPrdUpdate >> 1;
//        int32Epwm6SRDuty_B = int32EpwmPrdUpdate >> 1;

#endif

//    int32Epwm2PhaseShift = int32P1Phase;
//    int32Epwm3PhaseShift = 2;

//    int32Epwm4PhaseShift = (int32EpwmPrdUpdate >> 1);
//    int32Epwm5PhaseShift = (int32EpwmPrdUpdate >> 1) + int32P2Phase;
//    int32Epwm6PhaseShift = (int32EpwmPrdUpdate >> 1) + int32P2Phase;


	
	  
		
		//CM_GPIO->POSRF = (1 << 2);//2.46us
	  //GPIO_SetPins(GPIO_PORT_F,GPIO_PIN_02);
	  u32HrpwmCMPA = (int32EpwmPrdUpdate >> 1);
						
		//CMP
		u32HrpwmPhase1 = int32P1Phase;
		u32HrpwmPhase2 = u32HrpwmPhase1;
		u32HrpwmPhase3 = (int32EpwmPrdUpdate >> 2);
		u32HrpwmPhase4 = u32HrpwmPhase3 + int32P2Phase;
		u32HrpwmPhase5 = u32HrpwmPhase4;

    
		//SR UNIT3
		u32HrpwmCMPE_U3           = int32EPWM1DT_UP;                  //UINT1_DT_UP
		u32HrpwmCMPF_U3           = u32HrpwmCMPA + int32EPWM1DT_DOWN; //UINT1_DT_DOWN
		u32HrpwmCMPA_U3           = u32HrPWMDTU2 + s32SRDuty;
		if(u32HrpwmCMPA_U3 > u32HrpwmCMPA)
		{
			u32HrpwmCMPA_U3 = u32HrpwmCMPA;
		}
		u32HrpwmCMPB_U3           = u32HrpwmCMPF_U3 + s32SRDuty;
		if(u32HrpwmCMPB_U3 > int32EpwmPrdUpdate - 128)
		{
			u32HrpwmCMPB_U3 =  int32EpwmPrdUpdate - 128; 
		}
	
	  //SR UNIT4
		u32HrpwmCMPE_U4           = u32HrPWMDTU5;               		//UINT5_DT_UP
		u32HrpwmCMPF_U4           = u32HrpwmCMPA + u32HrPWMDTD5;		//UINT5_DT_DOWN
		u32HrpwmCMPA_U4           = u32HrPWMDTU5 + s32SRDuty;
		if(u32HrpwmCMPA_U4 > u32HrpwmCMPA)
		{
			u32HrpwmCMPA_U4 = u32HrpwmCMPA;
		}
		u32HrpwmCMPB_U4           = u32HrpwmCMPF_U4 + s32SRDuty;
		if(u32HrpwmCMPB_U4 > int32EpwmPrdUpdate - 128)
		{
			u32HrpwmCMPB_U4 =  int32EpwmPrdUpdate - 128; 
		}
				
	  if((CM_HRPWM_COMMON->GBSFLR & 0x0003F) == 0x0003F)
		{
				//CLR_REG32_BIT(CM_HRPWM_COMMON->GBCONR,0x0003f);
				CLR_REG32_BIT(CM_HRPWM_COMMON->GBSFLR,0x0003f);
				//update period,CMPx,Phasex....and so on

				WRITE_REG32(CM_HRPWM1->HRPERBR,int32EpwmPrdUpdate);
			
				WRITE_REG32(CM_HRPWM1->HRGCMCR,u32HrpwmCMPA);
			
			  WRITE_REG32(CM_HRPWM1->SCMCR,(int32EpwmPrdUpdate >> 2));//ADC trigger
			
			  WRITE_REG32(CM_HRPWM5->HRGCMCR,u32HrpwmCMPA);
			
				WRITE_REG32(CM_HRPWM2->HRGCMCR,u32HrpwmCMPA);
				WRITE_REG32(CM_HRPWM6->HRGCMCR,u32HrpwmCMPA);
				
				WRITE_REG32(CM_HRPWM4->HRGCMCR,u32HrpwmCMPA_U4);
				WRITE_REG32(CM_HRPWM4->HRGCMDR,u32HrpwmCMPB_U4);
				WRITE_REG32(CM_HRPWM4->HRGCMGR,u32HrpwmCMPE_U4);
				WRITE_REG32(CM_HRPWM4->HRGCMHR,u32HrpwmCMPF_U4);
			
				WRITE_REG32(CM_HRPWM3->HRGCMCR,u32HrpwmCMPA_U3);
				WRITE_REG32(CM_HRPWM3->HRGCMDR,u32HrpwmCMPB_U3);
				WRITE_REG32(CM_HRPWM3->HRGCMGR,u32HrpwmCMPE_U3);
				WRITE_REG32(CM_HRPWM3->HRGCMHR,u32HrpwmCMPF_U3);
				

				
				WRITE_REG32(CM_HRPWM1->PHSCMP1B,u32HrpwmPhase1);
				WRITE_REG32(CM_HRPWM1->PHSCMP2B,u32HrpwmPhase2);
				WRITE_REG32(CM_HRPWM1->PHSCMP3B,u32HrpwmPhase3);
				WRITE_REG32(CM_HRPWM1->PHSCMP4B,u32HrpwmPhase4);
				WRITE_REG32(CM_HRPWM1->PHSCMP5B,u32HrpwmPhase5);							
	
	
				//
				WRITE_REG32(CM_HRPWM1->HRDTUBR,u32HrPWMDTU1);
				WRITE_REG32(CM_HRPWM1->HRDTDBR,u32HrPWMDTD1);
				WRITE_REG32(CM_HRPWM5->HRDTUBR,u32HrPWMDTU5);
				WRITE_REG32(CM_HRPWM5->HRDTDBR,u32HrPWMDTD5);
				WRITE_REG32(CM_HRPWM2->HRDTUBR,u32HrPWMDTU2);
				WRITE_REG32(CM_HRPWM2->HRDTDBR,u32HrPWMDTD2);
				WRITE_REG32(CM_HRPWM6->HRDTUBR,u32HrPWMDTU4);
				WRITE_REG32(CM_HRPWM6->HRDTDBR,u32HrPWMDTD4);
				
				
//				CLR_REG32_BIT(CM_HRPWM1->GBCONR,0x0003f);
        //HRPWM_Buf_U1SingleTransTrigger();
				 WRITE_REG32(CM_HRPWM_COMMON->GBCONR,0xC000003f);		
			}	
				
			//WRITE_REG32(TMRA_UNIT->CMPAR1,int32VoltErr_0);
			//TMRA_SetCompareValue(TMRA_UNIT, TMRA_PWM_CH_FAN,int32VoltErr+600);
			u32SystimerCnt++;
			
		//ADC_ClearStatus(CM_ADC1,ADC_FLAG_EOCA);
    //GPIO_ResetPins(GPIO_PORT_F,GPIO_PIN_02);
		//	CM_GPIO->PORRC = (1 << 12);
    //TMR0_ClearStatus(CM_TMR0_1, TMR0_FLAG_CMP_B);
}

/*********************************************
* function    : SysStateMachineMag()
* description :
*
*
**********************************************/
void SysStateMachineMag(void)
{
     if(u16SysSM == SYS_INIT_MODE)
     {
         //HW driver disable
         SRA_HAL_DISABLE;  //SRA HW enable
         SRB_HAL_DISABLE;  //SRB HW enable

         //EPWM disable
         //ILLLC_HAL_PWMDriverEnable(0,0);
         //ILLLC_HAL_SRDriverEnable(0,0);
			   LLC_PWM_DISABLE_IDLEMODE_ALL;
         //clear flag
         sysFlagRtdb.bits.u16WorkEnableFlag = 0;
         sysFlagRtdb.bits.u16SoftStartFinishFlag = 0;

         u16SysSM = SYS_STABDY_MODE;
     }
     else if(u16SysSM == SYS_STABDY_MODE)
     {
         //HW driver disable
         SRA_HAL_DISABLE;  //SRA HW enable
         SRB_HAL_DISABLE;  //SRB HW enable

         //EPWM disable
         //ILLLC_HAL_PWMDriverEnable(0,0);
         //ILLLC_HAL_SRDriverEnable(0,0);
			   LLC_PWM_DISABLE_IDLEMODE_ALL;
         //clear flag
         sysFlagRtdb.bits.u16WorkEnableFlag = 0;
         sysFlagRtdb.bits.u16SoftStartFinishFlag = 0;

         if(u32Faultflag & Fault_MASK)
         {
             u16SysSM = SYS_FAULT_MODE;
         }
         else
         {
              if(u8WorkEnable && u8WorkEnable_USART)
               {
                  //SRA_HAL_ENABLE;  //SRA HW enable
                  //SRB_HAL_ENABLE;  //SRB HW enable

                  //ILLLC_HAL_PWMDriverEnable(1,1);
                  sysFlagRtdb.bits.u16WorkEnableFlag = 1;          //

                  u16SysSM = SYS_WORK_MODE;    //work mode
               }
          }
     }
     else if(u16SysSM == SYS_WORK_MODE)
     {
         if(u32Faultflag & Fault_MASK)
         {
             //HW driver disable
             SRA_HAL_DISABLE;  //SRA HW enable
             SRB_HAL_DISABLE;  //SRB HW enable

             //EPWM disable
             //ILLLC_HAL_PWMDriverEnable(0,0);
             //ILLLC_HAL_SRDriverEnable(0,0);
					   LLC_PWM_DISABLE_IDLEMODE_ALL;
             //clear flag
              sysFlagRtdb.bits.u16WorkEnableFlag = 0;
              sysFlagRtdb.bits.u16SoftStartFinishFlag = 0;

             u16SysSM = SYS_FAULT_MODE;
         }
         else if((u8WorkEnable == 0) || (u8WorkEnable_USART == 0)) //shut down
         {
             SRA_HAL_DISABLE;  //SRA HW enable
             SRB_HAL_DISABLE;  //SRB HW enable

             //EPWM disable
             //ILLLC_HAL_PWMDriverEnable(0,0);
             //ILLLC_HAL_SRDriverEnable(0,0);
					   LLC_PWM_DISABLE_IDLEMODE_ALL;
             sysFlagRtdb.bits.u16WorkEnableFlag = 0;
             sysFlagRtdb.bits.u16SoftStartFinishFlag = 0;

             u16SysSM = SYS_STABDY_MODE;
         }
         else
         {
             //check soft start is finish
             if(int32VoutSlewQ0 >= int32VoutSetADCQ0)
             {
            	 //if(int32VoutRtdbQ11 > 94208) //46V
            	 {
					 sysFlagRtdb.bits.u16SoftStartFinishFlag = 1;
					 //SRA_HAL_ENABLE;  //SRA HW enable
					 //SRB_HAL_ENABLE;  //SRB HW enable
            	 }
             }

         }

     }
     else if(u16SysSM == SYS_FAULT_MODE)
     {
         //HW driver disable
         SRA_HAL_DISABLE;  //SRA HW enable
         SRB_HAL_DISABLE;  //SRB HW enable

         //EPWM disable
         //ILLLC_HAL_PWMDriverEnable(0,0);
         //ILLLC_HAL_SRDriverEnable(0,0);
			   LLC_PWM_DISABLE_IDLEMODE_ALL;
         //clear flag
         sysFlagRtdb.bits.u16WorkEnableFlag = 0;
         sysFlagRtdb.bits.u16SoftStartFinishFlag = 0;

         if((u32Faultflag & Fault_MASK) == 0x0) //the fault is clear
         {
             u16SysSM = SYS_STABDY_MODE;
         }
     }
}

/*********************************************
* function    : HwFualtCheck()
* description : HW_OCP,HW_OVP,HW_UVP,
*               HW_PRICURR1_OCP
*               HW_PRICURR2_OCP
*               CMPSS1_PRIMARY1_OCP
*               CMPSS2_PRIMARY2_OCP
*               CMPSS3_OVP
*               CMPSS3_OCP
*
**********************************************/
void HwFualtCheck(void)
{
    uint32_t inputxbarflag_EMB0 = 0;
	  uint32_t inputxbarflag_EMB1 = 0;
    //inputxbarflag = XBAR_getInputFlagStatus(XBAR_InputFlag inputFlag)

    inputxbarflag_EMB0 = READ_REG32(CM_EMB0->STAT);//HWREG(XBAR_BASE + XBAR_O_FLG2);
	  inputxbarflag_EMB1 = READ_REG32(CM_EMB1->STAT);
    //HW_OVP check
    if((inputxbarflag_EMB0 & EMB_HW_OVP) || (inputxbarflag_EMB1 & EMB_HW_OVP))
    {
        u32Faultflag  |= Fault_HW_OVP;
    }
		
    //HW_OCP check
    if((inputxbarflag_EMB0 & EMB_HW_OCP) || (inputxbarflag_EMB1 & EMB_HW_OCP))
    {
        u32Faultflag  |= Fault_HW_OCP;
    }
		
    //HW_UVP check
//    if(inputxbarflag & USER_XBAR_INPUT_FLG_INPUT3)
//    {
//        u32Faultflag  |= Fault_HW_UVP;
//    }

    //HW_PRICURR1 check
    if(inputxbarflag_EMB0 & EMB_HW_PRICURRT1_OCP)
    {
        u32Faultflag  |= Fault_PRICURR1_HW_OCP;
    }

    //HW_PRICURR2 check
    if(inputxbarflag_EMB1 & EMB_HW_PRICURRT2_OCP)
    {
        u32Faultflag  |= Fault_PRICURR2_HW_OCP;
    }
    
		//CMP primary OCP - phaseA
		if((inputxbarflag_EMB0 & EMB_CMP_PRICURRT1_OCP))
		{
			 u32Faultflag  |= Fault_PRICURR1_CMPSS_OCP;
		}
		
		//CMP primary OCP - phaseB
		if((inputxbarflag_EMB1 & EMB_CMP_PRICURRT2_OCP))
		{
			 u32Faultflag  |= Fault_PRICURR2_CMPSS_OCP;
		}
		
		//primary
		if((inputxbarflag_EMB0 & EMB_CMP_OCP) || (inputxbarflag_EMB1 & EMB_CMP_OCP))
    {
        u32Faultflag  |= Fault_CMPSS_OCP;
    }
    //clear flag
    //HWREG(XBAR_BASE + XBAR_O_CLR2) =  inputxbarflag ;
    //WRITE_REG32(CM_EMB0->STATCLR,EMB_HW_FAULT_ALL);
    //WRITE_REG32(CM_EMB1->STATCLR,EMB_HW_FAULT_ALL);
#if 0
    //
    inputxbarflag = HWREG(XBAR_BASE + XBAR_O_FLG1);
    //SW CMPSS1 check
    if(inputxbarflag & USER_XBAR_INPUT_FLG_CMPSS1_CTRIPH)
    {
        u32Faultflag  |= Fault_PRICURR1_CMPSS_OCP;
    }

    //SW CMPSS2 check
    if(inputxbarflag & USER_XBAR_INPUT_FLG_CMPSS2_CTRIPH)
    {
        u32Faultflag  |= Fault_PRICURR2_CMPSS_OCP;
    }

    //SW CMPSS3 check
    if(inputxbarflag & USER_XBAR_INPUT_FLG_CMPSS3_CTRIPH)
    {
        u32Faultflag  |= Fault_CMPSS_OVP;
    }

    //SW CMPSS4 check
    if(inputxbarflag & USER_XBAR_INPUT_FLG_CMPSS4_CTRIPH)
    {
        u32Faultflag  |= Fault_CMPSS_OCP;
    }

    //clear flag
    HWREG(XBAR_BASE + XBAR_O_CLR1) =  inputxbarflag ;
#endif
}

/*********************************************
* function    : SwFualtCheck()
* description : SW_OCP,SW_OVP,SW_UVP,
*               SW_VIN_OVP
*               SW_TEMP1_OTP
*               SW_TEMP2_OTP
*
**********************************************/
void SwFualtCheck(void)
{
    static int16_t s16VoutOvpTimes = 0;
    static int16_t s16VoutUvpTimes = 0;
    static int16_t s16VoutOcpTimes = 0;
    static int16_t s16VinOvpTimes = 0;
    static int16_t s16VinUvpTimes = 0;
    static int16_t s16VinOvpRecoverTimes = 0;
//    static int16_t s16VinUvpRecoverTimes = 0;
    static int16_t s16Temp1OTPTimes = 0;
    static int16_t s16Temp2OTPTimes = 0;
    static int16_t s16Temp1OTPRecoverTimes = 0;
    static int16_t s16Temp2OTPRecoverTimes = 0;

    //SW OVP check
    if(int32VoutRtdbQ11 > VOUT_OVP_VALUE)
    {
        s16VoutOvpTimes++;
        if(s16VoutOvpTimes >= VOUT_OVP_TIMES)
        {
            s16VoutOvpTimes = VOUT_OVP_TIMES;
        }
    }
    else if((int32VoutRtdbQ11 + 205) < VOUT_OVP_VALUE)
    {
        s16VoutOvpTimes--;
        if(s16VoutOvpTimes < 1)
        {
            s16VoutOvpTimes = 1;
        }
    }

    if(s16VoutOvpTimes ==  VOUT_OVP_TIMES)
    {
        u32Faultflag  |= Fault_SW_OVP;
    }

#if 0
    //SW UVP check
    if(sysFlagRtdb.bits.u16SoftStartFinishFlag == 1)
    {
        if(int32VoutRtdbQ11 < VOUT_UVP_VALUE)
        {
            s16VoutUvpTimes++;
            if(s16VoutUvpTimes >= VOUT_UVP_TIMES)
            {
                s16VoutUvpTimes = VOUT_UVP_TIMES;
            }
        }
        else if(int32VoutRtdbQ11 > (VOUT_OVP_VALUE + 205))
        {
            s16VoutUvpTimes--;
            if(s16VoutUvpTimes < 1)
            {
                s16VoutUvpTimes = 1;
            }
        }

        if(s16VoutUvpTimes ==  VOUT_OVP_TIMES)
        {
            u32Faultflag  |= Fault_SW_UVP;
        }
    }
#endif

    //SW OCP check
    if(int32IoutRtdbQ11 > IOUT_OCP_VALUE)
    {
        s16VoutOcpTimes++;
        if(s16VoutOcpTimes >= IOUT_OCP_TIMES)
        {
            s16VoutOcpTimes = IOUT_OCP_TIMES;
        }
    }
    else if((int32IoutRtdbQ11 + 205) < IOUT_OCP_VALUE)
    {
        s16VoutOcpTimes--;
        if(s16VoutOcpTimes < 1)
        {
            s16VoutOcpTimes = 1;
        }
    }

    if(s16VoutOcpTimes ==  IOUT_OCP_TIMES)
    {
        u32Faultflag  |= Fault_SW_OCP;
    }

    //SW VIN OVP check
    if(int32VinRtdbQ11 > VIN_OVP_VALUE)
    {
        s16VinUvpTimes = 0;
        s16VinOvpRecoverTimes = 0;
        s16VinOvpTimes++;
        if(s16VinOvpTimes >= VIN_OVP_TIMES)
        {
            s16VinOvpTimes = VIN_OVP_TIMES;
        }
    }
    else if((int32VinRtdbQ11 < VIN_OVP_RECOVER_VALUE) && (int32VinRtdbQ11 > VIN_UVP_RECOVER_VALUE))
    {
        s16VinOvpTimes = 0;
        s16VinUvpTimes = 0;
        s16VinOvpRecoverTimes++;
        if(s16VinOvpRecoverTimes > VIN_OVP_RECOVER_TIMES)
        {
            s16VinOvpRecoverTimes = VIN_OVP_RECOVER_TIMES;
        }
    }
    else if(int32VinRtdbQ11 < VIN_UVP_RECOVER_VALUE)
    {
        s16VinOvpTimes = 0;
        s16VinOvpRecoverTimes = 0;
        s16VinUvpTimes++;
        if(s16VinUvpTimes >= VIN_UVP_TIMES)
        {
            s16VinUvpTimes = VIN_UVP_TIMES;
        }
    }

    if(s16VinOvpTimes ==  VIN_OVP_TIMES)
    {
        u32Faultflag  |= Fault_VIN_SW_OVP;
    }
    else if(s16VinUvpTimes ==  VIN_UVP_TIMES)
    {
        u32Faultflag  |= Fault_VIN_SW_UVP;
    }
    else if(s16VinOvpRecoverTimes == VIN_OVP_RECOVER_VALUE)
    {
        u32Faultflag  &= ~Fault_VIN_SW_OVP;
        u32Faultflag  &= ~Fault_VIN_SW_UVP;
    }

    //SW TEMP1 OTP
    if(int16Temp1ADCQ0 > TEMP1_OTP_VALUE)
    {
        s16Temp1OTPRecoverTimes = 0;
        s16Temp1OTPTimes++;
        if(s16Temp1OTPTimes > TEMP1_OTP_TIMES)
        {
            s16Temp1OTPTimes = TEMP1_OTP_TIMES;
        }
    }
    else if(int16Temp1ADCQ0 < TEMP1_OTP_RECOVER_VALUE)
    {
        s16Temp1OTPTimes = 0;
        s16Temp1OTPRecoverTimes++;
        if(s16Temp1OTPRecoverTimes > TEMP1_OTP_RECOVER_TIMES)
        {
            s16Temp1OTPRecoverTimes = TEMP1_OTP_RECOVER_TIMES;
        }
    }

    if(s16Temp1OTPTimes == TEMP1_OTP_TIMES)
    {
        u32Faultflag  |= Fault_TEMP1_OTP;
    }
    else if(s16Temp1OTPRecoverTimes == TEMP1_OTP_RECOVER_TIMES)
    {
        u32Faultflag &= ~Fault_TEMP1_OTP;
    }
    //SW TEMP2 OTP
    //SW TEMP1 OTP
    if(int16Temp2ADCQ0 > TEMP2_OTP_VALUE)
    {
        s16Temp2OTPRecoverTimes = 0;
        s16Temp2OTPTimes++;
        if(s16Temp2OTPTimes > TEMP2_OTP_TIMES)
        {
            s16Temp2OTPTimes = TEMP2_OTP_TIMES;
        }
    }
    else if(int16Temp2ADCQ0 < TEMP2_OTP_RECOVER_VALUE)
    {
        s16Temp2OTPTimes = 0;
        s16Temp2OTPRecoverTimes++;
        if(s16Temp2OTPRecoverTimes > TEMP2_OTP_RECOVER_TIMES)
        {
            s16Temp2OTPRecoverTimes = TEMP2_OTP_RECOVER_TIMES;
        }
    }

    if(s16Temp2OTPTimes == TEMP2_OTP_TIMES)
    {
        u32Faultflag  |= Fault_TEMP2_OTP;
    }
    else if(s16Temp2OTPRecoverTimes == TEMP2_OTP_RECOVER_TIMES)
    {
        u32Faultflag &= ~Fault_TEMP2_OTP;
    }
}


/*********************************************
* function    : ControllerInit()
* description : controller Init
*
**********************************************/
void ControllerInit(void)
{
	  uint8_t i = 0;
	   
	  for(i = 1;i < 128;i++)
	  {
				m_au8TxBuf[i] = ' ';	
	  } 
    //voltage loop
    VloopCtrl.int32A1 = ((int32_t)(1.0f * 256));
    VloopCtrl.int32A2 = ((int32_t)(0.0f * 256));
    VloopCtrl.int32B0 = ((int32_t)(40.0f * 256));//((int32_t)(45.0f * 8192));//((int32_t)(0.02934f * 16384));//0.18624f ((int32_t)(0.02328f * 16384));
    VloopCtrl.int32B1 = ((int32_t)(-36.0f * 256));//((int32_t)(-37.0f * 8192));//((int32_t)(-0.024f * 16384));//-0.0776f ((int32_t)(-0.0194f * 16384));
//    VloopCtrl.int32B0 = ((int32_t)(0.2934f * 16384));//0.18624f ((int32_t)(0.02328f * 16384));
//    VloopCtrl.int32B1 = ((int32_t)(-0.24f * 16384));//-0.0776f ((int32_t)(-0.0194f * 16384));
    VloopCtrl.int32B2 = ((int32_t)(0.0f * 256));

    VloopCtrl.int32X0  = 0;
    VloopCtrl.int32X1  = 0;
    VloopCtrl.int32X2  = 0;

    VloopCtrl.int32Y0  = 0;
    VloopCtrl.int32Y1  = 0;
    VloopCtrl.int32Y2  = 0;

    VloopCtrl.int32Max = VLOOP_CTRL_MAX;//
    VloopCtrl.int32Min = VLOOP_CTRL_MIN;


    //current loop
    CloopCtrl.int32A1 = ((int32_t)(1.0f * 1024));
    CloopCtrl.int32A2 = ((int32_t)(0.0f * 1024));
    CloopCtrl.int32B0 = ((int32_t)(2.65f * 1024));
    CloopCtrl.int32B1 = ((int32_t)(-2.35f * 1024));
    CloopCtrl.int32B2 = ((int32_t)(0.0f * 1024));

    CloopCtrl.int32X0  = 0;
    CloopCtrl.int32X1  = 0;
    CloopCtrl.int32X2  = 0;

    CloopCtrl.int32Y0  = 0;
    CloopCtrl.int32Y1  = 0;
    CloopCtrl.int32Y2  = 0;

    CloopCtrl.int32Max = ILOOP_CTRL_MAX;//
    CloopCtrl.int32Min = ILOOP_CTRL_MIN;

    //average current
    int32PriCurrErr = 0;       //resonant current error
    int32PriCurrCtrlOut = 0;   //

    int32CtrlOut = 0;
    int32EpwmPrdUpdate = 0;
}

/*********************************************
* function    : PowerOnConfig()
* description : Power on logic and configuration
*
*
*
**********************************************/
static inline void PowerOnConfig(void)
{
    //start check timers
    static uint16_t u16StartCnt  = 0;
    static uint16_t u16RelayDelayCnt = 0;
    //SysPowerONState = 1;
    switch(SysPowerONState)
    {

        case 0: //Vin check

         //GPIO_writePin(ILLLC_REALAY_ENABLE_IO, 0);
				 RELAY_DISABLE;
				
         u8WorkEnable = 0;

         if(int32VinRtdbQ11 > POWERON_ENABLE_VOLT)//120V
         {
             u16StartCnt++;
             if(u16StartCnt >= POWERON_CHECK_ENABLE_TIME_100MS)
             {
                 u16StartCnt = 0;
                 u16RelayDelayCnt = 0;
                 //GPIO_writePin(ILLLC_REALAY_ENABLE_IO, 1);
							   RELAY_ENABLE;
                 SysPowerONState = 1;
             }
         }
         else
         {
             u16StartCnt = 0;
         }

        break;

        case 1://relay delay

            //GPIO_writePin(ILLLC_REALAY_ENABLE_IO, 1);
				    RELAY_ENABLE;
            u8WorkEnable = 0;

            if(int32VinRtdbQ11 < POWERON_DISABLE_VOLT)//100V
            {
                u16StartCnt++;
                if(u16StartCnt >= POWERON_CHECK_DISABLE_TIME_20MS)
                {
                    u16StartCnt = 0;
                    SysPowerONState = 0;
									  RELAY_DISABLE;
                }
            }
            else
            {
                u16StartCnt = 0;
                u16RelayDelayCnt++;
                if(u16RelayDelayCnt > POWERON_DELAY_TIME_50MS)
                {
                    u16RelayDelayCnt = 0;
                    SysPowerONState = 2;
                }
            }

        break;

        case 2://Normal Run

            //GPIO_writePin(ILLLC_REALAY_ENABLE_IO, 1);
				    RELAY_ENABLE;
            u8WorkEnable = 1;

            if(int32VinRtdbQ11 < POWERON_DISABLE_VOLT)//100V
            {
                u16StartCnt++;
                if(u16StartCnt >= POWERON_CHECK_DISABLE_TIME_20MS)
                {
                    u16StartCnt = 0;
                    SysPowerONState = 0;
									  RELAY_DISABLE;
                }
            }

            break;

        default:

            u16StartCnt  = 0;
            u16RelayDelayCnt = 0;
            SysPowerONState = 0;

            break;
    }
}

/***********************************************************
 *
 ***********************************************************/
void LedDriverMang(void)
{
    static uint16_t  u16LedCnt = 0;
    static uint16_t  u16LedPrd = 100;
	  static uint16_t  u16FANCnt = 0;
	  static uint16_t  u16FANDuty_MAN = 10;
	  static int16_t   u16FinalTemp = 0;
	  static uint16_t  u16FanSpeedPre = 0;
	  uint16_t  u16FanSpeed = 0;
	  //static uint16_t  u16FANPrd = 10;
	
#if 1
	  
	
    if(u16SysSM == SYS_WORK_MODE)
    {
        //u16LedPrd = 400;
			  GPIO_TogglePins(LLC_HAL_LED2_Portx,LLC_HAL_LED2_PIN);
    }
    else if(u16SysSM == SYS_FAULT_MODE)
    {
        if(u32Faultflag  & Fault_HW_OVP)
        {
            u16LedPrd = 20;
        }
        else if(u32Faultflag  & Fault_HW_OCP)
        {
            u16LedPrd = 40;
        }
        else if(u32Faultflag  & Fault_PRICURR1_HW_OCP)
        {
            u16LedPrd = 60;
        }
        else if(u32Faultflag  & Fault_PRICURR2_HW_OCP)
        {
            u16LedPrd = 80;
        }
        if(u32Faultflag  & Fault_SW_OVP)
        {
            u16LedPrd = 200;
        }
        else if(u32Faultflag  & Fault_SW_OCP)
        {
            u16LedPrd = 220;
        }
        else if(u32Faultflag  & Fault_VIN_SW_OVP)
        {
            u16LedPrd = 240;
        }
        else if(u32Faultflag  & Fault_VIN_SW_UVP)
        {
            u16LedPrd = 260;
        }
				
				if(u16LedCnt++  >= u16LedPrd)
				{
						u16LedCnt = 0;
						GPIO_TogglePins(LLC_HAL_LED1_Portx,LLC_HAL_LED1_PIN);
				}
				GPIO_ResetPins(LLC_HAL_LED2_Portx,LLC_HAL_LED2_PIN);
    }
		else
		{
			 u16LedCnt = 0;
			 GPIO_SetPins(LLC_HAL_LED2_Portx,LLC_HAL_LED2_PIN);
			 GPIO_ResetPins(LLC_HAL_LED1_Portx,LLC_HAL_LED1_PIN);
		}


#else
//    if(int32PriCurrErr > 0)
//    {
//    	GPIO_writePin(33, 1);
//    }
//    else
//    {
//    	GPIO_writePin(33, 0);
//    }

#endif

     u16FANCnt++;
		 if(u16FANCnt >= u16FANPrd)
		 {
			 u16FANCnt = 0;
			 u16FANDuty_MAN = u16FanDuty;   //update duty
		 }
		 
//		 if(u16FANCnt < u16FANDuty_MAN)
//		 {
//			 GPIO_SetPins(GPIO_PORT_A, GPIO_PIN_01);
//		 }
//		 else 
//		 {
//			 GPIO_ResetPins(GPIO_PORT_A, GPIO_PIN_01);
//		 }
		 
		 //
		 u16FinalTemp = s16TempPhaseA;
     if(u16FinalTemp < s16TempPhaseB)
		 {
			 u16FinalTemp = s16TempPhaseB;
		 }
	 
		 if(u16FinalTemp < FAN_STOP_VALUE)
		 {
			  u16FanSpeed = (TMRA_PERIOD_VAL >> 2);  
		 }
		 else if(u16FinalTemp < FAN_SPEED1_VALUE)
		 {
			  u16FanSpeed = (TMRA_PERIOD_VAL >> 2); 
		 }
		 else if(u16FinalTemp < FAN_SPEED2_VALUE)
		 {
			  u16FanSpeed = (TMRA_PERIOD_VAL >> 1); 
		 }
		 else if(u16FinalTemp < FAN_SPEED3_VALUE)
		 {
			  u16FanSpeed = ((TMRA_PERIOD_VAL * 3) >> 2);  
		 }
		 else 
		 {
			 u16FanSpeed = TMRA_PERIOD_VAL - 100; 
		 }
		 
		 //u16FanSpeed = (TMRA_PERIOD_VAL >> 1);  
		 if(u16FanSpeed != u16FanSpeedPre)
		 {
				TMRA_SetCompareValue(TMRA_UNIT, TMRA_PWM_CH_FAN, u16FanSpeed);
			  u16FanSpeedPre = u16FanSpeed;
		 }
		 
//		 TMRA_SetCompareValue(TMRA_UNIT, TMRA_PWM_CH_FAN, u16FanSpeed);
}


void Inter2Asciix(uint8_t *txdatabuffer,uint32_t inputdata,uint8_t length)
{
	  uint32_t remaindata = 0; 
	  uint32_t inputdatatemp = inputdata;
	  if(length == 4)
		{
			remaindata = inputdatatemp / 1000;
			*(txdatabuffer + 0) = remaindata + '0';
			inputdatatemp = inputdatatemp - remaindata * 1000;
			
			remaindata = inputdatatemp / 100;
			*(txdatabuffer + 1) = remaindata + '0';
			inputdatatemp = inputdatatemp - remaindata * 100;
			
			remaindata = inputdatatemp / 10;
			*(txdatabuffer + 2) = remaindata + '0';
			inputdatatemp = inputdatatemp - remaindata * 10;
			
			*(txdatabuffer + 3) = inputdatatemp + '0';
		}
		else if(length == 5)
		{
			remaindata = inputdatatemp / 10000;
			*(txdatabuffer + 0) = remaindata + '0';
			inputdatatemp = inputdatatemp - remaindata * 10000;
			
			remaindata = inputdatatemp / 1000;
			*(txdatabuffer + 1) = remaindata + '0';
			inputdatatemp = inputdatatemp - remaindata * 1000;
			
			remaindata = inputdatatemp / 100;
			*(txdatabuffer + 2) = remaindata + '0';
			inputdatatemp = inputdatatemp - remaindata * 100;
			
			remaindata = inputdatatemp / 10;
			*(txdatabuffer + 3) = remaindata + '0';
			inputdatatemp = inputdatatemp - remaindata * 10;
			
			*(txdatabuffer + 4) = inputdatatemp + '0';			
		}
}


int32_t Asciix2Inter(uint8_t *rxdatabuffer)
{
	  int32_t remaindata = 0; 
	  uint16_t indexcnt = 0;
	
	  for(indexcnt = 1;indexcnt < 8;indexcnt++)
	  {
		   remaindata += *(rxdatabuffer + indexcnt);
			 remaindata *= 10;
	  }
		
		if(*rxdatabuffer == '-')
		{
			remaindata = -remaindata;
		}
		 
		return (remaindata);
}

/***********************************************************
 *  LogoDataUpdate
 *  
 ***********************************************************/
void LogoDataUpdate(uint8_t *txdatabuffer)
{
	uint8_t *p = txdatabuffer;
	uint8_t u8Length = 1;
	//Vout
	//*(p + u8Length) = 0;
	Inter2Asciix((uint8_t *)(p + 1),int32VoutADCQ0,4);
	//Iout
//	*(p + 7) = 0;
	Inter2Asciix((uint8_t *)(p + 7),int32IoutCalibADCQ0,4);
	//Vin
	//*(p + 12) = 0;
	Inter2Asciix((uint8_t *)(p + 12),int32VinCalibADCQ0,4);
	//Primarycurrent1
	//*(p + 17) = 0;
	Inter2Asciix((uint8_t *)(p + 17),int32PriCurr1ADCQ0,4);
	//Primarycurrent2
	//*(p + 22) = 0;
	Inter2Asciix((uint8_t *)(p + 22),int32PriCurr2ADCQ0,4);
	//Fault
	//*(p + 27) = 0;
	//Inter2Asciix((uint8_t *)(p + 27),int32VinADCQ0,4);
	/**************************OVP*****************************/
	//HW OVP
	*(p + 27) = '0'; 
	if(u32Faultflag & Fault_HW_OVP)
	{
		*(p + 27) = '1'; 
	}
	//SW OVP
	*(p + 28) = '0'; 
	if(u32Faultflag & Fault_SW_OVP)
	{
		*(p + 28) = '1'; 
	}
	//CMPSS OVP
	*(p + 29) = '0'; 
	if(u32Faultflag & Fault_CMPSS_OVP)
	{
		*(p + 29) = '1'; 
	}
	/**************************UVP*****************************/
	//SW OVP
	*(p + 30) = '0'; 
	if(u32Faultflag & Fault_SW_UVP)
	{
		*(p + 30) = '1'; 
	}
	/**************************OCP*****************************/
	//HW OCP
	*(p + 31) = '0'; 
	if(u32Faultflag & Fault_HW_OCP)
	{
		*(p + 31) = '1'; 
	}
	//SW OCP
	*(p + 32) = '0'; 
	if(u32Faultflag & Fault_SW_OCP)
	{
		*(p + 32) = '1'; 
	}
	//CMPSS OCP
	*(p + 33) = '0'; 
	if(u32Faultflag & Fault_CMPSS_OCP)
	{
		*(p + 33) = '1'; 
	}
	
	/**************************PRIMOCP*****************************/
	//HW OCP
	*(p + 34) = '0'; 
	if(u32Faultflag & Fault_PRICURR1_HW_OCP)
	{
		*(p + 34) = '1'; 
	}
	//SW OCP
	*(p + 35) = '0'; 
	if(u32Faultflag & Fault_PRICURR1_SW_OCP)
	{
		*(p + 35) = '1'; 
	}
	//CMPSS OCP
	*(p + 36) = '0'; 
	if(u32Faultflag & Fault_PRICURR1_CMPSS_OCP)
	{
		*(p + 36) = '1'; 
	}
	
	/**************************PRIMOCP*****************************/
	//HW OCP
	*(p + 37) = '0'; 
	if(u32Faultflag & Fault_PRICURR2_HW_OCP)
	{
		*(p + 37) = '1'; 
	}
	//SW OCP
	*(p + 38) = '0'; 
	if(u32Faultflag & Fault_PRICURR2_SW_OCP)
	{
		*(p + 38) = '1'; 
	}
	//CMPSS OCP
	*(p + 39) = '0'; 
	if(u32Faultflag & Fault_PRICURR2_CMPSS_OCP)
	{
		*(p + 39) = '1'; 
	}
	
	/**************************Vin *****************************/
	//HW OCP
	*(p + 40) = '0'; 
	if(u32Faultflag & Fault_VIN_SW_OVP)
	{
		*(p + 40) = '1'; 
	}
	//SW OCP
	*(p + 41) = '0'; 
	if(u32Faultflag & Fault_VIN_SW_UVP)
	{
		*(p + 41) = '1'; 
	}
	
	*(p + 42) = '0'; 
	
	//Statemachine
	*(p + 44) = u16SysSM + '0';
	
	//Vset
	//*(p + 46) = 0;
	Inter2Asciix((uint8_t *)(p + 46),int32VoutSlewQ0,4);
  //Iset
	//*(p + 51) = 0;
	Inter2Asciix((uint8_t *)(p + 51),/*int32IoutSlewQ0*/(VloopCtrl.int32B0 >> 8),4);
	//Vloop
	//*(p + 56) = 0;
	Inter2Asciix((uint8_t *)(p + 56),VloopCtrl.int32Y0,5);
	//Iloop
	//*(p + 62) = 0;
	Inter2Asciix((uint8_t *)(p + 62),CloopCtrl.int32Y0,5);
	//Phase1
	//*(p + 68) = 0;
	Inter2Asciix((uint8_t *)(p + 68),int32P1Phase,5);
	//Phase2
	//*(p + 74) = 0;
	Inter2Asciix((uint8_t *)(p + 74),int32P2Phase,5);
	//SR
	//*(p + 80) = 0;
	Inter2Asciix((uint8_t *)(p + 80),s32SRDuty,5);
	//
}
/**
 * @}
 */

/*******************************************************************************
 * EOF (not truncated)
 ******************************************************************************/
