/**
 * @file Ramses.c
 * @author Chen Jihang (embedded@eseaoptics.com)
 * @brief trios公司光谱仪驱动
 * @version 1.0
 * @date 2023-01-04
 * 
 * @copyright ESEA (c) 2020
 * 
 */
#include <string.h>
#include <math.h>
#include "Ramses.h"
#include "elog.h"
#include "FreeRTOS.h"
#include "cmsis_os.h"
#include "usart.h"
 
#if 1

// #include "endian.h"
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

static uint8_t Ramses_SN_command[8] = {0x23, 0x00, 0x08, 0x0B, 0x00, 0x00, 0x00, 0x01};
static uint8_t Ramses_Data_command[8] = {0x23, 0x00, 0x00, 0x80, 0xA8, 0x00, 0x81, 0x01};
static uint8_t Ramses_Set_IT_Auto[8] = {0x23, 0x00, 0x00, 0x30, 0x78, 0x05, 0x00, 0x01};

My_Ramses_TypeDef myramses =
{
    Ramses_init,
    Ramses_Port,

        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 *pRamsesUart = &huart2;
#define  Ramses_Uart_IRQHandler   USART2_IRQHandler

extern volatile uint8_t measure_flag  ;
 
uint8_t Ramses_Rcv_Buf[Ramses_Rcv_Buf_Size] = {0};
uint8_t Ramses_Rcv_Buf_Copy[Ramses_Rcv_Buf_Size] = {0};
Ramses_TypeDef *ramses;

osThreadId_t ramsesHandle;
const osThreadAttr_t RAMSES_attributes = {
    .name       = "ramses",
    .stack_size = 1024,
    .priority   = (osPriority_t) osPriorityBelowNormal,
};


void Ramses_Uart_IRQHandler(void)
{
    uint16_t count = 0;
    uint16_t data_length = 0;
      if (__HAL_UART_GET_FLAG(pRamsesUart, UART_FLAG_TC) != RESET)
      {
        log_i("Ramses_ ..huart TC callback ...");
        pRamsesUart->gState = HAL_UART_STATE_READY;
        __HAL_UART_CLEAR_FLAG(pRamsesUart, UART_FLAG_TC);
      }

      if (__HAL_UART_GET_FLAG(pRamsesUart, UART_FLAG_IDLE) != RESET)
      {
        log_i("Ramses_ ..huart  IDLE ...");
        count = __HAL_DMA_GET_COUNTER( pRamsesUart->hdmarx );
        data_length = Ramses_Rcv_Buf_Size - count;
        if ( data_length > 0 )
        {
            // log_i("Ramses_  IDLE.. data_length  %d",   data_length);
            Ramses_copy( ramses, data_length );
            // log_i("Oscar_. IDLE..res  %02X %d %02X",  oscar->uarthelper ->receive_buf[0],  oscar->uarthelper ->receive_buf[2],  oscar->uarthelper ->receive_buf[data_length-1]);
        }
        __HAL_UART_CLEAR_IDLEFLAG( pRamsesUart );
      }
}

int Ramses_copy(Ramses_TypeDef *ramses ,uint16_t size)
{
    // log_i( "Ramses_ idle copy  state %d   size :%d   ", ramses->state,size);
    // if ( ramses->state == MODULE_RAMSESE_STATE_CHECK_SENSOR  )
    // {
        __disable_irq();
        // memcpy(Ramses_Rcv_Buf_Copy+ramses->size_received, Ramses_Rcv_Buf+ramses->size_received, size);
        ramses->size_received =   size;
        // ramses->size_received = ramses->size_received + size;
    // log_i( "Rramses->size_received after %d      " , ramses->size_received);
        __enable_irq();
    //     return 0;
    // }
    return 0;
}

void Ramses_Port ()
{
    ramsesHandle = osThreadNew( Ramses_Task, (void*) ( &ramses->state ), &RAMSES_attributes );
}
 

void Ramses_Task(void * argument)
{
    Ramses_clean_data_recved_flag(ramses);
    Ramses_clean_ip_recved_flag(ramses);
    Ramses_clean_info_recved_flag(ramses);
    MODULE_RAMSESE_WORK_STATE *state = argument;
   static int err_con=0;
    for ( ; ; )
    {  
        /*  step-> 1 ：get_sn  2： get_ip （积分时间）  3 get_data, 倒过来，可能不一定依据顺序走  */
        // log_i( "Ramses_ state   %d   " , *state );
        switch ( *state )
        {
            case MODULE_RAMSESE_STATE_WAIT:
                break;
            case MODULE_RAMSESE_STATE_START:
                ramses->timebase = osKernelGetTickCount();
                // sensor_enable();
                *state=MODULE_RAMSESE_STATE_SEND_CMD;
                break;
            case MODULE_RAMSESE_STATE_SEND_CMD:
                if( (osKernelGetTickCount() - ramses->timebase) >=5000){
                    ramses->timebase = osKernelGetTickCount();
                    ramses->size_received = 0;
                    memset( Ramses_Rcv_Buf, 0, sizeof(Ramses_Rcv_Buf) );
                    Ramses_cmd_get_infomation(ramses,0x80);
                    __HAL_UART_ENABLE_IT( pRamsesUart, UART_IT_IDLE );
                    HAL_UART_Receive_DMA( pRamsesUart,  Ramses_Rcv_Buf,sizeof(Ramses_Rcv_Buf) );
                    err_con = 0;
                    *state = MODULE_RAMSESE_STATE_CHECK_SENSOR;
                }
                break;
            case MODULE_RAMSESE_STATE_CHECK_SENSOR:
                log_i( "Ramses_  CHECK_SENSOR   %d   size_received : %d  " , *state , ramses->size_received);
                // 判断 size_received 大小进行处理
                if( ramses->size_received >=19)
                {
                    Ramses_receive_callback( ramses, Ramses_Rcv_Buf, ramses->size_received) ;            
                }
                if(Ramses_get_serial_number(ramses)!=0xffff){
                    HAL_UART_DMAStop( pRamsesUart );
                    ramses->sn=Ramses_get_serial_number( ramses );
                    log_i( "Ramses_  SN  %4X  " ,  ramses->sn);
                    Ramses_clean_flag(ramses,Ramses_FLAG_MASK_RECVED_INFO);
                    ramses->error =0;
                    err_con=0;
                    log_i("Ramses Get Infomation Succeed");
                    Ramses_clean_flag( ramses ,Ramses_FLAG_MASK_BUSY|Ramses_FLAG_MASK_RECVED_DATA|Ramses_FLAG_MASK_RECVED_IP);

                    ramses->size_received = 0;
                    memset( Ramses_Rcv_Buf, 0, sizeof(Ramses_Rcv_Buf) );
                    Ramses_cmd_messung( ramses ,0x80);
                    __HAL_UART_ENABLE_IT( pRamsesUart, UART_IT_IDLE );
                    HAL_UART_Receive_DMA( pRamsesUart,  Ramses_Rcv_Buf,sizeof(Ramses_Rcv_Buf) );
                    *state=MODULE_RAMSESE_STATE_GET_DATA;
                }
                if( (osKernelGetTickCount() - ramses->timebase) >=10000){
                    log_w( "Ramses Get Infomation Failed %d,Retrying...", err_con );

                    err_con++;
                    if(err_con>10){
                        log_e("Ramses Init Error");
                        // operating_state.plug3_state=0;   //TODO?
                        ramses->error = 1;
                        *state=MODULE_RAMSESE_STATE_ERROR;
                        break;
                    }
                    ramses->timebase = osKernelGetTickCount();
                    Ramses_clean_flag( ramses ,Ramses_FLAG_MASK_BUSY);
                    ramses->size_received = 0;
                    memset( Ramses_Rcv_Buf, 0, sizeof(Ramses_Rcv_Buf) );
                    Ramses_cmd_get_infomation(ramses,0x80);
                    __HAL_UART_ENABLE_IT( pRamsesUart, UART_IT_IDLE );
                    HAL_UART_Receive_DMA( pRamsesUart,  Ramses_Rcv_Buf,sizeof(Ramses_Rcv_Buf) );
                    *state=MODULE_RAMSESE_STATE_CHECK_SENSOR;
                }
                break;
            case MODULE_RAMSESE_STATE_GET_DATA:
                // log_i( "Ramses_  GET_DATA   %d   size_received : %d  " , *state , ramses->size_received);
                if( ramses->size_received >=576)
                {
                 log_i( "Ramses_  receive data deal --->>>  : %d  "   , ramses->size_received);
                    Ramses_receive_callback( ramses, Ramses_Rcv_Buf, ramses->size_received) ;            
                }
                if(Ramses_get_flag( ramses ,Ramses_FLAG_MASK_RECVED_DATA)!=0){
                    ramses->error = 0;
                    ramses->flag_ip = Ramses_get_flag( ramses ,Ramses_FLAG_MASK_RECVED_INFO)!=0;
                    Ramses_clean_flag( ramses ,Ramses_FLAG_MASK_BUSY|Ramses_FLAG_MASK_RECVED_DATA|Ramses_FLAG_MASK_RECVED_IP);
                    log_i("Ramses Get Data Succeed");
                    // operating_state.plug3_state=1;  //TODO?
                    err_con=0;
                    *state=MODULE_RAMSESE_STATE_OK;
                }
                if( (osKernelGetTickCount() - ramses->timebase) >= 10000 ){
                    log_w("Ramses Get Data Failed %d,Retrying...",err_con);

                    err_con++;
                    if(err_con>=10){
                        log_e("Ramses Init Error");
                        // operating_state.plug3_state=0;     //TODO?
                        ramses->error =1;
                        *state = MODULE_RAMSESE_STATE_ERROR;
                        break;
                    } 
                ramses->timebase = osKernelGetTickCount();

                Ramses_clean_flag( ramses ,Ramses_FLAG_MASK_BUSY|Ramses_FLAG_MASK_RECVED_DATA|Ramses_FLAG_MASK_RECVED_IP);
                HAL_UART_DMAStop( pRamsesUart );
                ramses->size_received = 0;
                memset( Ramses_Rcv_Buf, 0, sizeof(Ramses_Rcv_Buf) );
                Ramses_cmd_messung( ramses ,0x80);
                __HAL_UART_ENABLE_IT( pRamsesUart, UART_IT_IDLE );
                HAL_UART_Receive_DMA( pRamsesUart,  Ramses_Rcv_Buf,sizeof(Ramses_Rcv_Buf) );
                }
                break;
            case MODULE_RAMSESE_STATE_OK:
                break;
            case MODULE_RAMSESE_STATE_ERROR:
                break;
            case MODULE_RAMSESE_STATE_STOP:
                break;
            default:
                *state=MODULE_RAMSESE_STATE_WAIT;
                break;
        }
 
        osDelay(80 );
    }
}


/**
 * @brief 初始化
 * 
 * @param trios_ramses 设备描述符
 * @param send_obj 发送函数的第一个参数
 * @param send_fun 发送函数
 * @return struct trios_ramses* 成功:设备描述符 失败:NULL
 */
int  Ramses_init(  )
{
 
    ramses = (Ramses_TypeDef *)malloc(sizeof(Ramses_TypeDef));

    /*  设置超时*/
    Ramses_Set_Timeout(ramses, 9000);

    // set paras;
    ramses->state = MODULE_RAMSESE_STATE_WAIT;
    ramses->flag = 0;
    ramses->timebase = 0;
    ramses->timeout_ms = 9000;
    ramses->status = Uart_Status_Ready;
    ramses->send_flag = 0;
    // ramses->command_seq = 4;
    ramses->step = 2;
    ramses->size_received = 0;
    ramses->sn =  0xffff; 
 
    return 0;
}



/**
 * @brief 向传感器发送命令
 * 
 * @param trios_ramses 设备描述符
 * @param dev_id 设备ID
 * @param module_id 模块ID
 * @param instruction 指令
 * @param parameter1 参数1
 * @param parameter2 参数2
 * @return int 
 */
int Ramses_send_cmd(Ramses_TypeDef *ramses,uint16_t dev_id,uint8_t module_id,uint8_t instruction,uint8_t parameter1,uint8_t parameter2)
{

    int i=0;
    ramses->cmd_buf[i++]=0x23;
    ramses->cmd_buf[i++]=(dev_id>>8)&0x0f;
    ramses->cmd_buf[i++]=dev_id&0xff;
    ramses->cmd_buf[i++]=module_id;
    ramses->cmd_buf[i++]=instruction;
    ramses->cmd_buf[i++]=parameter1;
    ramses->cmd_buf[i++]=parameter2;
    ramses->cmd_buf[i++]=0x01;


    HAL_UART_Transmit_DMA( pRamsesUart, ramses->cmd_buf, i );
    // HAL_UARTEx_ReceiveToIdle_DMA( )
    // ramses->send_fun(ramses->send_class,ramses->cmd_buf,i);
    return 0;
}
/**
 * @brief 测量
 * 
 * @param trios_ramses 设备描述符
 * @return int 成功:0 失败:负值
 */
int Ramses_cmd_messung(Ramses_TypeDef *ramses,uint8_t module_id)
{
    if((ramses->flag&Ramses_FLAG_MASK_BUSY)!=0 || (ramses->flag&Ramses_FLAG_MASK_RECVED_DATA)!=0){
        return -2;
    }
    ramses->flag|=Ramses_FLAG_MASK_BUSY;
    return Ramses_send_cmd(ramses,0x0000,module_id,0xa8,0x00,0x81);
}
/**
 * @brief 设置积分时间
 * 
 * @param trios_ramses 设备描述符
 * @param integration_time 积分时间
 * @return int 成功:0 失败:负值
 */
int Ramses_cmd_set_integration_time(Ramses_TypeDef *ramses,enum trios_integration_time integration_time)
{
    if((ramses->flag&Ramses_FLAG_MASK_BUSY)!=0){
        return -2;
    }
    if(integration_time<0 || integration_time>0x0c){
        return -1;
    }
    return Ramses_send_cmd(ramses,0x0000,0x30,0x78,0x05,integration_time);
}
/**
 * @brief 获取模块信息
 * 
 * @param trios_ramses 设备描述符
 * @param module_id 模块ID
 * @return int 成功:0 失败:负值
 */
int Ramses_cmd_get_infomation(Ramses_TypeDef *ramses,uint8_t module_id)
{
    log_w(" Ramses_cmd_get_infomation     .... %d ",ramses->flag&Ramses_FLAG_MASK_BUSY); 

    if((ramses->flag&Ramses_FLAG_MASK_BUSY)!=0){
        return -2;
    }
    ramses->flag|=Ramses_FLAG_MASK_BUSY;
    return Ramses_send_cmd(ramses,0x0000,module_id,0xb0,0x00,0x00);
}
/**
 * @brief 获取IP头原始数据
 * 
 * @param trios_ramses 设备描述符
 * @param buf 缓冲区
 * @param size 缓冲区大小
 * @return uint32_t 成功:数据大小 失败:负值
 */
uint32_t Ramses_get_ip_raw(Ramses_TypeDef *ramses,uint8_t *buf,uint32_t size)
{
    if(size<Ramses_IP_SIZE){
        return -1;
    }
    if((ramses->flag&Ramses_FLAG_MASK_RECVED_IP)==0){
        return -2;
    }
    memcpy(buf,ramses->ip.raw,Ramses_IP_SIZE);
    return Ramses_IP_SIZE;
}
/**
 * @brief 获取光谱原始数据
 * 
 * @param trios_ramses 设备描述符
 * @param buf 缓冲区
 * @param size 缓冲区大小
 * @return uint32_t 成功:数据大小 失败:负值
 */
uint32_t Ramses_get_data_raw(Ramses_TypeDef *ramses,uint8_t *buf,uint32_t size)
{
    int i;
    if(size<Ramses_DATA_SIZE*Ramses_DATA_NUM){
        return 0;
    }
    if((ramses->flag&Ramses_FLAG_MASK_RECVED_DATA)==0){
        return 0;
    }
    for(i=0;i<Ramses_DATA_NUM;i++){
        memcpy(&buf[i*Ramses_DATA_SIZE],ramses->data[i].raw,Ramses_DATA_SIZE);
    }
    return i*Ramses_DATA_SIZE;
}
/**
 * @brief 获取转换后的光谱数据
 * 
 * @param trios_ramses 设备描述符
 * @param data 数据缓冲区
 * @param num 缓冲区长度
 * @return uint32_t 成功:数据长度 失败:0xffffffff
 */
uint32_t Ramses_get_data(Ramses_TypeDef *ramses,int16_t *data,uint32_t num)
{
    int i;
    int j;
    uint8_t endian_mask;
    // endian_mask=endian_get_local_endian_mask(ENDIAN_MASK_LITTLE_ENDIAN);
    if(num<(Ramses_DATA_SIZE-8)*Ramses_DATA_NUM/2)
    {
        return 0;
    }
    if((ramses->flag&Ramses_FLAG_MASK_RECVED_DATA)==0){
        return 0;
    }
    for(i=0,j=0;i<Ramses_DATA_NUM;i++){
        // if(endian_write(endian_mask,ENDIAN_WIDTH_MASK_2BYTE,&((uint8_t*)data)[j],ramses->data[i].data,ramses->data[i].data_size)!=0){
        //     Ramses_clean_data_recved_flag(trios_ramses);
        //     return 0xffffffff;
        // }
        // j+=ramses->data[i].data_size;
    }
    return j/2;
}
/**
 * @brief 获取X轴倾斜角度
 * 
 * @param trios_ramses 设备描述符
 * @param incl_xgain 标定数据
 * @param incl_xoffset 标定数据
 * @return double 成功:X轴倾角 失败:NAN
 */
double Ramses_get_x_inclination_angle(Ramses_TypeDef *ramses,double incl_xgain,double incl_xoffset)
{
    if((ramses->flag&Ramses_FLAG_MASK_RECVED_IP)==0){
        return NAN;
    }
    return (ramses->ip.data[4]-incl_xoffset)*incl_xgain;
}
/**
 * @brief 获取Y轴倾斜角度
 * 
 * @param trios_ramses 设备描述符
 * @param incl_ygain 标定数据
 * @param incl_yoffset 标定数据
 * @return double 成功:Y轴倾角 失败:NAN
 */
double Ramses_get_y_inclination_angle(Ramses_TypeDef *ramses,double incl_ygain,double incl_yoffset)
{
    if((ramses->flag&Ramses_FLAG_MASK_RECVED_IP)==0){
        return NAN;
    }
    return (ramses->ip.data[5]-incl_yoffset)*incl_ygain;
}
/**
 * @brief 获取设备倾角
 * 
 * @param x_inclination_angle X轴倾角
 * @param y_inclination_angle Y轴倾角
 * @return double 成功:设备倾角 失败:NAN
 */
double Ramses_get_inclination_angle(double x_inclination_angle,double y_inclination_angle)
{
    if(x_inclination_angle==NAN || y_inclination_angle==NAN){
        return NAN;
    }
    return atan(sqrt(pow(tan(x_inclination_angle*M_PI/180),2)+pow(tan(y_inclination_angle*M_PI/180),2)));
}
/**
 * @brief 获取压力
 * 
 * @param trios_ramses 设备描述符
 * @param incl_kref 标定数据
 * @param incl_kbg 标定数据
 * @param press_gain 标定数据
 * @param press_surface_bar 标定数据
 * @param press_sens_mv_bar_1ma 标定数据
 * @param press_sens_mv_bar_4ma 标定数据
 * @return double 压力
 */
double Ramses_get_pressure(Ramses_TypeDef *ramses,double incl_kref,double incl_kbg,double press_gain,double press_surface_bar,double press_sens_mv_bar_1ma,double press_sens_mv_bar_4ma)
{
    uint16_t npress;
    uint16_t nbg;
    uint16_t nrefh;
    uint16_t nrefl;
    double noffset;
    double press_sens;
    double vpress_v;
    double p_bar;
    if((ramses->flag&Ramses_FLAG_MASK_RECVED_IP)==0){
        return NAN;
    }
    npress=((uint16_t)ramses->ip.data[7]<<8)+ramses->ip.data[6];
    nbg=((uint16_t)ramses->ip.data[11]<<8)+ramses->ip.data[10];
    nrefh=((uint16_t)ramses->ip.data[13]<<8)+ramses->ip.data[12];
    nrefl=((uint16_t)ramses->ip.data[15]<<8)+ramses->ip.data[14];
    noffset=nrefl-(incl_kref*(nrefh-nrefl));
    vpress_v=incl_kbg*(npress-noffset)/(nbg-noffset);
    press_sens=(press_sens_mv_bar_4ma<=0?press_sens_mv_bar_1ma:press_sens_mv_bar_4ma);
    p_bar=1000.0*vpress_v/(press_sens*press_gain);
    return p_bar-1.021;
}
/**
 * @brief 获取深度(米)
 * 
 * @param pressure 压力
 * @return double 成功:深度 失败:NAN
 */
double Ramses_get_depth_m(double pressure)
{
    if(pressure==NAN){
        return NAN;
    }
    return pressure*10;
}
/**
 * @brief 获取设备序列号
 * 
 * @param trios_ramses 设备描述符
 * @return uint16_t 成功:设备序列号 失败:0xffff
 */
uint16_t Ramses_get_serial_number(Ramses_TypeDef *ramses)
{
    if((ramses->flag&Ramses_FLAG_MASK_RECVED_INFO)==0){
        return 0xffff;
    }
    return ramses->infomation.serial_number;
}
/**
 * @brief 清除指定标志位
 * 
 * @param trios_ramses 设备描述符
 * @param mask 掩码
 */
void Ramses_clean_flag(Ramses_TypeDef *ramses,uint8_t mask)
{
    ramses->flag&=~mask;
}
/**
 * @brief 获取指定标志位
 * 
 * @param trios_ramses 设备描述符
 * @param mask 掩码
 * @return uint8_t 标志位
 */
uint8_t Ramses_get_flag(Ramses_TypeDef *ramses,uint8_t mask)
{
    return ramses->flag&mask;
}
/**
 * @brief 传感器消息解码
 * 
 * @param trios_ramses 设备描述符
 * @param buf 接收到的数据缓冲区
 * @param size 接收到的数据长度
 */
void Ramses_receive_callback(Ramses_TypeDef *ramses,uint8_t *buf,uint32_t size)
{
    int i;
    for(i=0;i<size;i++){
        if(ramses->recv_buf_p==0 && buf[i]!=0x23){  // 已经过滤 开始的0x13 0x11
            continue; 
        }
        if(ramses->recv_flag_40==1){
            if(buf[i]==0x64){
                ramses->recv_buf[ramses->recv_buf_p++]=0x40;
            }
            else if(buf[i]==0x65){
                ramses->recv_buf[ramses->recv_buf_p++]=0x23;
            }
            else if(buf[i]==0x66){
                ramses->recv_buf[ramses->recv_buf_p++]=0x11;
            }
            else if(buf[i]==0x67){
                ramses->recv_buf[ramses->recv_buf_p++]=0x13;
            }
            else{
                ramses->data_index=0;
                ramses->recv_flag_40=0;
                ramses->recv_buf_p=0;
                continue;
            }
            ramses->recv_flag_40=0;
        }
        else if(buf[i]==0x40){
            ramses->recv_flag_40=1;
        }
        else{  // == 0x23 切换 recv_buf 数组下标
            ramses->recv_buf[ramses->recv_buf_p++]=buf[i];
        }
        if(ramses->recv_buf_p>7 && ramses->recv_buf_p==8+(2<<(ramses->recv_buf[1]>>5))){
            if(ramses->recv_buf[4]==0xff){   // 0xff  表征 SN信息帧
                if(ramses->recv_buf_p!=Ramses_INFO_SIZE){
                    ramses->recv_flag_40=0;
                    ramses->recv_buf_p=0;
                    continue;
                }
                ramses->infomation.dev_id=((uint16_t)ramses->recv_buf[1]<<8)+ramses->recv_buf[2];
                ramses->infomation.module_id=ramses->recv_buf[3];
                ramses->infomation.serial_number=((uint16_t)ramses->recv_buf[8]<<8)+ramses->recv_buf[7];
                ramses->infomation.firmware_number[0]=ramses->recv_buf[10];
                ramses->infomation.firmware_number[1]=ramses->recv_buf[9];
                memcpy(ramses->infomation.individual_information,&ramses->recv_buf[7],Ramses_INFO_SIZE-12);
                ramses->recv_buf_p=0;
                ramses->flag|=Ramses_FLAG_MASK_RECVED_INFO;
                ramses->flag&=~Ramses_FLAG_MASK_BUSY;
            }
            else if(ramses->recv_buf[4]==0xfe){  // 0xfe错误
                ramses->data_index=0;
                ramses->recv_flag_40=0;
                ramses->recv_buf_p=0;
                continue;
            }
            else{
                if(ramses->recv_buf[3]==0x80 || ramses->recv_buf[3]==0x30 || ramses->recv_buf[3]==0x00){
                    // log_i("....  0x80 0x 30  0x00");
                    if((ramses->flag&Ramses_FLAG_MASK_RECVED_DATA)!=0){
                        ramses->data_index=0;
                        ramses->recv_flag_40=0;
                        ramses->recv_buf_p=0;
                        continue;
                    }
                    if(ramses->recv_buf_p!=Ramses_DATA_SIZE){
                        ramses->recv_flag_40=0;
                        ramses->recv_buf_p=0;
                        continue;
                    }
                    if(ramses->recv_buf[4]!=Ramses_DATA_NUM-1-ramses->data_index){
                        ramses->data_index=0;
                        ramses->recv_flag_40=0;
                        ramses->recv_buf_p=0;
                        continue;
                    }
                    memcpy(ramses->data[ramses->data_index].raw,ramses->recv_buf,Ramses_DATA_SIZE);
                    ramses->data[ramses->data_index].dev_id=((uint16_t)ramses->recv_buf[1]<<8)+ramses->recv_buf[2];
                    ramses->data[ramses->data_index].module_id=ramses->recv_buf[3];
                    ramses->data[ramses->data_index].framebyte=ramses->recv_buf[4];
                    ramses->data[ramses->data_index].data=&ramses->data[ramses->data_index].raw[7];
                    ramses->data[ramses->data_index].data_size=Ramses_DATA_SIZE-8;
                    ramses->data_index++;
                    ramses->recv_buf_p=0;
                    if(ramses->data_index==Ramses_DATA_NUM){
                        ramses->data_index=0;
                        ramses->flag|=Ramses_FLAG_MASK_RECVED_DATA;
                        ramses->flag&=~Ramses_FLAG_MASK_BUSY;
                    }
                }
                else if(ramses->recv_buf[3]==0x20){
                    if((ramses->flag&Ramses_FLAG_MASK_RECVED_IP)!=0){
                        ramses->data_index=0;
                        ramses->recv_flag_40=0;
                        ramses->recv_buf_p=0;
                        continue;
                    }
                    if(ramses->recv_buf_p!=Ramses_IP_SIZE){
                        ramses->recv_flag_40=0;
                        ramses->recv_buf_p=0;
                        continue;
                    }
                    memcpy(ramses->ip.raw,ramses->recv_buf,Ramses_IP_SIZE);
                    ramses->ip.dev_id=((uint16_t)ramses->recv_buf[1]<<8)+ramses->recv_buf[2];
                    ramses->ip.module_id=ramses->recv_buf[3];
                    ramses->ip.framebyte=ramses->recv_buf[4];
                    ramses->ip.data=&ramses->ip.raw[7];
                    ramses->ip.data_size=Ramses_IP_SIZE-8;
                    ramses->recv_buf_p=0;
                    ramses->flag|=Ramses_FLAG_MASK_RECVED_IP;
                }
                else{
                    ramses->data_index=0;
                    ramses->recv_flag_40=0;
                    ramses->recv_buf_p=0;
                    continue;
                }
            }
        }
    }
    return;
}

Ramses_Set_Timeout(Ramses_TypeDef *ramses,    uint16_t timeout_ms )
{
    ramses->timebase = osKernelGetTickCount();

    ramses->timeout_enable = 1;
}
Ramses_Get_Timeout( Ramses_TypeDef *ramses )
{
    if ((osKernelGetTickCount() - ramses->timebase) > ramses->timeout_ms)
        return 1;
    return 0;
}


void Ramses_Set_State (Ramses_TypeDef *ramses, MODULE_RAMSESE_WORK_STATE state)
{
    ramses->state = state;
}

#endif