
TargetLink is a production code generator that runs with MATLAB and Simulink in the background. It is compared to embedded coder and delivers highly efficient C code for production purposes.
Open a blank Simulink model.
Add a TargetLink Subsystem block.
Add a TargetLink MIL Handler block.
Save the model.
TargetLink Main Dialog
Generates production code for the currently selected code generation unit (CGU).
The build processes (Build SIL/Build PIL) cover code generation, compilation and download for the respective simulation mode (SIL/PIL).
Menu in the Simulink Editor
Generates code for the system or subsystem(s) currently selected.
Context menu of the Data Dictionary Manager
Generates code for the selected CGU or a selected module of a code generation unit.
Function block
Generates code for subsystems that are configured for incremental code generation (with the Enable
Incremental Code Generation checkbox selected on the Incremental page).
API function
Used for automation use cases
commands
tl_generate_code
tl_build_host
tl_build_target
TargetLink Main Dialog
DD
DD-based CGUs that reference the CodegenOptionSet object via their CodegenerationOptionSetRef property
Model-based CGUs in the model hierarchy of the root model whose TargetLink Main Dialog block references the CodegenOptionSet object via its Code Generator option set edit field.
--------------------------
TLPredefinedOptionSet :default (general-purpose Code Generator options)
---------------------------
Modifications
Via the All Options button on the Advanced page of the TargetLink Main Dialog Block
DD
Hook Scripts
When you create a new DD project file, you can choose from predefined system templates.
dsdd_master_basic.dd [System]
Provides the new DD workspace with a basic set of predefined DD objects, such as DD Typedef, DD VariableClass, and DD FunctionClass objects.
dsdd_master_advanced.dd [System]
Provides the new DD workspace with additional predefined objects, such as DD Template objects and DD RTOS objects required for multirate code generation.
dsdd_master_autosar4.dd [System]
Provides the new DD workspace with additional predefined objects, such as DD Typedef and DD Variable objects to be used in AUTOSAR SWCs, and a DD object for specifying AUTOSAR software components and interfaces. Use this template to work with the AUTOSAR 4.x standard.
variable objects that describe a fixed set of properties and can be used globally.
Using a variable object is useful if you often need to reference the same type of variable description or want to keep variable definitions consistent by sharing DD files with other team members of your workgroup.
Variable classes determine how the variables are created in the generated code
each properties combination represents a variable class
Predefined categories
DISP -> Observation
CAL -> Parameter Calibration
MACRO -> C macros (constants)
FCN -> Function Arguments
Explore the storage property of variable classes, selecting default, static, or external to control scope and visibility in blocks, and understand how memory allocation and declaration differ from the definition.
Explore the scope property of variable classes, including local, global, value parameter, reference parameter, function return, and struct component, and how storage relates to scope.
Explore the username property: turn it on to include the variable class name in generated code, or off to exclude it, using tl defines and the disp macro.
Use Cases - Module Separation
Explore typedefs in the data dictionary under pool, featuring base C language types, integers with various widths, float, and structs, and learn to customize or define user defined types.
Learn to customize typedefs by renaming base types in the data dictionary, replacing int16 with a company-specific name like n16 or i16, and ensure generated code reflects the new typedef.
Learn how to use the base type cut property in typedefs to create short type name prefixes for variables, and apply the dollar t macro to generate code-ready identifiers.
Explore base type constraints by embedding scaling, min/max, and offset into a typedef, enabling automatic constraint enforcement for multiple variables with the same data type in TargetLink.
Explicit Struct
Implicit Struct
To specify code decorations, such as declaration statements, pragmas or memory
sections, for code elements.
Learn to implement subsystems as C functions in generated code, and distinguish virtual versus atomic subsystems to ensure identical execution order in simulation and software-in-the-loop contexts.
Explore function interface in generated code by examining subsystem inputs and outputs, and learn how to pass by value or by reference using TargetLink and Simulink imports.
basic example
function with shared/specific parameters
Explore function inlining in TargetLink, comparing automatic and explicit methods, and show how inlining thresholds and function classes control code generation and replace function calls with inline bodies.
Embedding external code as statements or function
direct code implementation from within the block (code inlining). The inserted custom code is included directly in the production code. The inlined code is also used for MIL simulation (Type |)
Use Case:
"Suppose your target ECU is equipped with special hardware features, for example, a unit for PWM generation or a hardware timer that can be accessed by external C functions. To allow offline simulations, your controller model contains a rather complicated implementation of the hardware unit by basic blocks. In this case you do not want TargetLink to generate C code for the model of the hardware function, which is needed only for offline simulations. Instead, you want TargetLink to call your own existing C function in the generated code. You can achieve this behavior by embedding an external function."
Steps:
a subsystem that contains a TargetLink Function block with its step function class property Storage set to
extern.
Demonstrate how production code options shape TargetLink code generation, showing description comments beside variables and how unlisted variable classes lead to block optimization or elimination.
See how the production code option uses name macros to guarantee unique variable names in generated code, with defaults like sa1_n1 and a suffix for duplicates.
Explore scaling in TargetLink by adjusting the least significant bit and offset. Learn how this affects resolution, range, and linear conversion of physical value.
In model-based design, code generation plays a pivotal role in translating graphical or mathematical models into executable code. This approach enables engineers to design and simulate complex systems using graphical tools or mathematical representations, then automatically generate production-quality code directly from these models. Platforms like MATLAB/Simulink facilitate this process by providing a visual environment for system modeling and simulation. Once the model is validated through simulation, code generation tools generate efficient, optimized code tailored to the target platform, whether it's embedded systems, desktop applications, or hardware-in-the-loop (HIL) testing setups. This seamless transition from models to code enhances productivity, reduces development time, and ensures consistency between design and implementation, making model-based design a preferred methodology in various industries, including automotive, aerospace, and industrial automation.
TargetLink is software that generates highly efficient C code for electronic control units (ECUs) and domain controllers straight from MathWorks® Simulink®/Stateflow®, making TargetLink an essential tool for your model-based embedded production software and systems development.
TargetLink comes with software design features as well as built-in code optimization, simulation, and testing functionalities so that you can quickly verify and optimize your generated results. TargetLink interacts smoothly with other tools from dSPACE and with tools from other vendors, which together form the TargetLink Ecosystem.
This is a crash course for beginner in using targetLink.
In this course you will learn many areas of targetlink
Model Preparation
creating targetlink compliant models from scratch
convert existing simulink models into targetlink compliant models
Code Generation
Simulation
MIL
SIL
TargetLink Data Dictionary
TargetLink Data Dictionary Manager
Property Manager