I am trying to define peripheral ports in an embedded application as variables at fixed addresses (e.g. PortA is at address 0x10010). I could do this using #define:

#define PortA *(u32*)0x10010

However, this wouldn't allow me to view the value of PortA in a watch window when debugging, since there is no variable named PortA.

An alternative is to make a declaration in a header file:
extern u32* PortA;

then a definition in a source file:
u32* PortA = (u32*)0x10010;

I can then read/write to the port using the indirection *PortA, which can also be viewed in a watch window. I have proved this using a C compiler for a PIC processor, but it doesn't work in GCC. GCC gives the cryptic error "previous declaration of 'PortA' was here" at the line in the header file, and "declaration of 'Test' with no linkage follows extern" in the source file.

Incidentally the PIC compiler (PICC from Hi-Tech) also provides the provision of defining an address for a variable, for example:
static volatile near unsigned char TBLPTRH @ 0xFF7;

This defines a variable name TBLPTRH at memory location 0xFF7.

Can anyone suggest an alternative way to do it in GCC?

if you are using gcc for *nix or ms-windows, then there is no way to do it because those operating systems will not allow it.

No, I am using GCC for an ARM processor (namely an STM32).

Could you make due with doing

const u32* PortA = (u32*)0x10010;

without the define present and attempting to monitor *PortA ?

This will consume some extra space, so you may want to conditionally define PortA under DEBUG or something.

This article has been dead for over six months. Start a new discussion instead.