/*
 * File: BSER_CellBalcCtl.c
 *
 * Code generated for Simulink model 'BSER_CellBalcCtl'.
 *
 * Model version                  : D6_0
 * Simulink Coder version         : 8.11 (R2016b) 25-Aug-2016
 * C/C++ source code generated on : Tue Dec 15 10:14:15 2020
 *
 * 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 "BSER_CellBalcCtl.h"
#include "BSER_CellBalcCtl_private.h"

/* Named constants for Chart: '<S3>/BSER_CellBalCtrlRunTi' */
#define BSER_CellB_IN_NO_ACTIVE_CHILD_f ((uint8_T)0U)
#define BSER_CellBalcCtl_IN_End_Stt    ((uint8_T)1U)
#define BSER_CellBalcCtl_IN_Init_Stt   ((uint8_T)2U)

/* Named constants for Chart: '<S7>/BSER_CellBalSeqChart' */
#define BSER_CellBalcC_IN_BalCourse_Stt ((uint8_T)1U)
#define BSER_CellBalcCt_IN_BalPause_Stt ((uint8_T)1U)
#define BSER_CellBalcCt_IN_BalStart_Stt ((uint8_T)2U)
#define BSER_CellBalcCtl_IN_BalInit_Stt ((uint8_T)2U)

/* Named constants for Chart: '<S19>/Chart' */
#define BSER_CellBal_IN_BalDrv_Complete ((uint8_T)2U)
#define BSER_CellBalcC_IN_BalDrv_Active ((uint8_T)1U)
#define BSER_CellBalc_IN_BalDrv_Default ((uint8_T)3U)

/* 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_AFETempHighFlt_ASW;
volatile uint8_T DFC_BalCirOpenFlt_ASW;
volatile uint8_T DFC_BalCirShortFlt_ASW;
volatile uint8_T DFC_BalTempHighFlt_ASW;

#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 BSER_bBalDrvInteAct;
volatile boolean_T BSER_bBalFltNrm;
volatile boolean_T BSER_bBalaVald;
volatile boolean_T BSER_bCellBalACMode;
volatile boolean_T BSER_bCellBalDCMode;
volatile boolean_T BSER_bCellBalNrmEna;
volatile boolean_T BSER_bCellBalOffDna;
volatile boolean_T BSER_bCellBalOnEna;
volatile boolean_T BSER_bCellBalTempDvtEna;
volatile boolean_T BSER_bCellBalTempNrmEna;
volatile boolean_T BSER_bCellBalVoltNrmEna;
volatile boolean_T BSER_bCmcAFETempVld[8];
volatile boolean_T BSER_bCmcBordTempVld[8];
volatile boolean_T BSER_bCmuBalEna_A[8];
volatile boolean_T BSER_bCmuTNormFlg;
volatile boolean_T BSER_bFaultLvlEna;
volatile boolean_T BSER_bWkBalEna;
volatile uint8_T BSER_stBal;
volatile uint8_T BSER_stBalErrorCode;
volatile uint8_T BSER_stBalInteChgFlg;
volatile uint8_T BSER_stCellBal[96];
volatile real32_T BSER_tCmcAFETemp_A[8];
volatile real32_T BSER_tCmcBordMaxTemp;
volatile real32_T BSER_tCmcBordTempAll_A[16];
volatile real32_T BSER_tCmcBordTemp_A[8];

#define ASW1_OEM_STOP_SEC_DEFAULT_RAM2_CLEARED_UNSPECIFIED
#include "Asw1_OEM_MemMap.h"

/* Block signals (auto storage) */
B_BSER_CellBalcCtl_T BSER_CellBalcCtl_B;

/* Block states (auto storage) */
DW_BSER_CellBalcCtl_T BSER_CellBalcCtl_DW;

/* Forward declaration for local functions */
static void BSER_CellBalcC_Interception_Vec(const real32_T x[96], real32_T v[12],
  DW_BSER_CellBalSortCal_BSER_C_T *localDW);
static void BSER_CellBalcCtl_BalEnaCal(const real32_T Vx[12], const boolean_T
  Vb[8], const boolean_T CmcTempHiLvl1_A[8], const boolean_T CmcTempHiLvl2_A[8],
  uint8_T i_index, boolean_T EnVy[12], real32_T rtu_COMR_uMinCellVolt);
static void BSER_CellBalcCtl_EnaAdd(const boolean_T VxIn[12], boolean_T Vy[96],
  DW_BSER_CellBalSortCal_BSER_C_T *localDW);

/* Output and update for function-call system: '<S7>/BSER_CellBalInitCal' */
void BSER_CellBa_BSER_CellBalInitCal(boolean_T rty_BSER_bCellBalInit_A[96])
{
  memset(&rty_BSER_bCellBalInit_A[0], 0, 96U * sizeof(boolean_T));
}

/* Output and update for function-call system: '<S7>/BSER_CellBalWaitCal' */
void BSER_CellBa_BSER_CellBalWaitCal(boolean_T rty_BSER_bCellBalWait_A[96])
{
  memset(&rty_BSER_bCellBalWait_A[0], 0, 96U * sizeof(boolean_T));
}

/* Function for Chart: '<S12>/BSER_CellBalSort' */
static void BSER_CellBalcC_Interception_Vec(const real32_T x[96], real32_T v[12],
  DW_BSER_CellBalSortCal_BSER_C_T *localDW)
{
  uint8_T j;
  uint32_T qY;
  int32_T tmp;
  for (j = 0U; j < 12U; j++) {
    qY = localDW->i - 1U;
    if (qY > localDW->i) {
      qY = 0U;
    }

    tmp = (int32_T)(qY * 12U);
    if ((uint32_T)tmp > 255U) {
      tmp = 255;
    }

    tmp = (int32_T)(((uint32_T)tmp + j) + 1U);
    if ((uint32_T)tmp > 255U) {
      tmp = 255;
    }

    v[j] = x[tmp - 1];
  }
}

/* Function for Chart: '<S12>/BSER_CellBalSort' */
static void BSER_CellBalcCtl_BalEnaCal(const real32_T Vx[12], const boolean_T
  Vb[8], const boolean_T CmcTempHiLvl1_A[8], const boolean_T CmcTempHiLvl2_A[8],
  uint8_T i_index, boolean_T EnVy[12], real32_T rtu_COMR_uMinCellVolt)
{
  uint8_T j;
  real32_T x[12];
  int32_T iidx[12];
  uint8_T indexConv[12];
  uint8_T BalEnSum;
  int32_T i;
  int32_T tmp;
  for (i = 0; i < 12; i++) {
    x[i] = Vx[i];
  }

  mglflnohppppbiek_sort(x, iidx);
  for (i = 0; i < 12; i++) {
    tmp = iidx[i];
    if (iidx[i] < 0) {
      tmp = 0;
    } else {
      if (iidx[i] > 255) {
        tmp = 255;
      }
    }

    indexConv[i] = (uint8_T)tmp;
    EnVy[i] = false;
  }

  BalEnSum = 0U;

  /*   i=i_index; */
  if (Vb[i_index - 1]) {
    j = 1U;
    while ((j < 13) && (!(x[j - 1] - rtu_COMR_uMinCellVolt <
                          BSER_uCellVoltDvtBalOff_C))) {
      if (CmcTempHiLvl2_A[i_index - 1]) {
        if (BalEnSum < BSER_BalEnSumThrd3_C) {
          EnVy[indexConv[j - 1] - 1] = true;
          BalEnSum++;
        } else {
          EnVy[indexConv[j - 1] - 1] = false;
        }

        /*               continue; */
      } else if (CmcTempHiLvl1_A[i_index - 1]) {
        if (BalEnSum < BSER_BalEnSumThrd2_C) {
          EnVy[indexConv[j - 1] - 1] = true;
          BalEnSum++;
        } else {
          EnVy[indexConv[j - 1] - 1] = false;
        }

        /*               continue;   */
      } else if (BalEnSum < BSER_BalEnSumThrd1_C) {
        EnVy[indexConv[j - 1] - 1] = true;
        BalEnSum++;
      } else {
        EnVy[indexConv[j - 1] - 1] = false;
      }

      /* once deltaV<=VbalOff, BREAK */
      /*           if (y(j)-COMR_uMinCellVolt)<BSER_uCellVoltDvtBalOff_C */
      /*               break;      */
      /*           %special situation 1: No.1 */
      /*           elseif j==1 */
      /*               EnVy(indexConv(j))=true; */
      /*               continue; */
      /*           %special situation 2: index_sort 1 */
      /*           elseif (indexConv(j)==1)  */
      /*               if (EnVy(indexConv(j)+1)==false) */
      /*                   EnVy(indexConv(j))=true; */
      /*               end */
      /*           %special situation 3: index_sort 12 */
      /*           elseif (indexConv(j)==12) */
      /*               if (EnVy(indexConv(j)-1)==false) */
      /*                   EnVy(indexConv(j))=true; */
      /*               end */
      /*           %special situation 4: index_sort Mid && front/back FALSE */
      /*           elseif ((EnVy(indexConv(j)-1)==false) && (EnVy(indexConv(j)+1)==false)) */
      /*                   EnVy(indexConv(j))=true; */
      /*           else */
      /*               EnVy(indexConv(j))=false; */
      /*           end */
      j++;
    }
  }
}

