#include "viper.h"
#include "elog.h"
#include "FreeRTOS.h"
#include "cmsis_os.h"
#include "usart.h"

/******           Port             ***************/
#if 1
My_Viper_TypeDef myviper =
    {
        Viper_Init,
        Viper_Port,
        Viper_Set_State,
        // SEL_232_485_GPIO_Port,
        // HDPLX_GPIO_Port,
        // DE485_GPIO_Port,
        // SEL_232_485_Pin,
        // HDPLX_Pin,
        // DE485_Pin,
        NULL,
        NULL,
        NULL,
        0,
        0,
        0,
        Uart_Interface_Default, /* 接口类型 0: common, 1: 485 ,2:3160*/
        Uart_RSMODE_232,        /* mode_232_485 0 commome  1:485*/
        Uart_Trans_DMA,         /* trans_type0 :polling, 1: IT  2: DMA*/
        Uart_Trans_DMA,         /* rcv_type 0 :polling, 1: IT  2: DMA*/
        Uart_IDLE_IT_ENABLE,    /* idle_enable_disable 0 :不启用空闲 , 1: 启用空闲*/
       NULL, /* pointer huart*/
};

UART_HandleTypeDef *pViperUart = &huart2;
#define Viper_Uart_IRQHandler USART2_IRQHandler

extern volatile uint8_t measure_flag;
Viper_TypeDef *viper;

/*
    01 03 0B A4 00 05 C6 0E   (SN  2840 5  demo)
    01 03 00 0A 00 05 A5 CB    (SN  10 5   实际)
*/
uint8_t Viper_SN_command[8] = {0x01, 0x03, 0x00, 0x0A, 0x00, 0x05, 0xA5, 0xCB};

uint8_t Viper_Wavelength_sendbuf[3][8]   = {
        /*
        01 03 08 36 00 7A 26 47    (2102  122)
        01 03 08 B0 00 7A C7 AE    (2224  122)
        01 03 09 2A 00 7A E6 7D    (2346  122)
        */
        { 0x01, 0x03, 0x08, 0x36, 0x00, 0x7A, 0x26, 0x47 },
        { 0x01, 0x03, 0x08, 0xB0, 0x00, 0x7A, 0xC7, 0xAE },
        { 0x01, 0x03, 0x09, 0x2A, 0x00, 0x7A, 0xE6, 0x7D }
    };
uint8_t Viper_Data_sendbuf[3][8]   = {
        /*
        01 03 0A 36 00 7A 27 FF    (2614  122)
        01 03 0A B0 00 7A C6 16    (2736  122)
        01 03 0B 2A 00 7A E7 C5    (2858  122)
        */
        { 0x01, 0x03, 0x0A, 0x36, 0x00, 0x7A, 0x27, 0xFF },
        { 0x01, 0x03, 0x0A, 0xB0, 0x00, 0x7A, 0xC6, 0x16 },
        { 0x01, 0x03, 0x0B, 0x2A, 0x00, 0x7A, 0xE7, 0xC5 }
    };
uint8_t Viper_sn_buf[15] = {0};
// uint8_t Viper_rcvbuf[2][Viper_Rcv_Buf_Size] = {0};

uint8_t Viper_Rcv_Buf[2 * Viper_Rcv_Buf_Size] = {0};  // 一次接收 数据量 Oscar_Rcv_Buf_Size，翻倍*2， 多次

osThreadId_t viperHandle;
const osThreadAttr_t Viper_attributes = {
    .name = "viper",
    .stack_size = 1024,
    .priority = (osPriority_t)osPriorityBelowNormal,
};

