SoC
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
  • Arm Cortex M3
  • Contact
Reading: FreeRTOS Context Switch
SUBSCRIBE
SoCSoC
Font ResizerAa
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
Search
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
Have an existing account? Sign In
Follow US
  • Looking for Something?
  • Privacy Policy
  • About Us
  • Sitemap
  • Contact Us
© S-O-C.ORG, All Rights Reserved.
Arm

FreeRTOS Context Switch

Graham Kruk
Last updated: September 12, 2023 7:10 am
Graham Kruk 5 Min Read
Share
SHARE

A context switch is a fundamental part of multitasking real-time operating systems like FreeRTOS. It allows multiple tasks to share a single CPU core while ensuring each task appears to have full and exclusive access to the CPU. When a context switch occurs, the state of the current task is saved and the state of the next task to run is restored. This allows seamless switching between tasks.

Contents
What Triggers a Context Switch in FreeRTOS?Context Switch ProcessDetailed Context Switch StepsContext Switch TimeOptimizing Context SwitchesContext Switch vs Thread Switch

What Triggers a Context Switch in FreeRTOS?

There are several events that can trigger a context switch in FreeRTOS:

  • Blocking API call – When a task makes a blocking API call like vTaskDelay(), it voluntarily gives up the CPU allowing a context switch to another ready task.
  • Completion of task time slice – Each task has a pre-configured time slice. When the time allotted expires, a context switch occurs.
  • Higher priority task becomes ready – If a currently running task is preempted due to a higher priority task becoming ready to run, a context switch will happen.
  • Interrupt – Hardware interrupts and software interrupts like the PendSV exception will trigger a context switch.
  • Mutex give – When a task gives a mutex, a context switch occurs if there is a higher priority task waiting for that mutex.

Context Switch Process

Here are the steps involved during a context switch in FreeRTOS:

  1. Save context of current task – Processor registers like stack pointer, program counter and other register contents are pushed onto the current task’s stack.
  2. Select next task to run – Based on priority, state and scheduler logic, the highest priority ready task is selected.
  3. Restore context of next task – Stored register contents are popped from the stack of new task and loaded into processor registers, restoring its context.
  4. Jump to next task – The program counter is updated to point to the next task’s code, starting its execution.

Detailed Context Switch Steps

Here is a more detailed look at what happens during each step of a context switch:

  1. Save Context
    • Stack pointer (SP) is pushed onto current task’s stack
    • Processor status register and other registers are pushed
    • Critical section nesting value is stored
    • Current task’s stack pointer is updated
  2. Select Next Task
    • Scheduler algorithm selects highest priority ready task
    • Selected task’s stack pointer is moved to processor’s stack pointer register
  3. Restore Context
    • Saved registers and status are popped from new task’s stack
    • New task’s critical section nesting value is loaded
    • Stack pointer is restored from new task’s stack
  4. Jump to Next Task
    • Program counter is set to resume new task’s code
    • Execution begins for next task

Context Switch Time

The time required to perform a context switch is called context switch time or context switching overhead. It depends on:

  • Number of registers that must be saved/restored – More registers mean higher overhead.
  • Time to save/restore registers – Depends on processor architecture.
  • Time taken by scheduler logic – Scheduler may require complex ready task searches.
  • Task stack usage – Larger task stacks increase memory access time.

A typical context switch may take hundreds to thousands of clock cycles. Minimizing this time allows increased task parallelism.

Optimizing Context Switches

Here are some ways to optimize context switches in FreeRTOS:

  • Minimize unnecessary context switching using features like idle hooks.
  • Configure TIME_SLICING setting appropriately. Disable if response time is critical.
  • Reduce number of registers saved/restored by optimal compiler settings.
  • Use naked pragma to write context switch routines in assembly for faster saving/restoring.
  • Assign priorities appropriately to avoid frequent preemption.
  • Reduce task stack sizes to only what is needed.
  • Tune tick interrupt frequency and clock speed for tradeoff with accuracy.

Context Switch vs Thread Switch

The main differences between a context switch and a thread switch are:

  • Context switches can occur between tasks and ISRs. Thread switches only occur between threads.
  • Context switches require saving/restoring processor registers and status. Thread switches only require a stack pointer swap.
  • Context switches allow true parallelism on multi-core. Thread switches only allow pseudo-parallelism via time slicing on single core.
  • Context switch overhead varies based on architecture and compiler. Thread switch overhead is low and consistent.

In summary, context switching is a fundamental mechanism to allow RTOS multitasking on single and multi-core processors. Understanding how it works helps optimize real-time performance.

Newsletter Form (#3)

More ARM insights right in your inbox

 


Share This Article
Facebook Twitter Email Copy Link Print
Previous Article Context Switching in RTOS
Next Article Understanding FreeRTOS Context Switch Time
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

2k Followers Like
3k Followers Follow
10.1k Followers Pin
- Sponsored-
Ad image

You Might Also Like

What is a fault exception in the ARM Cortex-M?

A fault exception in the ARM Cortex-M is an unexpected…

10 Min Read

ARM Processor Advantages and Disadvantages

ARM processors are widely used in mobile devices and embedded…

6 Min Read

What is context switching in the Arm Cortex?

Context switching refers to the process of storing and restoring…

6 Min Read

Why Did ARM Decide to Automatically Load SP from First Entry of the Vector Table?

ARM processors always load the initial stack pointer (SP) value…

8 Min Read
SoCSoC
  • Looking for Something?
  • Privacy Policy
  • About Us
  • Sitemap
  • Contact Us
Welcome Back!

Sign in to your account