Main Content

Generate Optimized Code on Raspberry Pi Target

This example shows how to generate optimized code for the resample function that can be deployed onto a Raspberry Pi® hardware board target (ARM®-based device) using processor-in-the-loop (PIL) execution. For more details, see the SIL/PIL Manager Verification Workflow documentation.

Prerequisites

Create Code Generation Configuration Object

Create a code generation configuration object for a static library. Set the VerificationMode property to 'PIL' and the TargetLang property to 'C++'.

cfg = coder.config('lib');
cfg.VerificationMode = 'PIL';
cfg.TargetLang = 'C++';

Create Connection to Raspberry Pi

Use the MATLAB Support Package for Raspberry Pi Support Package function, raspi, to create a connection to the Raspberry Pi. In this line of code, replace:

  • raspiname with the host name of your Raspberry Pi

  • username with your user name

  • password with your password

r = raspi('raspiname','username','password');

Create Hardware Board Configuration Object

Create a hardware board configuration object for Raspberry Pi. Set the Hardware property of the code generation configuration object to the coder.hardware object.

hw = coder.hardware('Raspberry Pi');
cfg.Hardware = hw;

Generate Source C++ Code

Create random input data to feed to the resample function.

rng(0);
a = rand(100000,1);

Generate optimized C++ code using the codegen function. Specify interpolation and decimation factors as 1 and 6, respectively.

% codegen('doResample.m','-config','cfg','-args','{a, 1, 6}');

Compare the results generated by the MATLAB resample function and the generated MEX PIL function.

cg_out = doResample_pil(a,1,6);
### Starting application: 'codegen\lib\doResample\pil\doResample.elf'
    To terminate execution: clear doResample_pil
### Launching application doResample.elf...
ml_out = doResample(a,1,6);
max(cg_out-ml_out)
ans = 9.9920e-16

See Also

Functions