Main Content

RunPolyspaceAnalysis on Code Generated withEmbedded Coder

If you generate code from a Simulink®model by using Embedded Coder®or TargetLink®, you can analyze the generated code for bugs or run-time errors with Polyspace®from within the Simulink environment. You do not have to manually set up a Polyspace project.

This topic uses Embedded Coder for code generation. For analysis of TargetLink-generated code, seeRun Polyspace Analysis on Code Generated with TargetLink.

For a tutorial with a specific model, seeRun Polyspace Analysis on Code Generated from Simulink Model.

Prerequisites

Before you run Polyspace from Simulink, you must link your Polyspace and MATLAB®installations. SeeIntegrate Polyspace with MATLAB and Simulink.

Generate and Analyze Code

Configure Code Generation and Generate Code

To configure code generation and generate code from a model, doone of the following:

  • On theAppstab, selectEmbedded Coder. Then, on theC Codetab, selectQuick Start. Follow the on-screen instructions.

  • On theC Codetab, clickSettingsand configure code generation through Simulink configuration parameters. The chief parameters to set are:

    • Type(Simulink): SelectFixed-step.

    • Solver(Simulink): Selectauto (Automatic solver selection)orDiscrete (no continuous states).

    • System target file(Simulink Coder): Enterert.tlcorautosar.tlc. If you derive target files fromert.tlc, you can also specify them.

    • Code-to-model(Embedded Coder): Select this option to enable links from code to model.

    For the full list of parameters to set, seeRecommended Model Configuration Parameters for Polyspace Analysis.

    Alternatively, run the Code Generation Advisor with the objectivePolyspaceand see if the required parameters are already set. SeeConfigure Model for Code Generation Objectives by Using Code Generation Advisor(Embedded Coder).

    To generate code from the model, on theC Codetab, selectGenerate Code. You can follow the progress of code generation in the Diagnostic Viewer.

Configure Code Analysis

On theAppstab, selectPolyspace Code Verifier. On thePolyspacetab:

  1. Select the product to run:Bug FinderorCode Prover.

  2. SelectSettings. If needed, change default values of these options.

    • Settings from: Enable checking of MISRA®coding rules in addition to the default checks specified in the project configuration. The default Bug Finder checks look for bugs. The default Code Prover checks look for run-time errors.

    • Input,Tunable parametersandOutput: Constrain inputs, tunable parameters, or outputs for a more precise Code Prover analysis.

    • Output folder: Specify a dedicated folder for results. The default analysis saves the results in a folderresults_modelNamein the current working folder.

    • Open results automatically after verification

    For the full list of options to set, seeCode Prover Analysis in Simulink.

Analyze Code

To analyze the code generated from the model, click anywhere on the canvas. TheAnalyze Code fromfield shows the model name. SelectRun Analysis.

When using Embedded Coder, Polyspace checks for generated code when you clickRun Analysis. If no generated code is present or if the model has changed since the last Polyspace analysis, Polyspace first launches the code generation process and then starts the analysis.

If the current model is referenced in another model and you want to verify the generated code in the context where the model is referenced, instead ofCode Generated as Top Model, useCode Generated as Model Reference. In the latter case, Polyspace does not launch code generation automatically if there's no generated code. When analyzingCode Generated as Model Reference, generate code before running the Polyspace analysis.

You can follow the progress of the analysis in the MATLAB Command Window.

The results open automatically unless explicitly disabled. By default, the results are saved in a folderresults_ModelNamein the current folder. Each new run overwrites previous results. You can change the default folders or save the results to a Simulink project. To make these changes, on thePolyspacetab, selectSettings.

If you have closed the results and want to open them later, on thePolyspacetab, selectAnalysis Results. To open a result prior to the last run, selectOpen Earlier Resultsand navigate to the folder containing the previous results.

Review Analysis Results

Review Results in Code

The results appear in the Polyspace user interface on theResults Listpane. Click each result to see the source code on theSourcepane and details on theResult Detailspane. See also:

Navigate from Code to Model

Links in code comments show blocks that generate the subsequent lines of code. To see the blocks in the model, click the block names in the links. If you encounter issues, seeTroubleshoot Navigation from Code to Model.

Alternatively, you can right-click a variable name and selectGo to Model. This option is not available for all variables. Only a subset of source code variables can be directly traced to a Simulink block. TheGo to Modeloptions is available for such a variable. For more details on which variables in generated code can be traced to Simulink blocks, seeTrace Simulink Model Elements in Generated Code(Embedded Coder).

Fix Issue

调查是否重新在您的代码的问题lated to design flaws in the model.

Design flaws in the model can lead to issues in the generated code. For instance:

  • The generated code might be free of specific run-time errors only for a certain range of a block parameter. To fix this issue, you can change the storage class of that block parameter or use calibration data for the analysis by using the configuration parameterTunable parameters.

  • The generated code might be free of specific run-time errors only for a certain range of inputs. To determine this error-free range, you can specify a minimum and maximum value for the Inport block signals. The Polyspace analysis uses this constrained range. SeeWork with Signal Ranges in Blocks(Simulink).

  • Certain transitions in Stateflow®charts can be unreachable.

If you include handwritten C/C++ code in S-function blocks, the Polyspace analysis can reveal possible integration issues between the handwritten and generated code. You can also analyze the handwritten code in isolation. SeeRun Polyspace Analysis on S-Function Code.

Annotate Blocks to Justify Issues

You might want to justify some Polyspace results without modifying the code or the model. Annotate Simulink blocks either from the Polyspace user interface or the Simulink editor. SeeAddress Polyspace Results by Annotating Simulink Blocks.

Related Topics