Run Polyspace Analysis on Custom Code in Simulink Models
If you implement algorithms in your Simulink® model by using custom C/C++ code, you can analyze the custom code directly from the Simulink toolstrip without manually setting up a Polyspace® project. The behavior of the custom code in your model depends on the model context, such as number and nature of input and design range specification. When you run Polyspace analysis from MATLAB® or Simulink, the analysis takes the model context into account. When running a Polyspace analysis of the custom code outside of MATLAB/Simulink, specify the model context manually, for instance, by using options files.
A Polyspace analysis of the custom code has different goals and configurations compared to a Polyspace analysis of the generated code:
Generated Code Analysis | Custom Code Analysis |
---|---|
Analyzes the code in a C Caller, C Function, or S-Function block in isolation. | Analyzes the code generated from the entire model. |
Detects issues in the custom code that can cause bugs or run-time errors in a Simulink simulation. | Detects issues in the total generated code that might cause bugs or run-time errors when deployed to an embedded system. |
The target settings for Polyspace is compatible with a Simulink simulation. | The Target processor type settings for Polyspace is the same as the Hardware Implementation settings specified in the Configuration Parameters dialog box in Simulink. |
Prerequisite
Before you run Polyspace with Simulink, link your Polyspace and MATLAB installations. See Integrate Polyspace with MATLAB and Simulink or Integrate Polyspace Server Products with MATLAB.
Analyze Custom Code
You can implement custom algorithm by using different Simulink custom code blocks, such as:
C Function: See Integrate External C/C++ Code into Simulink Using C Function Blocks (Simulink)
C Caller: See Integrate C Code Using C Caller Blocks (Simulink)
S-Function: See Author Blocks Using C MEX S-Functions (Simulink)
These blocks have different functionalities. See Comparison of Custom Block Functionality (Simulink).
Specify Configuration
Before running Polyspace on a Simulink model, configure the Simulink model to be compatible with Polyspace.
To analyze custom code in Polyspace, select Import custom code in the Configuration Parameters dialog box, on the Simulation Target pane.
If the included custom code does not compile, the Polyspace analysis might fail. Before starting the Polyspace analysis, check the custom code for compilation issues. The C function block does not support including header files. For this block, specify the include statements in the Simulation Target pane. For the code included in C Caller and S Function blocks, specify the include statements in the source file. Polyspace has stricter code and compilation requirements than Simulink and your custom code might fail Polyspace compilation even though your model simulation produces expected results.
Start Polyspace Analysis
Start the Polyspace analysis of custom code in your model in the Simulink Editor or in the MATLAB Command Window.
For more information about running a Polyspace analysis on custom code in a S function block, see Run Polyspace Analysis on S-Function Code.
For more information about running a Polyspace analysis on custom code in a C Caller block, see Run Polyspace Analysis on Custom Code in C Caller Blocks and Stateflow Charts.
For more information about running a Polyspace analysis on custom code in a C function block, see Run Polyspace Analysis on Custom Code in C Function Block.
Once the analysis starts, Polyspace extracts the custom code from the model. To preserve the design range specification and nature of the inputs, Polyspace assumes each instance of a custom code block in a top model has a unique model context and treats the blocks as unique. When a model containing a custom code block is referenced multiple times in another top model, the model context of the custom code blocks remain the same. Polyspace treats the custom code block in different instances of the referenced model as a single custom code instance.
After extracting the code and model context, Polyspace analyzes them as handwritten code. See Bug Finder Analysis Assumptions.
Review Analysis Results
In the Simulink Editor, click Analysis Results. The Polyspace User Interface opens with the analysis results. The flagged issues appear in the Results List pane. See also:
To fix the flagged issues, modify the code. For more information, see Fix Identified Issues. Alternatively, modify the Simulink model to resolve the Polyspace results. See Fix Issues.
If a flagged issue is known or justified, then annotate that information in the custom code blocks. You can annotate the custom code blocks directly from the Polyspace User Interface. See Annotate Blocks to Justify Results.