Navigation
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:
| Transport | Hardware Interface | Typical Speed | Use Case |
|---|
| XCP on Serial | UART (RS-232) | 115200 - 460800 baud | Most common, simplest setup |
| XCP on TCP/IP | Ethernet | 10-100 Mbps | High-speed applications, many signals |
| XCP on USB | USB CDC (Virtual COM) | Up to 12 Mbaud | High-speed with USB-enabled boards |
Prerequisites
Before enabling External Mode, your model must have:
- Communication peripheral configured: A UART block (for Serial/USB) or Ethernet block (for TCP/IP)
- Communication pins connected: TX/RX pins routed to physical hardware connector
- 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
Step 1: Enable External Mode in Simulink
- Open Model Configuration Parameters (
Ctrl+E) - Navigate to: Code Generation โ Interface
- Check “External mode” checkbox
- Select transport layer:
- XCP on Serial: For UART/USB communication
- XCP on TCP/IP: For Ethernet communication
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)
Ensure your model includes a UART block configured for External Mode:
- Add a UART block from the MCHP Blockset library
- Configure UART settings:
- Baud rate: Must match External Mode setting
- TX/RX pins: Assign to physical pins connected to PC
- 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
- Build the model (
Ctrl+B)- External Mode communication code is automatically included
- Program the target microcontroller via your programmer (ICD, PICkit, etc.)
- Connect communication cable between PC and target board
Step 5: Connect and Monitor
In Simulink:
- Click “Connect to Target” button in toolbar (or press
Ctrl+K) - Wait for connection confirmation (status bar shows “Connected”)
- Click “Start Real-Time Code” (or press
Ctrl+T) - Observe signals in Simulink scopes updating in real-time
To modify parameters:
- Double-click a tunable parameter or constant block
- Adjust the value using the slider or numeric entry
- Changes take effect immediately on hardware
- 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
- Right-click the block parameter (e.g., Gain block’s “Gain” parameter)
- Select “Tune Parameter…”
- Check “Tunable” checkbox
- Optional: Set min/max limits for safety
- Rebuild the model
Method 2: Constant Block
- Use a Constant block instead of hardcoding values
- Right-click the Constant block โ Properties
- Check “Tunable”
- 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).
External Mode adds communication overhead to your embedded application:
| Aspect | Impact | Recommendation |
|---|
| CPU Overhead | 5-15% depending on logged signals | Minimize number of logged signals |
| Communication Latency | 10-100ms depending on transport | Use TCP/IP or USB for lower latency than slow UART |
| Maximum Sample Rate | Limited by communication bandwidth | Serial @ 115200 baud: ~1 kHz max Serial @ 460800 baud: ~5 kHz max Ethernet/USB: ~100 kHz max |
| Memory Usage | Additional 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:
- Verify communication cable: Check physical connection between PC and target
- Check COM port: Ensure correct COM port is selected in External Mode settings
- Verify UART configuration: Confirm UART block baud rate matches External Mode setting
- Check target is running: Toggle an LED to verify code is executing
- 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:
- Reduce number of logged signals: Too many signals saturate communication bandwidth
- Increase UART baud rate: Try 230400 or 460800 instead of 115200
- Check for electrical noise: Poor cable quality or EMI can corrupt data
- 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:
- Verify parameter is tunable: Right-click parameter โ check “Tunable” is enabled
- Rebuild model: Parameter tunability is set at compile time, requires rebuild
- Check parameter is not optimized away: Some unused parameters may be eliminated by compiler
- 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