/*  -------------------------- (C) COPYRIGHT 2020 Fortiortech ShenZhen ---------------------------*/
/*  File Name      : Communication.c
    /*  Author         : Fortiortech  Appliction Team
    /*  Version        : V1.0
    /*  Date           : 2020-04-09
    /*  Description    : This file contains XX-XX-XX function used for Motor Control.
    /*  ----------------------------------------------------------------------------------------------*/
/*                                     All Rights Reserved
    /*  ----------------------------------------------------------------------------------------------*/




#include "FU68xx_5.h"
#include <Myproject.h>
#include <Communication.h>

/************** Private variables *********************/
//MCUART xdata ubf_status;
#if (Debugg==1)


void CMDMONI_WT_INIT(void);

static int zop_cmdcheck(void);
static int zop_dummy(void);
static int zop_cmd00(void);
static int zop_cmd01(void);
static int zop_cmd02(void);
static int zop_cmd03(void);
static int zop_cmd04(void);
static int zop_cmd05(void);
static int zop_cmd06(void);
static int zop_cmd07(void);
static int zop_cmd08(void);
static int zop_cmd09(void);
static int zop_cmd0A(void);

static unsigned char zop_CalcCheckSum(unsigned char * p, int cnt);
static void zop_ClearSendCmdBuffer(void);
static void zop_PressSend(int cnt);
static void zop_ComCmdCheck(int id, int ctp);
static void zop_SetGetTraceData(void);
static void zop_trig_check(int trig_ch, int trig_mode);
static int  zop_SetByteToInt(unsigned char * pByte);
static int cmdmoni_getdata_size(int * adrs, char size);

const uint16 LengthMask[4] = {127, 255, 511, 1023};



unsigned char snd_ringbuf[SZ_SENDRINGBUF] = 0; //SZ_SENDRINGBUF

int16 iRd = 0;
int16 iWr = 0;


unsigned char rcv_cmdbuf[SZ_RECVBUF] = 0; //SZ_RECVBUF
unsigned char snd_cmdbuf[SZ_SENDBUF] = 0; //SZ_SENDBUF

int buf_trace[SZ_TRACEBUF] = 0; //SZ_TRACEBUF

int idy_trace = 0;
int idy_copy_trace = 0;

int iTraceCount = 0;
uint16 iTraceCntMax = 0;
uint16 iIntervalCount = 0;
int16 iTraceID = 0;
uint8 iReceiveCount = 0;

int * ptr_adrs[CH_MAX] = 0; //CH_MAX
char adrs_size[CH_MAX] = 0; //CH_MAX

int iTrigLevel = 0;
int iTrigDelay = 0;
int itrig_cnt_delay = 0;
int itrig_cnt_wait = 0;
int itrig_data_old = 0;

union  UNION_STATUS xdata ubf_status;

uint16 cnt_wt_timeout = 0;
uint16 cnt_wt_timeout2 = 0; //

void MD_WT_INTRE( void )
{
    //  REG_RESET();// 处理各种错误，如：针错误，数据超时等等
}

void MD_WT_INTR( void )
{
    int iCmd = rcv_cmdbuf[0];
    rcv_cmdbuf[iReceiveCount] = UT_DR;
    iCmd = rcv_cmdbuf[0];
    iReceiveCount++;
    
    if (iReceiveCount >= SZ_RECVBUF)
    {
        iReceiveCount = 0;
        iRd = 0;
        iWr = 0;
    }
}

void senddeal(void)
{
    int iCmd = 0;
    iCmd = rcv_cmdbuf[0];
    
    switch (iReceiveCount)
    {
        case RCVCMD6:
            if (iCmd == 0x01)
            {
                zop_cmd01();
                iReceiveCount = 0;
                break;
            }
            
            if (iCmd == 0x05)
            {
                zop_cmd05();
                iReceiveCount = 0;
                break;
            }
            
            if (iCmd == 0x07)
            {
                zop_cmd07();
                iReceiveCount = 0;
                break;
            }
            
            if (iCmd == 0x0A)
            {
                zop_cmd0A();
                iReceiveCount = 0;
                break;
            }
            
            break;
            
        case RCVCMD9:
            if (iCmd == 0x03)
            {
                zop_cmd03();
                iReceiveCount = 0;
                break;
            }
            
            if (iCmd == 0x09)
            {
                zop_cmd09();
                iReceiveCount = 0;
                break;
            }
            
            break;
            
        case RCVCMD11:
            if (iCmd == 0x06)
            {
                zop_cmd06();
                iReceiveCount = 0;
            }
            
            break;
            
        case RCVCMD14:
            if (iCmd == 0x04)
            {
                zop_cmd04();
                iReceiveCount = 0;
            }
            
            break;
            
        case RCVCMD16:
            if (iCmd == 0x08)
            {
                zop_cmd08();
                iReceiveCount = 0;
            }
            
            break;
    }
    
    return;
}

static void CMDMONI_WT_TRACEHALT(void)
{
    if (ubf_status.bf.halt_trace == 0)
    {
        ubf_status.bf.halt_trace = 1;
        idy_copy_trace = idy_trace;
    }
}