void Viper_Uart_IRQHandler(void)
{
    uint16_t count = 0;
    uint16_t data_length = 0;
    if (__HAL_UART_GET_FLAG(pViperUart, UART_FLAG_TC) != RESET)
    {
        log_i("Viper_.... TC IT ...");
        // viper->uarthelper ->status = Uart_Status_Send_Cplt ;
        // Viper_Set_UartHelper_Status( viper, Uart_Status_Send_Cplt);
        Viper_Set_GPIO_For_TC_Cplt(viper);

        viper->uarthelper->huart->gState = HAL_UART_STATE_READY;
        // pViperUart->gState = HAL_UART_STATE_READY;
        // UartHelper_Trans_Cplt_Callback(  viper->uarthelper  );
        //  log_i("Send Viper_Uart_IRQHandler->  interface_type %d,  232-485  %d ,  sel: %d ,de485  %d  , duplx %d "
        //     ,viper->uarthelper->interface->interface_type
        //     ,viper->uarthelper->interface->mode_232_485
        //     ,HAL_GPIO_ReadPin(viper->uarthelper->interface->sel_gpio,viper->uarthelper->interface->sel_pin)
        //     ,HAL_GPIO_ReadPin(viper->uarthelper->interface->de485_gpio,viper->uarthelper->interface->de485_pin)
        //     ,HAL_GPIO_ReadPin(viper->uarthelper->interface->dplx_gpio,viper->uarthelper->interface->dplx_pin)
        //     );
        // viper->status = Uart_Status_Send_Cplt;
        // huart3.gState = HAL_UART_STATE_READY;
        __HAL_UART_CLEAR_FLAG(viper->uarthelper->huart, UART_FLAG_TC);
    }

    if (__HAL_UART_GET_FLAG(pViperUart, UART_FLAG_IDLE) != RESET )
    {
        log_i("Viper_....  IDLE  IT ....  ");
        // TODO ...处理空闲 ，最后一位  datalength-1

        count = __HAL_DMA_GET_COUNTER(viper->uarthelper->huart->hdmarx);
        data_length = 2 * Viper_Rcv_Buf_Size - count;
        if ( data_length > 0 )
        {
            log_i("Viper_. IDLE..data_length  %d   count  %d ",   data_length, count);
            Viper_copy( viper, data_length );
            // log_i("Viper_. IDLE..res  %02X %d %02X",  viper->uarthelper ->receive_buf[0],  viper->uarthelper ->receive_buf[2],  viper->uarthelper ->receive_buf[data_length-1]);
        }
 
        __HAL_UART_CLEAR_IDLEFLAG(pViperUart);
    }

    if (__HAL_UART_GET_FLAG(pViperUart, UART_FLAG_ORE) != RESET)
    {
        log_i("Viper_....ORE");

        __HAL_UART_CLEAR_FLAG(pViperUart, UART_FLAG_ORE);
    }
    if (__HAL_UART_GET_FLAG(pViperUart, UART_FLAG_FE) != RESET)
    {
        log_i("Viper_....FE");

        __HAL_UART_CLEAR_FLAG(pViperUart, UART_FLAG_FE);
    }
    if (__HAL_UART_GET_FLAG(pViperUart, UART_FLAG_PE) != RESET)
    {
        log_i("Viper_....PE");

        __HAL_UART_CLEAR_FLAG(pViperUart, UART_FLAG_PE);
    }
    HAL_UART_IRQHandler(pViperUart);
}

int Viper_copy(Viper_TypeDef *viper ,uint16_t size)
{
    log_i( "Viper_ idle copy  state %d   size :%d   ", viper->state,size);
    if ( viper->state == MODULE_Viper_STATE_CHECK_SN  )
    {
        memcpy(viper->sn_buf, viper->uarthelper->receive_buf, size);
        viper->size_received = viper->size_received + size;
        return 0;
    }
    if ( viper->state == MODULE_Viper_STATE_GET_WL_0  )
    {
        memcpy(viper->Viper_Wavelength_Buf[0], viper->uarthelper->receive_buf, size);
        viper->size_received = viper->size_received + size;
        return 0;
    }
    if (  viper->state == MODULE_Viper_STATE_GET_WL_1)
    {
        memcpy(viper->Viper_Wavelength_Buf[1], viper->uarthelper->receive_buf, size);
        viper->size_received = viper->size_received + size;
        return 0;
    }
    if ( viper->state == MODULE_Viper_STATE_GET_DATA_0   )
    {
        memcpy(viper->Viper_Data_Buf[0], viper->uarthelper->receive_buf, size);
        viper->size_received = viper->size_received + size;
        return 0;
    }
    if (  viper->state == MODULE_Viper_STATE_GET_DATA_1)
    {
        memcpy(viper->Viper_Data_Buf[1], viper->uarthelper->receive_buf, size);
        viper->size_received = viper->size_received + size;
        return 0;
    }
 
    return 0;
}

