/*
 * File: HVSR_CrashChk.c
 *
 * Code generated for Simulink model 'HVSR_CrashChk'.
 *
 * Model version                  : 1.129
 * Simulink Coder version         : 8.13 (R2017b) 24-Jul-2017
 * C/C++ source code generated on : Mon Feb 22 11:19:42 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 "HVSR_CrashChk.h"
#include "HVSR_CrashChk_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_CrashCANNpl_ASW;
volatile uint8_T DFC_CrashChkHwDutyAbnorm_ASW;
volatile uint8_T DFC_CrashChkHwFrqAbnorm_ASW;
volatile uint8_T DFC_CrashHwNpl_ASW;
volatile uint8_T DFC_CrashNpl_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 boolean_T HVSR_bCrashChkCan;  /* Flag for crash state calculated according to CAN bus */
volatile boolean_T HVSR_bCrashChkHw;   /* Flag for crash state calculated according to hardwire */
volatile boolean_T HVSR_bCrashChkHwDutyAbnorm;
volatile boolean_T HVSR_bCrashChkHwDutyNorm;
volatile boolean_T HVSR_bCrashChkHwFrqAbnorm;
volatile boolean_T HVSR_bCrashChkHwFrqNorm;
volatile boolean_T HVSR_bCrashDet;     /* Flag for crash fault detected */
volatile boolean_T HVSR_bCrashHeal;    /* Flag for crash fault healed */
volatile boolean_T HVSR_bCrashPwmFrqAvl;/* Valid flag for frequency check of crash state received from hardwire */

#define ASW1_OEM_STOP_SEC_DEFAULT_RAM2_CLEARED_UNSPECIFIED
#include "Asw1_OEM_MemMap.h"

/* Block states (auto storage) */
DW_HVSR_CrashChk_T HVSR_CrashChk_DW;