static int cmdmoni_getdata_size(int * adrs, char size)
{
    switch (size)
    {
        case 1:
        {
            char * ptr = (char *)(adrs);
            return ((int)(*ptr));
        }
        break;
        
        case 2:
        {
            short * ptr = (short *)(adrs);
            return ((int)(*ptr));
        }
        break;
        
        case 4:
            return ((int)(*adrs));
            break;
            
        default:
            return ((int)(*adrs));
            break;
    }
}
int16  xdata GPFCON1  _at_ 0x000C2; //IA
int16  xdata GPFCON2  _at_ 0x000C4; //IB
int16  xdata GPFCON3  _at_ 0x000C6; //UD
int16  xdata GPFCON4  _at_ 0x000C8; //UQ
int16  xdata GPFCON5  _at_ 0x000CA; //角度
int16  xdata GPFCON6  _at_ 0x000A2; //母线电压
int16  xdata GPFCON7  _at_ 0x000A4; //给定转速
int16  xdata GPFCON8  _at_ 0x000A6; //反馈转速
int16  xdata GPFCON9  _at_ 0x000A8; //ipm温度
int16  xdata GPFCON10 _at_ 0x000AA; //开关机转太
/*根据所获得的地址设至相应的发送数据*/
void CMDMONI_WT_TRACEWATCH(void)
{
    int msk = 0;
    int iBase = 0;
    int itrig_data_new;
    {
        if (iIntervalCount < ubf_status.bf.interval)
        {
            iIntervalCount++;
            return;
        }
        else
        {
            iIntervalCount = 0;
        }
    }
    
    switch (adrs_size[ubf_status.bf.ch_trig])
    {
        case 1:
        {
            char * ptr = (char *)(ptr_adrs[ubf_status.bf.ch_trig]);
            itrig_data_new = (int)(*ptr);
        }
        break;
        
        case 2:
        {
            short * ptr = (short *)(ptr_adrs[ubf_status.bf.ch_trig]);
            itrig_data_new = (int)(*ptr);
        }
        break;
        
        case 4:
            itrig_data_new = (int)(*ptr_adrs[ubf_status.bf.ch_trig]);
            break;
    }
    
    if (ubf_status.bf.halt_trace == 0)
    {
        msk = LengthMask[(ubf_status.bf.len_trace & 0x03)];
        
        switch (ubf_status.bf.ch_trace)
        {
            case 0:
                iBase = idy_trace;
                #if BIGENDIAN
                buf_trace[iBase + 0] = cmdmoni_getdata_size(ptr_adrs[0], adrs_size[0]);
                #else
                buf_trace[iBase + 0] = 25122; //*(  int16 xdata *)(ptr_adrs[0]); //*ptr_adrs[0];//*ptr_adrs[0]:所放数据的地址
                #endif
                idy_trace++;
                idy_trace &= msk;
                break;
                
            case 1:
                iBase = idy_trace + idy_trace;
                #if BIGENDIAN
                buf_trace[iBase + 0] = cmdmoni_getdata_size(ptr_adrs[0], adrs_size[0]);
                buf_trace[iBase + 1] = cmdmoni_getdata_size(ptr_adrs[1], adrs_size[1]);
                #else
                //            GPFCON1=FOC__IA;
                //            GPFCON2=FOC__IB;
                buf_trace[iBase + 0] = *((  int16 xdata *)ptr_adrs[0]);//*((  uint8 xdata *)0x000C2);//*(  int16 xdata *)(ptr_adrs[0]); //*ptr_adrs[0];
                buf_trace[iBase + 1] = *((  int16 xdata *)ptr_adrs[1]);//*((  int16 xdata *)0x000A2);//(  int16 xdata *)ojhgt;//*(  int16 xdata *)(ptr_adrs[1]);//*ptr_adrs[1];
                #endif
                idy_trace++;
                idy_trace &= msk;
                break;
                
            case 2:
                iBase = idy_trace + idy_trace + idy_trace;
                #if BIGENDIAN
                buf_trace[iBase + 0] = cmdmoni_getdata_size(ptr_adrs[0], adrs_size[0]);
                buf_trace[iBase + 1] = cmdmoni_getdata_size(ptr_adrs[1], adrs_size[1]);
                buf_trace[iBase + 2] = cmdmoni_getdata_size(ptr_adrs[2], adrs_size[2]);
                #else
                buf_trace[iBase + 0] = FOC__IA;//*(  int16 xdata *)(ptr_adrs[0]);//*ptr_adrs[0];
                buf_trace[iBase + 1] = FOC__IB;//*(  int16 xdata *)(ptr_adrs[1]);//*ptr_adrs[1];
                buf_trace[iBase + 2] = FOC__IC;//*(  int16 xdata *)(ptr_adrs[2]);//*ptr_adrs[2];
                #endif
                idy_trace++;
                idy_trace &= msk;
                break;
                
            case 3:
                iBase = idy_trace + idy_trace + idy_trace + idy_trace;
                #if BIGENDIAN
                buf_trace[iBase + 0] = cmdmoni_getdata_size(ptr_adrs[0], adrs_size[0]);
                buf_trace[iBase + 1] = cmdmoni_getdata_size(ptr_adrs[1], adrs_size[1]);
                buf_trace[iBase + 2] = cmdmoni_getdata_size(ptr_adrs[2], adrs_size[2]);
                buf_trace[iBase + 3] = cmdmoni_getdata_size(ptr_adrs[3], adrs_size[3]);
                #else
                buf_trace[iBase + 0] = *(  int16 xdata *)(ptr_adrs[0]);//*ptr_adrs[0];
                buf_trace[iBase + 1] = *(  int16 xdata *)(ptr_adrs[1]);//*ptr_adrs[1];
                buf_trace[iBase + 2] = *(  int16 xdata *)(ptr_adrs[2]); //*ptr_adrs[2];
                buf_trace[iBase + 3] = *(  int16 xdata *)(ptr_adrs[3]);//*ptr_adrs[3];
                #endif
                idy_trace++;
                idy_trace &= msk;
                break;
        }
        
        itrig_cnt_wait++;
        
        if (itrig_cnt_wait > (msk * 2))
        {
            itrig_cnt_wait = msk * 2;
            
            if ((0 == ubf_status.bf.triggered) && (0 != ubf_status.bf.trig_en))
            {
                switch (ubf_status.bf.type_trig)
                {
                    case 0: // rise
                        if ((itrig_data_new >= iTrigLevel) && (itrig_data_old < iTrigLevel))
                        {
                            ubf_status.bf.triggered = 1;
                        }
                        
                        break;
                        
                    case 1: // fall
                        if ((itrig_data_new <= iTrigLevel) && (itrig_data_old > iTrigLevel))
                        {
                            ubf_status.bf.triggered = 1;
                        }
                        
                        break;
                        
                    case 2: // rise & fall
                        if (itrig_data_new >= iTrigLevel)
                        {
                            if (itrig_data_old < iTrigLevel)
                            {
                                ubf_status.bf.triggered = 1;
                            }
                        }
                        else
                        {
                            if (itrig_data_old > iTrigLevel)
                            {
                                ubf_status.bf.triggered = 1;
                            }
                        }
                        
                        break;
                        
                    default: // no
                        break;
                }
            }
        }
        
        if (0 != ubf_status.bf.triggered)
        {
            itrig_cnt_delay++;
            
            if (itrig_cnt_delay >= iTrigDelay)
            {
                CMDMONI_WT_TRACEHALT();
                return;
            }
        }
    }
    else
    {
        ubf_status.bf.triggered = 0;
        itrig_cnt_delay = 0;
        itrig_cnt_wait = 0;
        
        if (0 == ubf_status.bf.trig_normal)
        {
            ubf_status.bf.trig_en = 0;
        }
    }
    
    itrig_data_old = itrig_data_new;
}

