2
\$\begingroup\$

I am trying to understand the concept of 'Context switch' in reference to C-language and 32-bit MCUs.

For example if I am programming STM32F407 MCU in C-language using STM32CubeIDE and I want to demonstrate full context switch of the MCU then how could I do that?

Do I need to service an Interrupt request for example an interrupt from a periodic timer? So when I will go into the Timer ISR it would imply that the MCU has experienced a full-context switch while jumping to the ISR (Interrupt Handler).

Or Do I need to make a function call? So that when the MCU jumps into a new function that will mean that the MCU has experienced a full-context switch?

Can anyone explain how can I demonstrate a Context-Switch in C-language?

\$\endgroup\$
4
  • \$\begingroup\$ That does not make much sense, what context switch you mean? MCU switching between main code and interrupt context? RTOS switching between tasks? \$\endgroup\$ – Justme Jul 22 '20 at 13:48
  • 1
    \$\begingroup\$ A context switch is something an OS does, it is not something a C does. Are you trying to write an RTOS? Or execute a context switch in an existing RTOS? \$\endgroup\$ – DKNguyen Jul 22 '20 at 13:54
  • \$\begingroup\$ What I am not clear about is that even if a context switch is done by an RTOS then does it means something specific that I can do in a simple c-program (example call to an ISR, call to a function) or does it involve something more than that? \$\endgroup\$ – homecloud Jul 22 '20 at 14:09
  • \$\begingroup\$ You'd basically have to write a simple RTOS to do it. You have to structure everything in a certain way to do it. It's kind of like save-stating your MCU and then loading another save state, so you can go do something else and return to that state later and continue at where you left off. I guess in its most basic form it involves storing your registers and program counter, and loading the registers and program counter with something else. \$\endgroup\$ – DKNguyen Jul 22 '20 at 15:34
3
\$\begingroup\$

Under an RTOS, a context switch means the RTOS changes which task (process) is currently running on MCU. The switch to another task may happen due to user code (e.g. OS call to sleep for some amount of time) or via timer interrupt if a task is running and time slice it was given is used so it is time to run other tasks. Or a task says to OS it is done and next task can run if user calls yield().

The context switch is done by OS code, you don't need to worry about it. Just like coding under Windows or Linux, the CPU cores are switched between tasks automatically by the kernel, and your plain old single threas C program does not need to be aware of it.

\$\endgroup\$
1
\$\begingroup\$

An interrupt in a Cortex-M CPU would probably count as a context switch by itself. When handling an interrupt, the CPU:

  • Pushes its registers to the stack
  • Saves the address of the next instruction it would have executed.
  • Branches to the address of the ISR.

If "context" means register contents, stack frame, and PC, then an interrupt is a context switch. And when returning from an interrupt, the original "context" is restored.

\$\endgroup\$
3
  • \$\begingroup\$ It confirms that 'context-switch' is related to MCU register set and memory. It does not matter if there is an RTOS running on it or not, we are doing a 'context-switch' in simple c-program also when we are handling an interrupt. So when an interrupt occurs it triggers some events or actions in hardware which will do the 'context-switch' automatically since I would not be writing anything related to 'context-switch' in a c-program except writing an ISR. Now is there any other way to 'trigger' the same actions as above that I can do under the software control eg., a function call? \$\endgroup\$ – homecloud Jul 22 '20 at 17:24
  • \$\begingroup\$ You could use a software interrupt. It's exactly what it sounds like -- a software-triggered interrupt. It works like a function call except it uses the interrupt handling mechanism instead of just a branch instruction. \$\endgroup\$ – Adam Haun Jul 22 '20 at 19:39
  • \$\begingroup\$ It depends on what you count as a context switch. Basically, just calling a subroutine is kind of a context switch. When doing a subroutine call, the current address of execution is stored into stack, and is continued from new place of execution, and when the subroutine exits, it pops the address where to return to continue execution. \$\endgroup\$ – Justme Jul 22 '20 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.