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. This transposition applies to both row-major and column-major indexed arguments. However, in the case of row-major this transposition causes the input arguments to be swapped twice, so no change is visible to the indexing of the input arguments.

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 (Simulink 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).

AUTOSAR 4.0 Library Host Code Verification

To help support MATLAB host code verification for AUTOSAR models, AUTOSAR Blockset provides host implementations of IFX, IFL, MFX, and MFL routines in the AUTOSAR 4.0 library. The host library implementations enable software-in-the-loop (SIL) validation for models that trigger code replacements from the AUTOSAR 4.0 library.

Consider the following AUTOSAR model, which contains interpolation and math blocks that have been tuned to trigger AUTOSAR IFX and MFX routine code replacements. In the model configuration parameters, System target file (Simulink Coder) is set to autosar.tlc and Code replacement libraries (Embedded Coder) is set to AUTOSAR 4.0.

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

  1. Generates model code. MathWorks® host library implementations are used in 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 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 Blocks 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 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 dialog boxes 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, Curve, or Map. For example, here is a Prelookup block connected to a Curve Using Prelookup block.

autosar_lut_model_com.png

You can also open the model file by entering the following command at the MATLAB® command line:

open_system('mAutosarLutObjs');

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.

autosar_lut_com_block2.png

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

  1. 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'.

  2. 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'.

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

autosar_lut_ccode_com.png

Required Algorithm Property Settings for IFL/IFX Function and Block Mappings

IFL/IFX Function and Block MappingAlgorithm Property ParametersValue

Ifl_DPSearch

Prelookup

Extrapolation method

ExtrapMethod

Clip

Index search method

IndexSearchMethod

Linear search or Binary search

Use last breakpoint for input at or above upper limit

UseLastBreakPoint

On

Remove protection against out-of-range input in generated code

RemoveProtectionInput

Off

Integer rounding mode

RndMeth

Round or Zero

Ifl_IpoCur

Interpolation Using Prelookup

Interpolation method

InterpMethod

Linear

Extrapolation method

ExtrapMethod

Clip

Valid index input may reach last index

ValidIndexMayReachLast

On

Remove protection against out-of-range index in generated code

RemoveProtectionIndex

Off

Integer rounding mode

RndMeth

Round or Zero

Ifl_IpoMap

Interpolation Using Prelookup

Interpolation method

InterpMethod

Linear

Extrapolation method

ExtrapMethod

Clip

Valid index input may reach last index

ValidIndexMayReachLast

On

Remove protection against out-of-range index in generated code

RemoveProtectionIndex

Off

Integer rounding mode

RndMeth

Round or Zero

Ifl_IntIpoCur

n-D Lookup Table

Interpolation method

InterpMethod

Linear

Extrapolation method

ExtrapMethod

Clip

Index search method

IndexSearchMethod

Linear search or Binary search

Use last table value for inputs at or above last breakpoint

UseLastTableValue

On

Remove protection against out-of-range input in generated code

RemoveProtectionInput

Off

Integer rounding mode

RndMeth

Round or Zero

Ifl_IntIpoMap

n-D Lookup Table

Interpolation method

InterpMethod

Linear

Extrapolation method

ExtrapMethod

Clip

Index search method

IndexSearchMethod

Linear search or Binary search

Use last table value for inputs at or above last breakpoint

UseLastTableValue

On

Remove protection against out-of-range input in generated code

RemoveProtectionInput

Off

Integer rounding mode

RndMeth

Round or Zero

Ifx_DPSearch

Prelookup

Extrapolation method

ExtrapMethod

Clip

Index search method

IndexSearchMethod

Linear search or Binary search

Use last breakpoint for input at or above upper limit

UseLastBreakPoint

On

Remove protection against out-of-range input in generated code

RemoveProtectionInput

Off

Integer rounding mode

RndMeth

Round or Zero

Ifx_IpoCur

Interpolation Using Prelookup

Interpolation method

InterpMethod

Linear

Extrapolation method

ExtrapMethod

Clip

Valid index input may reach last index

ValidIndexMayReachLast

On

Remove protection against out-of-range index in generated code

RemoveProtectionIndex

Off

Integer rounding mode

RndMeth

Round or Zero

Ifx_LkUpCur

Interpolation Using Prelookup

Interpolation method

InterpMethod

Flat

Extrapolation method

ExtrapMethod

Clip

Remove protection against out-of-range index in generated code

RemoveProtectionIndex

