/*
 * File: COMR_BusOff.c
 *
 * Code generated for Simulink model 'COMR_BusOff'.
 *
 * Model version                  : A1.0_0
 * Simulink Coder version         : 8.11 (R2016b) 25-Aug-2016
 * C/C++ source code generated on : Mon Dec 07 10:53:38 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 "COMR_BusOff.h"
#include "COMR_BusOff_private.h"

/* Exported data definition */

/* Volatile memory section */
#define ASW1_OEM_START_SEC_DEFAULT_RAM1_CLEARED_UNSPECIFIED
#include "Asw1_OEM_MemMap.h"

/* Definition for custom storage class: Volatile */
volatile uint8_T DFC_DcCanBusOff_ASW;
volatile uint8_T DFC_InCanBusOff_ASW;
volatile uint8_T DFC_VeCanBusOff_ASW;

#define ASW1_OEM_STOP_SEC_DEFAULT_RAM1_CLEARED_UNSPECIFIED
#include "Asw1_OEM_MemMap.h"
#define ASW1_OEM_START_SEC_DEFAULT_RAM1_CLEARED_UNSPECIFIED
#include "Asw1_OEM_MemMap.h"

/* Definition for custom storage class: OutputSignal */
volatile boolean_T COMR_bDChrgCanBusoffDet;
volatile boolean_T COMR_bDChrgCanBusoffHeal;
volatile boolean_T COMR_bEvCanBusoffDet;
volatile boolean_T COMR_bEvCanBusoffHeal;
volatile boolean_T COMR_bInCanBusoffDet;
volatile boolean_T COMR_bInCanBusoffHeal;

#define ASW1_OEM_STOP_SEC_DEFAULT_RAM1_CLEARED_UNSPECIFIED
#include "Asw1_OEM_MemMap.h"

/* Block states (auto storage) */
DW_COMR_BusOff_T COMR_BusOff_DW;

