Main Content

Analyze Code and Test Software-in-the-Loop

Code Analysis and Testing Software-in-the-Loop Overview

You can analyze code to detect errors, check standards compliance, and evaluate key metrics such as length and cyclomatic complexity. For handwritten code, you typically check for run-time errors with static code analysis and run test cases that evaluate the code against requirements and evaluate code coverage. Based on the results, you refine the code and add tests.

In this example, you generate code and demonstrate that the code execution produces equivalent results to the model by using the same test cases and baseline results. Then you compare the code coverage to the model coverage. Based on test results, add tests and modify the model to regenerate code.

Workflow for code analysis and software-in-the-loop testing

Analyze Code for Defects, Metrics, and MISRA C:2012

This workflow describes how to check if your model produces MISRA™ C:2012 compliant code and how to check your generated code for code metrics and defects. To produce more MISRA compliant code from your model, you use the code generation and Model Advisor. To check whether the code is MISRA compliant, you use the Polyspace® MISRA C:2012 checker and report generation capabilities. For this example, you use the model simulinkCruiseErrorAndStandardsExample. To open the model:

  1. Open the project.

    pr = openProject("SimulinkVerificationCruise");

  2. From the project, open the model simulinkCruiseErrorAndStandardsExample.

    The simulinkCruiseErrorAndStandardsExample shows the Compute target speed subsystem, which has 5 inputs and 2 outputs.

Run Code Generator Checks

Check your model by using the Code Generation Advisor. Configure code generation parameters to generate code more compliant with MISRA C and more compatible with Polyspace.

  1. Right-click Compute target speed and select C/C++ Code > Code Generation Advisor.

  2. Select the Code Generation Advisor folder. In the right pane, move Polyspace to Selected objectives - prioritized. The MISRA C:2012 guidelines objective is already selected.

    Code Generation Objectives dialog box

  3. Click Run Selected Checks.

    The Code Generation Advisor checks whether the model includes blocks or configuration settings that are not recommended for MISRA C:2012 compliance and Polyspace code analysis. For this model, the check for incompatible blocks passes, but some configuration settings are incompatible with MISRA compliance and Polyspace checking.

    Code Generation Advisor results

  4. Click the check that did not pass. Accept the parameter changes by selecting Modify Parameters.

  5. Rerun the check by selecting Run This Check.

Run Model Advisor Checks

Before you generate code from your model, use the Model Advisor to check your model for MISRA C and Polyspace compliance. This example shows you how to use the Model Advisor to check your model before generating code.

  1. At the bottom of the Code Generation Advisor window, select Model Advisor.

  2. Under the By Task folder, select the Modeling Standards for MISRA C:2012 advisor checks.

  3. Click Run Checks and review the results.

  4. If any of the tasks fail, make the suggested modifications and rerun the checks until the MISRA modeling guidelines pass.

Generate and Analyze Code

After you have done the model compliance checking, you can generate the code. With Polyspace, you can check your code for compliance with MISRA C:2012 and generate reports to demonstrate compliance with MISRA C:2012.

  1. In the Simulink® editor, right-click Compute target speed and select C/C++ Code > Build This Subsystem.

  2. Use the default settings for the tunable parameters and select Build.

  3. After the code is generated, in the Simulink Editor, right-click Compute target speed and select Polyspace > Options.

  4. Click Configure to choose more advanced Polyspace analysis options in the Polyspace configuration window.

    Polyspace configuration dialog box

  5. On the left pane, click Coding Standards & Code Metrics, then select Calculate Code Metrics to enable code metric calculations for your generated code.

  6. Save and close the Polyspace configuration window.

  7. From your model, right-click Compute target speed and select Polyspace > Verify > Code Generated For Selected Subsystem.

    Polyspace Bug Finder™ analyzes the generated code for a subset of MISRA checks. You can see the progress of the analysis in the MATLAB® Command Window. After the analysis finishes, the Polyspace environment opens.

Review Results

The Polyspace environment shows you the results of the static code analysis.

  1. Expand the tree for rule 8.7 and click through the different results.

    Rule 8.7 states that functions and objects should not be global if the function or object is local. As you click through the 8.7 violations, you can see that these results refer to variables that other components also use, such as CruiseOnOff. You can annotate your code or your model to justify every result. Because this model is a unit in a larger program, you can also change the configuration of the analysis to check only a subset of MISRA rules.

    Polyspace Bug Finder dialog box

  2. In your model, right-click Compute target speed and select Polyspace > Options.

  3. Set the Settings from option to Project configuration to choose a subset of MISRA rules in the Polyspace configuration.

  4. Click Configure.

  5. In the Polyspace window, on the left pane, click Coding Standards & Code Metrics. Then select Check MISRA C:2012 and, from the drop-down list, select single-unit-rules. Now Polyspace checks only the MISRA C:2012 rules that are applicable to a single unit.

  6. Save and close the Polyspace configuration window.

  7. Rerun the analysis with the new configuration.

    The rules Polyspace showed previously were found because the model was analyzed by itself. When you limited the rules Polyspace checked to the single-unit subset, Polyspace found only two violations.

    Code coverage analysis dashboard

When you integrate this model with its parent model, you can add the rest of the MISRA C:2012 rules.

Generate Report

To demonstrate compliance with MISRA C:2012 and report on your generated code metrics, you must export your results. If you want to generate a report every time you run an analysis, see Generate report (Polyspace Bug Finder).

  1. If they are not open already, open your results in the Polyspace environment.

  2. From the toolbar, select Reporting > Run Report.

  3. Select BugFinderSummary as your report type.

  4. Click Run Report.

    The report is saved in the same folder as your results.

  5. To open the report, select Reporting > Open Report.

Test Code Against Model Using Software-in-the-Loop Testing

You previously showed that the model functionality meets its requirements by running test cases based on those requirements. Now run the same test cases on the generated code to show that the code produces equivalent results and fulfills the requirements. Then compare the code coverage to the model coverage to see the extent to which the tests exercised the generated code.

  1. In MATLAB, in the project window, open the tests folder, then open SILTests.mldatx. The file opens in the Test Manager.

  2. Review the test case. On the Test Browser pane, navigate to SIL Equivalence Test Case. This equivalence test case runs two simulations for the simulinkCruiseErrorAndStandardsExample model using a test harness.

    • Simulation 1 is a model simulation in normal mode.

    • Simulation 2 is a software-in-the-loop (SIL) simulation. For the SIL simulation, the test case runs the code generated from the model instead of running the model.

    The equivalence test logs one output signal and compares the results from the simulations. The test case also collects coverage measurements for both simulations.

  3. Run the equivalence test. Select the test case and click Run.

  4. Review the results in the Test Manager. In the Results and Artifacts pane, select SIL Equivalence Test Case to see the test results. The test case passed and the results show that the code produced the same results as the model for this test case.

    Test Manager showing passed test cases and model and code coverage results.

  5. Expand the Coverage Results section of the results. The coverage measurements show the extent to which the test case exercised the model and the code. When you run multiple test cases, you can view aggregated coverage measurements in the results for the whole run. Use the coverage results to add tests and meet coverage requirements, as shown in Perform Functional Testing and Analyze Test Coverage.

You can also test the generated code on your target hardware by running a processor-in-the-loop (PIL) simulation. By adding a PIL simulation to your test cases, you can compare the test results and coverage results from your model to the results from the generated code as it runs on the target hardware. For more information, see Code Verification Through Software-in-the-Loop and Processor-in-the-Loop Execution (Embedded Coder).

Related Topics