/*
 * File: BTMR_ThmMagDiag.c
 *
 * Code generated for Simulink model 'BTMR_ThmMagDiag'.
 *
 * Model version                  : 1.1446
 * Simulink Coder version         : 8.11 (R2016b) 25-Aug-2016
 * C/C++ source code generated on : Mon Mar 01 14:45:18 2021
 *
 * 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 "BTMR_ThmMagDiag.h"
#include "BTMR_ThmMagDiag_private.h"

/* 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_CoolPerfPlaus_ASW;
volatile uint8_T DFC_HtgPerfPlaus_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 real32_T BSPR_tClntDsStrtCool;
volatile real32_T BSPR_tClntDsStrtHtg;
volatile real32_T BSPR_tClntUsStrtCool;
volatile real32_T BSPR_tClntUsStrtHtg;
volatile boolean_T BTMR_bCoolPlausErrDet;
volatile boolean_T BTMR_bCoolPlausHealDet;
volatile boolean_T BTMR_bHtgPlausErrDet;
volatile boolean_T BTMR_bHtgPlausHealDet;
volatile boolean_T BTMR_bLowClntDet;
volatile boolean_T BTMR_flgThmCoolDiagEn;
volatile boolean_T BTMR_flgThmCoolTimeEn;
volatile boolean_T BTMR_flgThmHtgDiagEn;
volatile boolean_T BTMR_flgThmHtgTimeEn;

#define ASW1_OEM_STOP_SEC_DEFAULT_RAM2_CLEARED_UNSPECIFIED
#include "Asw1_OEM_MemMap.h"

/* Block signals (auto storage) */
B_BTMR_ThmMagDiag_T BTMR_ThmMagDiag_B;

/* Block states (auto storage) */
DW_BTMR_ThmMagDiag_T BTMR_ThmMagDiag_DW;

/* Previous zero-crossings (trigger) states */
PrevZCX_BTMR_ThmMagDiag_T BTMR_ThmMagDiag_PrevZCX;

