/*
 * File: BSPR_CellVoltCoord.c
 *
 * Code generated for Simulink model 'BSPR_CellVoltCoord'.
 *
 * Model version                  : 1.1172
 * Simulink Coder version         : 8.13 (R2017b) 24-Jul-2017
 * C/C++ source code generated on : Tue Jul 27 15:06:08 2021
 *
 * Target selection: ert.tlc
 * Embedded hardware selection: Infineon->TriCore
 * Code generation objectives:
 *    1. MISRA C:2012 guidelines
 *    2. Execution efficiency
 * Validation result: Not run
 */

#include "BSPR_CellVoltCoord.h"
#include "BSPR_CellVoltCoord_private.h"

/* Named constants for Chart: '<S90>/InputValueCalc' */
#define BSPR_CellVoltCoord_IN_Default_g ((uint8_T)1U)
#define BSPR_CellVoltCoord_IN_Update_a ((uint8_T)2U)

/* Exported data definition */

/* Volatile memory section */
#define ASW1_OEM_START_SEC_DEFAULT_RAM2_CLEARED_UNSPECIFIED
#include "Asw1_OEM_MemMap.h"

/* Definition for custom storage class: Volatile */
volatile uint8_T DFC_CellVoltRngHiErr_ASW; /* Max fault for cell voltage invalid signal */
volatile uint8_T DFC_CellVoltRngLoErr_ASW; /* Min fault for cell voltage invalid signal */
volatile uint8_T DFC_CellVoltVldMax_ASW;   /* Max fault for cell voltage invalid signal */
volatile uint8_T DFC_CellVoltVldMin_ASW;   /* Min fault for cell voltage invalid signal */

#define ASW1_OEM_STOP_SEC_DEFAULT_RAM2_CLEARED_UNSPECIFIED
#include "Asw1_OEM_MemMap.h"
#define ASW1_OEM_START_SEC_DEFAULT_RAM2_CLEARED_UNSPECIFIED
#include "Asw1_OEM_MemMap.h"

/* Definition for custom storage class: OutputSignal */
volatile boolean_T BSPR_bCellVoltElecNorm;/* Wire status for for cell voltage signal */
volatile boolean_T BSPR_bCellVoltRngHiErr;/* Wire status for for cell voltage signal */
volatile boolean_T BSPR_bCellVoltRngHiOk;/* Wire status for for cell voltage signal */
volatile boolean_T BSPR_bCellVoltRngLoErr;/* Wire status for for cell voltage signal */
volatile boolean_T BSPR_bCellVoltRngLoOk;/* Wire status for for cell voltage signal */
volatile uint8_T BSPR_cntCellSerNum;   /* number of cell series  in current battery package */
volatile uint8_T BSPR_cntSumCellVoltInvld;/* Calculated total invalid signal of cell voltage */
volatile uint8_T BSPR_idxCellVoltMax;  /* Calculated index of max cell voltage  */
volatile uint8_T BSPR_idxCellVoltMin;  /* Calculated index of min cell voltage */
volatile real32_T BSPR_uCellVoltAvrg;  /* Calculated average cell voltage */
volatile real32_T BSPR_uCellVoltMax;   /* Calculated max cell voltage */
volatile real32_T BSPR_uCellVoltMax2CAN;
volatile real32_T BSPR_uCellVoltMin;   /* Calculated min cell voltage */
volatile real32_T BSPR_uCellVoltMin2CAN;
volatile real32_T BSPR_uCellVolt_A[150];/* Calculated cell voltage after coordinating */

#define ASW1_OEM_STOP_SEC_DEFAULT_RAM2_CLEARED_UNSPECIFIED
#include "Asw1_OEM_MemMap.h"

/* Block signals (auto storage) */
B_BSPR_CellVoltCoord_T BSPR_CellVoltCoord_B;

/* Block states (auto storage) */
DW_BSPR_CellVoltCoord_T BSPR_CellVoltCoord_DW;

/*
 * Output and update for atomic system:
 *    '<S8>/VoltProcBMSDebounceStartZero1'
 *    '<S8>/VoltProcBMSDebounceStartZero2'
 */
void B_VoltProcBMSDebounceStartZero1(boolean_T rtu_Signal, real32_T
  rtu_Hearl2Fault, real32_T rtu_FaultStep, real32_T rtu_Fault2Heal, real32_T
  rtu_HealStep, B_VoltProcBMSDebounceStartZer_T *localB,
  DW_VoltProcBMSDebounceStartZe_T *localDW)
{
  boolean_T rtb_RelationalOperator_f;
  boolean_T rtb_RelationalOperator_iq;
  uint16_T rtb_Switch2_b;
  uint16_T rtb_Switch2_h2;
  real32_T tmp;
  if (localDW->StateName_g) {
    rtb_Switch2_b = 0U;
  } else {
    rtb_Switch2_b = localDW->StateName;
  }

  if (rtu_Signal) {
    tmp = (real32_T)fmod((real32_T)floor(rtu_FaultStep), 65536.0F);
    rtb_Switch2_b = (uint16_T)((uint32_T)(tmp < 0.0F ? (int32_T)(uint16_T)
      -(int16_T)(uint16_T)-tmp : (int32_T)(uint16_T)tmp) + rtb_Switch2_b);
    if (!(rtb_Switch2_b < 60000)) {
      rtb_Switch2_b = 60000U;
    }
  } else if (rtb_Switch2_b >= rtu_FaultStep) {
    tmp = (real32_T)fmod((real32_T)floor(rtu_FaultStep), 65536.0F);
    rtb_Switch2_b = (uint16_T)((uint32_T)rtb_Switch2_b - (tmp < 0.0F ? (int32_T)
      (uint16_T)-(int16_T)(uint16_T)-tmp : (int32_T)(uint16_T)tmp));
  } else {
    rtb_Switch2_b = 0U;
  }

  rtb_RelationalOperator_f = (rtb_Switch2_b > rtu_Hearl2Fault);
  if (localDW->StateName_k) {
    rtb_Switch2_h2 = 0U;
  } else {
    rtb_Switch2_h2 = localDW->StateName_n;
  }

  if (!rtu_Signal) {
    tmp = (real32_T)fmod((real32_T)floor(rtu_HealStep), 65536.0F);
    rtb_Switch2_h2 = (uint16_T)((uint32_T)(tmp < 0.0F ? (int32_T)(uint16_T)
      -(int16_T)(uint16_T)-tmp : (int32_T)(uint16_T)tmp) + rtb_Switch2_h2);
    if (!(rtb_Switch2_h2 < 60000)) {
      rtb_Switch2_h2 = 60000U;
    }
  } else if (rtb_Switch2_h2 >= rtu_HealStep) {
    tmp = (real32_T)fmod((real32_T)floor(rtu_HealStep), 65536.0F);
    rtb_Switch2_h2 = (uint16_T)((uint32_T)rtb_Switch2_h2 - (tmp < 0.0F ?
      (int32_T)(uint16_T)-(int16_T)(uint16_T)-tmp : (int32_T)(uint16_T)tmp));
  } else {
    rtb_Switch2_h2 = 0U;
  }

  rtb_RelationalOperator_iq = (rtb_Switch2_h2 > rtu_Fault2Heal);
  if (!localB->Switch) {
    localB->Switch = rtb_RelationalOperator_f;
  } else {
    localB->Switch = !rtb_RelationalOperator_iq;
  }

  localDW->StateName_g = rtb_RelationalOperator_f;
  localDW->StateName = rtb_Switch2_b;
  localDW->StateName_k = rtb_RelationalOperator_iq;
  localDW->StateName_n = rtb_Switch2_h2;
}

/*
 * Output and update for atomic system:
 *    '<S98>/InputValueCalc'
 *    '<S99>/InputValueCalc'
 */