/* Model step function */
void HVSR_CrashChk_step(void)
{
  boolean_T rtb_LogicalOperator5_h;
  boolean_T rtb_LogicalOperator9;
  boolean_T rtb_RelationalOperator;
  real32_T rtb_Product;
  real32_T rtb_Product_j;
  uint8_T rtb_Switch3_p0;
  boolean_T rtb_Switch;
  real32_T rtb_LogicalOperator9_0;
  if (BSPR_bKl15WkUp) {
    rtb_Product_j = 0.01F;
  } else {
    rtb_Product_j = 0.0F;
  }

  if (HVSR_tiCrashHwChkdly_C < HVSR_CrashChk_DW.Delay_DSTATE) {
    rtb_LogicalOperator9_0 = HVSR_tiCrashHwChkdly_C;
  } else {
    rtb_LogicalOperator9_0 = HVSR_CrashChk_DW.Delay_DSTATE;
  }

  rtb_Product = (rtb_Product_j + rtb_LogicalOperator9_0) * (real32_T)
    BSPR_bKl15WkUp;
  rtb_RelationalOperator = (rtb_Product > HVSR_tiCrashHwChkdly_C);
  HVSR_CrashChk_DW.Delay_DSTATE = rtb_Product;
  rtb_LogicalOperator5_h = ((BSPR_dcCrashPwmDuty < HVSR_dcCrashPwmDutyHi_C) &&
    (BSPR_dcCrashPwmDuty > HVSR_dcCrashPwmDutyLo_C));
  if ((HVSR_cntCrashChkDiagSwt_CW & 2) == 2) {
    HVSR_bCrashChkHw = (rtb_RelationalOperator && rtb_LogicalOperator5_h);
  } else {
    HVSR_bCrashChkHw = false;
  }

  rtb_LogicalOperator9 = ((!((DFC_CrashChkHwDutyAbnorm_ASW & 24) == 24)) &&
    (!((DFC_CrashChkHwFrqAbnorm_ASW & 24) == 24)) && HVSR_bCrashChkHw);
  if (rtb_LogicalOperator9) {
    rtb_LogicalOperator9_0 = 0.01F;
  } else {
    rtb_LogicalOperator9_0 = 0.0F;
  }

  if (HVSR_tiCrashHw_C < HVSR_CrashChk_DW.Delay_DSTATE_f) {
    rtb_Product_j = HVSR_tiCrashHw_C;
  } else {
    rtb_Product_j = HVSR_CrashChk_DW.Delay_DSTATE_f;
  }

  rtb_Product = (rtb_LogicalOperator9_0 + rtb_Product_j) * (real32_T)
    rtb_LogicalOperator9;
  rtb_LogicalOperator9 = (rtb_Product > HVSR_tiCrashHw_C);
  HVSR_CrashChk_DW.Delay_DSTATE_f = rtb_Product;
  if (HVSR_CrashChk_DW.Delay2_DSTATE_c) {
    rtb_Switch3_p0 = (uint8_T)~(uint8_T)((uint8_T)~DFC_CrashHwNpl_ASW | 8U);
  } else if (rtb_LogicalOperator9) {
    rtb_Switch3_p0 = (uint8_T)(DFC_CrashHwNpl_ASW | 152U);
  } else {
    rtb_Switch3_p0 = HVSR_CrashChk_DW.Delay3_DSTATE_m;
  }

  HVSR_CrashChk_DW.Delay2_DSTATE_c = false;
  HVSR_CrashChk_DW.Delay3_DSTATE_m = rtb_Switch3_p0;
  DFC_CrashHwNpl_ASW = rtb_Switch3_p0;
  if (BSPR_bKl15WkUp) {
    rtb_Product_j = 0.01F;
  } else {
    rtb_Product_j = 0.0F;
  }

  if (HVSR_tiCrashCANChkdly_C < HVSR_CrashChk_DW.Delay_DSTATE_p) {
    rtb_LogicalOperator9_0 = HVSR_tiCrashCANChkdly_C;
  } else {
    rtb_LogicalOperator9_0 = HVSR_CrashChk_DW.Delay_DSTATE_p;
  }

  rtb_Product = (rtb_Product_j + rtb_LogicalOperator9_0) * (real32_T)
    BSPR_bKl15WkUp;
  HVSR_CrashChk_DW.Delay_DSTATE_p = rtb_Product;
  if ((HVSR_cntCrashChkDiagSwt_CW & 1) == 1) {
    if (GeRTE_PrjEdition_enum != 0) {
      rtb_Switch = ((COMR_stCrashSgl == 1) || (COMR_stCrashSgl == 2) ||
                    (COMR_stCrashSgl == 3) || (COMR_stCrashSgl == 4) ||
                    (COMR_stCrashSgl == 5) || (COMR_stCrashSgl == 6) ||
                    (COMR_stCrashSgl == 7));
    } else {
      rtb_Switch = ((COMR_stCrashSgl == 1) || (COMR_stCrashSgl == 2) ||
                    (COMR_stCrashSgl == 3) || (COMR_stCrashSgl == 8) ||
                    (COMR_stCrashSgl == 9) || (COMR_stCrashSgl == 10) ||
                    (COMR_stCrashSgl == 11) || (COMR_stCrashSgl == 16) ||
                    (COMR_stCrashSgl == 17) || (COMR_stCrashSgl == 18) ||
                    (COMR_stCrashSgl == 19) || (COMR_stCrashSgl == 24) ||
                    (COMR_stCrashSgl == 25) || (COMR_stCrashSgl == 26) ||
                    (COMR_stCrashSgl == 27) || (COMR_stCrashSgl == 4));
    }

    HVSR_bCrashChkCan = (rtb_Switch && BSPR_flgGwAbmVld && (rtb_Product >
      HVSR_tiCrashCANChkdly_C) && (!GcISP_ChkSumMsg050_numb) &&
                         (!GcISP_RcMsg050_numb));
  } else {
    HVSR_bCrashChkCan = false;
  }

  if (HVSR_CrashChk_DW.Delay2_DSTATE_p) {
    rtb_Switch3_p0 = (uint8_T)~(uint8_T)((uint8_T)~DFC_CrashCANNpl_ASW | 8U);
  } else if (HVSR_bCrashChkCan) {
    rtb_Switch3_p0 = (uint8_T)(DFC_CrashCANNpl_ASW | 152U);
  } else {
    rtb_Switch3_p0 = HVSR_CrashChk_DW.Delay3_DSTATE_b;
  }

  HVSR_CrashChk_DW.Delay2_DSTATE_p = false;
  HVSR_CrashChk_DW.Delay3_DSTATE_b = rtb_Switch3_p0;
  DFC_CrashCANNpl_ASW = rtb_Switch3_p0;
  HVSR_bCrashDet = (rtb_LogicalOperator9 || HVSR_bCrashChkCan);
  if (HVSR_CrashChk_DW.Delay2_DSTATE) {
    rtb_Switch3_p0 = (uint8_T)~(uint8_T)((uint8_T)~DFC_CrashNpl_ASW | 8U);
  } else if (HVSR_bCrashDet) {
    rtb_Switch3_p0 = (uint8_T)(DFC_CrashNpl_ASW | 152U);
  } else {
    rtb_Switch3_p0 = HVSR_CrashChk_DW.Delay3_DSTATE;
  }

  HVSR_CrashChk_DW.Delay2_DSTATE = false;
  HVSR_CrashChk_DW.Delay3_DSTATE = rtb_Switch3_p0;
  DFC_CrashNpl_ASW = rtb_Switch3_p0;
  rtb_LogicalOperator9 = ((BSPR_dcCrashPwmDuty < HVSR_dcCrashPwmDutyNormHi_C) &&
    (BSPR_dcCrashPwmDuty > HVSR_dcCrashPwmDutyNormLo_C));
  rtb_LogicalOperator5_h = ((!rtb_LogicalOperator5_h) && (!rtb_LogicalOperator9));
  rtb_Switch = !(COMR_YearSet == 2080);
  if (((HVSR_cntCrashChkDiagSwt_CW & 4) == 4) && rtb_Switch) {
    HVSR_bCrashChkHwDutyAbnorm = (rtb_RelationalOperator &&
      rtb_LogicalOperator5_h);
  } else {
    HVSR_bCrashChkHwDutyAbnorm = false;
  }

  if (HVSR_bCrashChkHwDutyAbnorm) {
    rtb_Product_j = 0.01F;
  } else {
    rtb_Product_j = 0.0F;
  }

  if (HVSR_tiCrashHwDutyAbnorm_C < HVSR_CrashChk_DW.Delay_DSTATE_j) {
    rtb_LogicalOperator9_0 = HVSR_tiCrashHwDutyAbnorm_C;
  } else {
    rtb_LogicalOperator9_0 = HVSR_CrashChk_DW.Delay_DSTATE_j;
  }

  rtb_Product = (rtb_Product_j + rtb_LogicalOperator9_0) * (real32_T)
    HVSR_bCrashChkHwDutyAbnorm;
  HVSR_CrashChk_DW.Delay_DSTATE_j = rtb_Product;
  HVSR_bCrashChkHwDutyNorm = ((!rtb_LogicalOperator5_h) &&
    rtb_RelationalOperator);
  if (HVSR_bCrashChkHwDutyNorm) {
    rtb_Product_j = 0.01F;
  } else {
    rtb_Product_j = 0.0F;
  }

  if (HVSR_tiCrashHwDutyNorm_C < HVSR_CrashChk_DW.Delay_DSTATE_b) {
    rtb_LogicalOperator9_0 = HVSR_tiCrashHwDutyNorm_C;
  } else {
    rtb_LogicalOperator9_0 = HVSR_CrashChk_DW.Delay_DSTATE_b;
  }

  rtb_Product_j = (rtb_Product_j + rtb_LogicalOperator9_0) * (real32_T)
    HVSR_bCrashChkHwDutyNorm;
  HVSR_CrashChk_DW.Delay_DSTATE_b = rtb_Product_j;
  if (HVSR_CrashChk_DW.Delay2_DSTATE_d) {
    rtb_Switch3_p0 = (uint8_T)~(uint8_T)((uint8_T)~DFC_CrashChkHwDutyAbnorm_ASW | 8U);
  } else if (rtb_Product > HVSR_tiCrashHwDutyAbnorm_C) {
    rtb_Switch3_p0 = (uint8_T)(DFC_CrashChkHwDutyAbnorm_ASW | 152U);
  } else if (rtb_Product_j > HVSR_tiCrashHwDutyNorm_C) {
    rtb_Switch3_p0 = (uint8_T)((uint8_T)~(uint8_T)((uint8_T)
      ~DFC_CrashChkHwDutyAbnorm_ASW | 16U) | 8U);
  } else {
    rtb_Switch3_p0 = HVSR_CrashChk_DW.Delay3_DSTATE_d;
  }

  HVSR_CrashChk_DW.Delay2_DSTATE_d = false;
  HVSR_CrashChk_DW.Delay3_DSTATE_d = rtb_Switch3_p0;
  DFC_CrashChkHwDutyAbnorm_ASW = rtb_Switch3_p0;
  HVSR_bCrashPwmFrqAvl = ((BSPR_fCrashPwmFrq < HVSR_fCrashPwmFrqHi_C) &&
    (BSPR_fCrashPwmFrq > HVSR_fCrashPwmFrqLo_C));
  if (rtb_Switch && ((HVSR_cntCrashChkDiagSwt_CW & 8) == 8)) {
    HVSR_bCrashChkHwFrqAbnorm = ((!HVSR_bCrashPwmFrqAvl) &&
      rtb_RelationalOperator);
  } else {
    HVSR_bCrashChkHwFrqAbnorm = false;
  }

  if (HVSR_bCrashChkHwFrqAbnorm) {
    rtb_Product_j = 0.01F;
  } else {
    rtb_Product_j = 0.0F;
  }

  if (HVSR_tiCrashHwFrqAbnorm_C < HVSR_CrashChk_DW.Delay_DSTATE_pl) {
    rtb_LogicalOperator9_0 = HVSR_tiCrashHwFrqAbnorm_C;
  } else {
    rtb_LogicalOperator9_0 = HVSR_CrashChk_DW.Delay_DSTATE_pl;
  }

  rtb_Product = (rtb_Product_j + rtb_LogicalOperator9_0) * (real32_T)
    HVSR_bCrashChkHwFrqAbnorm;
  HVSR_CrashChk_DW.Delay_DSTATE_pl = rtb_Product;
  HVSR_bCrashChkHwFrqNorm = (rtb_RelationalOperator && HVSR_bCrashPwmFrqAvl);
  if (HVSR_bCrashChkHwFrqNorm) {
    rtb_Product_j = 0.01F;
  } else {
    rtb_Product_j = 0.0F;
  }

  if (HVSR_tiCrashHwFrqNorm_C < HVSR_CrashChk_DW.Delay_DSTATE_d) {
    rtb_LogicalOperator9_0 = HVSR_tiCrashHwFrqNorm_C;
  } else {
    rtb_LogicalOperator9_0 = HVSR_CrashChk_DW.Delay_DSTATE_d;
  }

  rtb_Product_j = (rtb_Product_j + rtb_LogicalOperator9_0) * (real32_T)
    HVSR_bCrashChkHwFrqNorm;
  HVSR_CrashChk_DW.Delay_DSTATE_d = rtb_Product_j;
  if (HVSR_CrashChk_DW.Delay2_DSTATE_i) {
    rtb_Switch3_p0 = (uint8_T)~(uint8_T)((uint8_T)~DFC_CrashChkHwFrqAbnorm_ASW | 8U);
  } else if (rtb_Product > HVSR_tiCrashHwFrqAbnorm_C) {
    rtb_Switch3_p0 = (uint8_T)(DFC_CrashChkHwFrqAbnorm_ASW | 152U);
  } else if (rtb_Product_j > HVSR_tiCrashHwFrqNorm_C) {
    rtb_Switch3_p0 = (uint8_T)((uint8_T)~(uint8_T)((uint8_T)
      ~DFC_CrashChkHwFrqAbnorm_ASW | 16U) | 8U);
  } else {
    rtb_Switch3_p0 = HVSR_CrashChk_DW.Delay3_DSTATE_a;
  }

  HVSR_CrashChk_DW.Delay2_DSTATE_i = false;
  HVSR_CrashChk_DW.Delay3_DSTATE_a = rtb_Switch3_p0;
  DFC_CrashChkHwFrqAbnorm_ASW = rtb_Switch3_p0;
  rtb_RelationalOperator = ((!HVSR_bCrashChkCan) && BSPR_flgGwAbmVld);
  if (rtb_RelationalOperator) {
    rtb_Product = 0.01F;
  } else {
    rtb_Product = 0.0F;
  }

  if (HVSR_tiCrashHw_C + 0.6F < HVSR_CrashChk_DW.Delay_DSTATE_i) {
    rtb_Product_j = HVSR_tiCrashHw_C + 0.6F;
  } else {
    rtb_Product_j = HVSR_CrashChk_DW.Delay_DSTATE_i;
  }

  rtb_Product = (rtb_Product + rtb_Product_j) * (real32_T)rtb_RelationalOperator;
  HVSR_CrashChk_DW.Delay_DSTATE_i = rtb_Product;
  if (rtb_LogicalOperator9) {
    rtb_LogicalOperator9_0 = 0.01F;
  } else {
    rtb_LogicalOperator9_0 = 0.0F;
  }

  if (HVSR_tiCrashHw_C + 0.6F < HVSR_CrashChk_DW.Delay_DSTATE_g) {
    rtb_Product_j = HVSR_tiCrashHw_C + 0.6F;
  } else {
    rtb_Product_j = HVSR_CrashChk_DW.Delay_DSTATE_g;
  }

  rtb_Product_j = (rtb_LogicalOperator9_0 + rtb_Product_j) * (real32_T)
    rtb_LogicalOperator9;
  HVSR_CrashChk_DW.Delay_DSTATE_g = rtb_Product_j;
  HVSR_bCrashHeal = ((rtb_Product > HVSR_tiCrashHw_C + 0.6F) && (rtb_Product_j >
    HVSR_tiCrashHw_C + 0.6F));
}

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

  /* block I/O */

  /* custom signals */
  HVSR_bCrashDet = false;
  HVSR_bCrashHeal = false;
  HVSR_CrashChk_DW.Delay2_DSTATE_c = true;
  HVSR_CrashChk_DW.Delay2_DSTATE_p = true;
  HVSR_CrashChk_DW.Delay2_DSTATE = true;
  HVSR_CrashChk_DW.Delay2_DSTATE_d = true;
  HVSR_CrashChk_DW.Delay2_DSTATE_i = true;
}

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