Pergunta

I'm working with embedded C for a microcontroller right now and I find that sometimes there are several peripherals that differ only by a single letter (for example UARTA, UARTB, etc). The code for each peripheral is often times identical except for lettering of otherwise identical registers. For example to setup peripheral A I would do something like:

UCA2CTL1 |= UCSWRST;                      // Put state machine in reset
UCA2CTL0 |= UCSYNC+UCCKPL+UCMSB;          // 3-pin, 8-bit SPI slave
// Continue initializing peripheral registers with "A" in name

And to setup peripheral B I have exactly the same code, except that the register names are transposed by 1 letter:

UCB2CTL1 |= UCSWRST;                      // Put state machine in reset
UCB2CTL0 |= UCSYNC+UCCKPL+UCMSB;          // 3-pin, 8-bit SPI slave
// Continue initializing peripheral registers with "B" in name

I would like to have the ability to change which peripheral I target without having to #ifdef/copy/paste code or find/replace. Is there some compiler directive or clever trick that can implement this behavior so that I only have to write the code once? I would love to just #define the last letter in the peripheral, but something like that seems to wander dangerously close to code stink to me.

Foi útil?

Solução

Will this work for you?

#define INITUC(device) \
UC ## device ## 2CTL1 |= UCSWRST; \
UC ## device ## 2CTL0 |= UCSYNC+UCCKPL+UCMSB
...
INITUC(A);
INITUC(B);

Outras dicas

Assuming those constants are const and not defines You can do something like that:

#define MAKECONST(X) const int X ## 1; \
                     const int X ## 0; \
                     X ## 1 |= UCSWRST; \
                     X ## 0 |= UCSYNC+UCCKPL+UCMSB;

And then:

MAKECONST(UCA2CTL)
MAKECONST(UCB2CTL)

Note that my example includes declaration, which I don't know if you need, if not, omit first two lines of the define. E.g.:

#define SETUP(X) X ## 1 |= UCSWRST; \
                 X ## 0 |= UCSYNC+UCCKPL+UCMSB;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top