/*
 * File: BSPR_BattCurrDiag.c
 *
 * Code generated for Simulink model 'BSPR_BattCurrDiag'.
 *
 * Model version                  : A1.3_0
 * Simulink Coder version         : 8.11 (R2016b) 25-Aug-2016
 * C/C++ source code generated on : Sun Aug 16 22:05:12 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_BattCurrDiag.h"
#include "BSPR_BattCurrDiag_private.h"

/* Exported block signals */
real32_T BSPR_iCurrMxStuk;             /* '<S10>/MinMax' */
real32_T BSPR_iCurrMnStuk;             /* '<S10>/MinMax1' */
uint8_T BSPR_cntCurrPedStukLo;         /* '<S43>/Add' */
uint8_T BSPR_cntCurrPedStukUp;         /* '<S42>/Add' */
uint8_T BSPR_cntCurrVehStukLo;         /* '<S41>/Add' */
uint8_T BSPR_cntCurrVehStukUp;         /* '<S40>/Add' */
boolean_T BSPR_bCurrDiagStukEn;        /* '<S10>/Logical Operator10' */
boolean_T BSPR_bCurrDiagChrgPlausEn;   /* '<S5>/Logical Operator' */

/* Exported data definition */

/* Volatile memory section */
/* Definition for custom storage class: Volatile */
volatile uint8_T DFC_CurrChrgPlaus_ASW;
volatile uint8_T DFC_CurrOffsPlaus_ASW;
volatile uint8_T DFC_CurrStukPlaus_ASW;

/* Definition for custom storage class: OutputSignal */
volatile boolean_T BSPR_bCurrChrgPlausDet;/* Condition for battery current charge fault */
volatile boolean_T BSPR_bCurrChrgPlausHeal;/* Condition for battery current charge healing */
volatile boolean_T BSPR_bCurrOffsDet;  /* Condition for battery current offset fault */
volatile boolean_T BSPR_bCurrOffsHeal; /* Condition for battery current offset healing */
volatile boolean_T BSPR_bCurrStukDet;  /* Condition for battery current stuck fault */
volatile boolean_T BSPR_bCurrStukHeal; /* Condition for battery current stuck healing */

/* Block signals (auto storage) */
B_BSPR_BattCurrDiag_T BSPR_BattCurrDiag_B;

/* Block states (auto storage) */
DW_BSPR_BattCurrDiag_T BSPR_BattCurrDiag_DW;

/* Previous zero-crossings (trigger) states */
PrevZCX_BSPR_BattCurrDiag_T BSPR_BattCurrDiag_PrevZCX;

