Contents

AUTOSAR Code Generation

This example shows you how to generate AUTOSAR-compliant code and export AUTOSAR software component description XML files from a Simulink® model.

Prerequisites:

The Embedded Coder Support Package for AUTOSAR Standard is required for working with the model in this example.

Install the AUTOSAR Standard Support PackageInstall the AUTOSAR Standard Support Package

Support Packages and Support Package InstallerSupport Packages and Support Package Installer

Prepare the Model

You can use the rtwdemo_autosar_counter example model to see the steps for generating AUTOSAR-compliant code.

  1. Open the model rtwdemo_autosar_counter.

  2. Click the Code menu and select: C/C++ Code > Code Generation Options

  3. Change the system target file to the AUTOSAR target (autosar.tlc).

An alternative method to change the system target file is to execute the following commands:

% Model defines
modelName = 'rtwdemo_autosar_counter';

% open the model
open_system( modelName );

% Programmatically set the system target file to autosar.tlc
set_param( modelName, 'SystemTargetFile', 'autosar.tlc' );
set_param( modelName, 'PortableWordSizes', 'on');

Change the Default AUTOSAR Port Settings

By default the AUTOSAR port name, data element name, and interface name are the same as the Simulink® port name. You can change these default settings by using the AUTOSAR Interface dialog or command-line objects.

Configure the model as AUTOSAR Component: Using the AUTOSAR Interface Dialog

Overview of the AUTOSAR Interface dialogOverview of the AUTOSAR Interface dialog

  1. Click the Code menu and select: C/C++ Code > Configure Model as AUTOSAR Component

  2. In the Simulink-AUTOSAR Mapping Explorer, configure the mapping of Simulink Inports, Outports, and Entry point functions.

  3. In the AUTOSAR Properties Explorer, configure the AUTOSAR Interfaces, Component, Ports, Runnables, and Events

  4. After making changes, click Validate to confirm that the changes conform to AUTOSAR identifier naming conventions.

An alternative method to change Simulink mapping and AUTOSAR properties is to modify configuration programmatically using functions in autosar.api package

help autosar.api.getAUTOSARProperties;
help autosar.api.getSimulinkMapping;
  autosar.api.getAUTOSARProperties API class.
 
  DATAOBJ = autosar.api.getAUTOSARProperties(MDLNAME) creates an object
  DATAOBJ that can be used to query and set the AUTOSAR Properties of 
  Simulink model MDLNAME. 
 
  getAUTOSARProperties methods:
     add                      - add a property to an AUTOSAR element
     addPackageableElement    - add an AUTOSAR element to an AUTOSAR package
     delete                   - delete an AUTOSAR element
     find                     - find AUTOSAR elements  
     get                      - get the property of an AUTOSAR element
     set                      - set the property of an AUTOSAR element
     deleteUnmappedComponents - delete all unmapped components
 
  See also autosar.api.create autosar.api.getSimulinkMapping

  autosar.api.getSimulinkMapping API class.
 
  MAPPINGOBJ = autosar.api.getSimulinkMapping(MDLNAME) creates an object
  MAPPINGOBJ that can be used to query and set the Simulink mapping of
  Simulink model MDLNAME.
 
  getSimulinkMapping methods:
     getDataTransfer   - get the mapping information for a Simulink data transfer line
     getFunction       - get the mapping information for a Simulink entry point function
     getInport         - get the mapping information for a Simulink inport
     getOutport        - get the mapping information for a Simulink outport
     getFunctionCaller - get the mapping information for a Simulink function caller block
     mapDataTransfer   - map a Simulink data transfer line
     mapFunction       - map a Simulink entry point function
     mapInport         - map an inport
     mapOutport        - map an outport
     mapFunctionCaller - map a function caller
 
  See also autosar.api.create autosar.api.getAUTOSARProperties 

Generate AUTOSAR-Compliant Code

Click the Code menu and select: C/C++ Code > Build Model

An alternative method to generate AUTOSAR-compliant code from the model is by pressing Ctrl-B, or by using the following command.

rtwbuild( modelName );
### Starting build procedure for model: rtwdemo_autosar_counter
### Generating XML files description for model: rtwdemo_autosar_counter
### Successful completion of code generation for model: rtwdemo_autosar_counter

INC, K, LIMIT, RESET have been configured as AUTOSAR calibration parameters, which are accessed using the AUTOSAR Rte_Calprm function signature in the generated code.

You can import the resulting generated XML files and C code into an AUTOSAR authoring tool.

Verify the AUTOSAR Code Using Software-in-the-Loop Testing

A common technique to verify the generated code is to run Software-in-the-Loop testing. This allows you to verify the generated code in simulation. The AUTOSAR target automatically configures the generated code to route simulation data using AUTOSAR run-time environment (RTE) API calls.

% Configure the model for Software-in-the-loop
set_param( modelName, 'SimulationMode', 'Software-in-the-loop' );
set_param( modelName, 'GenCodeOnly', 'off' );

% Run the Software-in-the-Loop simulation
[~, ~, yout_sil] = sim(modelName,10);
### Starting build procedure for model: rtwdemo_autosar_counter
### Generating XML files description for model: rtwdemo_autosar_counter
### Successful completion of build procedure for model: rtwdemo_autosar_counter
### Preparing to start SIL simulation ...
Building with 'gcc'.
MEX completed successfully.
### Updating code generation report with SIL files ...
### Starting SIL simulation for component: rtwdemo_autosar_counter
### Stopping SIL simulation for component: rtwdemo_autosar_counter

Further Information

AUTOSAR Target documentationAUTOSAR Target documentation

Was this topic helpful?