/*
 * File: BSER_SocRngDiag.c
 *
 * Code generated for Simulink model 'BSER_SocRngDiag'.
 *
 * Model version                  : 1.1395F
 * Simulink Coder version         : 8.11F (R2016b) 25-Aug-2016
 * C/C++ source code generated on : Thu Mar 26 14:37:30 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_SocRngDiag.h"
#include "BSER_SocRngDiag_private.h"

/* Exported data definition */

/* Definition for custom storage class: FileScope */
static boolean_T BSER_bBattSocRngHiWarnStt;
static boolean_T BSER_bBattSocRngLoFaultStt;
static boolean_T BSER_bBattSocRngLoWarnStt;
static boolean_T BSPR_bSocRngChrgCurrCond;
static boolean_T BSPR_bSocRngDisChCurrCond;

/* Volatile memory section */
 
 

/* Definition for custom storage class: Volatile */
volatile uint8_T DFC_SocRngHiMax_ASW;      /* Max fault of battery soc out of range high */
volatile uint8_T DFC_SocRngHiMin_ASW;      /* Min fault of battery soc out of range high */
volatile uint8_T DFC_SocRngLoMax_ASW;      /* Max fault of battery soc out of range low */
volatile uint8_T DFC_SocRngLoMin_ASW;      /* Min fault of battery soc out of range low */

 
 

/* Definition for custom storage class: OutputSignal */
volatile boolean_T BSER_bBattSocRngHiFaultDet;/* Fault detect flag for soc out of range high fault */
volatile boolean_T BSER_bBattSocRngHiFaultHeal;/* Fault healing flag for soc out of range high fault */
volatile boolean_T BSER_bBattSocRngHiWarnDet;/* Fault detect flag for soc out of range high warn */
volatile boolean_T BSER_bBattSocRngHiWarnHeal;/* Fault healing flag for soc out of range high warn */
volatile boolean_T BSER_bBattSocRngLoFaultDet;/* Fault detect flag for soc out of range low fault */
volatile boolean_T BSER_bBattSocRngLoFaultHeal;/* Fault healing flag for soc out of range low fault */
volatile boolean_T BSER_bBattSocRngLoWarnDet;/* Fault detect flag for soc out of range low warn */
volatile boolean_T BSER_bBattSocRngLoWarnHeal;/* Fault healing flag for soc out of range low warn */

/* Block states (auto storage) */
DW_BSER_SocRngDiag_T BSER_SocRngDiag_DW;