/* Output and update for atomic system: '<S1>/StuckEna' */
void BSPR_BattCurrDiag_StuckEna(void)
{
  boolean_T rtb_LogicalOperator1;
  real32_T rtb_Product_g;
  real32_T rtb_LogicalOperator1_0;
  rtb_LogicalOperator1 = ((COMR_VehSpd <= BSPR_VehCurrHiUThres_C) &&
    (COMR_VehSpd >= BSPR_VehCurrHiDThres_C));
  if (rtb_LogicalOperator1) {
    rtb_LogicalOperator1_0 = 0.01F;
  } else {
    rtb_LogicalOperator1_0 = 0.0F;
  }

  if (BSPR_tiVehCurrDe_C <= BSPR_BattCurrDiag_DW.Delay_DSTATE_po) {
    rtb_Product_g = BSPR_tiVehCurrDe_C;
  } else {
    rtb_Product_g = BSPR_BattCurrDiag_DW.Delay_DSTATE_po;
  }

  rtb_Product_g = (real32_T)((rtb_LogicalOperator1_0 + rtb_Product_g) * (real32_T)
    rtb_LogicalOperator1);
  rtb_LogicalOperator1 = (rtb_Product_g > BSPR_tiVehCurrDe_C);
  BSPR_BattCurrDiag_DW.Delay_DSTATE_po = rtb_Product_g;
  if (rtb_LogicalOperator1 &&
      (BSPR_BattCurrDiag_PrevZCX.FunctionCallSubsystem_Trig_ZCE != POS_ZCSIG)) {
    BSPR_cntCurrVehStukUp++;
  }

  BSPR_BattCurrDiag_PrevZCX.FunctionCallSubsystem_Trig_ZCE =
    rtb_LogicalOperator1;
  rtb_LogicalOperator1 = ((COMR_VehSpd <= BSPR_VehCurrLoUThres_C) &&
    (COMR_VehSpd >= BSPR_VehCurrLoDThres_C));
  if (rtb_LogicalOperator1) {
    rtb_LogicalOperator1_0 = 0.01F;
  } else {
    rtb_LogicalOperator1_0 = 0.0F;
  }

  if (BSPR_tiVehCurrDe_C <= BSPR_BattCurrDiag_DW.Delay_DSTATE_p) {
    rtb_Product_g = BSPR_tiVehCurrDe_C;
  } else {
    rtb_Product_g = BSPR_BattCurrDiag_DW.Delay_DSTATE_p;
  }

  rtb_Product_g = (real32_T)((rtb_LogicalOperator1_0 + rtb_Product_g) * (real32_T)
    rtb_LogicalOperator1);
  rtb_LogicalOperator1 = (rtb_Product_g > BSPR_tiVehCurrDe_C);
  BSPR_BattCurrDiag_DW.Delay_DSTATE_p = rtb_Product_g;
  if (rtb_LogicalOperator1 &&
      (BSPR_BattCurrDiag_PrevZCX.FunctionCallSubsystem1_Trig_ZCE != POS_ZCSIG))
  {
    BSPR_cntCurrVehStukLo++;
  }

  BSPR_BattCurrDiag_PrevZCX.FunctionCallSubsystem1_Trig_ZCE =
    rtb_LogicalOperator1;
  rtb_LogicalOperator1 = ((COMR_phiAPP <= BSPR_PhiCurrHiUThres_C) &&
    (COMR_phiAPP >= BSPR_PhiCurrHiDThres_C));
  if (rtb_LogicalOperator1) {
    rtb_LogicalOperator1_0 = 0.01F;
  } else {
    rtb_LogicalOperator1_0 = 0.0F;
  }

  if (BSPR_tiPhiCurrDe_C <= BSPR_BattCurrDiag_DW.Delay_DSTATE_k) {
    rtb_Product_g = BSPR_tiPhiCurrDe_C;
  } else {
    rtb_Product_g = BSPR_BattCurrDiag_DW.Delay_DSTATE_k;
  }

  rtb_Product_g = (real32_T)((rtb_LogicalOperator1_0 + rtb_Product_g) * (real32_T)
    rtb_LogicalOperator1);
  rtb_LogicalOperator1 = (rtb_Product_g > BSPR_tiPhiCurrDe_C);
  BSPR_BattCurrDiag_DW.Delay_DSTATE_k = rtb_Product_g;
  if (rtb_LogicalOperator1 &&
      (BSPR_BattCurrDiag_PrevZCX.FunctionCallSubsystem2_Trig_ZCE != POS_ZCSIG))
  {
    BSPR_cntCurrPedStukUp++;
  }

  BSPR_BattCurrDiag_PrevZCX.FunctionCallSubsystem2_Trig_ZCE =
    rtb_LogicalOperator1;
  rtb_LogicalOperator1 = ((COMR_phiAPP <= BSPR_PhiCurrLoUThres_C) &&
    (COMR_phiAPP <= BSPR_PhiCurrLoDThres_C));
  if (rtb_LogicalOperator1) {
    rtb_LogicalOperator1_0 = 0.01F;
  } else {
    rtb_LogicalOperator1_0 = 0.0F;
  }

  if (BSPR_tiPhiCurrDe_C <= BSPR_BattCurrDiag_DW.Delay_DSTATE_j) {
    rtb_Product_g = BSPR_tiPhiCurrDe_C;
  } else {
    rtb_Product_g = BSPR_BattCurrDiag_DW.Delay_DSTATE_j;
  }

  rtb_Product_g = (real32_T)((rtb_LogicalOperator1_0 + rtb_Product_g) * (real32_T)
    rtb_LogicalOperator1);
  rtb_LogicalOperator1 = (rtb_Product_g > BSPR_tiPhiCurrDe_C);
  BSPR_BattCurrDiag_DW.Delay_DSTATE_j = rtb_Product_g;
  if (rtb_LogicalOperator1 &&
      (BSPR_BattCurrDiag_PrevZCX.FunctionCallSubsystem3_Trig_ZCE != POS_ZCSIG))
  {
    BSPR_cntCurrPedStukLo++;
  }

  BSPR_BattCurrDiag_PrevZCX.FunctionCallSubsystem3_Trig_ZCE =
    rtb_LogicalOperator1;
  BSPR_bCurrDiagStukEn = ((BSPR_cntCurrVehStukUp >= BSPR_cntVehCurrStuk_C) &&
    COMR_VehSpdVld && (BSPR_cntCurrVehStukLo >= BSPR_cntVehCurrStuk_C) &&
    (DSMR_stFaultRank < 3) && ((BSPR_cntCurrPedStukUp >= BSPR_cntPhiCurrStuk_C) &&
    (!COMR_flgAPPInvld) && (BSPR_cntCurrPedStukLo >= BSPR_cntPhiCurrStuk_C)));
  if (COMR_iBattCurr >= BSPR_iCurrMxStuk) {
    BSPR_iCurrMxStuk = COMR_iBattCurr;
  }

  if (COMR_iBattCurr <= BSPR_iCurrMnStuk) {
    BSPR_iCurrMnStuk = COMR_iBattCurr;
  }
}