void CMDMONI_WT_1MS(void)
{
    if (iReceiveCount > 0)
    {
        cnt_wt_timeout++;
    }
    else
    {
        cnt_wt_timeout = 0;
    }
}

static void zop_ComCmdCheck(int id, int ctp)
{
    snd_cmdbuf[0] = 0x8A;
    snd_cmdbuf[1] = 0x04;
    snd_cmdbuf[2] = id;
    snd_cmdbuf[3] = 0x00;
    snd_cmdbuf[4] = ctp;
    snd_cmdbuf[5] = zop_CalcCheckSum(snd_cmdbuf, 5);
    zop_PressSend(6);
}
static int zop_SetByteToInt(unsigned char * pByte)
{
    union UNION_INT iDATA;
    #if BIGENDIAN
    iDATA.b[0] = *(pByte + 3);
    iDATA.b[1] = *(pByte + 2);
    iDATA.b[2] = *(pByte + 1);
    iDATA.b[3] = *(pByte + 0);
    #else
    iDATA.b[0] = *(pByte + 0);
    iDATA.b[1] = *(pByte + 1);
    iDATA.b[2] = *(pByte + 2);
    iDATA.b[3] = *(pByte + 3);
    #endif
    return iDATA.i;
}
// Hello
// RE[6]: 01H,04H,id,00H,00H,CS
// TX[8]: 81H,06H,id,STATUS(4),CS
static int zop_cmd01(void)
{
    uint8 cnt = RCVCMD6 - 1;
    unsigned char sum = zop_CalcCheckSum(rcv_cmdbuf, cnt);
    
    if (rcv_cmdbuf[cnt] != sum)
    {
        return 0;
    }
    
    snd_cmdbuf[0] = 0x81;
    snd_cmdbuf[1] = 0x06;
    snd_cmdbuf[2] = rcv_cmdbuf[2];
    #if BIGENDIAN
    snd_cmdbuf[3] = ubf_status.b[0];
    snd_cmdbuf[4] = ubf_status.b[1];
    snd_cmdbuf[6] = ubf_status.b[2];
    snd_cmdbuf[5] = ubf_status.b[3];
    #else
    snd_cmdbuf[3] = ubf_status.b[3];
    snd_cmdbuf[4] = ubf_status.b[2];
    snd_cmdbuf[6] = ubf_status.b[1];
    snd_cmdbuf[5] = ubf_status.b[0];
    #endif
    snd_cmdbuf[7] = zop_CalcCheckSum(snd_cmdbuf, 7);
    zop_PressSend(8);
    return 1;
}

static int zop_cmd02(void)
{
    return 0;
}

