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 component model, use the Initialize Function, Reset Function, 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 a component in response to initialize, reset, or terminate events. Examples of when to generate code that responds to these events include:
Starting and stopping a 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.
Component models can use the Initialize Function, Reset Function, 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 function-call modeling only.
The code generator produces initialization and termination code differently than reset
code. For initialization and termination code, the code generator includes your component
initialization and termination 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 callable entry-point functions that respond to platform function calls triggered by events:
- initialization functionmodel
_initialize
- reset function formodel
_tooHighResettooHighReset
event
- reset function, formodel
_tooLowResettooLowReset
event
- terminate functionmodel
_terminate
Customize the entry-point function names to align with function calls initiated by the platform 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 name for model initialize, reset, and terminate functions individually.
Define a function customization template for the initialize, reset, and terminate functions 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 that template for the model event-triggered functions.
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
_initializeIncludes 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) */ }
Generate Code for Reset Events
Generate code that responds to a reset event by including an Initialize Function or Terminate Function block in a modeling
component. 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
. For more information, see Event Names and Code Aggregation.model
_reset
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
(
),
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 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.
See Also
Initialize Function | Reset Function | Terminate Function