STM32 GPIO configuration: ODR, BSRR, BRR detailed explanation

Publisher:sdlg668Latest update time:2018-08-26 Source: eefocusKeywords:STM32  GPIO  ODR  BSRR  BRR Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Use the configured GPIO of stm32 to control the LED display status, and use ODR, BSRR, and BRR to directly control the pin output status.

The ODR register is readable and writable: it can control the pin to a high level or a low level.

For the pin, writing 1 to the gpio pin is high level, and writing 0 is low level.

BSRR Write-only register: [color=Red] can control the pin to be high or low.
Writing 1 to the upper 16 bits of the register will cause the corresponding pin to be low, and writing 1 to the lower 16 bits of the register will cause the corresponding pin to be high. Writing 0, no action

BRR Write-Only Register: It can only change the pin status to low level. If you write 1 to the corresponding bit of the register pin, the corresponding pin will be low level. If you write 0, there will be no action.

At the beginning, you may have the following doubts like me:

1. Since ODR can control the high and low levels of the pin, why do we need BSRR and SRR registers?
2. Since BSRR can achieve all the functions of BRR, why do we need SRR registers?

For question 1 ------ STMicroelectronics' answer is ---

“This way, there is no risk that an IRQ occurs between the read and the modify access.”
What does this mean? It means that when you use BSRR and BRR to change the pin state, there is no risk of being interrupted by an interrupt. There is no need to disable interrupts.

The pseudo code for operating GPIO with ODR is as follows:

disable_irq()
save_gpio_pin_sate = read_gpio_pin_state();
save_gpio_pin_sate = xxxx;
chang_gpio_pin_state(save_gpio_pin_sate);
enable_irq();

Disabling interrupts will obviously delay or lose the capture of an event, so it is best to use SBRR and BRR to control the state of GPIO.

Regarding question 2 ------- Based on my personal experience, STMicroelectronics probably did this just to make it easier for programmers to operate.

Because the lower 16 bits of BSRR happen to be the set operation, while the upper 16 bits are the reset operation and the lower 16 bits of BRR are the reset operation.

Simply put, the upper 16 bits of GPIOx_BSRR are called the clear register, and the lower 16 bits of GPIOx_BSRR are called the set register.

Another register GPIOx_BRR has only the lower 16 bits valid and has the same function as the upper 16 bits of GPIOx_BSRR.

Here is an example to illustrate how to use these two registers and the advantages they provide.

For example, the 16 IOs of GPIOE are all set as outputs, and each operation only requires

Change the lower 8 bits of data while keeping the upper 8 bits unchanged. Assuming the new 8 bits of data are in the variable Newdata,

This requirement can be achieved by operating these two registers. There are two functions in the STM32 firmware library

GPIO_SetBits() and GPIO_ResetBits() use these two registers to operate the port.

The above requirements can be achieved as follows:

GPIO_SetBits(GPIOE, Newdata & 0xff);
GPIO_ResetBits(GPIOE, (~Newdata & 0xff));

You can also directly operate these two registers:

GPIOE->BSRR = Newdata & 0xff;
GPIOE->BRR = ~Newdata & 0xff;

Of course, you can also complete the operation of 8 bits at a time:

GPIOE->BSRR = (Newdata & 0xff) | ( (~Newdata & 0xff)<<16 );

Of course, you can also complete the operation of 16 bits at a time:

GPIOE->BSRR = (Newdata & 0xffff) | ( (~Newdata )<<16 );

From the last operation, you can see that using the BSRR register, you can implement the simultaneous modification of 8 port bits.

Some people ask whether the upper 16 bits of BSRR are redundant. Please see the following example:

If you want to set bit 7 of GPIOE to '1' and bit 6 to '0' in one operation, it is very convenient to use BSRR: 
  GPIOE->BSRR = 0x400080; 

