Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

#pragma inside #define

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?

like image 722
Donotalo Avatar asked Jul 27 '10 05:07

Donotalo


2 Answers

C99 has the new _Pragma keyword that lets you place #pragma inside macros. Basically it expects a string as an argument that corresponds to the text that you would have give to the #pragma directive.

If your compiler doesn't support this (gcc does) and you'd go for an external implementation of what you need (as said, m4 could 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.

like image 63
Jens Gustedt Avatar answered Nov 05 '22 23:11

Jens Gustedt


A workround is to use code generation or another macro language to preprocess your code.

ie write the code with a different extension.

Have your makefile or similar call the macro language (e.g. m4) or a script of some form to generate a .c file

Then compile that.

like image 1
mmmmmm Avatar answered Nov 06 '22 00:11

mmmmmm