Memory mapped peripheral registers and IO access
This article delves into the concept of peripheral registers and how they are used in STM32 microcontrollers.
About peripheral registers
- All peripheral registers in STM32 microcontroller are of 32 bits wide.
- Different peripherals have different number of peripheral registers.
For example, a GPIO peripheral may have 10 registers, ADC may have 15, and I2C may have 20, so that depends upon the complexity of the peripheral.
Simple peripheral may have less number of peripheral registers. A complex peripheral like Timer, or Ethernet, MAC, CAN have more number of registers.
- You should never assume about the address of the peripheral registers. Always you should refer to the device reference manual.
These are the peripheral registers of GPIOD peripheral(Figure 1).
GPIOA, GPIOB, and GPIOC also have the same set of registers, but I’m considering GPIOD because we’ll be working on GPIOD peripherals.
GPIOD peripherals has up to 10 registers, as shown in Figure 1. Each register is 32 bits.
GPIOD port mode register: It is used to control the mode of an IO pin, whether you want to drive that IO pin as an input or output.
For example, if you are driving an LED, then you probably configuring the mode of an IO pin as output. If you are using an IO pin to connect a button, in that case, you probably want to configure the IO pin as an input. So, for all those configurations you do use the mode register of the GPIO port.
GPIOD port output type register: It decides what should be the output type of an IO pin if it is in output mode.
GPIOD port pull-up/pull-down register: It is one important register to activate or deactivate the internal pull-up resistors for the IO pins.
GPIOD port input data register: If you want to read data from the IO pins, then you probably would be using an input data register. So, this is used to read data from the IO pins.
GPIOD port output data register: It is used to write data into the IO pins.
So, to turn on or turn off an LED you probably use an output data register. Because through the output data register we actually write data into the IO pins.
And there are other registers that we will not be using for this exercise, so that’s why we need not explore all registers.
For this exercise, we should be using the mode register and output data register. The mode register is to configure the mode and the output data register is to configure write data.
For example, if you want to turn on the LED, then you have to write 1 into the output data register. If you want to turn off the LED, then you have to write 0. So, we’ll see that when we do programming.
These are the register sets of the GPIOD peripheral. All these registers everything is implemented inside the microcontroller.
Register Arrangement
Let’s see how they are arranged.
For example, the first register you find in the address range of GPIOD peripheral registers is the GPIOD mode register. That’s the very first register you find at the address 0x4002_0C00. As I said each register is of 32 bits. That’s why, GPIOD port MODE register consumes these many memory locations, as shown in Figure 3. 0x4002_0C00 to 0x4002_0C03 which is 32-bit width.
The GPIOD port output type register is the next register, which starts from 0x4002_0C04 at this address onwards.
GPIOD peripheral has 10 registers and 0x4002_0C00, 0x4002_0C04, 0x4002_0C08, 0x4002_0C0C, 0x4002_0C24 these are the addresses of the individual registers.
0x4002_0C00 is the address of the mode register, 0x4002_0C04 is the address of the output type register, like that.
So, if you just add +4 to the address, you get the address of the next register.
How do you control the output state of the GPIO pin?
You must be using the GPIOD output data register because through the output data register we send data to the IO pins.
For example, you can easily calculate the address of the GPIOD Output data register. so you already know how to calculate that.
The 0th-bit position of this register controls the 0th pin of the GPIOD port. So, the 0th-bit field controls PD0. If you make 0th bit field as high that is 1, then that makes the state of the PD0 as high, that means PD0 will be pulled to +VCC, as shown in Figure 6. So, that is actually 3.3 volts.
If you make this bit field 0, then PD0 will be pulled to the ground, it is like driven to ground 0 volts.
How do you control the PD12 where our LED is connected?
You should be touching the 12th bit of this GPIOD register. If you want to turn on the LED make the 12th-bit position as 1, so if you want to turn off the LED make the 12th-bit position of this register as 0. That’s all you have to do.
In the following lecture, let’s understand the procedure to turn on the LED.
Get the Full Course on Microcontroller Embedded C Programming Here.
FastBit Embedded Brain Academy Courses
Click here: https://fastbitlab.com/course1