void BSPR_CellVoltCoo_InputValueCalc(real32_T rtu_Input_Value, boolean_T
  rtu_Rige, const real32_T rtu_Old_Value[5], B_InputValueCalc_BSPR_CellVol_T
  *localB, DW_InputValueCalc_BSPR_CellVo_T *localDW)
{
  if (localDW->is_active_c5_BSPR_CellVoltCoord == 0U) {
    localDW->is_active_c5_BSPR_CellVoltCoord = 1U;
    localB->Output_Value[0] = rtu_Input_Value;
    localB->Output_Value[1] = rtu_Input_Value;
    localB->Output_Value[2] = rtu_Input_Value;
    localB->Output_Value[3] = rtu_Input_Value;
    localB->Output_Value[4] = rtu_Input_Value;
  } else if (rtu_Rige) {
    localB->Output_Value[0] = rtu_Input_Value;
    localB->Output_Value[1] = rtu_Old_Value[0];
    localB->Output_Value[2] = rtu_Old_Value[1];
    localB->Output_Value[3] = rtu_Old_Value[2];
    localB->Output_Value[4] = rtu_Old_Value[3];
  } else {
    localB->Output_Value[0] = rtu_Old_Value[0];
    localB->Output_Value[1] = rtu_Old_Value[1];
    localB->Output_Value[2] = rtu_Old_Value[2];
    localB->Output_Value[3] = rtu_Old_Value[3];
    localB->Output_Value[4] = rtu_Old_Value[4];
  }
}

/*
 * Output and update for atomic system:
 *    '<S98>/MaxValue_sort'
 *    '<S99>/MinValue_sort'
 */
void BSPR_CellVoltCoor_MaxValue_sort(const real32_T rtu_Input_Value[5], uint8_T
  rtu_Input_dim, B_MaxValue_sort_BSPR_CellVolt_T *localB,
  DW_MaxValue_sort_BSPR_CellVol_T *localDW)
{
  uint8_T i;
  uint8_T j;
  real32_T temp;
  if (localDW->is_active_c12_BSPR_CellVoltCoor == 0U) {
    localDW->is_active_c12_BSPR_CellVoltCoor = 1U;

    /* Input value sort from big to small */
    for (i = 0U; i < rtu_Input_dim; i++) {
      localB->Output_Value[i] = rtu_Input_Value[i];
    }

    for (i = 0U; i < rtu_Input_dim; i++) {
      temp = 0.0F;
      localB->Output_index[i] = 0U;
      for (j = 0U; j < rtu_Input_dim; j++) {
        if (localB->Output_Value[j] > temp) {
          temp = localB->Output_Value[j];
          localB->Output_index[i] = j;
        }
      }

      localB->Output_Value[localB->Output_index[i]] = 0.0F;
    }

    for (i = 0U; i < rtu_Input_dim; i++) {
      localB->Output_Value[i] = rtu_Input_Value[localB->Output_index[i]];
    }
  } else {
    for (i = 0U; i < rtu_Input_dim; i++) {
      localB->Output_Value[i] = rtu_Input_Value[i];
    }

    for (i = 0U; i < rtu_Input_dim; i++) {
      temp = 0.0F;
      localB->Output_index[i] = 0U;
      for (j = 0U; j < rtu_Input_dim; j++) {
        if (localB->Output_Value[j] > temp) {
          temp = localB->Output_Value[j];
          localB->Output_index[i] = j;
        }
      }

      localB->Output_Value[localB->Output_index[i]] = 0.0F;
    }

    for (i = 0U; i < rtu_Input_dim; i++) {
      localB->Output_Value[i] = rtu_Input_Value[localB->Output_index[i]];
    }
  }
}

/*
 * Output and update for atomic system:
 *    '<S112>/InputValueCalc1'
 *    '<S122>/InputValueCalc1'
 */
void BSPR_CellVoltCo_InputValueCalc1(uint8_T rtu_Input_Value, boolean_T rtu_Rige,
  const uint8_T rtu_Old_Value[5], B_InputValueCalc1_BSPR_CellVo_T *localB,
  DW_InputValueCalc1_BSPR_CellV_T *localDW)
{
  if (localDW->is_active_c8_BSPR_CellVoltCoord == 0U) {
    localDW->is_active_c8_BSPR_CellVoltCoord = 1U;
    localB->Output_Value[0] = rtu_Input_Value;
    localB->Output_Value[1] = rtu_Input_Value;
    localB->Output_Value[2] = rtu_Input_Value;
    localB->Output_Value[3] = rtu_Input_Value;
    localB->Output_Value[4] = rtu_Input_Value;
  } else if (rtu_Rige) {
    localB->Output_Value[0] = rtu_Input_Value;
    localB->Output_Value[1] = rtu_Old_Value[0];
    localB->Output_Value[2] = rtu_Old_Value[1];
    localB->Output_Value[3] = rtu_Old_Value[2];
    localB->Output_Value[4] = rtu_Old_Value[3];
  } else {
    localB->Output_Value[0] = rtu_Old_Value[0];
    localB->Output_Value[1] = rtu_Old_Value[1];
    localB->Output_Value[2] = rtu_Old_Value[2];
    localB->Output_Value[3] = rtu_Old_Value[3];
    localB->Output_Value[4] = rtu_Old_Value[4];
  }
}

/*
 * System initialize for atomic system:
 *    '<S129>/Unit Delay External IC1'
 *    '<S130>/Unit Delay External IC1'
 */
void BSPR__UnitDelayExternalIC1_Init(DW_UnitDelayExternalIC1_BSPR__T *localDW)
{
  localDW->StateName_j = 1U;
}

/*
 * System reset for atomic system:
 *    '<S129>/Unit Delay External IC1'
 *    '<S130>/Unit Delay External IC1'
 */
void BSPR_UnitDelayExternalIC1_Reset(DW_UnitDelayExternalIC1_BSPR__T *localDW)
{
  localDW->StateName_j = 1U;
  localDW->StateName = 0U;
}

/*
 * Outputs for atomic system:
 *    '<S129>/Unit Delay External IC1'
 *    '<S130>/Unit Delay External IC1'
 */
void BSPR_CellV_UnitDelayExternalIC1(uint16_T rtu_IC,
  B_UnitDelayExternalIC1_BSPR_C_T *localB, DW_UnitDelayExternalIC1_BSPR__T
  *localDW)
{
  if (localDW->StateName_j != 0) {
    localB->Init = rtu_IC;
  } else {
    localB->Init = localDW->StateName;
  }
}

/*
 * Update for atomic system:
 *    '<S129>/Unit Delay External IC1'
 *    '<S130>/Unit Delay External IC1'
 */
void BSP_UnitDelayExternalIC1_Update(uint16_T rtu_u,
  DW_UnitDelayExternalIC1_BSPR__T *localDW)
{
  localDW->StateName_j = 0U;
  localDW->StateName = rtu_u;
}

