Main Content

Processor-in-the-Loop Verification of MATLAB Functions

This example shows you how to use Embedded Coder® Support Package for ARM Cortex®-M Processors for Processor-in-the-Loop (PIL) verification of MATLAB® functions on the QEMU ARM Cortex-M3 emulator.

Introduction

In this example you will learn how to generate a PIL MEX function from a simple MATLAB function. When you run the PIL MEX function, the C-code generated from your MATLAB function runs on the QEMU ARM Cortex-M3 emulator. The results are transferred to MATLAB for numerical verification. During this process, you can profile the code execution. The PIL verification process is a crucial part of the design cycle to ensure that the behavior of the deployment code matches the design.

Prerequisites

Task 1 - Create a New Folder and Copy Relevant Files

The following code creates a folder in your current working folder. The new folder will only contain the files that are relevant for this example. If you do not want to affect the current folder (or if you cannot generate files in this folder), you should change your working folder.

codertarget.arm_cortex_m.demo_setup('simple_addition');

In this example, we use thesimple_addition.mfunction that simply adds two inputs and returns the result. To see the contents of this function:

type simple_addition

The%#codegendirective in this function indicates that the MATLAB code is intended for code generation.

Task 2 - Generate PIL MEX Function from Command Line

1:创建一个coder.EmbeddedCodeConfigobject to generate code and create a library for MATLAB functionsimple_addition.

config = coder.config('lib','ecoder',true);

2:Configure the object for PIL.

config.VerificationMode ='PIL';

3:Specify the hardware on which the generated code to be verified.

config.Hardware = coder.hardware('ARM Cortex-M3 (QEMU)');

4:Limit the stack size to reasonable size, for example 512 bytes, as default size is much larger than the memory available on the hardware.

config.StackUsageMax = 512;

5:You can enable verbose build to view the build log on command line.

config.Verbose = 1;

6:Generate library code for thesimple_additionMATLAB function and the PIL interface.

inp = single(zeros(1,30)); codegen('-config', config,'-args',{inp,inp},'simple_addition');

In above commands,inpdeclares the data type and size for input arguments to MATLAB function 'simple_addition'. Thecodegencommand generates code into following folders

  • codegen\lib\simple_addition - Standalone code forsimple_addition.

  • codegen\lib\simple_addition\pil - PIL interface code forsimple_addition.

Also, this step createssimple_addition_pilPIL MEX function in the current folder. This allows you to test the MATLAB code and the PIL MEX function and compare the results between both.

Task 3 - Run the PIL MEX Function

Run the PIL MEX function to compare its behavior to that of the original MATLAB function and to check for run-time errors.

u1 = single(rand(1,30)); u2 = single(rand(1,30)); y = simple_addition_pil(u1,u2);

Terminate PIL execution with the following command.

clearsimple_addition_pil;

Task 4 - Verify Generated Code

To verify the numerical accuracy of the generated code, compare MATLAB results with those of the PIL MEX function:

norm(y - simple_addition(u1,u2))

Task 5 - Profile Generated Code

1.To enable code execution profiling, setCodeExecutionProfilingof thecoder.EmbeddedCodeConfigcoder configuration object totruebefore generating code for the MATLAB function inStep 6of Task 2.

config.CodeExecutionProfiling = true;

When profiling is enabled, the generated code is instrumented with timing information. The profiling results are transferred to MATLAB when the PIL MEX function is cleared from memory.

2.To accumulate profiling results, run simple_addition function 100 times in a loop:

for k=1:100, y = simple_addition_pil(u1,u2); end

3.The profiling results are available after clearing the PIL MEX function:

明确simple_addition_pil

4.Bring up the profiling report:

分析器esultsWithoutCRL = getCoderExecutionProfile('simple_addition'); report(ProfileResultsWithoutCRL)

Task 6 - Use 'ARM Cortex-M' Code Replacement Library (CRL)

1.To take advantage of optimized CRL for ARM Cortex-M processors, execute this command and then re-build the PIL MEX function followingStep 6of Task 2:

config.CodeReplacementLibrary = 'ARM Cortex-M (CMSIS)';

2.Run the simple_addition function 100 times in a loop to accumulate profiling results:

for k=1:100, y = simple_addition_pil(u1,u2); end

3.The profiling results are available after clearing the PIL MEX function:

明确simple_addition_pil

4.Bring up the profiling report:

分析器esultsWithCRL = getCoderExecutionProfile('simple_addition'); report(ProfileResultsWithCRL)

Compare the profiling results to those obtained inTask 5.

Summary

This example introduced the workflow for code verification of MATLAB functions using PIL.