/*
 * File: BSER_SocJmpDiag.c
 *
 * Code generated for Simulink model 'BSER_SocJmpDiag'.
 *
 * Model version                  : 1.1451F
 * Simulink Coder version         : 8.11F (R2016b) 25-Aug-2016
 * C/C++ source code generated on : Sun Aug 11 09:35:07 2019
 *
 * 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_SocJmpDiag.h"
#include "BSER_SocJmpDiag_private.h"

/* Exported data definition */

/* Volatile memory section */
 
 

/* Definition for custom storage class: Volatile */
volatile uint8_T DFC_SocValJmp_ASW;        /* Min fault of battery soc jump */

 
 

/* Definition for custom storage class: OutputSignal */
volatile real32_T BSER_bBattSocJmpDta; /* socdisplay value after soc jump */
volatile boolean_T BSER_bBattSocJmpEn; /* Fault detect enable */
volatile boolean_T BSER_bBattSocJmpFaultDet;/* Fault detect flag for soc jump fault */
volatile boolean_T BSER_bBattSocJmpFaultHeal;/* Fault healing flag for soc jump fault */
volatile real32_T BSER_rSocDsplCont;   /* socdisplay value continuous */
volatile real32_T BSER_rSocDsplPer;    /* socdisplay value period */

/* Block states (auto storage) */
DW_BSER_SocJmpDiag_T BSER_SocJmpDiag_DW;

