Main Content

Set Up Model for Controlling Program Execution

To enable control of when generated code stops executing, for example, for instrumentation purposes, use the model configuration parameter Stop time or the Stop Simulation block.

When specifying stop time for generated code:

  • The value of parameter Stop time must be greater than or equal to 0, the required setting of parameter Start time for code generation.

  • If the stop time is zero or less than the start time, the generated program runs for one step.

  • If the stop time is set to inf, the generated program runs indefinitely. Typically, this is the stop time used when generating production code.

For the Stop Simulation block, the code generator inserts a call to the rtmSetStopRequested macro in the generated code. A main function can then check the setting of stopRequestedFlag. For more information, see Real-Time Model Data Structure.

Absolute Time Considerations

Some blocks have a dependency on absolute time (see Blocks That Depend on Absolute Time). If your model uses a block that depends on absolute time you might want to set up the code generated from the model to run indefinitely (with Stop time set to inf). The structure and macros that you use to do this depends on your system target file. For simulation targets, such as the one for Simulink® accelerator mode, the code generator produces the SimStruct data structure and corresponding macros. For other system target files, such as grt.tlc and ert.tlc, the code generator produces the rtModel data structure and corresponding macros. The rtModel data structure eliminates the potential for timer overflows that might occur during program execution. For more information, see Real-Time Model Data Structure and S-Function SimStruct Functions.

If you know how long an application that depends on absolute time needs to run, you can prevent timers from overflowing and force the use of optimal word sizes by changing the setting of model configuration parameter Application lifespan (days). See Optimize Memory Usage and Prevent Overflows for Time Counters for details.

Embedded Coder Considerations

For Simulink Coder, the code generator always honors the specified stop time from the generated static main function (see Static Main Function Module). When using Embedded Coder, the code generator honors a stop time setting if you select one of these model configuration parameters:

If you do not select one of these parameters, the program runs indefinitely.

The ERT system target file provides generated and static examples of the ert_main.c file. This file controls the overall model code execution by:

  • Calling the model_step function

  • Stopping program execution and then checking the settings of the error status (error_status) and simulation stop requested (stopRequestedFlag) flags in the real-time model data structure. For more information, see Real-Time Model Data Structure

Override Stop Time Set in Generated Code

From the Microsoft® Windows® command prompt or UNIX®1 command line, you can override the stop time that was set up for a generated program. To override the stop time, use the -tf switch.

model -tf n

The program runs for n seconds. If n is inf, the program runs indefinitely.

Custom Target Considerations

For a custom target, if you provide your own custom static main.c file, consider including support for stopping program execution. Use the macros rtmGetErrorStatus and rtmGetStopRequested to check the settings of the error status and simulation stop requested flags in the real-time model data structure (rtModel).

For more information, see Real-Time Model Data Structure and Custom Software for Target Hardware.

Related Topics


1 UNIX is a registered trademark of The Open Group in the United States and other countries.