MATLAB Examples

Code Verification and Validation with PIL

This example shows you how to use Simulink Coder Support Package for NXP FRDM-KL25Z Board for code verification and validation using PIL. To run this model you need to have Embedded Coder Toolbox.

Contents

Introduction

In this example you will learn how to configure a Simulink model to run Processor-In-the-Loop (PIL) simulation. In a PIL simulation, the generated code runs on the NXP FRDM-KL25Z Board. The results of the PIL simulation are transferred to Simulink to verify the numerical equivalence of the simulation and the code generation results. The PIL verification process is a crucial part of the development cycle to ensure that the behavior of the deployment code matches the design.

This example introduces the Simulink code generation and verification workflow by showing you how to:

  • Configure a Simulink model to run Model Block PIL simulations on the NXP FRDM-KL25Z board.
  • Configure a Simulink model to run Top-Model PIL simulations on the NXP FRDM-KL25Z board.
  • Configure a Simulink model to run PIL Block simulations on the NXP FRDM-KL25Z board.

Prerequisites

We recommend completing Getting Started with Simulink Coder Support Package for NXP FRDM-KL25Z Board.

Required Products

Embedded Coder

Required Hardware

To run this example you will need the following hardware:

On Mac:

  • NXP FRDM-KL25Z board
  • USB type A to Mini-B cable
  • USB TTL-232 cable - TTL-232R 3.3V

On Windows:

  • NXP FRDM-KL25Z board
  • USB type A to Mini-B cable
  • USB TTL-232 cable - TTL-232R 3.3V (This is required only when you use UART1 or UART2 serial communication interface)

Note:

  • This example is tested with the FTDI Friend USB TTL-232R 3.3V adapter.

OS Specific Limitations

On Mac:

External mode simulation is supported only through UART1 or UART2.

On Windows:

External mode simulation is supported through all UART communication interfaces.

Task 1 - Choose a Serial Communication Interface for PIL Simulation

The Simulink Coder Support Package for NXP FRDM-KL25Z Board supports three different serial communication interfaces for PIL: UART0, UART1, and UART2. Please note that, on Mac platforms, PIL simulation over UART0 is supported only on Mac version 'El Capitan'.

The UART0 serial communication interface is accessible through either the mini USB port labeled with openSDA on the NXP FRDM-KL25Z board or the onboard GPIO pins for UART0 . Using UART0 via the mini USB port does not require any additional cables or hardware, besides a USB type A to Mini-B cable that is used to connect the NXP FRDM-KL25Z board to the host computer. To use the UART0 through the other GPIO pins on NXP FRDM-KL25Z board, additional hardware, for example, a USB TTL-232R adapter.

The UART1 and UART2 serial communication interfaces are accessible only through pins on the NXP FRDM-KL25Z board. Using these interfaces requires additional hardware, for example, a USB TTL-232R adapter, to perform PIL simulations.

1. Choose a serial communication interface using the steps below:

  • Open your model configured for code generation on a NXP FRDM-KL25Z board. Select UART0 or UART1 or UART2 in Configuration Parameters > Hardware Implementation > Target Hardware Resources > PIL > Select hardware UART to choose the serial communication interface.
  • As an example, see the settings in the PIL Block model as shown below.

2. Choose the Tx and Rx GPIO pins for the selected serial communication interface:

  • Once the serial communication interface is selected, choose the pins for the selected UART in Configuration Parameters > Hardware Implementation > Target Hardware Resources > 'Selected Target UART in step 2 above'.
  • As an example for UART1, see the settings in the PIL Block model as shown below.

3. Connect to the hardware. Follow the steps below based on the serial interface you selected:

For UART0:

  • Connect a USB cable from your computer to the OpenSDA mini-B USB connector of the NXP FRDM-KL25Z Board.
  • If the TX Pin selected for UART0 is either PTE20 or PTD7, connect the RX pin of the USB TTL-232R adapter to the selected TX pin on the NXP FRDM-KL25Z board. However, if the TX pin selected for UART0 is PTA2 (USBTX), then the USB cable from your computer to the OpenSDA mini-B USB connector of the NXP FRDM-KL25Z Board is enough.
  • If the RX Pin selected for UART0 is either PTE21 or PTD6, connect the TX pin of the USB TTL-232R adapter to the selected RX pin on the NXP FRDM-KL25Z board. However, if the RX pin selected for UART0 is PTA1 (USBRX), then the USB cable from your computer to the OpenSDA mini-B USB connector of the NXP FRDM-KL25Z Board is enough.

For UART1:

  • Connect a USB cable from your computer to the OpenSDA mini-B USB connector of the NXP FRDM-KL25Z Board.
  • Connect ground pin of the USB TTL-232R adapter to one of the GND pins on the NXP FRDM-KL25Z board
  • Connect the RX pin of the USB TTL-232R adapter to the TX pin, selected in Configuration Parameters for UART1, on the NXP FRDM-KL25Z board
  • Connect the TX pin of the USB TTL-232R adapter to the RX pin, selected in Configuration Parameters for UART1, on the NXP FRDM-KL25Z board
  • Connect the USB side of the USB TTL-232R adapter to your host computer

