Documentation

Code Verification and Validation with PIL and External Mode

This example shows you how to use Embedded Coder Support Package for STMicroelectronics Discovery Boards for code verification and validation using PIL and External mode.

Introduction

In this example you will learn how to configure a Simulink model to run Processor-In-the-Loop (PIL) and External mode simulations. In a PIL simulation, the generated code runs on the STM32 Discovery boards. 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.

Simulink External mode enables parameter tuning and data logging while the model is running on the target.

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

  • Configure a Simulink model to run PIL simulations on the STM32F4-Discovery board or STM32F746G-Discovery board or STM32F769I-Discovery board

  • Configure a Simulink model to run External mode simulation

Prerequisites

We recommend completing Getting Started with Embedded Coder Support Package for STMicroelectronics Discovery Boards.

Required Hardware

To run this example you will need the following hardware:

  • STMicroelectronics STM32F4-Discovery board or STM32F746G-Discovery board or STM32F769I-Discovery board

  • USB type A to Mini-B cable

Serial communication for STM32F4-Discovery board:

  • USB TTL-232 cable - TTL-232R 3.3V Notes:

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

Note: On the STM32F746G-Discovery board and STM32F769I-Discovery board, you will use the serial over USB provided by the same USB connection needed for programming the board. So there is no need for an extra serial connection on the STM32F746G-Discovery board and STM32F769I-Discovery board.

Task 1 - Choose a Communication Interface for PIL Simulation

The Embedded Coder Support Package for STMicroelectronics Discovery Boards supports PIL.

The STM32F4-Discovery board supports two different communication interfaces for PIL: ST-LINK and serial. The ST-LINK communication interface does not require any additional cables or hardware besides a USB type A to Mini-B cable used to connect the STM32F4-Discovery board to the host computer. The serial communication interface requires a USB TTL-232 cable. Running a PIL simulation using the serial communication interface is much faster than the running a PIL simulation using ST-LINK. We recommend using the serial interface for PIL whenever possible. Unlike PIL, External mode simulations exclusively use the serial communication interface. Hence, you must have the required USB TTL-232 cable to run External mode simulations.

The STM32F746G Discovery board and STM32F769I-Discovery board supports PIL and External mode via the serial over USB connection marked as ST-LINK on the board. The serial communication interface does not require any additional hardware and needs a USB type A to Mini-B cable connected to the ST-LINK port on the board.

1. Choose a communication interface by following the steps below:

  • Open your model configured for code generation on a STM32F4-Discovery board. To work on the STM32F746G-Discovery board or STM32F769I-Discovery board change the target hardware on the Configuration Parameters > Hardware Implementation > Target Hardware pane. For STM32F4-Discovery, In Configuration Parameters > Hardware Implementation > Target Hardware Resources > PIL > PIL communication interface, select "ST-LINK" or "Serial (USART2)" to choose the communication method for PIL simulations.

  • As an example, you can check the settings in the PIL Block model as shown below.

For STM32F746G-Discovery or or STM32F769I-Discovery, In Configuration Parameters > Hardware Implementation > Target Hardware Resources > PIL > COM Port enter the COM port of the serial interface on Windows. Follow steps 3 and 4 to configure the COM port.

2. For STM32F4-Discovery, if you choose to use the serial communication interface, follow the hardware setup instructions below:

  • Connect ground pin of the USB TTL-232 cable to one of the GND pins on the STM32F4-Discovery board

  • Connect the RX pin of the USB TTL-232 cable to PA2 pin on the STM32F4-Discovery board

  • Connect the TX pin of the USB TTL-232 cable to PA3 pin on the STM32F4-Discovery board

  • Connect the USB side of the USB TTL-232 cable to your host computer

  • Power on your board by connecting a USB type A to Mini-B cable to the STM32F4-Discovery board

3. 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:

  • Open Devices and Printers in Windows®

  • Double-click the entry for your USB TTL-232 adapter device

  • In the device properties dialog, click the Hardware tab, and then click the Properties button

  • Click the Port Settings tab