// Read
// RE[9]:  03H,07H,id,00H,ADRESS(4),CS
// TX[13]: 83H,0BH,id,00H,ADRESS(4),DATA(4),CS
static int zop_cmd03(void)
{
    union UNION_DWORD xdata dwAdrs;
    union UNION_INT   xdata iDATA;
    union UNION_INT   xdata iDATA2;
    uint8 cnt = RCVCMD9 - 1;
    unsigned char sum = zop_CalcCheckSum(rcv_cmdbuf, cnt);
    
    if (rcv_cmdbuf[cnt] != sum)
    {
        return 0;
    }
    
    dwAdrs.dw = zop_SetByteToInt(&rcv_cmdbuf[4]);
    
    if (dwAdrs.dw == 0)
    {
        dwAdrs.dw = DEF_TRACEADDR;
    }
    
    #if BIGENDIAN
    
    switch (rcv_cmdbuf[3])
    {
        case 1:
        {
            char * ptr;
            ptr = (char *)(dwAdrs.dw);
            iDATA.b[0] = 0;
            iDATA.b[1] = 0;
            iDATA.b[2] = 0;
            iDATA.b[3] = *ptr++;
        }
        break;
        
        case 2:
        {
            char * ptr;
            ptr = (char *)(dwAdrs.dw);
            iDATA.b[0] = 0;
            iDATA.b[1] = 0;
            iDATA.b[2] = *ptr++;
            iDATA.b[3] = *ptr++;
        }
        break;
        
        default:
        {
            char * ptr;
            ptr = (char *)(dwAdrs.dw);
            iDATA.b[0] = *ptr++;
            iDATA.b[1] = *ptr++;
            iDATA.b[2] = *ptr++;
            iDATA.b[3] = *ptr++;
        }
        break;
    }
    
    #else
    iDATA.i = *((  int16 xdata *)dwAdrs.dw);;//*(int *)(0x40c6 );
    #endif
    snd_cmdbuf[0] = 0x83;
    snd_cmdbuf[1] = 0x0B;
    snd_cmdbuf[2] = rcv_cmdbuf[2];
    snd_cmdbuf[3] = 0x00;
    #if BIGENDIAN
    snd_cmdbuf[4] = dwAdrs.b[0];
    snd_cmdbuf[5] = dwAdrs.b[1];
    snd_cmdbuf[6] = dwAdrs.b[2];
    snd_cmdbuf[7] = dwAdrs.b[3];
    snd_cmdbuf[8]  = iDATA.b[0];
    snd_cmdbuf[9]  = iDATA.b[1];
    snd_cmdbuf[10] = iDATA.b[2];
    snd_cmdbuf[11] = iDATA.b[3];
    #else
    snd_cmdbuf[4] = dwAdrs.b[3];
    snd_cmdbuf[5] = dwAdrs.b[2];
    snd_cmdbuf[6] = dwAdrs.b[1];
    snd_cmdbuf[7] = dwAdrs.b[0];
    snd_cmdbuf[8]  = iDATA.b[0];
    snd_cmdbuf[9]  = iDATA.b[1];
    snd_cmdbuf[10] = iDATA.b[2];
    snd_cmdbuf[11] = iDATA.b[3];
    #endif
    snd_cmdbuf[12] = zop_CalcCheckSum(snd_cmdbuf, 12);
    zop_PressSend(13);
    return 3;
}

// Write
// RE[14]: 04H,0CH,id,00H,DATASIZE(1)ADRESS(4),DATA(4),CS
// TX[13]: 84H,0BH,id,00H,ADRESS(4),DATA(4),CS
static int zop_cmd04(void)
{
    union UNION_DWORD xdata dwADRS;
    union UNION_INT   xdata iDATA;
    uint8 cnt = RCVCMD14 - 1;
    unsigned char sum = zop_CalcCheckSum(rcv_cmdbuf, cnt);
    
    if (rcv_cmdbuf[cnt] != sum)
    {
        return 0;
    }
    
    dwADRS.dw = zop_SetByteToInt(&rcv_cmdbuf[5]);
    
    if (dwADRS.dw == 0)
    {
        dwADRS.dw = DEF_TRACEADDR;
    }
    
    iDATA.i = zop_SetByteToInt(&rcv_cmdbuf[9]);
    
    switch (rcv_cmdbuf[4])
    {
        case 1:
            #if BIGENDIAN
            *(char *)(dwADRS.dw) = iDATA.i;
            #else
            dwADRS.b[3] = 0;
            dwADRS.b[2] = 0;
            dwADRS.b[1] = 0;
            dwADRS.b[0] = iDATA.b[0];
            //          *(char *)(dwADRS.dw) = iDATA.b[0];
            #endif
            break;
            
        case 2:
            #if BIGENDIAN
            *(short *)(dwADRS.dw) = iDATA.i;
            #else
            {
                union UNION_SHORT sSHORT;
                ////                iDATA.b[3] = 0;
                ////                iDATA.b[2] = 0;
                sSHORT.b[1] = iDATA.b[3];
                sSHORT.b[0] = iDATA.b[2];
                *((  int16 xdata *)dwADRS.dw) = sSHORT.s;
            }
            #endif
            break;
            
        case 4:
            *(int *)(dwADRS.dw) = iDATA.i;
            break;
    }
    
    snd_cmdbuf[0] = 0x84;
    snd_cmdbuf[1] = 0x0B;
    snd_cmdbuf[2] = rcv_cmdbuf[2];
    snd_cmdbuf[3] = 0x00;
    #if BIGENDIAN
    snd_cmdbuf[4] = dwADRS.b[0];
    snd_cmdbuf[5] = dwADRS.b[1];
    snd_cmdbuf[6] = dwADRS.b[2];
    snd_cmdbuf[7] = dwADRS.b[3];
    snd_cmdbuf[8]  = iDATA.b[0];
    snd_cmdbuf[9]  = iDATA.b[1];
    snd_cmdbuf[10] = iDATA.b[2];
    snd_cmdbuf[11] = iDATA.b[3];
    #else
    snd_cmdbuf[4] = dwADRS.b[0];
    snd_cmdbuf[5] = dwADRS.b[1];
    snd_cmdbuf[6] = dwADRS.b[2];
    snd_cmdbuf[7] = dwADRS.b[3];
    snd_cmdbuf[8]  = iDATA.b[0];
    snd_cmdbuf[9]  = iDATA.b[1];
    snd_cmdbuf[10] = iDATA.b[2];
    snd_cmdbuf[11] = iDATA.b[3];
    #endif
    snd_cmdbuf[12] = zop_CalcCheckSum(snd_cmdbuf, 12);
    zop_PressSend(13);
    return 4;
}

