/*
 * File: RTC.c
 *
 * Code generated for Simulink model 'RTC'.
 *
 * Company                        : AIWAYS
 * Author                         : zhangzy
 * Model version                  : 'M100C100'
 * Simulink Coder version         : 8.13 (R2017b) 24-Jul-2017
 * C/C++ source code generated on : Fri Oct 23 15:09:38 2020
 *
 * Target selection: ert.tlc
 * Embedded hardware selection: Infineon->TriCore
 * Code generation objectives:
 *    1. MISRA C:2012 guidelines
 *    2. Safety precaution
 * Validation result: Not run
 */

#include "RTC.h"
#include "RTC_private.h"
#include "div_u32_round.h"

/* Block signals (auto storage) */
B_RTC_T RTC_B;

/* Block states (auto storage) */
DW_RTC_T RTC_DW;

/* Real-time model */
RT_MODEL_RTC_T RTC_M_;
RT_MODEL_RTC_T *const RTC_M = &RTC_M_;

/* Forward declaration for local functions */
static int32_T safe_cast_to_Enum_ISPEcuOffTiSt(int32_T input);
static int32_T safe_cast_to_Enum_CTCCtrCtlStat(int32_T input);

/*
 * System initialize for atomic system:
 *    '<S4>/Unit Delay External IC'
 *    '<S6>/Unit Delay External IC'
 */
void RTC_UnitDelayExternalIC_Init(DW_UnitDelayExternalIC_RTC_T *localDW)
{
  localDW->StateName_h = 1U;
}

void RTC_UnitDelayExternalIC(uint32_T rtu_IC, B_UnitDelayExternalIC_RTC_T
  *localB, DW_UnitDelayExternalIC_RTC_T *localDW)
{
  if (localDW->StateName_h != 0) {
    localB->Init0 = rtu_IC;
  } else {
    localB->Init0 = localDW->StateName;
  }
}

/*
 * Update for atomic system:
 *    '<S4>/Unit Delay External IC'
 *    '<S6>/Unit Delay External IC'
 */
void RTC_UnitDelayExternalIC_Update(uint32_T rtu_u, DW_UnitDelayExternalIC_RTC_T
  *localDW)
{
  localDW->StateName_h = 0U;
  localDW->StateName = rtu_u;
}

static int32_T safe_cast_to_Enum_ISPEcuOffTiSt(int32_T input)
{
  int32_T y;

  /* Initialize output value to default value for Enum_ISPEcuOffTiSt (Enum_ISPEcuOffTiSt_Valid) */
  y = 1;
  if ((input >= 1) && (input <= 3)) {
    /* Set output value to input value if it is a member of Enum_ISPEcuOffTiSt */
    y = input;
  }

  return y;
}

static int32_T safe_cast_to_Enum_CTCCtrCtlStat(int32_T input)
{
  int32_T y;

  /* Initialize output value to default value for Enum_CTCCtrCtlState (Enum_CTCCtrCtlState_Offline) */
  y = 0;
  if ((input >= 0) && (input <= 7)) {
    /* Set output value to input value if it is a member of Enum_CTCCtrCtlState */
    y = input;
  }

  return y;
}

/* Model step function */
void RTC_RealTime_10ms(void)
{
  uint32_T rtb_Switch_o;
  boolean_T rtb_Logical1;
  real32_T rtb_MinMax4;
  boolean_T rtb_RelationalOperator;
  boolean_T rtb_AND;
  uint32_T rtb_Sum;
  uint32_T q0;
  uint32_T rtb_RelationalOperator_0;
  RTC_UnitDelayExternalIC(get_EcNVM_CalenderLife_Day(),
    &RTC_B.UnitDelayExternalIC, &RTC_DW.UnitDelayExternalIC);
  if (get_GeCTC_CtrCtlState_enum() != (int32_T)Enum_CTCCtrCtlState_Online) {
    q0 = RTC_RUNTIME_MS;
  } else {
    q0 = RTC_CONST0_NUMB;
  }

  rtb_Sum = q0 + RTC_DW.StateName;
  if (rtb_Sum < q0) {
    rtb_Sum = MAX_uint32_T;
  }

  if (RTC_CONST4294967000_NUMB < rtb_Sum) {
    rtb_Sum = RTC_CONST4294967000_NUMB;
  }

  if (rtb_Sum > RTC_CONST0_NUMB) {
    LcRTC_OfflineTi_ms = rtb_Sum;
  } else {
    LcRTC_OfflineTi_ms = RTC_CONST0_NUMB;
  }

  LcRTC_OfflineTi_s = RTC_CONST1000_NUMB == 0U ? MAX_uint32_T :
    LcRTC_OfflineTi_ms / RTC_CONST1000_NUMB;
  LbRTC_LastOneDayCount1_flg = RTC_DW.StateName_f;
  rtb_Logical1 = (RTC_DW.StateName_i || LbRTC_LastOneDayCount1_flg);
  if (safe_cast_to_Enum_ISPEcuOffTiSt(get_GeISP_EcuOffTiSt_enum()) == (int32_T)
      Enum_ISPEcuOffTiSt_Valid) {
    LcRTC_EcuOffTi_s = get_GcISP_EcuOffTi_s();
  } else {
    LcRTC_EcuOffTi_s = RTC_CONST0_NUMB;
  }

  if (rtb_Logical1) {
    LcRTC_CalenderLife_s = LcRTC_OfflineTi_s;
  } else {
    rtb_Sum = LcRTC_EcuOffTi_s + LcRTC_OfflineTi_s;
    if (rtb_Sum < LcRTC_EcuOffTi_s) {
      rtb_Sum = MAX_uint32_T;
    }

    LcRTC_CalenderLife_s = rtb_Sum;
  }

  LbRTC_OneDayCount1_flg = (LcRTC_CalenderLife_s >= RTC_CONST86400_NUMB);
  if (LbRTC_OneDayCount1_flg) {
    q0 = div_u32_round(LcRTC_CalenderLife_s, RTC_CONST86400_NUMB);
  } else {
    q0 = RTC_CONST0_NUMB;
  }

  rtb_Sum = RTC_B.UnitDelayExternalIC.Init0 + q0;
  if (rtb_Sum < RTC_B.UnitDelayExternalIC.Init0) {
    rtb_Sum = MAX_uint32_T;
  }

  set_EcNVM_CalenderLife_Day(rtb_Sum);
  if (CbRTC_CalenderLifeOvrSwt_flg) {
    set_GcRTC_CalenderLife_Day(CcRTC_CalenderLifeOvr_Day);
  } else {
    set_GcRTC_CalenderLife_Day(rtb_Sum);
  }

  rtb_MinMax4 = fmaxf(get_GcADP_AcmDist_numb(), get_EcNVM_HighPreVehOdo_km());
  set_EcNVM_HighPreVehOdo_km(rtb_MinMax4);
  if (CbRTC_SumOdoOvrSwt_flg) {
    set_GcRTC_SumOdo_km(CcRTC_SumOdoOvr_km);
  } else {
    set_GcRTC_SumOdo_km(rtb_MinMax4);
  }

  RTC_UnitDelayExternalIC(get_EcNVM_PackUsageTi_Day(),
    &RTC_B.UnitDelayExternalIC_l, &RTC_DW.UnitDelayExternalIC_l);
  if (get_GeCTC_CtrCtlState_enum() == (int32_T)Enum_CTCCtrCtlState_Online) {
    q0 = RTC_RUNTIME_MS;
  } else {
    q0 = RTC_CONST0_NUMB;
  }

  rtb_Switch_o = q0 + RTC_DW.StateName_d;
  if (rtb_Switch_o < q0) {
    rtb_Switch_o = MAX_uint32_T;
  }

  if (RTC_CONST4294967000_NUMB < rtb_Switch_o) {
    rtb_Switch_o = RTC_CONST4294967000_NUMB;
  }

  if (rtb_Switch_o > RTC_CONST0_NUMB) {
    LcRTC_OnlineTi_ms = rtb_Switch_o;
  } else {
    LcRTC_OnlineTi_ms = RTC_CONST0_NUMB;
  }

  LcRTC_OnlineTi_s = RTC_CONST1000_NUMB == 0U ? MAX_uint32_T : LcRTC_OnlineTi_ms
    / RTC_CONST1000_NUMB;
  LbRTC_OneDayCount2_flg = (LcRTC_OnlineTi_s >= RTC_CONST86400_NUMB);
  if (LbRTC_OneDayCount2_flg) {
    q0 = div_u32_round(LcRTC_OnlineTi_s, RTC_CONST86400_NUMB);
  } else {
    q0 = RTC_CONST0_NUMB;
  }

  q0 += RTC_B.UnitDelayExternalIC_l.Init0;
  if (q0 < RTC_B.UnitDelayExternalIC_l.Init0) {
    q0 = MAX_uint32_T;
  }

  rtb_Switch_o = q0 + get_GcRTC_CalenderLife_Day();
  if (rtb_Switch_o < q0) {
    rtb_Switch_o = MAX_uint32_T;
  }

  set_EcNVM_PackUsageTi_Day(rtb_Switch_o);
  if (CbRTC_PackUsageTiOvrSwt_flg) {
    set_GcRTC_PackUsageTi_Day(CcRTC_PackUsageTiOvr_Day);
  } else {
    set_GcRTC_PackUsageTi_Day(rtb_Switch_o);
  }

  set_GbRTC_ExitRTCWakeup_flg((CbRTC_ExitRTCWakeupOvrSwt_flg &&
    CbRTC_ExitRTCWakeupOvr_flg));
  if (CbRTC_ServTimeReachOvrSwt_flg) {
    set_GbRTC_ServTimeReach_flg(CbRTC_ServTimeReachOvr_flg);
  } else {
    set_GbRTC_ServTimeReach_flg(((boolean_T)ASWTYPE_TRUE));
  }

  set_GbRTC_RtcCalErrFlt_flg((CbRTC_RtcCalErrFltOvrSwt_flg &&
    CbRTC_RtcCalErrFltOvr_flg));
  set_GbRTC_RtcInfoFlt_flg((CbRTC_RtcInfoFltOvrSwt_flg &&
    CbRTC_RtcInfoFltOvr_flg));
  if (CbRTC_SleepWaitTiOvrSwt_flg) {
    set_GcRTC_SleepWaitTi_min(CcRTC_SleepWaitTiOvr_min);
  } else {
    set_GcRTC_SleepWaitTi_min(0U);
  }

  rtb_RelationalOperator = (safe_cast_to_Enum_CTCCtrCtlStat
    (get_GeCTC_CtrCtlState_enum()) == (int32_T)Enum_CTCCtrCtlState_Offline);
  if (rtb_RelationalOperator) {
    rtb_Switch_o = RTC_RUNTIME_MS;
    rtb_RelationalOperator_0 = RTC_DW.StateName_d1;
  } else {
    rtb_Switch_o = RTC_CONST0_NUMB;
    rtb_RelationalOperator_0 = RTC_CONST0_NUMB;
  }

  rtb_Switch_o += rtb_RelationalOperator_0;
  if (RTC_CONST4294967000_NUMB < rtb_Switch_o) {
    rtb_Switch_o = RTC_CONST4294967000_NUMB;
  }

  if (!(rtb_Switch_o > RTC_CONST0_NUMB)) {
    rtb_Switch_o = RTC_CONST0_NUMB;
  }

  if (CbRTC_SleepTimeOvrSwt_flg) {
    set_GcRTC_SleepTime_min(CcRTC_SleepTimeOvr_min);
  } else {
    set_GcRTC_SleepTime_min(RTC_CONST60_NUMB == 0U ? MAX_uint32_T :
      LcRTC_EcuOffTi_s / RTC_CONST60_NUMB);
  }

  rtb_AND = ((!rtb_RelationalOperator) && RTC_DW.UnitDelay_DSTATE);
  RTC_DW.UnitDelay_DSTATE = rtb_RelationalOperator;
  LbRTC_NotOfflineState_flg = (rtb_AND || LbRTC_NotOfflineState_flg);
  if (CbRTC_ShelveTimeOvrSwt_flg) {
    set_GcRTC_ShelveTime_min(CcRTC_ShelveTimeOvr_min);
  } else if (LbRTC_NotOfflineState_flg) {
    set_GcRTC_ShelveTime_min(RTC_CONST60000_NUMB == 0U ? MAX_uint32_T :
      rtb_Switch_o / RTC_CONST60000_NUMB);
  } else {
    set_GcRTC_ShelveTime_min((RTC_CONST60000_NUMB == 0U ? MAX_uint32_T :
      rtb_Switch_o / RTC_CONST60000_NUMB) + get_GcRTC_SleepTime_min());
  }

  RTC_UnitDelayExternalIC_Update(rtb_Sum, &RTC_DW.UnitDelayExternalIC);
  if (LbRTC_OneDayCount1_flg == ((boolean_T)ASWTYPE_FALSE)) {
    RTC_DW.StateName = LcRTC_OfflineTi_ms;
  } else {
    RTC_DW.StateName = RTC_CONST0_NUMB;
  }

  RTC_DW.StateName_i = rtb_Logical1;
  RTC_DW.StateName_f = LbRTC_OneDayCount1_flg;
  RTC_UnitDelayExternalIC_Update(q0, &RTC_DW.UnitDelayExternalIC_l);
  if (LbRTC_OneDayCount2_flg == ((boolean_T)ASWTYPE_FALSE)) {
    RTC_DW.StateName_d = LcRTC_OnlineTi_ms;
  } else {
    RTC_DW.StateName_d = RTC_CONST0_NUMB;
  }

  RTC_DW.StateName_d1 = rtb_Switch_o;
}

/* Model initialize function */
void RTC_initialize(void)
{
  RTC_DW.StateName_i = ((boolean_T)ASWTYPE_FALSE);
  RTC_DW.StateName_f = ((boolean_T)ASWTYPE_FALSE);
  RTC_UnitDelayExternalIC_Init(&RTC_DW.UnitDelayExternalIC);
  RTC_UnitDelayExternalIC_Init(&RTC_DW.UnitDelayExternalIC_l);
  RTC_DW.UnitDelay_DSTATE = true;
}

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