/* Model step function */
void BTMR_ThmMagDiag_step(void)
{
  boolean_T rtb_LogicalOperator;
  boolean_T rtb_Switch_pl;
  boolean_T rtb_LogicalOperator_cu;
  real32_T rtb_Product_g;
  uint8_T rtb_Add_k;
  real32_T u0;
  real32_T rtb_LogicalOperator_c;
  real32_T tmp;
  rtb_LogicalOperator = (BSPR_bClntUsTVld && BSPR_bClntDsTVld);
  if (GeRTE_PrjEdition_enum == BTMR_ThmMagDiag_ConstB.DataTypeConversion1) {
    rtb_Switch_pl = ((BTMR_tHtgTempReq != 0.0F) && BTMR_flgHtgTempReq);
  } else {
    rtb_Switch_pl = ((BTMR_pHtgPwrReq > 0.0F) && BTMR_flgHtgPwrReq);
  }

  rtb_Switch_pl = (rtb_Switch_pl && ((BMCR_stBmsModeCtrl == 6) ||
    (BMCR_stBmsModeCtrl == 5) || (BMCR_stBmsModeCtrl == 11)));
  rtb_LogicalOperator_cu = ((DSMR_stFaultRank < 3) && rtb_LogicalOperator &&
    rtb_Switch_pl);
  if (rtb_LogicalOperator_cu) {
    rtb_LogicalOperator_c = 0.1F;
  } else {
    rtb_LogicalOperator_c = 0.0F;
  }

  if (BTMR_tiThmHtgDiag_C <= BTMR_ThmMagDiag_DW.Delay_DSTATE_g) {
    u0 = BTMR_tiThmHtgDiag_C;
  } else {
    u0 = BTMR_ThmMagDiag_DW.Delay_DSTATE_g;
  }

  rtb_Product_g = (real32_T)((rtb_LogicalOperator_c + u0) * (real32_T)
    rtb_LogicalOperator_cu);
  BTMR_flgThmHtgDiagEn = (rtb_Product_g > BTMR_tiThmHtgDiag_C);
  BTMR_ThmMagDiag_DW.Delay_DSTATE_g = rtb_Product_g;
  if (BTMR_flgThmHtgDiagEn) {
    if (!BTMR_ThmMagDiag_DW.Time_calc_MODE) {
      BTMR_ThmMagDiag_DW.Delay_DSTATE_i = 0U;
      BTMR_ThmMagDiag_DW.Time_calc_MODE = true;
    }

    BTMR_flgThmHtgTimeEn = !(BTMR_ThmMagDiag_DW.Delay_DSTATE_i >
      BTMR_tiThmHtgEndDiag_C);
  } else {
    if (BTMR_ThmMagDiag_DW.Time_calc_MODE) {
      BTMR_ThmMagDiag_DW.Time_calc_MODE = false;
    }
  }

  rtb_LogicalOperator_cu = (BTMR_flgThmHtgTimeEn && BTMR_flgThmHtgDiagEn);
  if (rtb_Switch_pl && (BTMR_ThmMagDiag_PrevZCX.Strt_temp_Trig_ZCE != POS_ZCSIG))
  {
    BSPR_tClntDsStrtHtg = BSPR_tClntDsT;
    BSPR_tClntUsStrtHtg = BSPR_tClntUsT;
  }

  BTMR_ThmMagDiag_PrevZCX.Strt_temp_Trig_ZCE = rtb_Switch_pl;
  if (rtb_LogicalOperator_cu) {
    if (!BTMR_ThmMagDiag_DW.ThmHtg_MODE) {
      BTMR_ThmMagDiag_DW.Delay_DSTATE = 0.0F;
      BTMR_ThmMagDiag_DW.ThmHtg_MODE = true;
    }

    u0 = BSPR_tClntDsT - BSPR_tClntDsStrtHtg;
    rtb_Product_g = BSPR_tClntUsT - BSPR_tClntUsStrtHtg;
    if (u0 >= rtb_Product_g) {
      rtb_Product_g = u0;
    }

    if (BSPR_tClntDsT <= BSPR_tClntUsT) {
      u0 = BSPR_tClntDsT;
    } else {
      u0 = BSPR_tClntUsT;
    }

    if (BMCR_stBmsModeCtrl == 5) {
      tmp = BTMR_tDChrgHtgDiag_C;
    } else {
      tmp = BTMR_tAChrgHtgDiag_C;
    }

    rtb_Switch_pl = ((rtb_Product_g <= look1_iflf_binlxpw(COMR_tEnvT, ((const
      real32_T *)&(BTMR_tEnvTCool_CUR[0])), ((const real32_T *)
      &(BTMR_tHeatDiag_CUR[0])), 7U)) && (u0 <= tmp));
    if (rtb_Switch_pl) {
      rtb_LogicalOperator_c = 0.1F;
    } else {
      rtb_LogicalOperator_c = 0.0F;
    }

    if (BTMR_tiThmHtgDebDiag_C <= BTMR_ThmMagDiag_DW.Delay_DSTATE) {
      u0 = BTMR_tiThmHtgDebDiag_C;
    } else {
      u0 = BTMR_ThmMagDiag_DW.Delay_DSTATE;
    }

    rtb_Product_g = (real32_T)((rtb_LogicalOperator_c + u0) * (real32_T)
      rtb_Switch_pl);
    BTMR_ThmMagDiag_B.RelationalOperator = (rtb_Product_g >
      BTMR_tiThmHtgDebDiag_C);
    BTMR_ThmMagDiag_DW.Delay_DSTATE = rtb_Product_g;
  } else {
    if (BTMR_ThmMagDiag_DW.ThmHtg_MODE) {
      BTMR_ThmMagDiag_DW.ThmHtg_MODE = false;
    }
  }

  if ((BTMR_cntHtgCoolDiag_CW & 1) == 1) {
    BTMR_bHtgPlausErrDet = (rtb_LogicalOperator_cu &&
      BTMR_ThmMagDiag_B.RelationalOperator);
  } else {
    BTMR_bHtgPlausErrDet = false;
  }

  BTMR_bHtgPlausHealDet = !BTMR_bHtgPlausErrDet;
  if (BTMR_ThmMagDiag_DW.Delay2_DSTATE) {
    rtb_Add_k = (uint8_T)~(uint8_T)((uint8_T)~DFC_HtgPerfPlaus_ASW | 8U);
  } else if (BTMR_bHtgPlausErrDet) {
    rtb_Add_k = (uint8_T)(DFC_HtgPerfPlaus_ASW | 152U);
  } else if (BTMR_bHtgPlausHealDet) {
    rtb_Add_k = (uint8_T)((uint8_T)~(uint8_T)((uint8_T)~DFC_HtgPerfPlaus_ASW | 16U) |
                          8U);
  } else {
    rtb_Add_k = BTMR_ThmMagDiag_DW.Delay3_DSTATE;
  }

  BTMR_ThmMagDiag_DW.Delay2_DSTATE = false;
  BTMR_ThmMagDiag_DW.Delay3_DSTATE = rtb_Add_k;
  DFC_HtgPerfPlaus_ASW = rtb_Add_k;
  if (GeRTE_PrjEdition_enum == BTMR_ThmMagDiag_ConstB.DataTypeConversion1_l) {
    rtb_Switch_pl = ((BTMR_tClntTempReq != 0.0F) && BTMR_flgClntTempReq);
  } else {
    rtb_Switch_pl = ((BTMR_pClntPwrReq > 0.0F) && BTMR_flgClntPwrReq);
  }

  rtb_LogicalOperator = ((DSMR_stFaultRank < 3) && (!((DFC_BattTGradtMax_ASW & 16) ==
    16)) && rtb_LogicalOperator && rtb_Switch_pl);
  if (rtb_LogicalOperator) {
    rtb_LogicalOperator_c = 0.1F;
  } else {
    rtb_LogicalOperator_c = 0.0F;
  }

  if (BTMR_tiThmCoolDiag_C <= BTMR_ThmMagDiag_DW.Delay_DSTATE_o) {
    u0 = BTMR_tiThmCoolDiag_C;
  } else {
    u0 = BTMR_ThmMagDiag_DW.Delay_DSTATE_o;
  }

  rtb_Product_g = (real32_T)((rtb_LogicalOperator_c + u0) * (real32_T)
    rtb_LogicalOperator);
  BTMR_flgThmCoolDiagEn = (rtb_Product_g > BTMR_tiThmCoolDiag_C);
  BTMR_ThmMagDiag_DW.Delay_DSTATE_o = rtb_Product_g;
  if (BTMR_flgThmCoolDiagEn) {
    if (!BTMR_ThmMagDiag_DW.Time_calc_MODE_p) {
      BTMR_ThmMagDiag_DW.Delay1_DSTATE = 0U;
      BTMR_ThmMagDiag_DW.Time_calc_MODE_p = true;
    }

    BTMR_flgThmCoolTimeEn = !(BTMR_ThmMagDiag_DW.Delay1_DSTATE >
      BTMR_tiThmCoolEndDiag_C);
  } else {
    if (BTMR_ThmMagDiag_DW.Time_calc_MODE_p) {
      BTMR_ThmMagDiag_DW.Time_calc_MODE_p = false;
    }
  }

  rtb_LogicalOperator = (BTMR_flgThmCoolTimeEn && BTMR_flgThmCoolDiagEn);
  if (rtb_Switch_pl && (BTMR_ThmMagDiag_PrevZCX.Strt_cool_temp_Trig_ZCE !=
                        POS_ZCSIG)) {
    BSPR_tClntDsStrtCool = BSPR_tClntDsT;
    BSPR_tClntUsStrtCool = BSPR_tClntUsT;
  }

  BTMR_ThmMagDiag_PrevZCX.Strt_cool_temp_Trig_ZCE = rtb_Switch_pl;
  if (rtb_LogicalOperator) {
    if (!BTMR_ThmMagDiag_DW.ThmCool_MODE) {
      BTMR_ThmMagDiag_DW.Delay_DSTATE_l = 0.0F;
      BTMR_ThmMagDiag_DW.ThmCool_MODE = true;
    }

    u0 = COMR_tEnvT - BSPR_tClntDsT;
    rtb_Product_g = COMR_tEnvT - BSPR_tClntUsT;
    if (u0 >= rtb_Product_g) {
      rtb_Product_g = u0;
    }

    rtb_Switch_pl = (rtb_Product_g <= look1_iflf_binlxpw(COMR_tEnvT, ((const
      real32_T *)&(BTMR_tEnvTCool_CUR[0])), ((const real32_T *)
      &(BTMR_tLowClntDiag_CUR[0])), 7U));
    u0 = BSPR_tClntDsStrtCool - BSPR_tClntDsT;
    rtb_Product_g = BSPR_tClntUsStrtCool - BSPR_tClntUsT;
    if (u0 >= rtb_Product_g) {
      rtb_Product_g = u0;
    }

    if (BSPR_tClntDsT <= BSPR_tClntUsT) {
      u0 = BSPR_tClntDsT;
    } else {
      u0 = BSPR_tClntUsT;
    }

    rtb_LogicalOperator_cu = ((rtb_Product_g <= look1_iflf_binlxpw(COMR_tEnvT,
      ((const real32_T *)&(BTMR_tEnvTCool_CUR[0])), ((const real32_T *)
      &(BTMR_tCoolDiag_CUR[0])), 7U)) && (u0 >= BTMR_tThmCoolLowLimt_C) &&
      rtb_Switch_pl);
    BTMR_bLowClntDet = !rtb_Switch_pl;
    if (rtb_LogicalOperator_cu) {
      rtb_LogicalOperator_c = 0.1F;
    } else {
      rtb_LogicalOperator_c = 0.0F;
    }

    if (BTMR_tiThmCoolDebDiag_C <= BTMR_ThmMagDiag_DW.Delay_DSTATE_l) {
      u0 = BTMR_tiThmCoolDebDiag_C;
    } else {
      u0 = BTMR_ThmMagDiag_DW.Delay_DSTATE_l;
    }

    rtb_Product_g = (real32_T)((rtb_LogicalOperator_c + u0) * (real32_T)
      rtb_LogicalOperator_cu);
    BTMR_ThmMagDiag_B.RelationalOperator_n = (rtb_Product_g >
      BTMR_tiThmCoolDebDiag_C);
    BTMR_ThmMagDiag_DW.Delay_DSTATE_l = rtb_Product_g;
  } else {
    if (BTMR_ThmMagDiag_DW.ThmCool_MODE) {
      BTMR_ThmMagDiag_DW.ThmCool_MODE = false;
    }
  }

  BTMR_bCoolPlausErrDet = (rtb_LogicalOperator && (((BTMR_cntHtgCoolDiag_CW & 2)
    == 2) && BTMR_ThmMagDiag_B.RelationalOperator_n));
  BTMR_bCoolPlausHealDet = !BTMR_bCoolPlausErrDet;
  if (BTMR_ThmMagDiag_DW.Delay2_DSTATE_l) {
    rtb_Add_k = (uint8_T)~(uint8_T)((uint8_T)~DFC_CoolPerfPlaus_ASW | 8U);
  } else if (BTMR_bCoolPlausErrDet) {
    rtb_Add_k = (uint8_T)(DFC_CoolPerfPlaus_ASW | 152U);
  } else if (BTMR_bCoolPlausHealDet) {
    rtb_Add_k = (uint8_T)((uint8_T)~(uint8_T)((uint8_T)~DFC_CoolPerfPlaus_ASW | 16U)
                          | 8U);
  } else {
    rtb_Add_k = BTMR_ThmMagDiag_DW.Delay3_DSTATE_p;
  }

  BTMR_ThmMagDiag_DW.Delay2_DSTATE_l = false;
  BTMR_ThmMagDiag_DW.Delay3_DSTATE_p = rtb_Add_k;
  DFC_CoolPerfPlaus_ASW = rtb_Add_k;
}

/* Model initialize function */
void BTMR_ThmMagDiag_initialize(void)
{
  BTMR_ThmMagDiag_PrevZCX.Strt_cool_temp_Trig_ZCE = POS_ZCSIG;
  BTMR_ThmMagDiag_PrevZCX.Strt_temp_Trig_ZCE = POS_ZCSIG;
  BTMR_ThmMagDiag_DW.Delay2_DSTATE = true;
  BTMR_ThmMagDiag_DW.Delay2_DSTATE_l = true;
}

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