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, 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.
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
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');
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.
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
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.
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.
Save the information that describes a target to its framework.
saveTarget(tgt);
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.
In MATLAB, on the Home tab,
select New > Simulink Model. The default
name of the model is untitled
. Change the name
to test
.
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, 'My ARM Cortex M
Board'
.
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, select auto
.
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.
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
.
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, 'My ARM Cortex M
Board'
.
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, select auto
.
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.