Interrupt Service Routines: Difference between revisions

Jump to navigation Jump to search
→‎Compiler Specific Directives: added clang, cleaned up
[unchecked revision][unchecked revision]
mNo edit summary
(→‎Compiler Specific Directives: added clang, cleaned up)
Line 66:
</source>
 
===Compiler Specific Interrupt Directives===
 
Some compilers for some processors have directives allowing you to declare a routine interrupt, offer a #pragma interrupt, or a dedicated macro. Borland C, Watcom C/C++, Microsoft C 6.0 and Free Pascal Compiler 1.9.* and up offer this, while VisualC++ and GCC don't:
 
Some compilers for some processors have directives allowing you to declare a routine interrupt, offer a #pragma interrupt, or a dedicated macro. Borland C, Watcom C/C++, Microsoft C 6.0 and Free Pascal Compiler 1.9.* and up offer this, while VisualCGCC does not. Visual C++ and GCCclang-llvm donoffer an alternative shown under '''Naked Functions'''t:
====Borland C====
<source lang="C">
/* Borland C */
Line 78:
</source>
 
====Watcom C/C++====
<source lang="C">
/* Watcom C/C++ */
Line 86 ⟶ 87:
</source>
 
====Naked Functions====
Actually, VisualC++ can be used to make interrupts, by making them naked. adding _declspec(naked) to your function will cause the compiler to leave out the stack handling code. This means you are free to set up and release stack space however you want. Just be careful that you put in a return statement, because the compiler won't, it will just allow execution to continue past the end of your code on into garbage. Also, if you plan to use local variables or function arguments in the C code, you need to set up the stack frame the way the compiler expects it. This is not such a problem in interrupts though, because since they are non-reentrant, you can simply use static variables.
Some Compilers can be used to make interrupt routines, but requires you to manually handle the stack and return operations. Doing so requires that the function be generated without an epilogue or prologue. This is called making the function ''naked'' - this is done in Visual C++ by adding the attribute ''_declspec(naked)'' and in clang-llvm by adding the attribute ''__attribute__((naked))'' to the function. You need to verify that you do include a return operation (such as ''iretd'') as that is part of the prologue that the compiler has now been instructed to not include.
 
If you intend to use local variables, you must set up the stack frame in the manner which the compiler expects; as ISRs are non-reentrant, however, you can simply use static variables.
 
=====Visual C++=====
 
<source lang="C">
Line 102 ⟶ 108:
}
</source>
 
=====clang-llvm=====
 
<source lang="C">
/* clang-llvm */
__attribute__((naked)) void interrupt_handler()
{
asm ("pushad");
 
/* do something */
 
asm (
"popad" \
"iretd"
);
}
</source>
 
====gcc / g++====
 
gcc nor g++ offer any means by which to have an interrupt service routine be only in C/C++ without performing '''black magic'''.
 
===Black Magic===
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu