Contenido principal

Code Generation with AUTOSAR Code Replacement Library

R2026b

If your model is configured for AUTOSAR code generation, you can use the AUTOSAR 4.x code replacement library (CRL) to replace generated C code for lookup tables and math operations with interpolation routines and math routines that the AUTOSAR standard defines. For more information about code replacement and code replacement libraries supported by Embedded Coder®, see What Is Code Replacement? (Embedded Coder) and Code Replacement Libraries (Embedded Coder).

To configure and verify generated C code with routines defined by the AUTOSAR standard:

  1. To configure the code generator to use the AUTOSAR 4.x code replacement library for your model. Open the Configuration Parameters dialog box and click Code Generation. Under Interface, set Code replacement libraries to AUTOSAR 4.x.

  2. Configure AUTOSAR Blockset lookup table blocks for the specific routine as specified in Required Block Parameter Settings for AUTOSAR IFL Routines and Required Block Parameter Settings for AUTOSAR IFX Routines.

  3. Generate AUTOSAR-compliant C code and inspect the generated routine calls.

  4. Run software-in-the-loop (SIL) simulations with the host library.

Code Replacement Library for AUTOSAR Code Generation

The AUTOSAR 4.x 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 standardized AUTOSAR service routines.

  • 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 compiles and links the AUTOSAR library with the generated code. For more information about building code for AUTOSAR, see Code Generation.

The AUTOSAR CRL provides function naming, argument ordering, and header usage the AUTOSAR standard.

The supported families of AUTOSAR routines are:

  • Floating-point interpolation (IFL)

  • Fixed-point interpolation (IFX)

  • Math function library (MFL, MFX)

Note

Simulink® and MATLAB® use different lookup table indexing than the AUTOSAR standard. MATLAB uses u1 for rows and u2 for columns, while AUTOSAR uses u2 for the x-axis and u1 for the y-axis. Because of this difference, the code generator transposes input arguments for AUTOSAR map routines. When you configure the layout of lookup tables as Row-major, the software transposes the input arguments twice, so no change is visible in the indexing of the input arguments. For more information about configuring array layouts for multidimensional lookup tables in Simulink models mapped to AUTOSAR software components, see Configure Array Layout for Multidimensional Lookup Tables.

Find Supported AUTOSAR Library Routines

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

To view AUTOSAR routines in the Code Replacement Viewer tool, expand the AUTOSAR 4.x folder and select a .mat file to view the AUTOSAR routines. After you select a routine in the contents page, the More Information pane displays details of the routine. For AUTOSAR 4.x routines, Conceptual argument(s) refer to Simulink inputs, and Implementation refer to AUTOSAR routine arguments in generated code. For example, in the following image routine Ifx_IntIpoCur_s8_s8 is selected.

Code Replacement Viewer. AUTOSAR 4.x code replacement library is expanded with one of the IFX routines selected.

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

Configure Code Generator to Use AUTOSAR 4.x Code Replacement Library

To configure the code generator to use the AUTOSAR 4.x code replacement library for your model, open the Configuration Parameters dialog box. Click Code Generation, and under Interface, change Code replacement libraries to AUTOSAR 4.x.

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

AUTOSAR 4.x Library Host Code Verification

To support MATLAB host code verification for AUTOSAR models, AUTOSAR Blockset provides host implementations of IFX, IFL, MFX, and MFL routines in the AUTOSAR 4.x library. The host library implementations enable software-in-the-loop (SIL) validation for models that trigger code replacements from the AUTOSAR 4.x library. For more information about code replacement library verification, see Verify Code Replacement Library (Embedded Coder).

Consider the following AUTOSAR model, which contains interpolation and math blocks configured to trigger AUTOSAR IFX and MFX routine code replacements.

Model with two AUTOSAR Curve blocks that interpolate incoming data from Counter Free-Running blocks, and perform math operations on the outputs of the Curve blocks.

Configure and run a SIL simulation of the model. The SIL simulation:

  1. Generates model code. The generated code uses MathWorks® host library implementations for IFX, IFL, MFX, and MFL routine code replacements.

  2. Builds the SIL application. The host library is linked to the SIL executable.

  3. Runs the model and produces simulation output, based on your SIL settings.

If you prefer to use your own host library or custom code for SIL simulations, you can disable the MathWorks host library by using the following command:

set_param(modelname,DisableAUTOSARRoutinesHostLibrary="on");

Code Replacement Library Checks

