Main Content

AUTOSAR Property and Map Function Examples

After creating a Simulink® model representation of an AUTOSAR software component, you refine the AUTOSAR configuration. You can refine the AUTOSAR configuration graphically, using the AUTOSAR Dictionary and the Code Mappings editor, or programmatically, using the AUTOSAR property and map functions.

This topic provides examples of using AUTOSAR property and map functions to programmatically refine an AUTOSAR configuration. The examples assume that you have created a Simulink model with an initial AUTOSAR configuration, as described in Component Creation. (To graphically refine an AUTOSAR configuration, see AUTOSAR Component Configuration.)

Here is representative ordering of programmatic configuration tasks.

For a list of AUTOSAR property and map functions, see the Functions list on the AUTOSAR Programmatic Interfaces page.

The examples use a function call format in which a handle to AUTOSAR properties or mapping information is passed as the first call argument:

arProps = autosar.api.getAUTOSARProperties(hModel);
swc = get(arProps,'XmlOptions','ComponentQualifiedName');

The same calls can be coded in a method call format. The formats are interchangeable. For example:

arProps = autosar.api.getAUTOSARProperties(hModel);
swc = arProps.get('XmlOptions','ComponentQualifiedName');

While configuring a model for AUTOSAR code generation, use the following functions to update and validate AUTOSAR model configurations:

  • autosar.api.syncModel — Update Simulink to AUTOSAR mapping of specified model with modifications to Simulink entry-point functions, data transfers, and function callers.

  • autosar.api.validateModel — Validate AUTOSAR properties and Simulink to AUTOSAR mapping of specified model.

The functions are equivalent to using the Update and Validate buttons in the Code Mappings editor.

Configure AUTOSAR Software Component

Configure AUTOSAR Software Component Name and Type

This example:

  1. Opens a model.

  2. Finds AUTOSAR software components.

  3. Loops through components and lists property values.

  4. Modifies the name and kind properties for a component.

% Open model
hModel = 'autosar_swc_expfcns';
addpath(fullfile(matlabroot,'/examples/autosarblockset/main'));
open_system(hModel);

% Use AUTOSAR property functions
arProps = autosar.api.getAUTOSARProperties(hModel);

% Find AUTOSAR software components
aswcPaths = find(arProps,[],'AtomicComponent','PathType','FullyQualified');

% Loop through components and list Name and Kind property values
for ii=1:length(aswcPaths)
    aswcPath = aswcPaths{ii};
    swcName = get(arProps,aswcPath,'Name');
    swcKind = get(arProps,aswcPath,'Kind'); % Application, SensorActuator, etc.
    fprintf('Component %s: Name %s, Kind %s\n',aswcPath,swcName,swcKind);
end
Component /pkg/swc/ASWC: Name ASWC, Kind Application
% Modify component Name and Kind
aswcName = 'mySwc';
aswcKind = 'SensorActuator';
set(arProps,aswcPaths{1},'Name',aswcName);
aswcPaths = find(arProps,[],'AtomicComponent','PathType','FullyQualified');
set(arProps,aswcPaths{1},'Kind',aswcKind);
swcName = get(arProps,aswcPaths{1},'Name');
swcKind = get(arProps,aswcPaths{1},'Kind');
fprintf('Component %s: Name %s, Kind %s\n',aswcPaths{1},swcName,swcKind);
Component /pkg/swc/mySwc: Name mySwc, Kind SensorActuator

Configure AUTOSAR Ports

There are three types of AUTOSAR ports:

  • Require (In)

  • Provide (Out)

  • Combined Provide-Require (InOut)

AUTOSAR ports can reference the following kinds of AUTOSAR communication interfaces:

  • Sender-Receiver

  • Client-Server

  • Mode-Switch

The properties and mapping that you can set for an AUTOSAR port vary according to the type of interface it references. These examples show how to use the AUTOSAR property and map functions to configure AUTOSAR ports for each type of interface.

Configure and Map Sender-Receiver Ports.  This example:

  1. Opens a model.

  2. Finds AUTOSAR sender or receiver ports.

  3. Loops through the ports and lists associated sender-receiver interfaces.

  4. Modifies the associated interface for a port.

  5. Maps a Simulink inport to an AUTOSAR receiver port.

See also Configure AUTOSAR Sender-Receiver Interfaces.

% Open model
hModel = 'autosar_swc_expfcns';
addpath(fullfile(matlabroot,'/examples/autosarblockset/main'));
open_system(hModel);

% Use AUTOSAR property functions
arProps = autosar.api.getAUTOSARProperties(hModel);

% Find AUTOSAR ports - specify DataReceiverPort, DataSenderPort, or DataSenderReceiverPort
arPortType = 'DataReceiverPort';
aswcPath = find(arProps,[],'AtomicComponent','PathType','FullyQualified');
rPorts=find(arProps,aswcPath{1},arPortType,'PathType','FullyQualified')
rPorts =
    {'/pkg/swc/ASWC/RPort'}
% Loop through ports and list their associated interfaces
for ii=1:length(rPorts)
    rPort = rPorts{ii};
    portIf = get(arProps,rPort,'Interface');
    fprintf('Port %s has S-R interface %s\n',rPort,portIf);
end
Port /pkg/swc/ASWC/RPort has S-R interface Interface1
% Set Interface property for AUTOSAR port
rPort = '/pkg/swc/ASWC/RPort';
set(arProps,rPort,'Interface','Interface2')
portIf = get(arProps,rPort,'Interface');
fprintf('Port %s has S-R interface %s\n',rPort,portIf);
Port /pkg/swc/ASWC/RPort has S-R interface Interface2
% Use AUTOSAR map functions
slMap=autosar.api.getSimulinkMapping(hModel);

% Get AUTOSAR mapping info for Simulink inport
[arPortName,arDataElementName,arDataAccessMode]=getInport(slMap,'RPort_DE2')
arPortName =
    'RPort'

arDataElementName =
  0×0 empty char array

