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. }
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
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Qorvo Q&A on Connectivity: Reducing Wi-Fi Interference
- What device is used to heat the chip on the motherboard in low temperature outdoor conditions?
- CB5654 Development Board Evaluation 3 - Building a Development Environment
- Two-phase brushless DC motor speed control system based on DSP and CPLD
- 15. [Learn LPC1768 library functions]
- Anxinke PB-02 module review (1) - Compilation environment construction & appearance display
- 3. [Record] Two library files that must be installed by the GCC compiler
- Analysis of the characteristics of IQ modulators
- Dating Spring---I am only one step away from nature working overtime
- Wanted FRDM-KL25Z