Search code examples
cmacroshardwareconfigurefirmware

using macros for configuration


I'm writing firmware in C for an embedded processor. I want to have all the configuration information in one header file called config.h. This is causing problems with the ADC initialization, where simple #defines won't do the trick. Right now the code is like so:

config.h

#define NUMBER_OF_POTS  1
#define POT_1_CHANNEL  27

adc.c

#define MAKE_CSS(channel) _CSS##channel
#define CALL_MAKE_CSS(channel) MAKE_CSS(channel)

void initialize_adc() {
   CALL_MAKE_CSS(POT_1_CHANNEL);
}

What I want to do is not have to touch adc.c if I change config.h to:

#define NUMBER_OF_POTS  2
#define POT_1_CHANNEL  27
#define POT_2_CHANNEL  29

adc.c should just automatically add a second CALL_MAKE_CSS with some macro trickery.

I guess the question is: is there a trick that gives you for loop capability with a macro?

Thanks,

Steve.


Solution

  • I didn't test this:

    // config.h
    
    #define NUMBER_OF_POTS  2
    extern int pots[];
    
    // config.c
    
    int pots[NUMBER_OF_POTS] = {
        27,
        29
    };
    
    
    // adc.c
    
    void initialize_adc() {
        for (int i = 0; i < NUMBER_OF_POTS; i++) {
            CALL_MAKE_CSS(pots[i]);
        }
    }