Main Content

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.

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

extModeObj = addNewExternalMode(tgt,'RM46 SCI External mode');

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

2. Confirm that the external mode configuration 'RM46 SCI External mode' is added to you target.

show(tgt);

TIRM46Lx2 Launchpad

Display Name TIRM46Lx2 Launchpad

RM46 TI Deployer 1

FreeRTOS 1

MyPIL Serial

RM46 SCI External mode 0

The output shows that the external mode configuration 'RM46 SCI External mode' is added to the target. However, the external mode configuration is not used for the hardware 'TIRM46Lx2 Launchpad', and this fact is denoted by showing 0 in the corresponding position for the hardware

3. Specify a setup function, SetupFcn, for your hardware.

extModeObj.SetupFcn = 'matlabshared.target.tirm46.internal.extModeSetupFcn(hObj)';

The code for this function is:

function extModeSetupFcn(hCS)

%extModeSetupFcn(hCS) sets up the OnTargetWaitForStart to be 'off'

% Copyright 2016 The MathWorks, Inc.

%we use this hook to also set the wait for start option

set_param(hCS.getModel(), 'OnTargetWaitForStart', 'off');

set_param(hCS.getModel(),'ExtModeTrigDuration', '20');

end

4. Add the location of generic target side source files of the external mode foundation.

extModeSrcFiles = '$(MATLAB_ROOT)/rtw/c/src/ext_mode/serial';

extModeObj.SourceFiles{end+1} = fullfile(extModeSrcFiles,'ext_serial_pkt.c');

extModeObj.SourceFiles{end+1} = fullfile(extModeSrcFiles,'rtiostream_serial_interface.c');

Here, '$(TARGET_ROOT)/src/TI_RM46852LxSerialRtiostream.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. The other three files are the files provided by the code generator software for serial communication interface.

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

5. Add the location of target specific source files.

extModeObj.SourceFiles{end+1} = '$(TARGET_ROOT)/src/TI_RM46852LxSerialRtiostream.c';

extModeObj.SourceFiles{end+1} = '$(TARGET_ROOT)/src/ext_svr_serial_transport.c';

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

6. Update the build configuration object, buildConfigurationObj.

buildConfigurationObj.IncludePaths{end+1} = '$(MATLAB_ROOT)/rtw/c/src/ext_mode/serial';

buildConfigurationObj.SourceFilesToRemove={'$(MATLAB_ROOT)/rtw/c/src/rtiostream/rtiostreamserial/rtiostream_serial.c'};

buildConfigurationObj.Defines{end+1} = 'EXTMODE_DISABLEPRINTF';

7. Map the ExternalMode object, extModeObj, to the to the Hardware object, hw, and to its I/O interface name, 'Serial'.

map(tgt,hw,extModeObj,ioInterfaceName);

8. Confirm that the external mode configuration 'RM46 SCI External mode' is used for the hardware 'TIRM46Lx2 Launchpad' and its I/O interface 'Serial'.

show(tgt);

TIRM46Lx2 Launchpad

Display Name TIRM46Lx2 Launchpad

RM46 TI Deployer 1

FreeRTOS 1

MyPIL Serial

RM46 SCI External mode Serial

The output shows that the external mode configuration 'RM46 SCI External mode' is used for the hardware 'TIRM46Lx2 Launchpad', and this fact is denoted by showing 'Serial', that is the I/O interface that it uses, in the corresponding position for the hardware.

9. View properties of the ExternalMode object, extModeObj.

extModeObj

extModeObj =

ExternalMode with properties:

Name: 'RM46 SCI External mode'

SourceFiles: {}

PreConnectFcn: ''

SetupFcn: ''

CloseFcn: ''

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

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

saveTarget(tgt);

11. Copy the external mode and PIL mode files to the target folder.

copyfile(fullfile(codertarget.arm_cortex_r.internal.getSpPkgRootDir(),...

'arm_cortex_r_examples', 'pil'),...

fullfile(tgt.Folder),'f');

copyfile(fullfile(codertarget.arm_cortex_r.internal.getSpPkgRootDir(),...

'arm_cortex_r_examples', 'externalmode'),...

fullfile(tgt.Folder),'f');

Note: The saveTarget function overwrites the file matlabshared.target.tirm46.ConnectivityConfig.m. Hence, the PIL mode files needs to copied again.

12. 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. 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, for example, TIRM46Lx2 Launchpad.

  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 the Hardware Implementation tab.

  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.

custom_target_based_on_armcortexr_Configuration_Parameters_test.png

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. In the Simulation tab, select Save and save your model as 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, for example, TIRM46Lx2 Launchpad.

  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 MATLAB®, on the Home tab, select Simulink Library. In Simulink® library, open Sources and add the Constant block to your model.

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

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

  9. In the Hardware tab, click Monitor & Tune to run the simulation in external mode. Confirm that the Scope block output is 1.

Related Topics