arDataAccessMode =
    'ImplicitReceive'
% Map Simulink inport to AUTOSAR port, data element, and data access mode
mapInport(slMap,'RPort_DE2','RPort','DE2','ExplicitReceive')
[arPortName,arDataElementName,arDataAccessMode]=getInport(slMap,'RPort_DE2')
arPortName =
RPort

arDataElementName =
DE2

arDataAccessMode =
ExplicitReceive

Configure Client-Server Ports.  This example:

  1. Opens a model.

  2. Finds AUTOSAR client or server ports.

  3. Loops through the ports and lists associated client-server interfaces.

  4. Modifies the associated interface for a port.

See also Configure AUTOSAR Client-Server Interfaces.

% Open model
hModel = 'mControllerWithInterface_server';
addpath(fullfile(matlabroot,'/help/toolbox/autosar/examples'));
open_system(hModel);

% Use AUTOSAR property functions
arProps = autosar.api.getAUTOSARProperties(hModel);

% Find AUTOSAR ports - specify ServerPort or ClientPort
arPortType = 'ServerPort';
aswcPath = find(arProps,[],'AtomicComponent','PathType','FullyQualified');
sPorts=find(arProps,aswcPath{1},arPortType,'PathType','FullyQualified');

% Loop through ports and list their associated interfaces
for ii=1:length(sPorts)
    sPort = sPorts{ii};
    portIf = get(arProps,sPort,'Interface');
    fprintf('Port %s has C-S interface %s\n',sPort,portIf);
end
Port /pkg/swc/SWC_Controller/sPort has C-S interface CsIf1
% Set Interface property for AUTOSAR port
set(arProps,sPorts{1},'Interface','CsIf2')
portIf = get(arProps,sPorts{1},'Interface');
fprintf('Port %s has C-S interface %s\n',sPorts{1},portIf);
Port /pkg/swc/SWC_Controller/sPort has C-S interface CsIf2

Configure and Map Mode Receiver Ports.  This example:

  1. Opens a model.

  2. Finds AUTOSAR mode receiver ports.

  3. Loops through the ports and lists associated mode-switch interfaces.

  4. Modifies the associated interface for a port.

  5. Maps a Simulink inport to an AUTOSAR mode receiver port.

See also Configure AUTOSAR Mode-Switch Interfaces.

% Add path to model and mode definition files and open model
addpath(fullfile(matlabroot,'/help/toolbox/autosar/examples'));
hModel = 'mAutosarMsConfigAfter';
open_system(hModel);

% Use AUTOSAR property functions
arProps = autosar.api.getAUTOSARProperties(hModel);

% Find AUTOSAR mode receiver ports
arPortType = 'ModeReceiverPort';
aswcPath = find(arProps,[],'AtomicComponent','PathType','FullyQualified');
mrPorts=find(arProps,aswcPath{1},arPortType,'PathType','FullyQualified');

% Loop through ports and list their associated interfaces
for ii=1:length(mrPorts)
    mrPort = mrPorts{ii};
    portIf = get(arProps,mrPort,'Interface');
    fprintf('Port %s has M-S interface %s\n',mrPort,portIf);
end
Port /pkg/swc/ASWC/myMRPort has M-S interface myMsIf
% Set Interface property for AUTOSAR port
set(arProps,mrPorts{1},'Interface','MsIf2')
portIf = get(arProps,mrPort,'Interface');
fprintf('Port %s has M-S interface %s\n',mrPorts{1},portIf);
Port /pkg/swc/ASWC/myMRPort has M-S interface MsIf2
% Use AUTOSAR map functions
slMap=autosar.api.getSimulinkMapping(hModel);

% Get AUTOSAR mapping info for Simulink inport
[arPortName,arDataElementName,arDataAccessMode]=getInport(slMap,'MRPort')
arPortName =
    'myMRPort'

arDataElementName =
  0×0 empty char array

arDataAccessMode =
    'ModeReceive'
% Map Simulink inport to AUTOSAR port, mode group, and data access mode
mapInport(slMap,'MRPort','myMRPort','mdgModes','ModeReceive')
[arPortName,arDataElementName,arDataAccessMode]=getInport(slMap,'MRPort')
arPortName =
    'myMRPort'

arDataElementName =
    'mdgModes'

arDataAccessMode =
    'ModeReceive'

Configure AUTOSAR Runnables and Events

The behavior of an AUTOSAR software component is implemented by one or more runnables. An AUTOSAR runnable is a schedulable entity that is directly or indirectly scheduled by the underlying AUTOSAR operating system. Each runnable is triggered by RTEEvents, events generated by the AUTOSAR run-time environment (RTE). For each runnable, you configure an event to which it responds. Here are examples of AUTOSAR events to which runnables respond.

  • TimingEvent — Triggers a periodic runnable.

  • DataReceivedEvent or DataReceiveErrorEvent — Triggers a runnable with a receiver port that is participating in sender-receiver communication.

  • OperationInvokedEvent — Triggers a runnable with a server port that is participating in client-server communication.

  • ModeSwitchEvent — Triggers a runnable with a mode receiver port that is participating in mode-switch communication.

  • InitEvent (AUTOSAR schema 4.1 or higher) — Triggers a runnable that performs component initialization.

  • ExternalTriggerOccurredEvent — Triggers a runnable with a trigger receiver port that is participating in external trigger event communication.

Configure AUTOSAR TimingEvent for Periodic Runnable.  This example:

  1. Opens a model.

  2. Finds AUTOSAR runnables.

  3. Loops through runnables and lists properties.

  4. Modifies the name and symbol for an AUTOSAR periodic runnable.

  5. Loops through AUTOSAR timing events and lists associated runnables.

  6. Renames an AUTOSAR timing event.

  7. Maps a Simulink entry-point function to an AUTOSAR periodic runnable.

% Open model
hModel = 'autosar_swc_expfcns';
addpath(fullfile(matlabroot,'/examples/autosarblockset/main'));
open_system(hModel);

