Preprocessor gurus help requested!

Hello preprocessor masters/gurus,

I am trying to make a piece of software as easily configurable as possible, by only indicting which port/pin combination controls the hardware:

// SPI master/slave arbitration
#define SPI_MASTER_IO_PORT                      0
#define SPI_MASTER_IO_PIN                       19

#define FIO_PORT_PIN(x)                         FIO##x##PIN

#define SPI_MASTER_REQUEST_IO_BIT_STATUS          ( (FIO_PORT_PIN(SPI_MASTER_IO_PORT)>>SPI_MASTER_IO_PIN)&1)

The result I want is this:

((FIO0PIN>>19)&1)

but this yields the following error message:

..\src\spi.c(341): error:  #20: identifier "FIOSPI_MASTER_IO_PORTCLR" is undefined</ptr>

Are there ways around the apparent failure of the double replacement required here...?

Thanks in advance


Parents
  • Ah, it is good to have colleagues and the internet/google! here is the solution:

    #define FIO_PORT_PIN_TEMP(x)                               FIO##x##PIN
    #define FIO_PORT_PIN(x)                         FIO_PORT_PIN_TEMP(x)
    

    logic:

    Macros that call other macros that stringify or concatenate.
    If an argument is stringified or concatenated, the prescan does not occur. If you want to expand a macro, then stringify or concatenate its expansion, you can do that by causing one macro to call another macro that does the stringification or concatenation. For instance, if you have
              #define AFTERX(x) X_ ## x
              #define XAFTERX(x) AFTERX(x)
              #define TABLESIZE 1024
              #define BUFSIZE TABLESIZE
    
    then AFTERX(BUFSIZE) expands to X_BUFSIZE, and XAFTERX(BUFSIZE) expands to X_1024. (Not to X_TABLESIZE. Prescan always does a complete expansion.)
    

Reply
  • Ah, it is good to have colleagues and the internet/google! here is the solution:

    #define FIO_PORT_PIN_TEMP(x)                               FIO##x##PIN
    #define FIO_PORT_PIN(x)                         FIO_PORT_PIN_TEMP(x)
    

    logic:

    Macros that call other macros that stringify or concatenate.
    If an argument is stringified or concatenated, the prescan does not occur. If you want to expand a macro, then stringify or concatenate its expansion, you can do that by causing one macro to call another macro that does the stringification or concatenation. For instance, if you have
              #define AFTERX(x) X_ ## x
              #define XAFTERX(x) AFTERX(x)
              #define TABLESIZE 1024
              #define BUFSIZE TABLESIZE
    
    then AFTERX(BUFSIZE) expands to X_BUFSIZE, and XAFTERX(BUFSIZE) expands to X_1024. (Not to X_TABLESIZE. Prescan always does a complete expansion.)
    

Children
More questions in this forum