This article has participated in the Denver Nuggets Creators Camp 3 “More Productive writing” track, see details: Digg project | creators Camp 3 ongoing, “write” personal impact.

preface

By register programming we can know that most of the time we need to modify the value of a single bit, so in STM32 how to implement the operation of a single bit? Bit band operation!

Scope of discussion

  • What is bitband operation?
  • What is the significance of bitband operations?
  • How to do bitband operation?

The body of the

A belt operation

Bitband operations (also known as bitband operations) can be understood as zones that allow bit operations.

Due to the underlying circuit design, the storage unit that stM32 single chip microcomputer can operate at a time is 4Byte, so it is logically impossible to carry out bit operation. ST, however, has set aside an address in the memory address mapping table in consideration of such user needs. This address is called the bitband alias area.

The function of bitband alias area is very simple, it is actually in the memory mapping table on the basis of the secondary mapping.

Since stM32 can only control 4 bytes at a time, we can use 1Bit as 4 bytes, where only the lowest Bit is valid.

However, not all memory addresses support bitband operations, so memory addresses that support bitband operations are also called bitband regions.

The diagram above shows the region that supports bitband operations. The blue and purple arrows point to the memory address region of each bit in the bitband region after the secondary mapping.

Let’s compare the address mapping table of the peripheral:

You can see that the addresses of all the on-chip peripherals are included. So we can do bitband operations whenever we operate peripherals.

Significance of bitband operations

At the beginning of the belt operation, there is a kind of chicken rib feeling.

There are two reasons:

  • Is it worth freeing up far more peripheral, SRAM memory than you actually need to operate on just to have a bit that can operate on a tiny fraction of memory alone?
  • Since the official firmware library function can modify a single bit, why do we need to use bitband operation?

After consulting a lot of information, I came up with an answer that I could barely convince myself of:

The essence of a firmware library is to call functions that are already wrapped, and functions run more slowly than register operations.

Using registers directly can be inconvenient and error-prone, so bitband operation is a compromise.

I don’t know if this is the right answer, but is it really worth it to free up so much memory for the potential performance savings? Maybe I can’t understand how precious it is.


Updated April 22, 2021:

The memory address mapping table is not the actual physical memory, but the range of memory that the CPU can control. From assembly, we know that the amount of memory the CPU can control is limited, so I still don’t understand the meaning of freeing up a lot of memory… Do something else with that memory budget…