We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
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
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.)
So the final macros look like this:
// SPI master/slave arbitration #define SPI_MASTER_IO_PORT 0 #define SPI_MASTER_IO_PIN 19 #define SPI_MASTER_DISPLAY_PORT 0 #define SPI_MASTER_DISPLAY_PIN 5 #define FIO_PORT_PIN_TEMP(x) FIO##x##PIN #define FIO_PORT_PIN(x) FIO_PORT_PIN_TEMP(x) #define FIO_PORT_CLR_TEMP(x) FIO##x##CLR #define FIO_PORT_CLR(x) FIO_PORT_CLR_TEMP(x) #define FIO_PORT_SET_TEMP(x) FIO##x##SET #define FIO_PORT_SET(x) FIO_PORT_SET_TEMP(x) #define SPI_MASTER_REQUEST_IO_IS_MASTER ( ( (FIO_PORT_PIN(SPI_MASTER_IO_PORT)>>SPI_MASTER_IO_PIN)&1) == 0) #define SPI_MASTER_REQUEST_IO_NOT_MASTER ( ( (FIO_PORT_PIN(SPI_MASTER_IO_PORT)>>SPI_MASTER_IO_PIN)&1) == 1) #define SPI_MASTER_REQUEST_IO_BIT_SET FIO_PORT_CLR(SPI_MASTER_IO_PORT) |= (1<<SPI_MASTER_IO_PIN) ; #define SPI_MASTER_REQUEST_IO_BIT_RESET FIO_PORT_SET(SPI_MASTER_IO_PORT) |= (1<<SPI_MASTER_IO_PIN) ; #define SPI_MASTER_REQUEST_DISPLAY_BIT_IS_MASTER ( ( (FIO_PORT_PIN(SPI_MASTER_DISPLAY_PORT)>>SPI_MASTER_DISPLAY_PIN)&1) == 0) #define SPI_MASTER_REQUEST_DISPLAY_BIT_NOT_MASTER ( ( (FIO_PORT_PIN(SPI_MASTER_DISPLAY_PORT)>>SPI_MASTER_DISPLAY_PIN)&1) == 1) #define SPI_MASTER_REQUEST_DISPLAY_BIT_SET FIO_PORT_CLR(SPI_MASTER_DISPLAY_PORT) |= (1<<SPI_MASTER_DISPLAY_PIN) ; #define SPI_MASTER_REQUEST_DISPLAY_BIT_RESET FIO_PORT_SET(SPI_MASTER_DISPLAY_PORT) |= (1<<SPI_MASTER_DISPLAY_PIN) ;