/*
 * File: BSPR_BattVoltDiag.c
 *
 * Code generated for Simulink model 'BSPR_BattVoltDiag'.
 *
 * Model version                  : 1.662
 * Simulink Coder version         : 8.13 (R2017b) 24-Jul-2017
 * C/C++ source code generated on : Fri Oct  9 11:29:17 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 "BSPR_BattVoltDiag.h"
#include "BSPR_BattVoltDiag_private.h"
#include "look1_iflf_binlxpw.h"

/* Named constants for Chart: '<S5>/DiagModeCalc' */
#define BSPR_BattVoltD_IN_DiagMode1_Stt ((uint8_T)2U)
#define BSPR_BattVoltD_IN_DiagMode2_Stt ((uint8_T)3U)
#define BSPR_BattVoltD_IN_DiagMode3_Stt ((uint8_T)4U)
#define BSPR_BattVoltD_IN_FaultMode_Stt ((uint8_T)5U)
#define BSPR_BattVoltDia_IN_DftMode_Stt ((uint8_T)1U)

/* Exported data definition */

/* Definition for custom storage class: FileScope */
static uint8_T BSPR_cntSum1;
static uint8_T BSPR_cntSum2;
static boolean_T BSPR_stHvDiagMode0;
static boolean_T BSPR_stHvDiagMode1;
static boolean_T BSPR_stHvDiagMode2;
static boolean_T BSPR_stHvDiagMode3;
static real32_T BSPR_uBattVoltLoDiag;
static real32_T BSPR_uMaxCriteria1;
static real32_T BSPR_uMaxCriteria2;
static real32_T BSPR_uMinCriteria1;
static real32_T BSPR_uMinCriteria2;
static real32_T BSPR_uSumCriteria1;
static real32_T BSPR_uSumCriteria2;

/* 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_BattUNpl_ASW;         /* Npl fault of battery voltage upstream the main contactor */
volatile uint8_T DFC_BattVoltHiMax_ASW;    /* Valid fault for battery voltage upstream the main contactor */
volatile uint8_T DFC_BattVoltLoMin_ASW;    /* Valid fault for battery downstream the main contactor */
volatile uint8_T DFC_DChrgUNpl_ASW;        /* Npl fault of battery voltage downstream the DC charge contactor */
volatile uint8_T DFC_PackUNpl_ASW;         /* Npl fault of battery voltage downstream the main contactor */

#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_bBattVoltErrDet;/* Error healing flag for battery voltage plausible diagnosis upstream the main contactor */
volatile boolean_T BSPR_bBattVoltErrHeal;/* Error detect flag for battery voltage plausible diagnosis upstream the main contactor */
volatile boolean_T BSPR_bBattVoltHiErrDet;/* Error healing flag for battery voltage plausible diagnosis upstream the main contactor */
volatile boolean_T BSPR_bBattVoltHiErrHeal;/* Error detect flag for battery voltage plausible diagnosis upstream the main contactor */
volatile boolean_T BSPR_bBattVoltLoErrDet;/* Error healing flag for battery voltage plausible diagnosis upstream the main contactor */
volatile boolean_T BSPR_bBattVoltLoErrHeal;/* Error detect flag for battery voltage plausible diagnosis upstream the main contactor */
volatile boolean_T BSPR_bDCHrgVoltNplErrDet;/* Error healing flag for DC charge  battery voltage plausible diagnosis */
volatile boolean_T BSPR_bDCHrgVoltNplErrHeal;/* Error detect flag for DC charge battery voltage plausible diagnosis */
volatile boolean_T BSPR_bPackVoltErrDet;/* Error healing flag for battery voltage plausible diagnosis  downstream  the main contactor */
volatile boolean_T BSPR_bPackVoltErrHeal;/* Error detect flag for battery voltage plausible diagnosis downstream the main contactor */
volatile uint8_T BSPR_cntSum;          /* Available number of voltage sample used for high voltage plausible diagnosis */
volatile boolean_T BSPR_flgMeanValCalcEn;/* Calculate mode for mean vlaue calculate */
volatile boolean_T BSPR_flgUBattDiagEn;/* Enalbe condition for battery voltage plausible diagnosis */
volatile boolean_T BSPR_flgUDChrgDiagEn;/* Enalbe condition for DC charge voltage plausible diagnosis */
volatile boolean_T BSPR_flgUPackDiagEn;/* Enalbe condition for pack voltage plausible diagnosis */
volatile uint8_T BSPR_stDiagMode;      /* Diagnostic mode for battery high voltage */
volatile real32_T BSPR_uMaxCriteria;   /* Maximum voltage among all  voltage sampling used for high voltage plausible diagnosis */
volatile real32_T BSPR_uMeanCoord;     /* Model voltage for battery voltage plausible diagnosis */
volatile real32_T BSPR_uMinCriteria;   /* Minimum voltage among all voltage sampling used for high voltage plausible diagnosis */
volatile real32_T BSPR_uSumCriteria;   /* Sum voltage of all voltage sampling used for high voltage plausible diagnosis */

