Calculating peripheral register addresses
In the previous post, I explained how to enable the peripheral clock of the GPIOD peripheral.
We do that by using the RCC register and the RCC register in our case is AHB1ENR.
Let’s calculate the address of the Clock control register(AHB1ENR).
Address of the Clock control register(AHB1ENR)
First, we have to take the base address and then we have to add the offset.
The base address of RCC is 0x40023800 and the offset is 0x30.
Address of the Clock control register(AHB1ENR) → 0x40023800 + 0x30 = 0x40023830
We will get 0x40023830.
Address of the GPIOD mode register (used to control mode)
Address of the GPIO mode register, so this we need to control the mode.
What is the base address of the GPIOD peripheral register?
The base address of the GPIOD peripheral register is 0x40020C00.
0x40020C00 is a base address. For that, you have to add the offset.
If you want to get the offset you have to go to the GPIO section. And go to GPIO registers, and the first register is the GPIO port mode register. Here, you have to configure the mode.
You can see that(Figure 3), reset this register is 0 for Port D. So, it is non-zero only for port A and port B, but for port D this register reset value is 0.
And here in this register, you can see that 32 bits are divided into 16 sections. MODER0 is the first section, MODER1 is the second section, and MODER15 is the 15th section.
Every 2 bits are used to configure the mode for 1 pin of the port. Each port has 16 pins. So, the mode for all those pins is controlled using this register. So, in this register, every two bits are given to control the mode for a single pin.
Why two bits are dedicated?
Because there are 4 different modes.
- Input(reset state)
- General purpose output mode
- Alternate function mode
- Analog mode
For this exercise, we will be keeping the pin in output mode. That’s why you have to go to the appropriate section here.
For pin-12 we should be looking at 24 and 25-bit fields. And here you have to program the values 0 and 1. So, if you do that the pin number 12 of the GPIOD peripheral will be in output mode.
Let’s calculate the address. The address offset is 0.
Address of the GPIOD mode register → 0x40020C00 + 0x00 = 0x40020C00.
We get this address 0x40020C00.
Address of the GPIOD output data register (used to write)
Next, calculate the address of the GPIOD output data register, this is used to write to the IO pins. Let’s again explore other GPIO registers.
GPIO output data register is shown in Figure 4. This is also a 32 bit register, but here from 16 to 31 bits are not used, they are reserved, so only 16 bits are implemented. So, these bit fields are used to control the different pins of a port.
In this case, bit 0 controls the 0th pin of the port, bit field 1 controls the first pin, bit field 2 for the second pin, all the way to the 15th pin. So, here we will be programming 1 into the bit field 12 in order to make LED high. And 0x14 is the offset.
Let’s calculate the address of the GPIOD Output data register. The base address is 0x40020C00, the address offset is 14.
Address of the GPIOD output data register → 0x40020C00 + 0x14 = 0x40020C14.
Now, we get this address 0x40020C14.
FastBit Embedded Brain Academy Courses
Click here: https://fastbitlab.com/course1