Model Single-Core, Single-Tasking Application
This example shows a model designed and configured for generating embedded system code intended to execute as an application in a single-core, single-tasking target environment. The application algorithm is captured as a multirate top model that is configured for C data interface code generation.
Periodic Multirate Model Set Up for Single-Tasking Execution
Open the example model.
open_system('MultirateSingleTasking');
The model is configured to display color-coded sample times with annotations. To see them, after opening the model, update the diagram by pressing Ctrl+D. To display the legend, press Ctrl+J.
Sample times for Inport blocks
In1_1s
andIn2_2s
are set to 1 and 2 seconds, respectively.To provide clean partitioning of rates, sample times for subsystems
SS1
andSS2
are set to 1.
Relevant Model Configuration Parameter Settings
Type set to
Fixed-step
.Solver set to
discrete (no continuous states)
.Treat each discrete rate as a separate task cleared.
Scheduling
Simulink® simulates the model based on the model configuration. Code generated from the model implements the same execution semantics. Simulink propagates and uses the Inport block sample times to order block execution based on a single-core, single-tasking execution platform.
For this model, the sample time legend shows an implicit rate grouping. Red represents the fastest discrete rate. Green represents the second fastest discrete rate.
The generated code schedules subrates in the model. In this example, the rate for Inport block In2_2s
, the green rate, is a subrate. The generated code properly transfers data between tasks running at the different rates.
Benefits of implicit rate grouping:
Simulink does not impose architectural constraints on the model.
Your execution framework does not require details about underlying function scheduling and data transfers between rates. Therefore, the model interface requirements are simplified. The execution framework uses generated interface code to write input, call the model step function, and read output.
The code generator optimizes code across rates based on single-tasking execution semantics.
Your execution framework can communicate with external devices for reading and writing model input. For example, model external devices by using Simulink S-Function blocks. Generate code for those blocks with the rest of the algorithm.
Generate Code and Report
Open the Embedded Coder app. Then, generate code and a code generation report. The example model generates a report.
Review Generated Code
From the code generation report, review the generated code.
ert_main.c
is an example main program (execution framework) for the model. This code controls model code execution by calling the entry-point functionMultirateSingleTasking_step
. Use this file as a starting point for coding your execution framework.MultirateSingleTasking.c
contains entry points for the code that implements the model algorithm. This file includes the rate scheduling code.MultirateSingleTasking.h
declares model data structures and a public interface to the model entry points and data structures.rtwtypes.h
defines data types, structures, and macros that the generated code requires.
Code Interface
Open and review the Code Interface Report. Use the information in that report to write the interface code for your execution framework:
Include the generated header file by adding directive
#include MultirateSingleTasking.h
.Write input data to the generated code for model Inport blocks.
Call the generated entry-point functions.
Read data from the generated code for model Outport blocks.
Input ports:
rtU.In1_1s
of data typereal_T
with dimension of 1rtU.In2_2s
of data typereal_T
with dimension of 1
Entry-point functions:
Initialization entry-point function,
void MultirateSingleTasking_initialize(void)
. At startup, call this function once.Output and update entry-point (step) function,
void rMultirateSingleTasking_step(void)
. Call this function periodically at the fastest rate in the model. For this model, call the function every second. To achieve real-time execution, attach this function to a timer.
Output ports:
rtY.Out1
of data typereal_T
with dimension of 1rtY.Out2
of data typereal_T
with dimension of 1
More About
Customize Code Organization and Format