Electronic – What does _SFR_IO8(0x04) do? AVR

assemblyavrmicrocontrollerportremappable-pins

I'm trying to write a program in assembly to toggle the on-board LED of the Arduino MEGA2560.

To do so I just have to indicate the direction of the pin (13 in this case which is PB7) as output and then set that pin as High or Low.

I understand that there are two registers related with this problem. One is the port's direction register (DDRB) and the register for the state of the pins (PORTB.)

I'm trying to understand what number or address those registers have so I can assign values to them in assembly. I went into the C++ library in which those constants are defined (iomxx0.h)
and for example DDRB is defined the following way:

#define DDRB    _SFR_IO8(0x04)
#define DDB7    7
#define DDB6    6
#define DDB5    5
#define DDB4    4
#define DDB3    3
#define DDB2    2
#define DDB1    1
#define DDB0    0

Is the register's address 0x04?

If that is the case, what is _SFR_IO8() doing?

Best Answer

It is a macro to access AVR IO address space registers, and in that address space, register 4 seems to be DDRB. You might want to actually use the datasheet as the register reference, instead of digging the info from avr-libc headers.

The macro _SFR_IO8() is actually a macro to call _MMIO_BYTE((io_addr) + __SFR_OFFSET), which in turn is a macro to just access the byte of memory pointed by the address like this: (*(volatile uint8_t *)(mem_addr))