Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8182997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:01:42+00:00 2026-06-07T01:01:42+00:00

I have some C macros that disable and enable interrupts so that I can

  • 0

I have some C macros that disable and enable interrupts so that I can define critical sections of code. I want to ensure that the optimizer complies with the operations and does not move them or remove them.

#define ARM_INT_KEY_TYPE         unsigned int
#define ARM_INT_LOCK(key_)   ({ \
    asm("MRS %0,cpsr" : "=r" (key_)); \
    asm("MSR cpsr_c,#(0x1F | 0x80 | 0x40)"); \
})
#define ARM_INT_UNLOCK(key_) asm("MSR cpsr_c,%0" : : "r" (key_))

The usage is as follows:

int init_i2c(p_device_i2c dev){

// Interrupts are enabled
doSomething();

ARM_INT_KEY_TYPE key;
ARM_INT_LOCK(key);

// Interrupts are disabled
pMX27_GPIO i2c_clk = (pMX27_GPIO)(GPIO_BASE_ADDR | I2C_CLK_PORT);
pMX27_GPIO i2c_sda = (pMX27_GPIO)(GPIO_BASE_ADDR | I2C_DATA_PORT);

i2c_clk->GIUS   &= ~(1 << I2C_CLK_PIN);         // I2C Signals
i2c_sda->GIUS   &= ~(1 << I2C_DATA_PIN);        // I2C Signals

ARM_INT_UNLOCK(key);
// Interrupts ON again

// Wait for stable
ARM_delay_clocks(5000);

i2c_stop(dev);

The code works as expected with optimization turned off but I suspect that there may be problems with optimization turned ON.

Will adding volatile to the asm statements do the trick?

#define ARM_INT_UNLOCK(key_) asm volatile ("MSR cpsr_c,%0" : : "r" (key_))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T01:01:44+00:00Added an answer on June 7, 2026 at 1:01 am

    Yes, asm volatile is the correct way to write this macro. You can easily check the behavior by stepping through this code on debugger and watching the order of operations at the assembly level. Or, you can just visually inspect the disassembly output for your function.

    Here’s some useful gcc documentation on inline asm

    http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html#ss5.4

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to export some macros that I have defined and be able
I have some arbitrary pixel data that I want to save as a PNG.
I want to have more control over macros such as assertions (and some logging
I have macros that are running on some workbooks and a status screen that
I have some macros like, #define dosomething(x) something(x) and it is compiled into a
I have some code that needs Unicode strings for the most part, but I
Intro: I have written some short excel macros (tested, they work fine) and want
I have some Motif code that also uses widgets from the Xmt widget set.
I have a macro that iterates through some rows, to update the colouring of
I am trying to have a macro that will do some formatting. I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.