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,
and
model
_initialize
. The code generator produces
reset code only if you model reset behavior. model
_terminate
For example, consider a component model that includes these elements:
An Initialize Function block
Two Reset Function blocks that specify event names
tooHighReset
andtooLowReset
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:
- initialization functionmodel
_initialize
- reset function formodel
_tooHighResettooHighReset
event
- reset function, formodel
_tooLowResettooLowReset
event
- terminate functionmodel
_terminate
If you are using Embedded Coder®, you can 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, Terminate, and Reset Events
This example shows how to generate code for a model that responds to initialize, terminate, and reset events.
Open Model
Open example model StartupResetShutdown
.
open_system('StartupResetShutdown');
Default Initialize and Terminate Entry-Point Functions
If you exclude the Initialize Function, Terminate Function, and Reset Function blocks from model StartupResetShutdown
, the code generator produces initialize and terminate entry-point functions that other code can interface with.
void StartupResetShutdown_initialize(void) void StartupResetShutdown_terminate(void)
The code for these functions appears in the generated file StartupResetShutdown.c
. If you disable support for nonfinite numbers and MAT-file logging, the generated code looks like this:
void StartupResetShutdown_initialize(void) { rtmSetErrorStatus(StartupResetShutdown_M, (NULL)); (void) memset((void *)&StartupResetShutdown_DW, 0, sizeof(DW_StartupResetShutdown_T)); StartupResetShutdown_Y.Out1 = 0.0; StartupResetShutdown_DW.DiscreteIntegrator_DSTATE = 0.0; } void StartupResetShutdown_terminate(void) { /* (no terminate code required) */ }
The initialize function, StartupResetShutdown_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, StartupResetShutdown_terminate
, does not require code.
Generate Code for Startup and Terminate Events
To generate code for startup and terminate events, add the Initialize Function and Terminate Function blocks to your model. When you generate code for a model 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
.
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 StartupResetShutdown_initialize
and StartupResetShutdown_terminate
. If you disable support for nonfinite numbers and MAT-file logging, the generated code for these functions looks like this:
void StartupResetShutdown_initialize(void) { rtmSetErrorStatus(rtwdemo_irt_initterm_M, (NULL)); (void) memset((void *)&StartupResetShutdown_DW, 0, sizeof(DW_StartupResetShutdown_T)); StartupResetShutdown_Y.Out1 = 0.0; StartupResetShutdown_DW.DiscreteIntegrator_DSTATE = 10.0; } void StartupResetShutdown_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 models that are configured to use a data code 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 model 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 model hierarchy and ending with the root (outermost). The name of the function is
model
_reset
. For more information, see Event Names and Code Aggregation.
The Reset Function block in model StartupResetShutdown
is 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.
For this Reset Function block, the code generator produces reset function StartupResetShutdown
_reset.
void StartupResetShutdown_reset(void) { StartupResetShutdown_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 StartupResetShutdownShared
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
(StartupResetShutdownShared_reset
), one for initialize
(StartupResetShutdownShared_initialize
), and one for terminate
(StartupResetShutdownShared_terminate
). Within each entry-point
function, after listing code for blocks configured with an initial condition
(
),
the code generator orders code for components, starting with the lowest level of the hierarchy
and ending with the root.model
_P.block
_IC
. . . void StartupResetShutdownShared_reset(void) { StartupResetShutdownShared_DW.SubIntegrator2_DSTATE = 5.0; StartupResetShutdownShared_DW.Integrator2_DSTATE = 5.0; } . . . void StartupResetShutdownShared_initialize(void) { rtmSetErrorStatus(StartupResetShutdownShared_M, (NULL)); (void) memset(((void *)&StartupResetShutdownShared_DW), 0, sizeof(DW_StartupResetShutdownShared_T)); StartupResetShutdownShared_Y.Out1 = 0.0; StartupResetShutdownShared_DW.Integrator1_DSTATE = 0.0; StartupResetShutdownShared_DW.SubIntegrator2_DSTATE = 2.0; StartupResetShutdownShared_DW.Integrator2_DSTATE = 10.0; . . . void StartupResetShutdownShared_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,
StartupResetShutdownShared_reset
and
StartupResetShutdownShared_reset_02
.
void StartupResetShutdownShared_reset(void) { StartupResetShutdownShared_DW.SubIntegrator2_DSTATE = 5.0; } void StartupResetShutdownShared_reset_02(void) { StartupResetShutdownShared_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
Initialize Function | Reset Function | Terminate Function
Related Topics
- Using Initialize, Reinitialize, Reset, and Terminate Functions
- Configure Generated C Function Interface for Model Entry-Point Functions
- Define Service Interfaces, Storage Classes, Memory Sections, and Function Templates for Software Architecture
- Initialization of Signal, State, and Parameter Data in the Generated Code