SREG and interrupts

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This question may be really easy...
I have to use inline asm:
asm volatile (
"IN R0,__SREG__ \n\t"
"PUSH R0 \n\t"
);
cli();
// some UNINTERRUPTIBLE code
asm volatile (
"POP R0 \n\t"
"OUT __SREG__,R0 \n\t"
);
Do I have another option without using ASM?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why not?

{
uint8_t sreg_saved = SREG;
cli();
...
SREG = sreg_saved;
}

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

:-(
I guess it should be library function...Look at the resulted code my previous solution looks much better. So the point does this code already exist in a lib?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry, I can't see your point. Given the following simple function:

#include
#include

void
foo(void)
{
    unsigned char sreg;

    sreg = SREG;
    cli();
    PORTB = 42;
    SREG = sreg;
}

... the compiler generates (-Os -mmcu=at90s8535):

.global foo
    .type foo, @function
foo:
/* prologue: frame size=0 */
/* prologue end (size=0) */
    in r25,95-0x20
/* #APP */
    cli
/* #NOAPP */
    ldi r24,lo8(42)
    out 56-0x20,r24
    out 95-0x20,r25
/* epilogue: frame size=0 */
    ret
/* epilogue end (size=1) */
/* function foo size 7 (6) */
    .size foo, .-foo
/* File "foo.c": code 7 = 0x0007 ( 6), prologues 0, epilogues 1 */

What's so poor about that?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

in my code sreg was in stack not in register (uninterruptible code just more complex).
For some other reasons optimization level 2 only acceptable.