4. Copy the COM port number to the Simulink model following the steps below:

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 STM32F4-Discovery target. You can run the model for STM32F746G-Discovery or STM32F769I-Discovery target, by changing the Hardware board to STM32F746G-Discovery or STM32F769I-Discovery in the Configuration Parameters > Hardware Implementation pane.

The objective here is to create a PIL block out of the Controller subsystem that you will run on the STM32 Discovery board.

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

3. Enable PIL by following Task 1 - Step 2 of the Code Verification and Validation with PIL example.

4. Create a PIL block for the Controller subsystem by following Task 1 - Step 3 of the Code Verification and Validation with PIL example.

5. Run PIL simulation by following Task 1 - Step 4 of the Code Verification and Validation with PIL example.

6. In case of STM32F4-Discovery, once you start the PIL simulation, a new OpenOCD session is launched. OpenOCD is used to download the code to the processor. If ST-LINK is selected as the PIL communication interface, OpenOCD will also be used to perform host to target communication during PIL simulations. On the STM32F746G-Discovery or STM32F769I-Discovery, the generated executable is copied to the drive letter in Windows of the board.

7. 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 STM32 Discovery 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 STM32F4-Discovery target. You can run the model for STM32F746G-Discovery or STM32F769I-Discovery target, by changing the Hardware board to STM32F746G-Discovery or STM32F769I-Discovery respectively in the Configuration Parameters > Hardware Implementation pane. The model contains two Model blocks that both point at the same referenced model. Note, that the Hardware board change has to be made in the reference model also by double clicking on counterA or counterB to open the refernced model and following the steps above. You will configure one of the Model blocks to run in PIL simulation mode and the other in normal mode.

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

3. Configure and run CounterA Model block in PIL simulation mode by following Task 2 - Step 2 of the Code Verification and Validation with PIL example.

4. When the model starts running, Scope1 displays the PIL simulation output running on the STM32 Discovery 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 STM32F4-Discovery target. You can run the model for STM32F746G-Discovery or STM32F769I-Discovery target, by changing the Hardware board to STM32F746G-Discovery or STM32F769I-Discovery respectively in the Configuration Parameters > Hardware Implementation pane.

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

3. Run the top model PIL simulation by following Task 3 - Step 2 of the Code Verification and Validation with PIL example.

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

Task 5 - External mode

In this task, you will run a model in External mode. When you are prototyping and developing algorithms, it is useful to monitor signals and tune parameters while the algorithms are running on the hardware. The Simulink External mode feature enables this capability.

The External mode simulation exclusively uses the serial communication interface. Hence, if you are using the STM32F4-Discovery, you must have the required USB TTL-232 cable to run External mode simulations.

1. Open the External mode model. This model is configured for the STM32F4-Discovery target. You can run the model for STM32F746G-Discovery or STM32F769I-Discovery target, by changing the Hardware board to STM32F746G-Discovery or STM32F769I-Discovery respectively in the Configuration Parameters > Hardware Implementation pane.

2. Configure the model for External mode simulation:

Specify the COM Port following the instructions in Task 1.

3. Configure the model for External mode simulation:

4. Start External mode simulation:

Once the model is loaded and running on the target, the External mode simulation will begin. Double click the Manual Switch block to change the input source. Double click on the Gain block to change the signal gain. Finally, double click on the Scope block to view the External mode simulation results. Note that the model is running in real-time on the target while performing these actions.

5. Stop External mode simulation:

Stopping the External mode simulation terminates the execution of the code running on the STM32 Discovery board.

Note: At any point during the course of the External mode simulation, you can open the External Mode Control Panel, using the following instructions. The External mode control panel provides more options like the ability to connect and disconnect to the target without terminating the execution of the generated code.

Summary

This example introduced code verification workflows using PIL and External mode.

Was this topic helpful?