void Viper_Set_State( MODULE_Viper_WORK_STATE state  )
{
    // log_i( "Set_State...viper->state .%d ", viper->state);
      viper->state = state;
}

uint8_t Viper_Init()
{

    viper = (Viper_TypeDef *)malloc(sizeof(Viper_TypeDef));

    UartInterface_TypeDef *interface    = UartInterface_Init( );
    UartHelper_TypeDef *uarthelper      = UartHelper_Init();

    Viper_Set_Uarthelper( viper,  uarthelper );
    Viper_Set_Interface( viper,  interface );
    Viper_Set_Huart( viper, pViperUart );
 
    /* set_GPIO  UartInterface */
    UartInterface_Set_Sel_GPIO_Pin( viper->uarthelper->interface, myviper.sel_gpio, myviper.sel_pin );
    UartInterface_Set_Dplx_GPIO_Pin( viper->uarthelper->interface, myviper.dplx_gpio, myviper.dplx_pin );
    UartInterface_Set_DE485_GPIO_Pin( viper->uarthelper->interface, myviper.de_gpio, myviper.de_pin );

    /* interface type */
    Viper_Set_Interface_Type( viper, myviper.interface_type );
    Viper_Set_RsMode_232_485 ( viper, myviper.mode_232_485 ); //  max3160 的232  485选择
    Viper_Set_Trans_Type( viper, myviper.trans_type ) ;
    Viper_Set_Rcv_Type ( viper, myviper.rcv_type ) ;
    Viper_Set_Idle_Enable (viper, myviper.idle_enable_disable ) ;
    // UartHelper_Setup_Interface_type( viper ,myviper.interface_type); 
 
    //  check  
    if ( viper->uarthelper->huart == pViperUart ){
        log_i ( "viper set huart ok. trans_type -> %d  rcv_type -> %d  ( 0 :polling, 1: IT  2: DMA) ",viper->trans_type ,viper->rcv_type ) ;
        log_i( " interface->   %d  %d (0:default, 1:485, 2:3160) " ,viper->uarthelper->interface_type  ,viper->uarthelper->interface->interface_type );  
        log_i( "viper 232_485 ->   %d  %d (0:232 ,1: 485)   " ,viper->uarthelper->mode_232_485, viper->uarthelper->interface->mode_232_485  );  
    }else{
        log_e ( "viper set huart failure "  ) ;
    }
        
    // viper send rcv buf 是动态的
    Viper_Set_Timeout( viper, 1000 );
    // Viper_Set_Sendbuf( viper,  Viper_SND_Buf, 8 );
    // Viper_Set_Rcvbuf(  viper,  Viper_RCV_Buf, sizeof(Viper_RCV_Buf) );

    // set paras;
    viper->timebase        = 0;
    // viper->timeout_ms    = 1000;
    viper->send_status     = Uart_Status_Ready;
    viper->send_flag       = 0;
    viper->command_seq     = 4;
    // viper->mode            = 2;
    // viper->data_begin_flag = 0;
    viper->state           = MODULE_Viper_STATE_WAIT;
    return 0;
}

void Viper_Port()
{
    viperHandle = osThreadNew(Viper_Task, (void*)(&viper->state), &Viper_attributes);
}
void Viper_Task(void* argument)
{
    // log_i( " viper tran mode : %d ", viper->->trans_mode );
    memset( viper->sn_buf, 0, sizeof(viper->sn_buf ));  
    memset( viper->Viper_Wavelength_Buf, 0, sizeof(viper->Viper_Wavelength_Buf ) );  
    memset( viper->Viper_Data_Buf, 0, sizeof(viper->Viper_Data_Buf ));  
    static int err_con=0;
    MODULE_Viper_WORK_STATE *state = argument;
    // FLAG   发送后进入 busy,  接收sn后就rcved_flag
    for (;;)
    {
        // log_i( " Viper_ state   %d   " , *state );
        switch (*state)
        {
        case MODULE_Viper_STATE_WAIT:
 
            break;
        case MODULE_Viper_STATE_START:
            // log_i( " Viper_Task  1 state   %d   " , *state );
                viper->timebase = osKernelGetTickCount();
                memset(Viper_Rcv_Buf, 0, sizeof(Viper_Rcv_Buf));
                UartHelper_Set_rcv_buf(viper->uarthelper , Viper_Rcv_Buf, sizeof(Viper_Rcv_Buf));
                viper->flag |= Viper_FLAG_MASK_BUSY;
                *state=MODULE_Viper_STATE_GET_SN; 
            break; 
        case MODULE_Viper_STATE_GET_SN:
            // log_i( " Viper_Task  2 state   %d    %d  " , *state ,viper->uarthelper->receive_buf_half_len );
                viper->timebase = osKernelGetTickCount();
                memset(Viper_Rcv_Buf, 0, sizeof(Viper_Rcv_Buf));
                viper->size_received = 0;
                UartHelper_Set_Trans_Buf(viper->uarthelper , Viper_SN_command, sizeof(Viper_SN_command));
            log_i( " Viper_Task  2 state   %d    %d  " , *state ,viper->uarthelper->receive_buf_half_len );
                Viper_Begin_Rcv(viper, viper->uarthelper->receive_buf, 2 * viper->uarthelper->receive_buf_half_len );
                Viper_Transmit(viper, viper->uarthelper->trans_buf, viper->uarthelper ->trans_size);
                *state = MODULE_Viper_STATE_CHECK_SN; 
            break; 
        case MODULE_Viper_STATE_CHECK_SN:
            log_i( " Viper_Task  3 check_sn   %d   sn %d   %d" , *state, viper->sn ,viper->flag);
            if ( viper->size_received == sizeof(viper->sn_buf)  )
            {
                log_i( " Viper_Task  Get SN  OK ... "   * );
                viper->flag |= Viper_FLAG_MASK_RECVED_SN;
                // Viper_clean_flag( viper ,Viper_FLAG_MASK_RECVED_WL|Viper_FLAG_MASK_RECVED_DATA);
                viper->flag |= Viper_FLAG_MASK_RECVED_SN;
                viper->error =0;
                err_con=0;
                viper->sn = viper->sn_buf[11]*256 + viper->sn_buf[12];
                HAL_UART_DMAStop( viper->uarthelper->huart );
                memset(Viper_Rcv_Buf, 0, sizeof(Viper_Rcv_Buf));
                memset(viper->Viper_Wavelength_Buf, 0, 3*Viper_Rcv_Buf_Size); 
                viper->size_received = 0;
                UartHelper_Set_Trans_Buf(viper->uarthelper , Viper_Wavelength_sendbuf[0], sizeof(Viper_SN_command));
                Viper_Begin_Rcv(viper, viper->uarthelper ->receive_buf, 2 * viper->uarthelper ->receive_buf_half_len);
                viper->timebase = osKernelGetTickCount();
                Viper_Transmit(viper, viper->uarthelper->trans_buf, viper->uarthelper ->trans_size);
                *state=MODULE_Viper_STATE_GET_WL_0;
            } 
            if( (osKernelGetTickCount()- viper->timebase) >=1000){
                log_w("Viper_ Get SN Failed %d,Retrying...",err_con);
                err_con++;
                if(err_con>10){
                    log_e("Viper_ Get SN Error");
                    // operating_state.plug3_state=0;   //TODO?
                    viper->error = 1;
                    *state=MODULE_Viper_STATE_ERROR;
                    break;
                }
                viper->timebase = osKernelGetTickCount();
                Viper_clean_flag( viper ,Viper_FLAG_MASK_BUSY);
                Viper_Transmit(viper, Viper_SN_command,8);
                *state=MODULE_Viper_STATE_GET_SN;
            }
            break;    
        case MODULE_Viper_STATE_GET_WL_0:
            log_i( " Viper_Task  4 state   %d  size_received  %d " , *state ,viper->size_received);
            log_i( " Viper_Task  viper->flag  %d " , *state ,viper->flag);
            // if ( viper->size_received == Viper_Rcv_Buf_Size  && (viper->flag &Viper_FLAG_MASK_RECVED_WL) !=0)
            if ( viper->size_received == Viper_Rcv_Buf_Size   )
            {
                log_i( "Viper_ WL0 sviper->size_received    %d      " ,viper->size_received  );
                viper->error =0;
                err_con=0;
                HAL_UART_DMAStop(viper->uarthelper ->huart );
                memset(Viper_Rcv_Buf, 0, sizeof(Viper_Rcv_Buf));
                viper->size_received = 0;
                UartHelper_Set_Trans_Buf(viper->uarthelper , Viper_Wavelength_sendbuf[1], sizeof(Viper_SN_command));
                Viper_Begin_Rcv(viper, viper->uarthelper->receive_buf, 2 * viper->uarthelper->receive_buf_half_len);
                viper->timebase = osKernelGetTickCount();
                Viper_Transmit(viper, viper->uarthelper->trans_buf, viper->uarthelper ->trans_size);
                *state=MODULE_Viper_STATE_GET_WL_1;               
            } 
            if( (osKernelGetTickCount()- viper->timebase) >=2000){
                log_w("Viper_ Get WL0 Failed %d,Retrying...",err_con);
                *state=MODULE_Viper_STATE_ERROR;
                break;
            }            
            break;
        case MODULE_Viper_STATE_GET_WL_1:
            // log_i( " Viper_Task  5 state   %d  size_received  %d " , *state ,viper->size_received);
            if ( viper->size_received == Viper_Rcv_Buf_Size  )
            {
                // log_i( "Viper_ WL1 viper->size_received    %d      " ,viper->size_received  );
                viper->error =0;
                err_con=0;
                HAL_UART_DMAStop(viper->uarthelper ->huart );
                memset(Viper_Rcv_Buf, 0, sizeof(Viper_Rcv_Buf) );
                viper->size_received = 0;
                UartHelper_Set_Trans_Buf(viper->uarthelper , Viper_Wavelength_sendbuf[2], sizeof(Viper_SN_command));
                Viper_Begin_Rcv(viper, viper->uarthelper->receive_buf, 2 * viper->uarthelper->receive_buf_half_len);
                viper->timebase = osKernelGetTickCount();
                Viper_Transmit(viper, viper->uarthelper->trans_buf, viper->uarthelper ->trans_size);
                *state=MODULE_Viper_STATE_GET_WL_2;               
            } 
            if( (osKernelGetTickCount()- viper->timebase) >=2000){
                log_w("Viper_ Get WL1 Failed %d,Retrying...",err_con);
                *state=MODULE_Viper_STATE_ERROR;
                break;
            }  
        case MODULE_Viper_STATE_GET_WL_2:
            // log_i( " Viper_Task  5 state   %d  size_received  %d " , *state ,viper->size_received);
            if ( viper->size_received == Viper_Rcv_Buf_Size    )
            {
                // log_i( "Viper_ WL1 viper->size_received    %d      " ,viper->size_received  );
                viper->error =0;
                err_con=0;
                HAL_UART_DMAStop(viper->uarthelper ->huart );
                memset(Viper_Rcv_Buf, 0, sizeof(Viper_Rcv_Buf));
                viper->size_received = 0;
                *state=MODULE_Viper_STATE_CHECK_WL;               
            } 
            if( (osKernelGetTickCount()- viper->timebase) >=2000){
                log_w("Viper_ Get WL0 Failed %d,Retrying...",err_con);
                *state=MODULE_Viper_STATE_ERROR;
                break;
            } 
        case MODULE_Viper_STATE_CHECK_WL:
            //  TODO 对获得波长数据进行校验 ,不状态跳转了
            // log_i( " Viper_  "  );
            if (1==1)
            {
                log_i( " Viper_  GET WL OK"  );
                viper->flag |= Viper_FLAG_MASK_RECVED_WL;
                Viper_clean_flag( viper, Viper_FLAG_MASK_BUSY );
                *state=MODULE_Viper_STATE_Ready_GET_DATA;
                memset(Viper_Rcv_Buf, 0, sizeof(Viper_Rcv_Buf));
                viper->size_received = 0;

                // log_w("Viper_  CKECKWL ...   %d ",viper->uarthelper->huart->gState );
            }
            break;
        case MODULE_Viper_STATE_Ready_GET_DATA:
            // 外部直接调用这个状态进行测量
            log_i( " Viper_Task  8  wait....  %d   %d " ,viper->size_received ,viper->uarthelper->huart->gState);
            break;
        case MODULE_Viper_STATE_START_DATA:
            // 外部直接调用这个状态进行测量
            log_i("Viper_  START_DATA ...   %d ",viper->uarthelper->huart->gState );

                viper->flag |= Viper_FLAG_MASK_BUSY;
                Viper_clean_flag( viper ,Viper_FLAG_MASK_RECVED_DATA);
                memset(Viper_Rcv_Buf, 0, sizeof(Viper_Rcv_Buf));
                viper->size_received = 0;
                HAL_UART_DMAStop(viper->uarthelper ->huart );
                Viper_Begin_Rcv(viper, viper->uarthelper->receive_buf, 2 * viper->uarthelper->receive_buf_half_len);
                UartHelper_Set_Trans_Buf(viper->uarthelper , Viper_Data_sendbuf[0], sizeof(Viper_SN_command));
                // 可以校验发送状态
                Viper_Transmit(viper, viper->uarthelper->trans_buf, viper->uarthelper ->trans_size);
            *state=MODULE_Viper_STATE_GET_DATA_0;
            break;
 
        case MODULE_Viper_STATE_GET_DATA_0:
                   log_i( " Viper_Task  10 state   %d  size_received  %d " , *state ,viper->size_received);
            log_i( " Viper_Task  viper->flag  %d " , *state ,viper->flag);
            if ( viper->size_received == Viper_Rcv_Buf_Size   )
            {
                // log_i( "Viper_ DATA0  viper->size_received    %d      " ,viper->size_received  );
                viper->error =0;
                err_con=0;
                HAL_UART_DMAStop(viper->uarthelper ->huart );
                memset(Viper_Rcv_Buf, 0, sizeof(Viper_Rcv_Buf));
                viper->size_received = 0;
                UartHelper_Set_Trans_Buf(viper->uarthelper , Viper_Data_sendbuf[1], sizeof(Viper_SN_command));
                Viper_Begin_Rcv(viper, viper->uarthelper->receive_buf, 2 * viper->uarthelper->receive_buf_half_len);
            viper->timebase = osKernelGetTickCount();
                Viper_Transmit(viper, viper->uarthelper->trans_buf, viper->uarthelper ->trans_size);
                *state = MODULE_Viper_STATE_GET_DATA_1;               
            } 
            if( (osKernelGetTickCount()- viper->timebase) >=2000){
                log_w("Viper_ Get DATA0 Failed %d,Retrying...",err_con);
                *state=MODULE_Viper_STATE_ERROR;
                break;
            }            
            break;
        case MODULE_Viper_STATE_GET_DATA_1:
            log_i( " Viper_Task  5 state   %d  size_received  %d " , *state ,viper->size_received);
            if ( viper->size_received == Viper_Rcv_Buf_Size    )
            {
                // log_i( "Viper_ Get DATA OK    %d      " ,viper->size_received  );
                viper->error =0;
                err_con=0;
                HAL_UART_DMAStop(viper->uarthelper ->huart );
                memset(Viper_Rcv_Buf, 0, sizeof(Viper_Rcv_Buf));
                viper->size_received = 0;
                UartHelper_Set_Trans_Buf(viper->uarthelper , Viper_Data_sendbuf[2], sizeof(Viper_SN_command));
                Viper_Begin_Rcv(viper, viper->uarthelper->receive_buf, 2 * viper->uarthelper->receive_buf_half_len);
            viper->timebase = osKernelGetTickCount();
                Viper_Transmit(viper, viper->uarthelper->trans_buf, viper->uarthelper ->trans_size);
                *state = MODULE_Viper_STATE_GET_DATA_2;         
            } 
            if( (osKernelGetTickCount()- viper->timebase) >=2000){
                log_w("Viper_ Get DATA1 Failed %d,Retrying...",err_con);
                *state=MODULE_Viper_STATE_ERROR;
                break;
            }  
        case MODULE_Viper_STATE_GET_DATA_2:
            log_i( " Viper_Task  5 state   %d  size_received  %d " , *state ,viper->size_received);
            if ( viper->size_received == Viper_Rcv_Buf_Size   )
            {
                // log_i( "Viper_ Get DATA OK    %d      " ,viper->size_received  );
                viper->error =0;
                err_con=0;
                viper->sn = viper->sn_buf[11]*256 + viper->sn_buf[12];
                HAL_UART_DMAStop(viper->uarthelper ->huart );
                memset(Viper_Rcv_Buf, 0, sizeof(Viper_Rcv_Buf));
            viper->timebase = osKernelGetTickCount();
                viper->size_received = 0;
                 *state=MODULE_Viper_STATE_CHECK_DATA;            
            } 
            if( (osKernelGetTickCount()- viper->timebase) >=2000){
                log_w("Viper_ Get DATA1 Failed %d,Retrying...",err_con);
                *state=MODULE_Viper_STATE_ERROR;
                break;
            } 
        case MODULE_Viper_STATE_CHECK_DATA:
 
            if (1==1)
            {
                log_i("Viper_ Get DATA OK " );
                viper->flag |= Viper_FLAG_MASK_RECVED_DATA;
                *state=    MODULE_Viper_STATE_OK;  
            }else{
                err_con++;
                log_w("Viper_ Get DATA Failed %d,Retrying...",err_con);
                if( err_con>3 ){
                *state=    MODULE_Viper_STATE_ERROR;                

                }
                *state=    MODULE_Viper_STATE_START_DATA;  
            }
            break;
        case MODULE_Viper_STATE_OK:
            HAL_UART_DMAStop(viper->uarthelper ->huart );
            log_w("Viper_ MODULE_Viper_STATE_OK " );
            viper->size_received = 0;
            break;
        case MODULE_Viper_STATE_ERROR:
                Viper_clean_flag( viper ,Viper_FLAG_MASK_BUSY);
                HAL_UART_DMAStop(viper->uarthelper ->huart );
                memset(Viper_Rcv_Buf, 0, sizeof(Viper_Rcv_Buf));
                viper->size_received = 0;
            break;   
        case MODULE_Viper_STATE_STOP:
                Viper_clean_flag( viper ,Viper_FLAG_MASK_BUSY);
                HAL_UART_DMAStop(viper->uarthelper ->huart );
                memset(Viper_Rcv_Buf, 0, sizeof(Viper_Rcv_Buf));
                viper->size_received = 0;
            break;
        default:
            Viper_clean_flag( viper ,Viper_FLAG_MASK_BUSY);
            *state=MODULE_Viper_STATE_WAIT;
            break;
        }

        osDelay(300);
    }
}