/* Model step function */
void BSER_SocRngDiag_step(void)
{
  boolean_T rtb_BSER_bBattSocRngHiFaultStt;
  boolean_T rtb_LogicalOperator1;
  boolean_T rtb_LogicalOperator2;
  real32_T rtb_Saturation_d;
  boolean_T rtb_RelationalOperator1_l;
  real32_T rtb_Product_a;
  uint8_T rtb_Switch4_i;
  real32_T rtb_RelationalOperator1_g;
  rtb_BSER_bBattSocRngHiFaultStt = (BSER_rSoc < BSER_rBattSocRngHiFault_C);
  rtb_LogicalOperator1 = !rtb_BSER_bBattSocRngHiFaultStt;
  if (rtb_LogicalOperator1) {
    if (!BSER_SocRngDiag_DW.CurrCond_MODE) {
      BSER_SocRngDiag_DW.Delay_DSTATE_mz = 0.0F;
      BSER_SocRngDiag_DW.Delay_DSTATE_mh = 0.0F;
      BSER_SocRngDiag_DW.CurrCond_MODE = true;
    }

    rtb_Saturation_d = 0.01F * BSPR_iBattCurr +
      BSER_SocRngDiag_DW.Delay_DSTATE_mz;
    if (rtb_Saturation_d > 0.0F) {
      rtb_Saturation_d = 0.0F;
    } else {
      if (rtb_Saturation_d < -10000.0F) {
        rtb_Saturation_d = -10000.0F;
      }
    }

    rtb_RelationalOperator1_l = (BSPR_iBattCurr <= BSPR_iSocRngChrgCurrHi_C);
    if (rtb_RelationalOperator1_l) {
      rtb_RelationalOperator1_g = 0.01F;
    } else {
      rtb_RelationalOperator1_g = 0.0F;
    }

    if (BSPR_tiSocRngCurrCont_C <= BSER_SocRngDiag_DW.Delay_DSTATE_mh) {
      rtb_Product_a = BSPR_tiSocRngCurrCont_C;
    } else {
      rtb_Product_a = BSER_SocRngDiag_DW.Delay_DSTATE_mh;
    }

    rtb_Product_a = (rtb_RelationalOperator1_g + rtb_Product_a) * (real32_T)
      rtb_RelationalOperator1_l;
    BSER_SocRngDiag_DW.Delay_DSTATE_mh = rtb_Product_a;
    BSPR_bSocRngChrgCurrCond = (((real32_T)fabs(rtb_Saturation_d) >=
      BSER_iSocRngChCurrAccuThres_C) || (rtb_Product_a > BSPR_tiSocRngCurrCont_C));
    BSER_SocRngDiag_DW.Delay_DSTATE_mz = rtb_Saturation_d;
  } else {
    if (BSER_SocRngDiag_DW.CurrCond_MODE) {
      BSER_SocRngDiag_DW.CurrCond_MODE = false;
    }
  }

  rtb_RelationalOperator1_l = ((BSER_cntSocRngDiag_CW & 2) == 2);
  if (rtb_RelationalOperator1_l) {
    BSER_bBattSocRngHiFaultDet = (rtb_LogicalOperator1 &&
      BSPR_bSocRngChrgCurrCond);
  } else {
    BSER_bBattSocRngHiFaultDet = false;
  }

  rtb_LogicalOperator1 = ((!((BSER_cntSocRngDiag_CW & 1) == 1)) ||
    BSER_bBattSocRngHiFaultDet);
  BSER_bBattSocRngHiWarnStt = ((BSER_rSoc >= BSER_rBattSocRngHiWarn_C) &&
    (!BSER_bBattSocRngHiFaultDet));
  if (BSER_bBattSocRngHiWarnStt) {
    rtb_Product_a = 0.01F;
  } else {
    rtb_Product_a = 0.0F;
  }

  if (BSER_tiBattSocRngHiWarn_C <= BSER_SocRngDiag_DW.Delay_DSTATE_p) {
    rtb_Saturation_d = BSER_tiBattSocRngHiWarn_C;
  } else {
    rtb_Saturation_d = BSER_SocRngDiag_DW.Delay_DSTATE_p;
  }

  rtb_Saturation_d = (rtb_Product_a + rtb_Saturation_d) * (real32_T)
    BSER_bBattSocRngHiWarnStt;
  BSER_SocRngDiag_DW.Delay_DSTATE_p = rtb_Saturation_d;
  BSER_bBattSocRngHiWarnDet = ((!rtb_LogicalOperator1) && (rtb_Saturation_d >
    BSER_tiBattSocRngHiWarn_C));
  rtb_LogicalOperator2 = !BSER_bBattSocRngHiWarnStt;
  if (rtb_LogicalOperator2) {
    rtb_Saturation_d = 0.01F;
  } else {
    rtb_Saturation_d = 0.0F;
  }

  if (BSER_tiBattSocRngHiWarn_C <= BSER_SocRngDiag_DW.Delay_DSTATE_gy) {
    rtb_Product_a = BSER_tiBattSocRngHiWarn_C;
  } else {
    rtb_Product_a = BSER_SocRngDiag_DW.Delay_DSTATE_gy;
  }

  rtb_Saturation_d = (rtb_Saturation_d + rtb_Product_a) * (real32_T)
    rtb_LogicalOperator2;
  BSER_SocRngDiag_DW.Delay_DSTATE_gy = rtb_Saturation_d;
  BSER_bBattSocRngHiWarnHeal = (rtb_LogicalOperator1 || (rtb_Saturation_d >
    BSER_tiBattSocRngHiWarn_C));
  if (BSER_SocRngDiag_DW.Delay2_DSTATE_j) {
    rtb_Switch4_i = (uint8_T)~(uint8_T)((uint8_T)~DFC_SocRngHiMin_ASW | 8U);
  } else if (BSER_bBattSocRngHiWarnDet) {
    rtb_Switch4_i = (uint8_T)(DFC_SocRngHiMin_ASW | 152U);
  } else if (BSER_bBattSocRngHiWarnHeal) {
    rtb_Switch4_i = (uint8_T)((uint8_T)~(uint8_T)((uint8_T)~DFC_SocRngHiMin_ASW |
      16U) | 8U);
  } else {
    rtb_Switch4_i = BSER_SocRngDiag_DW.Delay3_DSTATE_f;
  }

  BSER_SocRngDiag_DW.Delay2_DSTATE_j = false;
  BSER_SocRngDiag_DW.Delay3_DSTATE_f = rtb_Switch4_i;
  DFC_SocRngHiMin_ASW = rtb_Switch4_i;
  if (rtb_BSER_bBattSocRngHiFaultStt) {
    rtb_Saturation_d = 0.01F;
  } else {
    rtb_Saturation_d = 0.0F;
  }

  if (BSER_tiBattSocRngHiFault_C <= BSER_SocRngDiag_DW.Delay_DSTATE_m) {
    rtb_Product_a = BSER_tiBattSocRngHiFault_C;
  } else {
    rtb_Product_a = BSER_SocRngDiag_DW.Delay_DSTATE_m;
  }

  rtb_Saturation_d = (rtb_Saturation_d + rtb_Product_a) * (real32_T)
    rtb_BSER_bBattSocRngHiFaultStt;
  BSER_SocRngDiag_DW.Delay_DSTATE_m = rtb_Saturation_d;
  BSER_bBattSocRngHiFaultHeal = ((!rtb_RelationalOperator1_l) ||
    (rtb_Saturation_d > BSER_tiBattSocRngHiFault_C));
  if (BSER_SocRngDiag_DW.Delay2_DSTATE_j1) {
    rtb_Switch4_i = (uint8_T)~(uint8_T)((uint8_T)~DFC_SocRngHiMax_ASW | 8U);
  } else if (BSER_bBattSocRngHiFaultDet) {
    rtb_Switch4_i = (uint8_T)(DFC_SocRngHiMax_ASW | 152U);
  } else if (BSER_bBattSocRngHiFaultHeal) {
    rtb_Switch4_i = (uint8_T)((uint8_T)~(uint8_T)((uint8_T)~DFC_SocRngHiMax_ASW |
      16U) | 8U);
  } else {
    rtb_Switch4_i = BSER_SocRngDiag_DW.Delay3_DSTATE_l;
  }

  BSER_SocRngDiag_DW.Delay2_DSTATE_j1 = false;
  BSER_SocRngDiag_DW.Delay3_DSTATE_l = rtb_Switch4_i;
  DFC_SocRngHiMax_ASW = rtb_Switch4_i;
  BSER_bBattSocRngLoFaultStt = (BSER_rSoc > BSER_rBattSocRngLoFault_C);
  rtb_BSER_bBattSocRngHiFaultStt = !BSER_bBattSocRngLoFaultStt;
  if (rtb_BSER_bBattSocRngHiFaultStt) {
    if (!BSER_SocRngDiag_DW.CurrCond_MODE_p) {
      BSER_SocRngDiag_DW.Delay_DSTATE_g = 0.0F;
      BSER_SocRngDiag_DW.Delay_DSTATE_c = 0.0F;
      BSER_SocRngDiag_DW.CurrCond_MODE_p = true;
    }

    rtb_Saturation_d = 0.01F * BSPR_iBattCurr +
      BSER_SocRngDiag_DW.Delay_DSTATE_g;
    if (rtb_Saturation_d > 10000.0F) {
      rtb_Saturation_d = 10000.0F;
    } else {
      if (rtb_Saturation_d < 0.0F) {
        rtb_Saturation_d = 0.0F;
      }
    }

    rtb_RelationalOperator1_l = (BSPR_iBattCurr >= BSPR_iSocRngDisChCurrLo_C);
    if (rtb_RelationalOperator1_l) {
      rtb_RelationalOperator1_g = 0.01F;
    } else {
      rtb_RelationalOperator1_g = 0.0F;
    }

    if (BSPR_tiSocRngCurrCont_C <= BSER_SocRngDiag_DW.Delay_DSTATE_c) {
      rtb_Product_a = BSPR_tiSocRngCurrCont_C;
    } else {
      rtb_Product_a = BSER_SocRngDiag_DW.Delay_DSTATE_c;
    }

    rtb_Product_a = (rtb_RelationalOperator1_g + rtb_Product_a) * (real32_T)
      rtb_RelationalOperator1_l;
    BSER_SocRngDiag_DW.Delay_DSTATE_c = rtb_Product_a;
    BSPR_bSocRngDisChCurrCond = ((rtb_Saturation_d >=
      BSER_iSocRngDisCurrAccuThres_C) || (rtb_Product_a >
      BSPR_tiSocRngCurrCont_C));
    BSER_SocRngDiag_DW.Delay_DSTATE_g = rtb_Saturation_d;
  } else {
    if (BSER_SocRngDiag_DW.CurrCond_MODE_p) {
      BSER_SocRngDiag_DW.CurrCond_MODE_p = false;
    }
  }

  rtb_LogicalOperator1 = ((BSER_cntSocRngDiag_CW & 8) == 8);
  if (rtb_LogicalOperator1) {
    BSER_bBattSocRngLoFaultDet = (rtb_BSER_bBattSocRngHiFaultStt &&
      BSPR_bSocRngDisChCurrCond);
  } else {
    BSER_bBattSocRngLoFaultDet = false;
  }

  rtb_RelationalOperator1_l = ((!((BSER_cntSocRngDiag_CW & 4) == 4)) ||
    BSER_bBattSocRngLoFaultDet);
  BSER_bBattSocRngLoWarnStt = ((BSER_rSoc <= BSER_rBattSocRngLoWarn_C) &&
    (!BSER_bBattSocRngLoFaultDet));
  if (BSER_bBattSocRngLoWarnStt) {
    rtb_Product_a = 0.01F;
  } else {
    rtb_Product_a = 0.0F;
  }

  if (BSER_tiBattSocRngLoWarn_C <= BSER_SocRngDiag_DW.Delay_DSTATE_j) {
    rtb_Saturation_d = BSER_tiBattSocRngLoWarn_C;
  } else {
    rtb_Saturation_d = BSER_SocRngDiag_DW.Delay_DSTATE_j;
  }

  rtb_Saturation_d = (rtb_Product_a + rtb_Saturation_d) * (real32_T)
    BSER_bBattSocRngLoWarnStt;
  BSER_SocRngDiag_DW.Delay_DSTATE_j = rtb_Saturation_d;
  BSER_bBattSocRngLoWarnDet = ((!rtb_RelationalOperator1_l) && (rtb_Saturation_d
    > BSER_tiBattSocRngLoWarn_C));
  rtb_BSER_bBattSocRngHiFaultStt = !BSER_bBattSocRngLoWarnStt;
  if (rtb_BSER_bBattSocRngHiFaultStt) {
    rtb_Saturation_d = 0.01F;
  } else {
    rtb_Saturation_d = 0.0F;
  }

  if (BSER_tiBattSocRngLoWarn_C <= BSER_SocRngDiag_DW.Delay_DSTATE_d) {
    rtb_Product_a = BSER_tiBattSocRngLoWarn_C;
  } else {
    rtb_Product_a = BSER_SocRngDiag_DW.Delay_DSTATE_d;
  }

  rtb_Saturation_d = (rtb_Saturation_d + rtb_Product_a) * (real32_T)
    rtb_BSER_bBattSocRngHiFaultStt;
  BSER_SocRngDiag_DW.Delay_DSTATE_d = rtb_Saturation_d;
  BSER_bBattSocRngLoWarnHeal = (rtb_RelationalOperator1_l || (rtb_Saturation_d >
    BSER_tiBattSocRngLoWarn_C));
  if (BSER_SocRngDiag_DW.Delay2_DSTATE) {
    rtb_Switch4_i = (uint8_T)~(uint8_T)((uint8_T)~DFC_SocRngLoMin_ASW | 8U);
  } else if (BSER_bBattSocRngLoWarnDet) {
    rtb_Switch4_i = (uint8_T)(DFC_SocRngLoMin_ASW | 152U);
  } else if (BSER_bBattSocRngLoWarnHeal) {
    rtb_Switch4_i = (uint8_T)((uint8_T)~(uint8_T)((uint8_T)~DFC_SocRngLoMin_ASW |
      16U) | 8U);
  } else {
    rtb_Switch4_i = BSER_SocRngDiag_DW.Delay3_DSTATE;
  }

  BSER_SocRngDiag_DW.Delay2_DSTATE = false;
  BSER_SocRngDiag_DW.Delay3_DSTATE = rtb_Switch4_i;
  DFC_SocRngLoMin_ASW = rtb_Switch4_i;
  if (BSER_bBattSocRngLoFaultStt) {
    rtb_Product_a = 0.01F;
  } else {
    rtb_Product_a = 0.0F;
  }

  if (BSER_tiBattSocRngLoFault_C <= BSER_SocRngDiag_DW.Delay_DSTATE) {
    rtb_Saturation_d = BSER_tiBattSocRngLoFault_C;
  } else {
    rtb_Saturation_d = BSER_SocRngDiag_DW.Delay_DSTATE;
  }

  rtb_Saturation_d = (rtb_Product_a + rtb_Saturation_d) * (real32_T)
    BSER_bBattSocRngLoFaultStt;
  BSER_SocRngDiag_DW.Delay_DSTATE = rtb_Saturation_d;
  BSER_bBattSocRngLoFaultHeal = ((!rtb_LogicalOperator1) || (rtb_Saturation_d >
    BSER_tiBattSocRngLoFault_C));
  if (BSER_SocRngDiag_DW.Delay2_DSTATE_b) {
    rtb_Switch4_i = (uint8_T)~(uint8_T)((uint8_T)~DFC_SocRngLoMax_ASW | 8U);
  } else if (BSER_bBattSocRngLoFaultDet) {
    rtb_Switch4_i = (uint8_T)(DFC_SocRngLoMax_ASW | 152U);
  } else if (BSER_bBattSocRngLoFaultHeal) {
    rtb_Switch4_i = (uint8_T)((uint8_T)~(uint8_T)((uint8_T)~DFC_SocRngLoMax_ASW |
      16U) | 8U);
  } else {
    rtb_Switch4_i = BSER_SocRngDiag_DW.Delay3_DSTATE_h;
  }

  BSER_SocRngDiag_DW.Delay2_DSTATE_b = false;
  BSER_SocRngDiag_DW.Delay3_DSTATE_h = rtb_Switch4_i;
  DFC_SocRngLoMax_ASW = rtb_Switch4_i;
}

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

  /* block I/O */

  /* custom signals */
  BSER_bBattSocRngHiFaultDet = false;
  BSER_bBattSocRngHiWarnDet = false;
  BSER_bBattSocRngHiWarnHeal = false;
  BSER_bBattSocRngHiFaultHeal = false;
  BSER_bBattSocRngLoFaultDet = false;
  BSER_bBattSocRngLoWarnDet = false;
  BSER_bBattSocRngLoWarnHeal = false;
  BSER_bBattSocRngLoFaultHeal = false;
  BSER_SocRngDiag_DW.Delay2_DSTATE_j = true;
  BSER_SocRngDiag_DW.Delay2_DSTATE_j1 = true;
  BSER_SocRngDiag_DW.Delay2_DSTATE = true;
  BSER_SocRngDiag_DW.Delay2_DSTATE_b = true;
}

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