I'm using X macros to generate a MCU pin configuration table. The table would look like this.
#define IO_DEFS\
/* Port Pin Type Name */\
IODEF(00, 0, input, "Pin A")\
IODEF(00, 1, input, "Pin B")\
IODEF(01, 2, output, "Pin C")\
IODEF(01, 4, output, "Pin D")\
IODEF(02, 0, input, "Pin E")\
IODEF(03, 7, input, "Pin F")\
IODEF(03, 8, output, "Pin G")\
IODEF(03, 9, input, "Pin H")\
But then, among the various expansions that I need to generate from this table I need an enum that looks like this:
typedef enum PORT_IO_Defs_Tag
{
PORT_00,
PORT_01,
PORT_02,
PORT_03,
} PORT_IO_Defs_T;
But the problem is that when creating the macro expansion using the next code:
#undef IODEF
#define IODEF(port, pin, type, name) PORT_##port,
typedef enum PORT_IO_Defs_Tag
{
IO_DEFS
} PORT_IO_Defs_T;
What I'm getting is the following:
typedef enum PORT_IO_Defs_Tag
{
PORT_00,
PORT_00,
PORT_01,
PORT_01,
PORT_02,
PORT_03,
PORT_03,
PORT_03,
} PORT_IO_Defs_T;
I know that this macro expansion is correct but is not what I want since this is generating multiple same values for the enum and compilation errors.
My question here is if anyone know some C Preprocessor trick or technique I could use to achieve this. I was reading this article https://github.com/pfultz2/Cloak/wiki/C-Preprocessor-tricks,-tips,-and-idioms which contain some pretty interesting preprocessor macro techniques but I can't tweak them to ger the results I need.
I need to achieve this without using M4 macro processor if possible.
Regards.
02
and three03
ports if the example is to be believed. – Jonathan Leffler