#include "soft_can.h"
#include <string.h>
#include "can.h"
#include "soft_crc.h"
#include "stdbool.h"
#include "soft_crc.h"
#include "soft_uart.h"
#include "usart.h"
#include "stdio.h"
#include "soft_okcell.h"
#include "soft_tattu.h"
#include "soft_poweramp.h"
#include "soft_terrain.h"
#include "soft_obstacle.h"
#include "soft_engine.h"
#include "soft_p_2_c.h"
#include "soft_herewin.h"
#include "soft_eft.h"
#include "soft_water_device.h"
#include "soft_seed_device.h"
#include "soft_bms.h"
#include "soft_flow.h"
#include "soft_update.h"
#include "can_debug.h"
#include "qingxie_bms.h"

CAN_RxHeaderTypeDef RxHeader;
CAN_TxHeaderTypeDef TxHeader;
uint8_t RxData[8];
Radar Rupdate;


/**
  * @file    Can_decode_data
  * @brief   CAN解析 不过滤的ID
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
void Can_decode_data_function(CAN_RxHeaderTypeDef Rxhead)
{
    if(Rxhead.IDE == CAN_ID_STD)
    {
      switch (Rxhead.StdId)
      {
      //恩曌仿地雷达
      case CAN_MIMO_T_ID:
        can_recv_enzhao_terrain(RxHeader.StdId, RxData, RxHeader.DLC);
        break;

      //恩曌360雷达
      // case CAN_HEAD_MIMO360_ID ... CAN_INFO_MIMO360_ID:
      //   can_recv_mocib_360_obstacle(RxHeader.StdId, RxData, RxHeader.DLC);
      //   break;
      
      //恩曌流量计
      case CAN_MIMO_FLOW:
        can_recv_mimor_flow_function(RxHeader.StdId, RxData, RxHeader.DLC);
        break;

      //极目电池
      case CAN_POWERAMP:
        PowerAmpCanRecvHookFunction(RxHeader.StdId, RxData, RxHeader.DLC);
        break;

      //恩曌360雷达 新协议
      // case CAN_360MIMO_1ID ... CAN_360MIMO_2ID:
      //   //can_recv_mocib_new360_obstacle(RxHeader.StdId, RxData, RxHeader.DLC);
      //   break;
      //恩曌前后避障雷达(多点)
      case CAN_MIMO_FOBS_ID1 ... CAN_MIMO_FOBS_ID3:
        can_recv_enzhao_obstacle(RxHeader.StdId, RxData, RxHeader.DLC);
        break;
      case CAN_MIMO_BOBS_ID1 ... CAN_MIMO_BOBS_ID3:
        can_recv_enzhao_obstacle(RxHeader.StdId, RxData, RxHeader.DLC);
        break;
      
      //恩曌前后避障雷达(单点)
      case CAN_MIMO_FOBS_SIG ... CAN_MIMO_BOBS_SIG:
        can_recv_mimo_signal_obstacle(RxHeader.StdId, RxData, RxHeader.DLC);
        break;

      //恩曌分区防地
      case CAN_MIMO_T_ID1 ... CAN_MIMO_T_ID3:
        can_recv_enzhao_terrain(RxHeader.StdId, RxData, RxHeader.DLC);
        break;

      //恩曌雷达版本信息 和流量计系数
      case CAN_MIMO_VERSION:
        can_recv_mimo_radar_version(RxHeader.StdId, RxData, RxHeader.DLC);
        break;

      //吉利发动机
      case GEELY_ENGINE_START_ID ... GEELY_ENGINE_END_ID:
        EngGeelyCanRecvHookFunction(RxHeader.StdId, RxData, RxHeader.DLC);
        break;
      case 0x100: //geely发动机 上电长发包
        EngGeelyCanRecvHookFunction(RxHeader.StdId, RxData, RxHeader.DLC);
        break;
      case 0x031: //飞控发送时间回馈
        EngGeelyCanRecvHookFunction(RxHeader.StdId, RxData, RxHeader.DLC);
        break;
      //吉利发动机
      case GEELY_ENGINE_START_ID2 ... GEELY_ENGINE_END_ID2:
        EngGeelyCanRecvHookFunction(RxHeader.StdId, RxData, RxHeader.DLC);
        break;  
      //莫之比前后避障雷达(多点)
      case CAN_UAVRH_FOBS_ID1 ... CAN_UAVRH_BOBS_ID3: 
        can_recv_mocib_obstacle(RxHeader.StdId, RxData, RxHeader.DLC);
        break;
      case CAN_EZ_R_UPDATE:
        EZ_Radar_UpdateCanRecvHookFunction(RxHeader.StdId, RxData, RxHeader.DLC);
        break;
      default:
        break;
      }
    }
    else if(Rxhead.IDE == CAN_ID_EXT)
    {
      switch (Rxhead.ExtId)
      {
      //木牛仿地雷达
      // case CAN_MUNIU:
      //   can_recv_muniu_terrain(RxData);
      //   break;

      //格式电池
      case CAN_NEWTATTU_MSG ... CAN_NEWTATTU_MSG2:
        TattuCanRecvHookFunction(RxHeader.ExtId, RxData, RxHeader.DLC);
        break;

      //莫之比雷达SN号
      case CAN_OBSTARCLE11_SN ... CAN_OBSTARCLE56_SN:
        can_recv_mocib_version_info(Rxhead.ExtId, RxData, RxHeader.DLC);
        break;
      //协氢电池
      case QINGXIE_BMS0_CAN_ID1 ... QINGXIE_BMS0_CAN_ID3:
         QxBmsCanRecvHookFunction(Rxhead.ExtId, RxData, RxHeader.DLC);
         break;
      case QINGXIE_BMS1_CAN_ID1 ... QINGXIE_BMS1_CAN_ID3:
         QxBmsCanRecvHookFunction(Rxhead.ExtId, RxData, RxHeader.DLC);
         break;
      case QINGXIE_BMS2_CAN_ID1 ... QINGXIE_BMS2_CAN_ID3:
         QxBmsCanRecvHookFunction(Rxhead.ExtId, RxData, RxHeader.DLC);
         break;
      case QINGXIE_BMS3_CAN_ID1 ... QINGXIE_BMS3_CAN_ID3:
         QxBmsCanRecvHookFunction(Rxhead.ExtId, RxData, RxHeader.DLC);
         break;
      case CAN_MSGID_DM_LACKLOSS:
         DMlacklossCanRecvFunction(Rxhead.ExtId, RxData, RxHeader.DLC);
         break;
      default:
        //好盈ID基本没有固定位，全检测
        HobbywingCanRecvHookFunction(Rxhead.ExtId, RxData, RxHeader.DLC);
        break;
      }
    }
}


/**
  * @file    Can_filter_init
  * @brief   CAN过滤初始化
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
void Can_filter_init()
{
  CAN_FilterTypeDef sFilterConfig;

  //莫之比避障、仿地雷 0
  sFilterConfig.FilterBank = 0;
  sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
  sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
  sFilterConfig.FilterIdHigh = (CAN_MICOB_FILTER_ID >> 13) & 0xffff;
  sFilterConfig.FilterIdLow = ((CAN_MICOB_FILTER_ID << 3) & 0xffff) | CAN_ID_EXT;
  sFilterConfig.FilterMaskIdHigh = (CAN_MICOB_MASK_ID >> 13) & 0xffff;
  sFilterConfig.FilterMaskIdLow = ((CAN_MICOB_MASK_ID << 3) & 0xffff) | CAN_ID_EXT;
  sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
  sFilterConfig.FilterActivation = ENABLE;
  sFilterConfig.SlaveStartFilterBank = 14;
  if (HAL_CAN_ConfigFilter(&hcan, &sFilterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  //正方电池1
  sFilterConfig.FilterBank = 1;
  sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
  sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
  sFilterConfig.FilterIdHigh = (CAN_ZHENGFANG >> 13) & 0xffff;
  sFilterConfig.FilterIdLow = ((((uint32_t)CAN_ZHENGFANG << 3) & 0xffff) | CAN_ID_EXT);
  sFilterConfig.FilterMaskIdHigh = (CAN_ZHENGFANG_MASk >> 13) & 0xffff;
  sFilterConfig.FilterMaskIdLow = ((((uint32_t)CAN_ZHENGFANG_MASk << 3) & 0xffff) | CAN_ID_EXT);
  sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
  sFilterConfig.FilterActivation = ENABLE;
  sFilterConfig.SlaveStartFilterBank = 14;
  if (HAL_CAN_ConfigFilter(&hcan, &sFilterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  //避障雷达升级2
  sFilterConfig.FilterBank = 2;
  sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
  sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
  sFilterConfig.FilterIdHigh = (CAN_UAVRH_FILTER_ID >> 13) & 0xffff;
  sFilterConfig.FilterIdLow = ((CAN_UAVRH_FILTER_ID << 3) & 0xffff) | CAN_ID_EXT;
  sFilterConfig.FilterMaskIdHigh = (CAN_UAVRH_MASK_ID >> 13) & 0xffff;
  sFilterConfig.FilterMaskIdLow = ((CAN_UAVRH_MASK_ID << 3) & 0xffff) | CAN_ID_EXT;
  sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
  sFilterConfig.FilterActivation = ENABLE;
  sFilterConfig.SlaveStartFilterBank = 14;
  if (HAL_CAN_ConfigFilter(&hcan, &sFilterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  //富力电池3
  sFilterConfig.FilterBank = 3;
  sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
  sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
  sFilterConfig.FilterIdHigh = (uint16_t)((uint32_t)FULLY_CELL_FILTER_ID >> 13 & 0xffff);
  sFilterConfig.FilterIdLow = (uint16_t)((((uint32_t)FULLY_CELL_FILTER_ID << 3) & 0xffff) | CAN_ID_EXT | CAN_RTR_DATA);
  sFilterConfig.FilterMaskIdHigh = (uint16_t)((((uint32_t)FULLY_CELL_MASK_ID) >> 13) & 0xffff);
  sFilterConfig.FilterMaskIdLow = (uint16_t)((((uint32_t)FULLY_CELL_MASK_ID << 3) & 0xffff) | CAN_ID_EXT | CAN_RTR_DATA);
  sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
  sFilterConfig.FilterActivation = ENABLE;
  sFilterConfig.SlaveStartFilterBank = 14;
  if (HAL_CAN_ConfigFilter(&hcan, &sFilterConfig) != HAL_OK)
  {
    Error_Handler();
  }

  //海盈智能电池4
  sFilterConfig.FilterBank = 4;
  sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
  sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
  sFilterConfig.FilterIdHigh = (HEREWIN_FILTER_ID >> 13) & 0xffff;
  sFilterConfig.FilterIdLow = ((HEREWIN_FILTER_ID << 3) & 0xffff) | CAN_ID_EXT;
  sFilterConfig.FilterMaskIdHigh = (HEREWIN_MASK_ID >> 13) & 0xffff;
  sFilterConfig.FilterMaskIdLow = ((HEREWIN_MASK_ID << 3) & 0xffff) | CAN_ID_EXT;
  sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
  sFilterConfig.FilterActivation = ENABLE;
  sFilterConfig.SlaveStartFilterBank = 14;
  if (HAL_CAN_ConfigFilter(&hcan, &sFilterConfig) != HAL_OK)
  {
    Error_Handler();
  }

  //EFT播撒器5
  sFilterConfig.FilterBank = 5;
  sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
  sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
  sFilterConfig.FilterIdHigh = (EFT_FILTER_ID >> 13) & 0xffff;
  sFilterConfig.FilterIdLow = ((EFT_FILTER_ID << 3) & 0xffff) | CAN_ID_EXT;
  sFilterConfig.FilterMaskIdHigh = (EFT_MASK_ID >> 13) & 0xffff;
  sFilterConfig.FilterMaskIdLow = ((EFT_MASK_ID << 3) & 0xffff) | CAN_ID_EXT;
  sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
  sFilterConfig.FilterActivation = ENABLE;
  sFilterConfig.SlaveStartFilterBank = 14;
  if (HAL_CAN_ConfigFilter(&hcan, &sFilterConfig) != HAL_OK)
  {
    Error_Handler();
  }

  //VK协议各种设备 6
  sFilterConfig.FilterBank = 6;
  sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
  sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
  sFilterConfig.FilterIdHigh = (CAN_MC_FILTER_ID >> 13) & 0xffff;
  sFilterConfig.FilterIdLow = ((CAN_MC_FILTER_ID << 3) & 0xffff) | CAN_ID_EXT;
  sFilterConfig.FilterMaskIdHigh = (CAN_MC_MASK_ID >> 13) & 0xffff;
  sFilterConfig.FilterMaskIdLow = ((CAN_MC_MASK_ID << 3) & 0xffff) | CAN_ID_EXT;
  sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
  sFilterConfig.FilterActivation = ENABLE;
  sFilterConfig.SlaveStartFilterBank = 14;
  if (HAL_CAN_ConfigFilter(&hcan, &sFilterConfig) != HAL_OK)
  {
    Error_Handler();
  }

  //VK智能电池 7
  sFilterConfig.FilterBank = 7;
  sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
  sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
  sFilterConfig.FilterIdHigh = (VK_BMS_FILTER_ID >> 13) & 0xffff;
  sFilterConfig.FilterIdLow = (((uint32_t)VK_BMS_FILTER_ID << 3) & 0xffff) | CAN_ID_EXT;
  sFilterConfig.FilterMaskIdHigh = (VK_BMS_MASK_ID >> 13) & 0xffff;
  sFilterConfig.FilterMaskIdLow = (((uint32_t)VK_BMS_MASK_ID << 3) & 0xffff) | CAN_ID_EXT;
  sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
  sFilterConfig.FilterActivation = ENABLE;
  sFilterConfig.SlaveStartFilterBank = 14;
  if (HAL_CAN_ConfigFilter(&hcan, &sFilterConfig) != HAL_OK)
  {
    Error_Handler();
  }

  //什么ID都接收
  sFilterConfig.FilterBank = 13;
  sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
  sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
  sFilterConfig.FilterIdHigh = 0;
  sFilterConfig.FilterIdLow = 0;
  sFilterConfig.FilterMaskIdHigh = 0;
  sFilterConfig.FilterMaskIdLow = 0;
  sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
  sFilterConfig.FilterActivation = ENABLE;
  sFilterConfig.SlaveStartFilterBank = 14;
  if (HAL_CAN_ConfigFilter(&hcan, &sFilterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  
}


void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
  if (__HAL_CAN_GET_FLAG(hcan, CAN_FLAG_FF0) != RESET)
  {
    __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FF0);
  }
  else if (__HAL_CAN_GET_FLAG(hcan, CAN_FLAG_FOV0) != RESET)
  {
    __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FOV0);
  }
  if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &RxHeader, RxData) != HAL_OK)
  {
    Error_Handler();
  }

  //莫之比前、后避、仿地
  if (RxHeader.FilterMatchIndex == 0)
  {
    switch (RxHeader.ExtId)
    {
    case CAN_OBSTACLE_1:
      can_recv_mocib_F_obstacle(RxData);
      break;
    case CAN_OBSTACLE_2:
      can_recv_mocib_B_obstacle(RxData);
      break;
    case CAN_UAVH30_MSG:
      can_recv_mocib_terrain(RxData);
      break;
    default:
      can_recv_mocib_version_info(RxHeader.ExtId, RxData, RxHeader.DLC);
      break;
    }
  }

  //正方电池
  else if (RxHeader.FilterMatchIndex == 1)
  {
    OkcellCanRecvHookFunction(RxHeader.ExtId, RxData, RxHeader.DLC); 
  }

  //莫之比雷达升级  设置丶读取雷达灵敏度，版本信息
  else if (RxHeader.FilterMatchIndex == 2)
  {
    can_recv_mocib_updata_read_set_hookfunction(RxHeader.ExtId, RxData);
  }

  //海盈智能电池
  else if(RxHeader.FilterMatchIndex == 4)
  {
    HerewinCanRecvHookFunction(RxHeader.ExtId, RxData, RxHeader.DLC);
  }
  
  //EFT
  else if (RxHeader.FilterMatchIndex == 5)
  {
    EftCanRecvHookFunction(RxHeader.ExtId, RxData, RxHeader.DLC);
  }

  //VK协议设备
  else if (RxHeader.FilterMatchIndex == 6)
  {
    if(RxHeader.ExtId == 0x166C || RxHeader.ExtId == 0x166D || RxHeader.ExtId == 0x166E)
    {
      QxBmsCanRecvHookFunction(RxHeader.ExtId, RxData, RxHeader.DLC);
    }
    
    switch (((RxHeader.ExtId) & SRC_ID_MASK) >> 5)
    {
    //液位计
    case CAN_NODEID_LIQUED:
      liquid_recieved_hookfuction(RxHeader.ExtId, RxData, RxHeader.DLC);
      break;
    case CAN_NODEID_FLOW:
      Flow_recieved_hookfuction(RxHeader.ExtId, RxData, RxHeader.DLC);
      break;
    //称重传感器、播撒器
    case CAN_NODEID_WEIGHT:
      Weight_recieved_hookfuction(RxHeader.ExtId, RxData, RxHeader.DLC);
      break; 
    //VK设备升级
    case CAN_NODEID_RADAR_UPDATE://14
        switch (((RxHeader.ExtId) & MSG_ID_MASK) >> 19)
        {
        case CAN_MSGID_RADAR_START:
          if((RxHeader.ExtId & 0x3) == 1)//381401
          {
            uart_info.step_200_flag = true;
          }
          else if((RxHeader.ExtId & 0x3) == 2) //381402
          {
            Update_Dev_Bootversion_Function(RxData);
          }
          break;
        case CAN_MSGID_RADAR_ING:
          if(uart_info.vk_dev_pack_num == (RxData[1] + RxData[2] * 256))
          {
            uart_info.vk_dev_pack_num++;
            uart_info.step_201_flag = true;
          }
          break;
        case CAN_MSGID_RADAR_END:
          uart_info.step_202_flag = true;
          break;
        default:
          break;
        }
        break;
    case CAN_NODEID_DISTRIBUTOR:
        distributor_recieved_hookfuction(RxHeader.ExtId, RxData, RxHeader.DLC);
        break;
    case CAN_NODEID_LASER:
        switch (((RxHeader.ExtId) & MSG_ID_MASK) >> 19)
        {
        case CAN_MSGID_RADAR_INFO:
          DM_terrain_recieved_hookfuction(RxHeader.ExtId, RxData, RxHeader.DLC);
          break;
        case CAN_MSGID_RADAR_VERSION_INFO:
          DM_terrain_recieved_hookfuction(RxHeader.ExtId, RxData, RxHeader.DLC);
          break;
        case CAN_MSGID_FRADAR_INFO ... CAN_MSGID_FRADAR_VERSION_INFO:
          DM_Fobs_recieved_hookfuction(RxHeader.ExtId, RxData, RxHeader.DLC);
          break;
        default:
          break;
        }
        break;
    default:
      break;
    }
  }

  //VK电池
  else if(RxHeader.FilterMatchIndex == 7)
  {
    BMSCanRecvHookFunction(RxHeader.ExtId, RxData, RxHeader.DLC);
  }

  //其他不过滤ID
  else
  {
    Can_decode_data_function(RxHeader);
  }

  if(planep.Candebug_flag == true || HAL_GetTick() < 10000)
    put_candata_to_rkfifo_rx(RxHeader, RxData);
  
  
  //重新打开接收中断
  HAL_CAN_ActivateNotification(hcan, CAN_IT_RX_FIFO0_MSG_PENDING);
}


/**
  * @file    can_start_init
  * @brief   CAN初始化 
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
void can_start_init()
{
  Can_filter_init();
  HAL_CAN_Start(&hcan);

  //开启错误处理机
  HAL_NVIC_EnableIRQ(CAN1_SCE_IRQn);
  
  //开启错误中断
  __HAL_CAN_ENABLE_IT(&hcan, CAN_IT_ERROR | CAN_IT_ERROR_WARNING | CAN_IT_ERROR_PASSIVE | CAN_IT_BUSOFF | CAN_IT_LAST_ERROR_CODE);
  //开启 邮箱满、溢出、接收 中断
  __HAL_CAN_ENABLE_IT(&hcan, CAN_IT_RX_FIFO0_FULL | CAN_IT_RX_FIFO0_OVERRUN);

  HAL_CAN_ActivateNotification(&hcan, CAN_IT_RX_FIFO0_MSG_PENDING);
  HAL_GPIO_WritePin(GPIOA, CAN_SLIENT_Pin, GPIO_PIN_RESET); //CAN总线使能
}


/**
  * @file    Can_obstacle_update
  * @brief   莫之比雷达CAN口在线升级函数 
  * @param   0=发送升级请求    1=接收请求升级状态  2=发送格式化指令
  *           3=接收格式化状态 4=向雷达发送升级包 5=发送校验和  6=接收雷达固件更新状态
  * @details 
  * @author  Zhang Sir 
 **/
