Notice: We have migrated to GitLab launching 2024-05-01 see here:


Generating code from MATLAB Simulink models can be used to port complex simulations into a target processor architecture. In order to integrate the generated code with other modules running under RTEMS, such as a communications protocol layer, it is necessary to:

1) Install the Real-Time Workshop (RTW) Toolbox for MATLAB;

2) Create the Simulink Model and Prepare it for autocoding;

3) Correctly configure the RTW options and include a *.tcl file;

4) Build any S-Functions of the model;

5) Build the model (generate autocode including makefile);

6) Tune up the makefile with any missing options/libraries/files;

7) Integrate autocoded model in RTEMS using the wrapper.

NOTE: We will assume to have a simulink black box model to be integrated in RTEMS for sake of implementation clarity.

Install RTW

RTW is part of the MathWorks libraries coming with usual MATLAB package versions. During MATLAB installation, choose "manual installation" and tick the following options, besides Simulink and MATLAB itself:

  • MATLAB Real-Time WorkShop;
  • MATLAB Compilers <- This will install LCC, which is a GNU C compiler used to build the C S-Functions.

These libraries can be post-installed using the same procedure.

Prepare the model

After building the Simulink model, it is necessary to specify the I/O interface ports. These will be implemented as arrays of type and dimension specified in ExternalInputs_ModelName and ExternalOutputs_ModelName structs once the code is generated.

This is done by double clicking the inputs and outputs of the simulink model (inPort and outPort blocks respectively) and setting the "Port Dimensions", "Sample Time" and "Data Type" fields in the Signal Attributes tab.


If, for instance, we have a 6 doubles packet to be injected in the model at 10Hz, the following should be set in the inPort Simulink block:

  • Port Dimentions: 6
  • Sample Time: 0.1
  • Data Type: double

At this point, the simulink model is a black box with an inPort configured as above. If we are dealing with a feedback system, where the Simulink block is the feedback control block, it should have an outPort block configured as above but with the correct output type and dimensions.

Configure RTW

In the simulink model, under Tools -> Real-Time Workshop -> Options, we find several important configuration parameters crucial for autocoding.

  • System Target File: This is the autocode instruction file. The autocode process will follow this file in order to know how to create the code. In our black-box example, this file should include a model of the wrapper as following:

%include "model_interface_wrapper.tlc"

The wrapper model should have both Init_%<Model_Name> and Execute_%<Model_Name> function implementation models.

  • Generate Makefile: This option should be ticked in order to generate the makefile template of our simulink block objects library. In "Make Command", one should put make_rtw and the template makefile name can be grt_RTEMS.tmf.
  • Tasking Mode: In the Solver tab, one should choose the tasking mode (Single/Multitasking) according to the wrapper template. For instance, if the Execution function in the wrapper model uses SingleTasking timing functions such as rt_SimGetNextSampleHit and rt_SimUpdateDiscreteTaskSampleHits, as in the following example:

int Execute_%<Model_Name>(void){

real_T tnext; if (isrOverrun++) {


} if (rtmGetErrorStatus(S) != NULL) {


} tnext = rt_SimGetNextSampleHit(); rtsiSetSolverStopTime(rtmGetRTWSolverInfo(S),tnext); %<Model_Name>_MdlOutputs(0); %<Model_Name>_MdlUpdate(0); rt_SimUpdateDiscreteTaskSampleHits(rtmGetNumSampleTimes(S),

rtmGetTimingData(S), rtmGetSampleHitPtr(S), rtmGetTPtr(S));

if (rtmGetSampleTime(S,0) == CONTINUOUS_SAMPLE_TIME) {


} isrOverrun--;


this option should be left as SingleTasking. In practice, if working with slow-rate applications, this option does not impose problems.

  • Step Size: This should correspond to the "continuous" discrete approximation of your model, i.e., the base rate. The solver must be set as "Discrete" with Type "Fixed Step".
  • Add the sources: In case the model include any S-Functions, their sources should be set in the "Custom Code" tab. This information is necessary for generated makefile. Also, in the "Target Options" tab, the matlab root folder must be set. The generated code needs rtw source files present in the matlab root/rtw folder.

Build S-Functions

Before generating the autocode for the model, all S-Functions, if any, must be built first with mex. mex can be setup with "mex -setup" MATLAB command. Building the S-Functions will update the input/output states of the simulink blocks. If the ports are changed and the S-Functions are not built, this will cause segmentations faults at runtime.

Build the Model

Before building the model it is advised to run the "MATLAB Model Advisor Tool". This can be done from the Simulink model window Tools-> Model Advisor. The tool will run a series of tests on your model and warn about potential problems it might have once built.

Then, for this step a build.m script can be made with the following content:


Add the path of this file to the MATLAB path:

addpath pathToBuildFile -begin

Then, to build the model simply run "build" from the MATLAB command line. At this time, if no errors occurred, the code is generated in a folder in the model path.

Tune the generated Makefile

Inside the generated code folder, there is an auto-generated MakeFile. This file can have missing sources or libraries, so it's important to check its correctness. It is then important to add the make process of this library to your main RTEMS application makefile:


make -C generatedCodePath -f all

This will include the compilation of all the autocoded files and creation of the model.a library which also needs to be included at the linking process with "-lModelName" in the makefile.

Integrate the code under the RTEMS Project

This should be very straightforward. As said above, the wrapper includes Init and Exec functions. The Init should be used to initialize the model at an initial state of the application. Then, every time the model should be run to produce results, this is done with the Exec function. This function should run under a RTEMS task activated with the fundamental sample period set in the solver options as said above.

Before executing the model with the Exec function, one has to make sure that its inputs are filled with data. Again as said above, the I/O variables of the model are within structs ExternalInputs_ModelName and ExternalOutputs_ModelName in the ModelName.h header within the generated code.

Summarizing, the model execution task should first fill the ExternalInputs_ModelName variables, then execute the model using the Exec wrapper function and then read its outputs from the ExternalOutputs_ModelName struct variables. The names of the variables within this struc will be equal to the inPort and outPort names in the simulink model.

Last modified on 12/11/14 at 02:42:02 Last modified on 12/11/14 02:42:02