/******************         接口           *******************/
void Viper_Set_Uarthelper(Viper_TypeDef *viper, UartHelper_TypeDef *uarthelper)
{
    viper->uarthelper  = uarthelper;
}

void Viper_Set_Interface(Viper_TypeDef *viper, UartInterface_TypeDef *interface)
{
    viper->uarthelper->interface = interface;
}
void Viper_Set_Huart( Viper_TypeDef * viper,  UART_HandleTypeDef * huart  )
{
    viper->uarthelper->huart = huart;
}

void Viper_Set_Interface_Type( Viper_TypeDef *viper,    Uart_Interface_Type_Typedef interface_type  )
{
    viper->interface_type = interface_type;  
    viper->uarthelper->interface_type = interface_type; 
    UartInterface_Setup_Interface_type( viper->uarthelper->interface ,viper->interface_type); 
}

void Viper_Set_RsMode_232_485(Viper_TypeDef *viper, Uart_RS_Mode_TypeDef rs_232_485)
{
    //  TODO理清逻辑
    viper->mode_232_485 = rs_232_485; 
    viper->uarthelper->mode_232_485 = rs_232_485; 
       UartInterface_Setup_Mode_232_485( viper->uarthelper->interface, rs_232_485 );
}
void Viper_Set_Trans_Type( Viper_TypeDef *viper,  Uart_Transmode_TypeDef trans_type  )
{
    viper->trans_type = trans_type; 
}
void Viper_Set_Rcv_Type( Viper_TypeDef *viper,  Uart_Transmode_TypeDef rcv_type  )
{
    viper->rcv_type = rcv_type; 
    
}
void Viper_Set_Idle_Enable( Viper_TypeDef *viper,  Uart_IDLE_Enable_TypeDef idle_enable_disable  )
{
    viper->idle_enable_disable = idle_enable_disable; 

}
void Viper_Set_TransMode( Viper_TypeDef *viper,  Uart_Transmode_TypeDef trans_mode  )
{
    viper->trans_type = trans_mode; 
    viper->rcv_type = trans_mode; 

}
 
