also, the method you are describing will parse an error during preprocessing:
PORT_SET_OUTPUT(RF_SPI0_MOSI_PORT, RF_SPI0_MOSI_BIT) -> Description Resource Path Location Type
#20 identifier "PRF_SPI0_MOSI_PORTDIR" is undefined main.c /CC1200_Tester/src line 40 C/C++ Problem
It currently will only work with numerical values.
That was my bad. I did something incorrect, but happily the error led to the solution!
I need to explain something to you. Ordinarily, macros are not expanded when you use them in stringification # or pasting ##:
Code:
#define CAT dog
#define FOOD hot_##CAT
FOOD
FOOD produces hot_CAT, not hot_dog like we want, but you can get around it. Arguments to a macro must be expanded before they are passed to another macro though, so we should be able to do what you want. One macro to expand, and one to do the pasting.
Just to show it with the example:
Code:
#define CAT dog
#define NOEXPAND(A) hot_##A
#define FOOD(A) NOEXPAND(A)
FOOD(CAT)
produces hot_dog as wanted. So we can do what you want with pasting PORT and BIT, it just takes careful control of where things expand and where they shouldn't.
Code:
C:\Users\jk>more macros.c
#define PASTE_BIT(m) m##_BIT
#define PASTE_PORT(m) m##_PORT
#define USE_PORT(port) P##port##DIR
#define PORT_SET_OUTPUT(port, pin) USE_PORT(port) |= 1 << (pin)
/* you must define ports and pins at this point */
#define RF_SPI0_MOSI_PORT 3
#define RF_SPI0_MISO_PORT 3
#define RF_SPI0_SCLK_PORT 3
#define LED_RED_PORT 4
#define RF_SPI0_MOSI_BIT 1
#define RF_SPI0_MISO_BIT 2
#define RF_SPI0_SCLK_BIT 3
#define LED_RED_BIT 0
PORT_SET_OUTPUT(PASTE_PORT(RF_SPI0_MOSI), PASTE_BIT(RF_SPI0_MOSI))
PORT_SET_OUTPUT(PASTE_PORT(RF_SPI0_MISO), PASTE_BIT(RF_SPI0_MISO))
PORT_SET_OUTPUT(PASTE_PORT(RF_SPI0_SCLK), PASTE_BIT(RF_SPI0_SCLK))
PORT_SET_OUTPUT(PASTE_PORT(LED_RED), PASTE_BIT(LED_RED))
C:\Users\jk>gcc -E macros.c
# 1 "macros.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "macros.c"
# 21 "macros.c"
P3DIR |= 1 << (1)
P3DIR |= 1 << (2)
P3DIR |= 1 << (3)
P4DIR |= 1 << (0)
So that should be good. Hopefully the explanation is clear so that you can do this yourself. I notice that in some other examples you posted you use macros that I haven't seen before, but the solution for those would be similar to this including possibly functions like USE_PORT(x) so they expand to what you want.