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:
Opens a model.
Finds AUTOSAR software components.
Loops through components and lists property values.
Modifies the name and kind properties for a component.
% Open model hModel = 'autosar_swc_expfcns'; openExample(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:
Opens a model.
Finds AUTOSAR sender or receiver ports.
Loops through the ports and lists associated sender-receiver interfaces.
Modifies the associated interface for a port.
Maps a Simulink inport to an AUTOSAR receiver port.
See also Configure AUTOSAR Sender-Receiver Interfaces.
% Open model hModel = 'autosar_swc_expfcns'; openExample(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 entry point functions functions = find(slMap, "Functions")'
functions = 4×1 string array "Initialize" "ExportedFunction:Runnable1" "ExportedFunction:Runnable2" "ExportedFunction:Runnable3"
% Get AUTOSAR mapping info for a specific 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
Find either client or server ports of a model. List the associated client-server interfaces, and modify the associated interface for a port of that model.
See also: Configure AUTOSAR Client-Server Interfaces.
Create and open model 'mControllerWithInterface_server'
.
hModel = 'mControllerWithInterface_server';
open_system(hModel);
Retrieve AUTOSAR property functions.
arProps = autosar.api.getAUTOSARProperties(hModel);
Find AUTOSAR ports - specify either ServerPort
or ClientPort
.
arPortType = 'ServerPort'; aswcPath = find(arProps,[],'AtomicComponent','PathType','FullyQualified'); sPorts=find(arProps,aswcPath{1},arPortType,'PathType','FullyQualified');
Loop through ports and list the associated interfaces of the found AUTOSAR ports.
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 the found 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
Using model 'mAutosarMsConfigAfter'
find AUTOSAR mode receiver ports, loop through and list the associated mode-switch interfaces for these ports. Modify the associated interface for a port. Map a Simulink® inport to an AUTOSAR model receiver port.
See also: Configure AUTOSAR Mode-Switch Interfaces.
Open model 'mAutosarMsConfigAfter'
.
hModel = 'mAutosarMsConfigAfter';
open_system(hModel);
Retrieve 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 = 0x0 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
orDataReceiveErrorEvent
— 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:
Opens a model.
Finds AUTOSAR runnables.
Loops through runnables and lists properties.
Modifies the name and symbol for an AUTOSAR periodic runnable.
Loops through AUTOSAR timing events and lists associated runnables.
Renames an AUTOSAR timing event.
Maps a Simulink entry-point function to an AUTOSAR periodic runnable.
% Open model hModel = 'autosar_swc_expfcns'; openExample(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:
Opens a model.
Adds AUTOSAR initialization and periodic runnables to the model.
Adds a timing event to the periodic runnable.
Maps Simulink initialization and step functions to the AUTOSAR runnables.
See also Configure Events for Runnable Activation.
% Open model hModel = 'autosar_swc_counter'; openExample(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'}
Retrieve and Map Signals and States. This example:
Opens a model.
Creates a Simulink Mapping object.
Retrieve Signals and States.
Map Signals and States.
% Open model hModel = "autosar_swc_counter"; openExample(hModel); % Use AUTOSAR map functions slMap=autosar.api.getSimulinkMapping(hModel); % Use AUTOSAR mapping find function to retrieve Signals listOfSignals = find(slMap,"Signals") namesOfSignals = get_param(listOfSignals, "Name")
listOfSignals = 36.0001 37.0001 38.0001 namesOfSignals = 3×1 cell array {'equal_to_count'} {'sum_out' } {'switch_out' }
% Use AUTOSAR mapping find function to retrieve States listOfStates = find(slMap,"States")'
ans = "autosar_swc_counter/X"
% Map Signals mapSignal(slMap, listOfSignals(1),'StaticMemory') mapSignal(slMap, listOfSignals(2),'ArTypedPerInstanceMemory') mapSignal(slMap, listOfSignals(3),'Auto')
% Map States mapState(slMap, 'autosar_swc_counter/X', '', 'StaticMemory')
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'; openExample(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 shows:
Loops through events and runnables in an open model.
For each event or runnable, extracts information to use with a custom scheduler.
hModel
specifies the name of an open AUTOSAR model, in this case 'autosar_swc_expfcns'
.
The following code is an example of how to extract timing information for runnables to prepare for hooking up a custom scheduler.
First use AUTOSAR property functions to retrieve property values for the software component.
hModel = 'autosar_swc_expfcns'; open_system(hModel); arProps = autosar.api.getAUTOSARProperties(hModel); swc = get(arProps,'XmlOptions','ComponentQualifiedName');
As well as to determine AUTOSAR internal behavior, runnables and events.
ib = get(arProps,swc,'Behavior'); events = get(arProps,ib,'Events'); runnables = get(arProps,ib,'Runnables');
Loop through these events and runnables using for loops.
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
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
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
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 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 mMultitasking_4rates 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:
Opens a model.
Adds an AUTOSAR inter-runnable variable (IRV) to the model.
Maps a Simulink data transfer to the IRV.
% Open model hModel = 'autosar_swc_expfcns'; openExample(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); % Retrieve list of DataTransfers listOfDataTransfers = find(slMap, "DataTransfers")
listOfDataTransfers = 1×4 string array "irv3" "irv1" "irv2" "irv4"
% 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:
Opens a model with multiple rates.
Adds an AUTOSAR inter-runnable variable (IRV) to the model.
Maps a Simulink® Rate Transition block to the IRV.
Open the model 'mMultitasking_4rates'
.
hModel = 'mMultitasking_4rates';
open_system(hModel);
Use AUTOSAR property functions.
arProps = autosar.api.getAUTOSARProperties(hModel);
Retrieve AUTOSAR internal behavior and add IRV 'myIrv'
with SwCalibrationAccess ReadWrite
.
irvName = 'myIrv'; swCalibValue = 'ReadWrite'; swc = get(arProps,'XmlOptions','ComponentQualifiedName')
swc = '/mMultitasking_4rates_pkg/mMultitasking_4rates_swc/mMultitasking_4rates'
ib = get(arProps,swc,'Behavior')
ib = 'mMultitasking_4rates/Behavior'
irvs = get(arProps,ib,'IRV')
irvs = 1x3 cell
{'mMultitasking_4rates/Behavior/IRV1'} {'mMultitasking_4rates/Behavior/IRV2'} {'mMultitasking_4rates/Behavior/IRV3'}
add(arProps,ib,'IRV',irvName,'SwCalibrationAccess',swCalibValue); irvs = get(arProps,ib,'IRV');
Use AUTOSAR map functions:
slMap=autosar.api.getSimulinkMapping(hModel);
Map the Simulink Real-Time 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')
arIrvName = 'myIrv'
arDataAccessMode = 'Explicit'
To pass validation, remove any redundant IRV
in the AUTOSAR configuration.
irvs = get(arProps,ib,'IRV'); delete(arProps,[ib,'/IRV3']) irvs = get(arProps,ib,'IRV')
irvs = 1x3 cell
{'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:
Opens a model.
Adds an AUTOSAR sender-receiver interface to the model.
Adds data elements.
Creates sender and receiver ports.
Maps Simulink inports and outports to AUTOSAR receiver and sender ports.
See also Configure AUTOSAR Runnables and Events.
% Open model hModel = 'autosar_swc_expfcns'; openExample(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'; openExample(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
Using example model 'mControllerWithInterface_server'
add and configure an AUTOSAR client-server (C-S) interface.
Open example model 'mControllerWithInterface_server'
.
hModel = 'mControllerWithInterface_server';
open_system(hModel);
Use AUTOSAR property functions.
arProps = autosar.api.getAUTOSARProperties(hModel);
Add AUTOSAR C-S interface.
ifName = 'myCsIf'; ifPkg = get(arProps,'XmlOptions','InterfacePackage')
ifPkg = '/ControllerWithInterface_ar_pkg/ControllerWithInterface_ar_if'
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'}
args = 4x2 cell
{'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')
ans = 1x4 cell
{'myCsIf/readData/Op'} {'myCsIf/readData/Data'} {'myCsIf/readData/ERR'} {'myCsIf/readData/NegCode'}
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')
swc = '/pkg/swc/SWC_Controller'
ib = get(arProps,swc,'Behavior')
ib = 'SWC_Controller/ControllerWithInterface_ar'
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);
The function name value 'readData' is obsolete and will be removed in a future release. For valid function name values, use autosar.api.getSimulinkMapping(modelName).find("Functions"). The function name value 'readData' is obsolete and will be removed in a future release. For valid function name values, use autosar.api.getSimulinkMapping(modelName).find("Functions").
arRunnableName=getFunction(slMap,'readData')
The function name value 'readData' is obsolete and will be removed in a future release. For valid function name values, use autosar.api.getSimulinkMapping(modelName).find("Functions").
arRunnableName = 'Runnable_myReadData'
Configure Client Properties
This example:
Opens a model.
Adds an AUTOSAR client-server (C-S) interface to the model.
Adds an operation.
Creates a client port.
Maps a Simulink® function caller to the AUTOSAR client port and operation.
Open the model 'mControllerWithInterface_client'
.
hModel = 'mControllerWithInterface_client';
open_system(hModel);
Retrieve the 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')
ifPaths = 1x2 cell
{'/pkg/if/csInterface'} {'/pkg/if/myCsIf'}
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'}
args = 4x2 cell
{'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')
ans = 1x4 cell
{'myCsIf/readData/Op'} {'myCsIf/readData/Data'} {'myCsIf/readData/ERR'} {'myCsIf/readData/NegCode'}
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')
arPort = 'myCPort'
arOp = 'readData'
Configure AUTOSAR Mode-Switch Interfaces
This example:
Opens a model.
Declares an AUTOSAR mode declaration group.
Adds a mode-switch (M-S) interface to the model.
Adds a mode receiver port.
Adds a ModeSwitchEvent to a runnable.
Maps a Simulink® inport to the AUTOSAR mode receiver port and mode group.
Open model 'mAutosarMsConfig'
.
hModel = 'mAutosarMsConfig';
open_system(hModel);
Retrieve AUTOSAR property functions.
arProps = autosar.api.getAUTOSARProperties(hModel);
The file mdgModes.m
declares AUTOSAR mode declaration group mdgModes
for use with the M-S interface.
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')
ifPaths = 1x1 cell array
{'/pkg/if/myMsIf'}
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')
runnables = 1x4 cell
{'ASWC/Behavior/Runnable_Init'} {'ASWC/Behavior/Runnable1'} {'ASWC/Behavior/Runnable2'} {'ASWC/Behavior/Runnable3'}
add(arProps,ib,'Events',msEventName,'Category','ModeSwitchEvent',... 'Activation', 'OnTransition', ... 'StartOnEvent', [ib '/' msRunnable]);
Separate the following code. The code below sets ModeSwitchEvent
port and trigger values.
To pass validation, remove the redundant timing event in AUTOSAR configuration.
events = get(arProps,ib,'Events'); delete(arProps,[ib,'/Event_t_1tic_A']) events = get(arProps,ib,'Events')
events = 1x3 cell
{'ASWC/Behavior/Event_t_1tic_B'} {'ASWC/Behavior/Event_t_10tic'} {'ASWC/Behavior/myMSEvent'}
Export the mode declaration group information to the AUTOSAR data type package in XML.
mdgPkg = get(arProps,'XmlOptions','DataTypePackage'); mdgPath = [mdgPkg '/' modeGroup]
mdgPath = '/pkg/dt/mdgModes'
initMode = [mdgPath '/STARTUP']
initMode = '/pkg/dt/mdgModes/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 the M-S interface.
set(arProps,[ifPkg '/' ifName '/' modeGroup],'ModeGroup',mdgPath)
Set the port and trigger for AUTOSAR property ModeSwitchEvent
.
expTrigger = {[mrPortName '.STARTUP'], [mrPortName '.SHUTDOWN']}
expTrigger = 1x2 cell
{'myMRPort.STARTUP'} {'myMRPort.SHUTDOWN'}
set(arProps,[ib '/' msEventName],'Trigger',expTrigger)
Retrieve the AUTOSAR map functions.
slMap=autosar.api.getSimulinkMapping(hModel);
Map the Simulink® inport MRPort
to the AUTOSAR mode receiver port myMRPort
and the mode group mdgModes
.
msDataAccessMode = 'ModeReceive'; [arPortName,arDataElementName,arDataAccessMode]=getInport(slMap,'MRPort'); mapInport(slMap,'MRPort',mrPortName,modeGroup,msDataAccessMode); [arPortName,arDataElementName,arDataAccessMode]=getInport(slMap,'MRPort')
arPortName = 'myMRPort'
arDataElementName = 'mdgModes'
arDataAccessMode = 'ModeReceive'
To pass validation, set inport Runnable1
sample time to -1 (inherited).
set_param([hModel,'/Runnable1'],'SampleTime','-1')
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'; openExample(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'; openExample(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'