Processor-in-the-Loop Verification of MATLAB Functions Using STMicroelectronics STM32 Processors
This example shows you how to use the Embedded Coder® Support Package for STMicroelectronics® STM32 Processors for Processor-in-the-Loop (PIL) verification of MATLAB® functions.
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 STMicroelectronics Nucleo-F401RE board, an ARM Cortex-M4 based microcontroller. 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
If you are new to Embedded Coder, visit the Embedded Coder product page for an overview and tutorials.
If you are new to code generation from MATLAB code, we recommend completing the Embedded Coder Capabilities for Code Generation from MATLAB Code.
Getting Started with STMicroelectronics STM32 Processor Based Boards
Required Hardware
STMicroelectronics Nucleo-F401RE board
USB type A to Mini-B cable, for ST-LINK
Create a New Folder and Copy Relevant Files
In this example, you are going to verify a MATLAB function that adds two inputs and returns output. Create a new folder and copy relevant files which required to verify on ARM Cortex-M4 hardware board..
Navigate to new folder and create a function file, simple_addition.m, containing a function that adds two inputs and returns the result.
To see the contents of this function:
type simple_addition
The %#codegen
directive in this function indicates that the MATLAB code is intended for code generation.
Generate PIL MEX Function from Command Line
The following steps provide commands to generate code and a library for a MATLAB function to verify on STM32 Nucleo-F401RE board.
Step 1: Create a coder.EmbeddedCodeConfig
object to generate code and create a library for MATLAB function simple_addition
.
config = coder.config('lib','ecoder',true);
Step 2: Configure the object for PIL.
config.VerificationMode = 'PIL';
Step 3: Specify the hardware on which the generated code to be verified.
config.Hardware = coder.hardware('STM32F4xx Based');
Step 4: Configure the stm32_MATLAB_PIL.ioc file. For more information, see Getting Started with STMicroelectronics STM32 Processor Based Boards
Step 5: Choose the PIL communication interface.
To choose 'ST-LINK'
as PIL communication interface, below is the step:
config.Hardware.PILInterface = 'ST-LINK';
To choose 'Serial (USART2)'
as PIL communication interface, below are the steps.
config.Hardware.PILInterface = 'Serial (USART2)'; config.Hardware.PILCOMPort = 'COM28';
Provide the PILCOMPort
of your machine. The com port provided is just an example and might vary depending on individual machine. To see the list of available COM ports on your computer, select Start > Control Panel > Device Manager > Ports (COM & LPT)
Step 6: 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;
Step 7: You can enable verbose build to view the build log on command line.
config.Verbose = 1;
Step 8: Generate library code for the simple_addition
MATLAB function and the PIL interface.
inp = single(zeros(1,30)); codegen('-config', config, '-args', {inp,inp}, 'simple_addition');
In above commands, inp
declares the data type and size for input arguments to MATLAB function 'simple_addition'. The codegen
command generates code into following folders
codegen\lib\simple_addition - Standalone code for
simple_addition
.codegen\lib\simple_addition\pil - PIL interface code for
simple_addition
.
Also, this step creates simple_addition_pil
PIL 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.
Note: You can also use the build_pil_mex function to generate the PIL mex function for any MATLAB function. The required input arguments are:
1. fcName - Function name for which code is to be generated
2. Inputs - Inputs declaring the data type and size for input arguments as a cell array.
3. hwName - Name of the Hardware board
Eg: To generate code for simple_addition function for STM32 Nucleo-F401RE hardware :
inp = single(zeros(1,30)); build_pil_mex('simple_addition',{inp,inp},'STM32F4xx Based');
You can also specify the COM port and baud rate by passing them as name and value pairs. Default PIL COM port is COM28 and PIL baud rate is 115200.
build_pil_mex('simple_addition',{inp,inp},'STM32F4xx Based', 'PILCOMPort','COM6', 'PILBaudRate','9600');
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.
clear simple_addition_pil;
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))
Profile Generated Code
To enable code execution profiling, set CodeExecutionProfiling
of the coder.EmbeddedCodeConfig
coder configuration object to true
before generating code for the MATLAB function in Step 7 of Generate PIL MEX Function from Command Line.
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.
To accumulate profiling results, run simple_addition function 100 times in a loop:
for k=1:100, y = simple_addition_pil(u1,u2); end
The profiling results are available after clearing the PIL MEX function:
clear simple_addition_pil
Bring up the profiling report:
ProfileResultsWithoutCRL = getCoderExecutionProfile('simple_addition'); report(ProfileResultsWithoutCRL)
Use ARM Cortex-M Code Replacement Library (CRL)
To take advantage of optimized CRL for ARM Cortex-M processors, assign CodeReplacementLibrary
library 'ARM Cortex-M (CMSIS)'
and re-build the PIL MEX function following Step 7 of Generate PIL MEX Function from Command Line:
config.CodeReplacementLibrary = 'ARM Cortex-M (CMSIS)';
Note: If using build_pil_mex function, you can enable CRL replacements by specifying enableCRL as true .
build_pil_mex('simple_addition',{inp,inp},'STM32F4xx Based', 'PILCOMPort','COM6', 'PILBaudRate','9600','enableCRL', true) ;
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
The profiling results are available after clearing the PIL MEX function:
clear simple_addition_pil
Bring up the profiling report:
ProfileResultsWithCRL = getCoderExecutionProfile('simple_addition'); report(ProfileResultsWithCRL)
Compare the profiling results to those obtained in Profile Generated Code.