Main Content

Startup, Reset, and Shutdown Function Interfaces

Applications sometimes require logic that executes during system initialization, reset, and termination sequences. To model startup, reset, and shutdown processing in a model, use the Initialize Function, Reset Function (data interface configurations only), and Terminate Function blocks. You can use the blocks anywhere in a model hierarchy.

The Initialize Function, Reset Function, and Terminate Function blocks can control execution of an application or component in response to initialize, reset, or terminate events. Examples of when to generate code that responds to these events include:

  • Starting and stopping an application or component.

  • Calculating initial conditions.

  • Saving and restoring state from nonvolatile memory.

  • Generating reset entry-point functions that respond to external events.

You can place the blocks at different levels of a model hierarchy. Each nonvirtual subsystem can have its own set of initialize, reset, and terminate functions. In a lower-level model, Simulink® aggregates the content of the functions with corresponding instances in the parent model.

The Initialize Function and Terminate Function blocks contain an Event Listener block. To specify the event type of the function—Initialize, Reset, or Terminate—use the Event type parameter of the Event Listener block. The function block reads or writes the state of conditions for other blocks. By default, the Initialize Function block initializes block state with the State Writer block. The Terminate Function block saves the block state by using the State Reader block. When the function is triggered by an initialize or terminate event, the function writes the value of the state variable or reads the value from the specified block.

Application and component models can use the Initialize Function, Reset Function (data interface configurations only), and Terminate Function blocks to model complex startup, reset, and shutdown sequences. The subsystems work with various modeling styles. Software-in-the-loop (SIL) simulation of initialize, reset, or terminate functions works with export-function modeling only.

The code generator produces initialize and terminate code differently than reset code. For initialize and terminate code, the code generator includes your initialize and terminate code in the default entry-point functions, model_initialize and model_terminate. The code generator produces reset code only if you model reset behavior.

For example, consider a component model that includes these elements:

  • An Initialize Function block

  • Two Reset Function blocks that specify event names tooHighReset and tooLowReset

  • A Terminate Function block

For this scenario, the code generator produces four entry-point functions that respond to target platform function calls triggered by events:

  • model_initialize - initialization function

  • model_tooHighReset - reset function for tooHighReset event

  • model_tooLowReset - reset function, for tooLowReset event

  • model_terminate - terminate function

Customize the entry-point function names to align with function calls initiated by the target environment function scheduler by applying one of these approaches:

  • For the model, in the Code Mappings editor or by using the code mappings programming interface, configure the function names for model initialize, reset, and terminate functions individually.

  • Define function customization templates in the Embedded Coder Dictionary. Then, for the model, in the Code Mappings editor or by using the code mappings programming interface, configure the code generator to use the templates for the model event-triggered functions.

For more information about modeling startup, reset, and shutdown behavior, see Using Initialize, Reinitialize, Reset, and Terminate Functions. For guidelines on modeling startup and shutdown behavior for component models, see cgsl_0404: Model startup and shutdown events by using Initialize Function and Terminate Function blocks for component deployment.

Generate Code for Initialize and Terminate Events

When you generate code for a component that includes Initialize Function and Terminate Function blocks, the code generator:

  • Includes initialize event code with default initialize code in entry-point function model_initialize.

  • Includes terminate event code with default terminate code in entry-point function model_terminate.

Consider the model rtwdemo_irt_base.

For this model, the code generator produces initialize and terminate entry-point functions that other code can interface with.

void rtwdemo_irt_base_initialize(void)
void rtwdemo_irt_base_terminate(void)

This code appears in the generated file rtwdemo_irt_base.c. The initialize function, rtwdemo_irt_base_initialize:

  • Initializes an error status.

  • Allocates memory for block I/O and state parameters.

  • Sets the output value

  • Sets the initial condition for the discrete integrator.

The terminate function, ,rtwdemo_irt_base_terminate, requires no code.

This code assumes that support for nonfinite numbers and MAT-file logging is disabled.

void rtwdemo_irt_base_initialize(void)
{
  rtmSetErrorStatus(rtwdemo_irt_base_M, (NULL));

  (void) memset((void *)&rtwdemo_irt_base_DW, 0,
                 sizeof(DW_rtwdemo_irt_base_T));   
      
  rtwdemo_irt_base_Y.Out1 = 0.0;

  rtwdemo_irt_base_DW.DiscreteIntegrator_DSTATE = 0.0;
}
	
void rtwdemo_irt_base_terminate(void)
{
  /* (no terminate code required) */
}

Add Initialize Function and Terminate Function blocks to the model (see rtwdemo_irt_initterm). The Initialize Function block uses the State Writer block to set the initial condition of a Discrete Integrator block. The Terminate Function block includes a State Reader block, which reads the state of the Discrete Integrator block.

Parameter Event type for the Event Listener block for the initialize and terminate functions is set to Initialize and Terminate, respectively. The initialize function uses the State Writer block to initialize the input value for the Discrete Integrator block to 10. The terminate function uses the State Reader block to read the state of the Discrete Integrator block.