void Viper_Set_Timeout( Viper_TypeDef *viper,    uint16_t timeout_ms )
{
    viper->timeout_ms = timeout_ms; 
    viper->timeout_enable = 1;

}
void Viper_Set_Sendbuf( Viper_TypeDef *viper,  uint8_t * buf, uint16_t size )
{
 
    viper->send_buf = buf;
    viper->size_send = size;
}
void Viper_Set_Rcvbuf( Viper_TypeDef *viper,  uint8_t * buf, uint16_t size )
{
    viper->rcv_buf = buf;
    viper->size_rcv = size;

}

int Viper_Get_Timeout(Viper_TypeDef *viper)
{
    if ((osKernelGetTickCount() - viper->timebase) > viper->timeout_ms)
        return 1;
    return 0;
}

int Viper_Transmit(Viper_TypeDef *viper, uint8_t *buf, uint16_t size)
{

    if (viper->timeout_enable == 1)
    {
        viper->timebase = osKernelGetTickCount();
    }
    return UartHelper_Transmit(viper->uarthelper , buf, size);
}

int Viper_Begin_Rcv(Viper_TypeDef *viper, uint8_t *buf, uint16_t size)
{
    return UartHelper_Begin_Rcv(viper->uarthelper , buf, size);
}

Uart_Status_TypeDef Viper_Get_UartHelper_Status(Viper_TypeDef *viper)
{
    return viper->uarthelper ->status;
}
int Viper_Set_UartHelper_Status(Viper_TypeDef *viper, Uart_Status_TypeDef status)
{
    viper->uarthelper ->status = status;
    return 0;
}

