Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Iam trying to make an easy way of defining my IO. Im trying to do this with macro but I cant solve this problem.

I did this:

// Buzzer PORT and PIN mapping
#define BUZZER_PORT     B       // PORT
#define BUZZER_PIN      2       // PCR pin
#define BUZZER_ALT      1       // Pin alternativne function

#define INIT_BUZZER(PORTX, PIN, ALT) { PORT##PORTX##_PCR(PIN) = PORT_PCR_MUX((ALT)) |     PORT_PCR_DSE_MASK;            GPIO##PORTX##_PDDR |= PIN<<1; }

Function call working:

INIT_BUZZER(B, BUZZER_PIN, BUZZER_ALT);

Function call wanted:

INIT_BUZZER(BUZZER_PORT, BUZZER_PIN, BUZZER_ALT);

If I call it with port argument BUZZER_PORT I get an error because compiler doesnt take my arguments value but string itself.

How to deal with this?

Thank you.

share|improve this question
add comment (requires an account with 50 reputation)

2 Answers

use brackets as most as possible.

try below: INIT_BUZZER((BUZZER_PORT), BUZZER_PIN, BUZZER_ALT);

share|improve this answer
add comment (requires an account with 50 reputation)

You need an indirection:

#define INIT_BUZZER_(PORTX, PIN, ALT)                                   \
    do {                                                                \
        PORT##PORTX##_PCR(PIN) = PORT_PCR_MUX(ALT) | PORT_PCR_DSE_MASK; \
        GPIO##PORTX##_PDDR |= (PIN)<<1;                                 \
    } while (0)

#define INIT_BUZZER(PORTX, PIN, ALT)                                    \
        INIT_BUZZER_(PORTX, PIN, ALT)

Also note the do {...} while(0) and no semi-colon, which is the normal way to enclose several lines in a macro

share|improve this answer
Thank you. In that situation it works. What if I want to define a macro of toggle pin without argument something like this: – user2566355 Jul 10 at 8:02
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.