The code generator includes the event code that it produces for the Initialize Function and Terminate Function blocks with standard initialize and terminate code in entry-point functions rtwdemo_irt_initterm_initialize and rtwdemo_irt_initterm_terminate. This code assumes that support for nonfinite numbers and MAT-file logging is disabled.

void rtwdemo_irt_initterm_initialize(void)
{
  rtmSetErrorStatus(rtwdemo_irt_initterm_M, (NULL));
		
  (void) memset((void *)&rtwdemo_irt__initterm_DW, 0,
	          sizeof(DW_rtwdemo_irt__initterm_T));
	
  rtwdemo_irt_initterm_Y.Out1 = 0.0;

  rtwdemo_irt_initterm_DW.DiscreteIntegrator_DSTATE = 10.0;
}

void rtwdemo_irt__initterm_terminate(void)
{
  /* (no terminate code required) */
} 

For guidelines on modeling startup and shutdown behavior for component models, see cgsl_0404: Model startup and shutdown events by using Initialize Function and Terminate Function blocks for component deployment.

Generate Code for Reset Events

For application models and component models that are configured with a data interface, you can generate code that responds to a reset event by including an Initialize Function or Terminate Function block. Configure the block for a reset by setting the Event type parameter of its Event Listener block to Reset. Also set the Event name parameter. The default name is reset.

The code generator produces a reset entry-point function only if you model reset behavior. If a component contains multiple reset specifications, the code that the code generator produces depends on whether reset functions share an event name. For a given component hierarchy:

  • For reset functions with unique event names, the code generator produces a separate entry-point function for each named event. The name of each function is the name of the corresponding event.

  • For reset functions that share an event name, the code generator aggregates the reset code into one entry-point function. The code for the reset functions appears in order, starting with the lowest level (innermost) of the component hierarchy and ending with the root (outermost). The name of the function is model_reset. For more information, see Event Names and Code Aggregation.

Consider the model rtwdemo_irt_reset, which includes a Reset Function block derived from an Initialize Function block.

The Event type and Event name parameters of the Event Listener block are set to Reset and reset, respectively. The function uses the State Writer block to reset the input value for the Discrete Integrator block to 5.

The code generator produces reset function rtwdemo_irt_reset_reset.

void rtwdemo_irt_reset_reset(void)

{
  rtwdemo_irt_reset_DW.DiscreteIntegrator_DSTATE = 5.0;
}

Event Names and Code Aggregation

Use the Initialize Function and Terminate Function blocks to define multiple initialize, reset, and terminate functions for a component hierarchy. Define only one initialize function and one terminate function per hierarchy level. You can define multiple reset functions for a hierarchy level. The event names that you configure for the functions at a given level must be unique.

When producing code, the code generator aggregates code for functions that have a given event name across the entire component hierarchy into one entry-point function. The code for reset functions appears in order, starting with the lowest level (innermost) of the component hierarchy and ending with the root (outermost). The code generator uses the event name to name the function.

For example, the model rtwdemo_irt_shared includes a subsystem that replicates the initialize, reset, and terminate functions that are in the parent model.

Although the model includes multiple copies of the initialize, reset, and terminate functions, the code generator produces one entry-point function for reset (rtwdemo_irt_shared_reset), one for initialize (rtwdemo_irt_shared_initialize), and one for terminate (rtwdemo_irt_shared_terminate). Within each entry-point function, after listing code for blocks configured with an initial condition (model_P.block_IC), the code generator orders code for components, starting with the lowest level of the hierarchy and ending with the root.

.
.
.
void rtwdemo_irt_shared_reset(void)
{
  rtwdemo_irt_shared_DW.SubIntegrator2_DSTATE = 5.0;

  rtwdemo_irt_shared_DW.Integrator2_DSTATE = 5.0;
} 
.
.
.
void rtwdemo_irt_shared_initialize(void)
{
  rtmSetErrorStatus(rtwdemo_irt_shared_M, (NULL));

  (void) memset(((void *)&rtwdemo_irt_shared_DW), 0,
                sizeof(DW_rtwdemo_irt_shared_T));

  rtwdemo_irt_shared_Y.Out1 = 0.0;

  rtwdemo_irt_shared_DW.Integrator1_DSTATE = 0.0;

  rtwdemo_irt_shared_DW.SubIntegrator2_DSTATE = 2.0;

  rtwdemo_irt_shared_DW.Integrator2_DSTATE = 10.0;
.
.
.
void rtwdemo_irt_shared_terminate(void)
{
   /* (no terminate code required) */
   }

If you rename the event configured for the subsystem reset function to reset_02, the code generator produces two reset entry-point functions, rtwdemo_irt_shared_reset and rtwdemo_irt_shared_reset_02.

void rtwdemo_irt_shared_reset(void)
{
	  rtwdemo_irt_shared_DW.SubIntegrator2_DSTATE = 5.0;
}
 
void rtwdemo_irt_shared_reset_02(void)
{
   rtwdemo_irt_shared_DW.Integrator2_DSTATE = 5.0;
}

Limitation

  • You cannot generate code from a harness model—a root model that contains a Model block, which exposes initialize, reset, or terminate function ports.

  • You cannot model reset processing in a component model that is configured with a service code interface.

See Also

| |

Related Topics