This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Pointer to hardware registers

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?

  • You didn't list the compilation errors but: Start by putting "&"s in front of the register names. Also, you should probably make them "volatile u8 *".

  • Yes, you're right. The following initialization code seem to work:

    const GpioPortRegisterStruct GpioPortRegisters[]= {
            {(u8 *)&P1IN, (u8 *)&P1OUT, (u8 *)&P1DIR, (u8 *)&P1SEL0, (u8 *)&P1IV, (u8 *)&P1IES, (u8 *)&P1IE, (u8 *)&P1IFG },
            {(u8 *)&P2IN, (u8 *)&P2OUT, (u8 *)&P2DIR, (u8 *)&P2SEL0, (u8 *)&P2IV, (u8 *)&P2IES, (u8 *)&P2IE, (u8 *)&P2IFG }
    };

**Attention** This is a public forum