Main Content

Create PIL Target Connectivity Configuration for Simulink

Target Connectivity Configurations for PIL

Use target connectivity configurations and the target connectivity API to customize processor-in-the-loop (PIL) simulation for your target environments.

Through a target connectivity configuration, you specify:

  • A configuration name for a target connectivity API implementation.

  • Settings that define the set of compatible Simulink® models. For example, the set of models that have a particular system target file, template makefile, and hardware implementation.

A PIL simulation requires a target connectivity API implementation that integrates third-party tools for:

  • Cross-compiling generated code, creating the PIL application that runs on the target hardware.

  • Downloading, starting, and stopping the application on the target.

  • Communicating between Simulink and the target.

You can have many different target connectivity configurations for PIL simulation. Register a connectivity configuration with Simulink by creating an sl_customization.m file and placing it on the MATLAB® search path.

When you run a PIL simulation, the software determines which of the available connectivity configurations to use. The software looks for a connectivity configuration that is compatible with the model under test. If the software finds multiple or no compatible connectivity configurations, the software generates an error message with information about resolving the problem.

Create a Target Connectivity API Implementation

This diagram shows the components of the PIL target connectivity API.

You must provide implementations of the three API components:

  • Build API — Specify the Simulink Coder™ toolchain or template makefile approach for building generated code.

  • Launcher API — Control how Simulink starts and stops the PIL executable.

  • Communications API — Customize connectivity between Simulink and the PIL target. Embedded Coder® provides host-side support for TCP/IP and serial communications, which you can adapt for other protocols.

These steps outline how you create a target connectivity API implementation. The example code shown in the steps is taken from ConnectivityConfig.m in Configure Processor-In-The-Loop (PIL) for a Custom Target.

  1. Create a subclass of rtw.connectivity.Config.

    ConnectivityConfig < rtw.connectivity.Config

  2. In the subclass:

    • Instantiate rtw.connectivity.MakefileBuilder, which configures the build process.

      builder = rtw.connectivity.MakefileBuilder(componentArgs, ...
                                                 targetApplicationFramework, ...

    • Create a subclass of rtw.connectivity.Launcher, which downloads and executes the application using a third-party tool.

      launcher = mypil.Launcher(componentArgs, builder);

  3. Configure your rtiostream API implementation of the host-target communications channel.

    • For the target side, you must provide the driver code for communications, for example, TCP/IP or serial communications. To integrate this code into the build process, create a subclass of rtw.pil.RtIOStreamApplicationFramework.

    • For the host side, you can use a supplied library for TCP/IP or serial communications. Instantiate rtw.connectivity.RtIOStreamHostCommunicator, which loads and initializes the library that you specify.

      hostCommunicator = rtw.connectivity.RtIOStreamHostCommunicator(componentArgs, ...
                                                                     launcher, ...

  4. If you require execution-time profiling for generated code, create a timer object that provides details of the hardware-specific timer and associated source files. For more information, see Specify Hardware Timer for Simulink.

  5. If you require stack usage profiling for generated code, specify a driver implementation that obtains stack usage data from the target hardware. The driver must return the value of the stack register. If you do not specify a driver, the PIL simulation tries to use a default generic driver. For more information, see Implement Driver to Obtain Stack Usage Data During PIL Simulation.


Each time you modify a connectivity implementation, close and reopen the models to refresh them.

Register a Connectivity API Implementation

To register a target connectivity API implementation as a target connectivity configuration in Simulink:

  1. Create or update an sl_customization.m file. In this file:

    • Create a target connectivity configuration object that specifies, for example, the configuration name for a target connectivity API implementation and compatible models.

    • Invoke registerTargetInfo.

  2. Add the folder containing sl_customization.m to the search path and refresh your customizations.


For more information, see rtw.connectivity.ConfigRegistry.

Verify Target Connectivity Configuration

To verify your target connectivity configuration early on and independently of your model development and code generation, use the supplied piltest function. With the function, you can run a suite of tests. In the tests, the function runs various normal, SIL, and PIL simulations. The function compares results and produces errors if it detects differences between simulation modes.

Target Connectivity API Examples

For step-by-step examples, see:

  • Configure Processor-In-The-Loop (PIL) for a Custom Target

    This example shows you how to create a custom PIL implementation using the target connectivity APIs. You can examine the code that configures the build process to support PIL, a downloading and execution tool, and a communication channel between host and target. To activate a full host-based PIL configuration, follow the steps in the example.

  • Create a Target Communication Channel for Processor-in-the-Loop (PIL) Simulation

    This example shows you how to implement a communication channel for use with the Embedded Coder product and your embedded target. This communication channel enables exchange of data between different processes. PIL simulation requires exchange of data between the Simulink software running on your development computer and deployed code executing on target hardware.

    The rtiostream interface provides a generic communication channel that you can implement in the form of target connectivity drivers for a range of connection types. The example shows how to configure your own target-side driver for TCP/IP, to operate with the default host-side TCP/IP driver. The default TCP/IP communications allow high-bandwidth communication between host and target, which you can use for transferring data such as video.


    If you customize the rtiostream TCP/IP implementation for your PIL simulations, you must turn off Nagle's algorithm for the server side of the connection. If Nagle's algorithm is not turned off, your PIL simulations can run at a significantly slower speed. The matlabroot/toolbox/coder/rtiostream/src/rtiostreamtcpip/rtiostream_tcpip.c file shows how you can turn off Nagle's algorithm:

    /* Disable Nagle's Algorithm*/
    option = 1;
    sockStatus = setsockopt(lFd,IPPROTO_TCP,TCP_NODELAY,(char*)&option,sizeof(option));  
    The code for your custom TCP/IP implementation can require modification.

    The example also shows how to implement custom target connectivity drivers, for example, using serial, CAN, or USB for both host and target sides of the communication channel.

See Also

| | | | | |

Related Topics