/* Model step function */
void COMR_BusOff_step(void)
{
  boolean_T rtb_LogicalOperator4;
  real32_T rtb_Product_i;
  uint8_T rtb_Switch4_a;
  real32_T rtb_LogicalOperator4_0;
  rtb_LogicalOperator4 = ((!COMR_stEol) && COMR_bLvComDiagEn && (!BSW_stOTAMode));
  if (rtb_LogicalOperator4) {
    rtb_LogicalOperator4_0 = 0.05F;
  } else {
    rtb_LogicalOperator4_0 = 0.0F;
  }

  if (2.0F <= COMR_BusOff_DW.Delay_DSTATE_fk) {
    rtb_Product_i = 2.0F;
  } else {
    rtb_Product_i = COMR_BusOff_DW.Delay_DSTATE_fk;
  }

  rtb_Product_i = (rtb_LogicalOperator4_0 + rtb_Product_i) * (real32_T)
    rtb_LogicalOperator4;
  COMR_BusOff_DW.Delay_DSTATE_fk = rtb_Product_i;
  if (rtb_Product_i > 2.0F) {
    if (!COMR_BusOff_DW.InCan_MODE) {
      COMR_BusOff_DW.Delay_DSTATE_h = 0.0F;
      COMR_BusOff_DW.Delay_DSTATE_j = 0.0F;
      COMR_BusOff_DW.Delay2_DSTATE_e = true;
      COMR_BusOff_DW.Delay3_DSTATE_o = 0U;
      COMR_BusOff_DW.InCan_MODE = true;
    }

    if (InCan_BusOff) {
      rtb_Product_i = 0.05F;
    } else {
      rtb_Product_i = 0.0F;
    }

    if (COMR_tiInCanBusOffDebounce_C <= COMR_BusOff_DW.Delay_DSTATE_h) {
      rtb_LogicalOperator4_0 = COMR_tiInCanBusOffDebounce_C;
    } else {
      rtb_LogicalOperator4_0 = COMR_BusOff_DW.Delay_DSTATE_h;
    }

    rtb_Product_i = (rtb_Product_i + rtb_LogicalOperator4_0) * (real32_T)
      InCan_BusOff;
    COMR_bInCanBusoffDet = (rtb_Product_i > COMR_tiInCanBusOffDebounce_C);
    COMR_BusOff_DW.Delay_DSTATE_h = rtb_Product_i;
    rtb_LogicalOperator4 = !InCan_BusOff;
    if (rtb_LogicalOperator4) {
      rtb_LogicalOperator4_0 = 0.05F;
    } else {
      rtb_LogicalOperator4_0 = 0.0F;
    }

    if (COMR_tiInCanBusOffDebounce_C <= COMR_BusOff_DW.Delay_DSTATE_j) {
      rtb_Product_i = COMR_tiInCanBusOffDebounce_C;
    } else {
      rtb_Product_i = COMR_BusOff_DW.Delay_DSTATE_j;
    }

    rtb_Product_i = (rtb_LogicalOperator4_0 + rtb_Product_i) * (real32_T)
      rtb_LogicalOperator4;
    COMR_bInCanBusoffHeal = (rtb_Product_i > COMR_tiInCanBusOffDebounce_C);
    COMR_BusOff_DW.Delay_DSTATE_j = rtb_Product_i;
    if (COMR_BusOff_DW.Delay2_DSTATE_e) {
      rtb_Switch4_a = (uint8_T)~(uint8_T)((uint8_T)~DFC_InCanBusOff_ASW | 8U);
    } else if (COMR_bInCanBusoffDet) {
      rtb_Switch4_a = (uint8_T)(DFC_InCanBusOff_ASW | 152U);
    } else if (COMR_bInCanBusoffHeal) {
      rtb_Switch4_a = (uint8_T)((uint8_T)~(uint8_T)((uint8_T)~DFC_InCanBusOff_ASW |
        16U) | 8U);
    } else {
      rtb_Switch4_a = COMR_BusOff_DW.Delay3_DSTATE_o;
    }

    COMR_BusOff_DW.Delay2_DSTATE_e = false;
    COMR_BusOff_DW.Delay3_DSTATE_o = rtb_Switch4_a;
    DFC_InCanBusOff_ASW = rtb_Switch4_a;
    if (!COMR_BusOff_DW.VeCan_MODE) {
      COMR_BusOff_DW.Delay_DSTATE = 0.0F;
      COMR_BusOff_DW.Delay_DSTATE_f = 0.0F;
      COMR_BusOff_DW.Delay2_DSTATE = true;
      COMR_BusOff_DW.Delay3_DSTATE = 0U;
      COMR_BusOff_DW.VeCan_MODE = true;
    }

    if (BSWVECANBusFastRecoveryFail) {
      rtb_Product_i = 0.05F;
    } else {
      rtb_Product_i = 0.0F;
    }

    if (COMR_tiVeCanBusOffDebounce_C <= COMR_BusOff_DW.Delay_DSTATE) {
      rtb_LogicalOperator4_0 = COMR_tiVeCanBusOffDebounce_C;
    } else {
      rtb_LogicalOperator4_0 = COMR_BusOff_DW.Delay_DSTATE;
    }

    rtb_Product_i = (rtb_Product_i + rtb_LogicalOperator4_0) * (real32_T)
      BSWVECANBusFastRecoveryFail;
    COMR_bEvCanBusoffDet = (rtb_Product_i > COMR_tiVeCanBusOffDebounce_C);
    COMR_BusOff_DW.Delay_DSTATE = rtb_Product_i;
    rtb_LogicalOperator4 = !BSWVECANBusFastRecoveryFail;
    if (rtb_LogicalOperator4) {
      rtb_LogicalOperator4_0 = 0.05F;
    } else {
      rtb_LogicalOperator4_0 = 0.0F;
    }

    if (COMR_tiVeCanBusOffDebounce_C <= COMR_BusOff_DW.Delay_DSTATE_f) {
      rtb_Product_i = COMR_tiVeCanBusOffDebounce_C;
    } else {
      rtb_Product_i = COMR_BusOff_DW.Delay_DSTATE_f;
    }

    rtb_Product_i = (rtb_LogicalOperator4_0 + rtb_Product_i) * (real32_T)
      rtb_LogicalOperator4;
    COMR_bEvCanBusoffHeal = (rtb_Product_i > COMR_tiVeCanBusOffDebounce_C);
    COMR_BusOff_DW.Delay_DSTATE_f = rtb_Product_i;
    if (COMR_BusOff_DW.Delay2_DSTATE) {
      rtb_Switch4_a = (uint8_T)~(uint8_T)((uint8_T)~DFC_VeCanBusOff_ASW | 8U);
    } else if (COMR_bEvCanBusoffDet) {
      rtb_Switch4_a = (uint8_T)(DFC_VeCanBusOff_ASW | 152U);
    } else if (COMR_bEvCanBusoffHeal) {
      rtb_Switch4_a = (uint8_T)((uint8_T)~(uint8_T)((uint8_T)~DFC_VeCanBusOff_ASW |
        16U) | 8U);
    } else {
      rtb_Switch4_a = COMR_BusOff_DW.Delay3_DSTATE;
    }

    COMR_BusOff_DW.Delay2_DSTATE = false;
    COMR_BusOff_DW.Delay3_DSTATE = rtb_Switch4_a;
    DFC_VeCanBusOff_ASW = rtb_Switch4_a;
    if (!COMR_BusOff_DW.DcCan_MODE) {
      COMR_BusOff_DW.DcCan_MODE = true;
    }

    if (BSPR_bDCWkUp || ((real32_T)fabs(BSPR_uCC2Volt - 6.0F) <= 0.3F)) {
      if (DcCan_BusOff) {
        rtb_Product_i = 0.05F;
      } else {
        rtb_Product_i = 0.0F;
      }

      if (COMR_tiDcCanBusOffDebounce_C <= COMR_BusOff_DW.Delay_DSTATE_jx) {
        rtb_LogicalOperator4_0 = COMR_tiDcCanBusOffDebounce_C;
      } else {
        rtb_LogicalOperator4_0 = COMR_BusOff_DW.Delay_DSTATE_jx;
      }

      rtb_Product_i = (rtb_Product_i + rtb_LogicalOperator4_0) * (real32_T)
        DcCan_BusOff;
      COMR_bDChrgCanBusoffDet = (rtb_Product_i > COMR_tiDcCanBusOffDebounce_C);
      COMR_BusOff_DW.Delay_DSTATE_jx = rtb_Product_i;
      rtb_LogicalOperator4 = !DcCan_BusOff;
      if (rtb_LogicalOperator4) {
        rtb_LogicalOperator4_0 = 0.05F;
      } else {
        rtb_LogicalOperator4_0 = 0.0F;
      }

      if (COMR_tiDcCanBusOffDebounce_C <= COMR_BusOff_DW.Delay_DSTATE_l) {
        rtb_Product_i = COMR_tiDcCanBusOffDebounce_C;
      } else {
        rtb_Product_i = COMR_BusOff_DW.Delay_DSTATE_l;
      }

      rtb_Product_i = (rtb_LogicalOperator4_0 + rtb_Product_i) * (real32_T)
        rtb_LogicalOperator4;
      COMR_bDChrgCanBusoffHeal = (rtb_Product_i > COMR_tiDcCanBusOffDebounce_C);
      COMR_BusOff_DW.Delay_DSTATE_l = rtb_Product_i;
      if (COMR_BusOff_DW.Delay2_DSTATE_d) {
        rtb_Switch4_a = (uint8_T)~(uint8_T)((uint8_T)~DFC_DcCanBusOff_ASW | 8U);
      } else if (COMR_bDChrgCanBusoffDet) {
        rtb_Switch4_a = (uint8_T)(DFC_DcCanBusOff_ASW | 152U);
      } else if (COMR_bDChrgCanBusoffHeal) {
        rtb_Switch4_a = (uint8_T)((uint8_T)~(uint8_T)((uint8_T)~DFC_DcCanBusOff_ASW |
          16U) | 8U);
      } else {
        rtb_Switch4_a = COMR_BusOff_DW.Delay3_DSTATE_n;
      }

      COMR_BusOff_DW.Delay2_DSTATE_d = false;
      COMR_BusOff_DW.Delay3_DSTATE_n = rtb_Switch4_a;
      DFC_DcCanBusOff_ASW = rtb_Switch4_a;
    }
  } else {
    if (COMR_BusOff_DW.InCan_MODE) {
      COMR_BusOff_DW.InCan_MODE = false;
    }

    if (COMR_BusOff_DW.VeCan_MODE) {
      COMR_BusOff_DW.VeCan_MODE = false;
    }

    if (COMR_BusOff_DW.DcCan_MODE) {
      COMR_BusOff_DW.DcCan_MODE = false;
    }
  }
}

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

  /* block I/O */

  /* custom signals */
  COMR_bEvCanBusoffDet = false;
  COMR_bEvCanBusoffHeal = false;
  COMR_bInCanBusoffDet = false;
  COMR_bInCanBusoffHeal = false;
  COMR_bDChrgCanBusoffHeal = false;
  COMR_bDChrgCanBusoffDet = false;
  COMR_BusOff_DW.Delay2_DSTATE_e = true;
  COMR_BusOff_DW.Delay2_DSTATE = true;
  COMR_BusOff_DW.Delay2_DSTATE_d = true;
}

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