Electronic – Are the AVR GPIO registers used by AVRGCC

avravr-gccxmega

Most (all?) of the AVR family controllers provide some special GPIO registers.

4.8.1 General Purpose I/O Registers
The lowest 16 I/O Memory addresses is reserved for General Purpose I/O Registers. These registers can be used for storing information, and they are particularly useful for storing global variables and flags, as they are directly bit-accessible using the SBI, CBI, SBIS, and SBIC instructions.

Q: Are those registers actually used by compilers, especially AVRGCC? Or would one be free to use those registers in some inline ASM for example without messing things up? After some digging, I haven't found anything conclusive.


Background:
Controller: XMEGA128A1
In a recent project, I discovered that an uninitialized pointer to a struct (size = 9 bytes) was dereferenced (and "written" to), potentially corrupting data at address 0x0000. This address is located in the I/O memory section instead of the SRAM area.

enter image description here

The first 16 bytes are used by the 16 x 8-bit GPIO registers available for this controller.

enter image description here

As I discovered this bug (that has actually been present for some time) the question came up, why there were no noticeable ramifications. So it would be interesting to know, if those registers are used at all or if we just got lucky so far.

Best Answer

Peripheral registers are not accessed by GCC unless you specifically write code to access them.

There is a difference between CPU registers and peripheral registers. The CPU registers are in the CPU core. Peripheral registers reside in the peripheral blocks.

Peripheral registers are not used by GCC to compute any logic or math expressions or to manage the stack. Peripheral registers are only accessed when you specifically write code to access them.

//EXAMPLE PORTA = 0xFF ;//set all bits on port A to 1, writes to peripheral register PORTA

//EXAMPLE X = ((Y+Z)*76+P);//Calculate something, doesnt use peripheral registers.

If there are registers are implemented in your device and they don't connect to any used IO pins then you can probably use them as scratch registers without any problems.