/*
 * File: BSPR_RTE.c
 *
 * Code generated for Simulink model 'BSPR_RTE'.
 *
 * Model version                  : A1.0_0
 * Simulink Coder version         : 8.11 (R2016b) 25-Aug-2016
 * C/C++ source code generated on : Tue Nov 10 16:47:36 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_RTE.h"
#include "BSPR_RTE_private.h"

/* Exported data definition */
#define ASW1_OEM_START_SEC_DEFAULT_RAM1_CLEARED_UNSPECIFIED
#include "Asw1_OEM_MemMap.h"

/* Definition for custom storage class: OutputSignal */
volatile boolean_T BSPR_bDCWkUp;
volatile real32_T BSPR_dcCrashPwmDuty;
volatile real32_T BSPR_dcCrashPwmDutyOutDet;
volatile real32_T BSPR_dcHvilPwmDutyInp;
volatile real32_T BSPR_dcHvilPwmDutyOutDet;
volatile real32_T BSPR_fCrashPwmFrq;
volatile real32_T BSPR_fCrashPwmFrqOutDet;
volatile real32_T BSPR_fHvilPwmFrq;
volatile real32_T BSPR_fHvilPwmFrqOutDet;
volatile real32_T BSPR_iAdCurr;
volatile real32_T BSPR_iCabSplyCurr;
volatile real32_T BSPR_iDcChNegCtrHsdCurr;
volatile real32_T BSPR_iDcChPosCtrHsdCurr;
volatile real32_T BSPR_iMNegCtrHsdCurr;
volatile real32_T BSPR_iMPosCtrHsdCurr;
volatile uint8_T BSPR_stBmsSelfChk;
volatile uint8_T BSPR_stCrashInpt;
volatile uint8_T BSPR_stCrashOutpFdbk;
volatile uint8_T BSPR_stHVILInp;
volatile uint8_T BSPR_stHVILOutpFdbk;
volatile uint8_T BSPR_stNvmRunning;
volatile uint8_T BSPR_stNvmShutDown;
volatile real32_T BSPR_uCC2Volt;
volatile real32_T BSPR_uCCVolt;
volatile real32_T BSPR_uDcChNegCtrLsdVolt;
volatile real32_T BSPR_uDcChPosCtrLsdVolt;
volatile real32_T BSPR_uDchrgNegVolt;
volatile real32_T BSPR_uHwCurr;
volatile real32_T BSPR_uHwSplyVolt;
volatile real32_T BSPR_uIsoPosVolt;
volatile real32_T BSPR_uKl30;
volatile real32_T BSPR_uKl30c;
volatile real32_T BSPR_uMNegCtrHsdVolt;
volatile real32_T BSPR_uMPosCtrHsdVolt;
volatile real32_T BSPR_uNegVolt;
volatile real32_T BSPR_uReservedSig1;
volatile real32_T BSPR_uReservedSig2;
volatile real32_T BSPR_uReservedSig3;
volatile real32_T BSPR_uReservedSig4;
volatile real32_T BSPR_uReservedSig5;
volatile real32_T BSPR_uReservedSig6;
volatile real32_T BSPR_uReservedSig7;
volatile real32_T BSPR_uReservedSig8;
volatile real32_T COMR_iBattCurr;
volatile real32_T COMR_uBattVolt;
volatile real32_T COMR_uDChrgVolt;
volatile real32_T COMR_uPackVolt;

#define ASW1_OEM_STOP_SEC_DEFAULT_RAM1_CLEARED_UNSPECIFIED
#include "Asw1_OEM_MemMap.h"

/* Block states (auto storage) */
DW_BSPR_RTE_T BSPR_RTE_DW;

