What is the easiest way to create an array of pointers to hardware registers?
For example, let's take GPIO module. On the micro I am using now there are 2 ports but I don't want to refactor the code if I ever need to port it to other micro with more (or less) GPIO ports. For the sake of the argument let's assume there are maximum 4 ports on any micro and ports numbers are always sequential (i.e. the situation when the micro has port 4 but no port 1 is impossible).
#ifdef __MSP430_HAS_PORT4__
#define MAX_PORTS 4
#else
#ifdef __MSP430_HAS_PORT3__
#define MAX_PORTS 3
#else
#ifdef __MSP430_HAS_PORT2__
#define MAX_PORTS 2
#else
#ifdef __MSP430_HAS_PORT1__
#define MAX_PORTS 1
#else
#error "No GPIO ports on this micro!"
#endif
#endif
#endif
#endif
typedef struct GPIO_PORT_REGISTERS_STRUCT
{
u8 *In;
u8 *Out;
u8 *Dir;
u8 *Sel;
u8 *Iv;
u8 *Ies;
u8 *Ie;
u8 *Ifg;
} GpioPortRegisterStruct;
/* Let's assume we only have 2 ports in this example */
const GpioPortRegisterStruct GpioPortRegisters[MAX_PORTS]= {
{P1IN, P1OUT, P1DIR, P1SEL0, P1IV, P1IES, P1IE, P1IFG },
{P2IN, P2OUT, P2DIR, P2SEL0, P2IV, P2IES, P2IE, P2IFG }
};
The example above doesn't compile. I think this is because compiler is confused by the registers name as each register is defined twice:
1. In msp430xxxxx.h file as SFR_8BIT
2. In msp430xxxxx.cmd linker command file where it is set as an address.
What is the best way to make it work? I know I can simply assign pointers manually using data from .cmd (hardcode addresses) but then the software won't be portable anymore. Any ideas?