% Use AUTOSAR property functions
arProps = autosar.api.getAUTOSARProperties(hModel);

% Find AUTOSAR runnables
swc = get(arProps,'XmlOptions','ComponentQualifiedName');
ib = get(arProps,swc,'Behavior');
runnables = find(arProps,ib,'Runnable','PathType','FullyQualified');
runnables{2}
ans =
    '/pkg/swc/ASWC/IB/Runnable1'
% Loop through runnables and list property values
for ii=1:length(runnables)
    runnable = runnables{ii};
    rnName = get(arProps,runnable,'Name');
    rnSymbol = get(arProps,runnable,'symbol');
    rnCBIC = get(arProps,runnable,'canBeInvokedConcurrently');
    fprintf('Runnable %s: symbol %s, canBeInvokedConcurrently %u\n',...
      rnName,rnSymbol,rnCBIC);
end
Runnable Runnable_Init: symbol Runnable_Init, canBeInvokedConcurrently 0
Runnable Runnable1: symbol Runnable1, canBeInvokedConcurrently 0
Runnable Runnable2: symbol Runnable2, canBeInvokedConcurrently 0
Runnable Runnable3: symbol Runnable3, canBeInvokedConcurrently 0
% Modify Runnable1 name and symbol
set(arProps,runnables{2},'Name','myRunnable','symbol','myAlgorithm');
runnables = find(arProps,ib,'Runnable','PathType','FullyQualified');
rnName = get(arProps,runnables{2},'Name');
rnSymbol = get(arProps,runnables{2},'symbol');
rnCBIC = get(arProps,runnables{2},'canBeInvokedConcurrently');
fprintf('Runnable %s: symbol %s, canBeInvokedConcurrently %u\n',...
  rnName,rnSymbol,rnCBIC);
Runnable myRunnable: symbol myAlgorithm, canBeInvokedConcurrently 0
% Loop through AUTOSAR timing events and list runnable associations
events = find(arProps,ib,'TimingEvent','PathType','FullyQualified');
for ii=1:length(events)
    event = events{ii};
    eventStartOn = get(arProps,event,'StartOnEvent');
    fprintf('AUTOSAR event %s triggers %s\n',event,eventStartOn);
end
AUTOSAR event /pkg/swc/ASWC/IB/Event_t_1tic_A triggers ASWC/IB/myRunnable
AUTOSAR event /pkg/swc/ASWC/IB/Event_t_1tic_B triggers ASWC/IB/Runnable2
AUTOSAR event /pkg/swc/ASWC/IB/Event_t_10tic triggers ASWC/IB/Runnable3
% Modify AUTOSAR event name
set(arProps,events{1},'Name','myEvent');
events = find(arProps,ib,'TimingEvent','PathType','FullyQualified');
eventStartOn = get(arProps,events{1},'StartOnEvent');
fprintf('AUTOSAR event %s triggers %s\n',events{1},eventStartOn);
AUTOSAR event /pkg/swc/ASWC/IB/myEvent triggers ASWC/IB/myRunnable
% Use AUTOSAR map functions
slMap=autosar.api.getSimulinkMapping(hModel);

% Map Simulink exported function Runnable1 to renamed AUTOSAR runnable
mapFunction(slMap,'Runnable1','myRunnable');
arRunnableName = getFunction(slMap,'Runnable1')
arRunnableName =
    'myRunnable'

Configure and Map Runnables.  This example:

  1. Opens a model.

  2. Adds AUTOSAR initialization and periodic runnables to the model.

  3. Adds a timing event to the periodic runnable.

  4. Maps Simulink initialization and step functions to the AUTOSAR runnables.

See also Configure Events for Runnable Activation.

% Open model
hModel = 'autosar_swc_counter';
addpath(fullfile(matlabroot,'/examples/autosarblockset/main'));
open_system(hModel);

% Use AUTOSAR property functions
arProps = autosar.api.getAUTOSARProperties(hModel);

% Add AUTOSAR initialization and periodic runnables
initRunnable = 'myInitRunnable';
periodicRunnable = 'myPeriodicRunnable';
swc = get(arProps,'XmlOptions','ComponentQualifiedName')
ib = get(arProps,swc,'Behavior')
add(arProps,ib,'Runnables',initRunnable);
add(arProps,ib,'Runnables',periodicRunnable);

% Add AUTOSAR timing event
eventName = 'myPeriodicEvent';
add(arProps,ib,'Events',eventName,'Category','TimingEvent','Period',1,...
    'StartOnEvent',[ib '/' periodicRunnable]);

% Use AUTOSAR map functions
slMap=autosar.api.getSimulinkMapping(hModel);

% Map AUTOSAR runnables to Simulink initialize and step functions
mapFunction(slMap,'InitializeFunction',initRunnable);
mapFunction(slMap,'StepFunction',periodicRunnable);

% To pass validation, remove redundant initialize and step runnables in AUTOSAR configuration
runnables = get(arProps,ib,'Runnables');
delete(arProps,[ib,'/Runnable_Init']);
delete(arProps,[ib,'/Runnable_Step']);
runnables = get(arProps,ib,'Runnables')
swc =
    '/Company/Powertrain/Components/autosar_swc_counter'

ib =
    'autosar_swc_counter/ASWC_IB'

runnables =
    {'autosar_swc_counter/ASWC_IB/myInitRunnable'}
    {'autosar_swc_counter/ASWC_IB/myPeriodicRunnable'}

Configure Events for Runnable Activation.  This example shows the property function syntax for adding an AUTOSAR TimingEvent, DataReceivedEvent, and DataReceiveErrorEvent to a runnable in a model. For a DataReceivedEvent or DataReceiveErrorEvent, you specify a trigger. The trigger name includes the name of the AUTOSAR receiver port and data element that receives the event, for example, 'RPort.DE1'.

For OperationInvokedEvent syntax, see Configure AUTOSAR Client-Server Interfaces.

