This demo shows you how to use Target Function Libraries (TFLs) to replace operators and functions in the generated code. The models described below demonstrate the replacement capabilities. With each example model, a separate Target Function Library is provided to illustrate the creation of operator and function replacements using a MATLAB® based API and how to register them with Simulink®.
Using Target Function Libraries enables:
The Target Function Library can:
Please see the following sections from the Real-Time Workshop Embedded Coder documentation for more information on (login required):
You can follow these steps to use a Target Function Library
Please see the Introduction to Target Function Libraries section in the Real-Time Workshop Embedded Coder documentation for more information on the above steps (login required).
This example Target Function Library replaces '+' and '-' for two input, scalar operations on data types that are built-in integers:
You can execute the following command to open the rtwdemo_tfladdsub model that demonstrates these replacements.
open_system('rtwdemo_tfladdsub')
Please see the Example: Mapping Operators to Target-Specific Implementations section in the Real-Time Workshop Embedded Coder documentation for more information on operator replacements (login required).
This example Target Function Library replaces '*' and '/' for two input, scalar operations on data types that are built-in integers:
You can execute the following commands to open the rtwdemo_tflmuldiv model that demonstrates these replacements.
open_system('rtwdemo_tflmuldiv')
This example Target Function Library replaces '+', '-', '*' and '/' for two input, scalar operations on data types that are fixed point. Replacements can be defined as matching:
You can execute the following commands to open the rtwdemo_tflfixpt model that demonstrates these replacements.
open_system('rtwdemo_tflfixpt')
Note: Using fixed point data types in a model requires a Simulink Fixed Point license.
This example Target Function Library replaces '+' and '-' for two input, scalar operations on integer data types when using the emlc command.
To execute the code shown below, you need to first copy the M-file to a temporary directory. You will need write-permission to your system's temporary directory for this operation.
You can execute the following commands to copy the temporary file to your system's temporary directory:
% You can view the M-file by uncommenting the following MATLAB command % edit(fullfile(matlabroot,'toolbox','rtw','rtwdemos',
% 'tfl_demo','addsub_two_int16.m')) look here>. emlcdir = [tempname filesep 'emlcdir']; if ~exist(emlcdir,'dir') mkdir(emlcdir); end emlcsrc = ... fullfile(matlabroot,'toolbox','rtw','rtwdemos',
'tfl_demo','addsub_two_int16.m'); copyfile(emlcsrc,emlcdir,'f'); emlcurdir = pwd; cd(emlcdir);
You can execute the following commands to configure Embedded MATLAB Coder to use the Target Function Library by setting up the configuration parameters for a Real-Time Workshop build and define the operation input type:
addpath(fullfile(matlabroot,'toolbox','rtw','rtwdemos','tfl_demo')); sl_refresh_customizations; rtwConfig = emlcoder.RTWConfig('ert'); rtwConfig.TargetFunctionLibrary = 'Addition & Subtraction Examples'; rtwConfig.GenerateReport = false; rtwConfig.LaunchReport = false; t = int16(2);
If you have a copy of Real-Time Workshop, you can see the TFL table definition file in the tfl_table_addsub.m file.
You can execute the following command to compile the M-file using the configuration parameters that point to the desired Target Function Library and the example input class defined in the previous step as input parameters to the emlc command:
emlc -T rtw -s rtwConfig -c addsub_two_int16 -eg {t, t};
After compiling, you may want to explore the generated source code.
Target Function Libraries support replacement of the following math functions (float versions only):
Additional functions that are supported are:
You can execute the following commands to open the rtwdemo_tflmath model that demonstrates these replacements.
cd(emlcurdir);
open_system('rtwdemo_tflmath')
Please see the Example: Mapping Math Functions to Target-Specific Implementations section in the Real-Time Workshop Embedded Coder documentation for more information on math function replacements (login required).
A viewer is provided for examining and validating tables and their entries. For example, you can execute the following commands to view the tfl_tablemuldiv table:
tfl = tfl_table_muldiv; me = RTW.viewTfl(tfl);
Please see the Examining and Validating Function Replacement Tables section in the Real-Time Workshop Embedded Coder documentation for more information on the Target Function Library Viewer (login required).
Each entry in a Target Function Library table can specify build information such as:
Additionally, the method RTW.copyFileToBuildDir can be used to locally copy the source and header files specified by an entry. Please see the Specifying Build Information for Function Replacements section in the Real-Time Workshop Embedded Coder documentation for more information on specifying compilation information (login required).
Note: The models in this demo are configured for code generation only, as the implementations for the replacement functions are not provided.
Each function implementation name defined by a entry will be reserved as a unique identifier. Other identifiers can be specified with a table on a per-header-file basis. Providing additional reserved identifiers ensures that duplicate symbols and other identifier related compile and link issues do not occur.
Please see the Adding Target Function Library Reserved Identifiers section in the Real-Time Workshop Embedded Coder documentation for more information on specifying reserved identifiers (login required).
Once you are finished using the example models, you can remove the example Target Function Libraries and close the example models with the following commands:
rmpath(fullfile(matlabroot,'toolbox','rtw','rtwdemos','tfl_demo')); sl_refresh_customizations; close_system('rtwdemo_tfladdsub', 0) close_system('rtwdemo_tflmuldiv', 0) close_system('rtwdemo_tflfixpt', 0) close_system('rtwdemo_tflmath', 0) if isa(me, 'DAStudio.Explorer') me.delete; end clear tfl; clear me; clear emlcdir; clear emlcsrc; clear emlcurdir; clear n1; clear rtwConfig; clear t;