/*
 * File: BSPR_BattVoltCoord.c
 *
 * Code generated for Simulink model 'BSPR_BattVoltCoord'.
 *
 * Model version                  : 1.1113
 * Simulink Coder version         : 8.13 (R2017b) 24-Jul-2017
 * C/C++ source code generated on : Thu Nov 19 15:53:55 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_BattVoltCoord.h"
#include "BSPR_BattVoltCoord_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_BattVoltRngErr_ASW;   /* Battery voltage out of range fault */
volatile uint8_T DFC_BattVoltVld_ASW;      /* Valid fault for battery voltage upstream the main contactor */
volatile uint8_T DFC_DChrgVoltVld_ASW;     /* Valid fault for battery downstream the DC charge contactor */
volatile uint8_T DFC_HVSmplComFlt_ASW;
volatile uint8_T DFC_PackVoltVld_ASW;      /* Valid fault for battery downstream the main contactor */

#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 BSPR_bBattVoltInvld;/* Invalid flag for battery voltage */
volatile boolean_T BSPR_bBattVoltRngChkErr;/* Error detect flag for battery voltage out of range check */
volatile boolean_T BSPR_bBattVoltRngChkOk;/* Error healing flag for battery voltage out of range check */
volatile boolean_T BSPR_bBattVoltVld;  /* Valid flag for battery voltage upstream the main contactor  */
volatile boolean_T BSPR_bDChrgVoltVld; /* Valid flag for battery voltage downstream the DC charge contactor */
volatile boolean_T BSPR_bHVSmplComVld;
volatile boolean_T BSPR_bPackVoltVld;  /* Valid flag for battery voltage downstream the main contactor */
volatile real32_T BSPR_uBattVolt;      /* Battery voltage upstream the main contactor */
volatile real32_T BSPR_uDChrgVolt;     /* Battery voltage downstream the DC charge contactor */
volatile real32_T BSPR_uDChrgVoltRaw;  /* Battery voltage sampled downstream the DC charge contactor */
volatile real32_T BSPR_uPackVolt;      /* Battery voltage downstream the main contactor */
volatile real32_T BSPR_uPackVoltRaw;   /* Battery voltage sampled downstream the main contactor */

#define ASW1_OEM_STOP_SEC_DEFAULT_RAM2_CLEARED_UNSPECIFIED
#include "Asw1_OEM_MemMap.h"

/* Block states (auto storage) */
DW_BSPR_BattVoltCoord_T BSPR_BattVoltCoord_DW;