For ModeSwitchEvent syntax, see Configure AUTOSAR Mode-Switch Interfaces.

% Open model
hModel = 'autosar_swc_expfcns';
addpath(fullfile(matlabroot,'/examples/autosarblockset/main'));
open_system(hModel);

% Use AUTOSAR property functions
arProps = autosar.api.getAUTOSARProperties(hModel);

% Specify AUTOSAR runnable to which to add event
swc = get(arProps,'XmlOptions','ComponentQualifiedName')
ib = get(arProps,swc,'Behavior')
runnables = get(arProps,ib,'Runnables')
runnable = 'Runnable1';

% Add AUTOSAR timing event
timingEventName = 'myTimingEvent';
add(arProps,ib,'Events',timingEventName,'Category','TimingEvent',...
    'Period',1,'StartOnEvent',[ib '/' runnable]);

% Add AUTOSAR data received event
drEventName = 'myDREvent';
add(arProps,ib,'Events',drEventName,'Category','DataReceivedEvent',...
    'Trigger','RPort.DE1','StartOnEvent',[ib '/' runnable]);

% Add AUTOSAR data receive error event
dreEventName = 'myDREEvent';
add(arProps,ib,'Events',dreEventName,'Category','DataReceiveErrorEvent',...
    'Trigger','RPort.DE1','StartOnEvent',[ib '/' runnable]);

% To pass validation, remove redundant timing event in AUTOSAR configuration
events = get(arProps,ib,'Events');
delete(arProps,[ib,'/Event_t_1tic_A'])
events = get(arProps,ib,'Events')
swc =
    '/pkg/swc/ASWC'

ib =
    'ASWC/IB'

runnables =
  1×4 cell array
    {'ASWC/IB/Runnable_Init'}    {'ASWC/IB/Runnable1'}
    {'ASWC/IB/Runnable2'}    {'ASWC/IB/Runnable3'}

events =
  1×5 cell array
    {'ASWC/IB/Event_t_1tic_B'}    {'ASWC/IB/Event_t_10tic'}    {'ASWC/IB/myTimingEvent'}
    {'ASWC/IB/myDREvent'}    {'ASWC/IB/myDREEvent'}

Gather Information for AUTOSAR Custom Scheduler Script.  This example:

  1. Loops through events and runnables in an open model.

  2. For each event or runnable, extracts information to use with a custom scheduler.

hModel specifies the name of an open AUTOSAR model.

% Example of how to extract timing information for runnables
% to prepare for hooking up a custom scheduler

% Use AUTOSAR property functions
arProps = autosar.api.getAUTOSARProperties(hModel);

swc = get(arProps,'XmlOptions','ComponentQualifiedName');

% Get AUTOSAR internal behavior
ib = get(arProps,swc,'Behavior');

% Get AUTOSAR events and runnables
events = get(arProps,ib,'Events');
runnables = get(arProps,ib,'Runnables');

% Loop through events
for ii=1:length(events)
    event = events{ii};
    category = get(arProps,event,'Category');
    
    switch category
      case 'TimingEvent'
        runnablePath = get(arProps,event,'StartOnEvent');
        period = get(arProps,event,'Period');
        eventName = get(arProps,event,'Name');
        runnableName = get(arProps,runnablePath,'Name');
        fprintf('Event %s triggers runnable %s with period %g\n',eventName,runnableName,period);
      otherwise
        % Not interested in other events
    end
end

% Loop through runnables
for ii=1:length(runnables)
    runnable = runnables{ii};
    runnableName = get(arProps,runnable,'Name');
    runnableSymbol = get(arProps,runnable,'symbol');
    fprintf('Runnable %s has symbol %s\n',runnableName,runnableSymbol);
end

Running the example code on the example model autosar_swc_expfcns generates the following output:

Event Event_t_1tic_A triggers runnable Runnable1 with period 1
Event Event_t_1tic_B triggers runnable Runnable2 with period 1
Event Event_t_10tic triggers runnable Runnable3 with period 10
Runnable Runnable_Init has symbol Runnable_Init
Runnable Runnable1 has symbol Runnable1
Runnable Runnable2 has symbol Runnable2
Runnable Runnable3 has symbol Runnable3

Running the example code on the example model matlabroot/help/toolbox/autosar/examples/mMultitasking_4rates.slx generates the following output:

Event Event_Runnable_Step triggers runnable Runnable_Step with period 1
Event Event_Runnable_Step1 triggers runnable Runnable_Step1 with period 2
Event Event_Runnable_Step2 triggers runnable Runnable_Step2 with period 4
Event Event_Runnable_Step3 triggers runnable Runnable_Step3 with period 8
Runnable Runnable_Init has symbol Runnable_Init
Runnable Runnable_Step has symbol Runnable_Step
Runnable Runnable_Step1 has symbol Runnable_Step1
Runnable Runnable_Step2 has symbol Runnable_Step2
Runnable Runnable_Step3 has symbol Runnable_Step3

Configure AUTOSAR Inter-Runnable Variables

In an AUTOSAR software component with multiple runnables, inter-runnable variables (IRVs) are used to communicate data between runnables. In Simulink, you model IRVs using data transfer lines that connect subsystems. In an application with multiple rates, the data transfer lines might include Rate Transition blocks to handle transitions between differing rates.

These examples show how to use the AUTOSAR property and map functions to configure AUTOSAR IRVs without or with rate transitions.

Configure Inter-Runnable Variable for Data Transfer Line.  This example:

  1. Opens a model.

  2. Adds an AUTOSAR inter-runnable variable (IRV) to the model.

  3. Maps a Simulink data transfer to the IRV.

% Open model
hModel = 'autosar_swc_expfcns';
addpath(fullfile(matlabroot,'/examples/autosarblockset/main'));
open_system(hModel);

% Use AUTOSAR property functions
arProps = autosar.api.getAUTOSARProperties(hModel);

