/*
 * Trigger_BGND_BASE_PERIODIC_2158p0006.c
 *
 * Real-Time Workshop code generation for Simulink model "Controller.mdl".
 *
 * Model Version              : 1.375
 * Real-Time Workshop version : 7.3  (R2009a)  15-Jan-2009
 * C source code generated on : Mon Jan 11 12:01:10 2010
 *
 * Target selection: motohawk_ert_rtw.tlc
 * Embedded hardware selection: Specified
 * Code generation objectives: Unspecified
 * Validation result: Not run
 */

#include "Controller.h"
#include "Controller_private.h"

void Trigger_BGND_BASE_PERIODIC_2158p0006(void)
{
  {
    extern NativeError_S GetFreeRunningCounter64Bits(S_FreeRunningCounter64Bit *);
    S_FreeRunningCounter64Bit time64;
    GetFreeRunningCounter64Bits(&time64);
    Controller_M->Timing.clockTick0 = time64.u4Lower32Bits;
    Controller_M->Timing.clockTickH0 = time64.u4Upper32Bits;
  }

  {
    /* Output and update for function-call system: '<S2>/Background' */
    {
      /* local block i/o variables */
      uint16_T rtb_motohawk_ain5;
      boolean_T rtb_Merge;

      {
        boolean_T rtb_LogicalOperator;
        boolean_T rtb_UnitDelay_e;
        boolean_T rtb_RelationalOperator;
        uint16_T rtb_to65535_n;
        boolean_T rtb_LogicalOperator_e;
        uint16_T rtb_to65535_h;
        boolean_T rtb_LogicalOperator_nn;
        boolean_T rtb_UnitDelay_b;
        boolean_T rtb_UnitDelay1;
        uint32_T tmp;

        /* Logic: '<S40>/Logical Operator' incorporates:
         *  Logic: '<S40>/Logical Operator1'
         *  S-Function (motohawk_sfun_calibration): '<S40>/motohawk_calibration'
         *  UnitDelay: '<S40>/Unit Delay'
         */
        rtb_LogicalOperator = ((!VolatileWork.mh_ProcessorReboot) &&
          Controller_DWork.s40_UnitDelay_DSTATE);

        /* UnitDelay: '<S50>/Unit Delay' */
        rtb_UnitDelay_e = Controller_DWork.s50_UnitDelay_DSTATE;

        /* S-Function Block: <S38>/motohawk_ain5 Resource: ECUP */
        {
          extern NativeError_S ECUP_AnalogInput_Get(uint16_T *adc, uint16_T
            *status);
          ECUP_AnalogInput_Get(&rtb_motohawk_ain5, NULL);
        }

        /* RelationalOperator: '<S38>/Relational Operator' incorporates:
         *  S-Function (motohawk_sfun_ain): '<S38>/motohawk_ain5'
         *  S-Function (motohawk_sfun_calibration): '<S38>/motohawk_calibration'
         */
        rtb_RelationalOperator = (rtb_motohawk_ain5 >= ((uint16_T)
          ConstantWork.mh_ECUP_Threshold));

        /* Switch: '<S39>/Switch' incorporates:
         *  Constant: '<S39>/Constant1'
         *  DataTypeConversion: '<S39>/Data Type Conversion'
         *  Sum: '<S39>/Sum'
         *  UnitDelay: '<S39>/Unit Delay'
         */
        if (rtb_RelationalOperator) {
          tmp = (uint32_T)(Controller_DWork.s39_UnitDelay_DSTATE +
                           rtb_RelationalOperator);
          if (tmp > 65535U) {
            rtb_to65535_n = MAX_uint16_T;
          } else {
            rtb_to65535_n = (uint16_T)tmp;
          }
        } else {
          rtb_to65535_n = 0U;
        }

        /* Logic: '<S39>/Logical Operator' */
        rtb_LogicalOperator_e = !rtb_RelationalOperator;

        /* Switch: '<S39>/Switch1' incorporates:
         *  Constant: '<S39>/Constant3'
         *  DataTypeConversion: '<S39>/Data Type Conversion1'
         *  Sum: '<S39>/Sum1'
         *  UnitDelay: '<S39>/Unit Delay1'
         */
        if (rtb_LogicalOperator_e) {
          tmp = (uint32_T)(Controller_DWork.s39_UnitDelay1_DSTATE +
                           rtb_LogicalOperator_e);
          if (tmp > 65535U) {
            rtb_to65535_h = MAX_uint16_T;
          } else {
            rtb_to65535_h = (uint16_T)tmp;
          }
        } else {
          rtb_to65535_h = 0U;
        }

        /* Logic: '<S50>/Logical Operator2' incorporates:
         *  Constant: '<S39>/Constant2'
         *  Constant: '<S39>/Constant4'
         *  Logic: '<S39>/Logical Operator1'
         *  Logic: '<S39>/Logical Operator2'
         *  Logic: '<S50>/Logical Operator'
         *  Logic: '<S50>/Logical Operator1'
         *  RelationalOperator: '<S39>/Relational Operator1'
         *  RelationalOperator: '<S39>/Relational Operator2'
         */
        rtb_LogicalOperator_e = ((rtb_UnitDelay_e || (rtb_RelationalOperator &&
                                   (rtb_to65535_n >= 2))) &&
          (!(rtb_LogicalOperator_e && (rtb_to65535_h >= 5))));

        /* Logic: '<S38>/Logical Operator' incorporates:
         *  Logic: '<S38>/Logical Operator1'
         *  Logic: '<S38>/Logical Operator2'
         *  S-Function (motohawk_sfun_data_read): '<S38>/motohawk_data_read'
         */
        rtb_LogicalOperator_nn = ((!rtb_LogicalOperator) &&
          (rtb_LogicalOperator_e || VolatileWork.mh_MPRD_KeepAlive));

        /* Outputs for trigger SubSystem: '<S38>/Post Shutdown two ticks before MPRD off' incorporates:
         *  TriggerPort: '<S43>/Trigger'
         */
        if ((!rtb_LogicalOperator_nn) &&
            (Controller_PrevZCSigState.PostShutdowntwoticksbeforeMPRDoff_Trig_ZCE
             != ZERO_ZCSIG)) {
          /* S-Function (fcncallgen): '<S43>/Function-Call Generator' incorporates:
           *  SubSystem: '<S43>/Post Shutdown two ticks before MPRD off'
           */
          /* Code Coverage Test: Controller/Main Power Relay1/Background/Post Shutdown two ticks before MPRD off/Post Shutdown two ticks before MPRD off */
          {
            extern void MH_CodeCovered(uint32_T idx);
            MH_CodeCovered(15);
          }

          /* S-Function Block: <S53>/motohawk_event_call */
          {
            /* post the event */
            PostEvent(SHUTDOWN_EVENT);
          }

          /* Code Coverage Test: Controller/Main Power Relay1/Background/Post Shutdown two ticks before MPRD off */
          {
            extern void MH_CodeCovered(uint32_T idx);
            MH_CodeCovered(14);
          }
        }

        Controller_PrevZCSigState.PostShutdowntwoticksbeforeMPRDoff_Trig_ZCE =
          rtb_LogicalOperator_nn ? POS_ZCSIG : ZERO_ZCSIG;

        /* end of Outputs for SubSystem: '<S38>/Post Shutdown two ticks before MPRD off' */

        /* UnitDelay: '<S38>/Unit Delay2' */
        rtb_UnitDelay_e = Controller_DWork.s38_UnitDelay2_DSTATE;

        /* Outputs for enable and trigger SubSystem: '<S38>/Processor Reboot' incorporates:
         *  EnablePort: '<S44>/Enable'
         *  TriggerPort: '<S44>/Trigger'
         *  UnitDelay: '<S38>/Unit Delay5'
         */
        if (Controller_DWork.s38_UnitDelay5_DSTATE) {
          if ((!Controller_DWork.s38_UnitDelay2_DSTATE) &&
              (Controller_PrevZCSigState.ProcessorReboot_Trig_ZCE != ZERO_ZCSIG))
          {
            /* S-Function Block: <S44>/Force Module Reset Force Module Reset */
            {
              extern NativeError_S ModuleSupport_Reset(void);
              ModuleSupport_Reset();
            }

            /* Code Coverage Test: Controller/Main Power Relay1/Background/Processor Reboot */
            {
              extern void MH_CodeCovered(uint32_T idx);
              MH_CodeCovered(16);
            }
          }

          Controller_PrevZCSigState.ProcessorReboot_Trig_ZCE = rtb_UnitDelay_e ?
            POS_ZCSIG : ZERO_ZCSIG;
        }

        Controller_PrevZCSigState.ProcessorReboot_Trig_ZCE = rtb_UnitDelay_e ?
          POS_ZCSIG : ZERO_ZCSIG;

        /* end of Outputs for SubSystem: '<S38>/Processor Reboot' */

        /* UnitDelay: '<S38>/Unit Delay' */
        rtb_UnitDelay_b = Controller_DWork.s38_UnitDelay_DSTATE;

        /* Outputs for trigger SubSystem: '<S38>/Save NV Vars one tick before MPRD off' incorporates:
         *  TriggerPort: '<S45>/Trigger'
         */
        if ((!Controller_DWork.s38_UnitDelay_DSTATE) &&
            (Controller_PrevZCSigState.SaveNVVarsonetickbeforeMPRDoff_Trig_ZCE
             != ZERO_ZCSIG)) {
          /* S-Function (fcncallgen): '<S45>/Function-Call Generator' incorporates:
           *  SubSystem: '<S45>/Save NV Vars one tick before MPRD off'
           */
          /* Code Coverage Test: Controller/Main Power Relay1/Background/Save NV Vars one tick before MPRD off/Save NV Vars one tick before MPRD off */
          {
            extern void MH_CodeCovered(uint32_T idx);
            MH_CodeCovered(18);
          }

          {
            extern void StoreNonVolatile(void);
            StoreNonVolatile();
          }

          /* Code Coverage Test: Controller/Main Power Relay1/Background/Save NV Vars one tick before MPRD off */
          {
            extern void MH_CodeCovered(uint32_T idx);
            MH_CodeCovered(17);
          }
        }

        Controller_PrevZCSigState.SaveNVVarsonetickbeforeMPRDoff_Trig_ZCE =
          rtb_UnitDelay_b ? POS_ZCSIG : ZERO_ZCSIG;

        /* end of Outputs for SubSystem: '<S38>/Save NV Vars one tick before MPRD off' */

        /* UnitDelay: '<S38>/Unit Delay1' */
        rtb_UnitDelay1 = Controller_DWork.s38_UnitDelay1_DSTATE;

        /* Outputs for trigger SubSystem: '<S38>/Shutdown power on ECU565-128' incorporates:
         *  TriggerPort: '<S46>/Trigger'
         */
        if ((!Controller_DWork.s38_UnitDelay1_DSTATE) &&
            (Controller_PrevZCSigState.ShutdownpoweronECU565128_Trig_ZCE !=
             ZERO_ZCSIG)) {
          /* S-Function (fcncallgen): '<S46>/Function-Call Generator' incorporates:
           *  SubSystem: '<S46>/Shutdown power on ECU565-128'
           */
          /* Code Coverage Test: Controller/Main Power Relay1/Background/Shutdown power on ECU565-128/Shutdown power on ECU565-128 */
          {
            extern void MH_CodeCovered(uint32_T idx);
            MH_CodeCovered(20);
          }

          /* S-Function Block: <S55>/motohawk_shutdown_power */
          {
            /* Shut off power on PCM-128.  Other modules will ignore this call */
            SendCloseOSMessage();
          }

          /* Code Coverage Test: Controller/Main Power Relay1/Background/Shutdown power on ECU565-128 */
          {
            extern void MH_CodeCovered(uint32_T idx);
            MH_CodeCovered(19);
          }
        }

        Controller_PrevZCSigState.ShutdownpoweronECU565128_Trig_ZCE =
          rtb_UnitDelay1 ? POS_ZCSIG : ZERO_ZCSIG;

        /* end of Outputs for SubSystem: '<S38>/Shutdown power on ECU565-128' */

        /* If: '<S47>/If' incorporates:
         *  ActionPort: '<S56>/Action Port'
         *  ActionPort: '<S57>/Action Port'
         *  S-Function (motohawk_sfun_calibration): '<S47>/override_enable'
         *  SubSystem: '<S47>/NewValue'
         *  SubSystem: '<S47>/OldValue'
         */
        if (VolatileWork.mh_MPRD_ovr) {
          /* Inport: '<S56>/In1' incorporates:
           *  S-Function (motohawk_sfun_calibration): '<S47>/new_value'
           */
          rtb_Merge = VolatileWork.mh_MPRD_new;

          /* Code Coverage Test: Controller/Main Power Relay1/Background/motohawk_override_abs/NewValue */
          {
            extern void MH_CodeCovered(uint32_T idx);
            MH_CodeCovered(21);
          }
        } else {
          /* Inport: '<S57>/In1' incorporates:
           *  Logic: '<S38>/Logical Operator3'
           */
          rtb_Merge = (rtb_LogicalOperator_nn || rtb_UnitDelay_e);

          /* Code Coverage Test: Controller/Main Power Relay1/Background/motohawk_override_abs/OldValue */
          {
            extern void MH_CodeCovered(uint32_T idx);
            MH_CodeCovered(22);
          }
        }

        /* S-Function (motohawk_sfun_data_write): '<S38>/motohawk_data_write' */
        /* Write to Data Storage as scalar: MPRD */
        {
          VolatileWork.mh_MPRD = rtb_Merge;
        }

        /* S-Function Block: MPRD */
        {
        }

        /* UnitDelay: '<S38>/Unit Delay3' */
        rtb_UnitDelay_e = Controller_DWork.s38_UnitDelay3_DSTATE;

        /* UnitDelay: '<S38>/Unit Delay4' */
        rtb_RelationalOperator = Controller_DWork.s38_UnitDelay4_DSTATE;

        /* Outputs for trigger SubSystem: '<S41>/Clear' incorporates:
         *  S-Function (motohawk_sfun_calibration): '<S41>/motohawk_calibration'
         *  TriggerPort: '<S51>/Trigger'
         */
        if ((!VolatileWork.mh_RestoreNVFactoryDefaults) &&
            (Controller_PrevZCSigState.Clear_Trig_ZCE_e != ZERO_ZCSIG)) {
          /* S-Function (fcncallgen): '<S51>/Function-Call Generator' incorporates:
           *  SubSystem: '<S38>/motohawk_restore_nvmem'
           */
          /* Code Coverage Test: Controller/Main Power Relay1/Background/motohawk_restore_nvmem */
          {
            extern void MH_CodeCovered(uint32_T idx);
            MH_CodeCovered(23);
          }

          /* S-Function Restore Factory Defaults */
          {
            extern void RestoreDefaultNonVolatile(void);
            extern uint8_T NonVolatileStatus;
            RestoreDefaultNonVolatile();
            NonVolatileStatus = 5;
          }

          /* Code Coverage Test: Controller/Main Power Relay1/Background/Display Variable Event Function-Call/Clear */
          {
            extern void MH_CodeCovered(uint32_T idx);
            MH_CodeCovered(12);
          }
        }

        Controller_PrevZCSigState.Clear_Trig_ZCE_e =
          VolatileWork.mh_RestoreNVFactoryDefaults ? POS_ZCSIG : ZERO_ZCSIG;

        /* end of Outputs for SubSystem: '<S41>/Clear' */

        /* Outputs for trigger SubSystem: '<S42>/Clear' incorporates:
         *  S-Function (motohawk_sfun_calibration): '<S42>/motohawk_calibration'
         *  TriggerPort: '<S52>/Trigger'
         */
        if ((!VolatileWork.mh_StoreNV) &&
            (Controller_PrevZCSigState.Clear_Trig_ZCE != ZERO_ZCSIG)) {
          /* S-Function (fcncallgen): '<S52>/Function-Call Generator' incorporates:
           *  SubSystem: '<S38>/motohawk_store_nvmem'
           */
          /* Code Coverage Test: Controller/Main Power Relay1/Background/motohawk_store_nvmem */
          {
            extern void MH_CodeCovered(uint32_T idx);
            MH_CodeCovered(24);
          }

          {
            extern void StoreNonVolatile(void);
            StoreNonVolatile();
          }

          /* Code Coverage Test: Controller/Main Power Relay1/Background/Display Variable Event Function-Call1/Clear */
          {
            extern void MH_CodeCovered(uint32_T idx);
            MH_CodeCovered(13);
          }
        }

        Controller_PrevZCSigState.Clear_Trig_ZCE = VolatileWork.mh_StoreNV ?
          POS_ZCSIG : ZERO_ZCSIG;

        /* end of Outputs for SubSystem: '<S42>/Clear' */
        /* Code Coverage Test: Controller/Main Power Relay1/Background */
        {
          extern void MH_CodeCovered(uint32_T idx);
          MH_CodeCovered(11);
        }

        /* Update for UnitDelay: '<S40>/Unit Delay' incorporates:
         *  Update for S-Function (motohawk_sfun_calibration): '<S40>/motohawk_calibration'
         */
        Controller_DWork.s40_UnitDelay_DSTATE = VolatileWork.mh_ProcessorReboot;

        /* Update for UnitDelay: '<S50>/Unit Delay' */
        Controller_DWork.s50_UnitDelay_DSTATE = rtb_LogicalOperator_e;

        /* Update for UnitDelay: '<S39>/Unit Delay' */
        Controller_DWork.s39_UnitDelay_DSTATE = rtb_to65535_n;

        /* Update for UnitDelay: '<S39>/Unit Delay1' */
        Controller_DWork.s39_UnitDelay1_DSTATE = rtb_to65535_h;

        /* Update for UnitDelay: '<S38>/Unit Delay5' */
        Controller_DWork.s38_UnitDelay5_DSTATE = rtb_RelationalOperator;

        /* Update for UnitDelay: '<S38>/Unit Delay2' */
        Controller_DWork.s38_UnitDelay2_DSTATE = rtb_UnitDelay1;

        /* Update for UnitDelay: '<S38>/Unit Delay' */
        Controller_DWork.s38_UnitDelay_DSTATE = rtb_LogicalOperator_nn;

        /* Update for UnitDelay: '<S38>/Unit Delay1' */
        Controller_DWork.s38_UnitDelay1_DSTATE = rtb_UnitDelay_b;

        /* S-Function Block: MPRD */
        {
          if ((VolatileWork.mh_init_resource_MPRD < 0) && (((int32_T) RES_MPRD) >=
               0)) {
            S_DiscreteOutCreateAttributes CreateInfo;
            NativeError_S sErrorResult;
            CreateInfo.DynamicObj.eState = (rtb_Merge) ? RES_ON : RES_OFF;
            CreateInfo.DynamicObj.eResourceCondition = RES_ENABLED;
            CreateInfo.DynamicObj.uValidAttributesMask = USE_DISCRETE_CONDITION |
              USE_DISCRETE_STATE;
            CreateInfo.uValidAttributesMask = USE_DISCRETE_DYNAMIC_ON_CREATE;
            sErrorResult = CreateResource((E_ModuleResource) (((int32_T)
              RES_MPRD)), &CreateInfo, BEHAVIOUR_DISCRETE_OUT);
            if (SUCCESS(sErrorResult)) {
              VolatileWork.mh_init_resource_MPRD = ((int32_T) RES_MPRD);
            } else {
              VolatileWork.mh_init_resource_MPRD = -1;
              LogNativeError(sErrorResult);
            }

            {
              extern uint8_T dout_create_MPRD;
              if (SUCCESS(sErrorResult))
                dout_create_MPRD = 0;
              else
                dout_create_MPRD = (uint8_T) GetErrorCode(sErrorResult);
            }
          } else if (VolatileWork.mh_init_resource_MPRD >= 0) {
            SetDiscreteOutState((E_ModuleResource)
                                (VolatileWork.mh_init_resource_MPRD), (rtb_Merge)
                                ? RES_ON : RES_OFF);
          }
        }

        /* Update for UnitDelay: '<S38>/Unit Delay3' */
        Controller_DWork.s38_UnitDelay3_DSTATE = rtb_LogicalOperator;

        /* Update for UnitDelay: '<S38>/Unit Delay4' */
        Controller_DWork.s38_UnitDelay4_DSTATE = rtb_UnitDelay_e;
      }
    }
  }
}
