%%============================================================================
%%File:ertextmode.tlc
%%
%%Abstract:
%%ExternalModesupportforcodegeneration'sEmbedded-Ccodeformat.
%%
%%Copyright2003-2013TheMathWorks,Inc.
%%
%%============================================================================
%selectfile NULL_FILE
 
%if EXISTS("_ERT_EXTMODE_") == 0
%assign _ERT_EXTMODE_ = 1
 
%%Function:SLibErtGenExtModeCode======================================
%%Abstract:
%%GenerateextModeHeadandextModeTrailer,andcachecode
%%WhenGenerateGRTWrapperistrue
%%returnsemptycodebecausegrt_main()
%%willcallextmodeinterfacefunction.
%%
%function SLibErtGenExtModeCode(buffsRec, isPeriodicRateGrouping) void
  %assign ::BlockFcn = "Output"
 
  %openfile extModeHeader
  %% VxWorks doesn't need to pause and check for stop request here
  %if (ExtMode == 1) && (TargetOS != "VxWorksExample") && !EXISTS(C6000_EXT_MODE) && (!ExtModeXCP || ExtModeXCPClassicInterface)
    /* External mode */
    {
    boolean_T rtmStopReq = %;
    rtExtModePauseIfNeeded(%<RTMGet("RTWExtModeInfo")>, ...
      %<NumRuntimeExportedRates>, ...
      &rtmStopReq);
    if (rtmStopReq) {
      %<RTMSetStopRequested("true")>;
    }
     
    if (%<RTMGetStopRequested()> == %) {
      %if !GenerateGRTWrapper
        %% Do NOT set error status for classic call interface (for backward compatibility)
        %<RTMSetErrStat("/"Simulation finished/"")>;
      %endif
      break;
    }
    }
  %endif
  %closefile extModeHeader
  %addtorecord buffsRec ExtModeHeader extModeHeader
 
  %assign ::BlockFcn = "RootUpdate"
  %if ExtMode == 1
    %if !isPeriodicRateGrouping
      %% Always SingleTasking mode for ERT
      %openfile extModeTrailer
       
      %if (!ExtModeXCP || ExtModeXCPClassicInterface)
        /* External mode */
        rtExtModeUploadCheckTrigger(%<NumRuntimeExportedRates>);
      %endif
 
      %foreach tidIdx = NumRuntimeExportedRates
        %% Set all tids, independent of tid01Eq
        %if ::IncludeLoggingInstrumentation
          %<FcnGenerateTidGuardOpenCode(tidIdx)>
        %else
          %<FcnGenerateTidGuardOpenCodeInMain(tidIdx)>
        %endif
 
        %if (!ExtModeXCP || ExtModeXCPClassicInterface)
          rtExtModeUpload(%<tidIdx>, (real_T)%<RTMGetTaskTimeForTID(tidIdx)>);
        %else
          %if (NumRuntimeExportedRates != 1) || !CombineOutputUpdateFcns
            %% In the case of Multirate model or if CombineOutputUpdateFcns is disabled
            %% the model generated code never invokes the external Mode APIs,
            %% so by adding it here we guarantee that the extmodeEvent() is correctly
            %% invoked in rt_OneStep()
            extmodeErrorCode_T errorCode = EXTMODE_SUCCESS;
            extmodeSimulationTime_T currentTime = (extmodeSimulationTime_T) %<RTMGetTaskTimeForTID(tidIdx)>;
   
            /* Trigger External Mode event */
            errorCode = extmodeEvent(%<tidIdx>,currentTime);
            if (errorCode != EXTMODE_SUCCESS) {
                /* Code to handle External Mode event errors
                   may be added here */
            }
          %endif
        %endif
 
        %<FcnGenerateTidGuardCloseCode(tidIdx)>
      %endforeach
      %closefile extModeTrailer
      %addtorecord buffsRec ExtModeTrailer extModeTrailer
    %else
      %% MultiTasking mode/RateGrouping
      %openfile extModeTrailer0
       
      %if (!ExtModeXCP || ExtModeXCPClassicInterface)
        /* External mode */
        rtExtModeUploadCheckTrigger(%<NumRuntimeExportedRates>);
      %endif
      %closefile extModeTrailer0
      %assign tid01Eq = FixedStepOpts.TID01EQ
      %foreach tidIdx = NumRuntimeExportedRates
        %openfile tmpBuff
        %if tidIdx == 0
          %<extModeTrailer0>
        %endif
        %if (!ExtModeXCP || ExtModeXCPClassicInterface)
          %if tid01Eq && tidIdx == 0
            rtExtModeUpload(%<tid01Eq>, (real_T)%<RTMGetTaskTimeForTID(tidIdx)>);
          %else
            rtExtModeUpload(%<tidIdx>, (real_T)%<RTMGetTaskTimeForTID(tidIdx)>);
          %endif
        %else
          %if (NumContStates > 0) && (tidIdx == 0)
            %% If the model contains Continuous States, the extmodeEvent()
            %% needs to be invoked in the modelStep0
            {
                extmodeErrorCode_T returnCode = EXTMODE_SUCCESS;
            %if tid01Eq
                /* Trigger External Mode event */
            returnCode = extmodeEvent(%<tid01Eq>,%<RTMGetTaskTimeForTID(tidIdx)>);
            %else
                /* Trigger External Mode event */
            returnCode = extmodeEvent(%<tidIdx>,%<RTMGetTaskTimeForTID(tidIdx)>);
            %endif
 
                if (returnCode != EXTMODE_SUCCESS) {
                    /* Code to handle External Mode event errors
                       may be added here */
                }
            }
          %endif
        %endif
        %closefile tmpBuff
        %addtorecord buffsRec ExtModeTrailer%<tidIdx> tmpBuff
      %endforeach
    %endif
  %endif
