/*
 * File: BSER_BattPrfDiag.c
 *
 * Code generated for Simulink model 'BSER_BattPrfDiag'.
 *
 * Model version                  : 1.1382
 * Simulink Coder version         : 8.11 (R2016b) 25-Aug-2016
 * C/C++ source code generated on : Sat Dec 05 12:55:48 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_BattPrfDiag.h"
#include "BSER_BattPrfDiag_private.h"

/* Named constants for Chart: '<S18>/CurrProc' */
#define BSER_BattP_IN_Time100msProc_stt ((uint8_T)1U)
#define BSER_BattP_IN_Time10msStrt_stt1 ((uint8_T)3U)
#define BSER_BattPr_IN_Time10msProc_stt ((uint8_T)2U)

/* Exported data definition */

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

/* Definition for custom storage class: Volatile */
volatile uint8_T DFC_ChrgOCMax_ASW;        /* Max fault: battery over charge current */
volatile uint8_T DFC_ChrgOCMin_ASW;        /* Min fault: battery over charge current */
volatile uint8_T DFC_ChrgOCNpl_ASW;        /* Npl fault: battery over charge current */
volatile uint8_T DFC_DisChOCMax_ASW;       /* Max fault: battery over discharge current */
volatile uint8_T DFC_DisChOCMin_ASW;       /* Min fault: battery over discharge current */
volatile uint8_T DFC_DisChOCNpl_ASW;       /* Npl fault: battery over discharge current */

#define ASW1_OEM_STOP_SEC_DEFAULT_RAM1_CLEARED_UNSPECIFIED
#include "Asw1_OEM_MemMap.h"
#define ASW1_OEM_START_SEC_DEFAULT_RAM1_CLEARED_UNSPECIFIED
#include "Asw1_OEM_MemMap.h"

/* Definition for custom storage class: OutputSignal */
volatile boolean_T BSER_bChrgOCMaxDet; /* Fault detect flag for charge current has exceed the SOP limit */
volatile boolean_T BSER_bChrgOCMaxHeal;/* Fault healing flag for charge current has exceed the SOP limit */
volatile boolean_T BSER_bChrgOCMinDet; /* Fault detect flag for charge current has exceed the operating limit */
volatile boolean_T BSER_bChrgOCMinHeal;/* Fault healing flag for charge current has exceed the operating limit */
volatile boolean_T BSER_bChrgOCNplDet; /* Frequent fault detect flag for charge OC detected in current driving cycle */
volatile boolean_T BSER_bChrgOCNplHeal;/* Frequent fault healing flag for  charge OC detected in current driving cycle */
volatile boolean_T BSER_bContChrgOpLimErr;/* Error status flag for charge current has exceed the constant operating limit */
volatile boolean_T BSER_bContChrgOpLimOk;/* Healing status flag for charge current has exceed the constant operating limit */
volatile boolean_T BSER_bContDisChOpLimErr;/* Error status flag for discharge current has exceed the constant operating limit */
volatile boolean_T BSER_bContDisChOpLimOk;/* Healing status flag for discharge current has exceed the constant operating limit */
volatile boolean_T BSER_bDisChOCMaxDet;/* Fault detect flag for discharge current has exceed the SOP limit */
volatile boolean_T BSER_bDisChOCMaxHeal;/* Fault healing flag for discharge current has exceed the SOP limit */
volatile boolean_T BSER_bDisChOCMinDet;/* Fault detect flag for discharge current has exceed the operating limit */
volatile boolean_T BSER_bDisChOCMinHeal;/* Fault healing flag for discharge current has exceed the operating limit */
volatile boolean_T BSER_bDisChOCNplDet;/* Frequent fault detect flag for discharge OC detected in current driving cycle */
volatile boolean_T BSER_bDisChOCNplHeal;/* Frequent fault healing flag for  discharge OC detected in current driving cycle */
volatile boolean_T BSER_bLonChrgOpLimErr;/* Error status flag for charge current has exceed the continuous operating limit */
volatile boolean_T BSER_bLonChrgOpLimOk;/* Healing status flag for charge current has exceed the continuous operating limit */
volatile boolean_T BSER_bLonDisChOpLimErr;/* Error status flag for discharge current has exceed the continuous operating limit */
volatile boolean_T BSER_bLonDisChOpLimOk;/* Healing status flag for discharge current has exceed the continuous operating limit */
volatile boolean_T BSER_bShrtChrgOpLimErr;/* Error status flag for charge current has exceed the peak operating limit */
volatile boolean_T BSER_bShrtChrgOpLimOk;/* Healing status flag for charge current has exceed the peak operating limit */
volatile boolean_T BSER_bShrtDisChOpLimErr;/* Error status flag for discharge current has exceed the peak operating limit */
volatile boolean_T BSER_bShrtDisChOpLimOk;/* Healing status flag for discharge current has exceed the peak operating limit */
volatile uint8_T BSER_cntChrgOCNpl;    /* Counter for over current diagnosis during charge stage */
volatile uint8_T BSER_cntDisChOCNpl;   /* Counter for over current diagnosis during discharge stage */
volatile boolean_T BSER_flgChrgOvrCurrCond;/* Enable condition for over current diagnosis during charge stage */
volatile boolean_T BSER_flgDisChOvrCurrCond;/* Enable condition for over current diagnosis during discharge stage */
volatile real32_T BSER_iChrgCurrErr;   /* Current difference between actual charge current and minimum required current */
volatile real32_T BSER_iChrgCurrOk;    /* Current difference between minimum required current and actual charge current  */
volatile real32_T BSER_iDisChCurrErr;  /* Current difference between actual discharge current and minimum required current */
volatile real32_T BSER_iDisChCurrOk;   /* Current difference between minimum required current and actual discharge current  */
volatile real32_T BSER_iLonChrgCurrErr;/* Integrate of the current difference between actual continuous charge current and required continuous charge current */
volatile real32_T BSER_iLonChrgCurrOk; /* Integrate of the current difference between required continuous charge current and actual continuous charge current */
volatile real32_T BSER_iLonChrgCurrPerErr;/* Current calculated through required continuous charge current  for over current fault detect */
volatile real32_T BSER_iLonChrgCurrPerOk;/* Current calculated through required continuous charge current  for over current fault heal */
volatile real32_T BSER_iLonDisChCurrErr;/* Integrate of the current difference between actual continuous discharge current and required continuous discharge current */
volatile real32_T BSER_iLonDisChCurrOk;/* Integrate of the current difference between required continuous discharge current and actual continuous discharge current */
volatile real32_T BSER_iLonDisChCurrPerErr;/* Current calculated through required continuous discharge current  for over current fault detect */
volatile real32_T BSER_iLonDisChCurrPerOk;/* Current calculated through required continuous discharge current  for over current fault heal */
volatile real32_T BSER_iShrtChrgCurrErr;/* Integrate of the current difference between actual peak charge current and required peak charge current */
volatile real32_T BSER_iShrtChrgCurrOk;/* Integrate of the current difference between required peak charge current and actual peak charge current */
volatile real32_T BSER_iShrtChrgCurrPerErr;/* Current calculated through required peak charge current  for over current fault detect */
volatile real32_T BSER_iShrtChrgCurrPerOk;/* Current calculated through required peak charge current  for over current fault heal */
volatile real32_T BSER_iShrtDisChCurrErr;/* Integrate of the current difference between actual peak discharge current and required peak discharge current */
volatile real32_T BSER_iShrtDisChCurrOk;/* Integrate of the current difference between required peak discharge current and actual peak discharge current */
volatile real32_T BSER_iShrtDisChCurrPerErr;/* Current calculated through required peak discharge current  for over current fault detect */
volatile real32_T BSER_iShrtDisChCurrPerOk;/* Current calculated through required peak discharge current  for over current fault heal */
volatile real32_T BSER_iSumLonChrgCurrAct;/* Sum current for actual peak charge current in calculated period */
volatile real32_T BSER_iSumLonChrgCurrReq;/* Sum current for request continuous charge current in calculated period */
volatile real32_T BSER_iSumLonDisChCurrAct;/* Sum current for actual peak discharge current in calculated period */
volatile real32_T BSER_iSumLonDisChCurrReq;/* Sum current for request continuous discharge current in calculated period */
volatile real32_T BSER_iSumShrtChrgCurrAct;/* Sum current for actual continuous charge current in calculated period */
volatile real32_T BSER_iSumShrtChrgCurrReq;/* Sum current for request peak charge current in calculated period */
volatile real32_T BSER_iSumShrtDisChCurrAct;/* Sum current for actual continuous discharge current in calculated period */
volatile real32_T BSER_iSumShrtDisChCurrReq;/* Sum current for request peak discharge current in calculated period */

#define ASW1_OEM_STOP_SEC_DEFAULT_RAM1_CLEARED_UNSPECIFIED
#include "Asw1_OEM_MemMap.h"

/* Block signals (auto storage) */
B_BSER_BattPrfDiag_T BSER_BattPrfDiag_B;

/* Block states (auto storage) */
DW_BSER_BattPrfDiag_T BSER_BattPrfDiag_DW;

/*
 * Output and update for atomic system:
 *    '<S18>/CurrProc'
 *    '<S19>/CurrProc'
 *    '<S23>/CurrProc1'
 *    '<S24>/CurrProc'
 *    '<S41>/CurrProc'
 *    '<S42>/CurrProc'
 *    '<S46>/CurrProc'
 *    '<S47>/CurrProc'
 */
void BSER_BattPrfDiag_CurrProc(real32_T rtu_BSPR_iCurr, uint8_T
  rtu_BSER_tiCurrCalcPer_C, B_CurrProc_BSER_BattPrfDiag_T *localB,
  DW_CurrProc_BSER_BattPrfDiag_T *localDW)
{
  real32_T BSER_iCurrSqAvrgS;
  int32_T tmp;
  if (localDW->is_active_c2_BSER_BattPrfDiag == 0U) {
    localDW->is_active_c2_BSER_BattPrfDiag = 1U;
    localB->i = 0U;
    while (localB->i <= 9) {
      localDW->BSER_iCurrSqProcTmp_A[localB->i] = 0.0F;
      tmp = localB->i + 1;
      if (tmp > 255) {
        tmp = 255;
      }

      localB->i = (uint8_T)tmp;
    }

    localB->Index_s = 0U;
    while (localB->Index_s < 60) {
      localB->BSER_iCurrSqAvrgS_A[localB->Index_s] = 0.0F;
      tmp = localB->Index_s + 1;
      if (tmp > 255) {
        tmp = 255;
      }

      localB->Index_s = (uint8_T)tmp;
    }

    localB->i = 0U;
    localB->Index_s = 0U;
    localDW->is_c2_BSER_BattPrfDiag = BSER_BattP_IN_Time10msStrt_stt1;
    localDW->BSER_iCurrSqProcTmp = rtu_BSPR_iCurr * rtu_BSPR_iCurr;
    localDW->BSER_cntCurrSqProc = 0U;
  } else {
    switch (localDW->is_c2_BSER_BattPrfDiag) {
     case BSER_BattP_IN_Time100msProc_stt:
      tmp = localB->i + 1;
      if (tmp > 255) {
        tmp = 255;
      }

      localB->i = (uint8_T)tmp;
      if (localB->i > 9) {
        localB->i = 0U;
        BSER_iCurrSqAvrgS = 0.0F;
        while (localB->i <= 9) {
          BSER_iCurrSqAvrgS += localDW->BSER_iCurrSqProcTmp_A[localB->i];
          tmp = localB->i + 1;
          if (tmp > 255) {
            tmp = 255;
          }

          localB->i = (uint8_T)tmp;
        }

        localB->i = 0U;
        localB->BSER_iCurrSqAvrgS_A[localB->Index_s] = BSER_iCurrSqAvrgS / 10.0F;
        tmp = localB->Index_s + 1;
        if (tmp > 255) {
          tmp = 255;
        }

        localB->Index_s = (uint8_T)tmp;
        if (localB->Index_s >= rtu_BSER_tiCurrCalcPer_C) {
          localB->Index_s = 0U;
          localDW->is_c2_BSER_BattPrfDiag = BSER_BattPr_IN_Time10msProc_stt;
          localDW->BSER_iCurrSqProcTmp += rtu_BSPR_iCurr * rtu_BSPR_iCurr;
          tmp = localDW->BSER_cntCurrSqProc + 1;
          if (tmp > 255) {
            tmp = 255;
          }

          localDW->BSER_cntCurrSqProc = (uint8_T)tmp;
        } else {
          localDW->is_c2_BSER_BattPrfDiag = BSER_BattPr_IN_Time10msProc_stt;
          localDW->BSER_iCurrSqProcTmp += rtu_BSPR_iCurr * rtu_BSPR_iCurr;
          tmp = localDW->BSER_cntCurrSqProc + 1;
          if (tmp > 255) {
            tmp = 255;
          }

          localDW->BSER_cntCurrSqProc = (uint8_T)tmp;
        }
      } else {
        localDW->is_c2_BSER_BattPrfDiag = BSER_BattPr_IN_Time10msProc_stt;
        localDW->BSER_iCurrSqProcTmp += rtu_BSPR_iCurr * rtu_BSPR_iCurr;
        tmp = localDW->BSER_cntCurrSqProc + 1;
        if (tmp > 255) {
          tmp = 255;
        }

        localDW->BSER_cntCurrSqProc = (uint8_T)tmp;
      }
      break;

     case BSER_BattPr_IN_Time10msProc_stt:
      if (localDW->BSER_cntCurrSqProc >= 9) {
        localDW->is_c2_BSER_BattPrfDiag = BSER_BattP_IN_Time100msProc_stt;
        localDW->BSER_iCurrSqProcTmp_A[localB->i] = localDW->BSER_iCurrSqProcTmp
          / 10.0F;
        localDW->BSER_iCurrSqProcTmp = rtu_BSPR_iCurr * rtu_BSPR_iCurr;
        localDW->BSER_cntCurrSqProc = 0U;
      } else {
        localDW->BSER_iCurrSqProcTmp += rtu_BSPR_iCurr * rtu_BSPR_iCurr;
        tmp = localDW->BSER_cntCurrSqProc + 1;
        if (tmp > 255) {
          tmp = 255;
        }

        localDW->BSER_cntCurrSqProc = (uint8_T)tmp;
      }
      break;

     default:
      localDW->is_c2_BSER_BattPrfDiag = BSER_BattPr_IN_Time10msProc_stt;
      localDW->BSER_iCurrSqProcTmp += rtu_BSPR_iCurr * rtu_BSPR_iCurr;
      tmp = localDW->BSER_cntCurrSqProc + 1;
      if (tmp > 255) {
        tmp = 255;
      }

      localDW->BSER_cntCurrSqProc = (uint8_T)tmp;
      break;
    }
  }
}

