%% Develop a Model that Complies with the AUTOSAR Standard
% Generate AUTOSAR-compliant C code and export AUTOSAR XML (ARXML) descriptions from a Simulink® model.
% AUTOSAR Blockset software supports AUTomotive Open System ARchitecture (AUTOSAR), an open and standardized automotive software architecture. Automobile manufacturers, suppliers, and tool developers jointly develop AUTOSAR components. To develop AUTOSAR components in Simulink, follow this general workflow:
% Create a Simulink representation of an AUTOSAR component.
% Develop the component by refining the AUTOSAR configuration and creating algorithmic model content.
% Generate ARXML descriptions and algorithmic C code for testing in Simulink or integration into an AUTOSAR run-time environment. (AUTOSAR code generation requires Simulink Coder and Embedded Coder.)
%
%% Prepare Model for AUTOSAR Code Generation
% To see the steps for generating AUTOSAR-compliant C code and exporting ARXML descriptions from an AUTOSAR model, open a model and prepare the model for AUTOSAR code generation.
% Open a model from which you want to generate AUTOSAR code and descriptions. The model can be unconfigured or only partially configured for code generation. This example uses AUTOSAR example model autosar_swc.

open_system(fullfile(matlabroot,'examples','autosarblockset','main','autosar_swc'));

%
% To prepare the model for AUTOSAR code generation, use the Embedded Coder Quick Start. From the Apps tab, open the AUTOSAR Component Designer app. On the AUTOSAR tab, click Quick Start.
% Work through the quick-start procedure. In the Output window, select output option C code compliant with AUTOSAR.
%
% The quick-start software takes the following steps to configure an AUTOSAR software component model:
% Configures code generation settings for the model. If the AUTOSAR target is not already selected, the software sets model configuration parameter System target file to autosar.tlc and Generate XML for schema version to a default schema value.
% If no AUTOSAR mapping exists, creates a mapped AUTOSAR software component for the model.
% Performs a model build.
% In the last window, when you click Finish, your model opens in the AUTOSAR code perspective.
%
%
%% Develop Simulink Representation of AUTOSAR Software Component
% After you create an AUTOSAR software component model in Simulink, use the Code Mappings editor and the AUTOSAR Dictionary to further develop the AUTOSAR component. For more information, see AUTOSAR Component Configuration.
% In a tabbed table format, the Code Mappings editor displays Simulink model elements, such as entry-point functions, inports, outports, and data transfers. Use the editor to map Simulink model elements to AUTOSAR component elements from a Simulink model perspective. AUTOSAR component elements are defined in the AUTOSAR standard, and include runnable entities, ports, and inter-runnable variables (IRVs).
% In the AUTOSAR code perspective view of your model, select the Inports tab of the Code Mappings editor, and select a model inport. The attributes of the selected inport appear in the Property Inspector panel. This example selects Simulink inport In1_1s, which is mapped to AUTOSAR port ReceivePort and data element In1 with data access mode ImplicitReceive. In each Code Mappings editor tab, you can select model elements and modify their AUTOSAR mapping and attributes. Your modifications are reflected in the generated ARXML descriptions and C code.
% If you are using AUTOSAR example model autosar_swc with this example, modify the communication attributes for the mapped Simulink inport In1_1s. In the Property Inspector, change the AliveTimeout attribute from 60 to 30, change HandleNeverReceived from false to true, and change InitValue from 0 to 1.
%
% To configure the AUTOSAR properties of the mapped AUTOSAR software component, open the AUTOSAR Dictionary. In the Code Mappings editor, click the AUTOSAR Dictionary button, which is the leftmost button. The AUTOSAR Dictionary opens in the AUTOSAR view that corresponds to the Simulink element that you last selected and mapped in the Code Mappings editor. If you selected and mapped a Simulink inport, the dictionary opens in ReceiverPorts view and displays the AUTOSAR port to which you mapped the inport.
% In a tree format, the AUTOSAR Dictionary displays the mapped AUTOSAR component and its elements, communication interfaces, computation methods, software address methods, and XML options. Use the dictionary to configure AUTOSAR elements and properties from an AUTOSAR component perspective.
% In the ReceiverPorts view, select the AUTOSAR receiver port to which the Simulink inport was mapped in the Code Mappings editor. If an AUTOSAR element has additional undisplayed attributes, selecting the element displays them. In each AUTOSAR element view, you can add or rename AUTOSAR elements and modify their displayed properties. Your modifications are reflected in the generated ARXML descriptions and C code.
% If you are using AUTOSAR example model autosar_swc with this example, rename the AUTOSAR receiver port from ReceivePort to RequirePort. To initiate the edit, click in the Name value field.
%
%
%% Generate C Code and ARXML Descriptions
% If you have Simulink Coder and Embedded Coder software, you can build the AUTOSAR model. Building the AUTOSAR model generates AUTOSAR-compliant C code and exports ARXML descriptions. In the model window, press Ctrl+B.
% When the build completes, a code generation report opens. Examine the report. Verify that your Code Mappings editor and AUTOSAR Dictionary changes are reflected in the C code and ARXML descriptions. For example, use the Find field to search for the name of the AUTOSAR receiver port that you modified and renamed.
% The generated C code encodes the AUTOSAR receiver port name in AUTOSAR run-time environment API read calls.
%
% The generated ARXML description of the AUTOSAR receiver port uses the modified port name and the modified values for port communication attributes AliveTimeout, HandleNeverReceived, and InitValue.
%
%
%% Related Links
% AUTOSAR Code Generation
% AUTOSAR Component Configuration
% AUTOSAR Blockset
% Copyright 2020 The MathWorks, Inc.