#define ASW1_OEM_STOP_SEC_DEFAULT_RAM2_CLEARED_UNSPECIFIED
#include "Asw1_OEM_MemMap.h"

/* Block states (auto storage) */
DW_BSPR_BattVoltDiag_T BSPR_BattVoltDiag_DW;

/* Model step function */
void BSPR_BattVoltDiag_step(void)
{
  boolean_T rtb_Compare_mi;
  boolean_T rtb_Compare_lu;
  boolean_T rtb_Compare_j;
  boolean_T rtb_Compare_o1;
  real32_T rtb_Divide;
  real32_T rtb_Divide1;
  boolean_T rtb_LogicalOperator1_d;
  boolean_T rtb_LogicalOperator10;
  boolean_T rtb_LogicalOperator4;
  boolean_T rtb_LogicalOperator5_f;
  boolean_T rtb_LogicalOperator6_i;
  boolean_T rtb_LogicalOperator8_p;
  uint8_T rtb_Switch4_g;
  real32_T u1;
  real32_T tmp;
  rtb_Compare_mi = ((DFC_BattVoltVld_ASW & 16) == 16);
  rtb_Compare_lu = ((DFC_PackVoltVld_ASW & 16) == 16);
  rtb_Compare_j = ((DFC_DChrgVoltVld_ASW & 16) == 16);
  BSPR_stHvDiagMode0 = (rtb_Compare_mi || rtb_Compare_lu || rtb_Compare_j);
  BSPR_stHvDiagMode1 = ((BMCR_stBmsModeCtrl == 2) || (BMCR_stBmsModeCtrl == 3));
  rtb_Compare_o1 = (BCCR_stHVConnSt == 2);
  BSPR_stHvDiagMode2 = (((BMCR_stBmsModeCtrl == 4) || (BMCR_stBmsModeCtrl == 6))
                        && rtb_Compare_o1);
  BSPR_stHvDiagMode3 = (rtb_Compare_o1 && (BMCR_stBmsModeCtrl == 5) &&
                        (BCCR_bDChrgPosCtrSwtOn && BCCR_bDChrgNegCtrSwtOn));
  if (BSPR_BattVoltDiag_DW.is_active_c3_BSPR_BattVoltDiag == 0U) {
    BSPR_BattVoltDiag_DW.is_active_c3_BSPR_BattVoltDiag = 1U;
    BSPR_BattVoltDiag_DW.is_c3_BSPR_BattVoltDiag =
      BSPR_BattVoltDia_IN_DftMode_Stt;
    BSPR_stDiagMode = 0U;
    BSPR_flgMeanValCalcEn = false;
  } else {
    switch (BSPR_BattVoltDiag_DW.is_c3_BSPR_BattVoltDiag) {
     case BSPR_BattVoltDia_IN_DftMode_Stt:
      BSPR_stDiagMode = 0U;
      BSPR_flgMeanValCalcEn = false;
      if ((!BSPR_stHvDiagMode0) && BSPR_stHvDiagMode1 && (!BSPR_stHvDiagMode2) &&
          (!BSPR_stHvDiagMode3)) {
        BSPR_BattVoltDiag_DW.is_c3_BSPR_BattVoltDiag =
          BSPR_BattVoltD_IN_DiagMode1_Stt;
        BSPR_stDiagMode = 1U;
        BSPR_flgMeanValCalcEn = false;
      } else if (BSPR_stHvDiagMode0) {
        BSPR_BattVoltDiag_DW.is_c3_BSPR_BattVoltDiag =
          BSPR_BattVoltD_IN_FaultMode_Stt;
        BSPR_stDiagMode = 4U;
        BSPR_flgMeanValCalcEn = false;
      } else if ((!BSPR_stHvDiagMode0) && (!BSPR_stHvDiagMode1) &&
                 BSPR_stHvDiagMode2 && (!BSPR_stHvDiagMode3)) {
        BSPR_BattVoltDiag_DW.is_c3_BSPR_BattVoltDiag =
          BSPR_BattVoltD_IN_DiagMode2_Stt;
        BSPR_stDiagMode = 2U;
        BSPR_flgMeanValCalcEn = true;
      } else {
        if ((!BSPR_stHvDiagMode0) && (!BSPR_stHvDiagMode1) &&
            (!BSPR_stHvDiagMode2) && BSPR_stHvDiagMode3) {
          BSPR_BattVoltDiag_DW.is_c3_BSPR_BattVoltDiag =
            BSPR_BattVoltD_IN_DiagMode3_Stt;
          BSPR_stDiagMode = 3U;
          BSPR_flgMeanValCalcEn = true;
        }
      }
      break;

     case BSPR_BattVoltD_IN_DiagMode1_Stt:
      BSPR_stDiagMode = 1U;
      BSPR_flgMeanValCalcEn = false;
      if ((!BSPR_stHvDiagMode0) && (!BSPR_stHvDiagMode1) && BSPR_stHvDiagMode2 &&
          (!BSPR_stHvDiagMode3)) {
        BSPR_BattVoltDiag_DW.is_c3_BSPR_BattVoltDiag =
          BSPR_BattVoltD_IN_DiagMode2_Stt;
        BSPR_stDiagMode = 2U;
        BSPR_flgMeanValCalcEn = true;
      } else if (BSPR_stHvDiagMode0) {
        BSPR_BattVoltDiag_DW.is_c3_BSPR_BattVoltDiag =
          BSPR_BattVoltD_IN_FaultMode_Stt;
        BSPR_stDiagMode = 4U;
        BSPR_flgMeanValCalcEn = false;
      } else if ((!BSPR_stHvDiagMode0) && (!BSPR_stHvDiagMode1) &&
                 (!BSPR_stHvDiagMode2) && (!BSPR_stHvDiagMode3)) {
        BSPR_BattVoltDiag_DW.is_c3_BSPR_BattVoltDiag =
          BSPR_BattVoltDia_IN_DftMode_Stt;
        BSPR_stDiagMode = 0U;
        BSPR_flgMeanValCalcEn = false;
      } else {
        if ((!BSPR_stHvDiagMode0) && (!BSPR_stHvDiagMode1) &&
            (!BSPR_stHvDiagMode2) && BSPR_stHvDiagMode3) {
          BSPR_BattVoltDiag_DW.is_c3_BSPR_BattVoltDiag =
            BSPR_BattVoltD_IN_DiagMode3_Stt;
          BSPR_stDiagMode = 3U;
          BSPR_flgMeanValCalcEn = true;
        }
      }
      break;

     case BSPR_BattVoltD_IN_DiagMode2_Stt:
      BSPR_stDiagMode = 2U;
      BSPR_flgMeanValCalcEn = true;
      if ((!BSPR_stHvDiagMode0) && (!BSPR_stHvDiagMode1) && (!BSPR_stHvDiagMode2)
          && BSPR_stHvDiagMode3) {
        BSPR_BattVoltDiag_DW.is_c3_BSPR_BattVoltDiag =
          BSPR_BattVoltD_IN_DiagMode3_Stt;
        BSPR_stDiagMode = 3U;
        BSPR_flgMeanValCalcEn = true;
      } else if (BSPR_stHvDiagMode0) {
        BSPR_BattVoltDiag_DW.is_c3_BSPR_BattVoltDiag =
          BSPR_BattVoltD_IN_FaultMode_Stt;
        BSPR_stDiagMode = 4U;
        BSPR_flgMeanValCalcEn = false;
      } else if ((!BSPR_stHvDiagMode0) && BSPR_stHvDiagMode1 &&
                 (!BSPR_stHvDiagMode2) && (!BSPR_stHvDiagMode3)) {
        BSPR_BattVoltDiag_DW.is_c3_BSPR_BattVoltDiag =
          BSPR_BattVoltD_IN_DiagMode1_Stt;
        BSPR_stDiagMode = 1U;
        BSPR_flgMeanValCalcEn = false;
      } else {
        if ((!BSPR_stHvDiagMode0) && (!BSPR_stHvDiagMode1) &&
            (!BSPR_stHvDiagMode2) && (!BSPR_stHvDiagMode3)) {
          BSPR_BattVoltDiag_DW.is_c3_BSPR_BattVoltDiag =
            BSPR_BattVoltDia_IN_DftMode_Stt;
          BSPR_stDiagMode = 0U;
          BSPR_flgMeanValCalcEn = false;
        }
      }
      break;

     case BSPR_BattVoltD_IN_DiagMode3_Stt:
      BSPR_stDiagMode = 3U;
      BSPR_flgMeanValCalcEn = true;
      if (BSPR_stHvDiagMode0) {
        BSPR_BattVoltDiag_DW.is_c3_BSPR_BattVoltDiag =
          BSPR_BattVoltD_IN_FaultMode_Stt;
        BSPR_stDiagMode = 4U;
        BSPR_flgMeanValCalcEn = false;
      } else if ((!BSPR_stHvDiagMode0) && (!BSPR_stHvDiagMode1) &&
                 BSPR_stHvDiagMode2 && (!BSPR_stHvDiagMode3)) {
        BSPR_BattVoltDiag_DW.is_c3_BSPR_BattVoltDiag =
          BSPR_BattVoltD_IN_DiagMode2_Stt;
        BSPR_stDiagMode = 2U;
        BSPR_flgMeanValCalcEn = true;
      } else if ((!BSPR_stHvDiagMode0) && (!BSPR_stHvDiagMode1) &&
                 (!BSPR_stHvDiagMode2) && (!BSPR_stHvDiagMode3)) {
        BSPR_BattVoltDiag_DW.is_c3_BSPR_BattVoltDiag =
          BSPR_BattVoltDia_IN_DftMode_Stt;
        BSPR_stDiagMode = 0U;
        BSPR_flgMeanValCalcEn = false;
      } else {
        if ((!BSPR_stHvDiagMode0) && BSPR_stHvDiagMode1 && (!BSPR_stHvDiagMode2)
            && (!BSPR_stHvDiagMode3)) {
          BSPR_BattVoltDiag_DW.is_c3_BSPR_BattVoltDiag =
            BSPR_BattVoltD_IN_DiagMode1_Stt;
          BSPR_stDiagMode = 1U;
          BSPR_flgMeanValCalcEn = false;
        }
      }
      break;

     default:
      BSPR_stDiagMode = 4U;
      BSPR_flgMeanValCalcEn = false;
      if ((!BSPR_stHvDiagMode0) && BSPR_stHvDiagMode1 && (!BSPR_stHvDiagMode2) &&
          (!BSPR_stHvDiagMode3)) {
        BSPR_BattVoltDiag_DW.is_c3_BSPR_BattVoltDiag =
          BSPR_BattVoltD_IN_DiagMode1_Stt;
        BSPR_stDiagMode = 1U;
        BSPR_flgMeanValCalcEn = false;
      } else if ((!BSPR_stHvDiagMode0) && (!BSPR_stHvDiagMode1) &&
                 BSPR_stHvDiagMode2 && (!BSPR_stHvDiagMode3)) {
        BSPR_BattVoltDiag_DW.is_c3_BSPR_BattVoltDiag =
          BSPR_BattVoltD_IN_DiagMode2_Stt;
        BSPR_stDiagMode = 2U;
        BSPR_flgMeanValCalcEn = true;
      } else if ((!BSPR_stHvDiagMode0) && (!BSPR_stHvDiagMode1) &&
                 (!BSPR_stHvDiagMode2) && BSPR_stHvDiagMode3) {
        BSPR_BattVoltDiag_DW.is_c3_BSPR_BattVoltDiag =
          BSPR_BattVoltD_IN_DiagMode3_Stt;
        BSPR_stDiagMode = 3U;
        BSPR_flgMeanValCalcEn = true;
      } else {
        if ((!BSPR_stHvDiagMode0) && (!BSPR_stHvDiagMode1) &&
            (!BSPR_stHvDiagMode2) && (!BSPR_stHvDiagMode3)) {
          BSPR_BattVoltDiag_DW.is_c3_BSPR_BattVoltDiag =
            BSPR_BattVoltDia_IN_DftMode_Stt;
          BSPR_stDiagMode = 0U;
          BSPR_flgMeanValCalcEn = false;
        }
      }
      break;
    }
  }

  if (BSPR_flgMeanValCalcEn) {
    if (BSPR_uBattVolt > BSPR_uSumVoltCell) {
      u1 = BSPR_uBattVolt;
    } else {
      u1 = BSPR_uSumVoltCell;
    }

    if (BSPR_uPackVoltRaw > u1) {
      BSPR_uMaxCriteria1 = BSPR_uPackVoltRaw;
    } else {
      BSPR_uMaxCriteria1 = u1;
    }

    if (BSPR_uBattVolt < BSPR_uSumVoltCell) {
      u1 = BSPR_uBattVolt;
    } else {
      u1 = BSPR_uSumVoltCell;
    }

    if (BSPR_uPackVoltRaw < u1) {
      BSPR_uMinCriteria1 = BSPR_uPackVoltRaw;
    } else {
      BSPR_uMinCriteria1 = u1;
    }

    BSPR_uSumCriteria1 = (BSPR_uBattVolt + BSPR_uSumVoltCell) +
      BSPR_uPackVoltRaw;
    if ((BSPR_stDiagMode == 3) && (BSPR_flgVoltDChrgCsdr_CW == 1)) {
      BSPR_uSumCriteria2 = BSPR_uDChrgVoltRaw + BSPR_uSumCriteria1;
      if (BSPR_uDChrgVoltRaw > BSPR_uMaxCriteria1) {
        BSPR_uMaxCriteria2 = BSPR_uDChrgVoltRaw;
      } else {
        BSPR_uMaxCriteria2 = BSPR_uMaxCriteria1;
      }

      if (BSPR_uDChrgVoltRaw < BSPR_uMinCriteria1) {
        BSPR_uMinCriteria2 = BSPR_uDChrgVoltRaw;
      } else {
        BSPR_uMinCriteria2 = BSPR_uMinCriteria1;
      }

      BSPR_cntSum2 = (uint8_T)(BSPR_cntSum1 + 1U);
      BSPR_uMaxCriteria = BSPR_uMaxCriteria2;
      BSPR_uSumCriteria = BSPR_uSumCriteria2;
      BSPR_uMinCriteria = BSPR_uMinCriteria2;
      BSPR_cntSum = BSPR_cntSum2;
    } else {
      BSPR_uMaxCriteria = BSPR_uMaxCriteria1;
      BSPR_uSumCriteria = BSPR_uSumCriteria1;
      BSPR_uMinCriteria = BSPR_uMinCriteria1;
      BSPR_cntSum = BSPR_cntSum1;
    }

    rtb_Divide = (BSPR_uSumCriteria - BSPR_uMaxCriteria) / (real32_T)BSPR_cntSum;
    rtb_Divide1 = (BSPR_uSumCriteria - BSPR_uMinCriteria) / (real32_T)
      BSPR_cntSum;
    u1 = rtb_Divide - BSPR_uMinCriteria;
    tmp = BSPR_uMaxCriteria - rtb_Divide1;
    if (u1 == tmp) {
      BSPR_uMeanCoord = BSPR_uSumCriteria / (real32_T)(uint8_T)(BSPR_cntSum + 1U);
    } else if (u1 < tmp) {
      BSPR_uMeanCoord = rtb_Divide;
    } else {
      BSPR_uMeanCoord = rtb_Divide1;
    }
  }

  BSPR_flgUBattDiagEn = ((!rtb_Compare_mi) && BSPR_flgCurrHopEn);
  BSPR_flgUPackDiagEn = ((!rtb_Compare_lu) && ((BSPR_stDiagMode == 2) ||
    (BSPR_stDiagMode == 3)) && BSPR_flgCurrHopEn);
  BSPR_flgUDChrgDiagEn = ((BSPR_stDiagMode == 3) && (!rtb_Compare_j) &&
    BSPR_flgCurrHopEn);
  rtb_Compare_j = ((BSPR_cntHvBattDiag_CW & 1) == 1);
  rtb_Compare_lu = ((BSPR_cntHvBattDiag_CW & 2) == 2);
  rtb_Compare_mi = ((BSPR_cntHvBattDiag_CW & 4) == 4);
  rtb_Compare_o1 = ((BSPR_cntHvBattDiag_CW & 8) == 8);
  if ((BSPR_stDiagMode == 2) || (BSPR_stDiagMode == 3)) {
    u1 = BSPR_uMeanCoord;
  } else {
    u1 = BSPR_uSumVoltCell;
  }

  rtb_Divide = (real32_T)fabs(u1 - BSPR_uBattVolt);
  rtb_Divide1 = (real32_T)fabs(BSPR_uSumVoltCell - BSPR_uBattVolt);
  BSPR_uBattVoltLoDiag = look1_iflf_binlxpw(BSPR_tCellTempMin, ((const real32_T *)
    &(BSPR_uBattVoltLoDiag_CUR_X[0])), ((const real32_T *)
    &(BSPR_uBattVoltLoDiag_CUR[0])), 7U);
  rtb_LogicalOperator1_d = (BSPR_flgUBattDiagEn && (BSPR_uBattVolt >=
    BSPR_uBattVoltHiDiagThres_C));
  rtb_LogicalOperator10 = ((BSPR_flgUBattDiagEn && (rtb_Divide >
    BSPR_uBattVoltDiagThres_C)) || (BSPR_flgUBattDiagEn && (rtb_Divide1 >
    BSPR_uBattVoltDiagThres_C)));
  rtb_LogicalOperator4 = (BSPR_flgUBattDiagEn && (BSPR_uBattVolt <
    BSPR_uBattVoltHiDiagThres_C - BSPR_uBattVoltHystDiagThres_C));
  rtb_LogicalOperator5_f = (BSPR_flgUBattDiagEn && (BSPR_uBattVolt <=
    BSPR_uBattVoltLoDiag));
  rtb_LogicalOperator6_i = (BSPR_flgUBattDiagEn && (BSPR_uBattVolt >
    BSPR_uBattVoltLoDiag + BSPR_uBattVoltHystDiagThres_C));
  rtb_LogicalOperator8_p = (BSPR_flgUBattDiagEn && (rtb_Divide <=
    BSPR_uBattVoltDiagThres_C) && (BSPR_flgUBattDiagEn && (rtb_Divide1 <=
    BSPR_uBattVoltDiagThres_C)));
  if (rtb_LogicalOperator8_p) {
    rtb_Divide = 0.01F;
  } else {
    rtb_Divide = 0.0F;
  }

  if (BSPR_tiBattVoltDiagThres_C < BSPR_BattVoltDiag_DW.Delay_DSTATE_a) {
    u1 = BSPR_tiBattVoltDiagThres_C;
  } else {
    u1 = BSPR_BattVoltDiag_DW.Delay_DSTATE_a;
  }

  rtb_Divide = (rtb_Divide + u1) * (real32_T)rtb_LogicalOperator8_p;
  BSPR_BattVoltDiag_DW.Delay_DSTATE_a = rtb_Divide;
  BSPR_bBattVoltErrHeal = ((!rtb_Compare_j) || (rtb_Divide >
    BSPR_tiBattVoltDiagThres_C));
  if (rtb_LogicalOperator10) {
    rtb_Divide = 0.01F;
  } else {
    rtb_Divide = 0.0F;
  }

  if (BSPR_tiBattVoltDiagThres_C < BSPR_BattVoltDiag_DW.Delay_DSTATE_ms) {
    u1 = BSPR_tiBattVoltDiagThres_C;
  } else {
    u1 = BSPR_BattVoltDiag_DW.Delay_DSTATE_ms;
  }

  rtb_Divide = (rtb_Divide + u1) * (real32_T)rtb_LogicalOperator10;
  BSPR_BattVoltDiag_DW.Delay_DSTATE_ms = rtb_Divide;
  BSPR_bBattVoltErrDet = (rtb_Compare_j && (rtb_Divide >
    BSPR_tiBattVoltDiagThres_C));
  if (rtb_LogicalOperator1_d) {
    rtb_Divide = 0.01F;
  } else {
    rtb_Divide = 0.0F;
  }

  if (BSPR_tiBattVoltHiDiagThres_C < BSPR_BattVoltDiag_DW.Delay_DSTATE_bk) {
    u1 = BSPR_tiBattVoltHiDiagThres_C;
  } else {
    u1 = BSPR_BattVoltDiag_DW.Delay_DSTATE_bk;
  }

  rtb_Divide = (rtb_Divide + u1) * (real32_T)rtb_LogicalOperator1_d;
  BSPR_BattVoltDiag_DW.Delay_DSTATE_bk = rtb_Divide;
  BSPR_bBattVoltHiErrDet = (rtb_Compare_o1 && (rtb_Divide >
    BSPR_tiBattVoltHiDiagThres_C));
  if (rtb_LogicalOperator4) {
    rtb_Divide = 0.01F;
  } else {
    rtb_Divide = 0.0F;
  }

  if (BSPR_tiBattVoltHiDiagThres_C < BSPR_BattVoltDiag_DW.Delay_DSTATE_b) {
    u1 = BSPR_tiBattVoltHiDiagThres_C;
  } else {
    u1 = BSPR_BattVoltDiag_DW.Delay_DSTATE_b;
  }

  rtb_Divide = (rtb_Divide + u1) * (real32_T)rtb_LogicalOperator4;
  BSPR_BattVoltDiag_DW.Delay_DSTATE_b = rtb_Divide;
  BSPR_bBattVoltHiErrHeal = ((!rtb_Compare_o1) || (rtb_Divide >
    BSPR_tiBattVoltHiDiagThres_C));
  if (rtb_LogicalOperator5_f) {
    rtb_Divide = 0.01F;
  } else {
    rtb_Divide = 0.0F;
  }

  if (BSPR_tiBattVoltLoDiagThres_C < BSPR_BattVoltDiag_DW.Delay_DSTATE_m) {
    u1 = BSPR_tiBattVoltLoDiagThres_C;
  } else {
    u1 = BSPR_BattVoltDiag_DW.Delay_DSTATE_m;
  }

  rtb_Divide = (rtb_Divide + u1) * (real32_T)rtb_LogicalOperator5_f;
  BSPR_BattVoltDiag_DW.Delay_DSTATE_m = rtb_Divide;
  BSPR_bBattVoltLoErrDet = (rtb_Compare_o1 && (rtb_Divide >
    BSPR_tiBattVoltLoDiagThres_C));
  if (rtb_LogicalOperator6_i) {
    rtb_Divide = 0.01F;
  } else {
    rtb_Divide = 0.0F;
  }

  if (BSPR_tiBattVoltLoDiagThres_C < BSPR_BattVoltDiag_DW.Delay_DSTATE_ky) {
    u1 = BSPR_tiBattVoltLoDiagThres_C;
  } else {
    u1 = BSPR_BattVoltDiag_DW.Delay_DSTATE_ky;
  }

  rtb_Divide = (rtb_Divide + u1) * (real32_T)rtb_LogicalOperator6_i;
  BSPR_BattVoltDiag_DW.Delay_DSTATE_ky = rtb_Divide;
  BSPR_bBattVoltLoErrHeal = ((!rtb_Compare_o1) || (rtb_Divide >
    BSPR_tiBattVoltLoDiagThres_C));
  if (BSPR_BattVoltDiag_DW.Delay2_DSTATE_n) {
    rtb_Switch4_g = (uint8_T)~(uint8_T)((uint8_T)~DFC_BattUNpl_ASW | 8U);
  } else if (BSPR_bBattVoltErrDet) {
    rtb_Switch4_g = (uint8_T)(DFC_BattUNpl_ASW | 152U);
  } else if (BSPR_bBattVoltErrHeal) {
    rtb_Switch4_g = (uint8_T)((uint8_T)~(uint8_T)((uint8_T)~DFC_BattUNpl_ASW | 16U) |
      8U);
  } else {
    rtb_Switch4_g = BSPR_BattVoltDiag_DW.Delay3_DSTATE_i;
  }

  BSPR_BattVoltDiag_DW.Delay2_DSTATE_n = false;
  BSPR_BattVoltDiag_DW.Delay3_DSTATE_i = rtb_Switch4_g;
  DFC_BattUNpl_ASW = rtb_Switch4_g;
  rtb_Divide = (real32_T)fabs(BSPR_uPackVoltRaw - BSPR_uMeanCoord);
  rtb_Compare_j = (BSPR_flgUPackDiagEn && (rtb_Divide >
    BSPR_uPackVoltDiagThres_C));
  rtb_Compare_o1 = (BSPR_flgUPackDiagEn && (rtb_Divide <=
    BSPR_uPackVoltDiagThres_C));
  if (rtb_Compare_o1) {
    rtb_Divide = 0.01F;
  } else {
    rtb_Divide = 0.0F;
  }

  if (BSPR_tiPackVoltDiagThres_C < BSPR_BattVoltDiag_DW.Delay_DSTATE_e) {
    u1 = BSPR_tiPackVoltDiagThres_C;
  } else {
    u1 = BSPR_BattVoltDiag_DW.Delay_DSTATE_e;
  }

  rtb_Divide = (rtb_Divide + u1) * (real32_T)rtb_Compare_o1;
  BSPR_BattVoltDiag_DW.Delay_DSTATE_e = rtb_Divide;
  BSPR_bPackVoltErrHeal = ((!rtb_Compare_lu) || (rtb_Divide >
    BSPR_tiPackVoltDiagThres_C));
  if (rtb_Compare_j) {
    rtb_Divide = 0.01F;
  } else {
    rtb_Divide = 0.0F;
  }

  if (BSPR_tiPackVoltDiagThres_C < BSPR_BattVoltDiag_DW.Delay_DSTATE) {
    u1 = BSPR_tiPackVoltDiagThres_C;
  } else {
    u1 = BSPR_BattVoltDiag_DW.Delay_DSTATE;
  }

  rtb_Divide = (rtb_Divide + u1) * (real32_T)rtb_Compare_j;
  BSPR_BattVoltDiag_DW.Delay_DSTATE = rtb_Divide;
  BSPR_bPackVoltErrDet = (rtb_Compare_lu && (rtb_Divide >
    BSPR_tiPackVoltDiagThres_C));
  if (BSPR_BattVoltDiag_DW.Delay2_DSTATE_m) {
    rtb_Switch4_g = (uint8_T)~(uint8_T)((uint8_T)~DFC_PackUNpl_ASW | 8U);
  } else if (BSPR_bPackVoltErrDet) {
    rtb_Switch4_g = (uint8_T)(DFC_PackUNpl_ASW | 152U);
  } else if (BSPR_bPackVoltErrHeal) {
    rtb_Switch4_g = (uint8_T)((uint8_T)~(uint8_T)((uint8_T)~DFC_PackUNpl_ASW | 16U) |
      8U);
  } else {
    rtb_Switch4_g = BSPR_BattVoltDiag_DW.Delay3_DSTATE_k;
  }

  BSPR_BattVoltDiag_DW.Delay2_DSTATE_m = false;
  BSPR_BattVoltDiag_DW.Delay3_DSTATE_k = rtb_Switch4_g;
  DFC_PackUNpl_ASW = rtb_Switch4_g;
  rtb_Divide = (real32_T)fabs(BSPR_uDChrgVoltRaw - BSPR_uMeanCoord);
  rtb_Compare_lu = (BSPR_flgUDChrgDiagEn && (rtb_Divide >
    BSPR_uDChrgVoltDiagThres_C));
  rtb_Compare_j = (BSPR_flgUDChrgDiagEn && (rtb_Divide <=
    BSPR_uDChrgVoltDiagThres_C));
  if (rtb_Compare_j) {
    rtb_Divide = 0.01F;
  } else {
    rtb_Divide = 0.0F;
  }

  if (BSPR_tiDChrgVoltDiagThres_C < BSPR_BattVoltDiag_DW.Delay_DSTATE_k) {
    u1 = BSPR_tiDChrgVoltDiagThres_C;
  } else {
    u1 = BSPR_BattVoltDiag_DW.Delay_DSTATE_k;
  }

  rtb_Divide = (rtb_Divide + u1) * (real32_T)rtb_Compare_j;
  BSPR_BattVoltDiag_DW.Delay_DSTATE_k = rtb_Divide;
  BSPR_bDCHrgVoltNplErrHeal = ((!rtb_Compare_mi) || (rtb_Divide >
    BSPR_tiDChrgVoltDiagThres_C));
  if (rtb_Compare_lu) {
    rtb_Divide = 0.01F;
  } else {
    rtb_Divide = 0.0F;
  }

  if (BSPR_tiDChrgVoltDiagThres_C < BSPR_BattVoltDiag_DW.Delay_DSTATE_i) {
    u1 = BSPR_tiDChrgVoltDiagThres_C;
  } else {
    u1 = BSPR_BattVoltDiag_DW.Delay_DSTATE_i;
  }

  rtb_Divide = (rtb_Divide + u1) * (real32_T)rtb_Compare_lu;
  BSPR_BattVoltDiag_DW.Delay_DSTATE_i = rtb_Divide;
  BSPR_bDCHrgVoltNplErrDet = (rtb_Compare_mi && (rtb_Divide >
    BSPR_tiDChrgVoltDiagThres_C));
  if (BSPR_BattVoltDiag_DW.Delay2_DSTATE_k) {
    rtb_Switch4_g = (uint8_T)~(uint8_T)((uint8_T)~DFC_DChrgUNpl_ASW | 8U);
  } else if (BSPR_bDCHrgVoltNplErrDet) {
    rtb_Switch4_g = (uint8_T)(DFC_DChrgUNpl_ASW | 152U);
  } else if (BSPR_bDCHrgVoltNplErrHeal) {
    rtb_Switch4_g = (uint8_T)((uint8_T)~(uint8_T)((uint8_T)~DFC_DChrgUNpl_ASW | 16U)
      | 8U);
  } else {
    rtb_Switch4_g = BSPR_BattVoltDiag_DW.Delay3_DSTATE_e;
  }

  BSPR_BattVoltDiag_DW.Delay2_DSTATE_k = false;
  BSPR_BattVoltDiag_DW.Delay3_DSTATE_e = rtb_Switch4_g;
  DFC_DChrgUNpl_ASW = rtb_Switch4_g;
  if (BSPR_BattVoltDiag_DW.Delay2_DSTATE_l) {
    rtb_Switch4_g = (uint8_T)~(uint8_T)((uint8_T)~DFC_BattVoltHiMax_ASW | 8U);
  } else if (BSPR_bBattVoltHiErrDet) {
    rtb_Switch4_g = BSPR_BattVoltDiag_ConstB.FixPtBitwiseOperator1_p;
  } else if (BSPR_bBattVoltHiErrHeal) {
    rtb_Switch4_g = BSPR_BattVoltDiag_ConstB.FixPtBitwiseOperator1_h;
  } else {
    rtb_Switch4_g = BSPR_BattVoltDiag_DW.Delay3_DSTATE_a;
  }

  BSPR_BattVoltDiag_DW.Delay2_DSTATE_l = false;
  BSPR_BattVoltDiag_DW.Delay3_DSTATE_a = rtb_Switch4_g;
  DFC_BattVoltHiMax_ASW = rtb_Switch4_g;
  if (BSPR_BattVoltDiag_DW.Delay2_DSTATE) {
    rtb_Switch4_g = (uint8_T)~(uint8_T)((uint8_T)~DFC_BattVoltLoMin_ASW | 8U);
  } else if (BSPR_bBattVoltLoErrDet) {
    rtb_Switch4_g = BSPR_BattVoltDiag_ConstB.FixPtBitwiseOperator1_g;
  } else if (BSPR_bBattVoltLoErrHeal) {
    rtb_Switch4_g = BSPR_BattVoltDiag_ConstB.FixPtBitwiseOperator1_gf;
  } else {
    rtb_Switch4_g = BSPR_BattVoltDiag_DW.Delay3_DSTATE;
  }

  BSPR_BattVoltDiag_DW.Delay2_DSTATE = false;
  BSPR_BattVoltDiag_DW.Delay3_DSTATE = rtb_Switch4_g;
  DFC_BattVoltLoMin_ASW = rtb_Switch4_g;
}

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

  /* block I/O */

  /* custom signals */
  BSPR_bPackVoltErrHeal = false;
  BSPR_bPackVoltErrDet = false;
  BSPR_bDCHrgVoltNplErrHeal = false;
  BSPR_bDCHrgVoltNplErrDet = false;
  BSPR_bBattVoltErrHeal = false;
  BSPR_bBattVoltErrDet = false;
  BSPR_bBattVoltHiErrDet = false;
  BSPR_bBattVoltHiErrHeal = false;
  BSPR_bBattVoltLoErrDet = false;
  BSPR_bBattVoltLoErrHeal = false;
  BSPR_cntSum1 = 2U;
  BSPR_BattVoltDiag_DW.Delay2_DSTATE_n = true;
  BSPR_BattVoltDiag_DW.Delay2_DSTATE_m = true;
  BSPR_BattVoltDiag_DW.Delay2_DSTATE_k = true;
  BSPR_BattVoltDiag_DW.Delay2_DSTATE_l = true;
  BSPR_BattVoltDiag_DW.Delay2_DSTATE = true;
}

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