Documentation

Code Verification and Validation with External Mode

This example shows you how to use Simulink Coder Support Package for NXP FRDM-KL25Z for code verification and validation using External mode.

Introduction

In this example you will learn how to configure a Simulink model to run a simulation in External mode.

Simulink's External mode feature enables you to accelerate the process of parameter tuning by letting you change certain parameter values while the model is running on target hardware, without stopping the model. When you change parameter values from within Simulink, the modified parameter values are communicated to the target hardware immediately. The effects of the parameters tuning activity may be monitored by viewing algorithm signals on scopes or displays in Simulink.

Prerequisites

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

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 - External Mode

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

The Simulink Coder Support Package for NXP FRDM-KL25Z Board supports three different serial communication interfaces for External mode: UART0, UART1, and UART2. Please note that, on Mac platforms, External mode 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 GPIO pins on NXP FRDM-KL25Z board, additional hardware is required as mentioned below for the UART1 and UART2 serial communication interfaces.

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 External mode simulations.

1. Open the External mode model. This model has already been configured for the NXP FRDM-KL25Z target. To learn how to setup a model for this target please see the Getting Started with NXP FRDM-KL25Z Support Package example.

2. 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 > External mode > Select hardware UART to choose the serial communication interface.

  • As an example, see the settings in the External mode model as shown below.

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

  • After 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 External mode model as shown below.

4. 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), the USB cable from your computer the OpenSDA mini-B USB connector of the NXP FRDM-KL25Z Board is sufficient.

  • 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 the OpenSDA mini-B USB connector of the NXP FRDM-KL25Z Board is sufficient.

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

5. 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 selected UART1 or UART2 for serial external mode communication interface, note down the COM port associated with USB TTL-232R Adapter.

  • If you have selected 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.

6. Open your model configured for code generation on a NXP FRDM-KL25Z board. In Configuration Parameters > Hardware Implementation > Target Hardware Resources > External mode > 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.

7. Set Baud rate : Click on the selected UART group and enter the baud rate as shown below.

8. Click Apply and close the configuration parameters window.

9. Select External from the simulation mode drop down list present in the model's toolbar.

10. Click on the Run button to start the External mode.

Wait for the model to finish getting built and downloaded onto the target. Once this is done, the External mode simulation will begin. Double click the Manual Switch block while simulation is running 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 entire model is running on the target.

11. Stop External mode simulation:

Stopping External mode simulation terminates code running on the NXP FRDM-KL25Z. Before you can start another External mode simulation you need to run the generated code again. You can re-run the generated code by simply pressing the reset button on the NXP FRDM-KL25Z.

Things to remember while setting up the model to run in External mode

  • Make sure that COM port number entered in the External mode pane is correct.

  • In the Model explorer, go to the Code -> External Mode Control Panel and click on the 'Signal and Triggering' button. The default value of Duration parameter under Trigger Options Section is 1000. You need to change this value to 5 as the memory on the target is not enough to store 1000 data values. For more information on Duration parameter, refer External Signal Uploading and Triggering.

  • For UART0, the recommended baud rates for external mode communication are 9600, 19200 or 57600.

Summary

This example introduced the workflow for code verification using External mode.

Was this topic helpful?