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
orMfx.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:
Generates model code. MathWorks® host library implementations are used in IFX, IFL, MFX, and MFL routine code replacements.
Builds the SIL application. The host library is linked to the SIL executable.
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.
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.
For details about configuring the blocks in this example, see Configure COM_AXIS Lookup Tables by Using Lookup Table and Breakpoint Objects.
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 theCodeReplacementLibrary
parameter to'AUTOSAR 4.0'
.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 theGenerateCodeReplacementReport
parameter to'on'
.Build the model and review the generated code for expected code replacements. For example, search the generated code for the routine prefix
Ifl
.
Required Algorithm Property Settings for IFL/IFX Function and Block Mappings
IFL/IFX Function and Block Mapping | Algorithm Property Parameters | Value |
---|---|---|
Prelookup | Extrapolation method
|
|
Index search method
|
| |
Use last breakpoint for input at or above upper limit
| On | |
Remove protection against out-of-range input in generated code
| Off | |
Integer rounding mode
|
| |
Interpolation Using Prelookup | Interpolation method
|
|
Extrapolation method
|
| |
Valid index input may reach last index
| On | |
Remove protection against out-of-range index in generated code
| Off | |
Integer rounding mode
|
| |
Interpolation Using Prelookup | Interpolation method
|
|
Extrapolation method
|
| |
Valid index input may reach last index
| On | |
Remove protection against out-of-range index in generated code
| Off | |
Integer rounding mode
|
| |
n-D Lookup Table | Interpolation method
|
|
Extrapolation method
|
| |
Index search method
|
| |
Use last table value for inputs at or above last breakpoint
| On | |
Remove protection against out-of-range input in generated code
| Off | |
Integer rounding mode
|
| |
n-D Lookup Table | Interpolation method
|
|
Extrapolation method
|
| |
Index search method
|
| |
Use last table value for inputs at or above last breakpoint
| On | |
Remove protection against out-of-range input in generated code
| Off | |
Integer rounding mode
|
| |
Prelookup | Extrapolation method
| Clip |
Index search method
|
| |
Use last breakpoint for input at or above upper limit
| On | |
Remove protection against out-of-range input in generated code
| Off | |
Integer rounding mode
|
| |
Interpolation Using Prelookup | Interpolation method
|
|
Extrapolation method
|
| |
Valid index input may reach last index
| On | |
Remove protection against out-of-range index in generated code
| Off | |
Integer rounding mode
|
| |
Interpolation Using Prelookup | Interpolation method
|
|
Extrapolation method
|
| |
Remove protection against out-of-range index in generated code
| Off | |
Integer rounding mode
|
| |
Valid index input may reach last index
| On | |
Interpolation Using Prelookup | Interpolation method
|
|
Extrapolation method
|
| |
Valid index input may reach last index
| On | |
Remove protection against out-of-range index in generated code
| Off | |
Integer rounding mode
|
| |
Interpolation Using Prelookup | Interpolation method
|
|
Extrapolation method
|
| |
Remove protection against out-of-range index in generated code
| Off | |
Integer rounding mode
|
| |
Valid index input may reach last index
| On | |
Interpolation Using Prelookup | Interpolation method
|
|
Extrapolation method
|
| |
Remove protection against out-of-range index in generated code
| Off | |
Integer rounding mode
|
| |
Valid index input may reach last index
| On | |
n-D Lookup Table | Interpolation method
|
|
Extrapolation method
|
| |
Index search method
|
| |
Use last table value for inputs at or above last breakpoint
| On | |
Remove protection against out-of-range input in generated code
| Off | |
Integer rounding mode
|
| |
n-D Lookup Table | Interpolation method
|
|
Extrapolation method
|
| |
Index search method
|
| |
Remove protection against out-of-range input in generated code
| Off | |
Integer rounding mode
|
| |
Use last table value for inputs at or above last breakpoint
| On | |
n-D Lookup Table | Interpolation method
|
|
Extrapolation method
|
| |
Index search method
|
| |
Use last table value for inputs at or above last breakpoint
| On | |
Remove protection against out-of-range input in generated code
| Off | |
Integer rounding mode
|
| |
Use last table value for inputs at or above last breakpoint
| On | |
Model configuration parameter Optimization > Default parameter behavior
|
| |
Breakpoint data should match power 2 spacing. | ||
n-D Lookup Table | Interpolation method
|
|
Extrapolation method
|
| |
Index search method
|
| |
Remove protection against out-of-range input in generated code
| Off | |
Integer rounding mode
|
| |
Model configuration parameter Optimization > Signals and Parameters > Default parameter behavior
|
| |
Breakpoint data must match power 2 spacing. | ||
n-D Lookup Table | Interpolation method
|
|
Extrapolation method
|
| |
Index search method
|
| |
Use last table value for inputs at or above last breakpoint
| On | |
Remove protection against out-of-range input in generated code
| Off | |
Integer rounding mode
|
| |
Breakpoint data must not match power 2 spacing. | ||
n-D Lookup Table | Interpolation method
|
|
Extrapolation method
|
| |
Index search method
|
| |
Remove protection against out-of-range input in generated code
| Off | |
Integer rounding mode
|
| |
Use last table value for inputs at or above last breakpoint
| On | |
Breakpoint data must not match power 2 spacing. | ||
n-D Lookup Table | Interpolation method
|
|
Extrapolation method
|
| |
Index search method
|
| |
Use last table value for inputs at or above last breakpoint
| On | |
Remove protection against out-of-range input in generated code
| Off | |
Integer rounding mode
|
| |
n-D Lookup Table | Interpolation method
|
|
Extrapolation method
|
| |
Index search method
|
| |
Remove protection against out-of-range input in generated code
| Off | |
Integer rounding mode
|
| |
Use last table value for inputs at or above last breakpoint
| On | |
n-D Lookup Table | Interpolation method
|
|
Extrapolation method
|
| |
Index search method
|
| |
Remove protection against out-of-range input in generated code
| Off | |
Integer rounding mode
|
| |
Use last table value for inputs at or above last breakpoint
| On | |
n-D Lookup Table | Interpolation method
|
|
Extrapolation method
|
| |
Index search method
|
| |
Use last table value for inputs at or above last breakpoint
| On | |
Remove protection against out-of-range input in generated code
| Off | |
Integer rounding mode
|
| |
Model configuration parameter Optimization > Signals and Parameters > Default parameter behavior
|
| |
Breakpoint data must match power 2 spacing. | ||
n-D Lookup Table | Interpolation method
|
|
Extrapolation method
|
| |
Index search method
|
| |
Remove protection against out-of-range input in generated code
| Off | |
Integer rounding mode
|
| |
Use last table value for inputs at or above last breakpoint
| On | |
Model configuration parameter Optimization > Signals and Parameters > Default parameter behavior
|
| |
Breakpoint data must match power 2 spacing. | ||
n-D Lookup Table | Interpolation method
|
|
Extrapolation method
|
| |
Index search method
|
| |
Remove protection against out-of-range input in generated code
| Off | |
Integer rounding mode
|
| |
Use last table value for inputs at or above last breakpoint
| On | |
Model configuration parameter Optimization > Signals and Parameters > Default parameter behavior
|
| |
Breakpoint data must match power 2 spacing. | ||
n-D Lookup Table | Interpolation method
|
|
Extrapolation method
|
| |
Index search method
|
| |
Use last table value for inputs at or above last breakpoint
| On | |
Remove protection against out-of-range input in generated code
| Off | |
Integer rounding mode
|
| |
Breakpoint data must not match power 2 spacing. | ||
n-D Lookup Table | Interpolation method
|
|
Extrapolation method
|
| |
Index search method
|
| |
Remove protection against out-of-range input in generated code
| Off | |
Integer rounding mode
|
| |
Use last table value for inputs at or above last breakpoint
| On | |
Breakpoint data must not match power 2 spacing. | ||
n-D Lookup Table | Interpolation method
|
|
Extrapolation method
|
| |
Index search method
|
| |
Remove protection against out-of-range input in generated code
| Off | |
Integer rounding mode
|
| |
Use last table value for inputs at or above last breakpoint
| On | |
Breakpoint data must not match power 2 spacing. |
Related Examples
More About
- What Is Code Replacement? (Embedded Coder)
- Code Generation