AVR registers access

Hello All,

I’m trying to access the AVR registers in Arduino Mega. By including the necessary header files( avr/io and also for interrupts avr/interrputs.h).

The fuctionality which I’m trying has been achieved (I tried to access Timer 5 and generate an interrupt).
However, I want to try a different approach. Instead of just setting the values to registers, such as OCR5A = 154, I want to make use of bit access. For example, take a look at the snippet below:

#define TIMER5_INTERRUPT_MASK_REGISTER 0x73 //TIMSK5
#define TIMER5_INTERRUPT_FLAG_REGISTER 0x3A //TIFR5

typedef union _timer5_interrupt_masking_register
{
struct _timer5_interrupt_masking_bitfields
{
uint8_t TOIE5 : 1; /*!< [0] Overflow Interrupt Enable /
uint8_t OCIE5A : 1; /
!< [1] Output Compare A Match Interrupt Enable /
uint8_t OCIE5B : 1; /
!< [2] Output Compare B Match Interrupt Enable /
uint8_t OCIE5C : 1; /
!< [3] Output Compare C Match Interrupt Enable /
uint8_t RESERVED0 : 1; /
!< [4] Reserved Bit /
uint8_t ICIE5 : 1; /
!< [5] Input Capture Interrupt Enable /
uint8_t RESERVED1 : 1; /
!< [6] Reserved Bit /
uint8_t RESERVED2 : 1; /
!< [7] Reserved Bit */
} x4;
} timer5_interrupt_masking_register;

#pragma pack(1)
typedef struct _timer5_registers
{
timer5_interrupt_masking_register TIMER5_INTR_MASK_REG; /*!< Interrupt mask register /
timer5_interrupt_masking_register TIMER5_INTR_FLAG_REG; /
!< Interrupt flag register */
} timer5_regsiter_t;
#pragma pack()

By this approach, it makes my work easier by just accessing that bit and setting it low or high. For example, I want to enable a compare match interrupt, I can access corresponding bit on Interrupt Mask register and Mask it.

But the problem is, since Arduino already uses AVR library, defining this structure with the exact register name (which is already being used by AVR library) creates an ambiguity and throws an error.

““exit status 1
expected unqualified-id before numeric constant””

So my question, Is there any other way by which I can access the registers through the User defined structures?
(or)
Where can I find a published header file for Atmega2560 containing all necessary macros and register mapping?

So my question, Is there any other way by which I can access the registers through the User defined structures?

Use your own prefix for the names, so p.e. MY_OCIE5A instead of OCIE5A.

Where can I find a published header file for Atmega2560 containing all necessary macros and register mapping?

That's not one file, there are several dozens. All come in the IDE package you installed on your computer.

pylon:
Use your own prefix for the names, so p.e. MY_OCIE5A instead of OCIE5A.

This is the correct solution.

Don't use the register names (without a prefix, postfix, or like, changing to lowercase or something) for anything other than assigning values to the registers or reading values out of them.

If you want to re-define the already defined identifiers, use #undef to un-define them first.

The definitions of ATmega2560 register bits are included as <avr/io.h> which contains:

#elif defined (__AVR_ATmega2560__)
#  include <avr/iom2560.h>

And <avr/iom2560.h> includes <avr/iomxx0_1.h> which contains 38 kB of declarations.

You can find all that in your Arduino IDE install at: hardware/tools/avr/avr/include/avr