Activate the External Mode Feature

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

Here we assume that your hardware is connected to the host computer via a serial communication interface. Similar steps would apply if your hardware is connected to the host computer via an Ethernet interface. Otherwise, you may need to create a custom external mode. For more details about creating custom external mode, 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, ext, from the MATLAB® workspace before you save the target.

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

    show(tgt);
                                   My ARM Cortex M Board
    Display Name                   My ARM Cortex M Board
    My New Deployer                         1
    My Baremetal Scheduler                  1
    My New PIL                      My Serial Interface
    My New External Mode                    0
    

    The output shows that the external mode configuration 'My New External Mode' is added to the target. However, the external mode configuration is not used for the hardware 'My ARM Cortex M Board', and this fact is denoted by showing 0 in the corresponding position for the hardware

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

    map(tgt,hw,ext,'My Serial Interface');
  4. Confirm that the external mode configuration 'My External Mode' is used for the hardware 'My ARM Cortex M Board' and its I/O interface 'My Serial Interface'.

    show(tgt);
                                     My ARM Cortex M Board
    Display Name                     My ARM Cortex M Board
    My New Deployer                           1
    My Baremetal Scheduler                    1
    My New PIL                        My Serial Interface
    My New External Mode              My Serial Interface
    

    The output shows that the external mode configuration 'My New External Mode' is used for the hardware 'My ARM Cortex M Board', and this fact is denoted by showing 'My Serial Interface', that is the I/O interface that it uses, in the corresponding position for the hardware.

  5. View properties of the ExternalMode object, ext.

    ext
             ext =
    
            ExternalMode with properties:
    
                    Name: 'My New External Mode'
             SourceFiles: {}
           PreConnectFcn: ''
                SetupFcn: ''
                CloseFcn: ''
    

    Set the property values as needed for your hardware. For more information, see matlabshared.targetsdk.ExternalMode

  6. Specify the source files to be used 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 serial I/O interface enter

    ext.SourceFiles = {'$(TARGET_ROOT)/src/rtiostream_serial.c', …
    	'$(MATLAB_ROOT)/rtw/c/src/ext_mode/serial/ext_serial_pkt.c', …  
    	'$(MATLAB_ROOT)/rtw/c/src/ext_mode/serial/rtiostream_serial_interface.c', … 
    	'$(MATLAB_ROOT)/rtw/c/src/ext_mode/serial/ext_svr_serial_transport.c'}
    

    Here, '$(TARGET_ROOT)/src/rtiostream_serial.c' represents a source files that implement the serial communication between the host and the target hardware according to instructions given in the Create a Transport Layer for TCP/IP or Serial External Mode Communication (Simulink Coder). The other three files are the files provided by the code generator software for serial communication interface.

    Note

    $(TARGET_ROOT) is a token that stands for the root folder of the target and will be resolved by code generation software.

    $(MATLAB_ROOT) is a token that stands for the root folder of MATLAB and will be resolved by code generation software.

  7. Specify a MATLAB function that executes before 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);'

    Similarly, you can set the properties SetupFcn and CloseFcn. See ExternalMode for more details.

  8. Save the information that describes a target to its framework.

    saveTarget(tgt);
  9. 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. In MATLAB, on the Home tab, select New > Simulink Model. The default name of the model is untitled. Change the name to 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, select the Hardware Implementation tab.

  6. Set Hardware board to the hardware you registered, for example, 'My ARM Cortex M Board'.

  7. Select the External mode group. Confirm that the external mode properties are displayed correctly for your hardware, similar to those depicted in the figure below.

Confirm the Operation of the External Mode

  1. In MATLAB, on the Home tab, select New > Simulink Model. The default name of the model is untitled. Click File > Save As and save your model as test.

  2. Select Simulation > Model Configuration Parameters.

  3. In the Configuration Parameters dialog box, select the Hardware Implementation tab.

  4. Set Hardware board to the hardware you registered, for example, 'My ARM Cortex M Board'.

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

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

  7. In MATLAB, on the Home tab, select Simulink Library. In Simulink library, open Sources and add the Constant block to your model.

  8. In Simulink library, open Math Operations and add the Gain block to your model. Connect the Constant and the Gain block.

  9. In Simulink library, open Sinks and add the Scope block to your model. Connect the Scope and the Gain block.

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

  11. Press Run.

  12. Confirm that the Scope output is 1.