Bird
Raised Fist0
ARM Architectureknowledge~10 mins

PendSV and SysTick exceptions in ARM Architecture - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - PendSV and SysTick exceptions
SysTick Timer Counts Down
↓
SysTick Exception Triggered
↓
SysTick Handler Executes
↓
PendSV Exception Set Pending
↓
PendSV Exception Triggered
↓
PendSV Handler Executes
↓
Context Switch or Deferred Task
↓
Return
SysTick counts down and triggers its exception, which can set PendSV pending. PendSV runs later to handle context switching or deferred tasks.
Execution Sample
ARM Architecture
void SysTick_Handler() {
  // Set PendSV pending
  SCB->ICSR |= SCB_ICSR_PENDSVSET_Msk;
}

void PendSV_Handler() {
  // Perform context switch
  // Context switching code here
}
SysTick handler sets PendSV pending; PendSV handler performs context switch.
Analysis Table
StepEventActionException TriggeredHandler ExecutedResult
1SysTick timer reaches zeroSysTick exception triggeredSysTickSysTick_HandlerPendSV exception set pending
2PendSV exception pendingPendSV exception triggered after SysTickPendSVPendSV_HandlerContext switch performed
3Context switchedReturn to new task contextNoneNoneSystem continues with new task
💡 PendSV handler completes context switch; no more exceptions pending
State Tracker
VariableStartAfter Step 1After Step 2Final
SysTick_CountNon-zeroZero (trigger)ZeroReloaded after interrupt
PendSV_PendingFalseTrue (set by SysTick)False (handled)False
Current_TaskTask ATask ATask B (switched)Task B
Key Insights - 2 Insights
Why does SysTick set PendSV pending instead of doing the context switch directly?
SysTick_Handler sets PendSV pending to defer the context switch to a lower priority handler, avoiding interrupt nesting and ensuring safe switching (see execution_table step 1 and 2).
When exactly does PendSV execute after being set pending?
PendSV executes after all higher priority exceptions finish, here after SysTick_Handler completes (see execution_table step 2).
Visual Quiz - 3 Questions
Test your understanding
According to the execution_table, what happens immediately after SysTick exception triggers?
ASystem returns to main task
BPendSV exception is set pending
CContext switch is performed
DSysTick timer reloads
💡 Hint
Look at execution_table row 1, 'Result' column
At which step does the actual context switch occur?
AStep 2
BStep 1
CStep 3
DAfter Step 3
💡 Hint
Check execution_table 'Handler Executed' and 'Result' columns
If PendSV was not set pending by SysTick, what would change in the variable_tracker?
ASysTick_Count would not reach zero
BCurrent_Task would switch earlier
CPendSV_Pending would remain False after Step 1
DNo change in variables
💡 Hint
See variable_tracker row for PendSV_Pending after Step 1
Concept Snapshot
PendSV and SysTick exceptions work together for task switching.
SysTick triggers regularly and sets PendSV pending.
PendSV runs later at lower priority to switch tasks safely.
This avoids nested interrupts and keeps system stable.
SysTick counts down; PendSV handles context switch.
Full Transcript
In ARM architecture, the SysTick timer counts down and triggers the SysTick exception when it reaches zero. The SysTick handler runs and sets the PendSV exception as pending. PendSV is a special exception designed to run at a low priority. It executes after higher priority exceptions finish, allowing it to perform context switching or deferred tasks safely. This separation avoids nested interrupts and keeps the system stable. The execution flow starts with SysTick triggering, then PendSV pending is set, and finally PendSV runs to switch the current task context. Variables like SysTick_Count, PendSV_Pending, and Current_Task change accordingly during this process.

Practice

(1/5)
1. What is the primary purpose of the PendSV exception in ARM processors?
easy
A. To reset the processor
B. To provide a regular timer interrupt
C. To handle task switching smoothly in the background
D. To manage memory access

Solution

  1. Step 1: Understand PendSV role

    PendSV is designed to handle context switching between tasks without interrupting the main program flow.
  2. Step 2: Compare with other exceptions

    Unlike SysTick, which provides timing interrupts, PendSV is specifically for smooth task switching.
  3. Final Answer:

    To handle task switching smoothly in the background -> Option C
  4. Quick Check:

    PendSV = task switching [OK]
Hint: PendSV = background task switcher [OK]
Common Mistakes:
  • Confusing PendSV with SysTick timer
  • Thinking PendSV resets the processor
  • Assuming PendSV manages memory