uint32_t update_count = 0;
void Can_obstacle_update()
{

  static uint32_t update_i = 0;
  static uint32_t check_sum = 0;
  static uint16_t pack_count = 1;
  uint16_t size_count = 0;
  uint32_t for_i = 0;
  uint8_t obstacle_buf[8] = {0};

  switch (update_count)
  {
  case 0:
    //向雷达发送请求升级
    if (Update_buf[4] == 51)
    {
      obstacle_buf[0] = 0x11; // 前雷达
    }
    else if (Update_buf[4] == 52)
    {
      obstacle_buf[0] = 0x12; //后雷达
    }
    else if (Update_buf[4] == 56)
    {
      obstacle_buf[0] = 0x0B;
    }

    HAL_Delay(5);
    can_send_msg_normal(obstacle_buf, 1, 0x7E0);

    update_count = 1;
    
    break;

  case 1:
    
    if (Rupdate.buf_flag == true && Rupdate.U7E1 == true)
    {
      if (Rupdate.update_buf[1] == 0x00) //radar_update_buf[0] 0x11 0x12
      {
        //雷达进入刷写模式
        update_count = 2;
      }
      else if (Rupdate.update_buf[1] == 0x01)
      {                            //退出刷写模式
        radar_update_flag = false; //关闭升级
        update_count = 0;

      }
      Rupdate.U7E1 = false;
      Rupdate.buf_flag = false;
      memset(Rupdate.update_buf,0,8);
    }
    break;

  case 2:
    memset(obstacle_buf, 0, 8);

    obstacle_buf[0] = 0x01;
    obstacle_buf[1] = 0x09;
    obstacle_buf[2] = 0x09;
    obstacle_buf[3] = 0x04;
    obstacle_buf[4] = 0x00;
    obstacle_buf[5] = 0x09;
    obstacle_buf[6] = 0x02;
    obstacle_buf[7] = 0x06;

    HAL_Delay(5);
    can_send_msg_normal(obstacle_buf, 8, 0x7E2);

    update_count = 3;
    break;

  case 3:
    if (Rupdate.buf_flag == true && Rupdate.U7E3 == true)
    {
      if (Rupdate.update_buf[0] == 0xA6)
      { //格式化成功
        update_count = 4;
        update_i = 16;
        Update_ack_func(Update_buf[4], 200, &Update_buf[6]);
        uart_info.use_update_buf_flag = false;
        //防止连续升级
        check_sum = 0;
      }
      else if (Rupdate.update_buf[0] == 0xA8)
      { //格式化失败
        radar_update_flag = false;
        Rupdate.update_flag = false;
        update_count = 0;
      }
      Rupdate.U7E3 = false;
      Rupdate.buf_flag = false;
    }
    break;

  case 4:
    if (uart_info.use_update_buf_flag == true) 
    {
      if (Update_buf[5] == 201)
      {
        memcpy(&size_count, &Update_buf[6], 2);
        if (pack_count == size_count)
        { //防止发送相同包

          // 第一包前8字节不要 第二包开始发送全部128字节
          can_send_msg_normal(&Update_buf[update_i], 8, 0x7E4);

          for (for_i = 0; for_i < 8; for_i++) // 计算校验
          {
            check_sum += Update_buf[update_i + for_i];
          }
        
          update_i += 8;

          if (update_i >= 136)
          {
            update_i = 8;
            pack_count++;
            Update_ack_func(Update_buf[4], 201, &Update_buf[6]);
            memset(&Update_buf, 0, 150);
            uart_info.use_update_buf_flag = false;
          }
        }
        else if(pack_count > size_count)
        {
            Update_ack_func(Update_buf[4], 201, &Update_buf[6]);
            uart_info.use_update_buf_flag = false;
        }
      }
      else if (Update_buf[5] == 202)
      {
        //串口结束标志
        Update_ack_func(Update_buf[4], 202, &Update_buf[6]);
        update_count = 5;
        uart_info.use_update_buf_flag = false;
      }
      else if (Update_buf[5] == 200)
      {
        Update_ack_func(Update_buf[4], 200, &Update_buf[6]);
        uart_info.use_update_buf_flag = false;
      }
      else
      {
        uart_info.use_update_buf_flag = false;
      }
    }
    break;

  case 5:
    memset(obstacle_buf, 0, 8);

    obstacle_buf[0] = (check_sum >> 24) & 0xff;
    obstacle_buf[1] = (check_sum >> 16) & 0xff;
    obstacle_buf[2] = (check_sum >> 8) & 0xff;
    obstacle_buf[3] = (check_sum)&0xff;

    can_send_msg_normal(obstacle_buf, 4, 0x7E5);
    update_count = 6;

    break;

  case 6:
    if (Rupdate.buf_flag == true && Rupdate.U7E6 == true)
    {
      if (Rupdate.update_buf[0] == 0x66)
      {
        update_count = 0; //更新成功
      }
      else if (Rupdate.update_buf[0] == 0x88)
      {
        update_count = 0; //校验失败
      }
      else if (Rupdate.update_buf[0] == 0xff)
      {
        update_count = 0; //表示固件超出最大存储空间， bootloader将重新格式化扇区，并将版本回退到出厂固件??
      }
      pack_count = 1;
      Rupdate.buf_flag = false;
      radar_update_flag = false;
      Rupdate.update_flag = false;
      Rupdate.U7E6 = false;

      //重新发送版本信息
      uavr11_info.get_radar_ver_flag = false;
      uavr12_info.get_radar_ver_flag = false;
      uavr56_info.get_radar_ver_flag = false;
    }
    break;
  default:
    break;
  }

}


/**
  * @file    can_send_msg_normal
  * @brief   CAN口封装发送函数扩展帧 
  * @param   
  * @details 
  * @author  Zhang Sir 
 **/
void can_send_msg_normal(unsigned char *data, unsigned char length, unsigned int extid)
{
  //计算总帧数
  unsigned char frame_num = (length - 1) / 8 + 1;

  for (char sequence = 0; sequence < frame_num; sequence++)
  {
    uint8_t Data[8];
    TxHeader.ExtId = extid;
    TxHeader.RTR = CAN_RTR_DATA;
    TxHeader.IDE = CAN_ID_EXT;

    //标准帧是扩展帧的11
    TxHeader.StdId = TxHeader.ExtId & 0x1FFC0000;

    //如果剩下的长度大于8个字节，则此帧发8字节
    if (length - (sequence + 1) * 8 >= 0)
      TxHeader.DLC = 8;
    //否则发送剩余字节
    else
      TxHeader.DLC = length - sequence * 8;

    //字节赋值
    for (int i = 0; i < TxHeader.DLC; i++)
    {
      Data[i] = *(data + sequence * 8 + i);
    }
    if(planep.Candebug_flag == true || HAL_GetTick() < 10000)
       put_candata_to_rkfifo_tx(TxHeader, Data);
    //CAN共有三个发送邮箱，当三个邮箱都满时新数据无法发送
    //等待有可使用的邮箱，并发送出去
    uint32_t time2 = HAL_GetTick();
    uint32_t TxMailBox0 = 0;
    while (HAL_CAN_GetTxMailboxesFreeLevel(&hcan) == 0 && HAL_GetTick() - time2 < 2)
    {
    }
    HAL_CAN_AddTxMessage(&hcan, &TxHeader, Data, &TxMailBox0);
  }
}


/**
  * @file    can_send_msg_normalstd
  * @brief   CAN口封装发送函数标准帧 
  * @param   
  * @details 
  * @author  Zhang Sir 
 **/
void can_send_msg_normalstd(unsigned char *data, unsigned char length, unsigned int stdid)
{
  // 计算总帧数
  unsigned char frame_num = (length - 1) / 8 + 1;
  uint8_t Datas[8];
  for (char sequence = 0; sequence < frame_num; sequence++)
  {
    TxHeader.RTR = CAN_RTR_DATA;
    TxHeader.IDE = CAN_ID_STD;

    TxHeader.StdId = stdid;

    //如果剩下的长度大8个字节，则此帧发8个
    if (length - (sequence + 1) * 8 >= 0)
      TxHeader.DLC = 8;
    //否则发送剩余字节
    else
      TxHeader.DLC = length - sequence * 8;

    //字节赋节
    for (int i = 0; i < TxHeader.DLC; i++)
    {
      Datas[i] = *(data + sequence * 8 + i);
    }

    if(planep.Candebug_flag == true || HAL_GetTick() < 10000)
       put_candata_to_rkfifo_tx(TxHeader, Datas);

    //CAN共有三个发送邮箱，当三个邮箱都满时新数据无法发送
    //等待有可使用的邮箱，并发送出去
    uint32_t time2 = HAL_GetTick();
    uint32_t TxMailBox0 = 0;
    while (HAL_CAN_GetTxMailboxesFreeLevel(&hcan) == 0 && HAL_GetTick() - time2 < 2)
    {
    }
    HAL_CAN_AddTxMessage(&hcan, &TxHeader, Datas, &TxMailBox0);
  }
}