/* Function for Chart: '<S12>/BSER_CellBalSort' */
static void BSER_CellBalcCtl_EnaAdd(const boolean_T VxIn[12], boolean_T Vy[96],
  DW_BSER_CellBalSortCal_BSER_C_T *localDW)
{
  uint8_T k;
  uint32_T qY;
  int32_T tmp;

  /*   EnaOut(((ii-1)*12+1):((ii-1)*12+12))=EnaIn; */
  for (k = 0U; k < 12U; k++) {
    qY = localDW->i - 1U;
    if (qY > localDW->i) {
      qY = 0U;
    }

    tmp = (int32_T)(qY * 12U);
    if ((uint32_T)tmp > 255U) {
      tmp = 255;
    }

    tmp = (int32_T)(((uint32_T)tmp + k) + 1U);
    if ((uint32_T)tmp > 255U) {
      tmp = 255;
    }

    Vy[tmp - 1] = VxIn[k];
  }
}

/* System reset for function-call system: '<S7>/BSER_CellBalSortCal' */
void BSER__BSER_CellBalSortCal_Reset(B_BSER_CellBalSortCal_BSER_Ce_T *localB,
  DW_BSER_CellBalSortCal_BSER_C_T *localDW)
{
  int32_T i;
  for (i = 0; i < 8; i++) {
    localDW->UnitDelay_DSTATE_c[i] = false;
    localDW->UnitDelay_DSTATE[i] = false;
  }

  localDW->i = 0U;
  memset(&localB->BSER_bCellBalEna_A[0], 0, 96U * sizeof(boolean_T));
}

/* Output and update for function-call system: '<S7>/BSER_CellBalSortCal' */
void BSER_CellBa_BSER_CellBalSortCal(const real32_T rtu_COMR_uCellVolt_A[96],
  real32_T rtu_COMR_uMinCellVolt, const boolean_T rtu_BSER_bCmuBalEna_A[8],
  const real32_T rtu_BSER_tCmcBordTemp_A[8], const real32_T
  rtu_BSER_tCmcAFETemp_A[8], boolean_T rty_BSER_bCellBalEna_A[96],
  B_BSER_CellBalSortCal_BSER_Ce_T *localB, DW_BSER_CellBalSortCal_BSER_C_T
  *localDW)
{
  real32_T Vw[12];
  boolean_T EnaV[12];
  real32_T rtb_Add_a;
  real32_T rtb_Add1_c;
  boolean_T Switch1_m[8];
  boolean_T Switch1[8];
  int32_T i;
  real32_T rtb_MinMax_ol;
  boolean_T Switch1_m_0;
  boolean_T Switch1_0;
  rtb_Add_a = BSER_tCmcBordTempThrd1_C - BSER_tCmcBordTempHys_C;
  rtb_Add1_c = BSER_tCmcBordTempThrd2_C - BSER_tCmcBordTempHys_C;
  for (i = 0; i < 8; i++) {
    if (rtu_BSER_tCmcBordTemp_A[i] >= rtu_BSER_tCmcAFETemp_A[i]) {
      rtb_MinMax_ol = rtu_BSER_tCmcBordTemp_A[i];
    } else {
      rtb_MinMax_ol = rtu_BSER_tCmcAFETemp_A[i];
    }

    Switch1_m_0 = ((rtb_MinMax_ol > BSER_tCmcBordTempThrd1_C) ||
                   ((!(rtb_MinMax_ol < rtb_Add_a)) &&
                    localDW->UnitDelay_DSTATE_c[i]));
    localDW->UnitDelay_DSTATE_c[i] = Switch1_m_0;
    Switch1_0 = ((rtb_MinMax_ol > BSER_tCmcBordTempThrd2_C) || ((!(rtb_MinMax_ol
      < rtb_Add1_c)) && localDW->UnitDelay_DSTATE[i]));
    localDW->UnitDelay_DSTATE[i] = Switch1_0;
    Switch1_m[i] = Switch1_m_0;
    Switch1[i] = Switch1_0;
  }

  /*  sort cycle for all CSCs  */
  localDW->i = 1U;
  while (localDW->i <= BSER_cntModuleNum_C) {
    BSER_CellBalcC_Interception_Vec(rtu_COMR_uCellVolt_A, Vw, localDW);

    /*  Cell Balance sorting for single CSC  */
    BSER_CellBalcCtl_BalEnaCal(Vw, rtu_BSER_bCmuBalEna_A, Switch1_m, Switch1,
      localDW->i, EnaV, rtu_COMR_uMinCellVolt);
    BSER_CellBalcCtl_EnaAdd(EnaV, localB->BSER_bCellBalEna_A, localDW);
    i = localDW->i + 1;
    if (i > 255) {
      i = 255;
    }

    localDW->i = (uint8_T)i;
  }

  for (i = 0; i < 96; i++) {
    rty_BSER_bCellBalEna_A[i] = localB->BSER_bCellBalEna_A[i];
  }
}

/*
 * System reset for atomic system:
 *    '<S9>/CBCBMSDebounceStartZero1'
 *    '<S9>/CBCBMSDebounceStartZero2'
 */
void CBCBMSDebounceStartZero1_Reset(DW_CBCBMSDebounceStartZero1_B_T *localDW)
{
  localDW->StateName_k = false;
  localDW->StateName_h = false;
  localDW->StateName = 0U;
  localDW->StateName_d = false;
  localDW->StateName_f = 0U;
}

/*
 * Output and update for atomic system:
 *    '<S9>/CBCBMSDebounceStartZero1'
 *    '<S9>/CBCBMSDebounceStartZero2'
 */