2. Which of the following is the correct way to enable the SysTick timer in ARM Cortex-M processors?
easy
A. Set the CTRL register's ENABLE bit to 1
B. Clear the CTRL register's ENABLE bit to 0
C. Write 0xFFFFFFFF to the LOAD register
D. Disable the PendSV exception

Solution

  1. Step 1: Identify SysTick control

    The SysTick timer is controlled by the CTRL register, where setting the ENABLE bit starts the timer.
  2. Step 2: Eliminate incorrect options

    Clearing ENABLE stops the timer, writing max to LOAD sets reload value but doesn't enable, and disabling PendSV is unrelated.
  3. Final Answer:

    Set the CTRL register's ENABLE bit to 1 -> Option A
  4. Quick Check:

    SysTick enable = CTRL ENABLE bit set [OK]
Hint: Enable SysTick by setting CTRL ENABLE bit [OK]
Common Mistakes:
  • Confusing enabling with disabling bits
  • Thinking LOAD register enables timer
  • Mixing PendSV control with SysTick
3. Consider this simplified ARM Cortex-M code snippet:
SysTick->LOAD = 1000 - 1;
SysTick->VAL = 0;
SysTick->CTRL = 7;

What does the value 7 in SysTick->CTRL represent?
medium
A. Enable SysTick, enable interrupt, and use processor clock
B. Disable SysTick and interrupts
C. Enable SysTick only without interrupts
D. Reset SysTick counter

Solution

  1. Step 1: Decode CTRL register bits

    Value 7 in binary is 0b111, meaning bits ENABLE(0), TICKINT(1), and CLKSOURCE(2) are set.
  2. Step 2: Understand each bit meaning

    ENABLE starts the timer, TICKINT enables interrupt, CLKSOURCE selects processor clock.
  3. Final Answer:

    Enable SysTick, enable interrupt, and use processor clock -> Option A
  4. Quick Check:

    CTRL=7 means all three bits set [OK]
Hint: 7 in CTRL means enable + interrupt + clock source [OK]
Common Mistakes:
  • Ignoring interrupt enable bit
  • Assuming 7 disables timer
  • Confusing bits order
4. A developer writes this code to trigger PendSV manually:
SCB->ICSR |= (1 << 28);

But the PendSV exception does not occur. What is the likely cause?
medium
A. ICSR register is read-only
B. SysTick timer is disabled
C. PendSV cannot be triggered manually
D. PendSV priority is not set low enough

Solution

  1. Step 1: Understand PendSV triggering

    Writing to ICSR bit 28 sets the PendSV pending flag, but PendSV only runs if its priority is low enough.
  2. Step 2: Identify common mistake

    If PendSV priority is higher or equal to other interrupts, it won't run even if pending.
  3. Final Answer:

    PendSV priority is not set low enough -> Option D
  4. Quick Check:

    PendSV priority must be low to run [OK]
Hint: PendSV needs low priority to run when pending [OK]
Common Mistakes:
  • Assuming ICSR is read-only
  • Thinking SysTick affects PendSV trigger
  • Believing PendSV can't be triggered manually
5. In a real-time operating system (RTOS) on ARM Cortex-M, how do PendSV and SysTick work together to manage multitasking?
hard
A. PendSV triggers timer interrupts; SysTick switches tasks
B. SysTick triggers regular interrupts; PendSV performs context switching after SysTick
C. Both PendSV and SysTick handle memory management
D. SysTick disables PendSV during task switching

Solution

  1. Step 1: Understand SysTick role in RTOS

    SysTick generates periodic interrupts to keep track of time slices and trigger scheduler checks.
  2. Step 2: Understand PendSV role in RTOS

    PendSV is triggered by the scheduler to perform the actual task context switch smoothly in the background.
  3. Step 3: Combine roles for multitasking

    SysTick signals when to switch; PendSV does the switch, enabling efficient multitasking.
  4. Final Answer:

    SysTick triggers regular interrupts; PendSV performs context switching after SysTick -> Option B
  5. Quick Check:

    SysTick = timer, PendSV = switcher [OK]
Hint: SysTick triggers; PendSV switches tasks [OK]
Common Mistakes:
  • Swapping roles of PendSV and SysTick
  • Thinking both handle memory
  • Assuming SysTick disables PendSV
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard