Explain Macro from avr-libc

A

Affan Syed

I am not able to figure out exactly what this macro is doing. Can one of the
gurus around here decipher this?

#define sei() __asm__ __volatile__ ("sei" ::)

PS: It is from the avr-libc interrupt.h and it should set a partilcular
interrrupt.

Thanks.

Affan
 
J

jacob navia

Affan said:
I am not able to figure out exactly what this macro is doing. Can one of the
gurus around here decipher this?

#define sei() __asm__ __volatile__ ("sei" ::)

PS: It is from the avr-libc interrupt.h and it should set a partilcular
interrrupt.

Thanks.

Affan

This is GNU C. The macro expands to an __asm__ construct, that
emits an assembler instruction. I do not know anything more, and
since I do not use gcc I can't tell you more. Just google around
for the gcc documentation, and you will find the explanation.
 
R

Randy Howard

I am not able to figure out exactly what this macro is doing. Can one of the
gurus around here decipher this?

#define sei() __asm__ __volatile__ ("sei" ::)

PS: It is from the avr-libc interrupt.h and it should set a partilcular
interrrupt.

Without looking at the documentation for that particular compiler, it
is pretty hard to guess what that is doing. Apparently you have not
bothered to RTFM yet, because it is covered.

AVR Libc has a website at http://www.nongnu.org/avr-libc/

Looking at the online documentation on that site, they cover the sei
and cli macros under "Interrupts and Signals", which apparently
enable or disable the I bit of the status register. so the "::"
syntax is probably some flag to the compiler to make that happen.
The manual says it is a single instruction when compiled, so it is
low overhead.

It does not, as you imply above, set a particular interrupt. It
enables/disables interrupts entirely. If you want to work with
particular interrupts, again I refer you to the documentation under
"Interrupts and Signals" which cover this.

You would probably be a lot better off asking in a forum dedicated
to the AVR, or one of an embedded newsgroup instead if that is
insufficient.
 
A

Arthur J. O'Dwyer

It's a syntax error. :: is an invalid token in C.

Actually, :: /isn't/ a token in C. Therefore, what we really have
here is one : token, immediately followed by another : token. This
would indeed be a syntax error if it weren't "hidden" by the #define.

#define sei() a bunch of nonsense tokens :: ^%! asd6 **(!%

is perfectly valid C code on its own; it only contributes to a syntax
error once you use 'sei()' in an expression seen by the parser.[1]

Potentially more useful to the OP: The line of code you posted is
from a platform-specific implementation library. We don't discuss
individual platforms here; there are other Usenet newsgroups devoted
to them. Rest assured that you will never need to know what "__asm__
__volatile__ ("sei" ::)" means... or if you can't do that, ask the
question again in a group dedicated to your particular computer
architecture, where someone will probably know what "sei" means to
your computer.

-Arthur

[1] - The parser that parses "real" C code, not the parser that's
part of the preprocessor, that is. How does one accurately and
precisely refer to that concept, without resorting to Standardese
like "the seventh phase of translation," or whatever it is?
 
M

Mark F. Haigh

Affan said:
I am not able to figure out exactly what this macro is doing. Can one of the
gurus around here decipher this?

#define sei() __asm__ __volatile__ ("sei" ::)

PS: It is from the avr-libc interrupt.h and it should set a partilcular
interrrupt.

For the most part, off topic, but I think we can scrounge at least some
topicality. The C standard states:

7.1.3 Reserved identifiers

[...]

All identifiers that begin with an underscore and either
an uppercase letter or another underscore are always
reserved for any use.


So __asm__ and __volatile__ are implementation-defined identifiers, or
in other words, specific to your compiler.

[OT]

If you're using gcc, __asm__ is a construct for using assembly code
from within C code. The presence of __volatile__ tells the compiler
not to reorder or optimize away the instruction. "sei" is an assembly
instruction that enables interrupt processing on the processor. The
double colons delimit input and output operands, of which there are
none here. As always, see your compiler documentation for definitive
information.

When you're mucking around with enabling and disabling interrupts,
there's usually hard and strict requirements about exactly what is
executed with interrupts enabled/disabled. The last thing you want
there is the compiler rearranging or eliminating instructions like this
behind your back, hence the usage of __volatile__ in this case.


Mark F. Haigh
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Bare metal. 0
Macro Expansion 4
Linux: using "clone3" and "waitid" 0
Who can explain this bug? 57
Macro Mystery ... help? 4
libc Sleep api performs a busy waiting 3
reasoning for a macro 14
Macro expansion surprise. 14

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top