Processor-in-the-Loop (PIL) testing verifies that code generated from your Simulink model produces correct results when executed on actual target hardware. PIL runs your algorithm on the real microcontroller and compares the output with a reference simulation, providing both numerical verification and performance analysis.
PIL testing bridges the gap between simulation and real-world deployment by:
๐ก Why PIL is Critical: PIL catches errors that simulation alone cannot detect:
**Fixed-point quantization errors**: Hardware arithmetic vs. simulation floating-point
**Compiler optimization side effects**: Code may behave differently when optimized
**Hardware-specific behavior**: Peripheral timing, interrupt latency, memory access
**Performance bottlenecks**: Actual execution time vs. theoretical analysis
| Feature | PIL Testing | External Mode |
|---|---|---|
| Purpose | Verification & performance analysis | Real-time tuning & debugging |
| When to Use | Before production release | During development & commissioning |
| Code Modification | No modification (tests production code) | Adds communication overhead |
| Performance Impact | None (measured without overhead) | 5-15% CPU overhead |
| Hardware Connection | Programmer (ICD/PICkit) during test | Serial/Ethernet required during runtime |
| Test Automation | Fully automated | Manual interaction |
| Outputs | Pass/fail verification, timing data | Real-time plots, tuned parameters |
PIL testing provides two types of critical verification:
Compares hardware execution results with simulation reference to detect:
Pass Criteria:
| Data Type | Acceptable Error | Notes |
|---|---|---|
double (64-bit float) | < 1e-14 | Rounding errors only |
| @@PROT_9@@ (32-bit float) | < 1e-6 | Rounding errors only |
| @@PROT_10@@ / @@PROT_11@@ | 0 (exact match) | Must match exactly |
| @@PROT_12@@ / @@PROT_13@@ | 0 or ยฑ1 LSB | Check for acceptable quantization |
| Fixed-point | Application-dependent | Verify against requirements |
Measures actual execution time on target hardware:
Example Timing Report:
@@PROT_1@@
A typical PIL test model includes:
[Test Inputs] โ [PIL Block (Hardware)] โ [Output]
โ โ
[Timing Data] [Compare]
โ
[Reference Model] โโโโโโโโโโโ [Error]
โ
[Assertion]
(Fail if error > threshold)
Model Components:
Ctrl+T) in the test modelPASS: Hardware output matches simulation within tolerance
Max error: 3.2e-15 (double precision rounding)
Status: โ
PASS - Code generation verified correct
FAIL: Hardware output differs significantly from simulation
Max error: 0.045 (45 LSB in 16-bit fixed-point)
Status: โ FAIL - Investigate:
โข Fixed-point word length
โข Overflow protection
โข Scaling factors
โข Integer division rounding
PASS: Maximum execution time is less than sample period
Sample period: 1000 ฮผs (1 kHz)
Max execution: 650 ฮผs
Margin: 350 ฮผs (35%)
Status: โ
PASS - Meets real-time deadline
FAIL: Execution time exceeds deadline
Sample period: 1000 ฮผs (1 kHz)
Max execution: 1200 ฮผs
Status: โ FAIL - Deadline missed by 200 ฮผs
Solutions:
โข Enable compiler optimization (-O2 or -O3)
โข Simplify algorithm or reduce computation
โข Increase sample period (reduce execution rate)
โข Use faster microcontroller
If PIL timing fails (execution time exceeds deadline):
1. Enable Compiler Optimization
Use the Compiler Options Block
to enable -O2 optimization (free license) or -O3 (PRO license).
2. Profile to Find Bottlenecks
Use Timer PIL Profiling blocks on individual subsections to identify slow code paths.
3. Algorithm Optimization
4. Code Replacement Library
Enable assembly-optimized math functions in model configuration (Code Generation โ Interface โ Code replacement library).
5. Upgrade Hardware
Consider a faster microcontroller (e.g., dsPIC33E โ dsPIC33C โ dsPIC33A).
Phase 1: Algorithm Development
1. Develop algorithm in Simulink (pure simulation)
2. Verify functionality with simulation tests
Phase 2: PIL Verification
3. Create PIL test model with reference comparison
4. Run PIL test for numerical verification
5. If FAIL: debug fixed-point, scaling, overflow issues
Phase 3: Performance Validation
6. Add Timer PIL Profiling blocks
7. Measure execution time
8. If timing FAIL: optimize (see above)
Phase 4: Hardware Deployment
9. Deploy to hardware with External Mode for tuning
10. Final PIL test with tuned parameters
11. Disable External Mode and build production code
For comprehensive information including External Mode comparison, timing analysis strategies, and advanced PIL workflows, see:
๐ External Mode & PIL Testing Guide
This detailed guide covers:
Home | User Guide | Code Generation | External Mode | Compiler Setup | Video Demos | Block Reference