For UART2:

  • Connect a USB cable from your computer to the OpenSDA mini-B USB connector of the NXP FRDM-KL25Z Board.
  • Connect ground pin of the USB TTL-232R adapter to one of the GND pins on the NXP FRDM-KL25Z board
  • Connect the RX pin of the USB TTL-232R adapter to the TX pin, selected in Configuration Parameters for UART2, on the NXP FRDM-KL25Z board
  • Connect the TX pin of the USB TTL-232R adapter to the RX pin, selected in Configuration Parameters for UART2, on the NXP FRDM-KL25Z board
  • Connect the USB side of the USB TTL-232R adapter to your host computer

4. Once you complete the steps above, a new serial COM port should be available for use on your host computer. To find the COM port associated with your adapter cable, follow the steps below:

On Windows:

  • Open Device Manager
  • Expand the Ports Tab
  • If you have chosen UART1 or UART2 for serial PIL communication interface, note down the COM port associated with USB TTL-232R Adapter.
  • If you have chosen UART0 for serial external mode communication interface, and selected PTA2(USBTX) and PTA1(USBRX) for the TX and RX pins respectively, note down the OpenSDA COM port associated with NXP FRDM-KL25Z board by following steps described in the section Install Drivers for NXP FRDM-KL25Z Board.
  • If you have chosen UART0 for serial external mode communication interface, and the TX and RX pins selected for the same are other than PTA2(USBTX) and PTA1(USBRX) respectively, note down the COM port associated with USB TTL-232R Adapter.

On Mac:

  • Run ls /dev/cu.* command in a Terminal window.
  • Note down the serial port name associated with the USB TTL-232R Adapter.

5. Open your model configured for code generation on a NXP FRDM-KL25Z board. In Configuration Parameters > Hardware Implementation > Target Hardware Resources > PIL > Serial port, enter the Serial port field with the COM port number noted in the previous step.

For example, on Windows enter COM27 in the Serial port field, while on Mac enter /dev/cu.usbmodem1442.

Task 2 - Verify the generated code for a subsystem using a PIL block

This example shows how to use a PIL block for subsystem code verification. With this approach:

  • You can verify the code generated for a subsystem
  • You must provide a test harness model to supply a test vector or stimulus inputs
  • You must swap your original subsystem with a generated PIL block; you should be careful to avoid saving your model in this state as you would lose your original subsystem

1. Open the PIL Block model. This model is configured for the NXP FRDM-KL25Z target. The objective here is to create a PIL block out of the Controller subsystem that you will run on the NXP FRDM-KL25Z board.

2. Choose a PIL communication interface by following the steps in Task 1 above.

3. Follow the below steps to enable PIL:

4. Follow the below steps to create a PIL block for the Controller subsystem:

5. Follow the below steps to run the PIL simulation:

6. You can switch between the original and PIL block subsystems by double clicking on the Manual Switch block. Double click on the Numerical Differences block to see the difference between the simulated Controller subsystem and the PIL block running on the NXP FRDM-KL25Z board.

Task 3 - Verify referenced model code using PIL

This example shows how to verify the generated code for a referenced model by running a PIL simulation. With this approach:

  • You can verify code generated for referenced models
  • You must provide a test harness model to provide a test vector or stimulus inputs
  • You can easily switch a Model block between normal and PIL simulation mode

1. Open the Model Block PIL model. This model is configured for NXP FRDM-KL25Z target. The model contains two Model blocks that both point at the same referenced model. You will configure one of the Model blocks to run in PIL simulation mode and the other in normal mode.

2. Choose a PIL serial communication interface for the Model Block PIL and Referenced model by following the steps in Task 1 above.

3. Follow the below steps to configure and run the CounterA Model block in PIL mode:

4. When the model starts running, Scope1 displays the PIL simulation output running on the NXP FRDM-KL25Z board while Scope2 shows the normal mode simulation output.

Task 4 - Verify top model code using PIL

This example shows how to verify the generated code for a model by running a PIL simulation. With this approach:

  • You can verify code generated for a top model
  • You must configure the model to load test vectors or stimulus inputs from the MATLAB workspace
  • You can easily switch the entire model between normal and PIL simulation mode

1. Open the Top Model PIL model. This model is configured for the Freecale FRDM-KL25Z target.

2. Choose a PIL serial communication interface by following the steps in Task 1 above.

3. Follow the below steps to run the top model PIL simulation:

4. When the PIL simulation is completed, a logsOut variable is created in the base workspace. The logsOut data contains PIL simulation results. You can access the logged data for signals count_a and count_b using the following commands:

  • count_a = get(logsOut,'count_a');
  • count_a.Values.Data
  • count_b = get(logsOut,'count_b');
  • count_b.Values.Data

Summary

This example introduced code verification workflows using PIL