/* Model step function */
void BSPR_BattCurrDiag_step(void)
{
  boolean_T rtb_Compare_c;
  real32_T rtb_Switch1_k;
  real32_T rtb_Product_c;
  uint8_T rtb_Switch4;
  real32_T rtb_Compare_g;
  if (BSPR_bCurrAdapCoordEn) {
    BSPR_BattCurrDiag_B.LogicalOperator1 = (((real32_T)fabs
      (BSPR_iCurrStbAvrgCalc) >= BSPR_iCurrDiagAdapThres_C) && ((real32_T)fabs
      (BSPR_iCurrStbAvrgCalc - BSPR_iCurrOfsAdap_nvm) >=
      BSPR_iCurrOffsDiffThres_C));
    BSPR_BattCurrDiag_B.LogicalOperator_o =
      !BSPR_BattCurrDiag_B.LogicalOperator1;
  }

  rtb_Compare_c = ((BSPR_cntBattCurrDiag_CW & 1) == 1);
  BSPR_bCurrOffsDet = (rtb_Compare_c && BSPR_BattCurrDiag_B.LogicalOperator1);
  BSPR_bCurrOffsHeal = ((!rtb_Compare_c) ||
                        BSPR_BattCurrDiag_B.LogicalOperator_o);
  if (BSPR_BattCurrDiag_DW.Delay_DSTATE > 0.0F) {
    rtb_Switch1_k = COMR_iCurrObcChrg;
  } else {
    rtb_Switch1_k = (COMR_iCurrObcChrg - BSPR_BattCurrDiag_DW.UnitDelay_DSTATE) *
      (real32_T)BSPR_cofCurrChrgFlt_C + BSPR_BattCurrDiag_DW.UnitDelay_DSTATE;
  }

  BSPR_BattCurrDiag_DW.UnitDelay_DSTATE = rtb_Switch1_k;
  BSPR_BattCurrDiag_DW.Delay_DSTATE = 0.0F;
  rtb_Compare_c = (BMCR_stBmsModeCtrl == 6);
  if (rtb_Compare_c) {
    rtb_Compare_g = 0.01F;
  } else {
    rtb_Compare_g = 0.0F;
  }

  if (BSPR_tiChrgCurrDe_C <= BSPR_BattCurrDiag_DW.Delay_DSTATE_l) {
    rtb_Product_c = BSPR_tiChrgCurrDe_C;
  } else {
    rtb_Product_c = BSPR_BattCurrDiag_DW.Delay_DSTATE_l;
  }

  rtb_Product_c = (real32_T)((rtb_Compare_g + rtb_Product_c) * (real32_T)
    rtb_Compare_c);
  BSPR_BattCurrDiag_DW.Delay_DSTATE_l = rtb_Product_c;
  BSPR_bCurrDiagChrgPlausEn = ((rtb_Product_c > BSPR_tiChrgCurrDe_C) &&
    ((real32_T)fabs(rtb_Switch1_k - COMR_iCurrObcChrg) <=
     BSPR_iCurrChrgFltThres_C));
  if (BSPR_bCurrDiagChrgPlausEn) {
    rtb_Compare_c = ((real32_T)fabs(COMR_iBattCurr) - (real32_T)fabs
                     (COMR_iCurrObcChrg) >= BSPR_iCurrOBCDiffThres_C);
    if (rtb_Compare_c) {
      rtb_Compare_g = 0.01F;
    } else {
      rtb_Compare_g = 0.0F;
    }

    if (BSPR_tiChrgCurrDiagDe_C <= BSPR_BattCurrDiag_DW.Delay_DSTATE_i) {
      rtb_Product_c = BSPR_tiChrgCurrDiagDe_C;
    } else {
      rtb_Product_c = BSPR_BattCurrDiag_DW.Delay_DSTATE_i;
    }

    rtb_Switch1_k = (real32_T)((rtb_Compare_g + rtb_Product_c) * (real32_T)
      rtb_Compare_c);
    BSPR_BattCurrDiag_B.RelationalOperator_p = (rtb_Switch1_k >
      BSPR_tiChrgCurrDiagDe_C);
    BSPR_BattCurrDiag_DW.Delay_DSTATE_i = rtb_Switch1_k;
    BSPR_BattCurrDiag_B.LogicalOperator1_l =
      !BSPR_BattCurrDiag_B.RelationalOperator_p;
  }

  rtb_Compare_c = ((BSPR_cntCurrChrgDiag_CW & 1) == 1);
  BSPR_bCurrChrgPlausDet = (rtb_Compare_c &&
    BSPR_BattCurrDiag_B.RelationalOperator_p);
  BSPR_bCurrChrgPlausHeal = ((!rtb_Compare_c) ||
    BSPR_BattCurrDiag_B.LogicalOperator1_l);
  BSPR_BattCurrDiag_StuckEna();
  if (BSPR_bCurrDiagStukEn) {
    BSPR_BattCurrDiag_B.RelationalOperator = (BSPR_iCurrMxStuk -
      BSPR_iCurrMnStuk <= BSPR_iCurrStukDiffThres_C);
    BSPR_BattCurrDiag_B.LogicalOperator =
      !BSPR_BattCurrDiag_B.RelationalOperator;
  }

  rtb_Compare_c = ((BSPR_cntCurrStukDiag_CW & 1) == 1);
  BSPR_bCurrStukDet = (rtb_Compare_c && BSPR_BattCurrDiag_B.RelationalOperator);
  BSPR_bCurrStukHeal = ((!rtb_Compare_c) || BSPR_BattCurrDiag_B.LogicalOperator);
  if (BSPR_BattCurrDiag_DW.Delay2_DSTATE) {
    rtb_Switch4 = (uint8_T)~(uint8_T)((uint8_T)~DFC_CurrOffsPlaus_ASW | 8U);
  } else if (BSPR_bCurrOffsDet) {
    rtb_Switch4 = (uint8_T)(DFC_CurrOffsPlaus_ASW | 152U);
  } else if (BSPR_bCurrOffsHeal) {
    rtb_Switch4 = (uint8_T)((uint8_T)~(uint8_T)((uint8_T)~DFC_CurrOffsPlaus_ASW |
      16U) | 8U);
  } else {
    rtb_Switch4 = BSPR_BattCurrDiag_DW.Delay3_DSTATE;
  }

  BSPR_BattCurrDiag_DW.Delay2_DSTATE = false;
  BSPR_BattCurrDiag_DW.Delay3_DSTATE = rtb_Switch4;
  DFC_CurrOffsPlaus_ASW = rtb_Switch4;
  if (BSPR_BattCurrDiag_DW.Delay2_DSTATE_l) {
    rtb_Switch4 = (uint8_T)~(uint8_T)((uint8_T)~DFC_CurrChrgPlaus_ASW | 8U);
  } else if (BSPR_bCurrChrgPlausDet) {
    rtb_Switch4 = (uint8_T)(DFC_CurrChrgPlaus_ASW | 152U);
  } else if (BSPR_bCurrChrgPlausHeal) {
    rtb_Switch4 = (uint8_T)((uint8_T)~(uint8_T)((uint8_T)~DFC_CurrChrgPlaus_ASW |
      16U) | 8U);
  } else {
    rtb_Switch4 = BSPR_BattCurrDiag_DW.Delay3_DSTATE_g;
  }

  BSPR_BattCurrDiag_DW.Delay2_DSTATE_l = false;
  BSPR_BattCurrDiag_DW.Delay3_DSTATE_g = rtb_Switch4;
  DFC_CurrChrgPlaus_ASW = rtb_Switch4;
  if (BSPR_BattCurrDiag_DW.Delay2_DSTATE_f) {
    rtb_Switch4 = (uint8_T)~(uint8_T)((uint8_T)~DFC_CurrStukPlaus_ASW | 8U);
  } else if (BSPR_bCurrStukDet) {
    rtb_Switch4 = (uint8_T)(DFC_CurrStukPlaus_ASW | 152U);
  } else if (BSPR_bCurrStukHeal) {
    rtb_Switch4 = (uint8_T)((uint8_T)~(uint8_T)((uint8_T)~DFC_CurrStukPlaus_ASW |
      16U) | 8U);
  } else {
    rtb_Switch4 = BSPR_BattCurrDiag_DW.Delay3_DSTATE_i;
  }

  BSPR_BattCurrDiag_DW.Delay2_DSTATE_f = false;
  BSPR_BattCurrDiag_DW.Delay3_DSTATE_i = rtb_Switch4;
  DFC_CurrStukPlaus_ASW = rtb_Switch4;
}

/* Model initialize function */
void BSPR_BattCurrDiag_initialize(void)
{
  BSPR_BattCurrDiag_PrevZCX.FunctionCallSubsystem_Trig_ZCE = POS_ZCSIG;
  BSPR_BattCurrDiag_PrevZCX.FunctionCallSubsystem1_Trig_ZCE = POS_ZCSIG;
  BSPR_BattCurrDiag_PrevZCX.FunctionCallSubsystem2_Trig_ZCE = POS_ZCSIG;
  BSPR_BattCurrDiag_PrevZCX.FunctionCallSubsystem3_Trig_ZCE = POS_ZCSIG;
  BSPR_BattCurrDiag_DW.Delay_DSTATE = 1.0F;
  BSPR_BattCurrDiag_DW.Delay2_DSTATE = true;
  BSPR_BattCurrDiag_DW.Delay2_DSTATE_l = true;
  BSPR_BattCurrDiag_DW.Delay2_DSTATE_f = true;
}

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