STM32 register access

Publisher:敬亭山人Latest update time:2018-08-26 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

General register access needs to be implemented through the three steps of read-modify-write and bit operations of clearing and setting. However, in the programming of STM32, by utilizing some of its excellent features such as port bit set/reset register BSRR, bit binding, etc., we can greatly improve the access speed of registers and simplify the operation of registers.


//General register operations:

GPIOx->ODR |= 0x10; //Pin4 set to 1

GPIOx->ODR &= ~0x10; //Pin4 cleared to 0

BSRR/BRR Register 
Write the picture description here
Write the picture description here

GPIOx->BSRR //Write 1 to set the lower 16 bits of BSRR, and write 1 to clear the upper 16 bits of BSRR

GPIOx->BRR //Write 1 to clear the lower 16 bits of BRR, and keep the upper 16 bits of BRR


It can be seen that operating the ODR register through the BSRR/BRR register does not require the three steps of read-modify-write. It can be done in one step by just writing, which is much more convenient.


Bit Binding 

Of course, stm32 has an even more powerful feature - bit binding, which can realize single bit operation in just one clock cycle. Bit binding is to map a bit in a register to a storage unit in memory through a simple address conversion. In this way, a bit of the corresponding register can be indirectly accessed by reading and writing a memory unit. Of course, at this time, only the lowest bit of the 32-bit memory unit is valid!


However, the entire M3 core does not allow bit binding, only two areas do, namely

SARM: 0x20000000~0x2000FFFF 1M size 

The address bound to this area starts from 0x22000000;  

PERIPHERALS: 0x40000000~0x4000FFFFF 1M size

The address bound to this area starts from 0x42000000;


The corresponding bit binding formula is:  

SRAM:AliasADDr = 0x22000000+((A-0x20000000)*32+n*4)

Where A: 0x20000000~0x2000FFFF n: 0~31

PERIPHERALS: AliasADDr = 0x42000000+((A-0x40000000)*32+n*4)

Where A: 0x40000000~0x4000FFFFF n: 0~31

Now we can quickly implement bit operations through bit binding 
Write the picture description here
Write the picture description here

#define GPIOA_ODR_ADDR (GPIOA_BASE + 0x0C)

#define GPIOA_IDR_ADDR (GPIOA_BASE + 0x08)


#define BitBind(Addr, bitNum) (*(volatile unsigned long *)((Addr&0xF0000000)+0x2000000+((Addr&0xFFFFF)<<5)+(bitNum<<2)))

//Addr&0xF0000000 is to distinguish SRAM or PERIPHERALS

//Addr&0xFFFFF is equivalent to (A-0x20000000) or (A-0x40000000)

//Left shift is to achieve fast multiplication operation: left shift n bits is equivalent to multiplying by 2^n


#define PAout(n) BitBind(GPIOA_ODR_ADDR, n) 

#define PAin(n) BitBind(GPIOA_IDR_ADDR, n)


This implements an operation similar to the 51 microcontroller access to I/O


sbit P10 = P1^0

P10 = 0; or P10 = 1;


PAout(3) = 1; or PAout(3) = 0;   


Pretty cool, huh!


Keywords:STM32 Reference address:STM32 register access

Previous article:Comparison of library functions and register operations under STM32
Next article:Some good programming ideas, methods and good programs in the STM32 library

Recommended ReadingLatest update time:2024-11-16 19:48

Some questions about STM32 send interrupt
SECTION 2 Let's talk about TC first. It stands for Transmission Complete. The interrupt is triggered after a byte is sent, which is called "interrupt after sending". It is the same as the original 8051 TI method, which is to interrupt after sending. You need to send a byte in the sending function to trigger the int
[Microcontroller]
Understanding of the STM32 quadrature encoder edge
The TIM_EncoderInterfaceConfig() function in STM32 configures the timer to have encoder interface and other functions. Generally, only channel 1 and channel 2 of the timer can be used as the input port of the encoder, corresponding to the two output ports of the encoder.  The TIMx parameter is which timer is used as t
[Microcontroller]
Understanding of the STM32 quadrature encoder edge
STM32 learning: discussion on timer programming
Assume that timer 3 is used to time every 1 millisecond; the function saved to the SD card is StartSave(); Case 1: timer is fast, main loop is slow 1. Code design 1 (wrong design) view plain copy int cnt = 0; //Counting   //TIM3 interrupt handling function   void TIM3_IRQHandler(void)   {       if(TIM_GetITStatus(TIM
[Microcontroller]
How to confirm whether the STM32 clock configuration is correct
Configure STM32F103 clock (HSI) to 48M void SystemClock_Config(void) {     RCC_DeInit();     RCC_HSICmd(ENABLE);     while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);     RCC_HCLKConfig(RCC_SYSCLK_Div1);          RCC_PCLK1Config(RCC_HCLK_Div2);     RCC_PCLK2Config(RCC_HCLK_Div1);       RCC_PLLConfig(RCC_PLLS
[Microcontroller]
stm32 gpio configuration method
The STM32 input and output pins have the following 8 possible configurations: (4 inputs + 2 outputs + 2 multiplexed outputs) ① Floating input_IN_FLOATING ② With pull-up input_IPU    ③ With pull-down input_IPD                      ④ Analog input_AIN ⑤ Open drain output_OUT_OD          ⑥ Push-pull output_OUT_PP
[Microcontroller]
LCD displays a small number of Chinese characters in STM32
How to display Chinese characters (only Chinese characters) on the LCD display in STM32? The following is my little opinion on this issue. If you have any questions, I hope readers will leave a message; The following program mainly operates on the library functions in STM32: To display Chinese characters on LCD, y
[Microcontroller]
STM32 switch total interrupt
In STM32/Cortex-M3, interrupts are enabled or disabled by changing the current priority of the CPU.    PRIMASK bit: Only NMI and hard fault exceptions are allowed, and other interrupts/exceptions are masked (current CPU priority = 0).   FAULTMASK bit: Only NMI is allowed, and all other interrupts/exceptions are masked
[Microcontroller]
STM32 SPI mode
SPI protocol (Serial Peripheral Interface), also known as serial peripheral interface, is a high-speed full-duplex Communication bus, it was proposed by Motorola, and the latest version is V04.01-2004. There are four SPI modes according to the different configurations of SPI clock polarity (CPOL) and clock phase
[Microcontroller]
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号