SoC
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
  • Arm Cortex M3
  • Contact
Reading: Cortex M0 Returning from Interrupt
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

Cortex M0 Returning from Interrupt

Neil Salmon
Last updated: October 5, 2023 9:58 am
Neil Salmon 8 Min Read
Share
SHARE

When an interrupt occurs on a Cortex M0 processor, the processor must handle the interrupt properly by saving its current state, servicing the interrupt handler routine, and then restoring its previous state upon return from the interrupt. This process allows the processor to temporarily pause what it was doing, take care of the urgent interrupt request, and then resume normal operation as if the interrupt never occurred. Understanding how the Cortex M0 returns from an interrupt is important for developing robust embedded systems.

Contents
What Happens When an Interrupt OccursDetails of Register SavingReturning from the ISRContext SwitchingInterrupt Priority LevelsInterrupt LatencyDebugging InterruptsConfiguring InterruptsTypical Uses of InterruptsConclusion

What Happens When an Interrupt Occurs

On the Cortex M0, when an interrupt occurs, the processor automatically stores key register values onto the stack. This includes saving the current program counter value so it knows where to return to after the interrupt. It also saves the current processor status flags. The processor then loads the program counter with the interrupt vector address corresponding to the interrupt request. This causes a jump to the start of the specific interrupt service routine (ISR).

Inside the ISR, the programmer can add code to service the device that triggered the interrupt. For example, if a UART peripheral raised an interrupt to signal that data is ready, the ISR would run code to read the incoming data from the UART data register. When the handling is complete, the ISR executes an exception return instruction to exit back to the original program.

Details of Register Saving

On the Cortex M0, eight registers are automatically pushed onto the stack when an IRQ interrupt occurs. These are: R0-R3, R12, LR, PC, and xPSR. The xPSR register contains the program status flags like interrupt masks and processor mode bits. Pushing these registers preserves the processor state so it can be restored exactly as it was before the interrupt.

The Link Register (LR) contains the return address after the interrupt. This corresponds to the program counter value at the time of the interrupt. The PC is then overwritten with the interrupt vector address to jump to the correct ISR handler. By saving these registers, execution can return seamlessly back to where it left off.

Returning from the ISR

To return from the ISR, the exception return instruction BX LR is used. This branches to the address contained in the LR register, which was previously saved on the stack. BX stands for Branch & Exchange, meaning it both branches to the LR address, and exchanges instruction sets if needed (ARM vs Thumb).

The BX instruction triggers automatic unstacking to restore the registers that were pushed upon interrupt entry. So R0-R3, R12, LR, PC, and xPSR all get popped back off the stack into their original locations. This restores the exact processor state as before the interrupt occurred.

Context Switching

In a more advanced system with multiple application tasks or threads, an interrupt may trigger a context switch to a different task. In this case, the ISR would save additional registers and stack data as needed when switching tasks. The LR and PC registers would instead be set to values corresponding to the new task being switched to.

So upon return, a different area of code would execute instead of the original interrupted task. The previously running task’s context would be restored later when switching back to it. This enables multi-tasking type behavior as a result of interrupts.

Interrupt Priority Levels

The Cortex M0 supports nested interrupts through the use of interrupt priority levels. Higher priority interrupts can preempt lower priority code. This means a high priority interrupt can pause the execution of a lower priority ISR. The lower priority ISR will resume after the higher level interrupt completes.

This nested interrupt behavior works because the processor pushes register sets onto the stack for each nested interrupt. When the highest priority ISR finishes, it will return and restore the state of the next lower interrup that it preempted.

Interrupt Latency

Interrupt latency is the time delay from when the peripheral asserts the interrupt signal until the first instruction of the corresponding ISR executes. Fast interrupt latency is often desired so that the processor can respond quickly to time-sensitive events.

On Cortex M0, the latency is minimized by automatically saving only the minimal set of registers on the stack. And using a simple exception return without unstacking also accelerates the interrupt exit time. This optimized exception handling helps achieve low interrupt latency.

Debugging Interrupts

Debugging interrupt driven code on Cortex M0 requires understanding the stack unwinding and context switching that occurs. A debug register view will show the stacked PC, LR, and other registers rising and falling as interrupts commence and complete.

Stepping through code in a debugger will automatically follow the program flow across interrupt handlers. Breakpoints can also be set within specific ISR code to halt execution and inspect variables.

In some cases, forcing or simulating external interrupts in the debugger can help reproduce the real world timing of interrupt events during debug sessions. This helps verify that critical code sections behave correctly.

Configuring Interrupts

Using the Nested Vectored Interrupt Controller (NVIC) on Cortex M0, developers can configure the priority levels, enable or disable interrupts, and set other options. The NVIC registers allow interrupts to be tailored to the needs of the application.

Interrupt service routines must follow certain conventions and be registered into the Interrupt Vector Table. This table maps interrupts to their corresponding ISR handler functions. Properly registering ISRs ensures interrupts jump to the right code.

Typical Uses of Interrupts

Here are some typical uses of interrupts in embedded Cortex M0 applications:

  • I/O peripherals (UART, SPI, I2C, etc) use interrupts to signal events like data ready to read.
  • Timers use interrupts when they expire to trigger periodic application code.
  • GPIO pins can trigger interrupts on value change to detect external signals.
  • ADC interfaces use interrupts to indicate conversion results are ready.
  • Keyboard inputs trigger interrupts to process key presses instantly.

Interrupts allow rapid response to hardware events and are vital for real-time embedded systems. Understanding the Cortex M0 interrupt handling process facilitates designing effective interrupt driven applications.

Conclusion

Efficient interrupt handling is a key requirement in embedded microcontroller applications. The Cortex M0 architecture provides a fast, low latency interrupt capability to service peripheral events and external stimuli. By automatically saving context onto the stack, interrupts can quickly transfer control to an ISR, and then seamlessly return to normal execution afterwards. With proper configuration, interrupts can enable powerful real-time processing in energy-constrained Cortex M0 systems.

Newsletter Form (#3)

More ARM insights right in your inbox

 


Share This Article
Facebook Twitter Email Copy Link Print
Previous Article How to Activate the Debug Viewer for Cortex-M0?
Next Article SWD-Port of the Cortex-M0: Cannot connect to MEM-AP on Cortex-M0 Designstart Eval
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

Cortex M0 Pipeline Stages

The Cortex-M0 is a 32-bit ARM processor optimized for low-power…

7 Min Read

What are the fundamentals of the ARM Cortex M3?

The ARM Cortex-M3 is a 32-bit processor core designed for…

6 Min Read

ARM Cortex-M0 Stack Pointer

The stack pointer is one of the key registers in…

6 Min Read

Cortex-M0 Multiply Cycles

The Cortex-M0 is an ultra low power 32-bit ARM processor…

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

Sign in to your account