/**
  * @file    check_radar_link_status
  * @brief   检查雷达连接函数 
  * @param   
  * @details 
  * @author  Zhang Sir 
 **/
void check_radar_link_status()
{
  //Check_dev_link(&muniu_ter_info.Link,3000,(char *)&muniu_ter_info,sizeof(uavr_terrain));
  Check_dev_link(&mimo_ter_info.Link,3000,(char *)&mimo_ter_info,sizeof(uavr_terrain));
  Check_dev_link(&uavr56_info.Link,3000,(char *)&uavr56_info,sizeof(uavr_terrain));
  Check_dev_link(&uavr11_info.Link,3000,(char *)&uavr11_info,sizeof(uavr_obs));
  Check_dev_link(&uavr12_info.Link,3000,(char *)&uavr12_info,sizeof(uavr_obs));
  Check_dev_link(&mimo_f_info.Link,3000,(char *)&mimo_f_info,sizeof(uavr_obs));
  Check_dev_link(&mimo_b_info.Link,3000,(char *)&mimo_b_info,sizeof(uavr_obs));
  Check_dev_link(&DM_ter_info.Link,3000,(char *)&DM_ter_info,sizeof(uavr_terrain));
  Check_dev_link(&DM_f_info.Link,3000,(char *)&DM_f_info,sizeof(uavr_obs));
  //Check_dev_link(&mimo_360_info,3000,NULL,sizeof(uavr_obs));
  Check_dev_link(&DM_status,3000,NULL,sizeof(DM_RADAR));
  Check_dev_link(&Dev.Part_Tradar_Link,3000,(char *)&Dev.Part_radarT,sizeof(Part_Tradar));
  Check_dev_link(&Dev.Part_Fradar_Link,3000,(char *)&Dev.Part_radarF,sizeof(Part_Fradar));
}

void put_date_to_can(uint8_t *buf,uint8_t par1,uint8_t par2,uint8_t par3,uint8_t par4,uint8_t par5,
                        uint8_t par6,uint8_t par7,uint8_t par8)
{
  uint8_t *ptr = buf;
  *(ptr++) = par1;
  *(ptr++) = par2;
  *(ptr++) = par3;
  *(ptr++) = par4;
  *(ptr++) = par5;
  *(ptr++) = par6;
  *(ptr++) = par7;
  *(ptr++) = par8;
}

/**
  * @file    CAN1_SCE_IRQHandler
  * @brief   CAN错误中断处理 
  * @param   
  * @details 
  * @author  Zhang Sir 
 **/
void CAN1_SCE_IRQHandler()
{
  if (__HAL_CAN_GET_FLAG(&hcan, CAN_FLAG_BOF) != RESET)
  {
    __HAL_CAN_CLEAR_FLAG(&hcan, CAN_FLAG_ERRI);
  }
  if (__HAL_CAN_GET_FLAG(&hcan, CAN_FLAG_EPV) != RESET)
  {
    __HAL_CAN_CLEAR_FLAG(&hcan, CAN_FLAG_ERRI);
  }
  if (__HAL_CAN_GET_FLAG(&hcan, CAN_FLAG_EWG) != RESET)
  {
    __HAL_CAN_CLEAR_FLAG(&hcan, CAN_FLAG_ERRI);
  }

  __HAL_CAN_CLEAR_FLAG(&hcan, CAN_FLAG_ERRI);
}

