Activate the Application Deployment Feature
To execute code generated from a Simulink® model, you must compile, link, and download the code to the supported hardware board. In this section, you specify tools for compiling and linking generated code. You also specify tools for downloading and executing generated code on your hardware board.
A toolchain compiles and links the generated code with other embedded software to produce an executable application that can run on hardware board. The reference target for ARM® Cortex®-M hardware board, provides support for the GNU Tools for ARM Embedded Processors toolchain. If this toolchain is suitable for compiling and linking code for your new target and the hardware board that it supports, you do not need to integrate another toolchain. Instead, you can reuse the GNU Tools for ARM Embedded Processors toolchain. If you need to support different toolchains, integrate new toolchains by following instructions described in Custom Toolchain Registration.
Specify Application Deployment Options
You specify application deployment options for each hardware board that you want the
new target to support. Specifically, you add a Deployer
feature object to your target and map it to the Hardware
object by calling the map
method
of the Target
object. You can map one Deployer
object to multiple Hardware
objects.
Create a
Deployer
object,dep
, and add it to theTarget
object,tgt
, by callingaddNewDeployer
with the name of the deployer, for example,'My New Deployer'
.dep = addNewDeployer(tgt,'My New Deployer');
Do not delete the
Deployer
object from the MATLAB® workspace before you save your new target.Confirm that the deployer is added to your target.
show(tgt);
My ARM Cortex M Board Display Name My ARM Cortex M Board My New Deployer 0
The deployer
'My New Deployer'
is added to the target. However, the0
indicates that the deployer is not used for the hardware board'My ARM Cortex M Board'
.Map the
Deployer
object to theHardware
object,hw
.map(tgt,hw,dep);
Confirm that the deployer is used for the hardware board
'My ARM Cortex M Board'
.show(tgt);
My ARM Cortex M Board Display Name My ARM Cortex M Board My New Deployer 1
The
1
indicates that the deployer'My New Deployer'
is used for the hardware board'My ARM Cortex M Board'
.Create a
Toolchain
object,toolchain
, and add it to theDeployer
object by callingaddNewToolChain
with the name of the toolchain, for example,'GNU Tools for ARM Embedded Processors'
.toolchain = dep.addNewToolchain('GNU Tools for ARM Embedded Processors');
Create a
BuildConfiguration
object,buildConfiguration
, and add it to theToolchain
object by callingaddNewBuildConfiguration
with the name of the build configuration, for example,'My build configuration'
.buildConfiguration = toolchain.addNewBuildConfiguration('My Build Configuration');
Set the properties of the
BuildConfiguration
object as needed for your hardware board. For example, you can set the compiler flags by setting theCompilerFlags
property:buildConfiguration.CompilerFlags = '-mcpu=cortex-m3 -mthumb -mlittle-endian -mthumb-interwork -fsingle-precision-constant';
Similarly, you can set the compiler include paths by setting the
IncludePaths
property to'$(ARM_CORTEX_M_ROOT_DIR)/include'
.buildConfiguration.IncludePaths = '$(ARM_CORTEX_M_ROOT_DIR)/include';
If the source, header, and library files are not located within the
$(ARM_CORTEX_M_ROOT_DIR)/include
folder, assign a cell array of all required paths toIncludePaths
.Note
$(ARM_CORTEX_M_ROOT_DIR)
is a token that stands for the root folder of the target for ARM Cortex-M hardware board and will be resolved by the code generation software.You can also set the
LinkerFlags
,AssemblerFlags
, andDefines
properties, respectively.buildConfiguration.LinkerFlags = '-mcpu=cortex-m3 -mthumb -mlittle-endian -mthumb-interwork -nostartfiles -T yourLinkerFile.ld' buildConfiguration.AssemblerFlags = '-mcpu=cortex-m3 -mthumb -mlittle-endian -mthumb-interwork -fsingle-precision-constant' buildConfiguration.Defines = {'ARM_MATH_CM3=1', 'NULL=0', 'EXIT_FAILURE=1'}
Note
The linker flags, assembler flags, and defines shown above are toolchain specific, you need to modify these flags for your specific toolchain configuration.
Register the
$(ARM_CORTEX_M_ROOT_DIR)
token.dep.Tokens{1} = struct('Name', 'ARM_CORTEX_M_ROOT_DIR', 'Value', 'codertarget.arm_cortex_m.internal.getSpPkgRootDir');
Create a
Loader
object,loader
, and add it to theDeployer
object, by callingaddNewLoader
with the name of the loader, for example,'My Loader'
.loader = dep.addNewLoader('My Loader');
Specify the load command that downloads and executes generated code on hardware board by setting the
LoadCommand
property of theLoader
object. For example, the target for ARM Cortex-M hardware QEMU uses the MATLAB function'codertarget.arm_cortex_m.internal.loadAndRun'
.loader.LoadCommand = 'matlab:codertarget.arm_cortex_m.internal.loadAndRun';
Set the other properties of the
Loader
object as needed. For example, set theLoadCommandArguments
property:loader.LoadCommandArguments ='-f board/stm32f4discovery.cfg';
Note
A target load command needs to be designed to operate with the selected hardware board or family.
The
'codertarget.arm_cortex_m.internal.loadAndRun'
function can be used as a reference to develop and register your own load command.The prefix
matlab:
signifies a MATLAB function. If you omit the prefixmatlab:
, the command is a system command.
Save the target description information to its framework.
saveTarget(tgt);
Test that the application deployment works correctly.
testTarget(tgt,'deployer');
Upon completion of the test, a summary result is displayed. If the test
PASSED
, then you can proceed with adding the next feature. Otherwise, if the test eitherFAILED
or isINCOMPLETE
, a link to the test diagnostic logs is shown below the test summary.
Confirm the Operation of the Application Deployment Feature
Create a blank Simulink model named
test
.On the Apps tab, click Run on Hardware Board. In the Run on Hardware Board dialog box, set Hardware board to the hardware you registered.
In the Hardware tab, click Hardware Settings.
In the Configuration Parameters dialog box, select
Solver
.From the Type list, select
Fixed-step
. From the Solver list, selectauto
.In the Configuration Parameters dialog box, select Code Generation and set System target file to
ert.tlc
.In the Build process pane, under Toolchain Settings, set Toolchain to your toolchain. Click OK.
Open the Simulink Library Browser, and from the Sources library, add a Constant block to your model.
From the Sinks library, add an Outport block to your model. Connect the Constant and the Outport block.
In the Hardware tab, click Build, Deploy & Start > Build. After the build completes, a
test.elf
file is added to your current folder.