// GetTrace
// RE[6]:     05H,04H,id,00H,00H,CS
// TXHEAD[7]: 85H,05H,id,00H,Halt(2),cs   //Halt(2)通道
// TXBODY[?]: 85H,??H,id,FFH,NUM(2),{DATA(4) * (CHANNEL)},CS//数据
static int zop_cmd05(void)
{
    uint8 cnt = RCVCMD6 - 1;
    union UNION_WORD xdata wd;
    unsigned char sum = zop_CalcCheckSum(rcv_cmdbuf, cnt);
    
    if (rcv_cmdbuf[cnt] != sum)
    {
        return 0;
    }
    
    if (ubf_status.bf.cmd_gettrace)
    {
        return 0;
    }
    
    CMDMONI_WT_TRACEHALT();
    iTraceID = rcv_cmdbuf[2];
    iTraceCount = 0;
    iTraceCntMax = (0x80 << ubf_status.bf.len_trace);
    ubf_status.bf.cmd_gettrace = 1;
    wd.w = idy_copy_trace;
    snd_cmdbuf[0] = 0x85;
    snd_cmdbuf[1] = 0x05;
    snd_cmdbuf[2] = iTraceID;
    snd_cmdbuf[3] = 0x00;
    #if BIGENDIAN
    snd_cmdbuf[4] = wd.b[0];
    snd_cmdbuf[5] = wd.b[1];
    #else
    snd_cmdbuf[5] = wd.b[1];
    snd_cmdbuf[4] = wd.b[0];
    #endif
    snd_cmdbuf[6] = zop_CalcCheckSum(snd_cmdbuf, 6);
    zop_PressSend(7);
    return 5;
}

//int pokujg=0;
// SetTraceAddress
// RE[11]: 06H,09H,id,00H,CHANNEL(1),DATASIZE(1),ADRESS(4),CS
// TX[6]:  86H,04H,id,00H,00H,CS
static int zop_cmd06(void)
{
    uint8 cnt = RCVCMD11 - 1;
    unsigned char sum = zop_CalcCheckSum(rcv_cmdbuf, cnt);
    
    if (rcv_cmdbuf[cnt] != sum)
    {
        return 0;
    }
    
    {
        int iCH = (rcv_cmdbuf[4] & 0x03);
        int dwAdrs = zop_SetByteToInt(&rcv_cmdbuf[6]);
        
        if (dwAdrs == 0)
        {
            dwAdrs = DEF_TRACEADDR;
        }
        
        ptr_adrs[iCH] = (int *)dwAdrs;
        adrs_size[iCH] = rcv_cmdbuf[5];
    }
    
    //pokujg=ptr_adrs[0];
    snd_cmdbuf[0] = 0x86;
    snd_cmdbuf[1] = 0x04;
    snd_cmdbuf[2] = rcv_cmdbuf[2];
    snd_cmdbuf[3] = 0x00;
    snd_cmdbuf[4] = 0x00;
    snd_cmdbuf[5] = zop_CalcCheckSum(snd_cmdbuf, 5);
    zop_PressSend(6);
    return 6;
}


// TraceHALT_Clear
// RE[6]: 07H,04H,id,00H,00H,CS
// TX[6]: 87H,04H,id,00H,00H,CS
static int zop_cmd07(void)
{
    uint8 cnt = RCVCMD6 - 1;
    unsigned char sum = zop_CalcCheckSum(rcv_cmdbuf, cnt);
    
    if (rcv_cmdbuf[cnt] != sum)
    {
        return 0;
    }
    
    snd_cmdbuf[0] = 0x87;
    snd_cmdbuf[1] = 0x04;
    snd_cmdbuf[2] = rcv_cmdbuf[2];
    snd_cmdbuf[3] = 0x00;
    snd_cmdbuf[4] = 0x00;
    snd_cmdbuf[5] = zop_CalcCheckSum(snd_cmdbuf, 5);
    zop_PressSend(6);
    return 7;
}