% Get AUTOSAR internal behavior and add IRV myIrv with SwCalibrationAccess ReadWrite
irvName = 'myIrv';
swCalibValue = 'ReadWrite';
swc = get(arProps,'XmlOptions','ComponentQualifiedName')
ib = get(arProps,swc,'Behavior')
irvs = get(arProps,ib,'IRV')
add(arProps,ib,'IRV',irvName,'SwCalibrationAccess',swCalibValue);
irvs = get(arProps,ib,'IRV');

% Use AUTOSAR map functions
slMap=autosar.api.getSimulinkMapping(hModel);

% Map Simulink signal irv1 to AUTOSAR IRV myIrv with access mode Explicit
irvAccess = 'Explicit';
[arIrvName,arDataAccessMode] = getDataTransfer(slMap,'irv1');
mapDataTransfer(slMap,'irv1',irvName,irvAccess);
[arIrvName,arDataAccessMode] = getDataTransfer(slMap,'irv1')

% To pass validation, remove redundant IRV in AUTOSAR configuration
irvs = get(arProps,ib,'IRV');
delete(arProps,[ib,'/IRV1'])
irvs = get(arProps,ib,'IRV')
swc =
    '/pkg/swc/ASWC'

ib =
    'ASWC/IB'

irvs =
    {'ASWC/IB/IRV1'}    {'ASWC/IB/IRV2'}
    {'ASWC/IB/IRV3'}    {'ASWC/IB/IRV4'}

arIrvName =
    'myIrv'

arDataAccessMode =
    'Explicit'

irvs =
    {'ASWC/IB/IRV2'}    {'ASWC/IB/IRV3'}
    {'ASWC/IB/IRV4'}    {'ASWC/IB/myIrv'}

Configure Inter-Runnable Variable for Data Transfer with Rate Transition.  This example:

  1. Opens a model with multiple rates.

  2. Adds an AUTOSAR inter-runnable variable (IRV) to the model.

  3. Maps a Simulink Rate Transition block to the IRV.

% Open model
hModel = 'mMultitasking_4rates';
addpath(fullfile(matlabroot,'/help/toolbox/autosar/examples'));
open_system(hModel);

% Use AUTOSAR property functions
arProps = autosar.api.getAUTOSARProperties(hModel);

% Get AUTOSAR internal behavior and add IRV myIrv with SwCalibrationAccess ReadWrite
irvName = 'myIrv';
swCalibValue = 'ReadWrite';
swc = get(arProps,'XmlOptions','ComponentQualifiedName')
ib = get(arProps,swc,'Behavior')
irvs = get(arProps,ib,'IRV')
add(arProps,ib,'IRV',irvName,'SwCalibrationAccess',swCalibValue);
irvs = get(arProps,ib,'IRV');

% Use AUTOSAR map functions
slMap=autosar.api.getSimulinkMapping(hModel);

% Map Simulink RT block RateTransition2 to AUTOSAR IRV myIrv with access mode Explicit
irvAccess = 'Explicit';
[arIrvName,arDataAccessMode] = getDataTransfer(slMap,'mMultitasking_4rates/RateTransition2');
mapDataTransfer(slMap,'mMultitasking_4rates/RateTransition2',irvName,irvAccess);
[arIrvName,arDataAccessMode] = getDataTransfer(slMap,'mMultitasking_4rates/RateTransition2')

% To pass validation, remove redundant IRV in AUTOSAR configuration
irvs = get(arProps,ib,'IRV');
delete(arProps,[ib,'/IRV3'])
irvs = get(arProps,ib,'IRV')
swc =
    '/mMultitasking_4rates_pkg/mMultitasking_4rates_swc/mMultitasking_4rates'

ib =
    'mMultitasking_4rates/Behavior'

irvs =
    {'mMultitasking_4rates/Behavior/IRV1'}    {'mMultitasking_4rates/Behavior/IRV2'}
    {'mMultitasking_4rates/Behavior/IRV3'}

arIrvName =
    'myIrv'

arDataAccessMode =
    'Explicit'

irvs =
    {'mMultitasking_4rates/Behavior/IRV1'}    {'mMultitasking_4rates/Behavior/IRV2'}
    {'mMultitasking_4rates/Behavior/myIrv'}

Configure AUTOSAR Interfaces

AUTOSAR software components can use ports and interfaces to implement the following forms of communication:

  • Sender-receiver (S-R)

  • Client-server (C-S)

  • Mode-switch (M-S)

  • Nonvolatile (NV) data

These examples show how to use AUTOSAR property and map functions to configure AUTOSAR ports, interfaces, and related elements for S-R, C-S, and M-S communication. The techniques shown for configuring S-R ports and interfaces also broadly apply to NV communication.

Configure AUTOSAR Sender-Receiver Interfaces

Configure and Map Sender-Receiver Interface.  This example:

  1. Opens a model.

  2. Adds an AUTOSAR sender-receiver interface to the model.

  3. Adds data elements.

  4. Creates sender and receiver ports.

  5. Maps Simulink inports and outports to AUTOSAR receiver and sender ports.

See also Configure AUTOSAR Runnables and Events.

% Open model
hModel = 'autosar_swc_expfcns';
addpath(fullfile(matlabroot,'/examples/autosarblockset/main'));
open_system(hModel);

% Use AUTOSAR property functions
arProps = autosar.api.getAUTOSARProperties(hModel);

% Add AUTOSAR S-R interface
ifName = 'mySrIf';
ifPkg = get(arProps,'XmlOptions','InterfacePackage')
addPackageableElement(arProps,'SenderReceiverInterface',ifPkg,ifName,'IsService',false);
ifPaths=find(arProps,[],'SenderReceiverInterface','PathType','FullyQualified')

% Add AUTOSAR S-R data elements with ReadWrite calibration access
de1 = 'myDE1';
de2 = 'myDE2';
swCalibValue= 'ReadWrite';
add(arProps, [ifPkg '/' ifName],'DataElements',de1,'SwCalibrationAccess',swCalibValue);
add(arProps, [ifPkg '/' ifName],'DataElements',de2,'SwCalibrationAccess',swCalibValue);

