Main Content

Code Generation with AUTOSAR Code Replacement Library

If your model is configured for AUTOSAR code generation, you can use the AUTOSAR 4.0 code replacement library to produce functions that closely align with the AUTOSAR standard.

Code Replacement Library for AUTOSAR Code Generation

The AUTOSAR 4.0 code replacement library enables you to customize the code generator to produce C code that closely aligns with the AUTOSAR standard. Consider using the code replacement library if:

  • You want to use service routines provided in the library.

  • You have replacement code for the service routines.

  • The replacement code follows the AUTOSAR file naming convention, that is, routines for any given specification are in one header file (for example, Mfl.h or Mfx.h)

  • You have a build harness setup that can compile and link the AUTOSAR library with the generated code. For more information about building code for AUTOSAR, see Code Generation.

Note

MATLAB® and Simulink® lookup table indexing differs from AUTOSAR MAP indexing. MATLAB takes the linear algebra approach—row (u1) and column (u2). AUTOSAR (and ASAM) takes the Cartesian coordinate approach—x-axis (u2) and y-axis (u1), where u1 and u2 are input arguments to Simulink 2-D lookup table blocks. Due to the difference, the code replacement software transposes the input arguments for AUTOSAR MAP routines.

For more information on code replacement and code replacement libraries, see What Is Code Replacement? (Embedded Coder) and Code Replacement Libraries (Embedded Coder).

Find Supported AUTOSAR Library Routines

To explore the AUTOSAR library routines supported by the AUTOSAR code replacement library, use the Code Replacement Viewer (Embedded Coder). To open the viewer, at the command prompt, enter crviewer('AUTOSAR 4.0').

For more information, see Choose a Code Replacement Library (Embedded Coder).

Configure Code Generator to Use AUTOSAR 4.0 Code Replacement Library

To configure the code generator to use the AUTOSAR code replacement library for your model, open the Configuration Parameters dialog box. Select Code Generation > Interface > Code replacement libraries > AUTOSAR 4.0.

For more information on code replacement and code replacement libraries, see What Is Code Replacement? (Embedded Coder) and Code Replacement Libraries (Embedded Coder).

Code Replacement Library Checks

Code replacement requires that the combination of types for input, breakpoint, table, and output types are compatible with the AUTOSAR specification. Floating-point (IFL) replacement only supports single types while fixed-point (IFX) replacement supports uint8, uint16, int8, int16 and associated fixed-point types. When using these routine blocks, the type combination requirements vary and are enforced as required.

AUTOSAR Code Replacement Library Example for IFX/IFL Function Replacement

The Code Replacement Viewer lists AUTOSAR floating-point interpolation (IFL) and fixed-point interpolation (IFX) library routines that you can generate in lookup table C code. For replacing lookup table C code with IFL or IFX library routines, AUTOSAR Blockset provides lookup table blocks that are preconfigured for AUTOSAR code generation. You insert a block such as Curve or Map in your model, then open the block dialog box and configure the block to generate a specific interpolation routine required by your design. For more information, see Configure Lookup Tables for AUTOSAR Measurement and Calibration.

This example shows how to replace code generated for AUTOSAR lookup table blocks with functions that are compatible with AUTOSAR IFL library routines. If you want to replace code with IFX library routines, you can edit the lookup table block dialog boxes to change the targeted routine library.

  1. Create your Simulink model by using any of these AUTOSAR lookup table blocks: Prelookup, Curve Using Prelookup, Map Using Prelookup, Curve, or Map. For example, here is a Prelookup block connected to a Curve Using Prelookup block.

    Alternatively, you can open the AUTOSAR example model mAutosarLutObjs.slx, which contains the displayed blocks. To copy the model file to your working folder, enter this MATLAB command:

    copyfile(fullfile(matlabroot,'help/toolbox/autosar/examples/mAutosarLutObjs.slx'),'.')
  2. Open each lookup table block and configure it to generate a routine from the AUTOSAR 4.0 code replacement library (CRL). As you modify block settings, the block dialog box updates the name of the targeted AUTOSAR routine.

    For details about configuring the blocks in this example, see Configure COM_AXIS Lookup Tables by Using Lookup Table and Breakpoint Objects.

  3. Configure the code generator to use the AUTOSAR 4.0 CRL for your model. In the Configuration Parameters dialog box, select Code Generation > Interface > Code replacement libraries > AUTOSAR 4.0. Alternatively, from the command line or programmatically, use set_param to set the CodeReplacementLibrary parameter to 'AUTOSAR 4.0'.

  4. Optionally, you can configure the model to produce a code generation report that summarizes which blocks trigger code replacements. In the Configuration Parameters dialog box, in the Code Generation > Report pane, select the option Summarize which blocks triggered code replacements. Alternatively, from the command line or programmatically, use set_param to set the GenerateCodeReplacementReport parameter to 'on'.

  5. Build the model and review the generated code for expected code replacements. For example, search the generated code for the routine prefix Ifl.

Related Examples

More About