3 Working with TargetLink-Generated Code in ReactisdSPACE provides a utility for wrapping generated C code in a
TargetLink-independent S-Function for use in the simulation environment. Since the
Reactis for C Plugin supports white-box analysis of‘S-Functions, the wrapper
model provides an easy means to work with generated code in Reactis. In
particular, it can be used to verify the model behavior against the code using
the comprehensive test vectors generated by Reactis Tester from the TargetLink
model. 3.1 Creating an S-Function Wrapper for TargetLink-Generated CodeTo create a TargetLink-independent S-Function wrapper for generated C code,
do the following: - Make sure the TargetLink model runs successfully in Model-In-the-Loop
(MIL) mode in TargetLink.
- Make sure the TargetLink model runs successfully in Software-In-the-Loop
(SIL) mode. This confirms that the code can be generated from the model, and
that any supporting C files have been located, and that a linkable construct
has been built.
- Select the option “Do not log anything” in the Simulation Frame Options window
of the TargetLink main dialog.
- To build the TargetLink-independent S-Function, at the MATLAB command prompt, type
the following (filling in the appropriate parameters):
tl_build_standalone('Model',bdroot,'destModel','demo_dll', 'GenerateAll','On');
For more information, at the MATLAB command prompt type:
help tl_build_standalone
Executing the tl_build_standalone command creates a new version of the
model consisting of a single S-Function wrapping the generated C code. 3.2 Comparing Behavior of Generated Code to ModelSince the S-Function wrapper model is a standard Simulink model, there are a
number of ways to easily compare its behavior against the original model from
which it was generated. To describe the different approaches assume the
following:
-
A.mdl is a TargetLink model
- Reactis Tester is used to generate test suite TS.rst from A.mdl
- B.mdl is generated from A.mdl using
tl_build_standalone
The following approaches are then available to compare the behavior of A.mdl
to the behavior of B.mdl.
-
Using the runtests utility
-
In Reactis Simulator, export TS.rst to a MATLAB file TS.m
- In MATLAB type runtests 2 at the MATLAB command line
to start the runtests GUI.
- In the runtests GUI:
-
Click the Load Model button and select B.mdl
- Click the Load Test File button and select TS.m
- Click the Run Test button. The tests generated from
A.mdl will be run on B.mdl to flag any differences in behavior.
- Using the Reactis for C Plugin and white-box mode
-
Make sure a license is available and enable the Reactis for C Plugin
- Load B.mdl in Reactis
- Create and attach a Reactis Makefile (.rsm file) that lists the C source files used by B.mdl.
This process is described in Section 3.3.
- Start Reactis Simulator
- Load and run TS.rst. Any differences in behavior will be flagged. The executing
C code can be examined.
NOTE: Using Reactis in black box mode is not an option for comparing the
S-Function wrapped code against the model. This is because the TargetLink-generated
code uses global variables and the generated wrapper does not register these with
S-Function callbacks to track the global data as required by the black-box mode of
Reactis (See Section
14.2.1
of the Reactis User’s Guide). 3.3 Creating Reactis Makefiles for S-Function Wrapper Models of TargetLink-Generated Code
The Reactis for C Plugin offers white-box testing of the C code parts of models
(S-Functions and Stateflow custom code). This section describes how to create a
Reactis Makefile (RSM file) for S-Function wrapper models generated by the
steps described above. Doing so enables the white-box analysis of TargetLink-generated
code in Reactis. IMPORTANT NOTE: The tl_hostframe.c file mentioned below includes
the file windows.h. This file is not included in the Reactis for C
header files; however, there is an easy workaround. Simply create an empty
windows.h file and place it in the folder where the S-Function wrapper
model resides. An RSM file includes the following information:
-
The C source files comprising the S-Function
- The location of RSM files for any libraries used by the S-Function
- The search path for finding header files during preprocessing
- Any macro definitions required to compile the S-Function
Of course this is exactly the information contained in the build scripts used to
compile the S-Function DLL which is used when the wrapper model is run in
Simulink outside of Reactis. In its normal mode of operation TargetLink hides
the details of the build script from the user when constructing the DLL.
In order to discover these details, we ran tl_build_standalone in verbose
mode to capture the mex command used to build the DLL. Translation of this
information (C files, libraries, include path, macro definitions)
to an RSM file is fairly straightforward. The RSM file for one of the TargetLink demo models is shown
in Figure 5.
This particular RSM file is configured for TargetLink 3.0.
Note that the settings may vary for older versions of TargetLink.
The RSM File Editor (shown in the
figure) is opened by right-clicking on an S-Function in the main Reactis panel and
then selecting either "Assign RSM File" (to create a new RSM file) or "Edit RSM File"
(to edit an existing RSM file). The C files are the following:
-
TLSim\fuelratecontroller_sfcn.c
- S-Function wrapper code. TargetLink
generates this file in the TLSim subfolder of the model folder. The file name
is formed by adding "
_sfcn" to base name of main C file.
- fuelratecontroller.c
- Main C file generated by TargetLink.
- C:\dspace\TL300R2008a\MATLAB\tl\SimFiles\HostPC\tl_hostframe.c
- This file
is part of the TargetLink distribution. Adjust the path according to where you installed
TargetLink.
No libraries are used by this model. If you require libraries you would need to define
RSM files for those libraries and list them in the "Libraries" section of the RSM file for the S-Function.
The first 4 entries of the Include Search Path are from the TargetLink distribution and
will be the same for any generated S-Function wrapper model. The
last two entries are the folder where the S-Function wrapper model is located and
its TLSim subfolder (generated by TargetLink). The name of the
__FUELRATECONTROLLER_SFCN__ macro should be adjusted to match
the name of the S-function being compiled,
but the other macro definitions do not change for different models.
| Figure 5: The RSM file for an S-Function wrapper model in TargetLink 3.0. |
Some other information to be aware of when creating RSM files includes the following: -
The macro "MX_COMPAT_32" is defined in some generated build scripts.
When defined, this macro
specifies that 32-bit mode should be used when compiling on a 64-bit architecture.
Currently the Reactis for C Plugin only supports 32-bit C code.
Hence MX_COMPAT_32 is unnecessary and may cause compilation errors,
so it should be omitted.
- The macro "MATLAB_MEX_FILE" is defined in generated build scripts.
Reactis automatically defines MATLAB_MEX_FILE for all S-Functions,
so it is not necessary to include it in the list of user-specified macro definitions.
|