% Add AUTOSAR receiver and sender ports with S-R interface name
rPortName = 'myRPort';
pPortName = 'myPPort';
aswcPath = find(arProps,[],'AtomicComponent','PathType','FullyQualified');
add(arProps,aswcPath{1},'ReceiverPorts',rPortName,'Interface',ifName);
add(arProps,aswcPath{1},'SenderPorts',pPortName,'Interface',ifName);

% Use AUTOSAR map functions
slMap=autosar.api.getSimulinkMapping(hModel);

% Map Simulink inport RPort_DE2 to AUTOSAR receiver port myRPort and data element myDE2
rDataAccessMode = 'ImplicitReceive';
[arPortName,arDataElementName,arDataAccessMode]=getInport(slMap,'RPort_DE2')
mapInport(slMap,'RPort_DE2',rPortName,de2,rDataAccessMode);
[arPortName,arDataElementName,arDataAccessMode]=getInport(slMap,'RPort_DE2')

% Map Simulink outport PPort_DE1 to AUTOSAR sender port myPPort and data element myDE1
sDataAccessMode = 'ImplicitSend';
[arPortName,arDataElementName,arDataAccessMode]=getOutport(slMap,'PPort_DE1')
mapOutport(slMap,'PPort_DE1',pPortName,de1,sDataAccessMode);
[arPortName,arDataElementName,arDataAccessMode]=getOutport(slMap,'PPort_DE1')
ifPkg =
    '/pkg/if'

ifPaths =
    {'/pkg/if/Interface1'}    {'/pkg/if/Interface2'}    {'/pkg/if/mySrIf'}

arPortName =
    'RPort'
arDataElementName =
    'DE2'
arDataAccessMode =
    'ImplicitReceive'

arPortName =
    'myRPort'
arDataElementName =
    'myDE2'
arDataAccessMode =
    'ImplicitReceive'

arPortName =
    'PPort'
arDataElementName =
    'DE1'
arDataAccessMode =
    'ImplicitSend'

arPortName =
    'myPPort'
arDataElementName =
    'myDE1'
arDataAccessMode =
    'ImplicitSend'

Configure Sender-Receiver Data Element Properties.  This example loops through AUTOSAR sender-receiver (S-R) interfaces and data elements to configure calibration properties for S-R data elements.

% Open model
hModel = 'autosar_swc_expfcns';
addpath(fullfile(matlabroot,'/examples/autosarblockset/main'));
open_system(hModel);

% Use AUTOSAR property functions
arProps = autosar.api.getAUTOSARProperties(hModel);

% Configure SwCalibrationAccess for AUTOSAR data elements in S-R interfaces
srIfs = find(arProps,[],'SenderReceiverInterface','PathType','FullyQualified')

% Loop through S-R interfaces and get data elements
for i=1:length(srIfs)
    srIf = srIfs{i};
    dataElements = get(arProps,srIf,'DataElements','PathType','FullyQualified')

% Loop through data elements for each S-R interface and set SwCalibrationAccess
    swCalibValue = 'ReadWrite';
    for ii=1:length(dataElements)
        dataElement = dataElements{ii};
        set(arProps,dataElement,'SwCalibrationAccess',swCalibValue)
        get(arProps,dataElement,'SwCalibrationAccess');
    end
end
srIfs =
    {'/pkg/if/Interface1'}    {'/pkg/if/Interface2'}

dataElements =
    {'/pkg/if/Interface1/DE1'}    {'/pkg/if/Interface1/DE2'}

dataElements =
    {'/pkg/if/Interface2/DE1'}    {'/pkg/if/Interface2/DE2'}
    {'/pkg/if/Interface2/DE3'}    {'/pkg/if/Interface2/DE4'}

Configure AUTOSAR Client-Server Interfaces

Configure Server Properties.  This example:

  1. Opens a model.

  2. Adds an AUTOSAR client-server interface to the model.

  3. Adds an operation.

  4. Creates a server port.

  5. Creates a server runnable.

  6. Maps a Simulink function to the AUTOSAR server runnable.

% Open model
hModel = 'mControllerWithInterface_server';
addpath(fullfile(matlabroot,'/help/toolbox/autosar/examples'));
open_system(hModel);

% Use AUTOSAR property functions
arProps = autosar.api.getAUTOSARProperties(hModel);

% Add AUTOSAR C-S interface
ifName = 'myCsIf';
ifPkg = get(arProps,'XmlOptions','InterfacePackage')
addPackageableElement(arProps,'ClientServerInterface',ifPkg,ifName,'IsService',false);
ifPaths=find(arProps,[],'ClientServerInterface','PathType','FullyQualified');

% Add AUTOSAR operation to C-S interface
csOp = 'readData';
add(arProps, [ifPkg '/' ifName],'Operations',csOp);

% Add AUTOSAR arguments to C-S operation with Direction and SwCalibrationAccess properties
args = {'Op','In'; 'Data','Out'; 'ERR','Out'; 'NegCode','Out'}
swCalibValue = 'ReadOnly';
for i=1:length(args)
    add(arProps,[ifPkg '/' ifName '/' csOp],'Arguments',args{i,1},'Direction',args{i,2},...
        'SwCalibrationAccess',swCalibValue);
end
get(arProps,[ifPkg '/' ifName '/' csOp],'Arguments')

% Add AUTOSAR server port with C-S interface name
sPortName = 'mySPort';
aswcPath = find(arProps,[],'AtomicComponent','PathType','FullyQualified');
add(arProps,aswcPath{1},'ServerPorts',sPortName,'Interface',ifName);

% Add AUTOSAR server runnable with symbol name that matches Simulink function name
serverRunnable = 'Runnable_myReadData';
serverRunnableSymbol = 'readData';
swc = get(arProps,'XmlOptions','ComponentQualifiedName')
ib = get(arProps,swc,'Behavior')
runnables = get(arProps,ib,'Runnables');
% To avoid symbol conflict, remove existing runnable with symbol name readData
delete(arProps,'SWC_Controller/ControllerWithInterface_ar/Runnable_readData')
add(arProps,ib,'Runnables',serverRunnable,'symbol',serverRunnableSymbol);
runnables = get(arProps,ib,'Runnables');

% Add AUTOSAR operation invoked event
oiEventName = 'Event_myReadData';
add(arProps,ib,'Events',oiEventName,'Category','OperationInvokedEvent',...
    'Trigger','mySPort.readData','StartOnEvent',[ib '/' serverRunnable]);

% Use AUTOSAR map functions
slMap=autosar.api.getSimulinkMapping(hModel);

% Map Simulink function readData to AUTOSAR runnable Runnable_myReadData
mapFunction(slMap,'readData',serverRunnable);
arRunnableName=getFunction(slMap,'readData')
ifPkg =
    '/ControllerWithInterface_ar_pkg/ControllerWithInterface_ar_if'

args =
    {'Op'     }    {'In' }
    {'Data'   }    {'Out'}
    {'ERR'    }    {'Out'}
    {'NegCode'}    {'Out'}

ans =
    {'myCsIf/readData/Op'}    {'myCsIf/readData/Data'}
    {'myCsIf/readData/ERR'}    {'myCsIf/readData/NegCode'}

swc =
    '/pkg/swc/SWC_Controller'

ib =
    'SWC_Controller/ControllerWithInterface_ar'

arRunnableName =
    'Runnable_myReadData'

Configure Client Properties.  This example:

  1. Opens a model.

  2. Adds an AUTOSAR client-server interface to the model.

  3. Adds an operation.

  4. Creates a client port.

  5. Maps a Simulink function caller to the AUTOSAR client port and operation.

% Open model
hModel = 'mControllerWithInterface_client';
addpath(fullfile(matlabroot,'/help/toolbox/autosar/examples'));
open_system(hModel);

% Use AUTOSAR property functions
arProps = autosar.api.getAUTOSARProperties(hModel);

% Add AUTOSAR C-S interface
ifName = 'myCsIf';
ifPkg = get(arProps,'XmlOptions','InterfacePackage');
addPackageableElement(arProps,'ClientServerInterface',ifPkg,ifName,'IsService',false);
ifPaths=find(arProps,[],'ClientServerInterface','PathType','FullyQualified')

% Add AUTOSAR operation to C-S interface
csOp = 'readData';
add(arProps, [ifPkg '/' ifName],'Operations',csOp);

% Add AUTOSAR arguments to C-S operation with Direction and SwCalibrationAccess properties
args = {'Op','In'; 'Data','Out'; 'ERR','Out'; 'NegCode','Out'}
swCalibValue = 'ReadOnly';
for i=1:length(args)
    add(arProps,[ifPkg '/' ifName '/' csOp],'Arguments',args{i,1},'Direction',args{i,2},...
        'SwCalibrationAccess',swCalibValue);
end
get(arProps,[ifPkg '/' ifName '/' csOp],'Arguments')

% Add AUTOSAR client port with C-S interface name
cPortName = 'myCPort';
aswcPath = find(arProps,[],'AtomicComponent','PathType','FullyQualified');
add(arProps,aswcPath{1},'ClientPorts',cPortName,'Interface',ifName);

% Use AUTOSAR map functions
slMap=autosar.api.getSimulinkMapping(hModel);

% Map Simulink function caller readData to AUTOSAR client port and operation
[arPort,arOp] = getFunctionCaller(slMap,'readData');
mapFunctionCaller(slMap,'readData',cPortName,csOp);
[arPort,arOp] = getFunctionCaller(slMap,'readData')
ifPaths =
    {'/pkg/if/csInterface'}    {'/pkg/if/myCsIf'}

args =
    {'Op'     }    {'In' }
    {'Data'   }    {'Out'}
    {'ERR'    }    {'Out'}
    {'NegCode'}    {'Out'}

ans =
    {'myCsIf/readData/Op'}    {'myCsIf/readData/Data'}
    {'myCsIf/readData/ERR'}    {'myCsIf/readData/NegCode'}

arPort =
    'myCPort'

arOp =
    'readData'

Configure AUTOSAR Mode-Switch Interfaces

This example:

  1. Opens a model.

  2. Declares an AUTOSAR mode declaration group.

  3. Adds a mode-switch interface to the model.

  4. Adds a mode receiver port.

  5. Adds a ModeSwitchEvent to a runnable.

  6. Maps a Simulink inport to the AUTOSAR mode receiver port and mode group.

% Add path to model and mode definition files and open model
addpath(fullfile(matlabroot,'/help/toolbox/autosar/examples'));
hModel = 'mAutosarMsConfig';
open_system(hModel);

% Use AUTOSAR property functions
arProps = autosar.api.getAUTOSARProperties(hModel);

% File mdgModes.m declares AUTOSAR mode declaration group mdgModes for use with the M-S interface.
% See matlabroot/help/toolbox/autosar/examples/mdgModes.m, which must be on the MATLAB path.
% The enumerated mode values are:
%    STARTUP(0)
%    RUN(1)
%    SHUTDOWN(2)
% Separate code, below, defines mode declaration group information for XML export.

% Apply data type mdgModes to Simulink inport MRPort
set_param([hModel,'/MRPort'],'OutDataTypeStr','Enum: mdgModes')
get_param([hModel,'/MRPort'],'OutDataTypeStr');
% Apply data type mdgModes and value STARTUP to Runnable1_subsystem/Enumerated Constant
set_param([hModel,'/Runnable1_subsystem/Enumerated Constant'],'OutDataTypeStr','Enum: mdgModes')
set_param([hModel,'/Runnable1_subsystem/Enumerated Constant'],'Value','mdgModes.STARTUP')

