Analysis of bit-band operation of STM32

Publisher:诚信与爱Latest update time:2019-01-30 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The 8051 microcontroller can directly read and write a certain IO bit, and the Cortex-M3's bit-banding operation is an enhanced version of the 8051 bit addressing area. After using the bit-banding operation, you can use ordinary load/store instructions to read and write a single bit.

1. Related concepts

Bit-band region: An address region that supports bit-band operations.

Bit-band alias area: The access to the alias address ultimately affects the access to the bit-band area. The access to the bit-band alias area has an address mapping process.


2. Principle of bit-band operation

The ultimate goal of bit-banding operation is to perform independent read and write operations on the bits in the bit-band area, but it is achieved by operating on the bit-band alias area.

The specific process is as follows:

The bit-band alias area is accessed for reading and writing, the bit-band alias area is mapped to the corresponding bit-band area through an address mapping relationship, and the original bit reading and writing operations are performed on the bit-band area.

3. Address Mapping

The above briefly introduces the bit-band operation. So which addresses support the bit-band operation? What are their corresponding bit-band alias area addresses? What is the address mapping relationship between the two?

1. Addresses that support bit-band operations

The Cortex-M3 has two regions that support bit-banding operations. In addition to being used like normal RAM, these two regions can also be operated through bit-banding alias regions. The two regions (bit-banding regions) are:

The lowest 1MB in the SRAM area: 0x2000 0000 - 0x200F FFFF

The lowest 1MB of on-chip peripherals: 0x4000 0000 - 0x400F FFFF

The bit-band alias regions corresponding to these two bit-band regions are:

The bit-band alias area corresponding to the lowest 1MB bit-band area in the SRAM area is:

The lowest 1MB bit-band area in the on-chip peripherals corresponds to the bit-band alias area:

2. Address mapping relationship between bit-band area and bit-band alias area

The bit-band alias area expands each bit in the bit-band area into a 32-bit word, that is, each bit in the bit-band area corresponds to a 4-byte address in the bit-band alias area.

The following figure shows the expansion correspondence between the lowest 1MB bit-band area and the bit-band alias area in the SRAM area:

Calculation formula:

For a certain bit in the SRAM bit band area, let its byte address be A and the bit number be n (0<=n<=7), then the address of the bit band alias area corresponding to the bit is:

For a certain bit in the on-chip peripheral bit-band area, let its byte address be A and the bit number be n (0<=n<=7), then the address of the bit-band alias area corresponding to the bit is:

Note: "*4" means a word is 4 bytes, and "*8" means a byte has 8 bits.


4. Mechanism of read and write operations

In the bit-band area, although each bit is mapped to a word in the alias area, only the LSB (least significant bit) of the word in the alias area is valid, so the read and write operations are performed on the LSB of the word in the alias area, and the value of the LSB is 0 or 1.

1. Reading process:

for example:

Read the value of the second bit of SRAM address 0x2000 0000:

2. Writing process: read, modify, write

for example:

Set the second bit of SRAM address 0x2000 0000 to 1:


5. Bit-band operation programming implementation

There is no direct support for bit-banding in the C compiler. For example, the C compiler does not know that the same memory block can be accessed with different addresses, nor does it know that access to a bit-band aliased area is only valid for the LSB. To use bit-banding in C, the simplest way is to #define the address of a bit-band aliased area.

1. Macro definition of bit-band operation

To simplify bit-band operations, we can propose a macro that converts "bit-band address + bit number" to an alias address, and then create a macro that converts the alias address to a pointer type:


#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2)) /* Macro to convert "bit band address + bit number" to alias address */

#define MEM_ADDR(addr) *((volatile unsigned long *)(addr)) /* Macro to convert alias address to pointer type */

#define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum)) /* Macro for bit-band operation of a certain bit at a certain address */



2. Give an example

The following uses the GPIOA->ODR register (address 0x40020014) of the STM32F407 as an example to read and write through bit-band operations, and compares it with the traditional reading and writing methods, and sends the information to the console for display through the serial port. The code is as follows:

Header file definition:

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

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

#define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum)) 

//IO port address mapping

#define GPIOA_ODR_Addr (GPIOA_BASE+20) //0x40020014

#define PAout(n) BIT_ADDR(GPIOA_ODR_Addr,n)  

main function:

int main(void)

