Understanding of STM32 alias area

Publisher:CyborgDreamerLatest update time:2013-02-22 Source: 21IC Keywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. What are bit segments and bit band alias areas?

2. What are the benefits?

Answer 1: Yes, remember MCS51? MCS51 has bit operations, which are operations with one bit (BIT) as the data object.

MCS51 can simply operate the second bit of port P1 independently: P1.2=0;P1.2=1; this is how the third pin (BIT2) of port P1 is set to 0.

Now the bit segment and bit band alias area of ​​STM32 are used to achieve such functions.

The object can be SRAM, I/O peripheral space, and implement operations on a certain bit in these places.

It is like this. In another place of the address space (32-bit address is 4GB), take the alias area space, starting from this address, each word (32BIT)

It corresponds to one bit of SRAM or I/O.

In this way, 1MB SRAM can have 32MB of corresponding alias area space, that is, 1 bit expands to 32 bits (1BIT becomes 1 word)

When we operate a certain word at the beginning of this alias area space, setting it to 0 or 1 is equivalent to operating a certain bit of a certain address corresponding to the SRAM or I/O it maps.

Answer 2: Simply put, the code can be reduced in size, making it faster, more efficient, and safer.

The normal operation requires 6 instructions, but using the bit-band alias area only requires 4 instructions.

The general operation is a read-modify-write method, while the bit-band alias area is a write operation, which prevents interrupts from affecting the read-modify-write method.

// STM32 supports bit-banding operation (bit_band), and bit-banding is implemented in two areas. One is the lowest 1MB range of the SRAM area, and the second is the on-chip peripherals.

// The lowest 1MB range of the area. In addition to being able to be used like ordinary RAM, the addresses in these two areas also have their own "bit-band alias area".

// Expand each bit into a 32-bit word.

//

// Each bit expands into a 32-bit word, that is, 1M is expanded to 32M,

//

// So; RAM address 0X200000000 (one byte) is expanded to 8 32-bit words, which are: (SRAM in STM32 is still 8 bits, so any address in RAM corresponds to one byte content)

// 0X220000000, 0X220000004, 0X220000008, 0X22000000C, 0X220000010, 0X220000014, 0X220000018, 0X22000001C

// The ranges of the two memory areas supporting bit-band operations are:

// 0x2000_0000�x200F_FFFF (lowest 1MB in SRAM area)

// 0x4000_0000�x400F_FFFF (lowest 1MB in the on-chip peripheral area)

