User Guide Topics

External Mode enables real-time parameter tuning and signal monitoring on running hardware through a bidirectional communication link between Simulink and the target device. While your embedded application is running, you can modify parameter values and observe their effects immediately without rebuilding or reprogramming.

Overview

Key Capabilities:

  • Modify parameters in real-time without rebuilding or reprogramming
  • Monitor signals and scopes while application runs on hardware
  • Capture data from hardware to Simulink workspace
  • Compare simulation vs. hardware behavior side-by-side
  • Start/stop target execution from Simulink interface
  • Trigger data capture based on signal conditions
๐Ÿ’ก Typical Use Case: A motor control engineer uses External Mode to tune PID controller gains while the motor is running. Each parameter change takes effect immediately, allowing real-time observation of motor response without stopping the motor or rebuilding code.

Communication Protocol: XCP

External Mode uses the industry-standard XCP (Universal Measurement and Calibration Protocol) for communication between Simulink and the target microcontroller.

XCP Features:

  • Standardized protocol originally developed for automotive applications
  • Efficient binary data format minimizes communication overhead
  • Supports multiple transport layers (Serial, TCP/IP, CAN)
  • Synchronization with model execution timing
  • Parameter upload (Simulink โ†’ hardware) and signal download (hardware โ†’ Simulink)

Supported Transport Layers:

TransportHardware InterfaceTypical SpeedUse Case
XCP on SerialUART (RS-232)115200 - 460800 baudMost common, simplest setup
XCP on TCP/IPEthernet10-100 MbpsHigh-speed applications, many signals
XCP on USBUSB CDC (Virtual COM)Up to 12 MbaudHigh-speed with USB-enabled boards

Prerequisites

Before enabling External Mode, your model must have:

  1. Communication peripheral configured: A UART block (for Serial/USB) or Ethernet block (for TCP/IP)
  2. Communication pins connected: TX/RX pins routed to physical hardware connector
  3. Communication cable: USB cable, FTDI adapter, or Ethernet cable connecting PC to target
โš ๏ธ Hardware Connection Required: External Mode requires a working communication link. Verify UART or Ethernet is functional before enabling External Mode. Test with a simple loopback or echo program first.

Configuration Steps

  1. Open Model Configuration Parameters (Ctrl+E)
  2. Navigate to: Code Generation โ†’ Interface
  3. Check “External mode” checkbox
  4. Select transport layer:
    • XCP on Serial: For UART/USB communication
    • XCP on TCP/IP: For Ethernet communication

Step 2: Configure Communication Parameters

For XCP on Serial (UART):

  • Baud rate: 115200, 230400, 460800, or higher (must match UART block setting)
  • COM port: Auto-detected or manually specified (e.g., COM3)
  • Data bits: 8
  • Stop bits: 1
  • Parity: None

For XCP on TCP/IP (Ethernet):

  • Target IP address: Static IP of your microcontroller (e.g., 192.168.1.100)
  • Port: 17725 (default XCP port)
  • Timeout: 10 seconds (default)

Step 3: Configure UART Peripheral (if using Serial)

Ensure your model includes a UART block configured for External Mode:

  1. Add a UART block from the MCHP Blockset library
  2. Configure UART settings:
    • Baud rate: Must match External Mode setting
    • TX/RX pins: Assign to physical pins connected to PC
  3. Do not use this UART for other purposes (External Mode owns the peripheral)
๐Ÿ’ก Dedicated UART Recommendation: Use a dedicated UART peripheral for External Mode. Sharing UART with application logging or other communication can cause data corruption and connection failures.

Step 4: Build and Program

  1. Build the model (Ctrl+B)
    • External Mode communication code is automatically included
  2. Program the target microcontroller via your programmer (ICD, PICkit, etc.)
  3. Connect communication cable between PC and target board

Step 5: Connect and Monitor

In Simulink:

  1. Click “Connect to Target” button in toolbar (or press Ctrl+K)
  2. Wait for connection confirmation (status bar shows “Connected”)
  3. Click “Start Real-Time Code” (or press Ctrl+T)
  4. Observe signals in Simulink scopes updating in real-time

To modify parameters:

  1. Double-click a tunable parameter or constant block
  2. Adjust the value using the slider or numeric entry
  3. Changes take effect immediately on hardware
  4. Observe the effect in real-time scopes