Off

Integer rounding mode

RndMeth

Round or Zero

Valid index input may reach last index

ValidIndexMayReachLast

On

Ifx_IpoMap

Interpolation Using Prelookup

Interpolation method

InterpMethod

Linear

Extrapolation method

ExtrapMethod

Clip

Valid index input may reach last index

ValidIndexMayReachLast

On

Remove protection against out-of-range index in generated code

RemoveProtectionIndex

Off

Integer rounding mode

RndMeth

Round or Zero

Ifx_LkUpMap

Interpolation Using Prelookup

Interpolation method

InterpMethod

Nearest

Extrapolation method

ExtrapMethod

Clip

Remove protection against out-of-range index in generated code

RemoveProtectionIndex

Off

Integer rounding mode

RndMeth

Round or Zero

Valid index input may reach last index

ValidIndexMayReachLast

On

Ifx_LkUpBaseMap

Interpolation Using Prelookup

Interpolation method

InterpMethod

Flat

Extrapolation method

ExtrapMethod

Clip

Remove protection against out-of-range index in generated code

RemoveProtectionIndex

Off

Integer rounding mode

RndMeth

Round or Zero

Valid index input may reach last index

ValidIndexMayReachLast

On

Ifx_IntIpoCur

n-D Lookup Table

Interpolation method

InterpMethod

Linear

Extrapolation method

ExtrapMethod

Clip

Index search method

IndexSearchMethod

Linear search or Binary search

Use last table value for inputs at or above last breakpoint

UseLastTableValue

On

Remove protection against out-of-range input in generated code

RemoveProtectionInput

Off

Integer rounding mode

RndMeth

Round or Zero

Ifx_IntLkUpCur

n-D Lookup Table

Interpolation method

InterpMethod

Flat

Extrapolation method

ExtrapMethod

Clip

Index search method

IndexSearchMethod

Linear search or Binary search

Remove protection against out-of-range input in generated code

RemoveProtectionInput

Off

Integer rounding mode

RndMeth

Round or Zero

Use last table value for inputs at or above last breakpoint

UseLastTableValue

On

Ifx_IntIpoFixCur

n-D Lookup Table

Interpolation method

InterpMethod

Linear

Extrapolation method

ExtrapMethod

Clip

Index search method

IndexSearchMethod

Evenly spaced points

Use last table value for inputs at or above last breakpoint

UseLastTableValue

On

Remove protection against out-of-range input in generated code

RemoveProtectionInput

Off

Integer rounding mode

RndMeth

Round or Zero

Use last table value for inputs at or above last breakpoint

UseLastTableValue

On

Model configuration parameter Optimization > Default parameter behavior

DefaultParameterBehavior

Inlined

Breakpoint data should match power 2 spacing.

 

Ifx_IntLkUpFixCur

n-D Lookup Table

Interpolation method

InterpMethod

Flat

Extrapolation method

ExtrapMethod

Clip

Index search method

IndexSearchMethod

Evenly spaced points

Remove protection against out-of-range input in generated code

RemoveProtectionInput

Off

Integer rounding mode

RndMeth

Round or Zero

Model configuration parameter Optimization > Signals and Parameters > Default parameter behavior

DefaultParameterBehavior

Inlined

Breakpoint data must match power 2 spacing.

 

Ifx_IntIpoFixICur

n-D Lookup Table

Interpolation method

InterpMethod

Linear

Extrapolation method

ExtrapMethod

Clip

Index search method

IndexSearchMethod

Evenly spaced points

Use last table value for inputs at or above last breakpoint

UseLastTableValue

On

Remove protection against out-of-range input in generated code

RemoveProtectionInput

Off

Integer rounding mode

RndMeth

Round or Zero

Breakpoint data must not match power 2 spacing.

 

Ifx_IntLkUpFixICur

n-D Lookup Table

Interpolation method

InterpMethod

Flat

Extrapolation method

ExtrapMethod

Clip

Index search method

IndexSearchMethod

Evenly spaced points

Remove protection against out-of-range input in generated code

RemoveProtectionInput

Off

Integer rounding mode

RndMeth

Round or Zero

Use last table value for inputs at or above last breakpoint

UseLastTableValue

On

Breakpoint data must not match power 2 spacing.

 

Ifx_IntIpoMap

n-D Lookup Table

Interpolation method

InterpMethod

Linear

Extrapolation method

ExtrapMethod

Clip

Index search method

IndexSearchMethod