/* Model step function */
void BSPR_CellVoltCoord_step(void)
{
  int8_T n;
  uint8_T rtb_Product2;
  boolean_T rtb_RelationalOperator2[96];
  int16_T s159_iter;
  boolean_T rtb_Compare_d;
  uint32_T tmp;
  int32_T i;
  boolean_T rtb_Switch1_j[96];
  boolean_T rtb_Switch_g[96];
  real32_T rtb_UnitDelay21[5];
  real32_T rtb_Product_c;
  real32_T rtb_Product;
  uint8_T rtb_UnitDelay1[5];
  uint16_T rtb_LcCDP_IniOutputCellVolt_mV;
  boolean_T rtb_Compare_mc;
  real32_T rtb_LaCDP_CellVolt_mV[96];
  real32_T rtb_UnitDelay21_f[480];
  uint16_T StateName_o;
  uint8_T StateName_mo;
  uint8_T StateName_mr;
  uint16_T StateName_f;
  int32_T i_0;
  int32_T tmp_0;
  int32_T tmp_1;
  real32_T rtb_Compare_j;
  tmp = GaISP_AFECellVoltUpdErr_numb[0];
  for (i = 0; i < 111; i++) {
    tmp += GaISP_AFECellVoltUpdErr_numb[i + 1];
  }

  BSPR_bCellVoltElecNorm = ((uint8_T)tmp == 0);
  if (BSPR_CellVoltCoord_DW.is_active_c14_BSPR_CellVoltCoor == 0U) {
    BSPR_CellVoltCoord_DW.is_active_c14_BSPR_CellVoltCoor = 1U;
    BSPR_CellVoltCoord_DW.i_i = 0U;
    while (BSPR_CellVoltCoord_DW.i_i < BSPR_cntCellSerNum_C) {
      BSPR_CellVoltCoord_B.Output_Value_o[BSPR_CellVoltCoord_DW.i_i] =
        GaISP_CellVoltVld_numb[BSPR_CellVoltCoord_DW.i_i];
      i = BSPR_CellVoltCoord_DW.i_i + 1;
      if (i > 255) {
        i = 255;
      }

      BSPR_CellVoltCoord_DW.i_i = (uint8_T)i;
    }

    BSPR_CellVoltCoord_DW.i_i = 0U;
  } else {
    while (BSPR_CellVoltCoord_DW.i_i < BSPR_cntCellSerNum_C) {
      BSPR_CellVoltCoord_B.Output_Value_o[BSPR_CellVoltCoord_DW.i_i] =
        GaISP_CellVoltVld_numb[BSPR_CellVoltCoord_DW.i_i];
      i = BSPR_CellVoltCoord_DW.i_i + 1;
      if (i > 255) {
        i = 255;
      }

      BSPR_CellVoltCoord_DW.i_i = (uint8_T)i;
    }

    BSPR_CellVoltCoord_DW.i_i = 0U;
  }

  for (i_0 = 0; i_0 < 96; i_0++) {
    rtb_RelationalOperator2[i_0] = ((BSPR_CellVoltCoord_B.Output_Value_o[i_0] &
      97) == 0);
  }

  if (BSPR_CellVoltCoord_DW.is_active_c15_BSPR_CellVoltCoor == 0U) {
    BSPR_CellVoltCoord_DW.is_active_c15_BSPR_CellVoltCoor = 1U;
    BSPR_CellVoltCoord_DW.i_g = 0U;
    while (BSPR_CellVoltCoord_DW.i_g < BSPR_cntCellSerNum_C) {
      BSPR_CellVoltCoord_B.Output_Value_l[BSPR_CellVoltCoord_DW.i_g] =
        GaISP_CellVoltUpdCnt_numb[BSPR_CellVoltCoord_DW.i_g];
      i = BSPR_CellVoltCoord_DW.i_g + 1;
      if (i > 255) {
        i = 255;
      }

      BSPR_CellVoltCoord_DW.i_g = (uint8_T)i;
    }

    BSPR_CellVoltCoord_DW.i_g = 0U;
  } else {
    while (BSPR_CellVoltCoord_DW.i_g < BSPR_cntCellSerNum_C) {
      BSPR_CellVoltCoord_B.Output_Value_l[BSPR_CellVoltCoord_DW.i_g] =
        GaISP_CellVoltUpdCnt_numb[BSPR_CellVoltCoord_DW.i_g];
      i = BSPR_CellVoltCoord_DW.i_g + 1;
      if (i > 255) {
        i = 255;
      }

      BSPR_CellVoltCoord_DW.i_g = (uint8_T)i;
    }

    BSPR_CellVoltCoord_DW.i_g = 0U;
  }

  for (i_0 = 0; i_0 < 96; i_0++) {
    if (CbCDP_IgnorUpdateVld_flg) {
      rtb_Compare_d = ((BSPR_CellVoltCoord_B.Output_Value_o[i_0] & 97) != 0);
    } else {
      rtb_Compare_d = (((BSPR_CellVoltCoord_B.Output_Value_o[i_0] & 97) != 0) ||
                       (!(BSPR_CellVoltCoord_B.Output_Value_l[i_0] == 0)));
    }

    if (rtb_Compare_d) {
      rtb_LaCDP_CellVolt_mV[i_0] = BSPR_CellVoltCoord_DW.StateName[i_0];
    } else {
      rtb_LaCDP_CellVolt_mV[i_0] = COMR_uCellVolt_A[i_0];
    }

    if (CbCDP_IgnorUpdateVld_flg) {
      rtb_Switch_g[i_0] = rtb_RelationalOperator2[i_0];
    } else {
      rtb_Switch_g[i_0] = (rtb_RelationalOperator2[i_0] &&
                           (BSPR_CellVoltCoord_B.Output_Value_l[i_0] == 0));
    }

    rtb_Switch1_j[i_0] = rtb_Compare_d;
  }

  rtb_Product2 = 0U;
  for (s159_iter = 1; s159_iter <= BSPR_cntCellSerNum_C; s159_iter++) {
    i = (int32_T)((uint32_T)rtb_Switch_g[s159_iter - 1] + rtb_Product2);
    if ((uint32_T)i > 255U) {
      i = 255;
    }

    rtb_Product2 = (uint8_T)i;
    BSPR_CellVoltCoord_B.LbCDP_CellVoltUpdateVld_flg = ((uint8_T)i >=
      CcCDP_CellVoltUpdateThrd_numb);
  }

  if (BSPR_CellVoltCoord_B.LbCDP_CellVoltUpdateVld_flg) {
    if (!BSPR_CellVoltCoord_DW.MaxMin_CellVolt_Get_Sys_MODE) {
      BSPR_CellVoltCoord_DW.MaxMin_CellVolt_Get_Sys_MODE = true;
    }

    for (i_0 = 0; i_0 < 96; i_0++) {
      if (CbCDP_IgnorUpdateVld_flg) {
        rtb_Switch_g[i_0] = rtb_RelationalOperator2[i_0];
      } else {
        rtb_Switch_g[i_0] = (rtb_RelationalOperator2[i_0] &&
                             (BSPR_CellVoltCoord_B.Output_Value_l[i_0] == 0));
      }
    }

    StateName_o = 0U;
    rtb_Product2 = 0U;
    StateName_mo = 0U;
    BSPR_UnitDelayExternalIC1_Reset(&BSPR_CellVoltCoord_DW.UnitDelayExternalIC1);
    BSPR_UnitDelayExternalIC1_Reset
      (&BSPR_CellVoltCoord_DW.UnitDelayExternalIC1_i);
    StateName_mr = 1U;
    StateName_f = 0U;
    for (s159_iter = 1; s159_iter <= CcCDP_CellVoltConfig_numb; s159_iter++) {
      i = s159_iter - 1;
      rtb_Product = (real32_T)fmod((real32_T)floor(rtb_LaCDP_CellVolt_mV[i]),
        65536.0F);
      rtb_LcCDP_IniOutputCellVolt_mV = (uint16_T)(rtb_Product < 0.0F ? (int32_T)
        (uint16_T)-(int16_T)(uint16_T)-rtb_Product : (int32_T)(uint16_T)
        rtb_Product);
      BSPR_CellV_UnitDelayExternalIC1(rtb_LcCDP_IniOutputCellVolt_mV,
        &BSPR_CellVoltCoord_B.UnitDelayExternalIC1,
        &BSPR_CellVoltCoord_DW.UnitDelayExternalIC1);
      if ((BSPR_CellVoltCoord_B.UnitDelayExternalIC1.Init <=
           rtb_LcCDP_IniOutputCellVolt_mV) && rtb_Switch_g[i]) {
        BSPR_CellVoltCoord_B.LcCDP_MaxCellVolt_mV_f =
          rtb_LcCDP_IniOutputCellVolt_mV;
        BSPR_CellVoltCoord_B.LcCDP_MaxCellVoltNumb_numb = (uint8_T)s159_iter;
      } else {
        BSPR_CellVoltCoord_B.LcCDP_MaxCellVolt_mV_f = StateName_o;
        BSPR_CellVoltCoord_B.LcCDP_MaxCellVoltNumb_numb = rtb_Product2;
      }

      BSPR_CellV_UnitDelayExternalIC1(rtb_LcCDP_IniOutputCellVolt_mV,
        &BSPR_CellVoltCoord_B.UnitDelayExternalIC1_i,
        &BSPR_CellVoltCoord_DW.UnitDelayExternalIC1_i);
      if ((BSPR_CellVoltCoord_B.UnitDelayExternalIC1_i.Init >=
           rtb_LcCDP_IniOutputCellVolt_mV) && rtb_Switch_g[s159_iter - 1]) {
        BSPR_CellVoltCoord_B.LcCDP_MinCellVolt_mV_e =
          rtb_LcCDP_IniOutputCellVolt_mV;
        BSPR_CellVoltCoord_B.LcCDP_MinCellVoltNumb_numb = (uint8_T)s159_iter;
      } else {
        if (StateName_mr != 0) {
          BSPR_CellVoltCoord_B.LcCDP_MinCellVolt_mV_e = CcCDP_MinVoltIniVal_mV;
        } else {
          BSPR_CellVoltCoord_B.LcCDP_MinCellVolt_mV_e = StateName_f;
        }

        BSPR_CellVoltCoord_B.LcCDP_MinCellVoltNumb_numb = StateName_mo;
      }

      BSP_UnitDelayExternalIC1_Update
        (BSPR_CellVoltCoord_B.LcCDP_MaxCellVolt_mV_f,
         &BSPR_CellVoltCoord_DW.UnitDelayExternalIC1);
      StateName_o = BSPR_CellVoltCoord_B.LcCDP_MaxCellVolt_mV_f;
      rtb_Product2 = BSPR_CellVoltCoord_B.LcCDP_MaxCellVoltNumb_numb;
      BSP_UnitDelayExternalIC1_Update
        (BSPR_CellVoltCoord_B.LcCDP_MinCellVolt_mV_e,
         &BSPR_CellVoltCoord_DW.UnitDelayExternalIC1_i);
      StateName_mr = 0U;
      StateName_f = BSPR_CellVoltCoord_B.LcCDP_MinCellVolt_mV_e;
      StateName_mo = BSPR_CellVoltCoord_B.LcCDP_MinCellVoltNumb_numb;
    }

    BSPR_CellVoltCoord_B.LcCDP_MinCellVolt_mV =
      BSPR_CellVoltCoord_B.LcCDP_MinCellVolt_mV_e;
    BSPR_CellVoltCoord_B.LcCDP_MaxCellVolt_mV =
      BSPR_CellVoltCoord_B.LcCDP_MaxCellVolt_mV_f;
  } else {
    if (BSPR_CellVoltCoord_DW.MaxMin_CellVolt_Get_Sys_MODE) {
      BSPR_CellVoltCoord_DW.MaxMin_CellVolt_Get_Sys_MODE = false;
    }
  }

  for (i_0 = 0; i_0 < 5; i_0++) {
    rtb_UnitDelay21[i_0] = BSPR_CellVoltCoord_DW.StateName_m[i_0];
  }

  BSPR_CellVoltCoo_InputValueCalc(BSPR_CellVoltCoord_B.LcCDP_MaxCellVolt_mV,
    BSPR_CellVoltCoord_B.LbCDP_CellVoltUpdateVld_flg, rtb_UnitDelay21,
    &BSPR_CellVoltCoord_B.sf_InputValueCalc,
    &BSPR_CellVoltCoord_DW.sf_InputValueCalc);
  BSPR_CellVoltCoor_MaxValue_sort
    (BSPR_CellVoltCoord_B.sf_InputValueCalc.Output_Value,
     CcCDP_MaxCellVoltFilCfig_numb, &BSPR_CellVoltCoord_B.sf_MaxValue_sort,
     &BSPR_CellVoltCoord_DW.sf_MaxValue_sort);
  i = (int32_T)(CcCDP_MaxCellVoltFilCfig_numb + 1U);
  i_0 = i;
  if ((uint32_T)i > 255U) {
    i_0 = 255;
  }

  rtb_Product = (real32_T)fmod((real32_T)floor
    (BSPR_CellVoltCoord_B.sf_MaxValue_sort.Output_Value[(int32_T)((uint32_T)i_0 >>
    1) - 1]), 65536.0F);
  StateName_o = (uint16_T)(rtb_Product < 0.0F ? (int32_T)(uint16_T)-(int16_T)
    (uint16_T)-rtb_Product : (int32_T)(uint16_T)rtb_Product);
  if (CcCDP_CellVoltHiThrd_mV < StateName_o) {
    StateName_o = CcCDP_CellVoltHiThrd_mV;
  }

  if (!(StateName_o > CcCDP_CellVoltLoThrd_mV)) {
    StateName_o = CcCDP_CellVoltLoThrd_mV;
  }

  BSPR_uCellVoltMax = 0.001F * (real32_T)StateName_o;
  B_VoltProcBMSDebounceStartZero1(BSPR_bCellVoltElecNorm && (BSPR_uCellVoltMax >=
    BSPR_uCellVoltHi_C), BSPR_tiCellVoltRngHiChk_C, (real32_T)
    BSPR_tiCellVoltRngHiChkdT_C, BSPR_tiCellVoltRngHiChk_C, (real32_T)
    BSPR_tiCellVoltRngHiChkdT_C,
    &BSPR_CellVoltCoord_B.VoltProcBMSDebounceStartZero1,
    &BSPR_CellVoltCoord_DW.VoltProcBMSDebounceStartZero1);
  rtb_Compare_d = ((BSPR_cntCellVoltOutRngDiag_CW & 1) == 1);
  BSPR_bCellVoltRngHiErr = (rtb_Compare_d &&
    BSPR_CellVoltCoord_B.VoltProcBMSDebounceStartZero1.Switch);
  if (rtb_Compare_d) {
    BSPR_bCellVoltRngHiOk =
      !BSPR_CellVoltCoord_B.VoltProcBMSDebounceStartZero1.Switch;
  } else {
    BSPR_bCellVoltRngHiOk = true;
  }

  if (BSPR_CellVoltCoord_DW.Delay2_DSTATE_n) {
    rtb_Product2 = (uint8_T)~(uint8_T)((uint8_T)~DFC_CellVoltRngHiErr_ASW | 8U);
  } else if (BSPR_bCellVoltRngHiErr) {
    rtb_Product2 = (uint8_T)(DFC_CellVoltRngHiErr_ASW | 152U);
  } else if (BSPR_bCellVoltRngHiOk) {
    rtb_Product2 = (uint8_T)((uint8_T)~(uint8_T)((uint8_T)~DFC_CellVoltRngHiErr_ASW |
      16U) | 8U);
  } else {
    rtb_Product2 = BSPR_CellVoltCoord_DW.Delay3_DSTATE_l;
  }

  BSPR_CellVoltCoord_DW.Delay2_DSTATE_n = false;
  BSPR_CellVoltCoord_DW.Delay3_DSTATE_l = rtb_Product2;
  DFC_CellVoltRngHiErr_ASW = rtb_Product2;
  for (i_0 = 0; i_0 < 5; i_0++) {
    rtb_UnitDelay21[i_0] = BSPR_CellVoltCoord_DW.StateName_k[i_0];
  }

  BSPR_CellVoltCoo_InputValueCalc(BSPR_CellVoltCoord_B.LcCDP_MinCellVolt_mV,
    BSPR_CellVoltCoord_B.LbCDP_CellVoltUpdateVld_flg, rtb_UnitDelay21,
    &BSPR_CellVoltCoord_B.sf_InputValueCalc_d,
    &BSPR_CellVoltCoord_DW.sf_InputValueCalc_d);
  BSPR_CellVoltCoor_MaxValue_sort
    (BSPR_CellVoltCoord_B.sf_InputValueCalc_d.Output_Value,
     CcCDP_MaxCellVoltFilCfig_numb, &BSPR_CellVoltCoord_B.sf_MinValue_sort,
     &BSPR_CellVoltCoord_DW.sf_MinValue_sort);
  i_0 = i;
  if ((uint32_T)i > 255U) {
    i_0 = 255;
  }

  rtb_Product = (real32_T)fmod((real32_T)floor
    (BSPR_CellVoltCoord_B.sf_MinValue_sort.Output_Value[(int32_T)((uint32_T)i_0 >>
    1) - 1]), 65536.0F);
  StateName_o = (uint16_T)(rtb_Product < 0.0F ? (int32_T)(uint16_T)-(int16_T)
    (uint16_T)-rtb_Product : (int32_T)(uint16_T)rtb_Product);
  if (CcCDP_CellVoltHiThrd_mV < StateName_o) {
    StateName_o = CcCDP_CellVoltHiThrd_mV;
  }

  if (!(StateName_o > CcCDP_CellVoltLoThrd_mV)) {
    StateName_o = CcCDP_CellVoltLoThrd_mV;
  }

  BSPR_uCellVoltMin = 0.001F * (real32_T)StateName_o;
  B_VoltProcBMSDebounceStartZero1(BSPR_bCellVoltElecNorm && (BSPR_uCellVoltMin <=
    BSPR_uCellVoltLo_C), BSPR_tiCellVoltRngLoChk_C, (real32_T)
    BSPR_tiCellVoltRngLoChkdT_C, BSPR_tiCellVoltRngLoChk_C, (real32_T)
    BSPR_tiCellVoltRngLoChkdT_C,
    &BSPR_CellVoltCoord_B.VoltProcBMSDebounceStartZero2,
    &BSPR_CellVoltCoord_DW.VoltProcBMSDebounceStartZero2);
  rtb_Compare_d = ((BSPR_cntCellVoltOutRngDiag_CW & 2) == 2);
  BSPR_bCellVoltRngLoErr = (rtb_Compare_d &&
    BSPR_CellVoltCoord_B.VoltProcBMSDebounceStartZero2.Switch);
  if (rtb_Compare_d) {
    BSPR_bCellVoltRngLoOk =
      !BSPR_CellVoltCoord_B.VoltProcBMSDebounceStartZero2.Switch;
  } else {
    BSPR_bCellVoltRngLoOk = true;
  }

  if (BSPR_CellVoltCoord_DW.Delay2_DSTATE_e) {
    rtb_Product2 = (uint8_T)~(uint8_T)((uint8_T)~DFC_CellVoltRngLoErr_ASW | 8U);
  } else if (BSPR_bCellVoltRngLoErr) {
    rtb_Product2 = (uint8_T)(DFC_CellVoltRngLoErr_ASW | 152U);
  } else if (BSPR_bCellVoltRngLoOk) {
    rtb_Product2 = (uint8_T)((uint8_T)~(uint8_T)((uint8_T)~DFC_CellVoltRngLoErr_ASW |
      16U) | 8U);
  } else {
    rtb_Product2 = BSPR_CellVoltCoord_DW.Delay3_DSTATE_e;
  }

  BSPR_CellVoltCoord_DW.Delay2_DSTATE_e = false;
  BSPR_CellVoltCoord_DW.Delay3_DSTATE_e = rtb_Product2;
  DFC_CellVoltRngLoErr_ASW = rtb_Product2;
  tmp = (uint32_T)!rtb_RelationalOperator2[0];
  for (i_0 = 0; i_0 < 95; i_0++) {
    tmp += !rtb_RelationalOperator2[i_0 + 1];
  }

  BSPR_cntSumCellVoltInvld = (uint8_T)tmp;
  rtb_Compare_d = ((BSPR_cntSumCellVoltInvld >= 1) && (BSPR_cntSumCellVoltInvld <=
    BSPR_cntCellVoltVldHi_C) && BSPR_bCellVoltElecNorm);
  if (rtb_Compare_d) {
    rtb_Compare_j = BSPR_tiCellVoltInvldChkdT_C;
  } else {
    rtb_Compare_j = 0.0F;
  }

  if (BSPR_tiCellVoltInvldChk_C < BSPR_CellVoltCoord_DW.Delay_DSTATE_d) {
    rtb_Product = BSPR_tiCellVoltInvldChk_C;
  } else {
    rtb_Product = BSPR_CellVoltCoord_DW.Delay_DSTATE_d;
  }

  rtb_Product_c = (rtb_Compare_j + rtb_Product) * (real32_T)rtb_Compare_d;
  BSPR_CellVoltCoord_DW.Delay_DSTATE_d = rtb_Product_c;
  rtb_Compare_d = (BSPR_cntSumCellVoltInvld == 0);
  if (rtb_Compare_d) {
    rtb_Compare_j = BSPR_tiCellVoltInvldChkdT_C;
  } else {
    rtb_Compare_j = 0.0F;
  }

  if (BSPR_tiCellVoltInvldChk_C < BSPR_CellVoltCoord_DW.Delay_DSTATE) {
    rtb_Product = BSPR_tiCellVoltInvldChk_C;
  } else {
    rtb_Product = BSPR_CellVoltCoord_DW.Delay_DSTATE;
  }

  rtb_Product = (rtb_Compare_j + rtb_Product) * (real32_T)rtb_Compare_d;
  rtb_Compare_d = (rtb_Product > BSPR_tiCellVoltInvldChk_C);
  BSPR_CellVoltCoord_DW.Delay_DSTATE = rtb_Product;
  if (BSPR_CellVoltCoord_DW.Delay2_DSTATE_k) {
    rtb_Product2 = (uint8_T)~(uint8_T)((uint8_T)~DFC_CellVoltVldMin_ASW | 8U);
  } else {
    rtb_Compare_mc = ((BSPR_cntCellVoltVldDiag_CW & 1) == 1);
    if (rtb_Compare_mc && (rtb_Product_c > BSPR_tiCellVoltInvldChk_C)) {
      rtb_Product2 = (uint8_T)(DFC_CellVoltVldMin_ASW | 152U);
    } else if ((!rtb_Compare_mc) || rtb_Compare_d) {
      rtb_Product2 = (uint8_T)((uint8_T)~(uint8_T)((uint8_T)~DFC_CellVoltVldMin_ASW |
        16U) | 8U);
    } else {
      rtb_Product2 = BSPR_CellVoltCoord_DW.Delay3_DSTATE_o;
    }
  }

  BSPR_CellVoltCoord_DW.Delay2_DSTATE_k = false;
  BSPR_CellVoltCoord_DW.Delay3_DSTATE_o = rtb_Product2;
  DFC_CellVoltVldMin_ASW = rtb_Product2;
  rtb_Compare_mc = ((BSPR_cntSumCellVoltInvld > BSPR_cntCellVoltVldHi_C) &&
                    BSPR_bCellVoltElecNorm);
  if (rtb_Compare_mc) {
    rtb_Product_c = BSPR_tiCellVoltInvldChkdT_C;
  } else {
    rtb_Product_c = 0.0F;
  }

  if (BSPR_tiCellVoltInvldChk_C < BSPR_CellVoltCoord_DW.Delay_DSTATE_k) {
    rtb_Product = BSPR_tiCellVoltInvldChk_C;
  } else {
    rtb_Product = BSPR_CellVoltCoord_DW.Delay_DSTATE_k;
  }

  rtb_Product_c = (rtb_Product_c + rtb_Product) * (real32_T)rtb_Compare_mc;
  BSPR_CellVoltCoord_DW.Delay_DSTATE_k = rtb_Product_c;
  if (BSPR_CellVoltCoord_DW.Delay2_DSTATE) {
    rtb_Product2 = (uint8_T)~(uint8_T)((uint8_T)~DFC_CellVoltVldMax_ASW | 8U);
  } else {
    rtb_Compare_mc = ((BSPR_cntCellVoltVldDiag_CW & 2) == 2);
    if (rtb_Compare_mc && (rtb_Product_c > BSPR_tiCellVoltInvldChk_C)) {
      rtb_Product2 = (uint8_T)(DFC_CellVoltVldMax_ASW | 152U);
    } else if ((!rtb_Compare_mc) || rtb_Compare_d) {
      rtb_Product2 = (uint8_T)((uint8_T)~(uint8_T)((uint8_T)~DFC_CellVoltVldMax_ASW |
        16U) | 8U);
    } else {
      rtb_Product2 = BSPR_CellVoltCoord_DW.Delay3_DSTATE;
    }
  }

  BSPR_CellVoltCoord_DW.Delay2_DSTATE = false;
  BSPR_CellVoltCoord_DW.Delay3_DSTATE = rtb_Product2;
  DFC_CellVoltVldMax_ASW = rtb_Product2;
  for (i_0 = 0; i_0 < 96; i_0++) {
    rtb_Switch1_j[i_0] = !rtb_Switch1_j[i_0];
  }

  for (i_0 = 0; i_0 < 480; i_0++) {
    rtb_UnitDelay21_f[i_0] = BSPR_CellVoltCoord_B.Output_Value_a[i_0];
  }

  if (BSPR_CellVoltCoord_DW.is_active_c2_BSPR_CellVoltCoord == 0U) {
    BSPR_CellVoltCoord_DW.is_active_c2_BSPR_CellVoltCoord = 1U;
    BSPR_CellVoltCoord_DW.is_c2_BSPR_CellVoltCoord =
      BSPR_CellVoltCoord_IN_Default_g;

    /* {Output_Value[0] = Input_Value;
       Output_Value[1] = Input_Value;
       Output_Value[2] = Input_Value;
       Output_Value[3] = Input_Value;
       Output_Value[4] = Input_Value;} */
    BSPR_CellVoltCoord_DW.i = 0U;
    while (BSPR_CellVoltCoord_DW.i < BSPR_cntCellSerNum_C) {
      BSPR_CellVoltCoord_B.Output_Value_a[5 * BSPR_CellVoltCoord_DW.i] =
        rtb_LaCDP_CellVolt_mV[BSPR_CellVoltCoord_DW.i];
      BSPR_CellVoltCoord_B.Output_Value_a[1 + 5 * BSPR_CellVoltCoord_DW.i] =
        rtb_LaCDP_CellVolt_mV[BSPR_CellVoltCoord_DW.i];
      BSPR_CellVoltCoord_B.Output_Value_a[2 + 5 * BSPR_CellVoltCoord_DW.i] =
        rtb_LaCDP_CellVolt_mV[BSPR_CellVoltCoord_DW.i];
      BSPR_CellVoltCoord_B.Output_Value_a[3 + 5 * BSPR_CellVoltCoord_DW.i] =
        rtb_LaCDP_CellVolt_mV[BSPR_CellVoltCoord_DW.i];
      BSPR_CellVoltCoord_B.Output_Value_a[4 + 5 * BSPR_CellVoltCoord_DW.i] =
        rtb_LaCDP_CellVolt_mV[BSPR_CellVoltCoord_DW.i];
      i_0 = BSPR_CellVoltCoord_DW.i + 1;
      if (i_0 > 255) {
        i_0 = 255;
      }

      BSPR_CellVoltCoord_DW.i = (uint8_T)i_0;
    }
  } else if (BSPR_CellVoltCoord_DW.is_c2_BSPR_CellVoltCoord ==
             BSPR_CellVoltCoord_IN_Default_g) {
    if (BSPR_CellVoltCoord_DW.i > BSPR_cntCellSerNum_C - 1) {
      BSPR_CellVoltCoord_DW.is_c2_BSPR_CellVoltCoord =
        BSPR_CellVoltCoord_IN_Update_a;
      BSPR_CellVoltCoord_DW.i = 0U;
      while (BSPR_CellVoltCoord_DW.i < BSPR_cntCellSerNum_C) {
        if (rtb_Switch1_j[BSPR_CellVoltCoord_DW.i]) {
          BSPR_CellVoltCoord_B.Output_Value_a[5 * BSPR_CellVoltCoord_DW.i] =
            rtb_LaCDP_CellVolt_mV[BSPR_CellVoltCoord_DW.i];
          BSPR_CellVoltCoord_B.Output_Value_a[1 + 5 * BSPR_CellVoltCoord_DW.i] =
            rtb_UnitDelay21_f[5 * BSPR_CellVoltCoord_DW.i];
          BSPR_CellVoltCoord_B.Output_Value_a[2 + 5 * BSPR_CellVoltCoord_DW.i] =
            rtb_UnitDelay21_f[5 * BSPR_CellVoltCoord_DW.i + 1];
          BSPR_CellVoltCoord_B.Output_Value_a[3 + 5 * BSPR_CellVoltCoord_DW.i] =
            rtb_UnitDelay21_f[5 * BSPR_CellVoltCoord_DW.i + 2];
          BSPR_CellVoltCoord_B.Output_Value_a[4 + 5 * BSPR_CellVoltCoord_DW.i] =
            rtb_UnitDelay21_f[5 * BSPR_CellVoltCoord_DW.i + 3];
        } else {
          BSPR_CellVoltCoord_B.Output_Value_a[5 * BSPR_CellVoltCoord_DW.i] =
            rtb_UnitDelay21_f[5 * BSPR_CellVoltCoord_DW.i];
          BSPR_CellVoltCoord_B.Output_Value_a[1 + 5 * BSPR_CellVoltCoord_DW.i] =
            rtb_UnitDelay21_f[5 * BSPR_CellVoltCoord_DW.i + 1];
          BSPR_CellVoltCoord_B.Output_Value_a[2 + 5 * BSPR_CellVoltCoord_DW.i] =
            rtb_UnitDelay21_f[5 * BSPR_CellVoltCoord_DW.i + 2];
          BSPR_CellVoltCoord_B.Output_Value_a[3 + 5 * BSPR_CellVoltCoord_DW.i] =
            rtb_UnitDelay21_f[5 * BSPR_CellVoltCoord_DW.i + 3];
          BSPR_CellVoltCoord_B.Output_Value_a[4 + 5 * BSPR_CellVoltCoord_DW.i] =
            rtb_UnitDelay21_f[5 * BSPR_CellVoltCoord_DW.i + 4];
        }

        i_0 = BSPR_CellVoltCoord_DW.i + 1;
        if (i_0 > 255) {
          i_0 = 255;
        }

        BSPR_CellVoltCoord_DW.i = (uint8_T)i_0;
      }

      BSPR_CellVoltCoord_DW.i = 0U;
    }
  } else {
    while (BSPR_CellVoltCoord_DW.i < BSPR_cntCellSerNum_C) {
      if (rtb_Switch1_j[BSPR_CellVoltCoord_DW.i]) {
        BSPR_CellVoltCoord_B.Output_Value_a[5 * BSPR_CellVoltCoord_DW.i] =
          rtb_LaCDP_CellVolt_mV[BSPR_CellVoltCoord_DW.i];
        BSPR_CellVoltCoord_B.Output_Value_a[1 + 5 * BSPR_CellVoltCoord_DW.i] =
          rtb_UnitDelay21_f[5 * BSPR_CellVoltCoord_DW.i];
        BSPR_CellVoltCoord_B.Output_Value_a[2 + 5 * BSPR_CellVoltCoord_DW.i] =
          rtb_UnitDelay21_f[5 * BSPR_CellVoltCoord_DW.i + 1];
        BSPR_CellVoltCoord_B.Output_Value_a[3 + 5 * BSPR_CellVoltCoord_DW.i] =
          rtb_UnitDelay21_f[5 * BSPR_CellVoltCoord_DW.i + 2];
        BSPR_CellVoltCoord_B.Output_Value_a[4 + 5 * BSPR_CellVoltCoord_DW.i] =
          rtb_UnitDelay21_f[5 * BSPR_CellVoltCoord_DW.i + 3];
      } else {
        i_0 = 5 * BSPR_CellVoltCoord_DW.i;
        BSPR_CellVoltCoord_B.Output_Value_a[i_0] = rtb_UnitDelay21_f[i_0];
        BSPR_CellVoltCoord_B.Output_Value_a[1 + i_0] = rtb_UnitDelay21_f[i_0 + 1];
        BSPR_CellVoltCoord_B.Output_Value_a[2 + i_0] = rtb_UnitDelay21_f[i_0 + 2];
        BSPR_CellVoltCoord_B.Output_Value_a[3 + i_0] = rtb_UnitDelay21_f[i_0 + 3];
        BSPR_CellVoltCoord_B.Output_Value_a[4 + i_0] = rtb_UnitDelay21_f[i_0 + 4];
      }

      i_0 = BSPR_CellVoltCoord_DW.i + 1;
      if (i_0 > 255) {
        i_0 = 255;
      }

      BSPR_CellVoltCoord_DW.i = (uint8_T)i_0;
    }

    BSPR_CellVoltCoord_DW.i = 0U;
  }

  if (BSPR_CellVoltCoord_DW.is_active_c13_BSPR_CellVoltCoor == 0U) {
    BSPR_CellVoltCoord_DW.is_active_c13_BSPR_CellVoltCoor = 1U;

    /* Input value sort from big to small */
    rtb_Product2 = 0U;
    StateName_mo = 0U;
    while (rtb_Product2 < CcCDP_CellVoltFilCfig_numb) {
      while (StateName_mo < BSPR_cntCellSerNum_C) {
        i_0 = 5 * StateName_mo;
        BSPR_CellVoltCoord_B.Output_Value[rtb_Product2 + i_0] =
          BSPR_CellVoltCoord_B.Output_Value_a[i_0 + rtb_Product2];
        StateName_mo++;
      }

      rtb_Product2++;
      StateName_mo = 0U;
    }

    if (CcCDP_CellVoltFilCfig_numb != 1) {
      i_0 = CcCDP_CellVoltFilCfig_numb - 1;
      if (i_0 < 0) {
        i_0 = 0;
      }

      StateName_mo = (uint8_T)i_0;
      rtb_Product2 = 0U;
      while (rtb_Product2 < BSPR_cntCellSerNum_C) {
        while (StateName_mo >= 1) {
          i_0 = StateName_mo - 1;
          tmp_0 = i_0;
          if (i_0 > 127) {
            tmp_0 = 127;
          }

          for (n = (int8_T)tmp_0; n >= 0; n--) {
            tmp_0 = 5 * rtb_Product2;
            if (BSPR_CellVoltCoord_B.Output_Value[tmp_0 + n] >
                BSPR_CellVoltCoord_B.Output_Value[tmp_0 + StateName_mo]) {
              rtb_Product_c = BSPR_CellVoltCoord_B.Output_Value[5 * rtb_Product2
                + n];
              BSPR_CellVoltCoord_B.Output_Value[n + tmp_0] =
                BSPR_CellVoltCoord_B.Output_Value[5 * rtb_Product2 +
                StateName_mo];
              BSPR_CellVoltCoord_B.Output_Value[StateName_mo + tmp_0] =
                rtb_Product_c;
            }
          }

          StateName_mo = (uint8_T)i_0;
        }

        rtb_Product2++;
        i_0 = CcCDP_CellVoltFilCfig_numb - 1;
        if (i_0 < 0) {
          i_0 = 0;
        }

        StateName_mo = (uint8_T)i_0;
      }
    }

    i_0 = CcCDP_CellVoltFilCfig_numb - 1;
    i_0 = ((i_0 < 0) + i_0) >> 1;
    if (i_0 < 0) {
      i_0 = 0;
    }

    for (rtb_Product2 = 0U; rtb_Product2 < BSPR_cntCellSerNum_C; rtb_Product2++)
    {
      BSPR_CellVoltCoord_B.TargetOutput_Value[rtb_Product2] =
        BSPR_CellVoltCoord_B.Output_Value[5 * rtb_Product2 + (uint8_T)i_0];
    }
  } else {
    rtb_Product2 = 0U;
    StateName_mo = 0U;
    while (rtb_Product2 < CcCDP_CellVoltFilCfig_numb) {
      while (StateName_mo < BSPR_cntCellSerNum_C) {
        i_0 = 5 * StateName_mo;
        BSPR_CellVoltCoord_B.Output_Value[rtb_Product2 + i_0] =
          BSPR_CellVoltCoord_B.Output_Value_a[i_0 + rtb_Product2];
        StateName_mo++;
      }

      rtb_Product2++;
      StateName_mo = 0U;
    }

    if (CcCDP_CellVoltFilCfig_numb != 1) {
      i_0 = CcCDP_CellVoltFilCfig_numb - 1;
      tmp_0 = i_0;
      if (i_0 < 0) {
        tmp_0 = 0;
      }

      StateName_mo = (uint8_T)tmp_0;
      rtb_Product2 = 0U;
      while (rtb_Product2 < BSPR_cntCellSerNum_C) {
        while (StateName_mo >= 1) {
          tmp_0 = StateName_mo - 1;
          tmp_1 = tmp_0;
          if (tmp_0 > 127) {
            tmp_1 = 127;
          }

          for (n = (int8_T)tmp_1; n >= 0; n--) {
            tmp_1 = 5 * rtb_Product2;
            if (BSPR_CellVoltCoord_B.Output_Value[tmp_1 + n] >
                BSPR_CellVoltCoord_B.Output_Value[tmp_1 + StateName_mo]) {
              rtb_Product_c = BSPR_CellVoltCoord_B.Output_Value[5 * rtb_Product2
                + n];
              BSPR_CellVoltCoord_B.Output_Value[n + tmp_1] =
                BSPR_CellVoltCoord_B.Output_Value[5 * rtb_Product2 +
                StateName_mo];
              BSPR_CellVoltCoord_B.Output_Value[StateName_mo + tmp_1] =
                rtb_Product_c;
            }
          }

          StateName_mo = (uint8_T)tmp_0;
        }

        rtb_Product2++;
        tmp_0 = i_0;
        if (i_0 < 0) {
          tmp_0 = 0;
        }

        StateName_mo = (uint8_T)tmp_0;
      }
    }

    i_0 = CcCDP_CellVoltFilCfig_numb - 1;
    i_0 = ((i_0 < 0) + i_0) >> 1;
    if (i_0 < 0) {
      i_0 = 0;
    }

    for (rtb_Product2 = 0U; rtb_Product2 < BSPR_cntCellSerNum_C; rtb_Product2++)
    {
      BSPR_CellVoltCoord_B.TargetOutput_Value[rtb_Product2] =
        BSPR_CellVoltCoord_B.Output_Value[5 * rtb_Product2 + (uint8_T)i_0];
    }
  }

  if (BSPR_CellVoltCoord_DW.is_active_c17_BSPR_CellVoltCoor == 0U) {
    BSPR_CellVoltCoord_DW.is_active_c17_BSPR_CellVoltCoor = 1U;
    BSPR_CellVoltCoord_DW.i_c = 0U;
    while (BSPR_CellVoltCoord_DW.i_c < BSPR_cntCellSerNum_C) {
      BSPR_CellVoltCoord_B.Output_Value_d[BSPR_CellVoltCoord_DW.i_c] =
        BSPR_CellVoltCoord_B.TargetOutput_Value[BSPR_CellVoltCoord_DW.i_c] /
        1000.0F;
      i_0 = BSPR_CellVoltCoord_DW.i_c + 1;
      if (i_0 > 255) {
        i_0 = 255;
      }

      BSPR_CellVoltCoord_DW.i_c = (uint8_T)i_0;
    }

    BSPR_CellVoltCoord_DW.i_c = 0U;
  } else {
    while (BSPR_CellVoltCoord_DW.i_c < BSPR_cntCellSerNum_C) {
      BSPR_CellVoltCoord_B.Output_Value_d[BSPR_CellVoltCoord_DW.i_c] =
        BSPR_CellVoltCoord_B.TargetOutput_Value[BSPR_CellVoltCoord_DW.i_c] /
        1000.0F;
      i_0 = BSPR_CellVoltCoord_DW.i_c + 1;
      if (i_0 > 255) {
        i_0 = 255;
      }

      BSPR_CellVoltCoord_DW.i_c = (uint8_T)i_0;
    }

    BSPR_CellVoltCoord_DW.i_c = 0U;
  }

  BSPR_uCellVoltMax2CAN = BSPR_uCellVoltMax;
  BSPR_uCellVoltMin2CAN = BSPR_uCellVoltMin;
  for (i_0 = 0; i_0 < 5; i_0++) {
    rtb_UnitDelay1[i_0] = BSPR_CellVoltCoord_DW.StateName_b[i_0];
  }

  BSPR_CellVoltCo_InputValueCalc1
    (BSPR_CellVoltCoord_B.LcCDP_MaxCellVoltNumb_numb,
     BSPR_CellVoltCoord_B.LbCDP_CellVoltUpdateVld_flg, rtb_UnitDelay1,
     &BSPR_CellVoltCoord_B.sf_InputValueCalc1,
     &BSPR_CellVoltCoord_DW.sf_InputValueCalc1);
  i_0 = i;
  if ((uint32_T)i > 255U) {
    i_0 = 255;
  }

  BSPR_idxCellVoltMax =
    BSPR_CellVoltCoord_B.sf_InputValueCalc1.Output_Value[BSPR_CellVoltCoord_B.sf_MaxValue_sort.Output_index
    [(int32_T)((uint32_T)i_0 >> 1) - 1]];
  for (i_0 = 0; i_0 < 5; i_0++) {
    rtb_UnitDelay1[i_0] = BSPR_CellVoltCoord_DW.StateName_n[i_0];
  }

  BSPR_CellVoltCo_InputValueCalc1
    (BSPR_CellVoltCoord_B.LcCDP_MinCellVoltNumb_numb,
     BSPR_CellVoltCoord_B.LbCDP_CellVoltUpdateVld_flg, rtb_UnitDelay1,
     &BSPR_CellVoltCoord_B.sf_InputValueCalc1_f,
     &BSPR_CellVoltCoord_DW.sf_InputValueCalc1_f);
  if ((uint32_T)i > 255U) {
    i = 255;
  }

  BSPR_idxCellVoltMin =
    BSPR_CellVoltCoord_B.sf_InputValueCalc1_f.Output_Value[BSPR_CellVoltCoord_B.sf_MinValue_sort.Output_index
    [(int32_T)((uint32_T)i >> 1) - 1]];
  rtb_Compare_d = true;
  for (s159_iter = 1; s159_iter <= BSPR_cntCellSerNum_C; s159_iter++) {
    BSPR_CellVoltCoord_B.LbCDP_SumCellVoltVld_flg =
      (rtb_RelationalOperator2[s159_iter - 1] && rtb_Compare_d);
    rtb_Compare_d = BSPR_CellVoltCoord_B.LbCDP_SumCellVoltVld_flg;
  }

  if (BSPR_CellVoltCoord_B.LbCDP_SumCellVoltVld_flg) {
    if (!BSPR_CellVoltCoord_DW.Sum_CellVolt_Get_Sys_MODE) {
      BSPR_CellVoltCoord_DW.Sum_CellVolt_Get_Sys_MODE = true;
    }

    rtb_Product_c = 0.0F;
    for (s159_iter = 1; s159_iter <= CcCDP_CellVoltConfig_numb; s159_iter++) {
      BSPR_CellVoltCoord_B.LcCDP_SumCellVolt_mV =
        rtb_LaCDP_CellVolt_mV[s159_iter - 1] + rtb_Product_c;
      rtb_Product_c = BSPR_CellVoltCoord_B.LcCDP_SumCellVolt_mV;
    }

    BSPR_CellVoltCoord_B.GcCDP_AvgCellVolt_mV =
      BSPR_CellVoltCoord_B.LcCDP_SumCellVolt_mV / (real32_T)
      CcCDP_CellVoltConfig_numb;
  } else {
    if (BSPR_CellVoltCoord_DW.Sum_CellVolt_Get_Sys_MODE) {
      BSPR_CellVoltCoord_DW.Sum_CellVolt_Get_Sys_MODE = false;
    }
  }

  BSPR_uCellVoltAvrg = 0.001F * BSPR_CellVoltCoord_B.GcCDP_AvgCellVolt_mV;
  if (BSPR_cntCellSerNum_C < 150) {
    BSPR_cntCellSerNum = BSPR_cntCellSerNum_C;
  } else {
    BSPR_cntCellSerNum = 150U;
  }

  for (i_0 = 0; i_0 < 150; i_0++) {
    BSPR_uCellVolt_A[i_0] = BSPR_CellVoltCoord_B.Output_Value_d[i_0];
  }

  for (i_0 = 0; i_0 < 96; i_0++) {
    BSPR_CellVoltCoord_DW.StateName[i_0] = rtb_LaCDP_CellVolt_mV[i_0];
  }

  for (i_0 = 0; i_0 < 5; i_0++) {
    BSPR_CellVoltCoord_DW.StateName_m[i_0] =
      BSPR_CellVoltCoord_B.sf_InputValueCalc.Output_Value[i_0];
    BSPR_CellVoltCoord_DW.StateName_k[i_0] =
      BSPR_CellVoltCoord_B.sf_InputValueCalc_d.Output_Value[i_0];
    BSPR_CellVoltCoord_DW.StateName_b[i_0] =
      BSPR_CellVoltCoord_B.sf_InputValueCalc1.Output_Value[i_0];
    BSPR_CellVoltCoord_DW.StateName_n[i_0] =
      BSPR_CellVoltCoord_B.sf_InputValueCalc1_f.Output_Value[i_0];
  }
}