/*

For a certain bit in the SRAM bit band area, record its byte address as A, bit number

The address in the alias area is:

AliasAddr= 0x22000000 +((A�x20000000)*8+n)*4 =0x22000000+ (A�x20000000)*32 + n*4

For a bit in the on-chip peripheral bit band area, let the address of the byte where it is located be A, and the bit number be n (0<=n<=7), then the bit

The address in the alias area is:

AliasAddr= 0x42000000+((A�x40000000)*8+n)*4 =0x42000000+ (A�x40000000)*32 + n*4

In the above formula, "*4" means that a word is 4 bytes, and "*8" means that there are 8 bits in a byte.

// Convert "bit band address + bit sequence number" to alias address macro

#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))

//Convert the address into a pointer

#define MEM_ADDR(addr) *((volatile unsigned long *)(addr))

// MEM_ADDR(BITBAND( (u32)&CRCValue,1)) = 0x1;[page]

For example, lighting up an LED

// Using STM32 library

GPIO_ResetBits(GPIOC, GPIO_Pin_4); //Turn off LED5

GPIO_SetBits(GPIOC, GPIO_Pin_7); //Turn on LED2

// General read operation

STM32_Gpioc_Regs->bsrr.bit.BR4 =1; // 1: Clear the corresponding ODRy bit to 0

STM32_Gpioc_Regs->bsrr.bit.BS7 =1; // 1: Set the corresponding ODRy bit to 1

//If you use the bit band alias area operation

STM32_BB_Gpioc_Regs->BSRR.BR[4] =1; // 1: Clear the corresponding ODRy bit to 0

STM32_BB_Gpioc_Regs->BSRR.BS[7] =1; // 1: Set the corresponding ODRy bit to 1

The code is ten times more efficient than the STM32 library!

Bit operations on memory variables.

1. // SRAM variables

2.

3. long CRCValue;

4.

5. // Convert "bit band address + bit sequence number" to alias address macro

6. #define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))

7. //Convert the address into a pointer

8. #define MEM_ADDR(addr) *((volatile unsigned long *)(addr))

9.

10. // Set BIT1 of the 32-bit variable to 1:

11.

12. MEM_ADDR(BITBAND( (u32)&CRCValue,1)) = 0x1;

13.

14. //Judge any one digit (the 23rd digit):

15.

16. if(MEM_ADDR(BITBAND( (u32)&CRCValue,23))==1)

17. {

18.

19. }

1.jpg

Keywords:STM32 Reference address:Understanding of STM32 alias area

Previous article:8 configurations of STM32 IO ports
Next article:STM32 interrupt and nested NVIC quick introduction

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

The STM32 serial port DMA sends two frames continuously, resulting in partial data coverage
Problem Description Using the STM32 serial port for DMA transmission (Noraml mode), the transmission function log_printf() is called twice in a task, but the data sent back is not consistent with the expectation displayed on the serial port debugging assistant. Part of the data sent for the first time is overwritten b
[Microcontroller]
STM32 _I _0 _IO volatile const
What do __I, __O, and __IO mean? These are macro definitions in the ST library, and are defined as follows: #define __I volatile const /*! #define __O volatile /*! #define __IO volatile /*! Obviously, these three macro definitions are used to replace volatile and const, so we must first understand the role of
[Microcontroller]
STM32 USB clock configuration
1. First, configure the system clock as follows: RCC_DeInit(); //Reset the peripheral RCC register to the default value     /* Enable HSE */     RCC_HSEConfig(RCC_HSE_ON); //Set external high speed crystal oscillator (HSE)     /* Wait till HSE is ready */     HSEStartUpStatus = RCC_WaitForHSEStartUp(); //Wait f
[Microcontroller]
STM32 SPI slave routine
#include "stm32f10x.h" /*RCC clock configuration*/ void RCC_config(void) {  ErrorStatus HSEStartUpStatus; /* RCC registers are set to default configuration */ RCC_DeInit(); /* Turn on external high speed clock */ RCC_HSEConfig(RCC_HSE_ON); /* Wait for the external high-speed clock to stabilize*/ HSEStartUpStatus
[Microcontroller]
[STM32 motor vector control] Record 5 - FOC principle
FOC control algorithm: What FOC controls is actually the direction of the electromagnetic field of the motor. The rotor torque of the rotor is proportional to the vector product of the stator magnetic field vector and the rotor magnetic field vector. From the relationship between the vectors, it can be seen that if th
[Microcontroller]
[STM32 motor vector control] Record 5 - FOC principle
STM32 Learning - AD single channel and multi-channel conversion (DMA)
Chapter 3 AD Conversion This chapter is divided into two parts. The first is the single channel conversion of AD, and the second is the multi-channel conversion of AD. First, convert the single channel.  The maximum conversion frequency of the AD built into STM32 is 14MHZ. There are 16 conversion channels in total. Th
[Microcontroller]
Use of STM32 ADC and internal temperature sensor
STM32 comes with 1-3 ADC modules, and the sampling accuracy reaches 12 bits, which is a small upgrade compared to the 10 bits of the AVR microcontroller used in those days. This test program uses the ADC DMA interrupt method, so that the CPU can hand over the ADC task to the hardworking DMA. When the DMA completes a
[Analog Electronics]
stm32 - ordinary serial port (receive query and interrupt combined)
Serial port experiment 1. Before doing any experiment, import the target file into the library file, then import the header file in the main function, and write the path in FWLIB. Of course, the serial port is no exception.                                                                      2. After importing the fi
[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号