/* Model step function */
void BSPR_BattVoltCoord_step(void)
{
  boolean_T rtb_Compare;
  boolean_T rtb_LogicalOperator1_a;
  boolean_T rtb_Compare_h;
  boolean_T rtb_LogicalOperator10;
  real32_T rtb_Product_j;
  int32_T i;
  uint8_T rtb_Switch4_k3;
  real32_T rtb_LogicalOperator10_0;
  rtb_Compare = (COMR_uBattVolt == 6553.5F);
  BSPR_bBattVoltVld = !rtb_Compare;
  if (BSPR_BattVoltCoord_DW.Delay2_DSTATE_a) {
    rtb_Switch4_k3 = (uint8_T)~(uint8_T)((uint8_T)~DFC_BattVoltVld_ASW | 8U);
  } else if (rtb_Compare) {
    rtb_Switch4_k3 = (uint8_T)(DFC_BattVoltVld_ASW | 152U);
  } else if (BSPR_bBattVoltVld) {
    rtb_Switch4_k3 = (uint8_T)((uint8_T)~(uint8_T)((uint8_T)~DFC_BattVoltVld_ASW |
      16U) | 8U);
  } else {
    rtb_Switch4_k3 = BSPR_BattVoltCoord_DW.Delay3_DSTATE_n;
  }

  BSPR_BattVoltCoord_DW.Delay2_DSTATE_a = false;
  BSPR_BattVoltCoord_DW.Delay3_DSTATE_n = rtb_Switch4_k3;
  DFC_BattVoltVld_ASW = rtb_Switch4_k3;
  rtb_LogicalOperator1_a = ((COMR_uBattVolt > BSPR_uBattVoltRngHi_C) ||
    (COMR_uBattVolt < BSPR_uBattVoltRngLo_C));
  if (BSPR_bBattVoltHwSel_C) {
    rtb_Compare_h = (BSWBusHVDataMsgRx && BSWBatteryHVDataMsgRx);
  } else {
    rtb_Compare_h = true;
  }

  rtb_Compare_h = (rtb_Compare_h && (!rtb_Compare));
  rtb_LogicalOperator10 = (rtb_LogicalOperator1_a && rtb_Compare_h);
  if (rtb_LogicalOperator10) {
    rtb_LogicalOperator10_0 = 0.01F;
  } else {
    rtb_LogicalOperator10_0 = 0.0F;
  }

  if (BSPR_tiBattVoltRngChkErr_C < BSPR_BattVoltCoord_DW.Delay_DSTATE_f) {
    rtb_Product_j = BSPR_tiBattVoltRngChkErr_C;
  } else {
    rtb_Product_j = BSPR_BattVoltCoord_DW.Delay_DSTATE_f;
  }

  rtb_Product_j = (rtb_LogicalOperator10_0 + rtb_Product_j) * (real32_T)
    rtb_LogicalOperator10;
  BSPR_bBattVoltRngChkErr = (rtb_Product_j > BSPR_tiBattVoltRngChkErr_C);
  BSPR_BattVoltCoord_DW.Delay_DSTATE_f = rtb_Product_j;
  rtb_Compare_h = ((!rtb_LogicalOperator1_a) && rtb_Compare_h);
  if (rtb_Compare_h) {
    rtb_LogicalOperator10_0 = 0.01F;
  } else {
    rtb_LogicalOperator10_0 = 0.0F;
  }

  if (BSPR_tiBattVoltRngChkErr_C < BSPR_BattVoltCoord_DW.Delay_DSTATE_d) {
    rtb_Product_j = BSPR_tiBattVoltRngChkErr_C;
  } else {
    rtb_Product_j = BSPR_BattVoltCoord_DW.Delay_DSTATE_d;
  }

  rtb_Product_j = (rtb_LogicalOperator10_0 + rtb_Product_j) * (real32_T)
    rtb_Compare_h;
  BSPR_bBattVoltRngChkOk = (rtb_Product_j > BSPR_tiBattVoltRngChkErr_C);
  BSPR_BattVoltCoord_DW.Delay_DSTATE_d = rtb_Product_j;
  if (BSPR_BattVoltCoord_DW.Delay2_DSTATE_os) {
    rtb_Switch4_k3 = (uint8_T)~(uint8_T)((uint8_T)~DFC_BattVoltRngErr_ASW | 8U);
  } else if (BSPR_bBattVoltRngChkErr) {
    rtb_Switch4_k3 = (uint8_T)(DFC_BattVoltRngErr_ASW | 152U);
  } else if (BSPR_bBattVoltRngChkOk) {
    rtb_Switch4_k3 = (uint8_T)((uint8_T)~(uint8_T)((uint8_T)~DFC_BattVoltRngErr_ASW |
      16U) | 8U);
  } else {
    rtb_Switch4_k3 = BSPR_BattVoltCoord_DW.Delay3_DSTATE_m;
  }

  BSPR_BattVoltCoord_DW.Delay2_DSTATE_os = false;
  BSPR_BattVoltCoord_DW.Delay3_DSTATE_m = rtb_Switch4_k3;
  DFC_BattVoltRngErr_ASW = rtb_Switch4_k3;
  BSPR_bBattVoltInvld = (rtb_LogicalOperator1_a || rtb_Compare);
  if (BSPR_bBattVoltInvld) {
    rtb_Product_j = BSPR_uMdulVolt_A[0];
    for (i = 0; i < 15; i++) {
      rtb_Product_j += BSPR_uMdulVolt_A[i + 1];
    }

    rtb_Product_j = BSPR_uSumVoltCell - rtb_Product_j;
    rtb_Product_j = (real32_T)fabs(rtb_Product_j);
    if (rtb_Product_j > BSPR_uSumVoltDiffThres_C) {
      rtb_Product_j = BSPR_BattVoltCoord_DW.Delay_DSTATE;
    } else {
      rtb_Product_j = BSPR_uSumVoltCell;
    }
  } else {
    rtb_Product_j = COMR_uBattVolt;
  }

  if (rtb_Product_j > 500.0F) {
    BSPR_uBattVolt = 500.0F;
  } else if (rtb_Product_j < 0.0F) {
    BSPR_uBattVolt = 0.0F;
  } else {
    BSPR_uBattVolt = rtb_Product_j;
  }

  rtb_Compare_h = (COMR_uDChrgVolt == 6553.5F);
  BSPR_bDChrgVoltVld = !rtb_Compare_h;
  if (BSPR_BattVoltCoord_DW.Delay2_DSTATE_o) {
    DFC_DChrgVoltVld_ASW = (uint8_T)~(uint8_T)((uint8_T)~DFC_DChrgVoltVld_ASW | 8U);
  } else if (!BSPR_bDChrgVoltVld) {
    DFC_DChrgVoltVld_ASW = (uint8_T)(DFC_DChrgVoltVld_ASW | 152U);
  } else {
    DFC_DChrgVoltVld_ASW = (uint8_T)((uint8_T)~(uint8_T)((uint8_T)~DFC_DChrgVoltVld_ASW |
      16U) | 8U);
  }

  BSPR_BattVoltCoord_DW.Delay2_DSTATE_o = false;
  BSPR_uDChrgVoltRaw = COMR_uDChrgVolt;
  if (rtb_Compare_h) {
    if (BCCR_stBmsDChrgCtrMode == 3) {
      BSPR_uDChrgVolt = BSPR_uSumVoltCell;
    } else {
      BSPR_uDChrgVolt = 0.0F;
    }
  } else {
    if (!BSPR_bBattVoltHwSel_C) {
      rtb_Compare_h = BCCR_bDcChNegCtrHsdEna;
    } else {
      rtb_Compare_h = ((BCCR_bDcChNegCtrHsdEna && (BCCR_dcDcChNegCtrLsdPwmDuty ==
        100.0F)) || BCCR_bSw5CtrDiagSwt);
    }

    if (rtb_Compare_h) {
      rtb_LogicalOperator10_0 = COMR_uDChrgVolt - BSPR_uDchrgNegVolt *
        BSPR_cofDChrgVoltConv_C;
      if (rtb_LogicalOperator10_0 > 500.0F) {
        BSPR_uDChrgVolt = 500.0F;
      } else if (rtb_LogicalOperator10_0 < 0.0F) {
        BSPR_uDChrgVolt = 0.0F;
      } else {
        BSPR_uDChrgVolt = rtb_LogicalOperator10_0;
      }
    } else {
      BSPR_uDChrgVolt = 0.0F;
    }
  }

  rtb_Compare_h = (COMR_uPackVolt == 6553.5F);
  BSPR_bPackVoltVld = !rtb_Compare_h;
  if (BSPR_BattVoltCoord_DW.Delay2_DSTATE) {
    DFC_PackVoltVld_ASW = (uint8_T)~(uint8_T)((uint8_T)~DFC_PackVoltVld_ASW | 8U);
  } else if (!BSPR_bPackVoltVld) {
    DFC_PackVoltVld_ASW = (uint8_T)(DFC_PackVoltVld_ASW | 152U);
  } else {
    DFC_PackVoltVld_ASW = (uint8_T)((uint8_T)~(uint8_T)((uint8_T)~DFC_PackVoltVld_ASW |
      16U) | 8U);
  }

  BSPR_BattVoltCoord_DW.Delay2_DSTATE = false;
  BSPR_uPackVoltRaw = COMR_uPackVolt;
  if (rtb_Compare_h) {
    if (BCCR_stBmsCtrMode == 4) {
      BSPR_uPackVolt = BSPR_uSumVoltCell;
    } else {
      BSPR_uPackVolt = 0.0F;
    }
  } else if ((BCCR_stBmsCtrMode == 4) || (BCCR_stBmsCtrMode == 3)) {
    rtb_LogicalOperator10_0 = COMR_uPackVolt - BSPR_uNegVolt *
      BSPR_cofPackVoltConv_C;
    if (rtb_LogicalOperator10_0 > 500.0F) {
      BSPR_uPackVolt = 500.0F;
    } else if (rtb_LogicalOperator10_0 < 0.0F) {
      BSPR_uPackVolt = 0.0F;
    } else {
      BSPR_uPackVolt = rtb_LogicalOperator10_0;
    }
  } else {
    BSPR_uPackVolt = 0.0F;
  }

  rtb_Compare = (COMR_stHVSmplComFlt != 0);
  BSPR_bHVSmplComVld = !rtb_Compare;
  if (BSPR_BattVoltCoord_DW.Delay2_DSTATE_p) {
    rtb_Switch4_k3 = (uint8_T)~(uint8_T)((uint8_T)~DFC_HVSmplComFlt_ASW | 8U);
  } else if (rtb_Compare) {
    rtb_Switch4_k3 = (uint8_T)(DFC_HVSmplComFlt_ASW | 152U);
  } else if (BSPR_bHVSmplComVld) {
    rtb_Switch4_k3 = (uint8_T)((uint8_T)~(uint8_T)((uint8_T)~DFC_HVSmplComFlt_ASW |
      16U) | 8U);
  } else {
    rtb_Switch4_k3 = BSPR_BattVoltCoord_DW.Delay3_DSTATE_a;
  }

  BSPR_BattVoltCoord_DW.Delay2_DSTATE_p = false;
  BSPR_BattVoltCoord_DW.Delay3_DSTATE_a = rtb_Switch4_k3;
  DFC_HVSmplComFlt_ASW = rtb_Switch4_k3;
  BSPR_BattVoltCoord_DW.Delay_DSTATE = rtb_Product_j;
}

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

  /* block I/O */

  /* custom signals */
  BSPR_uBattVolt = 0.0F;
  BSPR_uDChrgVoltRaw = 0.0F;
  BSPR_uDChrgVolt = 0.0F;
  BSPR_uPackVoltRaw = 0.0F;
  BSPR_uPackVolt = 0.0F;
  BSPR_bBattVoltVld = false;
  BSPR_bBattVoltInvld = false;
  BSPR_bDChrgVoltVld = false;
  BSPR_bPackVoltVld = false;
  BSPR_bHVSmplComVld = false;
  BSPR_BattVoltCoord_DW.Delay2_DSTATE_a = true;
  BSPR_BattVoltCoord_DW.Delay2_DSTATE_os = true;
  BSPR_BattVoltCoord_DW.Delay2_DSTATE_o = true;
  BSPR_BattVoltCoord_DW.Delay2_DSTATE = true;
  BSPR_BattVoltCoord_DW.Delay2_DSTATE_p = true;
}

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