Making Parameters Tunable

By default, most parameters are compiled as constants and cannot be modified at runtime. To enable real-time tuning:

Method 1: Block Parameter

  1. Right-click the block parameter (e.g., Gain block’s “Gain” parameter)
  2. Select “Tune Parameter…”
  3. Check “Tunable” checkbox
  4. Optional: Set min/max limits for safety
  5. Rebuild the model

Method 2: Constant Block

  1. Use a Constant block instead of hardcoding values
  2. Right-click the Constant block โ†’ Properties
  3. Check “Tunable”
  4. Rebuild the model
โš ๏ธ Safety Limits: When tuning parameters for motor control or safety-critical applications, always set appropriate min/max limits. This prevents accidental entry of dangerous values (e.g., excessive PWM duty cycle, negative speed commands).

Performance Considerations

External Mode adds communication overhead to your embedded application:

AspectImpactRecommendation
CPU Overhead5-15% depending on logged signalsMinimize number of logged signals
Communication Latency10-100ms depending on transportUse TCP/IP or USB for lower latency than slow UART
Maximum Sample RateLimited by communication bandwidthSerial @ 115200 baud: ~1 kHz max
Serial @ 460800 baud: ~5 kHz max
Ethernet/USB: ~100 kHz max
Memory UsageAdditional RAM for communication buffers~2-8 KB depending on configuration

Detailed External Mode Guide

For comprehensive information including PIL testing, timing analysis, and advanced External Mode features, see:

๐Ÿ“š External Mode & PIL Testing Guide

This detailed guide covers:

  • External Mode vs PIL comparison
  • Numerical verification procedures
  • Timing analysis and profiling
  • Best practices for development and commissioning
  • Troubleshooting connection issues

Video Tutorial

๐Ÿ“น Watch: Getting Started with External Mode

Learn External Mode setup and usage in this video demonstration:

๐Ÿ”— External Mode Video Tutorial

The video covers:

  • Enabling External Mode in Simulink
  • Configuring UART communication
  • Building and connecting to hardware
  • Real-time parameter tuning demonstration
  • Monitoring signals on scopes

Troubleshooting

Problem: Connection Timeout

Symptom: Simulink shows “Cannot connect to target” after clicking Connect.

Solutions:

  1. Verify communication cable: Check physical connection between PC and target
  2. Check COM port: Ensure correct COM port is selected in External Mode settings
  3. Verify UART configuration: Confirm UART block baud rate matches External Mode setting
  4. Check target is running: Toggle an LED to verify code is executing
  5. Try different COM port: Sometimes Windows assigns different port number

Problem: Connection Drops During Operation

Symptom: Connection works initially but drops randomly during tuning.

Solutions:

  1. Reduce number of logged signals: Too many signals saturate communication bandwidth
  2. Increase UART baud rate: Try 230400 or 460800 instead of 115200
  3. Check for electrical noise: Poor cable quality or EMI can corrupt data
  4. Disable USB power saving: Windows may put USB ports to sleep (Power Management โ†’ uncheck “Allow computer to turn off this device”)

Problem: Parameters Not Updating

Symptom: Changing parameter value in Simulink has no effect on hardware.

Solutions:

  1. Verify parameter is tunable: Right-click parameter โ†’ check “Tunable” is enabled
  2. Rebuild model: Parameter tunability is set at compile time, requires rebuild
  3. Check parameter is not optimized away: Some unused parameters may be eliminated by compiler
  4. Verify connection is active: Status bar should show “Connected” and “Running”

Problem: Baud Rate Mismatch

Symptom: Garbled data, connection timeouts, or random disconnects.

Solution:

  • Ensure baud rates match exactly:
    • External Mode settings (Model Configuration Parameters)
    • UART block configuration in Simulink model
    • Physical UART peripheral register settings (auto-generated, but verify in code)

Problem: UART Already in Use

Symptom: Build fails with “UART peripheral already configured” or similar error.

Solution:

  • External Mode requires dedicated UART: Remove any other UART blocks or UART communication code
  • Use different UART: If application needs UART for logging, use a second UART peripheral for External Mode

Home | User Guide | Code Generation | PIL Testing | Compiler Setup | Video Demos | Block Reference