%endfunction %% SLibErtGenExtModeCode
 
%%Function:SLibGenERTExtModeInit=============================================
%%Abstract:
%%Generateinitializationcodeforexternalmode.
%%WhenGenerateGRTWrapperistrue
%%returnsemptycodebecausegrt_main()
%%willcallextmodeinterfacefunction.
%%
%function SLibGenERTExtModeInit() void
   
  %openfile extModeInitBuf
  %if (!ExtModeXCP || ExtModeXCPClassicInterface)
    /* External mode */
    %if !PurelyIntegerCode
      rtSetTFinalForExtMode(&%<RTMGet("TFinal")>);
    %else
      rtSetTFinalForExtMode(rteiGetPtrTFinalTicks(%<RTMGet("RTWExtModeInfo")>));
    %endif
 
    %% for VxWorks, this initialization is done elsewhere
    %if TargetOS != "VxWorksExample" && !EXISTS(C6000_EXT_MODE)
      rtExtModeCheckInit(%<NumRuntimeExportedRates>);
      {
        boolean_T rtmStopReq = %;
        rtExtModeWaitForStartPkt(%<RTMGet("RTWExtModeInfo")>, ...
          %<NumRuntimeExportedRates>, ...
          &rtmStopReq);
        if (rtmStopReq) {
          %<RTMSetStopRequested("true")>;
        }
      }
    %endif
     
    rtERTExtModeStartMsg();
  %else
    /* External Mode initialization */
    %if !PurelyIntegerCode
      errorCode = extmodeInit(%<RTMGet">RTMGet("RTWExtModeInfo")>, &%<RTMGet">RTMGet("TFinal")>);
    %else
      errorCode = extmodeInit(%<RTMGet">RTMGet("RTWExtModeInfo")>, (extmodeSimulationTime_T *)rteiGetPtrTFinalTicks(%<RTMGet">RTMGet("RTWExtModeInfo")>));
    %endif
    if (errorCode != EXTMODE_SUCCESS) {
        /* Code to handle External Mode initialization errors
           may be added here */
    }
 
    if (errorCode == EXTMODE_SUCCESS) {
        /* Wait until a Start or Stop Request has been received from the Host */
        extmodeWaitForHostRequest(EXTMODE_WAIT_FOREVER);
 
        if (extmodeStopRequested()) {
          %<RTMSetStopRequested("true")>;
        }
    }
  %endif
 
  %closefile extModeInitBuf
   
  %return extModeInitBuf
 
%endfunction %% SLibGenERTExtModeInit
 
%%Function:FcnGenerateExtModeOneStep====================
%%Abstract:
%%GeneratethertExtModeOneStepfunctioncall
%%
%function FcnGenerateExtModeOneStep() void
  %openfile retBuf
 
  %if (!ExtModeXCP || ExtModeXCPClassicInterface)
      /* External mode */
      %% for VxWorks, the stepping is done by a seperate task
      %if TargetOS != "VxWorksExample" && !EXISTS(C6000_EXT_MODE)
        {
          boolean_T rtmStopReq = %;
          rtExtModeOneStep(%<RTMGet("RTWExtModeInfo")>, ...
            %<NumRuntimeExportedRates>, ...
            &rtmStopReq);
          if (rtmStopReq) {
            %<RTMSetStopRequested("true")>;
          }
        }
      %endif
  %else
    /* Run External Mode background activities */
    errorCode = extmodeBackgroundRun();
    if (errorCode != EXTMODE_SUCCESS) {
        /* Code to handle External Mode background task errors
           may be added here */
    }
 
  %endif
 
  %closefile retBuf
 
  %return retBuf
%endfunction
 
%endif %% _ERT_EXTMODE_
 
%%[EOF]ertextmode.tlc