void BSER_C_CBCBMSDebounceStartZero1(boolean_T rtu_Signal, real32_T
  rtu_Hearl2Fault, real32_T rtu_FaultStep, real32_T rtu_Fault2Heal, real32_T
  rtu_HealStep, B_CBCBMSDebounceStartZero1_BS_T *localB,
  DW_CBCBMSDebounceStartZero1_B_T *localDW)
{
  boolean_T rtb_RelationalOperator_k;
  boolean_T rtb_RelationalOperator_a;
  uint16_T rtb_Switch2_o0;
  uint16_T rtb_Switch2_ne;
  real32_T tmp;
  if (localDW->StateName_h) {
    rtb_Switch2_o0 = 0U;
  } else {
    rtb_Switch2_o0 = localDW->StateName;
  }

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

  rtb_RelationalOperator_k = (rtb_Switch2_o0 > rtu_Hearl2Fault);
  if (localDW->StateName_d) {
    rtb_Switch2_ne = 0U;
  } else {
    rtb_Switch2_ne = localDW->StateName_f;
  }

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

  rtb_RelationalOperator_a = (rtb_Switch2_ne > rtu_Fault2Heal);
  if (!localDW->StateName_k) {
    localB->Switch = (rtb_RelationalOperator_k || localDW->StateName_k);
  } else {
    localB->Switch = ((!rtb_RelationalOperator_a) && localDW->StateName_k);
  }

  localDW->StateName_k = localB->Switch;
  localDW->StateName_h = rtb_RelationalOperator_k;
  localDW->StateName = rtb_Switch2_o0;
  localDW->StateName_d = rtb_RelationalOperator_a;
  localDW->StateName_f = rtb_Switch2_ne;
}

/* System initialize for atomic system: '<Root>/BSER_CellBalCtl' */
void BSER_CellB_BSER_CellBalCtl_Init(void)
{
  BSER_CellBalcCtl_B.BSER_bBalaVald_h = enmBalRunSts_INACTIVE;
  BSER_CellBalcCtl_DW.Delay2_DSTATE_e = true;
  BSER_CellBalcCtl_DW.Delay2_DSTATE_a = true;
  BSER_CellBalcCtl_DW.Delay2_DSTATE_l = true;
  BSER_CellBalcCtl_DW.Delay2_DSTATE = true;
  BSER_CellBalcCtl_DW.Delay1_DSTATE_c = true;
  BSER_CellBalcCtl_B.stBalDrvInteFlg = enmBalDrvInteSts_INACTIVE;
}

/* Output and update for atomic system: '<Root>/BSER_CellBalCtl' */
void BSER_CellBalcCt_BSER_CellBalCtl(void)
{
  boolean_T rtb_RelationalOperator1;
  boolean_T rtb_LogicalOperator6_h;
  real32_T rtb_Add3;
  real32_T rtb_Add1;
  boolean_T rtb_LogicalOperator3;
  uint32_T tmp;
  uint8_T rtb_SumofElements2;
  boolean_T rtb_RelationalOperator3[8];
  boolean_T rtb_RelationalOperator2[8];
  boolean_T rtb_Switch1_f[8];
  boolean_T rtb_Switch1_k[8];
  int8_T rtb_DataTypeConversion6[8];
  int8_T rtb_DataTypeConversion2[8];
  int32_T i;
  real32_T rtb_RelationalOperator1_0;
  if (BSER_CellBalcCtl_DW.is_active_c3_BSER_CellBalcCtl == 0U) {
    BSER_CellBalcCtl_DW.is_active_c3_BSER_CellBalcCtl = 1U;
    BSER_CellBalcCtl_DW.is_c3_BSER_CellBalcCtl = BSER_CellBalcCtl_IN_Init_Stt;
    BSER_CellBalcCtl_DW.i_j = 0.0F;
    BSER_CellBalcCtl_B.BSER_bBalaVald_h = enmBalRunSts_INACTIVE;
  } else {
    switch (BSER_CellBalcCtl_DW.is_c3_BSER_CellBalcCtl) {
     case BSER_CellBalcCtl_IN_End_Stt:
      break;

     default:
      if ((BSER_CellBalcCtl_DW.i_j > BSER_cntBalStartThres_C) &&
          BSER_BalFuncEna_C) {
        BSER_CellBalcCtl_DW.is_c3_BSER_CellBalcCtl = BSER_CellBalcCtl_IN_End_Stt;
        BSER_CellBalcCtl_DW.i_j = 0.0F;
        BSER_CellBalcCtl_B.BSER_bBalaVald_h = enmBalRunSts_ACTIVE;
      } else {
        BSER_CellBalcCtl_DW.i_j++;
      }
      break;
    }
  }

  BSER_bBalaVald = (BSER_CellBalcCtl_B.BSER_bBalaVald_h != 0U);
  if (BSER_bBalaVald) {
    if (!BSER_CellBalcCtl_DW.BSER_CellBalCtlCal_MODE) {
      BSER_CellBalcCtl_DW.Delay_DSTATE_p = false;
      BSER_CellBalcCtl_DW.Delay1_DSTATE = false;
      BSER_CellBalcCtl_DW.Delay_DSTATE_f = 0.0F;
      BSER_CellBalcCtl_DW.Delay2_DSTATE_e = true;
      BSER_CellBalcCtl_DW.Delay2_DSTATE_a = true;
      for (i = 0; i < 8; i++) {
        BSER_CellBalcCtl_DW.UnitDelay_DSTATE_o[i] = false;
      }

      CBCBMSDebounceStartZero1_Reset
        (&BSER_CellBalcCtl_DW.CBCBMSDebounceStartZero1);
      BSER_CellBalcCtl_DW.Delay2_DSTATE_l = true;
      for (i = 0; i < 8; i++) {
        BSER_CellBalcCtl_DW.UnitDelay_DSTATE_g[i] = false;
      }

      CBCBMSDebounceStartZero1_Reset
        (&BSER_CellBalcCtl_DW.CBCBMSDebounceStartZero2);
      BSER_CellBalcCtl_DW.Delay2_DSTATE = true;
      for (i = 0; i < 8; i++) {
        BSER_CellBalcCtl_DW.UnitDelay_DSTATE_n[i] = false;
        BSER_CellBalcCtl_DW.UnitDelay_DSTATE_p[i] = false;
      }

      BSER_CellBalcCtl_DW.Delay1_DSTATE_c = true;
      BSER_CellBalcCtl_DW.Delay_DSTATE_hc = 0.0F;
      BSER_CellBalcCtl_DW.is_active_c4_BSER_CellBalcCtl = 0U;
      BSER_CellBalcCtl_DW.is_c4_BSER_CellBalcCtl =
        BSER_CellB_IN_NO_ACTIVE_CHILD_f;
      BSER_CellBalcCtl_DW.Cnt = 0.0F;
      BSER_CellBalcCtl_B.stBalDrvInteFlg = enmBalDrvInteSts_INACTIVE;
      BSER_CellBalcCtl_B.bBalDrvInteAct = false;
      BSER_CellBalcCtl_DW.Delay_DSTATE_je = 0.0F;
      BSER_CellBalcCtl_DW.Delay_DSTATE_h = 0.0F;
      BSER_CellBalcCtl_DW.Delay_DSTATE_j = 0.0F;
      BSER_CellBalcCtl_DW.Delay_DSTATE_i = 0.0F;
      BSER_CellBalcCtl_DW.Delay_DSTATE_g = 0.0F;
      BSER_CellBalcCtl_DW.is_BalCourse_Stt = BSER_CellB_IN_NO_ACTIVE_CHILD_f;
      BSER_CellBalcCtl_DW.is_active_c2_BSER_CellBalcCtl = 0U;
      BSER_CellBalcCtl_DW.is_c2_BSER_CellBalcCtl =
        BSER_CellB_IN_NO_ACTIVE_CHILD_f;
      BSER_CellBalcCtl_DW.i = 0.0F;
      BSER_CellBalcCtl_DW.cntBalOn = 0.0F;
      BSER_CellBalcCtl_B.BSER_bCellBalFlg = false;
      BSER__BSER_CellBalSortCal_Reset(&BSER_CellBalcCtl_B.BSER_CellBalSortCal,
        &BSER_CellBalcCtl_DW.BSER_CellBalSortCal);
      BSER_CellBalcCtl_DW.BSER_CellBalCtlCal_MODE = true;
    }

    for (i = 0; i < 8; i++) {
      rtb_RelationalOperator3[i] = (COMR_stBalCirOpenFlt_A[i] == 0);
    }

    if (BSER_CellBalcCtl_DW.Delay2_DSTATE_e) {
      rtb_SumofElements2 = (uint8_T)~(uint8_T)((uint8_T)~DFC_BalCirOpenFlt_ASW | 8U);
    } else {
      for (i = 0; i < 8; i++) {
        rtb_DataTypeConversion2[i] = (int8_T)!rtb_RelationalOperator3[i];
      }

      tmp = (uint32_T)rtb_DataTypeConversion2[0];
      for (i = 0; i < 7; i++) {
        tmp += rtb_DataTypeConversion2[i + 1];
      }

      if ((uint8_T)tmp != 0) {
        rtb_SumofElements2 = (uint8_T)(DFC_BalCirOpenFlt_ASW | 152U);
      } else {
        rtb_SumofElements2 = (uint8_T)((uint8_T)~(uint8_T)((uint8_T)
          ~DFC_BalCirOpenFlt_ASW | 16U) | 8U);
      }
    }

    BSER_CellBalcCtl_DW.Delay2_DSTATE_e = false;
    DFC_BalCirOpenFlt_ASW = rtb_SumofElements2;
    for (i = 0; i < 8; i++) {
      rtb_RelationalOperator2[i] = (COMR_stBalCirShortFlt_A[i] == 0);
    }

    if (BSER_CellBalcCtl_DW.Delay2_DSTATE_a) {
      rtb_SumofElements2 = (uint8_T)~(uint8_T)((uint8_T)~DFC_BalCirShortFlt_ASW | 8U);
    } else {
      for (i = 0; i < 8; i++) {
        rtb_DataTypeConversion2[i] = (int8_T)!rtb_RelationalOperator2[i];
      }

      tmp = (uint32_T)rtb_DataTypeConversion2[0];
      for (i = 0; i < 7; i++) {
        tmp += rtb_DataTypeConversion2[i + 1];
      }

      if ((uint8_T)tmp != 0) {
        rtb_SumofElements2 = (uint8_T)(DFC_BalCirShortFlt_ASW | 152U);
      } else {
        rtb_SumofElements2 = (uint8_T)((uint8_T)~(uint8_T)((uint8_T)
          ~DFC_BalCirShortFlt_ASW | 16U) | 8U);
      }
    }

    BSER_CellBalcCtl_DW.Delay2_DSTATE_a = false;
    DFC_BalCirShortFlt_ASW = rtb_SumofElements2;
    rtb_Add3 = BSER_tCMUPCBTempHi_C - BSER_tCMUPCBTempHiHys_C;
    for (i = 0; i < 8; i++) {
      rtb_RelationalOperator1 = ((BSER_tCmcBordTemp_A[i] > BSER_tCMUPCBTempHi_C)
        || ((!(BSER_tCmcBordTemp_A[i] < rtb_Add3)) &&
            BSER_CellBalcCtl_DW.UnitDelay_DSTATE_o[i]));
      BSER_CellBalcCtl_DW.UnitDelay_DSTATE_o[i] = rtb_RelationalOperator1;
      rtb_DataTypeConversion2[i] = (int8_T)rtb_RelationalOperator1;
    }

    tmp = (uint32_T)rtb_DataTypeConversion2[0];
    for (i = 0; i < 7; i++) {
      tmp += rtb_DataTypeConversion2[i + 1];
    }

    BSER_C_CBCBMSDebounceStartZero1((uint8_T)tmp != 0, BSER_tiCMUPCBTempHi_C,
      BSER_tiCellBalCtlCalTask_C, BSER_tiCMUPCBTempHi_C,
      BSER_tiCellBalCtlCalTask_C, &BSER_CellBalcCtl_B.CBCBMSDebounceStartZero1,
      &BSER_CellBalcCtl_DW.CBCBMSDebounceStartZero1);
    if (BSER_CellBalcCtl_DW.Delay2_DSTATE_l) {
      DFC_BalTempHighFlt_ASW = (uint8_T)~(uint8_T)((uint8_T)~DFC_BalTempHighFlt_ASW | 8U);
    } else if (BSER_CellBalcCtl_B.CBCBMSDebounceStartZero1.Switch) {
      DFC_BalTempHighFlt_ASW = (uint8_T)(DFC_BalTempHighFlt_ASW | 152U);
    } else {
      DFC_BalTempHighFlt_ASW = (uint8_T)((uint8_T)~(uint8_T)((uint8_T)
        ~DFC_BalTempHighFlt_ASW | 16U) | 8U);
    }

    BSER_CellBalcCtl_DW.Delay2_DSTATE_l = false;
    rtb_Add3 = BSER_tCMUAFETempHi_C - BSER_tCMUAFETempHiHys_C;
    for (i = 0; i < 8; i++) {
      BSER_tCmcAFETemp_A[i] = 0.032F * (real32_T)GaBSW_VAFETmp_numb[i] - 273.0F;
      rtb_RelationalOperator1 = ((BSER_tCmcAFETemp_A[i] > BSER_tCMUAFETempHi_C) ||
        ((!(BSER_tCmcAFETemp_A[i] < rtb_Add3)) &&
         BSER_CellBalcCtl_DW.UnitDelay_DSTATE_g[i]));
      BSER_CellBalcCtl_DW.UnitDelay_DSTATE_g[i] = rtb_RelationalOperator1;
      rtb_DataTypeConversion2[i] = (int8_T)rtb_RelationalOperator1;
    }

    tmp = (uint32_T)rtb_DataTypeConversion2[0];
    for (i = 0; i < 7; i++) {
      tmp += rtb_DataTypeConversion2[i + 1];
    }

    BSER_C_CBCBMSDebounceStartZero1((uint8_T)tmp != 0, BSER_tiCMUAFETempHi_C,
      BSER_tiCellBalCtlCalTask_C, BSER_tiCMUAFETempHi_C,
      BSER_tiCellBalCtlCalTask_C, &BSER_CellBalcCtl_B.CBCBMSDebounceStartZero2,
      &BSER_CellBalcCtl_DW.CBCBMSDebounceStartZero2);
    if (BSER_CellBalcCtl_DW.Delay2_DSTATE) {
      DFC_AFETempHighFlt_ASW = (uint8_T)~(uint8_T)((uint8_T)~DFC_AFETempHighFlt_ASW | 8U);
    } else if (BSER_CellBalcCtl_B.CBCBMSDebounceStartZero2.Switch) {
      DFC_AFETempHighFlt_ASW = (uint8_T)(DFC_AFETempHighFlt_ASW | 152U);
    } else {
      DFC_AFETempHighFlt_ASW = (uint8_T)((uint8_T)~(uint8_T)((uint8_T)
        ~DFC_AFETempHighFlt_ASW | 16U) | 8U);
    }

    BSER_CellBalcCtl_DW.Delay2_DSTATE = false;
    rtb_Add3 = BSER_tCMUPCBTempMax_C - BSER_tCMUPCBTempMaxHys_C;
    rtb_Add1 = BSER_tCMUAFETempMax_C - BSER_tCMUAFETempMaxHys_C;
    for (i = 0; i < 8; i++) {
      rtb_RelationalOperator1 = ((BSER_tCmcBordTemp_A[i] > BSER_tCMUPCBTempMax_C)
        || ((!(BSER_tCmcBordTemp_A[i] < rtb_Add3)) &&
            BSER_CellBalcCtl_DW.UnitDelay_DSTATE_n[i]));
      BSER_CellBalcCtl_DW.UnitDelay_DSTATE_n[i] = rtb_RelationalOperator1;
      rtb_DataTypeConversion2[i] = (int8_T)!rtb_RelationalOperator1;
      rtb_LogicalOperator3 = ((BSER_tCmcAFETemp_A[i] > BSER_tCMUAFETempMax_C) ||
        ((!(BSER_tCmcAFETemp_A[i] < rtb_Add1)) &&
         BSER_CellBalcCtl_DW.UnitDelay_DSTATE_p[i]));
      BSER_CellBalcCtl_DW.UnitDelay_DSTATE_p[i] = rtb_LogicalOperator3;
      rtb_DataTypeConversion6[i] = (int8_T)!rtb_LogicalOperator3;
      BSER_bCmcBordTempVld[i] = ((!(BSER_tCmcBordTemp_A[i] <
        BSER_tCmcBordTempLowLim_C)) && (!(BSER_tCmcBordTemp_A[i] >
        BSER_tCmcBordTempHiLim_C)));
      rtb_Switch1_f[i] = rtb_RelationalOperator1;
      rtb_Switch1_k[i] = rtb_LogicalOperator3;
    }

    tmp = (uint32_T)rtb_DataTypeConversion2[0];
    for (i = 0; i < 7; i++) {
      tmp += rtb_DataTypeConversion2[i + 1];
    }

    rtb_RelationalOperator1 = ((uint8_T)tmp != 0);
    tmp = (uint32_T)rtb_DataTypeConversion6[0];
    for (i = 0; i < 7; i++) {
      tmp += rtb_DataTypeConversion6[i + 1];
    }

    BSER_bCmuTNormFlg = (rtb_RelationalOperator1 && ((uint8_T)tmp != 0));
    for (i = 0; i < 8; i++) {
      BSER_bCmcAFETempVld[i] = ((!(BSER_tCmcAFETemp_A[i] <
        BSER_tCmcAFETempLowLim_C)) && (!(BSER_tCmcAFETemp_A[i] >
        BSER_tCmcAFETempHiLim_C)));
      if (BSER_BalCirFltDiagEna_C) {
        rtb_RelationalOperator1 = (rtb_RelationalOperator2[i] &&
          rtb_RelationalOperator3[i]);
      } else {
        rtb_RelationalOperator1 = BSER_bBalCirStNorm_C[i];
      }

      BSER_bCmuBalEna_A[i] = (rtb_RelationalOperator1 && (!rtb_Switch1_f[i]) &&
        (!rtb_Switch1_k[i]) && BSER_bCmcBordTempVld[i] && BSER_bCmcAFETempVld[i]);
    }

    BSER_bBalFltNrm = ((!((DFC_BalTempHighFlt_ASW & 24) == 24)) &&
                       (!((DFC_AFETempHighFlt_ASW & 24) == 24)));
    rtb_RelationalOperator1 = ((BMCR_stBmsModeCtrl ==
      BSER_CellBalcCtl_ConstB.DataTypeConversion_o) && COMR_bLeadAcidChgHvOn);
    rtb_LogicalOperator3 = !BSER_CellBalcCtl_DW.Delay1_DSTATE_c;
    BSER_CellBalcCtl_DW.Delay1_DSTATE_c = rtb_RelationalOperator1;
    rtb_LogicalOperator6_h = ((!BSER_CellBalcCtl_DW.Delay_DSTATE_p) ||
      ((real32_T)fabs(BSPR_iBattCurr) > BSER_iBalInteClrCurr_C));
    if (rtb_LogicalOperator6_h) {
      rtb_Add3 = BSER_tiCellBalcBase_C;
    } else {
      rtb_Add3 = 0.0F;
    }

    if (BSER_tiTONVInteTi <= BSER_CellBalcCtl_DW.Delay_DSTATE_hc) {
      rtb_Add1 = BSER_tiTONVInteTi;
    } else {
      rtb_Add1 = BSER_CellBalcCtl_DW.Delay_DSTATE_hc;
    }

    rtb_Add3 = (rtb_Add3 + rtb_Add1) * (real32_T)rtb_LogicalOperator6_h;
    BSER_CellBalcCtl_DW.Delay_DSTATE_hc = rtb_Add3;
    if (BSER_CellBalcCtl_DW.is_active_c4_BSER_CellBalcCtl == 0U) {
      BSER_CellBalcCtl_DW.is_active_c4_BSER_CellBalcCtl = 1U;
      BSER_CellBalcCtl_DW.is_c4_BSER_CellBalcCtl =
        BSER_CellBalc_IN_BalDrv_Default;
      BSER_CellBalcCtl_B.stBalDrvInteFlg = enmBalDrvInteSts_INACTIVE;
      BSER_CellBalcCtl_B.bBalDrvInteAct = false;
      BSER_CellBalcCtl_DW.Cnt = 0.0F;
    } else {
      switch (BSER_CellBalcCtl_DW.is_c4_BSER_CellBalcCtl) {
       case BSER_CellBalcC_IN_BalDrv_Active:
        if ((rtb_Add3 > BSER_tiTONVInteTi) || BSER_CellBalcCtl_DW.Delay1_DSTATE)
        {
          BSER_CellBalcCtl_DW.is_c4_BSER_CellBalcCtl =
            BSER_CellBal_IN_BalDrv_Complete;
          BSER_CellBalcCtl_B.stBalDrvInteFlg = enmBalDrvInteSts_BALCOMP;
          BSER_CellBalcCtl_B.bBalDrvInteAct = false;
        }
        break;

       case BSER_CellBal_IN_BalDrv_Complete:
        if (BSER_CellBalcCtl_DW.Cnt > BSER_cntBalInteCompHld_C) {
          BSER_CellBalcCtl_DW.is_c4_BSER_CellBalcCtl =
            BSER_CellBalc_IN_BalDrv_Default;
          BSER_CellBalcCtl_B.stBalDrvInteFlg = enmBalDrvInteSts_INACTIVE;
          BSER_CellBalcCtl_B.bBalDrvInteAct = false;
          BSER_CellBalcCtl_DW.Cnt = 0.0F;
        } else {
          BSER_CellBalcCtl_DW.Cnt++;
        }
        break;

       default:
        if (rtb_LogicalOperator3 && rtb_RelationalOperator1) {
          BSER_CellBalcCtl_DW.is_c4_BSER_CellBalcCtl =
            BSER_CellBalcC_IN_BalDrv_Active;
          BSER_CellBalcCtl_B.stBalDrvInteFlg = enmBalDrvInteSts_BALACTIVE;
          BSER_CellBalcCtl_B.bBalDrvInteAct = true;
        }
        break;
      }
    }

    BSER_bBalDrvInteAct = (BSER_bBalDrvInteEnaSw_C &&
      BSER_CellBalcCtl_B.bBalDrvInteAct);
    if (BSER_bWkBalEnaSw_C) {
      BSER_bWkBalEna = ((BSW_stSysWkUpSrc & 32) > 0);
    } else {
      BSER_bWkBalEna = false;
    }

    BSER_bCellBalACMode = (BCMR_stAChrgMode ==
      BSER_CellBalcCtl_ConstB.DataTypeConversion_n);
    BSER_bCellBalDCMode = (BCMR_stDChrgMode ==
      BSER_CellBalcCtl_ConstB.DataTypeConversion2_a);
    if (BSER_bVCUFltEnaSw_C) {
      rtb_RelationalOperator1 = (COMR_stVcuFalutLvl <
        BSER_CellBalcCtl_ConstB.DataTypeConversion_i);
    } else {
      rtb_RelationalOperator1 = true;
    }

    BSER_bFaultLvlEna = (rtb_RelationalOperator1 && (DSMR_stFaultRank <
      BSER_CellBalcCtl_ConstB.DataTypeConversion1_l));
    rtb_RelationalOperator1 = ((BSPR_tCellTempMax < BSER_tCellTempMaxLmt_C) &&
      (BSPR_tCellTempMin > BSER_tCellTempMinLmt_C));
    if (rtb_RelationalOperator1) {
      rtb_RelationalOperator1_0 = BSER_tiCellBalcBase_C;
    } else {
      rtb_RelationalOperator1_0 = 0.0F;
    }

    if (BSER_tiTONVTempTi <= BSER_CellBalcCtl_DW.Delay_DSTATE_je) {
      rtb_Add1 = BSER_tiTONVTempTi;
    } else {
      rtb_Add1 = BSER_CellBalcCtl_DW.Delay_DSTATE_je;
    }

    rtb_Add3 = (rtb_RelationalOperator1_0 + rtb_Add1) * (real32_T)
      rtb_RelationalOperator1;
    BSER_bCellBalTempNrmEna = (rtb_Add3 > BSER_tiTONVTempTi);
    BSER_CellBalcCtl_DW.Delay_DSTATE_je = rtb_Add3;
    rtb_RelationalOperator1 = (BSPR_tCellTempMax - BSPR_tCellTempMin <
      BSER_tCellTempDvtLmt_C);
    if (rtb_RelationalOperator1) {
      rtb_RelationalOperator1_0 = BSER_tiCellBalcBase_C;
    } else {
      rtb_RelationalOperator1_0 = 0.0F;
    }

    if (BSER_tiTONVTempTi <= BSER_CellBalcCtl_DW.Delay_DSTATE_h) {
      rtb_Add1 = BSER_tiTONVTempTi;
    } else {
      rtb_Add1 = BSER_CellBalcCtl_DW.Delay_DSTATE_h;
    }

    rtb_Add3 = (rtb_RelationalOperator1_0 + rtb_Add1) * (real32_T)
      rtb_RelationalOperator1;
    BSER_bCellBalTempDvtEna = (rtb_Add3 > BSER_tiTONVTempTi);
    BSER_CellBalcCtl_DW.Delay_DSTATE_h = rtb_Add3;
    rtb_Add3 = 1000.0F * COMR_uMaxCellVolt;
    BSER_CellBalcCtl_B.Gain1 = 1000.0F * COMR_uMinCellVolt;
    rtb_RelationalOperator1 = ((rtb_Add3 < BSER_uBalStartCellVoltLmtUp_C) &&
      (BSER_CellBalcCtl_B.Gain1 > BSER_uBalStartCellVoltLmtDn_C));
    if (rtb_RelationalOperator1) {
      rtb_RelationalOperator1_0 = BSER_tiCellBalcBase_C;
    } else {
      rtb_RelationalOperator1_0 = 0.0F;
    }

    if (BSER_tiTONVVoltTi <= BSER_CellBalcCtl_DW.Delay_DSTATE_j) {
      rtb_Add1 = BSER_tiTONVVoltTi;
    } else {
      rtb_Add1 = BSER_CellBalcCtl_DW.Delay_DSTATE_j;
    }

    rtb_Add1 = (rtb_RelationalOperator1_0 + rtb_Add1) * (real32_T)
      rtb_RelationalOperator1;
    BSER_bCellBalVoltNrmEna = (rtb_Add1 > BSER_tiTONVVoltTi);
    BSER_CellBalcCtl_DW.Delay_DSTATE_j = rtb_Add1;
    BSER_bCellBalNrmEna = ((BSER_bBalDrvInteAct || BSER_bWkBalEna ||
      BSER_bCellBalACMode || BSER_bCellBalDCMode || BSER_bBalFrON_C) &&
      BSER_bFaultLvlEna && BSER_bCellBalTempNrmEna && BSER_bCellBalTempDvtEna &&
      BSER_bCellBalVoltNrmEna && BSER_bBalFltNrm);
    rtb_SumofElements2 = UDSR_bBalReqEx[0];
    for (i = 0; i < 95; i++) {
      if (!(rtb_SumofElements2 >= UDSR_bBalReqEx[i + 1])) {
        rtb_SumofElements2 = UDSR_bBalReqEx[i + 1];
      }
    }

    rtb_RelationalOperator1 = (rtb_SumofElements2 != 0);
    rtb_Add3 -= BSER_CellBalcCtl_B.Gain1;
    rtb_LogicalOperator3 = (rtb_Add3 >= BSER_uCellVoltDvtBalOn_C);
    if (rtb_LogicalOperator3) {
      rtb_RelationalOperator1_0 = BSER_tiCellBalcBase_C;
    } else {
      rtb_RelationalOperator1_0 = 0.0F;
    }

    if (BSER_tiTONVVoltTi <= BSER_CellBalcCtl_DW.Delay_DSTATE_i) {
      rtb_Add1 = BSER_tiTONVVoltTi;
    } else {
      rtb_Add1 = BSER_CellBalcCtl_DW.Delay_DSTATE_i;
    }

    rtb_Add1 = (rtb_RelationalOperator1_0 + rtb_Add1) * (real32_T)
      rtb_LogicalOperator3;
    BSER_bCellBalOnEna = (rtb_Add1 > BSER_tiTONVVoltTi);
    BSER_CellBalcCtl_DW.Delay_DSTATE_i = rtb_Add1;
    rtb_LogicalOperator3 = (rtb_Add3 <= BSER_uCellVoltDvtBalOff_C);
    if (rtb_LogicalOperator3) {
      rtb_RelationalOperator1_0 = BSER_tiCellBalcBase_C;
    } else {
      rtb_RelationalOperator1_0 = 0.0F;
    }

    if (BSER_tiTONVVoltTi <= BSER_CellBalcCtl_DW.Delay_DSTATE_g) {
      rtb_Add1 = BSER_tiTONVVoltTi;
    } else {
      rtb_Add1 = BSER_CellBalcCtl_DW.Delay_DSTATE_g;
    }

    rtb_Add3 = (rtb_RelationalOperator1_0 + rtb_Add1) * (real32_T)
      rtb_LogicalOperator3;
    BSER_bCellBalOffDna = (rtb_Add3 > BSER_tiTONVVoltTi);
    BSER_CellBalcCtl_DW.Delay_DSTATE_g = rtb_Add3;
    if (BSER_CellBalcCtl_DW.is_active_c2_BSER_CellBalcCtl == 0U) {
      BSER_CellBalcCtl_DW.is_active_c2_BSER_CellBalcCtl = 1U;
      BSER_CellBalcCtl_DW.is_c2_BSER_CellBalcCtl =
        BSER_CellBalcCtl_IN_BalInit_Stt;
      BSER_CellBa_BSER_CellBalInitCal(BSER_CellBalcCtl_B.Merge);
      BSER_CellBalcCtl_B.BSER_bCellBalFlg = false;
      BSER_CellBalcCtl_DW.i = 0.0F;
    } else if (BSER_CellBalcCtl_DW.is_c2_BSER_CellBalcCtl ==
               BSER_CellBalcC_IN_BalCourse_Stt) {
      if ((!BSER_bCellBalNrmEna) || BSER_bCellBalOffDna ||
          (BSER_CellBalcCtl_DW.cntBalOn > BSER_sBalTimeMax_C) ||
          (!BSER_bCmuTNormFlg)) {
        BSER_CellBalcCtl_DW.is_BalCourse_Stt = BSER_CellB_IN_NO_ACTIVE_CHILD_f;
        BSER_CellBalcCtl_DW.is_c2_BSER_CellBalcCtl =
          BSER_CellBalcCtl_IN_BalInit_Stt;
        BSER_CellBa_BSER_CellBalInitCal(BSER_CellBalcCtl_B.Merge);
        BSER_CellBalcCtl_B.BSER_bCellBalFlg = false;
        BSER_CellBalcCtl_DW.i = 0.0F;
      } else {
        BSER_CellBalcCtl_DW.cntBalOn++;
        BSER_CellBalcCtl_B.BSER_bCellBalFlg = true;
        if (BSER_CellBalcCtl_DW.is_BalCourse_Stt ==
            BSER_CellBalcCt_IN_BalPause_Stt) {
          if (BSER_CellBalcCtl_DW.i > BSER_cntBalStopDur_C) {
            BSER_CellBalcCtl_DW.is_BalCourse_Stt =
              BSER_CellBalcCt_IN_BalStart_Stt;
            BSER_CellBa_BSER_CellBalSortCal(((real32_T *)&(COMR_uCellVolt_A[0])),
              BSER_CellBalcCtl_B.Gain1, ((boolean_T *)&(BSER_bCmuBalEna_A[0])),
              ((real32_T *)&(BSER_tCmcBordTemp_A[0])), ((real32_T *)
              &(BSER_tCmcAFETemp_A[0])), BSER_CellBalcCtl_B.Merge,
              &BSER_CellBalcCtl_B.BSER_CellBalSortCal,
              &BSER_CellBalcCtl_DW.BSER_CellBalSortCal);
            BSER_CellBalcCtl_DW.i = 0.0F;
            if ((BSER_CellBalcCtl_DW.i > BSER_cntBalStartDur_C) ||
                BSER_bCellBalConEna_C) {
              BSER_CellBa_BSER_CellBalSortCal(((real32_T *)&(COMR_uCellVolt_A[0])),
                BSER_CellBalcCtl_B.Gain1, ((boolean_T *)&(BSER_bCmuBalEna_A[0])),
                ((real32_T *)&(BSER_tCmcBordTemp_A[0])), ((real32_T *)
                &(BSER_tCmcAFETemp_A[0])), BSER_CellBalcCtl_B.Merge,
                &BSER_CellBalcCtl_B.BSER_CellBalSortCal,
                &BSER_CellBalcCtl_DW.BSER_CellBalSortCal);
              BSER_CellBalcCtl_DW.i = 0.0F;
            }
          } else {
            BSER_CellBalcCtl_DW.i++;
          }
        } else if ((BSER_CellBalcCtl_DW.i > BSER_cntBalStartDur_C) &&
                   BSER_bCellBalPWMEna_C) {
          BSER_CellBalcCtl_DW.is_BalCourse_Stt = BSER_CellBalcCt_IN_BalPause_Stt;
          BSER_CellBa_BSER_CellBalWaitCal(BSER_CellBalcCtl_B.Merge);
          BSER_CellBalcCtl_DW.i = 0.0F;
        } else {
          BSER_CellBalcCtl_DW.i++;
          if ((BSER_CellBalcCtl_DW.i > BSER_cntBalStartDur_C) ||
              BSER_bCellBalConEna_C) {
            BSER_CellBa_BSER_CellBalSortCal(((real32_T *)&(COMR_uCellVolt_A[0])),
              BSER_CellBalcCtl_B.Gain1, ((boolean_T *)&(BSER_bCmuBalEna_A[0])),
              ((real32_T *)&(BSER_tCmcBordTemp_A[0])), ((real32_T *)
              &(BSER_tCmcAFETemp_A[0])), BSER_CellBalcCtl_B.Merge,
              &BSER_CellBalcCtl_B.BSER_CellBalSortCal,
              &BSER_CellBalcCtl_DW.BSER_CellBalSortCal);
            BSER_CellBalcCtl_DW.i = 0.0F;
          }
        }
      }
    } else {
      if (BSER_bCellBalNrmEna && BSER_bCellBalOnEna && BSER_bCmuTNormFlg) {
        BSER_CellBalcCtl_DW.is_c2_BSER_CellBalcCtl =
          BSER_CellBalcC_IN_BalCourse_Stt;
        BSER_CellBalcCtl_DW.cntBalOn = 0.0F;
        BSER_CellBalcCtl_B.BSER_bCellBalFlg = true;
        BSER_CellBalcCtl_DW.is_BalCourse_Stt = BSER_CellBalcCt_IN_BalPause_Stt;
        BSER_CellBa_BSER_CellBalWaitCal(BSER_CellBalcCtl_B.Merge);
        BSER_CellBalcCtl_DW.i = 0.0F;
      }
    }

    if (rtb_RelationalOperator1) {
      BSER_stBalErrorCode = BSER_CellBalcCtl_ConstB.DataTypeConversion1;
    } else if (BSER_CellBalcCtl_B.BSER_bCellBalFlg) {
      BSER_stBalErrorCode = BSER_CellBalcCtl_ConstB.DataTypeConversion;
    } else if (!BSER_bCellBalNrmEna) {
      BSER_stBalErrorCode = BSER_CellBalcCtl_ConstB.DataTypeConversion2;
    } else if (!BSER_bCmuTNormFlg) {
      BSER_stBalErrorCode = BSER_CellBalcCtl_ConstB.DataTypeConversion3;
    } else if (BSER_bCellBalOnEna) {
      BSER_stBalErrorCode = BSER_CellBalcCtl_ConstB.DataTypeConversion4;
    } else if (BSER_bCellBalOffDna) {
      BSER_stBalErrorCode = BSER_CellBalcCtl_ConstB.DataTypeConversion5;
    } else {
      BSER_stBalErrorCode = BSER_CellBalcCtl_ConstB.DataTypeConversion6;
    }

    for (i = 0; i < 96; i++) {
      if (BSER_bCellBalEnaSw_C) {
        BSER_stCellBal[i] = BSER_bCellBalEnaVal_C[i];
      } else if (rtb_RelationalOperator1) {
        BSER_stCellBal[i] = UDSR_bBalReqEx[i];
      } else {
        BSER_stCellBal[i] = (uint8_T)BSER_CellBalcCtl_B.Merge[i];
      }
    }

    rtb_RelationalOperator1 = (BSER_CellBalcCtl_B.bBalDrvInteAct &&
      (!BSER_CellBalcCtl_DW.Delay1_DSTATE));
    if (!rtb_RelationalOperator1) {
      rtb_RelationalOperator1_0 = 0.0F;
    } else {
      rtb_RelationalOperator1_0 = BSER_CellBalcCtl_DW.Delay_DSTATE_f;
    }

    rtb_Add3 = (real32_T)rtb_RelationalOperator1 + rtb_RelationalOperator1_0;
    BSER_stBalInteChgFlg = BSER_CellBalcCtl_B.stBalDrvInteFlg;
    BSER_stBal = (uint8_T)BSER_CellBalcCtl_B.BSER_bCellBalFlg;
    BSER_CellBalcCtl_DW.Delay_DSTATE_p = BSER_CellBalcCtl_B.BSER_bCellBalFlg;
    BSER_CellBalcCtl_DW.Delay1_DSTATE = (rtb_Add3 > BSER_tiBalInteChgTimeGap_C);
    BSER_CellBalcCtl_DW.Delay_DSTATE_f = rtb_Add3;
  } else {
    if (BSER_CellBalcCtl_DW.BSER_CellBalCtlCal_MODE) {
      BSER_stBalErrorCode = 0U;
      memset(((uint8_T *)&(BSER_stCellBal[0])), 0, 96U * sizeof(uint8_T));
      BSER_stBal = 0U;
      BSER_stBalInteChgFlg = 0U;
      BSER_CellBalcCtl_DW.BSER_CellBalCtlCal_MODE = false;
    }
  }
}

/* Model step function */
void BSER_CellBalcCtl_step(void)
{
  real32_T maxV;
  boolean_T rtb_RelationalOperator_j[16];
  int32_T i;
  for (i = 0; i < 16; i++) {
    rtb_RelationalOperator_j[i] = (COMR_uPcbTemp_A[i] > BSER_uPcbTempInitVal_C);
  }

  if (rtb_RelationalOperator_j[0] || rtb_RelationalOperator_j[1] ||
      rtb_RelationalOperator_j[2] || rtb_RelationalOperator_j[3] ||
      rtb_RelationalOperator_j[4] || rtb_RelationalOperator_j[5] ||
      rtb_RelationalOperator_j[6] || rtb_RelationalOperator_j[7] ||
      rtb_RelationalOperator_j[8] || rtb_RelationalOperator_j[9] ||
      rtb_RelationalOperator_j[10] || rtb_RelationalOperator_j[11] ||
      rtb_RelationalOperator_j[12] || rtb_RelationalOperator_j[13] ||
      rtb_RelationalOperator_j[14] || rtb_RelationalOperator_j[15]) {
    if (!BSER_CellBalcCtl_DW.Subsystem_MODE) {
      for (i = 0; i < 16; i++) {
        BSER_CellBalcCtl_DW.UnitDelay_DSTATE[i] = 0.0F;
      }

      BSER_CellBalcCtl_DW.Delay_DSTATE = 1.0F;
      BSER_CellBalcCtl_DW.Subsystem_MODE = true;
    }

    for (i = 0; i < 16; i++) {
      maxV = BSER_uTempUnitCovt_C * COMR_uPcbTemp_A[i];
      if (!(BSER_CellBalcCtl_DW.Delay_DSTATE > 0.0F)) {
        maxV = (maxV - BSER_CellBalcCtl_DW.UnitDelay_DSTATE[i]) *
          BSER_cofPcbTempFlt_C + BSER_CellBalcCtl_DW.UnitDelay_DSTATE[i];
      }

      BSER_CellBalcCtl_DW.UnitDelay_DSTATE[i] = maxV;
      BSER_CellBalcCtl_B.uDLookupTable[i] = look1_iflf_binlcpw(maxV, ((const
        real32_T *)&(BSER_tPcbTemp_CUR_X[0])), ((const real32_T *)
        &(BSER_tPcbTemp_CUR[0])), 190U);
    }

    BSER_CellBalcCtl_DW.Delay_DSTATE = 0.0F;
  } else {
    if (BSER_CellBalcCtl_DW.Subsystem_MODE) {
      BSER_CellBalcCtl_DW.Subsystem_MODE = false;
    }
  }

  for (i = 0; i < 16; i++) {
    if (rtb_RelationalOperator_j[i]) {
      BSER_tCmcBordTempAll_A[i] = BSER_CellBalcCtl_B.uDLookupTable[i];
    } else {
      BSER_tCmcBordTempAll_A[i] = BSER_tPcbTempInitVal_C;
    }
  }

  if (BSER_tCmcBordTempAll_A[0] >= BSER_tCmcBordTempAll_A[1]) {
    BSER_tCmcBordTemp_A[0] = BSER_tCmcBordTempAll_A[0];
  } else {
    BSER_tCmcBordTemp_A[0] = BSER_tCmcBordTempAll_A[1];
  }

  if (BSER_tCmcBordTempAll_A[2] >= BSER_tCmcBordTempAll_A[3]) {
    BSER_tCmcBordTemp_A[1] = BSER_tCmcBordTempAll_A[2];
  } else {
    BSER_tCmcBordTemp_A[1] = BSER_tCmcBordTempAll_A[3];
  }

  if (BSER_tCmcBordTempAll_A[4] >= BSER_tCmcBordTempAll_A[5]) {
    BSER_tCmcBordTemp_A[2] = BSER_tCmcBordTempAll_A[4];
  } else {
    BSER_tCmcBordTemp_A[2] = BSER_tCmcBordTempAll_A[5];
  }

  if (BSER_tCmcBordTempAll_A[6] >= BSER_tCmcBordTempAll_A[7]) {
    BSER_tCmcBordTemp_A[3] = BSER_tCmcBordTempAll_A[6];
  } else {
    BSER_tCmcBordTemp_A[3] = BSER_tCmcBordTempAll_A[7];
  }

  if (BSER_tCmcBordTempAll_A[8] >= BSER_tCmcBordTempAll_A[9]) {
    BSER_tCmcBordTemp_A[4] = BSER_tCmcBordTempAll_A[8];
  } else {
    BSER_tCmcBordTemp_A[4] = BSER_tCmcBordTempAll_A[9];
  }

  if (BSER_tCmcBordTempAll_A[10] >= BSER_tCmcBordTempAll_A[11]) {
    BSER_tCmcBordTemp_A[5] = BSER_tCmcBordTempAll_A[10];
  } else {
    BSER_tCmcBordTemp_A[5] = BSER_tCmcBordTempAll_A[11];
  }

  if (BSER_tCmcBordTempAll_A[12] >= BSER_tCmcBordTempAll_A[13]) {
    BSER_tCmcBordTemp_A[6] = BSER_tCmcBordTempAll_A[12];
  } else {
    BSER_tCmcBordTemp_A[6] = BSER_tCmcBordTempAll_A[13];
  }

  if (BSER_tCmcBordTempAll_A[14] >= BSER_tCmcBordTempAll_A[15]) {
    BSER_tCmcBordTemp_A[7] = BSER_tCmcBordTempAll_A[14];
  } else {
    BSER_tCmcBordTemp_A[7] = BSER_tCmcBordTempAll_A[15];
  }

  BSER_CellBalcCt_BSER_CellBalCtl();
  maxV = BSER_tCmcBordTempAll_A[0];
  for (i = 0; i < 15; i++) {
    if (!(maxV >= BSER_tCmcBordTempAll_A[i + 1])) {
      maxV = BSER_tCmcBordTempAll_A[i + 1];
    }
  }

  BSER_tCmcBordMaxTemp = maxV;
}

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

  /* block I/O */

  /* custom signals */
  {
    int32_T i;
    for (i = 0; i < 16; i++) {
      BSER_tCmcBordTempAll_A[i] = 25.0F;
    }
  }

  {
    int32_T i;
    for (i = 0; i < 8; i++) {
      BSER_tCmcBordTemp_A[i] = 25.0F;
    }
  }

  BSER_tCmcBordMaxTemp = 25.0F;

  {
    int32_T i;
    for (i = 0; i < 8; i++) {
      BSER_tCmcAFETemp_A[i] = 25.0F;
    }
  }

  {
    int32_T i;
    for (i = 0; i < 8; i++) {
      BSER_bCmcBordTempVld[i] = true;
    }
  }

  BSER_bCmuTNormFlg = true;

  {
    int32_T i;
    for (i = 0; i < 8; i++) {
      BSER_bCmcAFETempVld[i] = true;
    }
  }

  {
    int32_T i;
    for (i = 0; i < 8; i++) {
      BSER_bCmuBalEna_A[i] = true;
    }
  }

  BSER_bBalFltNrm = true;
  BSER_bBalDrvInteAct = true;
  BSER_bWkBalEna = true;
  BSER_bCellBalACMode = true;
  BSER_bCellBalDCMode = true;
  BSER_bFaultLvlEna = true;
  BSER_bCellBalNrmEna = true;
  BSER_bCellBalOffDna = true;
  BSER_bCellBalOnEna = true;
  BSER_bCellBalVoltNrmEna = true;
  BSER_bCellBalTempDvtEna = true;
  BSER_bCellBalTempNrmEna = true;
  BSER_CellBalcCtl_DW.Delay_DSTATE = 1.0F;
  BSER_CellB_BSER_CellBalCtl_Init();
}

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