SoC
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
  • Arm Cortex M3
  • Contact
Reading: The Basepri Register in Cortex-M4 Processors
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

The Basepri Register in Cortex-M4 Processors

Graham Kruk
Last updated: October 5, 2023 9:56 am
Graham Kruk 6 Min Read
Share
SHARE

The basepri register is a key component of the interrupt handling system in Cortex-M4 processors. It allows priority filtering of interrupts and provides more control over interrupt preemption. The basepri register works together with the primask register to allow flexible interrupt prioritization and masking in Cortex-M4 cores.

Contents
What is the Basepri Register?How Basepri WorksTypical Uses of BasepriCritical SectionsInterrupt Priority ManagementInterrupt Nesting ControlInteraction Between Basepri and PrimaskDefining Interrupt Priority LevelsConfiguring BasepriSaving and Restoring BasepriConclusion

What is the Basepri Register?

The basepri register is an 8-bit read/write register present in NVIC (Nested Vectored Interrupt Controller) of Cortex-M4 processors. It holds a priority value that is used for priority filtering of interrupts. The basepri value acts as a “priority floor” – only interrupts with a priority higher than the basepri value can interrupt the current execution. Interrupts with priority equal to or lower than basepri remain pending but are ignored for preemption purposes.

This allows selective enabling/disabling of preemption based on interrupt priority. Setting basepri to 0 allows preemption from all interrupts as normal. Setting it to higher priority values prevents lower priority interrupts from preempting current execution. This provides finer control over interrupt preemption compared to simply enabling/disabling all interrupts using primask.

How Basepri Works

The Cortex-M4 interrupt handling scheme assigns a priority from 0 (highest) to 255 (lowest) to each interrupt. At any time, the “threshold” for preemption is determined by the MAX(basepri, Current running priority).

When an interrupt occurs:

  • If its priority is higher than the threshold, it will preempt current execution.
  • If its priority is equal or lower than the threshold, it will be held pending and ignored for preemption purposes.

Therefore, by adjusting basepri, the threshold can be moved up and down to selectively allow or prevent preemption from certain priority levels. Setting basepri = 0 means the threshold is the current running priority. Setting it higher raises the threshold to prevent lower priority interrupts.

Typical Uses of Basepri

Critical Sections

A common use of basepri is to protect critical sections of code from preemption. This is done by:

  1. Saving current basepri value to a temporary variable on function entry.
  2. Setting basepri to the highest priority in critical section to lock out all interrupts.
  3. Restoring original basepri value on exit.

This prevents any lower priority interrupts from interrupting the critical section. basepri manipulations are typically faster than disalbing/enabling interrupts using primask.

Interrupt Priority Management

basepri allows selective filtering out of lower priority interrupts without having to individually disable each one. For example, a periodic motor control ISR may need to run without interruption from lower priority interrupts like ADC sampling. This can be ensured by setting basepri to the motor control priority.

Interrupt Nesting Control

Setting basepri can also manage interrupt nesting behaviors. An interrupt service routine (ISR) executing with basepri > 0 will prevent nesting of equal or lower priority interrupts. This saves on stack usage in nested interrupts.

Interaction Between Basepri and Primask

basepri and primask registers can be used together to flexibly control interrupt blocking. The priority threshold for preemption is: Threshold = MAX(basepri, Current running priority) if primask = 0 = 255 if primask = 1

Therefore:

  • primask globally enables/disables all interrupts
  • basepri selectivity filters interrupts for preemption

Typical use is to disable all interrupts using primask in the most critical sections, and use basepri for less critical ones. primask disables and saves more context than basepri.

Defining Interrupt Priority Levels

Proper priority assignment to interrupts is important for basepri to work effectively. Higher priority values need to be assigned to more critical interrupts. Some guidelines include:

  • The SysTick timer must have the highest priority to ensure correct RTOS timing.
  • Time critical interrupts like motor control PWM should have priorities just lower than SysTick.
  • High frequency interrupts like ADC sampling can have moderate priorities.
  • Low priority for simple communications and user interface tasks.

Higher priority interrupts should have lower ISR execution times. The priorities can be adjusted during development for optimal real-time performance.

Configuring Basepri

The basepri register is memory mapped into the System Control Block(SCB) in the Cortex-M4 core. It can be set directly by writing to the register address. For example: #define SCB_BASE 0xE000ED00 #define SCB_BASEPRI (SCB_BASE + 0x5C) void SetBasepri(uint32_t basePri) { *(volatile uint32_t *)SCB_BASEPRI = basePri; }

This writes the basePri value directly into the basepri register address. The CMSIS core headers define the BASEPRI register as: #define SCB_BASEPRI_R (*((volatile uint32_t *)0xE000ED1C))

So it can also be accessed using: SCB_BASEPRI_R = basePri;

The basepri value written must be between 0 and 255, corresponding to the priority levels. Reads of basepri return the current value.

Saving and Restoring Basepri

When using basepri to protect critical sections, the original value needs to be saved and restored: // Save current basepri uint32_t priMask = __get_BASEPRI(); // Set basepri to new level __set_BASEPRI(newLevel); // Critical section… // Restore original basepri __set_BASEPRI(priMask);

This prevents any change to basepri from affecting other parts of the program. The CMSIS intrinsic functions are used here for convenience.

Conclusion

The basepri register gives finer control over interrupt preemption in Cortex-M4 processors. It allows selective priority filtering of interrupts for preemption purposes. Together with optimal priority assignment, basepri usage can improve real-time performance and deterministic behaviors in Cortex-M4 applications.

Newsletter Form (#3)

More ARM insights right in your inbox

 


Share This Article
Facebook Twitter Email Copy Link Print
Previous Article Primask Register in Cortex-M4
Next Article ARM Cortex M4 Development Board
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

Is Arm Better Than X64?

The debate between Arm and x64 architectures has been going…

7 Min Read

Which Stack Is Used Coming Out of Reset In ARM Cortex-M, MSP or PSP?

When an ARM Cortex-M processor comes out of reset, it…

8 Min Read

What is the difference between ARM M4 and M55?

The key differences between the ARM M4 and M55 processors…

7 Min Read

What register is the program counter(arm cortex)?

The program counter in ARM Cortex processors is stored in…

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

Sign in to your account