Linear search or Binary search

Use last table value for inputs at or above last breakpoint

UseLastTableValue

On

Remove protection against out-of-range input in generated code

RemoveProtectionInput

Off

Integer rounding mode

RndMeth

Round or Zero

Ifx_IntLkUpMap

n-D Lookup Table

Interpolation method

InterpMethod

Nearest

Extrapolation method

ExtrapMethod

Clip

Index search method

IndexSearchMethod

Linear search or Binary search

Remove protection against out-of-range input in generated code

RemoveProtectionInput

Off

Integer rounding mode

RndMeth

Round or Zero

Use last table value for inputs at or above last breakpoint

UseLastTableValue

On

Ifx_IntLkUpBaseMap

n-D Lookup Table

Interpolation method

InterpMethod

Flat

Extrapolation method

ExtrapMethod

Clip

Index search method

IndexSearchMethod

Linear search or Binary search

Remove protection against out-of-range input in generated code

RemoveProtectionInput

Off

Integer rounding mode

RndMeth

Round or Zero

Use last table value for inputs at or above last breakpoint

UseLastTableValue

On

Ifx_IntIpoFixMap

n-D Lookup Table

Interpolation method

InterpMethod

Linear

Extrapolation method

ExtrapMethod

Clip

Index search method

IndexSearchMethod

Evenly spaced points

Use last table value for inputs at or above last breakpoint

UseLastTableValue

On

Remove protection against out-of-range input in generated code

RemoveProtectionInput

Off

Integer rounding mode

RndMeth

Round or Zero

Model configuration parameter Optimization > Signals and Parameters > Default parameter behavior

DefaultParameterBehavior

Inlined

Breakpoint data must match power 2 spacing.

 

Ifx_IntLkUpFixMap

n-D Lookup Table

Interpolation method

InterpMethod

Nearest

Extrapolation method

ExtrapMethod

Clip

Index search method

IndexSearchMethod

Evenly spaced points

Remove protection against out-of-range input in generated code

RemoveProtectionInput

Off

Integer rounding mode

RndMeth

Round or Zero

Use last table value for inputs at or above last breakpoint

UseLastTableValue

On

Model configuration parameter Optimization > Signals and Parameters > Default parameter behavior

DefaultParameterBehavior

Inlined

Breakpoint data must match power 2 spacing.

 

Ifx_IntLkUpFixBaseMap

n-D Lookup Table

Interpolation method

InterpMethod

Flat

Extrapolation method

ExtrapMethod

Clip

Index search method

IndexSearchMethod

Evenly spaced points

Remove protection against out-of-range input in generated code

RemoveProtectionInput

Off

Integer rounding mode

RndMeth

Round or Zero

Use last table value for inputs at or above last breakpoint

UseLastTableValue

On

Model configuration parameter Optimization > Signals and Parameters > Default parameter behavior

DefaultParameterBehavior

Inlined

Breakpoint data must match power 2 spacing.

 

Ifx_IntIpoFixIMap

n-D Lookup Table

Interpolation method

InterpMethod

Linear

Extrapolation method

ExtrapMethod

Linear

Index search method

IndexSearchMethod

Evenly spaced points

Use last table value for inputs at or above last breakpoint

UseLastTableValue

On

Remove protection against out-of-range input in generated code

RemoveProtectionInput

Off

Integer rounding mode

RndMeth

Round or Zero

Breakpoint data must not match power 2 spacing.

 

Ifx_IntLkUpFixIMap

n-D Lookup Table

Interpolation method

InterpMethod

Nearest

Extrapolation method

ExtrapMethod

Clip

Index search method

IndexSearchMethod

Evenly spaced points

Remove protection against out-of-range input in generated code

RemoveProtectionInput

Off

Integer rounding mode

RndMeth

Round or Zero

Use last table value for inputs at or above last breakpoint

UseLastTableValue

On

Breakpoint data must not match power 2 spacing.

 

Ifx_IntLkUpFixIBaseMap

n-D Lookup Table

Interpolation method

InterpMethod

Flat

Extrapolation method

ExtrapMethod

Clip

Index search method

IndexSearchMethod

Evenly spaced points

Remove protection against out-of-range input in generated code

RemoveProtectionInput

Off

Integer rounding mode

RndMeth

Round or Zero

Use last table value for inputs at or above last breakpoint

UseLastTableValue

On

Breakpoint data must not match power 2 spacing.

 

Related Examples

More About