% Add AUTOSAR M-S interface and set its ModeGroup to mdgModes
ifName = 'myMsIf';
modeGroup = 'mdgModes';
ifPkg = get(arProps,'XmlOptions','InterfacePackage');
addPackageableElement(arProps,'ModeSwitchInterface',ifPkg,ifName,'IsService',true);
add(arProps,[ifPkg '/' ifName],'ModeGroup',modeGroup)
ifPaths=find(arProps,[],'ModeSwitchInterface','PathType','FullyQualified')

% Add AUTOSAR mode-receiver port with M-S interface name
mrPortName = 'myMRPort';
aswcPath = find(arProps,[],'AtomicComponent','PathType','FullyQualified');
add(arProps,aswcPath{1},'ModeReceiverPorts',mrPortName,'Interface',ifName);

% Define AUTOSAR ModeSwitchEvent for runnable
msRunnable = 'Runnable1';
msEventName = 'myMSEvent';
swc = get(arProps,'XmlOptions','ComponentQualifiedName');
ib = get(arProps,swc,'Behavior');
runnables = get(arProps,ib,'Runnables')
add(arProps,ib,'Events',msEventName,'Category','ModeSwitchEvent',...
    'Activation', 'OnTransition', ...
    'StartOnEvent', [ib '/' msRunnable]);
% Separate code, below, sets ModeSwitchEvent port and trigger values.

% To pass validation, remove redundant timing event in AUTOSAR configuration
events = get(arProps,ib,'Events');
delete(arProps,[ib,'/Event_t_1tic_A'])
events = get(arProps,ib,'Events')

% Export mode declaration group information to AUTOSAR data type package in XML
mdgPkg = get(arProps,'XmlOptions','DataTypePackage');
mdgPath = [mdgPkg '/' modeGroup]
initMode = [mdgPath '/STARTUP']
addPackageableElement(arProps,'ModeDeclarationGroup',mdgPkg,modeGroup,'OnTransitionValue',100)
% Add modes to ModeDeclarationGroup and set InitialMode
add(arProps,mdgPath,'Mode','STARTUP','Value',0)
add(arProps,mdgPath,'Mode','RUN','Value',1)
add(arProps,mdgPath,'Mode','SHUTDOWN','Value',2)
set(arProps,mdgPath,'InitialMode',initMode)
% Set ModeGroup for M-S interface
set(arProps,[ifPkg '/' ifName '/' modeGroup],'ModeGroup',mdgPath)

% Set port and trigger for AUTOSAR ModeSwitchEvent
expTrigger = {[mrPortName '.STARTUP'], [mrPortName '.SHUTDOWN']}
set(arProps,[ib '/' msEventName],'Trigger',expTrigger)

% Use AUTOSAR map functions
slMap=autosar.api.getSimulinkMapping(hModel);

% Map Simulink inport MRPort to AUTOSAR mode receiver port myMRPort and mode group mdgModes
msDataAccessMode = 'ModeReceive';
[arPortName,arDataElementName,arDataAccessMode]=getInport(slMap,'MRPort');
mapInport(slMap,'MRPort',mrPortName,modeGroup,msDataAccessMode);
[arPortName,arDataElementName,arDataAccessMode]=getInport(slMap,'MRPort')

% To pass validation, set inport Runnable1 sample time to -1 (inherited)
set_param([hModel,'/Runnable1'],'SampleTime','-1')
ifPaths =
    {'/pkg/if/myMsIf'}

runnables =
    {'ASWC/Behavior/Runnable_Init'}    {'ASWC/Behavior/Runnable1'}
    {'ASWC/Behavior/Runnable2'}    {'ASWC/Behavior/Runnable3'}

events =
    {'ASWC/Behavior/Event_t_1tic_B'}    {'ASWC/Behavior/Event_t_10tic'}
    {'ASWC/Behavior/myMSEvent'}

mdgPath =
    '/pkg/dt/mdgModes'

initMode =
    '/pkg/dt/mdgModes/STARTUP'

expTrigger =
    {'myMRPort.STARTUP'}    {'myMRPort.SHUTDOWN'}

arPortName =
    'myMRPort'
arDataElementName =
    'mdgModes'
arDataAccessMode =
    'ModeReceive'

Configure AUTOSAR XML Export

Configure XML Export Options

This example configures AUTOSAR XML export parameter Exported XML file packaging (ArxmlFilePackaging).

To configure AUTOSAR package paths, see Configure AUTOSAR Package Paths.

% Open model
hModel = 'autosar_swc_counter';
addpath(fullfile(matlabroot,'/examples/autosarblockset/main'));
open_system(hModel);

% Use AUTOSAR property functions
arProps = autosar.api.getAUTOSARProperties(hModel);

% Set exported AUTOSAR XML file packaging to Single file
get(arProps,'XmlOptions','ArxmlFilePackaging')
set(arProps,'XmlOptions','ArxmlFilePackaging','SingleFile');
get(arProps,'XmlOptions','ArxmlFilePackaging')
ans =
    'Modular'

ans =
    'SingleFile'

Configure AUTOSAR Package Paths

This example configures an AUTOSAR package path for XML export. For other AUTOSAR package path property names, see Configure AUTOSAR Packages and Paths.

To configure other XML export options, see Configure XML Export Options.

% Open model
hModel = 'autosar_swc_counter';
addpath(fullfile(matlabroot,'/examples/autosarblockset/main'));
open_system(hModel);

% Use AUTOSAR property functions
arProps = autosar.api.getAUTOSARProperties(hModel);

% Specify AUTOSAR application data type package path for XML export
get(arProps,'XmlOptions','ApplicationDataTypePackage')
set(arProps,'XmlOptions','ApplicationDataTypePackage','/Company/Powertrain/DataTypes/ADTs');
get(arProps,'XmlOptions','ApplicationDataTypePackage')
ans =
    '/Company/Powertrain/DataTypes/ApplDataTypes'

ans =
    '/Company/Powertrain/DataTypes/ADTs'

See Also

|

Related Examples

More About