The Scheduler Options block configures the behavior of the built-in Rate Monotonic Scheduler used for multitasking in MCHP Blockset models. The scheduler manages execution of multiple tasks at different sample rates with preemptive priority-based scheduling. Rate Monotonic Scheduling Principle: - Highest rate = Highest priority - Faster tasks automatically get higher priority - Preemptive execution - Higher priority tasks interrupt lower priority tasks - Deterministic behavior - Guaranteed timing if schedulability conditions are met - Automatic priority assignment - No manual priority configuration needed
Rate Monotonic Scheduler Architecture
Task Priority Assignment
| Sample Time | Priority Level | Can Preempt | Example Use |
|---|
| Base rate (fastest) | 40 (highest) | All slower tasks | Fast current loop (50 µs) |
| 2à base rate | 30 | Slower tasks | Speed controller (100 µs) |
| 10à base rate | 20 | Even slower tasks | Position controller (500 µs) |
| 100Ć base rate | 10 | Background tasks | Communication (5 ms) |
| Triggered subsystems | Configurable | Based on config | Interrupt-driven tasks |
Block Parameters
Task Overload Behavior
| Option | Behavior | Use Case | Risk |
|---|
| Queue the task | (queue length is 1) | ⢠If task is still running when next trigger occurs, queue ONE additional execution | ⢠Task executes twice back-to-back when current run completes |
| Skip this task execution once | ⢠If task is running, skip the triggered execution | ⢠Resume normal execution at next trigger | ⢠Overload does not accumulate |
| Delay all new tasks | until end of overload | ⢠All task triggers are blocked during overload | ⢠Tasks resume when overloaded task completes |
Overload Behavior Comparison
// Task with 10 ms period, execution takes 12 ms (overload)
Option 1: Queue (queue length = 1)
t=0ms: Task starts
t=10ms: Trigger ā QUEUED (1 pending)
t=12ms: Task ends ā immediately starts queued execution
t=20ms: Trigger ā LOST (queue full)
t=24ms: Task ends
t=30ms: Normal execution resumes
Option 2: Skip once
t=0ms: Task starts
t=10ms: Trigger ā SKIPPED
t=12ms: Task ends
t=20ms: Normal execution
t=30ms: Normal execution
Option 3: Delay all
t=0ms: Task starts
t=10ms: Trigger ā DELAYED
t=12ms: Task ends ā delayed trigger executes immediately
t=24ms: Task ends (ran at t=12)
t=30ms: Back to normal (with 2ms delay accumulated)
Multitasking Configuration
Simulink Configuration Requirements
- Solver Type: Fixed-step (required for code generation)
Configuration Parameters ā Solver
Type: Fixed-step
Solver: discrete (no continuous states)
- Sample Times: Define task rates as integer multiples
Base rate: 50e-6 % 50 µs (20 kHz)
Task 1: 100e-6 % 2Ć base rate
Task 2: 500e-6 % 10Ć base rate
Task 3: 5e-3 % 100Ć base rate
- Tasking Mode: Enable multitasking in configuration
Configuration Parameters ā Code Generation ā Interface
Multi-instance code: off
Single output/update function: off
ā ļø Let MCHP blockset manage tasking automatically
Scheduling Algorithm
// Simplified scheduler pseudocode
execute_task(highest);
mark_complete(highest);
ready_tasks = check_triggered_tasks();
}
}
Examples
Example 1: Motor Control Application
% Three-level motor control hierarchy
% Base rate: 50 µs, Overload: Queue
Sample times:
- Current loop: 50e-6 (Priority 40 - highest)
- Speed loop: 500e-6 (Priority 30)
- Position loop: 5e-3 (Priority 20)
- Communication: 100e-3 (Priority 10 - lowest)
Scheduler Options:
TaskOverloadBehaviour: 'Queue the task (queue length is 1)'
% Result: Current loop can preempt all others
% Brief overloads are queued and caught up
Example 2: Data Acquisition System
% Robust data logging with guaranteed timing
% Base rate: 100 µs, Overload: Delay all
Sample times:
- Fast ADC: 100e-6 (Priority 40)
- Slow sensors: 10e-3 (Priority 30)
- Data logging: 100e-3 (Priority 20)
- Display update: 500e-3 (Priority 10)
Scheduler Options:
TaskOverloadBehaviour: 'Delay all new tasks until end of overload'
% Result: No samples lost, timing may shift temporarily
Example 3: Communication System
% Non-critical monitoring allows missed samples
% Base rate: 1 ms, Overload: Skip
Sample times:
- Protocol handler: 1e-3 (Priority 40)
- Data processing: 10e-3 (Priority 30)
- Status update: 100e-3 (Priority 20)
Scheduler Options:
TaskOverloadBehaviour: 'Skip this task execution once'
% Result: Skipped status updates acceptable
Schedulability Analysis
Liu & Layland Utilization Bound
For Rate Monotonic Scheduling, the system is schedulable if:
U = Σ(Cᵢ/Tᵢ) ⤠n(2^(1/n) - 1)
Where:
U = Total CPU utilization
Cįµ¢ = Execution time of task i
Tįµ¢ = Period of task i
n = Number of tasks
For n tasks:
n=1: U ⤠100%
n=2: U ⤠82.8%
n=3: U ⤠78.0%
nāā: U ⤠69.3%
Example Calculation
Task 1: Cā=20µs, Tā=50µs ā Uā = 20/50 = 0.40 (40%)
Task 2: Cā=80µs, Tā=500µs ā Uā = 80/500 = 0.16 (16%)
Task 3: Cā=1ms, Tā=10ms ā Uā = 1/10 = 0.10 (10%)
Total: U = 0.66 (66%)
Bound for n=3: 0.78 (78%)
ā System is schedulable (66% < 78%)
Advanced Topics
Interrupt-Driven Tasks
Tasks triggered by hardware interrupts (using [MCHP_Interrupt] block) have configurable priorities independent of sample time:
// Interrupt priority can override rate monotonic assignment
Interrupt priority: 1-7 (device-dependent)
Multiplied by -100 for scheduler priority
Example: Priority 7 interrupt ā scheduler priority -700
(higher than any rate monotonic task)
Context Switching Overhead
| Device Family | Context Switch Time | Notes |
|---|
| dsPIC30F/33F | ~100 cycles | Register save/restore |
| dsPIC33E/C/A | ~80 cycles | Optimized context save |
| PIC32 (MIPS) | ~150 cycles | Shadow register set available |
| SAM (ARM) | ~20 cycles | Hardware-assisted context switch |
Troubleshooting
| Problem | Cause | Solution |
|---|
| Tasks executing out of order | Incorrect sample time configuration | Verify all sample times are integer multiples of base rate |
| Timing jitter observed | Overload with Queue option | Change to Skip or reduce task execution time |
| System becomes unresponsive | Overload with Delay option causing cascade | Reduce CPU load or change to Skip option |
| Data loss in logging | Skip option losing samples | Change to Queue or reduce overload frequency |
See Also
- [MCHP_Master] - Main configuration block
- [MCHP_Interrupt] - Interrupt-driven task configuration
- [MCHP_MCU_LOAD] - CPU utilization monitoring
- [MCHP_MCU_OVERLOAD] - Overload detection
- [MCHP_IdleTask] - Background task execution