Code replacement requires that the combination of types for inputs, breakpoint data sets, lookup tables, and output data types are compatible with the AUTOSAR specification. Floating-point interpolation (IFL) replacement supports only the single data type, while fixed-point interpolation (IFX) replacement supports uint8, uint16, int8, int16, and fixed-point types. When using these routine blocks, the type combination requirements vary and are enforced by the software.

AUTOSAR Code Replacement Library Example for IFX and 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 pre-configured 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 Calibration and Measurement.

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 parameters to change the targeted routine library.

You can create your Simulink model by using any of these AUTOSAR lookup table blocks: Prelookup, Curve Using Prelookup, Map Using Prelookup, Cuboid Using Prelookup, Curve, or Map.

Open the model mAutosarLutObjs, which has several AUTOSAR LUT blocks pre-configured for AUTOSAR code generation.

open_system("mAutosarLutObjs");

In the model, each lookup table block is configured to generate an IFL (floating-point) routine from the AUTOSAR 4.x code replacement library (CRL). As you modify block parameters, the Block Parameters dialog box automatically updates the name of the targeted AUTOSAR routine. For the Curve Using Prelookup block, the Targeted Routine Library parameter is set to IFL (floating-point), so the read-only block parameter Targeted Routine is automatically set to Ifl_IpoCur.

EM_CurveUsingPreLookup.png

For details about configuring the blocks in this example, see Configure Lookup Tables for AUTOSAR Calibration and Measurement.

To configure the code generator to use the AUTOSAR 4.x CRL for your model, select the AUTOSAR 4.x code replacement library in the model configuration parameters. In the Configuration Parameters dialog box, click Code Generation and under Interface, set Code replacement libraries to AUTOSAR 4.x. Alternatively, in the MATLAB® Command Window enter:

set_param("mAutosarLutObjs","CodeReplacementLibrary","AUTOSAR 4.x");

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, click Code Generation. Under Report, expand Advanced parameters, and select Summarize which blocks triggered code replacements. Alternatively, in the MATLAB Command Window enter:

set_param("mAutosarLutObjs","GenerateCodeReplacementReport","on");

Generate and inspect code for the expected code replacements. In generated file mAutosarLutObjs.c, the Runnable_Step function contains the Rte_Write functions for each of the LUT blocks with the expected AUTOSAR routine. For the Using Prelookup blocks, the following Rte_Write functions are generated with the routines indicated by the Targeted Routine block parameter.

For the Curve Using Prelookup block, the code generator produces:

Rte_IWrite_Runnable_Step_Out2_Out2(Ifl_IpoCur_f32(&tmp,
    &mAutosarLutObjs_ConstP.pooled1[0]));

For the Map Using Prelookup block, the code generator produces:

Rte_IWrite_Runnable_Step_Out4_Out4(Ifl_IpoMap_f32(&tmp, &tmp_0, 3U,
    &mAutosarLutObjs_ConstP.pooled3[0]));

For the Cuboid Using Prelookup block, the code generator produces:

Rte_IWrite_Runnable_Step_Out5_Out5(Ifl_IpoCub_f32(&tmp, &tmp_0, &tmp_1, 3U, 3U,
    &mAutosarLutObjs_ConstP.CuboidUsingPrelookup_Table[0]));

In the exported ARXML, the IFL routine library is included as an external header file in the REQUIRED-ARTIFACTS section of the implementation ARXML file.

<REQUIRED-ARTIFACTS>
  <DEPENDENCY-ON-ARTIFACT>
    <SHORT-NAME>Ifl</SHORT-NAME>
    <ARTIFACT-DESCRIPTOR>
      <SHORT-LABEL>Ifl</SHORT-LABEL>
      <CATEGORY>SWHDR</CATEGORY>
    </ARTIFACT-DESCRIPTOR>
  </DEPENDENCY-ON-ARTIFACT>
</REQUIRED-ARTIFACTS>

Review the generated code for expected code replacements. For example, search the generated code for the routine prefix ifl.

For an example that uses math code replacements, see Generate AUTOSAR Cuboid Interpolation Routines.

Required Block Parameter Settings for AUTOSAR IFL Routines

To use IFL routines in generated code, you must set the Targeted Routine Library block parameter to IFL (floating-point). This table shows the additional required parameter settings for an AUTOSAR block to generate code that uses a specific targeted IFL routine.

Targeted IFL RoutineAUTOSAR Blockset Interpolation Lookup Table BlockAdditional Required Block Configuration
ParameterRequired Setting
Ifl_DPSearch

Prelookup

Index search method

Linear search or Binary search

Ifl_IpoCur

Curve Using Prelookup

Interpolation method

Linear point-slope

Ifl_IpoMap

Map Using Prelookup

Interpolation method

Linear point-slope

Ifl_IpoCub

Cuboid Using Prelookup

None

Not applicable

Ifl_IntIpoCur

Curve

Interpolation method

Linear point-slope

Index search method

Linear search or Binary search

Ifl_IntIpoMap

Map

Interpolation method

Linear point-slope

Index search method

Linear search or Binary search

Required Block Parameter Settings for AUTOSAR IFX Routines

To use IFX routines in generated code set Targeted Routine Library to IFX (fixed-point). This table shows the additional required parameter settings for an AUTOSAR block to generate code that uses a specific targeted IFX routine.

Targeted IFX RoutineAUTOSAR Blockset Interpolation Lookup Table BlockAdditional Required Block Configuration
ParameterRequired Setting
Ifx_DPSearch

Prelookup

Index search method

Linear search or Binary search

Ifx_IpoCub

Cuboid Using Prelookup

None

Not applicable

Ifx_IpoCur

Curve Using Prelookup

Interpolation method

Linear point-slope

Ifx_IntIpoCur

Curve

Interpolation method

Linear point-slope

Index search method

Linear search or Binary search

Ifx_LkUpCur

Curve Using Prelookup

Interpolation method

Flat

Ifx_IpoMap

Map Using Prelookup

Interpolation method

Linear point-slope

Ifx_IntIpoMap

Map

Interpolation method

Linear point-slope

Ifx_LkUpMap

Map Using Prelookup

Interpolation method

Nearest

Ifx_LkUpBaseMap

Map Using Prelookup

Interpolation method

Flat

This table shows the remaining one-dimensional IFX curve routine families that can be generated by using the Curve block and the required settings to generate a routine for each family.

Targeted IFX RoutineAdditional Required Curve Block Configuration

Parameter

Required Setting

Ifx_IntLkUpCur

Interpolation method

Flat

Index search method

Linear search or Binary search

Ifx_IntIpoFixCur

Data specification

Table and breakpoints

Breakpoint specification

Even spacing

Spacing

The values must match power two spacing.

Interpolation method

Linear point-slope

Ifx_IntLkUpFixCur

Data specification

Table and breakpoints

Breakpoint specification

Even spacing

Spacing

The values must match power two spacing.

Interpolation method

Flat

Ifx_IntIpoFixICur

Data specification

Table and breakpoints

Breakpoint specification

Even spacing

Spacing

The values must not match power two spacing.

Interpolation method

Linear point-slope

Ifx_IntLkUpFixICur

Data specification

Table and breakpoints

Breakpoint specification

Even spacing

Spacing

The values must not match power two spacing.

Interpolation method

Flat

This table shows the remaining two-dimensional IFX map routine families that can be generated by using the Map block and the required settings to generate a routine for each family.

Targeted IFX RoutineAdditional Required Map Block Configuration

Parameter

Required Setting

Ifx_IntLkUpMap

Interpolation method

Nearest

Index search method

Linear search or Binary search

Ifx_IntLkUpBaseMap

Interpolation method

Flat

Index search method

Linear search or Binary search

Ifx_IntIpoFixMap

Data specification

Table and breakpoints

Breakpoints specification

Even spacing

Spacing

The values must match power two spacing.

Interpolation method

Linear point-slope

Ifx_IntLkUpFixMap

Data specification

Table and breakpoints

Breakpoints specification

Even spacing

Spacing

The values must match power two spacing.

Interpolation method

Nearest

Ifx_IntLkUpFixBaseMap

Data specification

Table and breakpoints

Breakpoints specification

Even spacing

Spacing

The values must match power two spacing.

Interpolation method

Flat

Ifx_IntIpoFixIMap

Data specification

Table and breakpoints

Breakpoints specification

Even spacing

Spacing

The values must not match power two spacing.

Interpolation method

Linear point-slope

Ifx_IntLkUpFixIMap

Data specification

Table and breakpoints

Breakpoints specification

Even spacing

Spacing

The values must not match power two spacing.

Interpolation method

Nearest

Ifx_IntLkUpFixIBaseMap

Data specification

Table and breakpoints

Breakpoints specification

Even spacing

Spacing

The values must not match power two spacing.

Interpolation method

Flat

See Also

Topics