/* Model step function */
void BSPR_RTE_step(void)
{
  real32_T rtb_y;
  boolean_T rtb_RelationalOperator;
  boolean_T rtb_RelationalOperator1;
  real32_T rtb_Product;
  real32_T rtb_RelationalOperator1_0;
  BSPR_uCC2Volt = BSW_uCC2Volt;
  BSPR_uCCVolt = BSW_uCCVolt;
  BSPR_uIsoPosVolt = BSW_uIsoPosVolt;
  BSPR_uKl30c = BSW_uKl30c;
  BSPR_uKl30 = BSW_uKl30 + 0.3F;
  BSPR_uHwSplyVolt = BSW_uHwSplyVolt;
  rtb_y = BSW_uMPosCtrHsdCurr / 1000.0F;
  if (rtb_y < 0.04F) {
    BSPR_iMPosCtrHsdCurr = 1.25F * rtb_y;
  } else if (rtb_y <= 1.16F) {
    BSPR_iMPosCtrHsdCurr = (rtb_y - 0.04F) * 0.848214269F + 0.05F;
  } else if (rtb_y <= 2.44F) {
    BSPR_iMPosCtrHsdCurr = (rtb_y - 1.16F) * 0.78125F + 1.0F;
  } else if (rtb_y <= 4.94F) {
    BSPR_iMPosCtrHsdCurr = (rtb_y - 2.44F) * 0.8F + 2.0F;
  } else {
    BSPR_iMPosCtrHsdCurr = rtb_y / 2150.0F * 1740.0F;
  }

  rtb_y = BSW_uDcChPosCtrHsdCurr / 1000.0F;
  if (rtb_y < 0.04F) {
    BSPR_iDcChPosCtrHsdCurr = 1.25F * rtb_y;
  } else if (rtb_y <= 1.16F) {
    BSPR_iDcChPosCtrHsdCurr = (rtb_y - 0.04F) * 0.848214269F + 0.05F;
  } else if (rtb_y <= 2.44F) {
    BSPR_iDcChPosCtrHsdCurr = (rtb_y - 1.16F) * 0.78125F + 1.0F;
  } else if (rtb_y <= 4.94F) {
    BSPR_iDcChPosCtrHsdCurr = (rtb_y - 2.44F) * 0.8F + 2.0F;
  } else {
    BSPR_iDcChPosCtrHsdCurr = rtb_y / 2150.0F * 1740.0F;
  }

  rtb_y = BSW_uMNegCtrHsdCurr / 1000.0F;
  if (rtb_y < 0.04F) {
    BSPR_iMNegCtrHsdCurr = 1.25F * rtb_y;
  } else if (rtb_y <= 1.16F) {
    BSPR_iMNegCtrHsdCurr = (rtb_y - 0.04F) * 0.848214269F + 0.05F;
  } else if (rtb_y <= 2.44F) {
    BSPR_iMNegCtrHsdCurr = (rtb_y - 1.16F) * 0.78125F + 1.0F;
  } else if (rtb_y <= 4.94F) {
    BSPR_iMNegCtrHsdCurr = (rtb_y - 2.44F) * 0.8F + 2.0F;
  } else {
    BSPR_iMNegCtrHsdCurr = rtb_y / 2150.0F * 1740.0F;
  }

  rtb_y = BSW_uDcChNegCtrHsdCurr / 1000.0F;
  if (rtb_y < 0.04F) {
    BSPR_iDcChNegCtrHsdCurr = 1.25F * rtb_y;
  } else if (rtb_y <= 1.16F) {
    BSPR_iDcChNegCtrHsdCurr = (rtb_y - 0.04F) * 0.848214269F + 0.05F;
  } else if (rtb_y <= 2.44F) {
    BSPR_iDcChNegCtrHsdCurr = (rtb_y - 1.16F) * 0.78125F + 1.0F;
  } else if (rtb_y <= 4.94F) {
    BSPR_iDcChNegCtrHsdCurr = (rtb_y - 2.44F) * 0.8F + 2.0F;
  } else {
    BSPR_iDcChNegCtrHsdCurr = rtb_y / 2150.0F * 1740.0F;
  }

  rtb_y = BSW_uCabCurr / 1000.0F;
  if (rtb_y < 0.05F) {
    BSPR_iCabSplyCurr = 0.001F * rtb_y * 500.0F;
  } else if (rtb_y <= 0.78F) {
    BSPR_iCabSplyCurr = 0.001F * rtb_y * 450.0F;
  } else {
    BSPR_iCabSplyCurr = 0.001F * rtb_y * 440.0F;
  }

  BSPR_uHwCurr = BSW_uAdCurr;
  if ((real32_T)fabs(BSW_uReservedSig8) < 0.0001F) {
    rtb_y = 0.0F;
  } else {
    rtb_y = (BSPR_uHwCurr / (BSW_uReservedSig8 / 5.0F) - 2.5F) / 0.002F;
  }

  if (rtb_y > 1024.0F) {
    BSPR_iAdCurr = 1024.0F;
  } else if (rtb_y < -1023.0F) {
    BSPR_iAdCurr = -1023.0F;
  } else {
    BSPR_iAdCurr = rtb_y;
  }

  BSPR_uReservedSig1 = BSW_uReservedSig1;
  BSPR_uReservedSig2 = BSW_uReservedSig2;
  BSPR_uReservedSig3 = BSW_uReservedSig3;
  BSPR_uReservedSig4 = BSW_uReservedSig4;
  BSPR_uReservedSig5 = BSW_uReservedSig5;
  BSPR_uReservedSig6 = BSW_uReservedSig6;
  BSPR_uReservedSig7 = BSW_uReservedSig7;
  BSPR_uReservedSig8 = BSW_uReservedSig8;
  BSPR_uDcChPosCtrLsdVolt = BSW_uDcChPosCtrLsdVolt;
  BSPR_uDcChNegCtrLsdVolt = BSW_uDcChNegCtrLsdVolt;
  BSPR_uMPosCtrHsdVolt = BSW_uMPosCtrHsdVolt;
  BSPR_uMNegCtrHsdVolt = BSW_uMNegCtrHsdVolt;
  BSPR_uNegVolt = BSW_uNegVolt;
  BSPR_uDchrgNegVolt = BSW_uDchrgNegVolt;
  rtb_RelationalOperator = (BSW_uDCWkUp <= RTE_uDCWkUpLow_C);
  rtb_RelationalOperator1 = (BSW_uDCWkUp >= RTE_uDCWkUpUp_C);
  if (rtb_RelationalOperator) {
    rtb_y = RTE_tiBase_C;
  } else {
    rtb_y = 0.0F;
  }

  if (RTE_tiDebs_C <= BSPR_RTE_DW.Delay_DSTATE_a) {
    rtb_Product = RTE_tiDebs_C;
  } else {
    rtb_Product = BSPR_RTE_DW.Delay_DSTATE_a;
  }

  rtb_y = (rtb_y + rtb_Product) * (real32_T)rtb_RelationalOperator;
  BSPR_RTE_DW.Delay_DSTATE_a = rtb_y;
  if (rtb_RelationalOperator1) {
    rtb_RelationalOperator1_0 = RTE_tiBase_C;
  } else {
    rtb_RelationalOperator1_0 = 0.0F;
  }

  if (RTE_tiDebs_C <= BSPR_RTE_DW.Delay_DSTATE) {
    rtb_Product = RTE_tiDebs_C;
  } else {
    rtb_Product = BSPR_RTE_DW.Delay_DSTATE;
  }

  rtb_Product = (rtb_RelationalOperator1_0 + rtb_Product) * (real32_T)
    rtb_RelationalOperator1;
  BSPR_RTE_DW.Delay_DSTATE = rtb_Product;
  if (rtb_y >= RTE_tiDebs_C) {
    BSPR_bDCWkUp = false;
  } else {
    BSPR_bDCWkUp = ((rtb_Product >= RTE_tiDebs_C) || BSPR_bDCWkUp);
  }

  COMR_uBattVolt = BSW_uBattVolt;
  COMR_uPackVolt = BSW_uPackVolt;
  if (RTE_bDchrgDiagStdSel_C) {
    COMR_uDChrgVolt = BSW_uDChrgVolt;
  } else {
    COMR_uDChrgVolt = COMR_uPackVolt;
  }

  BSPR_dcHvilPwmDutyInp = BSW_dcHvilPwmDutyInp;
  BSPR_fHvilPwmFrq = BSW_fHvilPwmFrq;
  BSPR_stHVILInp = BSW_stHVILInp;
  BSPR_dcCrashPwmDuty = BSW_dcCrashPwmDuty;
  BSPR_fCrashPwmFrq = BSW_fCrashPwmFrq;
  BSPR_stCrashInpt = BSW_stCrashInpt;
  BSPR_dcHvilPwmDutyOutDet = BSW_dcHvilPwmDutyOutDet;
  BSPR_fHvilPwmFrqOutDet = BSW_fHvilPwmFrqOutDet;
  BSPR_stHVILOutpFdbk = BSW_stHVILOutpFdbk;
  BSPR_dcCrashPwmDutyOutDet = BSW_dcCrashPwmDutyOutDet;
  BSPR_fCrashPwmFrqOutDet = BSW_fCrashPwmFrqOutDet;
  BSPR_stCrashOutpFdbk = BSW_stCrashOutpFdbk;
  if (!(BCCR_flgPackCtrCtrlEna && (BSW_stSysModeMag == 2))) {
    BSPR_stBmsSelfChk = 1U;
  } else {
    BSPR_stBmsSelfChk = 3U;
  }

  COMR_iBattCurr = COMR_iBattCurr_mA / 1000.0F;
  BSPR_stNvmShutDown = BSW_stNvmShutDown;
  BSPR_stNvmRunning = BSW_stNvmRunning;
}

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

  /* block I/O */

  /* custom signals */
  BSPR_fHvilPwmFrq = 1000.0F;
  BSPR_fCrashPwmFrq = 714.0F;
  BSPR_fHvilPwmFrqOutDet = 1000.0F;
  BSPR_fCrashPwmFrqOutDet = 714.0F;
}

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