int Viper_Get_Data_OK(Viper_TypeDef *viper)
{
    return viper->data_ok;
}
void Viper_Set_Send_Flag(Viper_TypeDef *viper)
{
    // vPortEnterCritical();
    viper->send_flag = 1;
    viper->command_seq = 0;
    viper->data_ok = 0;
    // vPortExitCritical( );
}


void Viper_callback(Viper_TypeDef *viper, uint8_t *buf, uint16_t size)
{
}

void Viper_Set_GPIO_For_TC_Cplt(Viper_TypeDef *viper)
{
    // viper->status = 2;
    UartHelper_Set_GPIO_For_Trans_Cplt(viper->uarthelper );
}


void Viper_clean_flag(Viper_TypeDef *viper,uint8_t mask)
{
    viper->flag&=~mask;
}
 
uint8_t Viper_get_flag(Viper_TypeDef *viper,uint8_t mask)
{
    return viper->flag&mask;
}

uint16_t Viper_get_serial_number(Viper_TypeDef * viper )
{
    if((viper->flag&Viper_FLAG_MASK_RECVED_SN)==0){
        return 0xffff;
    }
    return viper->sn;
}


void Viper_Error(Viper_TypeDef *viper, char *str)
{
    log_e(str);
    while (1)
        ;
}


#endif