/* Model initialize function */
void BSPR_CellVoltCoord_initialize(void)
{
  /* Registration code */

  /* block I/O */

  /* custom signals */
  BSPR_uCellVoltMax = 0.0F;
  BSPR_uCellVoltMin = 0.0F;
  BSPR_uCellVoltMax2CAN = 0.0F;
  BSPR_uCellVoltMin2CAN = 0.0F;
  BSPR_uCellVoltAvrg = 0.0F;

  {
    int32_T i;
    for (i = 0; i < 150; i++) {
      BSPR_uCellVolt_A[i] = 0.0F;
    }
  }

  BSPR_idxCellVoltMax = ((uint8_T)0U);
  BSPR_idxCellVoltMin = ((uint8_T)0U);
  BSPR_cntCellSerNum = ((uint8_T)0U);
  BSPR__UnitDelayExternalIC1_Init(&BSPR_CellVoltCoord_DW.UnitDelayExternalIC1);
  BSPR__UnitDelayExternalIC1_Init(&BSPR_CellVoltCoord_DW.UnitDelayExternalIC1_i);
  BSPR_CellVoltCoord_DW.Delay2_DSTATE_n = true;
  BSPR_CellVoltCoord_DW.Delay2_DSTATE_e = true;
  BSPR_CellVoltCoord_DW.Delay2_DSTATE_k = true;
  BSPR_CellVoltCoord_DW.Delay2_DSTATE = true;
}

/*
 * File trailer for generated code.
 *
 * [EOF]
 */
