Real-Time Workshop | ![]() ![]() |
Rapid Prototyping Application Components
The application components contain the generated code for the Simulink model, including the code for any S-functions in the model. This code is referred to as the model code because these functions implement the Simulink model.
However, the generated code contains more than just functions to execute the model (as described in the previous section). There are also functions to perform initialization, facilitate data access, and complete tasks before program termination. To perform these operations, the generated code must define functions that:
The Real-Time Model Data Structure
The generated code includes the file simstruc.h
, which contains the definition of the SimStruct
data structure. Each instance of a model (or an S-function) in the program creates its own SimStruct
, which it uses for reading and writing data.
All functions in the generated code are public. For this reason, there can be only one instance of a model in a real-time program. This function, which always has the same name as the model, is called during program initialization to return a pointer to the SimStruct
and initialize any S-functions.
The real-time model data structure encapsulates model data and associated information necessary to fully describe the model. Its contents include:
The real-time model data structure is used for all targets. In previous releases, the ERT target used the rtObject data structure, and other targets used the simstruct data structure for encapsulating model data. Now all targets are treated the same, except for the fact that the real-time model data structure is pruned for ERT targets to save space in executables. Even when not pruned, the real-time model data structure is more space-efficient than the root simstruct used by earlier releases for non-ERT targets, as it only contains fields for child (S-function) simstructs that are actually used in a model.
Rapid Prototyping Model Code Functions
The functions defined by the model code are called at various stages of program execution (i.e., initialization, model execution, or program termination).
The following diagram illustrates the functions defined in the generated code and shows what part of the program executes each function.
Figure 7-8: Execution of the Model Code
The Model Registration Function
The model registration function has the same name as the Simulink model from which it is generated. It is called directly by the main program during initialization. Its purpose is to initialize and return a pointer to the real-time model data structure.
Models Containing S-Functions
A noninlined S-function is any C MEX S-function that is not implemented using a customized TLC file. If you create a C MEX S-function as part of a Simulink model, it is by default noninlined unless you write your own TLC file that inlines it within the body of the model
.c
code. Real-Time Workshop automatically incorporates your non-inlined C code S-functions into the program if they adhere to the S-function API described in the Simulink documentation.
This format defines functions and a SimStruct
that are local to the S-function. This allows you to have multiple instances of the S-function in the model. The model's real-time model data structure contains a pointer to each S-function's SimStruct
.
Code Generation and S-Functions
If a model contains S-functions, the source code for the S-function must be on the search path the make utility uses to find other source files. The directories that are searched are specified in the template makefile that is used to build the program.
S-functions are implemented in a way that is directly analogous to the model code. They contain their own public registration function (which is called by the top-level model code) that initializes static function pointers in its SimStruct
. When the top-level model needs to execute the S-function, it does so via the function pointers in the S-function's SimStruct
. The S-functions use the same SimStruct
data structure as the generated code; however, There can be more than one S-function with the same name in your model. This is accomplished by having function pointers to static functions.
Inlining S-Functions
You can incorporate C MEX S-functions, along with the generated code, into the program executable. You can also write a target file for your C MEX S-function to inline the S-function, thus improving performance by eliminating function calls to the S-function itself. For more information on inlining S-functions, see the Target Language Compiler Reference Guide.
Application Modules for Application Components
When Real-Time Workshop generates code, it produces the following files:
model
.c
-- The C code generated from the Simulink block diagram. This code implements the block diagram's system equations as well as performing initialization and updating outputs.
model
_data.c
-- An optional file containing data for parameters and constant block i/o, which are also declared as extern
in model
.h
. Only generated when rtP
and rtC
structures are populated.
model
.h
-- Header file containing the block diagram's simulation parameters, I/O structures, work structures, and other declarations. It includes model
_private.h
.
model
_private.h
-- Header file containing declarations of exported signals and parameters.
These files are named for the Simulink model from which they are generated.
In addition, a dummy include file always named rtmodel.h
is generated, which includes the above model-specific data structures and entry points. This enables the (static) target-specific main programs to reference files generated by Real-Time Workshop without needing to know the names of the models involved.
If you have created custom blocks using C MEX S-functions, you need the source code for these S-functions available during the build process.
![]() | Rapid Prototyping System-Independent Components | Embedded Program Framework | ![]() |