Documentation

Generate Code That Is Compliant with the AUTOSAR Standard

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 Package

Support Package Installation

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 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 Configure AUTOSAR Interface dialog box or command-line objects.

Configure Model as AUTOSAR Component Using the Configure AUTOSAR Interface Dialog Box

Overview of the Configure AUTOSAR Interface dialog box

  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, entry point functions, and data transfers.

  3. In the AUTOSAR Properties Explorer, configure the AUTOSAR ports, runnables, events, inter-runnable variables, interfaces, computational methods, and XML options.

  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

    Reference page in Doc Center
       doc autosar.api.getAUTOSARProperties


  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
     getLookupTable    - get the mapping information for a Simulink lookup table
     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
     mapLookupTable    - map a lookup table
 
  See also autosar.api.syncModel autosar.api.create autosar.api.getAUTOSARProperties

    Reference page in Doc Center
       doc autosar.api.getSimulinkMapping


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 to press Ctrl-B, or use 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 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' );

% Run the Software-in-the-Loop simulation
silOut = sim(modelName,'ReturnWorkspaceOutputs', 'on');
### Starting build procedure for model: rtwdemo_autosar_counter
### Generated code for 'rtwdemo_autosar_counter' is up to date because no structural, parameter or code replacement library changes were found.
### 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

Embedded Coder AUTOSAR documentation

Was this topic helpful?