I’m working in a micro-controller using the C language. In this specific micro, the interrupts have to be defined using #pragma in following way:
static void func();
#pragma INTERRUPT func <interrupt_address> <interrupt_category>
static void func() { /* function body */ }
The <interrupt_address> is address of the interrupt in vector table. The <interrupt_category> is either 1 or 2. For example, to define an interrupt in Port 0 pin 0:
static void _int_p00();
#pragma INTERRUPT _int_p00 0x10 1
static void _int_p00() { (*isr_p00)(); }
We define actual interrupt service routine elsewhere and use function pointer (like isr_p00 in the example) to execute them.
It would be convenient if the interrupts could be defined using a macro. I want do define a macro in following way:
#define DECLARE_INTERRUPT(INT_NAME, INT_CAT) \
static void _int_##INT_NAME(); \
#pragma INTERRUPT _int_##INT_NAME INT_NAME##_ADDR INT_CAT \
static void _int_##INT_NAME() { (*isr_##INT_NAME)(); }
The compiler throwing the following error:
Formal parameter missing after '#'
indicating following line:
static void _int_##INT_NAME() { (*isr_##INT_NAME)(); }
I guess preprocessor directives cannot be used in #defines? Is there any work around?
C99 has the new
_Pragmakeyword that lets you place#pragmainside macros. Basically it expects a string as an argument that corresponds to the text that you would have give to the#pragmadirective.If your compiler doesn’t support this (gcc does) and you’d go for an external implementation of what you need (as said,
m4could be a choice) the best would probably be to stay as close as possible to that not-so-new_Pragma. Then once your compiler builder catches up with the standard you could just stop using your script.