/* Model step function */
void BSER_BattPrfDiag_step(void)
{
  boolean_T rtb_LogicalOperator3_n;
  boolean_T rtb_RelationalOperator2_ed;
  boolean_T rtb_LogicalOperator4_n;
  boolean_T rtb_Compare_k;
  boolean_T rtb_Switch2_m;
  boolean_T rtb_Switch4_e;
  boolean_T rtb_LogicalOperator4;
  real32_T rtb_Abs;
  real32_T rtb_SumofElements_f;
  real32_T rtb_SumofElements_dw;
  real32_T rtb_Abs1;
  real32_T rtb_SumofElements_k;
  real32_T rtb_Abs2;
  real32_T rtb_SumofElements_lk;
  boolean_T rtb_RelationalOperator4_k;
  boolean_T rtb_LogicalOperator4_lw;
  int32_T i;
  uint8_T rtb_Switch4_n;
  rtb_LogicalOperator3_n = ((3 != DSMR_stFaultRank) && (DSMR_stFaultRank != 4));
  rtb_RelationalOperator2_ed = (BCCR_stBmsCtrMode == 4);
  rtb_LogicalOperator4_n = ((5 != BMCR_stBmsModeCtrl) && (BMCR_stBmsModeCtrl !=
    6));
  BSER_flgChrgOvrCurrCond = (rtb_LogicalOperator3_n &&
    rtb_RelationalOperator2_ed && rtb_LogicalOperator4_n && (BSPR_iBattCurr <
    0.0F));
  if (BSER_flgChrgOvrCurrCond) {
    rtb_Abs = (real32_T)fabs(BSPR_iBattCurr);
    BSER_BattPrfDiag_CurrProc(rtb_Abs, BSER_tiCurrPeakPer_C,
      &BSER_BattPrfDiag_B.sf_CurrProc, &BSER_BattPrfDiag_DW.sf_CurrProc);
    rtb_SumofElements_f = BSER_BattPrfDiag_B.sf_CurrProc.BSER_iCurrSqAvrgS_A[0];
    for (i = 0; i < 59; i++) {
      rtb_SumofElements_f +=
        BSER_BattPrfDiag_B.sf_CurrProc.BSER_iCurrSqAvrgS_A[i + 1];
    }

    BSER_iSumShrtChrgCurrAct = rtb_SumofElements_f;
    BSER_BattPrfDiag_CurrProc(rtb_Abs, BSER_tiCurrConstPer_C,
      &BSER_BattPrfDiag_B.sf_CurrProc_p, &BSER_BattPrfDiag_DW.sf_CurrProc_p);
    rtb_SumofElements_dw = BSER_BattPrfDiag_B.sf_CurrProc_p.BSER_iCurrSqAvrgS_A
      [0];
    for (i = 0; i < 59; i++) {
      rtb_SumofElements_dw +=
        BSER_BattPrfDiag_B.sf_CurrProc_p.BSER_iCurrSqAvrgS_A[i + 1];
    }

    BSER_iSumLonChrgCurrAct = rtb_SumofElements_dw;
    rtb_Abs1 = (real32_T)fabs(BSER_iPeakCurrCh);
    BSER_BattPrfDiag_CurrProc(rtb_Abs1, BSER_tiCurrPeakPer_C,
      &BSER_BattPrfDiag_B.sf_CurrProc_e, &BSER_BattPrfDiag_DW.sf_CurrProc_e);
    rtb_SumofElements_k = BSER_BattPrfDiag_B.sf_CurrProc_e.BSER_iCurrSqAvrgS_A[0];
    for (i = 0; i < 59; i++) {
      rtb_SumofElements_k +=
        BSER_BattPrfDiag_B.sf_CurrProc_e.BSER_iCurrSqAvrgS_A[i + 1];
    }

    BSER_iSumShrtChrgCurrReq = rtb_SumofElements_k;
    rtb_Abs2 = (real32_T)fabs(BSER_iConstCurrChMax);
    BSER_BattPrfDiag_CurrProc(rtb_Abs2, BSER_tiCurrConstPer_C,
      &BSER_BattPrfDiag_B.sf_CurrProc1, &BSER_BattPrfDiag_DW.sf_CurrProc1);
    rtb_SumofElements_lk = BSER_BattPrfDiag_B.sf_CurrProc1.BSER_iCurrSqAvrgS_A[0];
    for (i = 0; i < 59; i++) {
      rtb_SumofElements_lk +=
        BSER_BattPrfDiag_B.sf_CurrProc1.BSER_iCurrSqAvrgS_A[i + 1];
    }

    BSER_iSumLonChrgCurrReq = rtb_SumofElements_lk;
    if ((BSER_iSumShrtChrgCurrReq > 0.0F) && (BSER_iSumLonChrgCurrReq > 0.0F)) {
      BSER_iShrtChrgCurrPerOk = rtb_SumofElements_k * BSER_rShrtChrgCurrHeal_C;
      BSER_iLonChrgCurrPerOk = rtb_SumofElements_lk * BSER_rLonChrgCurrHeal_C;
      BSER_BattPrfDiag_B.LogicalOperator1_i = ((rtb_SumofElements_k >
        BSER_iSumShrtChrgCurrReq_C) && (rtb_SumofElements_f <
        BSER_iShrtChrgCurrPerOk) && ((rtb_SumofElements_dw <
        BSER_iLonChrgCurrPerOk) && (rtb_SumofElements_lk >
        BSER_iSumLonChrgCurrReq_C)));
      BSER_iShrtChrgCurrPerErr = rtb_SumofElements_k * BSER_rShrtChrgCurrHi_C;
      BSER_iLonChrgCurrPerErr = rtb_SumofElements_lk * BSER_rLonChrgCurrHi_C;
      BSER_BattPrfDiag_B.LogicalOperator4_l = ((rtb_SumofElements_f >
        BSER_iShrtChrgCurrPerErr) || (rtb_SumofElements_dw >
        BSER_iLonChrgCurrPerErr));
    }

    if (BSPR_uBattVolt != 0.0F) {
      rtb_SumofElements_f = BSPR_uBattVolt;
    } else {
      rtb_SumofElements_f = BSER_uBattVoltConst_C;
    }

    rtb_SumofElements_dw = (rtb_Abs - rtb_Abs1) * 0.01F + BSER_iShrtChrgCurrErr;
    if (rtb_SumofElements_dw > 15000.0F) {
      BSER_iShrtChrgCurrErr = 15000.0F;
    } else if (rtb_SumofElements_dw < 0.0F) {
      BSER_iShrtChrgCurrErr = 0.0F;
    } else {
      BSER_iShrtChrgCurrErr = rtb_SumofElements_dw;
    }

    rtb_SumofElements_dw = (rtb_Abs - rtb_Abs2) * 0.01F + BSER_iLonChrgCurrErr;
    if (rtb_SumofElements_dw > 30000.0F) {
      BSER_iLonChrgCurrErr = 30000.0F;
    } else if (rtb_SumofElements_dw < 0.0F) {
      BSER_iLonChrgCurrErr = 0.0F;
    } else {
      BSER_iLonChrgCurrErr = rtb_SumofElements_dw;
    }

    rtb_SumofElements_dw = (rtb_Abs1 - rtb_Abs) * 0.01F + BSER_iShrtChrgCurrOk;
    if (rtb_SumofElements_dw > 15000.0F) {
      BSER_iShrtChrgCurrOk = 15000.0F;
    } else if (rtb_SumofElements_dw < 0.0F) {
      BSER_iShrtChrgCurrOk = 0.0F;
    } else {
      BSER_iShrtChrgCurrOk = rtb_SumofElements_dw;
    }

    rtb_SumofElements_dw = (rtb_Abs2 - rtb_Abs) * 0.01F + BSER_iLonChrgCurrOk;
    if (rtb_SumofElements_dw > 30000.0F) {
      BSER_iLonChrgCurrOk = 30000.0F;
    } else if (rtb_SumofElements_dw < 0.0F) {
      BSER_iLonChrgCurrOk = 0.0F;
    } else {
      BSER_iLonChrgCurrOk = rtb_SumofElements_dw;
    }

    if ((BSER_cntBattOCMinDiag_C & 4) == 4) {
      BSER_bShrtChrgOpLimErr = (BSER_iShrtChrgCurrErr > BSER_iShrtChrgCurrErr_C);
    } else {
      BSER_bShrtChrgOpLimErr = false;
    }

    if ((BSER_cntBattOCMinDiag_C & 8) == 8) {
      BSER_bLonChrgOpLimErr = (BSER_iLonChrgCurrErr > BSER_iLonChrgCurrErr_C);
    } else {
      BSER_bLonChrgOpLimErr = false;
    }

    BSER_iChrgCurrErr = rtb_Abs - BSER_pPeakPwrCh * BSER_cntPwrConvertConst_C /
      rtb_SumofElements_f;
    rtb_RelationalOperator4_k = (BSER_iChrgCurrErr > BSER_iChrgCurrErr_C);
    if (rtb_RelationalOperator4_k) {
      rtb_Abs2 = 0.01F;
    } else {
      rtb_Abs2 = 0.0F;
    }

    if (BSER_tiChrgCurrErr_C <= BSER_BattPrfDiag_DW.Delay_DSTATE_n5) {
      rtb_Abs1 = BSER_tiChrgCurrErr_C;
    } else {
      rtb_Abs1 = BSER_BattPrfDiag_DW.Delay_DSTATE_n5;
    }

    rtb_Abs1 = (rtb_Abs2 + rtb_Abs1) * (real32_T)rtb_RelationalOperator4_k;
    BSER_bContChrgOpLimErr = (rtb_Abs1 > BSER_tiChrgCurrErr_C);
    BSER_BattPrfDiag_DW.Delay_DSTATE_n5 = rtb_Abs1;
    BSER_bChrgOCMinDet = (BSER_bShrtChrgOpLimErr || BSER_bLonChrgOpLimErr ||
                          BSER_bContChrgOpLimErr);
    BSER_iChrgCurrOk = BSER_pConstPwrCh / rtb_SumofElements_f - rtb_Abs;
    rtb_RelationalOperator4_k = (BSER_iChrgCurrOk > BSER_iChrgCurrOk_C);
    if (rtb_RelationalOperator4_k) {
      rtb_Abs2 = 0.01F;
    } else {
      rtb_Abs2 = 0.0F;
    }

    if (BSER_tiChrgCurrOk_C <= BSER_BattPrfDiag_DW.Delay_DSTATE_l) {
      rtb_Abs1 = BSER_tiChrgCurrOk_C;
    } else {
      rtb_Abs1 = BSER_BattPrfDiag_DW.Delay_DSTATE_l;
    }

    rtb_Abs = (rtb_Abs2 + rtb_Abs1) * (real32_T)rtb_RelationalOperator4_k;
    BSER_bContChrgOpLimOk = (rtb_Abs > BSER_tiChrgCurrOk_C);
    BSER_BattPrfDiag_DW.Delay_DSTATE_l = rtb_Abs;
    if ((BSER_cntBattOCMinDiag_C & 4) == 4) {
      BSER_bShrtChrgOpLimOk = (BSER_iShrtChrgCurrOk > BSER_iShrtChrgCurrOk_C);
    } else {
      BSER_bShrtChrgOpLimOk = true;
    }

    if ((BSER_cntBattOCMinDiag_C & 8) == 8) {
      BSER_bLonChrgOpLimOk = (BSER_iLonChrgCurrOk > BSER_iLonChrgCurrOk_C);
    } else {
      BSER_bLonChrgOpLimOk = true;
    }

    BSER_bChrgOCMinHeal = (BSER_bContChrgOpLimOk && BSER_bShrtChrgOpLimOk &&
      BSER_bLonChrgOpLimOk);
    BSER_bChrgOCMaxDet = BSER_BattPrfDiag_B.LogicalOperator4_l;
    BSER_bChrgOCMaxHeal = BSER_BattPrfDiag_B.LogicalOperator1_i;
  }

  rtb_RelationalOperator4_k = ((BSER_cntBattPrfDiag_CW & 2) == 2);
  rtb_Switch2_m = (rtb_RelationalOperator4_k && BSER_bChrgOCMaxDet);
  rtb_Compare_k = ((BSER_cntBattPrfDiag_CW & 4) == 4);
  rtb_Switch4_e = (rtb_Compare_k && BSER_bChrgOCMinDet);
  rtb_LogicalOperator4 = (rtb_Switch2_m || rtb_Switch4_e);
  rtb_LogicalOperator4_lw = ((!BSER_BattPrfDiag_DW.Delay1_DSTATE_k) &&
    rtb_LogicalOperator4);
  BSER_BattPrfDiag_DW.Delay1_DSTATE_k = rtb_LogicalOperator4;
  BSER_cntChrgOCNpl = (uint8_T)((uint32_T)rtb_LogicalOperator4_lw +
    BSER_cntChrgOCNpl);
  BSER_bChrgOCNplDet = (BSER_cntChrgOCNpl > BSER_cntChrgOCNpl_C);
  BSER_bChrgOCNplHeal = !BSER_bChrgOCNplDet;
  if (BSER_BattPrfDiag_DW.Delay2_DSTATE_mj) {
    rtb_Switch4_n = (uint8_T)~(uint8_T)((uint8_T)~DFC_ChrgOCNpl_ASW | 8U);
  } else {
    rtb_LogicalOperator4 = ((BSER_cntBattPrfDiag_CW & 1) == 1);
    if (rtb_LogicalOperator4 && BSER_bChrgOCNplDet) {
      rtb_Switch4_n = (uint8_T)(DFC_ChrgOCNpl_ASW | 152U);
    } else if ((!rtb_LogicalOperator4) || BSER_bChrgOCNplHeal) {
      rtb_Switch4_n = (uint8_T)((uint8_T)~(uint8_T)((uint8_T)~DFC_ChrgOCNpl_ASW |
        16U) | 8U);
    } else {
      rtb_Switch4_n = BSER_BattPrfDiag_DW.Delay3_DSTATE_el;
    }
  }

  BSER_BattPrfDiag_DW.Delay2_DSTATE_mj = false;
  BSER_BattPrfDiag_DW.Delay3_DSTATE_el = rtb_Switch4_n;
  DFC_ChrgOCNpl_ASW = rtb_Switch4_n;
  if (BSER_BattPrfDiag_DW.Delay2_DSTATE_n) {
    rtb_Switch4_n = (uint8_T)~(uint8_T)((uint8_T)~DFC_ChrgOCMax_ASW | 8U);
  } else if (rtb_Switch2_m) {
    rtb_Switch4_n = (uint8_T)(DFC_ChrgOCMax_ASW | 152U);
  } else if ((!rtb_RelationalOperator4_k) || BSER_bChrgOCMaxHeal) {
    rtb_Switch4_n = (uint8_T)((uint8_T)~(uint8_T)((uint8_T)~DFC_ChrgOCMax_ASW | 16U)
      | 8U);
  } else {
    rtb_Switch4_n = BSER_BattPrfDiag_DW.Delay3_DSTATE_b;
  }

  BSER_BattPrfDiag_DW.Delay2_DSTATE_n = false;
  BSER_BattPrfDiag_DW.Delay3_DSTATE_b = rtb_Switch4_n;
  DFC_ChrgOCMax_ASW = rtb_Switch4_n;
  if (BSER_BattPrfDiag_DW.Delay2_DSTATE_h) {
    rtb_Switch4_n = (uint8_T)~(uint8_T)((uint8_T)~DFC_ChrgOCMin_ASW | 8U);
  } else if (rtb_Switch4_e) {
    rtb_Switch4_n = (uint8_T)(DFC_ChrgOCMin_ASW | 152U);
  } else if ((!rtb_Compare_k) || BSER_bChrgOCMinHeal) {
    rtb_Switch4_n = (uint8_T)((uint8_T)~(uint8_T)((uint8_T)~DFC_ChrgOCMin_ASW | 16U)
      | 8U);
  } else {
    rtb_Switch4_n = BSER_BattPrfDiag_DW.Delay3_DSTATE_o;
  }

  BSER_BattPrfDiag_DW.Delay2_DSTATE_h = false;
  BSER_BattPrfDiag_DW.Delay3_DSTATE_o = rtb_Switch4_n;
  DFC_ChrgOCMin_ASW = rtb_Switch4_n;
  BSER_flgDisChOvrCurrCond = (rtb_LogicalOperator3_n &&
    rtb_RelationalOperator2_ed && rtb_LogicalOperator4_n && (BSPR_iBattCurr >
    0.0F));
  if (BSER_flgDisChOvrCurrCond) {
    BSER_BattPrfDiag_CurrProc(BSPR_iBattCurr, BSER_tiCurrPeakPer_C,
      &BSER_BattPrfDiag_B.sf_CurrProc_o, &BSER_BattPrfDiag_DW.sf_CurrProc_o);
    rtb_Abs = BSER_BattPrfDiag_B.sf_CurrProc_o.BSER_iCurrSqAvrgS_A[0];
    for (i = 0; i < 59; i++) {
      rtb_Abs += BSER_BattPrfDiag_B.sf_CurrProc_o.BSER_iCurrSqAvrgS_A[i + 1];
    }

    BSER_iSumShrtDisChCurrAct = rtb_Abs;
    BSER_BattPrfDiag_CurrProc(BSPR_iBattCurr, BSER_tiCurrConstPer_C,
      &BSER_BattPrfDiag_B.sf_CurrProc_c, &BSER_BattPrfDiag_DW.sf_CurrProc_c);
    rtb_SumofElements_f = BSER_BattPrfDiag_B.sf_CurrProc_c.BSER_iCurrSqAvrgS_A[0];
    for (i = 0; i < 59; i++) {
      rtb_SumofElements_f +=
        BSER_BattPrfDiag_B.sf_CurrProc_c.BSER_iCurrSqAvrgS_A[i + 1];
    }

    BSER_iSumLonDisChCurrAct = rtb_SumofElements_f;
    BSER_BattPrfDiag_CurrProc(BSER_iPeakCurrDisCh, BSER_tiCurrPeakPer_C,
      &BSER_BattPrfDiag_B.sf_CurrProc_ek, &BSER_BattPrfDiag_DW.sf_CurrProc_ek);
    rtb_Abs1 = BSER_BattPrfDiag_B.sf_CurrProc_ek.BSER_iCurrSqAvrgS_A[0];
    for (i = 0; i < 59; i++) {
      rtb_Abs1 += BSER_BattPrfDiag_B.sf_CurrProc_ek.BSER_iCurrSqAvrgS_A[i + 1];
    }

    BSER_iSumShrtDisChCurrReq = rtb_Abs1;
    BSER_BattPrfDiag_CurrProc(BSER_iConstCurrDisChMax, BSER_tiCurrConstPer_C,
      &BSER_BattPrfDiag_B.sf_CurrProc_j, &BSER_BattPrfDiag_DW.sf_CurrProc_j);
    rtb_Abs2 = BSER_BattPrfDiag_B.sf_CurrProc_j.BSER_iCurrSqAvrgS_A[0];
    for (i = 0; i < 59; i++) {
      rtb_Abs2 += BSER_BattPrfDiag_B.sf_CurrProc_j.BSER_iCurrSqAvrgS_A[i + 1];
    }

    BSER_iSumLonDisChCurrReq = rtb_Abs2;
    if ((BSER_iSumShrtDisChCurrReq > 0.0F) && (BSER_iSumLonDisChCurrReq > 0.0F))
    {
      BSER_iShrtDisChCurrPerOk = rtb_Abs1 * BSER_rShrtDisChCurrHeal_C;
      BSER_iLonDisChCurrPerOk = rtb_Abs2 * BSER_rLonDisChCurrHeal_C;
      BSER_BattPrfDiag_B.LogicalOperator1 = ((rtb_Abs1 >
        BSER_iSumShrtDisChCurrReq_C) && (rtb_Abs < BSER_iShrtDisChCurrPerOk) &&
        ((rtb_SumofElements_f < BSER_iLonDisChCurrPerOk) && (rtb_Abs2 >
        BSER_iSumLonDisChCurrReq_C)));
      BSER_iShrtDisChCurrPerErr = rtb_Abs1 * BSER_rShrtDisChCurrHi_C;
      BSER_iLonDisChCurrPerErr = rtb_Abs2 * BSER_rLonDisChCurrHi_C;
      BSER_BattPrfDiag_B.LogicalOperator4 = ((rtb_Abs >
        BSER_iShrtDisChCurrPerErr) || (rtb_SumofElements_f >
        BSER_iLonDisChCurrPerErr));
    }

    if (BSPR_uBattVolt != 0.0F) {
      rtb_Abs = BSPR_uBattVolt;
    } else {
      rtb_Abs = BSER_uBattVoltConst_C;
    }

    rtb_SumofElements_dw = (BSER_iConstCurrDisChMax - BSPR_iBattCurr) * 0.01F +
      BSER_iLonDisChCurrOk;
    if (rtb_SumofElements_dw > 50000.0F) {
      BSER_iLonDisChCurrOk = 50000.0F;
    } else if (rtb_SumofElements_dw < 0.0F) {
      BSER_iLonDisChCurrOk = 0.0F;
    } else {
      BSER_iLonDisChCurrOk = rtb_SumofElements_dw;
    }

    rtb_SumofElements_dw = (BSER_iPeakCurrDisCh - BSPR_iBattCurr) * 0.01F +
      BSER_iShrtDisChCurrOk;
    if (rtb_SumofElements_dw > 25000.0F) {
      BSER_iShrtDisChCurrOk = 25000.0F;
    } else if (rtb_SumofElements_dw < 0.0F) {
      BSER_iShrtDisChCurrOk = 0.0F;
    } else {
      BSER_iShrtDisChCurrOk = rtb_SumofElements_dw;
    }

    rtb_SumofElements_dw = (BSPR_iBattCurr - BSER_iConstCurrDisChMax) * 0.01F +
      BSER_iLonDisChCurrErr;
    if (rtb_SumofElements_dw > 50000.0F) {
      BSER_iLonDisChCurrErr = 50000.0F;
    } else if (rtb_SumofElements_dw < 0.0F) {
      BSER_iLonDisChCurrErr = 0.0F;
    } else {
      BSER_iLonDisChCurrErr = rtb_SumofElements_dw;
    }

    rtb_SumofElements_dw = (BSPR_iBattCurr - BSER_iPeakCurrDisCh) * 0.01F +
      BSER_iShrtDisChCurrErr;
    if (rtb_SumofElements_dw > 25000.0F) {
      BSER_iShrtDisChCurrErr = 25000.0F;
    } else if (rtb_SumofElements_dw < 0.0F) {
      BSER_iShrtDisChCurrErr = 0.0F;
    } else {
      BSER_iShrtDisChCurrErr = rtb_SumofElements_dw;
    }

    BSER_iDisChCurrOk = BSER_pConstPwrDisCh / rtb_Abs - BSPR_iBattCurr;
    rtb_LogicalOperator3_n = (BSER_iDisChCurrOk > BSER_iDisChCurrOk_C);
    if (rtb_LogicalOperator3_n) {
      rtb_SumofElements_f = 0.01F;
    } else {
      rtb_SumofElements_f = 0.0F;
    }

    if (BSER_tiDisChCurrOk_C <= BSER_BattPrfDiag_DW.Delay_DSTATE_c) {
      rtb_Abs1 = BSER_tiDisChCurrOk_C;
    } else {
      rtb_Abs1 = BSER_BattPrfDiag_DW.Delay_DSTATE_c;
    }

    rtb_SumofElements_f = (rtb_SumofElements_f + rtb_Abs1) * (real32_T)
      rtb_LogicalOperator3_n;
    BSER_bContDisChOpLimOk = (rtb_SumofElements_f > BSER_tiDisChCurrOk_C);
    BSER_BattPrfDiag_DW.Delay_DSTATE_c = rtb_SumofElements_f;
    if ((BSER_cntBattOCMinDiag_C & 1) == 1) {
      BSER_bShrtDisChOpLimOk = (BSER_iShrtDisChCurrOk > BSER_iShrtDisChCurrOk_C);
    } else {
      BSER_bShrtDisChOpLimOk = true;
    }

    if ((BSER_cntBattOCMinDiag_C & 2) == 2) {
      BSER_bLonDisChOpLimOk = (BSER_iLonDisChCurrOk > BSER_iLonDisChCurrOk_C);
    } else {
      BSER_bLonDisChOpLimOk = true;
    }

    BSER_bDisChOCMinHeal = (BSER_bContDisChOpLimOk && BSER_bShrtDisChOpLimOk &&
      BSER_bLonDisChOpLimOk);
    if ((BSER_cntBattOCMinDiag_C & 1) == 1) {
      BSER_bShrtDisChOpLimErr = (BSER_iShrtDisChCurrErr >
        BSER_iShrtDisChCurrErr_C);
    } else {
      BSER_bShrtDisChOpLimErr = false;
    }

    if ((BSER_cntBattOCMinDiag_C & 2) == 2) {
      BSER_bLonDisChOpLimErr = (BSER_iLonDisChCurrErr > BSER_iLonDisChCurrErr_C);
    } else {
      BSER_bLonDisChOpLimErr = false;
    }

    BSER_iDisChCurrErr = BSPR_iBattCurr - BSER_pPeakPwrDisCh *
      BSER_cntPwrConvertConst_C / rtb_Abs;
    rtb_LogicalOperator3_n = (BSER_iDisChCurrErr > BSER_iDisChCurrErr_C);
    if (rtb_LogicalOperator3_n) {
      rtb_SumofElements_f = 0.01F;
    } else {
      rtb_SumofElements_f = 0.0F;
    }

    if (BSER_tiDisChCurrErr_C <= BSER_BattPrfDiag_DW.Delay_DSTATE_b) {
      rtb_Abs1 = BSER_tiDisChCurrErr_C;
    } else {
      rtb_Abs1 = BSER_BattPrfDiag_DW.Delay_DSTATE_b;
    }

    rtb_Abs = (rtb_SumofElements_f + rtb_Abs1) * (real32_T)
      rtb_LogicalOperator3_n;
    BSER_bContDisChOpLimErr = (rtb_Abs > BSER_tiDisChCurrErr_C);
    BSER_BattPrfDiag_DW.Delay_DSTATE_b = rtb_Abs;
    BSER_bDisChOCMinDet = (BSER_bShrtDisChOpLimErr || BSER_bLonDisChOpLimErr ||
      BSER_bContDisChOpLimErr);
    BSER_bDisChOCMaxDet = BSER_BattPrfDiag_B.LogicalOperator4;
    BSER_bDisChOCMaxHeal = BSER_BattPrfDiag_B.LogicalOperator1;
  }

  rtb_LogicalOperator3_n = ((BSER_cntBattPrfDiag_CW & 16) == 16);
  rtb_RelationalOperator2_ed = (rtb_LogicalOperator3_n && BSER_bDisChOCMaxDet);
  rtb_LogicalOperator4_n = ((BSER_cntBattPrfDiag_CW & 32) == 32);
  rtb_RelationalOperator4_k = (rtb_LogicalOperator4_n && BSER_bDisChOCMinDet);
  rtb_Switch2_m = (rtb_RelationalOperator2_ed || rtb_RelationalOperator4_k);
  rtb_Compare_k = ((!BSER_BattPrfDiag_DW.Delay1_DSTATE) && rtb_Switch2_m);
  BSER_BattPrfDiag_DW.Delay1_DSTATE = rtb_Switch2_m;
  BSER_cntDisChOCNpl = (uint8_T)((uint32_T)rtb_Compare_k + BSER_cntDisChOCNpl);
  BSER_bDisChOCNplDet = (BSER_cntDisChOCNpl >= BSER_cntDisChOCNpl_C);
  BSER_bDisChOCNplHeal = !BSER_bDisChOCNplDet;
  if (BSER_BattPrfDiag_DW.Delay2_DSTATE) {
    rtb_Switch4_n = (uint8_T)~(uint8_T)((uint8_T)~DFC_DisChOCNpl_ASW | 8U);
  } else {
    rtb_Switch2_m = ((BSER_cntBattPrfDiag_CW & 8) == 8);
    if (rtb_Switch2_m && BSER_bDisChOCNplDet) {
      rtb_Switch4_n = (uint8_T)(DFC_DisChOCNpl_ASW | 152U);
    } else if ((!rtb_Switch2_m) || BSER_bDisChOCNplHeal) {
      rtb_Switch4_n = (uint8_T)((uint8_T)~(uint8_T)((uint8_T)~DFC_DisChOCNpl_ASW |
        16U) | 8U);
    } else {
      rtb_Switch4_n = BSER_BattPrfDiag_DW.Delay3_DSTATE;
    }
  }

  BSER_BattPrfDiag_DW.Delay2_DSTATE = false;
  BSER_BattPrfDiag_DW.Delay3_DSTATE = rtb_Switch4_n;
  DFC_DisChOCNpl_ASW = rtb_Switch4_n;
  if (BSER_BattPrfDiag_DW.Delay2_DSTATE_g) {
    rtb_Switch4_n = (uint8_T)~(uint8_T)((uint8_T)~DFC_DisChOCMax_ASW | 8U);
  } else if (rtb_RelationalOperator2_ed) {
    rtb_Switch4_n = (uint8_T)(DFC_DisChOCMax_ASW | 152U);
  } else if ((!rtb_LogicalOperator3_n) || BSER_bDisChOCMaxHeal) {
    rtb_Switch4_n = (uint8_T)((uint8_T)~(uint8_T)((uint8_T)~DFC_DisChOCMax_ASW | 16U)
      | 8U);
  } else {
    rtb_Switch4_n = BSER_BattPrfDiag_DW.Delay3_DSTATE_i;
  }

  BSER_BattPrfDiag_DW.Delay2_DSTATE_g = false;
  BSER_BattPrfDiag_DW.Delay3_DSTATE_i = rtb_Switch4_n;
  DFC_DisChOCMax_ASW = rtb_Switch4_n;
  if (BSER_BattPrfDiag_DW.Delay2_DSTATE_m) {
    rtb_Switch4_n = (uint8_T)~(uint8_T)((uint8_T)~DFC_DisChOCMin_ASW | 8U);
  } else if (rtb_RelationalOperator4_k) {
    rtb_Switch4_n = (uint8_T)(DFC_DisChOCMin_ASW | 152U);
  } else if ((!rtb_LogicalOperator4_n) || BSER_bDisChOCMinHeal) {
    rtb_Switch4_n = (uint8_T)((uint8_T)~(uint8_T)((uint8_T)~DFC_DisChOCMin_ASW | 16U)
      | 8U);
  } else {
    rtb_Switch4_n = BSER_BattPrfDiag_DW.Delay3_DSTATE_e;
  }

  BSER_BattPrfDiag_DW.Delay2_DSTATE_m = false;
  BSER_BattPrfDiag_DW.Delay3_DSTATE_e = rtb_Switch4_n;
  DFC_DisChOCMin_ASW = rtb_Switch4_n;
}

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

  /* block I/O */

  /* custom signals */
  BSER_bChrgOCNplDet = false;
  BSER_bChrgOCNplHeal = false;
  BSER_bDisChOCNplDet = false;
  BSER_bDisChOCNplHeal = false;
  BSER_bShrtDisChOpLimOk = false;
  BSER_bLonDisChOpLimOk = false;
  BSER_bDisChOCMinHeal = false;
  BSER_bShrtDisChOpLimErr = false;
  BSER_bLonDisChOpLimErr = false;
  BSER_bDisChOCMinDet = false;
  BSER_bDisChOCMaxDet = false;
  BSER_bDisChOCMaxHeal = false;
  BSER_bContDisChOpLimOk = false;
  BSER_bContDisChOpLimErr = false;
  BSER_bShrtChrgOpLimErr = false;
  BSER_bLonChrgOpLimErr = false;
  BSER_bChrgOCMinDet = false;
  BSER_bShrtChrgOpLimOk = false;
  BSER_bLonChrgOpLimOk = false;
  BSER_bChrgOCMinHeal = false;
  BSER_bChrgOCMaxDet = false;
  BSER_bChrgOCMaxHeal = false;
  BSER_bContChrgOpLimOk = false;
  BSER_bContChrgOpLimErr = false;
  BSER_BattPrfDiag_DW.Delay1_DSTATE_k = true;
  BSER_BattPrfDiag_DW.Delay2_DSTATE_mj = true;
  BSER_BattPrfDiag_DW.Delay2_DSTATE_n = true;
  BSER_BattPrfDiag_DW.Delay2_DSTATE_h = true;
  BSER_BattPrfDiag_DW.Delay1_DSTATE = true;
  BSER_BattPrfDiag_DW.Delay2_DSTATE = true;
  BSER_BattPrfDiag_DW.Delay2_DSTATE_g = true;
  BSER_BattPrfDiag_DW.Delay2_DSTATE_m = true;
}

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