ARM Technical Support Knowledge Articles


Applies to: CARM C Compiler


Information in this article applies to:


I want to create ATOMIC instruction sequences that are interrupt protected. But once the startup code sets the ARM CPU into USER operation mode, the CPSR can no longer be modified.

Is there another method to implement ATOMIC instruction sequences? Can you give me a guideline?


The ARM CPU provides an SWI (Software Interrupt) instruction. Software Interrupts store the CPSR (Current Program Status Register) and disable IRQ interrupts. Therefore an SWI function is protected against interrupts.

The Keil CARM Compiler provides an efficient SWI interrupt handler. The definition of software interrupt functions (in this example an ATOMIC AND operation) is as shown below:

// Atomic AND operation: *val &= mask
unsigned int AImsk (unsigned int *val, unsigned int mask) __swi(8)  {
  return (*val &= mask);

As explained above, SWI functions automatically disable the IRQ interrupt. If you need to block the FIQ interrupt also, you may write the SWI function in ARM mode and use the MSR CPSR_c instruction to block the FIQ interrupt. The following routine shows this:

// Atomic AND operation: *val &= mask
unsigned int AImsk (unsigned int *val, unsigned int mask) __arm __swi(8)  {
  asm {MSR CPSR_c, #0xd3}    // disable FIQ
  return (*val &= mask);

To use the asm {MSR CPSR_c, #0xd3} instruction, you must translate the function in ARM mode using the __arm function attribute. In Thumb mode, the instruction is not available and generates a syntax error.



Article last edited on: 2005-06-28 17:53:32

Rate this article

Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential