Search code examples
cembeddedmicrocontrollerstellaris

what is the purpose of using offset to get register full address in micro-controller?


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 to 0x4002.5FFF)and it's pins (using APB bus)

  1. Activate clk to this port by setting (bit 5) to 1 in RCGCGPIO register which it's Base address is 0x400F.E000 with Offset 0x608 so full address is 0x400FE608
  2. Configure the GPIODIR reg which it's base address is 0x4002.5000 with offset 0x400 so full address is 0x4002.5400
  3. Configure the GPIODEN reg which it's base address is 0x4002.5000 with offset 0x51C so full address is 0x4002.551C
  4. Configure the 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?


Update

I found that Base address has more usage than obtaining the full address of a register.

for example : GPIODATA controls 0-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 on Port F we write to the address base address 0x4002.5000 + offset 0x008 directly.


Solution

  • 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.