// SetTriggerEnvironment
// RE[16]: 08H,0EH,id,00H,TRIG CHANNEL(1),TRIG MODE(1),TRIG TYPE(1),UP LEVEL(4),LO LEVEL(4),CS
// TX[6]:  88H,04H,id,00H,00H,CS
static int zop_cmd08(void)
{
    int16 TG_ch   = (rcv_cmdbuf[4] & 0x03);
    int16 TG_mode = (rcv_cmdbuf[5] & 0x01);
    int16 TG_type = (rcv_cmdbuf[6] & 0x07);
    int LV_up   = zop_SetByteToInt(&rcv_cmdbuf[7]);
    int LV_lo   = zop_SetByteToInt(&rcv_cmdbuf[11]);
    int cnt = RCVCMD16 - 1;
    unsigned char sum = zop_CalcCheckSum(rcv_cmdbuf, cnt);
    
    if (rcv_cmdbuf[cnt] != sum)
    {
        return 0;
    }
    
    ubf_status.bf.trig_normal = 0;
    
    if (TG_mode)
    {
        ubf_status.bf.trig_normal = 1;
    }
    
    ubf_status.bf.ch_trig = TG_ch;
    
    if (TG_ch > ubf_status.bf.ch_trace)
    {
        TG_ch = ubf_status.bf.ch_trace;
    }
    
    ubf_status.bf.type_trig = TG_type;
    iTrigLevel = LV_up;
    iTrigDelay = LV_lo;
    snd_cmdbuf[0] = 0x88;
    snd_cmdbuf[1] = 0x04;
    snd_cmdbuf[2] = rcv_cmdbuf[2];
    snd_cmdbuf[3] = 0x00;
    snd_cmdbuf[4] = 0x00;
    snd_cmdbuf[5] = zop_CalcCheckSum(snd_cmdbuf, 5);
    zop_PressSend(6);
    ubf_status.bf.trig_en = 1;
    return 8;
}

// SetTraceEnvironment
// RE[9]: 09H,07H,id,00H,DATA LENGTH(1),CHANEEL NUM(1),INTERVEL(2),CS
// TX[6]: 89H,04H,id,00H,00H,CS
static int zop_cmd09(void)
{
    union UNION_WORD uword;
    uint8 cnt = RCVCMD9 - 1;
    unsigned char sum = zop_CalcCheckSum(rcv_cmdbuf, cnt);
    
    if (rcv_cmdbuf[cnt] != sum)
    {
        return 0;
    }
    
    #if BIGENDIAN
    uword.b[1] = rcv_cmdbuf[6];
    uword.b[0] = rcv_cmdbuf[7];
    #else
    uword.b[0] = rcv_cmdbuf[6];
    uword.b[1] = rcv_cmdbuf[7];
    #endif
    ubf_status.bf.interval  = uword.w;
    snd_cmdbuf[0] = 0x89;
    snd_cmdbuf[1] = 0x04;
    snd_cmdbuf[2] = rcv_cmdbuf[2];
    snd_cmdbuf[3] = 0x00;
    snd_cmdbuf[4] = 0x00;
    snd_cmdbuf[5] = zop_CalcCheckSum(snd_cmdbuf, 5);
    zop_PressSend(6);
    return 9;
}


// RE[6]: 0AH,04H,id,00H,CHECK TYPE(1),CS
// TX[6]: 8AH,04H,id,00H,CHECK TYPE(1),CS
static int zop_cmd0A(void)
{
    uint8 cnt = RCVCMD6 - 1;
    int16 iChkType = rcv_cmdbuf[4];
    unsigned char sum = zop_CalcCheckSum(rcv_cmdbuf, cnt);
    
    if (rcv_cmdbuf[cnt] != sum)
    {
        return 0;
    }
    
    // UserTraceHalt
    if (iChkType == 0)
    {
        CMDMONI_WT_TRACEHALT();
    }
    
    // UserTraceHalt Clear
    if (iChkType == 1)
    {
        ubf_status.bf.halt_trace = 0;
    }
    
    zop_ComCmdCheck((int)rcv_cmdbuf[2], (int)rcv_cmdbuf[4]);
    return 10;
}

/*TRACE 发送数据的处理*/
void CMDMONI_WT_CHECKCMD(void)
{
    static int8 uiui = 0;
    
    if (! ubf_status.bf.cmd_gettrace)
    {
        return;
    }
    
    {
        int s = iRd - iWr;
        
        if (s <= 0)
        {
            s += SZ_SENDRINGBUF;
        }
        
        if (s < 64)
        {
            return;
        }
    }
    
    //  if(uiui<250)
    {
        //      uiui++;
        zop_SetGetTraceData();
        iTraceCount++;
        
        if (iTraceCount >= iTraceCntMax)
        {
            ubf_status.bf.halt_trace = 0;
            ubf_status.bf.cmd_gettrace = 0;
        }
    }
}
uint8 txbuff = 0;
/*一般数据发送的处理*/
void CMDMONI_WT_SENDCMD(void)
{
    if ((cnt_wt_timeout > 100) || (cnt_wt_timeout2 > 100))
    {
        CMDMONI_WT_INIT();
        cnt_wt_timeout = 0;
        cnt_wt_timeout2 = 0;
        return;
    }
    
    if ((iRd != iWr) && (TI == 0))
    {
        txbuff = snd_ringbuf[iRd];//while (TI==0){};TI=0;
        UT_DR =  txbuff;
        iRd++;
        
        if (iRd >= SZ_SENDRINGBUF) //
        { iRd = 0; }
        
        cnt_wt_timeout2 = 0;
    }
}
void CMDMONI_WT_INIT(void)
{
    iReceiveCount = 0;
    iRd = 0;
    iWr = 0;
    idy_trace = 0;
    idy_copy_trace = 0;
    iTraceCount = 0;
    iTraceCntMax = 0;
    iIntervalCount = 0;
    iTraceID = 0;
    {
        uint8 i;
        
        for (i = 0; i < CH_MAX; i++)
        {
            ptr_adrs[i] = (int *)DEF_TRACEADDR;
            adrs_size[i] = 4;
        }
    }
    ubf_status.ui = 0;
    ubf_status.bf.trig_support = 1;
    ubf_status.bf.ch_trace  = CH_MAX - 1;
    ubf_status.bf.len_trace = LEN_TRACE;
}

