/*
 * File: COMR_RtcCalc.c
 *
 * Code generated for Simulink model 'COMR_RtcCalc'.
 *
 * Model version                  : A1.0_0
 * Simulink Coder version         : 8.11 (R2016b) 25-Aug-2016
 * C/C++ source code generated on : Fri Sep 25 16:59:33 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_RtcCalc.h"
#include "COMR_RtcCalc_private.h"

/* Exported data definition */

/* Definition for custom storage class: OutputSignal */
volatile boolean_T COMR_BMSCalCondEna_C;
volatile boolean_T COMR_BMSCalEna1_C;
volatile boolean_T COMR_BMSCalEna2_C;
volatile uint8_T COMR_DaySetWt;
volatile uint8_T COMR_HourSetWt;
volatile uint8_T COMR_MinSetWt;
volatile uint8_T COMR_MonthSetWt;
volatile uint16_T COMR_YearSetWt;
volatile real32_T COMR_tiDayCalTime;
volatile real32_T COMR_tiDayNvmTime;
volatile real32_T COMR_tiMinCalStopTime;
volatile real32_T COMR_tiStopTime;

/* Block signals (auto storage) */
B_COMR_RtcCalc_T COMR_RtcCalc_B;

/* Block states (auto storage) */
DW_COMR_RtcCalc_T COMR_RtcCalc_DW;

/* Previous zero-crossings (trigger) states */
PrevZCX_COMR_RtcCalc_T COMR_RtcCalc_PrevZCX;

/* Model step function */
void COMR_RtcCalc_step(void)
{
  boolean_T rtb_Switch;
  real32_T rtb_Product;
  int16_T rtb_Add2_k;
  real32_T rtb_Switch_0;
  rtb_Switch = ((!COMR_RtcCalc_DW.UnitDelay_DSTATE) && GbBSW_NvmShutdownVld_flg);
  if (rtb_Switch && (COMR_RtcCalc_PrevZCX.COMR_RtcCalc_Trig_ZCE != POS_ZCSIG)) {
    COMR_BMSCalEna2_C = ((COMR_MonthSet >= COMR_MonthSetNvmRd) || (COMR_DaySet >=
      COMR_DaySetNvmRd) || (COMR_HourSet >= COMR_HourSetNvmRd) || (COMR_MinSet >=
      COMR_MinSetNvmRd));
    COMR_BMSCalEna1_C = (COMR_YearSet >= COMR_YearSetNvmRd);
    COMR_BMSCalCondEna_C = (COMR_BMSCalEna1_C && COMR_BMSCalEna2_C);
    COMR_tiDayCalTime = (((((real32_T)COMR_YearSet * 365.0F + (real32_T)floor
      ((real32_T)COMR_YearSet / 4.0F)) - (real32_T)floor((real32_T)COMR_YearSet /
      100.0F)) + (real32_T)COMR_YearSet / 400.0F) +
                         rtCP_Month2Day_tableData[plook_u32u8_bincka
                         (COMR_MonthSet, rtCP_Month2Day_bp01Data, 11U)]) +
      (real32_T)COMR_DaySet;
    COMR_tiDayNvmTime = (((((real32_T)COMR_YearSetNvmRd * 365.0F + (real32_T)
      floor((real32_T)COMR_YearSetNvmRd / 4.0F)) - (real32_T)floor((real32_T)
      COMR_YearSetNvmRd / 100.0F)) + (real32_T)COMR_YearSetNvmRd / 400.0F) +
                         rtCP_Month2Day_tableData_k[plook_u32u8_bincka
                         (COMR_MonthSetNvmRd, rtCP_Month2Day_bp01Data_f, 11U)])
      + (real32_T)COMR_DaySetNvmRd;
    if (COMR_BMSCalCondEna_C) {
      rtb_Product = (real32_T)fmod((real32_T)floor(COMR_tiDayCalTime -
        COMR_tiDayNvmTime), 65536.0F);
      rtb_Add2_k = (int16_T)(rtb_Product < 0.0F ? (int32_T)(int16_T)-(int16_T)
        (uint16_T)-rtb_Product : (int32_T)(int16_T)(uint16_T)rtb_Product);
      if (!(rtb_Add2_k >= 0)) {
        rtb_Add2_k = 0;
      }

      rtb_Product = (real32_T)((((COMR_HourSet - COMR_HourSetNvmRd) * 60 +
        rtb_Add2_k * 1440) + COMR_MinSet) - COMR_MinSetNvmRd);
      if (rtb_Product < 0.0F) {
        COMR_RtcCalc_B.Saturation = 0.0F;
      } else {
        COMR_RtcCalc_B.Saturation = rtb_Product;
      }
    }

    COMR_tiMinCalStopTime = COMR_RtcCalc_B.Saturation;
  }

  COMR_RtcCalc_PrevZCX.COMR_RtcCalc_Trig_ZCE = rtb_Switch;
  if (COMR_bBMSScndVldFlg) {
    COMR_tiStopTime = (real32_T)floor((real32_T)COMR_BMSSleepScndTime /
      (real32_T)COMR_tiTmt2_C);
  } else {
    COMR_tiStopTime = COMR_tiMinCalStopTime;
  }

  rtb_Switch = ((DFC_Msg37FTmt_ASW & 8) == 8);
  if (rtb_Switch) {
    rtb_Switch_0 = 0.1F;
  } else {
    rtb_Switch_0 = 0.0F;
  }

  if (COMR_tiT37FVld_C <= COMR_RtcCalc_DW.Delay_DSTATE) {
    rtb_Product = COMR_tiT37FVld_C;
  } else {
    rtb_Product = COMR_RtcCalc_DW.Delay_DSTATE;
  }

  rtb_Product = (rtb_Switch_0 + rtb_Product) * (real32_T)rtb_Switch;
  COMR_RtcCalc_DW.Delay_DSTATE = rtb_Product;
  if (rtb_Product > COMR_tiT37FVld_C) {
    COMR_YearSetWt = COMR_YearSet;
    COMR_MonthSetWt = COMR_MonthSet;
    COMR_DaySetWt = COMR_DaySet;
    COMR_HourSetWt = COMR_HourSet;
    COMR_MinSetWt = COMR_MinSet;
  }

  COMR_RtcCalc_DW.UnitDelay_DSTATE = false;
}

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

  /* block I/O */

  /* custom signals */
  COMR_tiStopTime = 0.0F;
  COMR_YearSetWt = ((uint16_T)0U);
  COMR_MonthSetWt = ((uint8_T)0U);
  COMR_DaySetWt = ((uint8_T)0U);
  COMR_HourSetWt = ((uint8_T)0U);
  COMR_MinSetWt = ((uint8_T)0U);
  COMR_RtcCalc_PrevZCX.COMR_RtcCalc_Trig_ZCE = POS_ZCSIG;
  COMR_RtcCalc_DW.UnitDelay_DSTATE = true;
}

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