Real-Time Workshop User's Guide | ![]() ![]() |
Tutorial: Creating a Custom Target Configuration
This tutorial walks through the task of creating a skeletal rapid prototyping target. This exercise illustrates several tasks that are usually required when creating a custom target:
In a real-world application, you would incorporate inlined and noninlined device driver S-functions into the model and the generated code. In this tutorial, we inline a simple S-function that multiplies its input by two.
You can use this process as a starting point for your own projects.
This example uses the LCC compiler under Windows. LCC
is distributed with Real-Time Workshop. If you use a different compiler, you can set up LCC temporarily as your default compiler by typing the MATLAB command
mex -setup
A command prompt window will open; follow the prompts and select LCC.
Note On UNIX systems, make sure that you have a C compiler installed. You can then do this exercise substituting appropriate UNIX directory syntax. |
In this example, the code is generated from targetModel.mdl
, a very simple fixed-step model (see Figure 17-1). The resultant program behaves exactly as if it had been built for the generic real-time target.
The S-Function block will use the source code from the timestwo
example. See the Writing S-Functions manual for a complete discussion of this S-function. The Target Language Compiler Reference Guide discusses timestwo.tlc
, the inlined version of timestwo
.
To create the skeletal target system:
.tlc
and .tmf
files. We refer to this directory as d:/work/mytarget
.
d:/work/mytarget
to your MATLAB path.
addpath d:/work/mytarget
d:/work/mytarget
your working directory. Real-Time Workshop writes the output files of the code generation process into a build directory within the working directory.
timestwo
S-function C source code from matlabroot
/toolbox/rtw/rtwdemos/tlctutorial/timestwo/solutions/timestwo.c
tod:/work/mytarget
.
timestwo
MEX-file in d:/work/mytarget
.
mex timestwo.c
fixed-step
and ode4
.
timestwo
. The block is now bound to the timestwo
MEX-file. Click OK.
timestwo
S-function multiplies its input by 2.0.
timestwo
S-Function block, you must have a corresponding TLC file in the working directory. If the Target Language Compiler detects a C-code S-function and a TLC file with the same name in the working directory, it generates inline code from the TLC file. Otherwise, it generates a function call to the external S-function.
To ensure that the build process generates inlined code from the timestwo
block, copy the timestwo
TLC source code from matlabroot
/toolbox/rtw/
rtwdemos/tlctutorial/timestwo/solutions/timestwo.tlc
to
d:/work/mytarget
.
matlabroot
/rtw/c/grt
contains the main program (grt_main.c
) and the system target file (grt.tlc
) for the generic real-time target. Copy grt_main.c
and grt.tlc
to d:/work/mytarget
. Rename them to mytarget_main.c
and mytarget.tlc
.
mytarget.tlc
. The lines to remove are shown below.
%% SYSTLC: Generic Real-Time Target \ %% TMF: grt_default_tmf MAKE: make_rtw EXTMODE: ext_comm %% SYSTLC: Visual C/C++ Project Makefile only for the "grt" target \ %% TMF: grt_msvc.tmf MAKE: make_rtw EXTMODE: ext_comm
The initial comment lines have significance only if you want to add
my_target
to the System Target File Browser. For now you should remove
them.
rtwgensettings
structure in the system target file.
To set the suffix to a more appropriate string, change the line
rtwgensettings.BuildDirSuffix = '_grt_rtw'
to
rtwgensettings.BuildDirSuffix = '_mytarget_rtw'
Your build directory will be named targetModel__mytarget_rtw.
matlabroot
/rtw/c/grt
contains several compiler-specific template makefiles for the generic real-time target. The appropriate template makefile for the LCC compiler is grt_lcc.tmf
. Copy grt_lcc.tmf
to d:/work/mytarget
, and rename it to mytarget.tmf
.
mytarget.tmf
. The SYS_TARGET FILE
parameter must be changed so that the correct file reference is generated in the make
file. Change the line
SYS_TARGET FILE = grt.tlc
to
SYS_TARGET FILE = mytarget.tlc
Also, change the source file
list to include mytarget_main.c
instead of
grt_main.c
.
REQ_SRCS = $(MODEL).c $(MODULES) mytarget_main.c...
Finally, change the line
%.obj : $(MATLAB_ROOT)/rtw/c/grt/%.c
to
%.obj : d:/work/mytarget/%.c
mytarget_main.c
. In an actual application, you would modify mytarget_main.c
to execute your model code under the control of a timer interrupt, and make other changes.
### Created executable: targetModel.exe
### Successful completion of Real-Time Workshop build procedure for model: targetModel
Your working directory will contain the targetModel.exe
file and the build
directory, targetModel_mytarget_rtw.
d:/work/mytarget/targetModel_mytarget_rtw/targetModel.c
and locate the MdlOutputs
function. Observe the inlined code.
/* S-Function Block: <Root>/S-Function (timestwo) */ rtB.S_Function = 2.0 * rtB.Sine_Wave;
Because the working directory contained a TLC file (timestwo.tlc)
with
the same name as the timestwo
S-Function block, the Target Language
Compiler generated inline code instead of a function call to the external C-
code S-function.
![]() | Control Files | Customizing the Build Process | ![]() |