I'm new to embedded systems programming, and trying to make my way.
Using Stellaris LM4F120 LaunchPad Evaluation Board with datasheet LM4F120H5QR Microcontroller I found to get the full address of some registers you have always to add an offset! which I don't get the importance of it as instead we can use the full address directly!
For example to configure Port F (which starts from
0x4002.5000to0x4002.5FFF)and it's pins (using APB bus)
- Activate clk to this port by setting (bit 5) to 1 in
RCGCGPIOregister which it's Base address is0x400F.E000with Offset0x608so full address is0x400FE608 - Configure the
GPIODIRreg which it's base address is0x4002.5000with offset0x400so full address is0x4002.5400 - Configure the
GPIODENreg which it's base address is0x4002.5000with offset0x51Cso full address is0x4002.551C - Configure the
GPIODATAreg which it's base address is0x4002.5000with0x3FCso full address is0x4002.50x3FC
If I can guess it would be the offset here is used to make it less prone to error as we can write it like this :
#define GPIO_PORTF_BASE 0x40025000
#define GPIO_PORTF_DATA (*((volatile unsigned long *)(GPIO_PORTF_BASE + 0x3FC)))
#define GPIO_PORTF_DIR (*((volatile unsigned long *)(GPIO_PORTF_BASE + 0x400)))
#define GPIO_PORTF_DEN (*((volatile unsigned long *)(GPIO_PORTF_BASE + 0x51C)))
Does using offset increases readability and makes it easier and unsophisticated as We only have to write the offset to get the desired register?
Update
I found that Base address has more usage than obtaining the full address of a register.
for example :
GPIODATAcontrols0-7 pinsand it has 255 registers that can allow us to configure each pin individually and even their combination just by adding an offset to the base address e.g. If we want to configure the Red Led which is onPort Fwe write to the addressbase address 0x4002.5000 + offset 0x008directly.