{

u8 Temp;

u16 Data;

Data = 0;

Stm32_Clock_Init(336,8,2,7); //Initialize the clock to 168Mhz

delay_init(168); //Initialize delay function

uart_init(84,115200); //Serial port initialized to 115200

 

printf("init finished\r\n");

GPIOA->ODR = 0xffff; /* Traditional writing method: write 1 to each bit of GPIOA->ODR */

printf("Tratidional operate: GPIOA->ODR[0x%x]:0x%x\r\n", &GPIOA->ODR, GPIOA->ODR);

GPIOA->ODR = 0x0; /* Clear GPIOA->ODR*/

printf("Clear GPIOA->ODR: GPIOA->ODR[0x%x]:0x%x\r\n", &GPIOA->ODR, GPIOA->ODR);

 

for(Temp = 0; Temp < 16; Temp++)

{

PAout(Temp) = 1; /* Bit-band write operation: write GPIOA->ODR to 1 via the bit-band alias area */

}

printf("Bit-band write: GPIOA->ODR[0x%x]:0x%x\r\n", &GPIOA->ODR, GPIOA->ODR); /* You can see the value of each bit of GPIOA->ODR after the bit-band operation*/

 

for(Temp = 0; Temp < 16; Temp++)

{

Data |= (PAout(Temp) << Temp); /* Bit-band read operation: read the value of each bit of GPIOA->ODR through the bit-band alias area and store it in Data*/

}

printf("Bit-band read: GPIOA->ODR[0x%x]:0x%x\r\n", &GPIOA->ODR, Data); /* You can see the value of the alias area corresponding to each bit of GPIOA->ODR after the bit-band operation is read*/

while(1)

{

}

return 1;

}


Information printed by the serial port:


This article is mainly written with reference to the following materials:

Chapter 5 of "CM3 Definitive Guide" (pages 87-92)

"STM32F4xx Chinese Reference Manual"


Keywords:STM32 Reference address:Analysis of bit-band operation of STM32

Previous article:STM32 timer output PWM mode
Next article:STM32 bitband operation summary

Recommended ReadingLatest update time:2024-11-22 12:16

Programmable voltage detection PVD of stm32
The order of configuring PVD is as follows: Note that PVD is usually enabled after system initialization is complete. /**   * @brief Configures EXIT Lines.   * @param  None   * @retval None   */ static void EXTI_Configuration(void) {   EXTI_InitTypeDef EXTI_InitStructure;     /* Configure EXTI Line16(PVD Output) t
[Microcontroller]
STM32 study notes - using the general timer TIM2 for precise delay
1. General timer overview and performance 1.1 Overview On the stm32 chip used by the author, there is a total of 1 advanced timer TIM1 and 3 general timers TIM2, TIM3, and TIM4. Each general timer consists of a 16-bit auto-load counter driven by a programmable prescaler. It is suitable for a variety of occasions,
[Microcontroller]
STM32 study notes - using the general timer TIM2 for precise delay
STM32 simulates IIC master device non-IIC silent mode
//Why use software simulation IIC instead of hardware IIC? In addition to the problem of ST's IIC module itself, it is also because the hardware IIC is not convenient to transplant and cannot be used in different MCUs. /****************************Copyright(c)*********************************************           
[Microcontroller]
Stm32 IAP programming and user programming
Stm32f10x series MCU  Bootloader  process Chip: stm32f103ze Required software: SecureCRT (used to send Application files using the Ymode protocol). In fact, we should write a host computer ourselves. Here, SecureCRT is used to act as our own application (used to verify whether the Bootloader is successful). Keil ver
[Microcontroller]
Stm32 IAP programming and user programming
Solution to stm32 serial port download error
Since my J-LINK was broken and the new one hadn't arrived yet, I used the serial port tool to download. The information prompt box indicates that the download content is not within the range of 0x08000000 and 0x20000000, that is, it is not on the program FLASH or SRAM. However, there is no problem in reading devic
[Microcontroller]
Solution to stm32 serial port download error
STM32 GPIO Operation (by woody)
The steps are: 1) Enable the IO port clock. The calling function is RCC_APB2PeriphClockCmd(). 2) Initialize IO parameters. Call function GPIO_Init(); 3) Operate IO. The method of operating IO is the method we explained above. GPIO_InitTypeDef  GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin =; GPIO_InitStructure.GP
[Microcontroller]
STM32 ADC_DMA
This is just a measurement trend, not a very precise measurement. Once again, we reiterate that different channels of the STM32 ADC correspond to different pins. In this code, PA1 corresponds to channel 1. Included Files: Due to Baidu's word limit, only the key codes are posted here: (1)Main   C languag
[Microcontroller]
STM32 ADC_DMA
stm32 Flash read and write
                The programming operation of stm32 can be realized by reading and writing the flash inside stm32.                   The built-in programmable Flash of stm32 is of great significance in many occasions. For example, its support for ICP features allows developers to debug and develop stm32, and can prog
[Microcontroller]
Latest Microcontroller Articles
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号