/* Model step function */
void BSER_SocJmpDiag_step(void)
{
  real32_T rtb_Product_mb;
  boolean_T rtb_LogicalOperator3;
  boolean_T rtb_LogicalOperator2;
  uint8_T rtb_Switch4;
  real32_T rtb_LogicalOperator3_0;
  if (BSER_tiBattSocValJmpDiag_C <= BSER_SocJmpDiag_DW.Delay_DSTATE_j) {
    rtb_Product_mb = BSER_tiBattSocValJmpDiag_C;
  } else {
    rtb_Product_mb = BSER_SocJmpDiag_DW.Delay_DSTATE_j;
  }

  BSER_bBattSocJmpEn = (0.01F + rtb_Product_mb > BSER_tiBattSocValJmpDiag_C);
  BSER_SocJmpDiag_DW.Delay_DSTATE_j = 0.01F + rtb_Product_mb;
  if (BSER_bBattSocJmpEn) {
    if (!BSER_SocJmpDiag_DW.SocSglCal_MODE) {
      BSER_SocJmpDiag_DW.Delay1_DSTATE = 1.0F;
      BSER_SocJmpDiag_DW.Delay2_DSTATE = true;
      BSER_SocJmpDiag_DW.Delay3_DSTATE = 1U;
      BSER_SocJmpDiag_DW.Delay_DSTATE = 0.0F;
      BSER_SocJmpDiag_DW.SocSglCal_MODE = true;
    }

    BSER_rSocDsplCont = BSER_rSocDspl;
    rtb_LogicalOperator3 = !BSER_SocJmpDiag_DW.Delay2_DSTATE;
    if (rtb_LogicalOperator3) {
      rtb_LogicalOperator3_0 = 0.01F;
    } else {
      rtb_LogicalOperator3_0 = 0.0F;
    }

    if (BSER_tiBattSocValJmpFault_C <= BSER_SocJmpDiag_DW.Delay_DSTATE) {
      rtb_Product_mb = BSER_tiBattSocValJmpFault_C;
    } else {
      rtb_Product_mb = BSER_SocJmpDiag_DW.Delay_DSTATE;
    }

    rtb_Product_mb = (rtb_LogicalOperator3_0 + rtb_Product_mb) * (real32_T)
      rtb_LogicalOperator3;
    rtb_LogicalOperator3 = (rtb_Product_mb > BSER_tiBattSocValJmpFault_C);
    BSER_SocJmpDiag_DW.Delay_DSTATE = rtb_Product_mb;
    if (rtb_LogicalOperator3) {
      BSER_rSocDsplPer = BSER_rSocDspl;
    } else if (BSER_SocJmpDiag_DW.Delay3_DSTATE > 0) {
      BSER_rSocDsplPer = BSER_rSocDspl;
    } else {
      BSER_rSocDsplPer = BSER_SocJmpDiag_DW.Delay1_DSTATE;
    }

    BSER_SocJmpDiag_DW.Delay1_DSTATE = BSER_rSocDsplPer;
    BSER_SocJmpDiag_DW.Delay2_DSTATE = rtb_LogicalOperator3;
    BSER_SocJmpDiag_DW.Delay3_DSTATE = 0U;
    if (!BSER_SocJmpDiag_DW.BSER_SocValRst_MODE) {
      BSER_SocJmpDiag_DW.Delay_DSTATE_p = 0.0F;
      BSER_SocJmpDiag_DW.Delay_DSTATE_h = 0.0F;
      BSER_SocJmpDiag_DW.BSER_SocValRst_MODE = true;
    }

    BSER_bBattSocJmpDta = (real32_T)fabs(BSER_rSocDsplCont - BSER_rSocDsplPer);
    rtb_LogicalOperator3 = (BSER_bBattSocJmpDta >= BSER_rBattSocValJmpFault_C);
    rtb_LogicalOperator2 = !rtb_LogicalOperator3;
    if (rtb_LogicalOperator3) {
      rtb_LogicalOperator3_0 = 0.01F;
    } else {
      rtb_LogicalOperator3_0 = 0.0F;
    }

    if (BSER_tiSocValJmpErr_C <= BSER_SocJmpDiag_DW.Delay_DSTATE_p) {
      rtb_Product_mb = BSER_tiSocValJmpErr_C;
    } else {
      rtb_Product_mb = BSER_SocJmpDiag_DW.Delay_DSTATE_p;
    }

    rtb_Product_mb = (rtb_LogicalOperator3_0 + rtb_Product_mb) * (real32_T)
      rtb_LogicalOperator3;
    BSER_bBattSocJmpFaultDet = (rtb_Product_mb > BSER_tiSocValJmpErr_C);
    BSER_SocJmpDiag_DW.Delay_DSTATE_p = rtb_Product_mb;
    if (rtb_LogicalOperator2) {
      rtb_LogicalOperator3_0 = 0.01F;
    } else {
      rtb_LogicalOperator3_0 = 0.0F;
    }

    if (BSER_tiSocValJmpOk_C <= BSER_SocJmpDiag_DW.Delay_DSTATE_h) {
      rtb_Product_mb = BSER_tiSocValJmpOk_C;
    } else {
      rtb_Product_mb = BSER_SocJmpDiag_DW.Delay_DSTATE_h;
    }

    rtb_Product_mb = (rtb_LogicalOperator3_0 + rtb_Product_mb) * (real32_T)
      rtb_LogicalOperator2;
    BSER_bBattSocJmpFaultHeal = (rtb_Product_mb > BSER_tiSocValJmpOk_C);
    BSER_SocJmpDiag_DW.Delay_DSTATE_h = rtb_Product_mb;
  } else {
    if (BSER_SocJmpDiag_DW.SocSglCal_MODE) {
      BSER_SocJmpDiag_DW.SocSglCal_MODE = false;
    }

    if (BSER_SocJmpDiag_DW.BSER_SocValRst_MODE) {
      BSER_SocJmpDiag_DW.BSER_SocValRst_MODE = false;
    }
  }

  if (BSER_SocJmpDiag_DW.Delay2_DSTATE_m) {
    rtb_Switch4 = (uint8_T)~(uint8_T)((uint8_T)~DFC_SocValJmp_ASW | 8U);
  } else if ((BSER_bSocJmpDiagSlct_CW > 0.0F) && BSER_bBattSocJmpFaultDet) {
    rtb_Switch4 = (uint8_T)(DFC_SocValJmp_ASW | 152U);
  } else if ((!(BSER_bSocJmpDiagSlct_CW > 0.0F)) || BSER_bBattSocJmpFaultHeal) {
    rtb_Switch4 = (uint8_T)((uint8_T)~(uint8_T)((uint8_T)~DFC_SocValJmp_ASW | 16U) |
      8U);
  } else {
    rtb_Switch4 = BSER_SocJmpDiag_DW.Delay3_DSTATE_m;
  }

  BSER_SocJmpDiag_DW.Delay2_DSTATE_m = false;
  BSER_SocJmpDiag_DW.Delay3_DSTATE_m = rtb_Switch4;
  DFC_SocValJmp_ASW = rtb_Switch4;
}

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

  /* block I/O */

  /* custom signals */
  BSER_bBattSocJmpDta = 0.0F;
  BSER_SocJmpDiag_DW.Delay1_DSTATE = 1.0F;
  BSER_SocJmpDiag_DW.Delay2_DSTATE = true;
  BSER_SocJmpDiag_DW.Delay3_DSTATE = 1U;
  BSER_SocJmpDiag_DW.Delay2_DSTATE_m = true;
}

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