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.5000
to0x4002.5FFF
)and it's pins (using APB bus)
RCGCGPIO
register which it's Base address is 0x400F.E000
with Offset 0x608
so
full address is 0x400FE608
GPIODIR
reg which it's base address is 0x4002.5000
with offset 0x400
so full address is 0x4002.5400
GPIODEN
reg which it's base address is 0x4002.5000
with offset 0x51C
so full address is 0x4002.551C
GPIODATA
reg which it's base address is 0x4002.5000
with 0x3FC
so full address is 0x4002.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?
I found that Base address has more usage than obtaining the full address of a register.
for example :
GPIODATA
controls0-7 pins
and 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 F
we write to the addressbase address 0x4002.5000 + offset 0x008
directly.
That's because the header you copied those definitions from is auto-generated from the CMSIS System View Description format. This format is used by chip manufactures to describe the core and peripheral elements of their microprocessors in a standardized way. Usually you can download those so called ".svd" files at some repository or at the manufacturers homepage.
One of those described peripherals of the LM4F120H5QR would be the general purpose IO port F (GPIOF). The .svd file would contain an element for the port with some base-address and then a sub-element for every register the peripheral has with some offset.