static unsigned char zop_CalcCheckSum(unsigned char * p, int cnt)
{
    uint8 i;
    int sum = 0;
    
    for (i = 0; i < cnt; i++)
    {
        sum += *p++;
    }
    
    sum = ~sum + 1;
    sum = sum & 0x0FF;
    return sum;
}
//uint8 faflag=0;
static void zop_PressSend(int cnt)
{
    uint8 i;
    
    for (i = 0; i < cnt; i++)
    {
        snd_ringbuf[iWr] = snd_cmdbuf[i];
        iWr++;
        
        if (iWr >= SZ_SENDRINGBUF)
        { iWr = 0; }
    }
    
    //  faflag=1;
}

// GetTrace
// TX HEAD[7]: 85H,05H,id,00H,Halt?C???f?N?X(2),cs
// TX BODY[?]: 85H,??H,id,FFH,NUM(2),{DATA(4) * (CHANNEL NUM)},CS

static void zop_SetGetTraceData(void)
{
    int msk = LengthMask[(ubf_status.bf.len_trace & 0x03)];
    union UNION_WORD xdata wd;
    union UNION_INT  xdata ui;
    int iDATA0 = 0;
    int iDATA1 = 0;
    int iDATA2 = 0;
    int iDATA3 = 0;
    wd.w = iTraceCount;
    snd_cmdbuf[0] = 0x85;
    snd_cmdbuf[1] = 0x00;
    snd_cmdbuf[2] = iTraceID;
    snd_cmdbuf[3] = 0xFF;
    #if BIGENDIAN
    snd_cmdbuf[5] = wd.b[0];
    snd_cmdbuf[4] = wd.b[1];
    #else
    snd_cmdbuf[5] = wd.b[1];
    snd_cmdbuf[4] = wd.b[0];
    #endif
    
    switch (ubf_status.bf.ch_trace)
    {
        case 0:
            snd_cmdbuf[1] = 0x09;
            {
                int iBase = idy_copy_trace;
                iDATA0 = buf_trace[iBase + 0];
                idy_copy_trace++;
                idy_copy_trace &= msk;
            }
            ui.i = iDATA0;
            #if BIGENDIAN
            snd_cmdbuf[9] = ui.b[0];
            snd_cmdbuf[8] = ui.b[1];
            snd_cmdbuf[7] = ui.b[2];
            snd_cmdbuf[6] = ui.b[3];
            #else
            snd_cmdbuf[9] = ui.b[3];
            snd_cmdbuf[8] = ui.b[2];
            snd_cmdbuf[7] = ui.b[1];
            snd_cmdbuf[6] = ui.b[0];
            #endif
            snd_cmdbuf[10] = zop_CalcCheckSum(snd_cmdbuf, 10);
            zop_PressSend(11);
            break;
            
        case 1:
            snd_cmdbuf[1] = 0x0D;
            {
                int iBase = (idy_copy_trace + idy_copy_trace);
                iDATA0 = buf_trace[iBase + 0];
                iDATA1 = buf_trace[iBase + 1];
                idy_copy_trace++;
                idy_copy_trace &= msk;
            }
            ui.i = iDATA0;
            #if BIGENDIAN
            snd_cmdbuf[9] = ui.b[0];
            snd_cmdbuf[8] = ui.b[1];
            snd_cmdbuf[7] = ui.b[2];
            snd_cmdbuf[6] = ui.b[3];
            #else
            snd_cmdbuf[9] = ui.b[3];
            snd_cmdbuf[8] = ui.b[2];
            snd_cmdbuf[7] = ui.b[1];
            snd_cmdbuf[6] = ui.b[0];
            #endif
            ui.i = iDATA1;
            #if BIGENDIAN
            snd_cmdbuf[13] = ui.b[0];
            snd_cmdbuf[12] = ui.b[1];
            snd_cmdbuf[11] = ui.b[2];
            snd_cmdbuf[10] = ui.b[3];
            #else
            snd_cmdbuf[13] = ui.b[3];
            snd_cmdbuf[12] = ui.b[2];
            snd_cmdbuf[11] = ui.b[1];
            snd_cmdbuf[10] = ui.b[0];
            #endif
            snd_cmdbuf[14] = zop_CalcCheckSum(snd_cmdbuf, 14);
            zop_PressSend(15);
            break;
            
        case 2:
            snd_cmdbuf[1] = 0x11;
            {
                int iBase = (idy_copy_trace + idy_copy_trace + idy_copy_trace);
                iDATA0 = buf_trace[iBase + 0];
                iDATA1 = buf_trace[iBase + 1];
                iDATA2 = buf_trace[iBase + 2];
                idy_copy_trace++;
                idy_copy_trace &= msk;
            }
            ui.i = iDATA0;
            #if BIGENDIAN
            snd_cmdbuf[6] = ui.b[0];
            snd_cmdbuf[7] = ui.b[1];
            snd_cmdbuf[8] = ui.b[2];
            snd_cmdbuf[9] = ui.b[3];
            #else
            snd_cmdbuf[6] = ui.b[3];
            snd_cmdbuf[7] = ui.b[2];
            snd_cmdbuf[8] = ui.b[1];
            snd_cmdbuf[9] = ui.b[0];
            #endif
            ui.i = iDATA1;
            #if BIGENDIAN
            snd_cmdbuf[10]  = ui.b[0];
            snd_cmdbuf[11]  = ui.b[1];
            snd_cmdbuf[12] = ui.b[2];
            snd_cmdbuf[13] = ui.b[3];
            #else
            snd_cmdbuf[10]  = ui.b[3];
            snd_cmdbuf[11]  = ui.b[2];
            snd_cmdbuf[12] = ui.b[1];
            snd_cmdbuf[13] = ui.b[0];
            #endif
            ui.i = iDATA2;
            #if BIGENDIAN
            snd_cmdbuf[14] = ui.b[0];
            snd_cmdbuf[15] = ui.b[1];
            snd_cmdbuf[16] = ui.b[2];
            snd_cmdbuf[17] = ui.b[3];
            #else
            snd_cmdbuf[14] = ui.b[3];
            snd_cmdbuf[15] = ui.b[2];
            snd_cmdbuf[16] = ui.b[1];
            snd_cmdbuf[17] = ui.b[0];
            #endif
            snd_cmdbuf[18] = zop_CalcCheckSum(snd_cmdbuf, 18);
            zop_PressSend(19);
            break;
            
        case 3:
            snd_cmdbuf[1] = 0x15;
            {
                int iBase = (idy_copy_trace + idy_copy_trace + idy_copy_trace + idy_copy_trace);
                iDATA0 = buf_trace[iBase + 0];
                iDATA1 = buf_trace[iBase + 1];
                iDATA2 = buf_trace[iBase + 2];
                iDATA3 = buf_trace[iBase + 3];
                idy_copy_trace++;
                idy_copy_trace &= msk;
            }
            ui.i = iDATA0;
            #if BIGENDIAN
            snd_cmdbuf[6] = ui.b[0];
            snd_cmdbuf[7] = ui.b[1];
            snd_cmdbuf[8] = ui.b[2];
            snd_cmdbuf[9] = ui.b[3];
            #else
            snd_cmdbuf[6] = ui.b[3];
            snd_cmdbuf[7] = ui.b[2];
            snd_cmdbuf[8] = ui.b[1];
            snd_cmdbuf[9] = ui.b[0];
            #endif
            ui.i = iDATA1;
            #if BIGENDIAN
            snd_cmdbuf[10] = ui.b[0];
            snd_cmdbuf[11] = ui.b[1];
            snd_cmdbuf[12] = ui.b[2];
            snd_cmdbuf[13] = ui.b[3];
            #else
            snd_cmdbuf[10] = ui.b[3];
            snd_cmdbuf[11] = ui.b[2];
            snd_cmdbuf[12] = ui.b[1];
            snd_cmdbuf[13] = ui.b[0];
            #endif
            ui.i = iDATA2;
            #if BIGENDIAN
            snd_cmdbuf[14] = ui.b[0];
            snd_cmdbuf[15] = ui.b[1];
            snd_cmdbuf[16] = ui.b[2];
            snd_cmdbuf[17] = ui.b[3];
            #else
            snd_cmdbuf[14] = ui.b[3];
            snd_cmdbuf[15] = ui.b[2];
            snd_cmdbuf[16] = ui.b[1];
            snd_cmdbuf[17] = ui.b[0];
            #endif
            ui.i = iDATA3;
            #if BIGENDIAN
            snd_cmdbuf[18] = ui.b[0];
            snd_cmdbuf[19] = ui.b[1];
            snd_cmdbuf[20] = ui.b[2];
            snd_cmdbuf[21] = ui.b[3];
            #else
            snd_cmdbuf[18] = ui.b[3];
            snd_cmdbuf[19] = ui.b[2];
            snd_cmdbuf[20] = ui.b[1];
            snd_cmdbuf[21] = ui.b[0];
            #endif
            snd_cmdbuf[22] = zop_CalcCheckSum(snd_cmdbuf, 22);
            zop_PressSend(23);
            break;
    }
}


void PC_MONITOR_MAIN(void)
{
    CMDMONI_WT_CHECKCMD();
}

void PC_MONITOR_1MS(void)
{
	CMDMONI_WT_1MS();
}

void PC_MONITOR_PWM(void)
{
	CMDMONI_WT_TRACEWATCH();
}

void PC_MONITOR_INTR(void)
{
    {
        MD_WT_INTR();
    }
}

#endif