Activate the External Mode Feature

External mode is used to validate algorithm code by enabling parameter tuning and signal monitoring. In this section, you add the external mode feature to your target.

Follow these steps if your hardware:

  • Runs the Linux® or VxWorks® operating system

  • Includes a TCP/IP software stack

  • Is connected to the host computer via an Ethernet interface

For more details about creating custom external mode, see Create a Transport Layer for TCP/IP or Serial External Mode Communication (Simulink Coder).

  1. Create and add an ExternalMode object, ext, to your Target object, tgt, by calling addNewExternalMode with the name to be used for this external mode configuration, for example, 'My New External Mode'.

    ext = addNewExternalMode(tgt,'My New External mode');

    Do not delete the ExternalMode object from the MATLAB® workspace before you save the target.

  2. Confirm that the external mode configuration named 'My New External Mode' is added to you target.

    show(tgt);
                                   My ARM Cortex A Board
    Display Name                   My ARM Cortex A Board
    My New Deployer                         1
    My New PIL                     My Ethernet Interface
    Linux                                   1
    My New External Mode                    0
    

    The external mode configuration 'My New External Mode' is added to the target. However, the 0 indicates that the external mode configuration is not used for the hardware 'My ARM Cortex A Board'.

  3. Map the ExternalMode object to the to the Hardware object, hw, and to its I/O interface named 'My Ethernet Interface'.

    map(tgt,hw,ext,'My Ethernet Interface');
  4. Confirm that the external mode configuration is used for the hardware and its I/O interface.

    show(tgt);
                                     My ARM Cortex A Board
    Display Name                     My ARM Cortex A Board
    My New Deployer                           1
    My New PIL                       My Ethernet Interface
    Linux                                     1
    My New External Mode             My Ethernet Interface
    

    The external mode configuration 'My New External Mode' is used for the hardware 'My ARM Cortex A Board' as shown by its I/O interface, 'My Ethernet Interface', appearing in the corresponding position for the hardware.

  5. View properties of the ExternalMode object.

    ext
             ext =
    
            ExternalMode with properties:
    
                    Name: 'My New External Mode'
             SourceFiles: {}
           PreConnectFcn: ''
                SetupFcn: ''
                CloseFcn: ''
    
  6. Set the property values as needed for your hardware. For more information, see matlabshared.targetsdk.ExternalMode

  7. Specify the source files to use when building a model that uses external mode feature by setting the SourceFiles property of the ExternalMode object. For example, to set the source files for external mode using Ethernet I/O interface enter, enter:

    ext.SourceFiles = {'$(MATLAB_ROOT)/rtw/c/src/ext_mode/common/rtiostream_interface.c',
                                     '$(ARM_CORTEX_A_ROOT_DIR)/src/rtiostream_tcpip.c'}
    

    Note

    $(MATLAB_ROOT) is a token that stands for the root folder of MATLAB. The code generation software resolves this token.

  8. Specify that MATLAB executes a function before an external mode connection is established between Simulink® and generated code. Set the PreConnectFcn property of the ExternalMode object to, for example, 'pause(5);'.

    ext.PreConnectFcn = 'pause(5);'
  9. Save the information that describes a target to its framework.

    saveTarget(tgt);
  10. Test that the external mode works correctly.

    testTarget(tgt,'externalmode');

    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 That External Mode is Registered for Your Hardware

  1. Create a blank Simulink model named test.

  2. Select Simulation > Model Configuration Parameters.

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

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

  5. In the Configuration Parameters dialog box, on the Hardware Implementation pane, set Hardware board to the hardware you registered, for example, 'My ARM Cortex A Board'.

  6. In the Target Hardware Resources section, select the External mode group. Confirm that the external mode properties are displayed correctly for your hardware, similar to those shown here.

Confirm the Operation of the External Mode

  1. Create a blank Simulink model named test.

  2. Select Simulation > Model Configuration Parameters.

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

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

  5. In the Configuration Parameters dialog box, on the Hardware Implementation pane, set Hardware board to the hardware you registered, for example, 'My ARM Cortex A Board'.

  6. From the Sources library, add the Constant block to your model.

  7. From the Math Operations library, add the Gain block to your model. Connect the Constant and the Gain block.

  8. From the Sinks library, add the Scope block to your model. Connect the Scope and the Gain block.

  9. In the model toolbar, change the Simulation mode to External. Open the Scope block.

  10. Run the simulation. Confirm that the Scope block output is 1.