If there is no upper 16 bits of BSRR, the operation will be divided into two times, resulting in the asynchronous changes of bit 7 and bit 6! 
  GPIOE->BSRR = 0x80; 
  GPIOE->BRR = 0x40;

Another feature of BSRR is that the level of Set is higher than that of Reset.

That is to say, the same bit is set and reset, and the final result is Set

To synchronize the changes just simply GPIOx->BSRR = 0xFFFF0000 | PATTEN;

No need to consider which ones need to be set to 1 and which ones need to be cleared to 0.

From the last operation, we can see that using the BSRR register, the 8 port bits can be modified simultaneously.


Keywords:STM32  GPIO  ODR  BSRR  BRR Reference address:STM32 GPIO configuration: ODR, BSRR, BRR detailed explanation

Previous article:About BSRR and BRR registers of STM32_GPIO
Next article:STM32 study notes - GPIO from library functions to registers

Recommended ReadingLatest update time:2024-11-16 16:17

STM32 printf usage
#include //Add the following code to support the printf function without selecting use MicroLIB    #if 1 #pragma import(__use_no_semihosting)              //Support functions required by the standard library                  struct __FILE  {  int handle;  };  FILE __stdout;        // define _sys_exit() to avoid
[Microcontroller]
Detailed explanation of the meaning of the five states of FLASH_Status in STM32
Get status: FLASH_Status FLASH_GetStatus(void); The return value is defined by the enumeration type.  typedef enum {     FLASH_BUSY = 1, //Busy    FLASH_ERROR_PG, //Programming error    FLASH_ERROR_WRP, //Write protection error    FLASH_COMPLETE, //Operation completed    FLASH_TIMEOUT //Operation timeout  } FLASH_S
[Microcontroller]
Common problems and solutions during STM32 debugging?
Common problems and solutions during STM32 debugging 1. After setting the emulator type under the "Debug tab", it prompts "No ULINK Device found." when downloading the program.     Solution: Keil MDK uses the ULINK emulator to download programs by default. Under "Project --- Option for Target 'xxx' --- Utilities tab
[Microcontroller]
STM32 uses PWM to control multiple servos
Preface     I've been playing with a 6-DOF robotic arm recently. The core control device of this robotic arm is the six servos that can rotate 180 degrees. I didn't learn how to use servos in a systematic way at school, so I just used the STM32 to write the driver code myself to drive the six servos. Steering gear c
[Microcontroller]
STM32 uses PWM to control multiple servos
Introduction to STM32 interrupt functions
The NVIC driver has many uses, such as enabling or disabling IRQ (interrupt request), or enabling or disabling a separate IRQ channel, or changing its priority. The following introduces the functions and basic usage of related functions. 1. The function of NVIC_DeInit is to reset the peripheral NVIC registers to defau
[Microcontroller]
Why does STM32 remap boot from Flash address 0x08000000
When I first wrote an STM32 program, I encountered a confusion. The STM32 Flash was set to start at address 0x0800 0000 in MDK, but the CM3 manual stipulates that the interrupt vector should be taken from address 0x0000 0000 when the chip is reset. So how does the STM32 execute the code? Address remapping? Or is there
[Microcontroller]
stm32 DMA performance data
In order to quickly transfer a block of data, the DMA transfer program (Mem to Mem 16bits) was used to measure the transfer time. 128 16b data took about 12us (72Mhz clock). In other words, the STM32 DMA performance is 10M/s      I used memcpy to compare, and it took about 4us, so memcpy is obviously much faster.
[Microcontroller]
Macros for s3c2410 GPIO in Linux header files
1. GPIO register definition 1. #define GPCON(x) __REG2(0x56000000, (x) * 0x10) This sentence defines the control register of 2410's GPIO. Note: the parameter of __REG2 is the physical address of the register. This physical address is converted to a virtual address by the _REG2 macro. By referring to the manual of 24
[Microcontroller]
Macros for s3c2410 GPIO in Linux header files
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号