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).
Create and add an
ExternalMode
object,extModeObj
, to yourTarget
object,tgt
, by callingaddNewExternalMode
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.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 hardwareSpecify 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
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 (Simulink Coder). 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.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.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';
Map the
ExternalMode
object,extModeObj
, to the to theHardware
object,hw
, and to its I/O interface name,'Serial'
.map(tgt,hw,extModeObj,ioInterfaceName);
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.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
Save the information that describes a target to its framework.
saveTarget(tgt);
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 filematlabshared.target.tirm46.ConnectivityConfig.m
. Hence, the PIL mode files needs to copied again.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 eitherFAILED
or isINCOMPLETE
, a link to the test diagnostic logs is shown below the test summary.
Confirm That External Mode is Registered for Your Hardware
In MATLAB, on the Home tab, select New > Simulink Model. The default name of the model is
untitled
. Change the name totest
.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
.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 the
Hardware Implementation
tab.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
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 astest
.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
.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 MATLAB, on the Home tab, select Simulink Library. In Simulink® library, open Sources and add the Constant block to your model.
In Simulink library, open Math Operations and add the Gain block to your model. Connect the Constant and the Gain block.
In Simulink library, open Sinks and add the Scope block to your model. Connect the Scope and the Gain block.
In the Hardware tab, click Monitor & Tune to run the simulation in external mode. Confirm that the Scope block output is 1.