SoC
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
  • Arm Cortex M3
  • Contact
Reading: xpsr arm cortex-m3
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

xpsr arm cortex-m3

Graham Kruk
Last updated: October 5, 2023 9:55 am
Graham Kruk 8 Min Read
Share
SHARE

The xPSR (program status register) is one of the key registers in the ARM Cortex-M3 processor. It contains information about the current state of the processor and is used to control and monitor program execution.

Contents
Overview of xPSRReading xPSRWriting to xPSRUses of xPSRTypical Operations on xPSRxPSR and Exception HandlingxPSR and Power ManagementxPSR in DebuggingConsiderations when Using xPSR

Overview of xPSR

The xPSR is a 32-bit read/write register that combines bits from the following CPU status registers:

  • APSR – Application Program Status Register
  • IPSR – Interrupt Program Status Register
  • EPSR – Execution Program Status Register

The layout of the xPSR register is as follows: 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 N Z C V Q J RES0 GE[3:0] Exception number ICI/IT T APSR IPSR EPSR

Some key bits include:

  • N, Z, C, V – Status flags for arithmetic operations
  • Q – Cumulative saturation flag
  • ICI/IT – Interrupt control bits
  • T – Thumb execution state bit
  • Exception number – Code for current exception
  • GE – Greater than or equal flags

Reading xPSR

To read the current value of xPSR, the MRS instruction can be used like: MRS R0, PSR ; Read PSR into R0

This will copy the value from xPSR into a general purpose register, from where individual bits can be examined as needed by the program.

Writing to xPSR

The xPSR register can be written to using the MSR instruction. For example: MRS R0, PSR ; Read PSR BIC R0, R0, #0x1F ; Clear IT bits MSR PSR_cxsf, R0 ; Write back PSR

This sequence reads xPSR into R0, clears the interrupt control bits, and writes the modified value back into the register. However, many parts of xPSR like the exception number and execution state bits cannot be changed directly. The allowed writeable fields are:

  • APSR – N, Z, C, V, Q flags
  • EPSR – T bit
  • IPSR – ICI/IT bits

Uses of xPSR

The xPSR register has several important uses in Cortex-M3 applications:

  • Exception Handling – The IPSR field identifies the exception number and stack frame. This allows the exception handler to identify the source and type of exception.
  • Interrupts – The ICI/IT bits are used to set priority and control interrupt behaviour.
  • Conditional Execution – Status flags like N, Z, C, V can be tested to conditionally execute code based on result of arithmetic/logical operations.
  • Change Thumb State – The T bit can be used to change between ARM and Thumb execution states.
  • Saturation Detection – The Q flag is set if an arithmetic instruction saturates, indicating an overflow.

Typical Operations on xPSR

Here are some common xPSR operations:

  • Read xPSR – To save current CPU state, examine flags etc. MRS R0, PSR
  • Store xPSR – Save xPSR value on stack or memory during exception handling PUSH {R0-R3, LR} ; Save registers MRS R1, PSR ; Read PSR STR R1, [SP, #-4]! ; Save on stack
  • Modify Status Flags – Change N, Z, C, V flags for conditional code MOVS R0, #1 ; Set flags MRS R1, PSR ; Read PSR ORR R1, R1, #0x40000000 ; Force Z flag MSR APSR, R1 ; Update flags
  • Set Interrupt Control – Configure interrupt handling MOV R0, #0x1F ; IT bits mask MRS R1, PSR ; Read PSR BIC R1, R1, R0 ; Clear IT fields ORR R1, #0x10 ; Set IT bits MSR APSR, R1 ; Configure IT
  • Check for Exceptions – Identify exception source MRS R0, PSR ; Read PSR LSRS R1, R0, #16 ; Extract IPSR CMP R1, #16 ; Check for usage fault BEQ UsageFault ; Branch if fault

xPSR and Exception Handling

One of the main uses of the xPSR is in exception handling. When an exception occurs like an interrupt, fault or trap, the processor automatically saves xPSR into the stack frame for that exception.

The exception handler can then examine the stacked xPSR value to determine:

  • Type of exception from IPSR
  • Processor mode and execution state from EPSR
  • Interrupt control context from IPSR
  • Status flag values from APSR

This information helps the exception handler take the right actions and restore state correctly after handling the exception. The xPSR bits allow nested exceptions to be managed efficiently.

Some typical operations in exception handlers include:

  • Read stacked xPSR to identify exception source and context
  • Preserve xPSR value by pushing onto stack if nesting exceptions
  • Clear ICI/IT fields if necessary before returning from exception
  • Restore stacked xPSR value to resume execution after servicing exception

Managing xPSR correctly is key to robust exception handling in Cortex-M3 applications.

xPSR and Power Management

The xPSR register plays a role in power management on Cortex-M3 processors. Some of the ways it is used include:

  • The SLEEPONEXIT bit can be set in EPSR to sleep the processor when returning from an ISR.
  • xPSR saved stack frame state is used to resume execution after waking from sleep.
  • ICI/IT bits are used for interrupt wake up behavior configuration.
  • EPSR and stacked xPSR indicates to power management controller when CPU is idle or active.

By managing xPSR configuration, idle sleep can be introduced transparently within exception handlers and low power modes activated when the CPU does not need to be fully active.

xPSR in Debugging

When debugging Cortex-M3 applications, viewing and modifying xPSR assists with:

  • Identifying the source of exceptions from IPSR.
  • Determining execution state issues from EPSR.
  • Monitoring interrupt control context in IPSR.
  • Setting status flags in APSR to influence program flow.
  • Detecting saturation or arithmetic issues using Q flag.

Debuggers or IDEs allow inspecting and modifying xPSR during development. This helps analyze issues and alter program execution to assist debugging complex problems.

Considerations when Using xPSR

Some things to keep in mind when working with xPSR in Cortex-M3 applications:

  • Only some fields can be directly written, other fields like EPSR/IPSR require SYSm instructions.
  • Preserve existing flag values when modifying status flags in APSR.
  • Use stack to save/restore complete xPSR value for exceptions.
  • Avoid clearing ICI/IT erroneously, this can lead to faulty interrupt handling.
  • Configure SLEEPONEXIT and wakeup behavior appropriately for low power operation.

Overall, the xPSR register is a powerful tool for managing exceptions, interrupts, status flags and power control in Cortex-M3 CPUs. Mastering xPSR usage and manipulation is key to unlocking the potential of these ARM processors.

Newsletter Form (#3)

More ARM insights right in your inbox

 


Share This Article
Facebook Twitter Email Copy Link Print
Previous Article Arm Trap Instruction
Next Article ARM Cortex-M EPSR
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 EK TM4C123GXL?

The EK TM4C123GXL is a low-cost evaluation board based on…

6 Min Read

ARM Cortex M Cache

ARM Cortex-M processors are known for their power efficiency and…

8 Min Read

What is the size of the ARM Cortex-M3’s address bus?

The ARM Cortex-M3 processor has a 32-bit address bus, which…

10 Min Read

Avoiding ARM/Thumb Interworking Issues on Cortex M3

When mixing ARM and Thumb code on the Cortex M3,…

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

Sign in to your account