Main Content

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.

  1. Create a Deployer object, dep, and add it to the Target object, tgt, by calling addNewDeployer 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.

  2. 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, the 0 indicates that the deployer is not used for the hardware board 'My ARM Cortex M Board'.

  3. Map the Deployer object to the Hardware object, hw.

    map(tgt,hw,dep);
  4. 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'.

  5. Create a Toolchain object, toolchain, and add it to the Deployer object by calling addNewToolChain with the name of the toolchain, for example, 'GNU Tools for ARM Embedded Processors'.

    toolchain = dep.addNewToolchain('GNU Tools for ARM Embedded Processors');
  6. Create a BuildConfiguration object, buildConfiguration, and add it to the Toolchain object by calling addNewBuildConfiguration with the name of the build configuration, for example, 'My build configuration'.

    buildConfiguration = toolchain.addNewBuildConfiguration('My Build Configuration');
  7. Set the properties of the BuildConfiguration object as needed for your hardware board. For example, you can set the compiler flags by setting the CompilerFlags property:

    buildConfiguration.CompilerFlags = '-mcpu=cortex-m3 -mthumb -mlittle-endian -mthumb-interwork -fsingle-precision-constant';
  8. 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 to IncludePaths.

    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.

  9. You can also set the LinkerFlags, AssemblerFlags, and Defines 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.

  10. 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');
  11. Create a Loader object, loader, and add it to the Deployer object, by calling addNewLoader with the name of the loader, for example, 'My Loader'.

    loader = dep.addNewLoader('My Loader');
  12. Specify the load command that downloads and executes generated code on hardware board by setting the LoadCommand property of the Loader 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 the LoadCommandArguments 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 prefix matlab:, the command is a system command.

  13. Save the target description information to its framework.

    saveTarget(tgt);
  14. 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 either FAILED or is INCOMPLETE, a link to the test diagnostic logs is shown below the test summary.

Confirm the Operation of the Application Deployment Feature

  1. Create a blank Simulink model named test.

  2. 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.

  3. In the Hardware tab, click Hardware Settings.

  4. In the Configuration Parameters dialog box, select Solver.

  5. From the Type list, select Fixed-step. From the Solver list, select auto.

  6. In the Configuration Parameters dialog box, select Code Generation and set System target file to ert.tlc.

  7. In the Build process pane, under Toolchain Settings, set Toolchain to your toolchain. Click OK.

  8. Open the Simulink Library Browser, and from the Sources library, add a Constant block to your model.

  9. From the Sinks library, add an Outport block to your model. Connect the Constant and the Outport block.

  10. In the Hardware tab, click Build, Deploy & Start > Build. After the build completes, a test.elf file is added to your current folder.