STM32- bit operation

Reference: "STM32F4xx Chinese reference manual," Memory and bus architecture section, GPIO chapter, "Cortex®-M4 core Programming Manual" 2.2.5 Bit-banding.

Bit Profile

Bit operation is possible for a single bit read and write. F429 no such keywords, but to achieve by accessing bit-band alias region.
There are two places to achieve a bit, a space is the lowest 1MB SRAM area and the other is the lowest 1MB space peripheral area
of these two 1MB of space in addition to outside like normal RAM as the operation, they have their own bit alias region, this bit-band alias region expanding each bit word into a 32-bit space of 1MB, when these bits with a word access alias region, can achieve the purpose of access to a certain bit bit-band region.
5d305578df57b12814

Peripheral bit-band region

Peripheral zone with the address bits is: 0X40000000 ~ 0X400F0000, a size of 1MB, the size of 1MB which contains the APB1 / 2 and so AHB1 peripheral register, register on AHB2 / 3 bus is not included. Address bits peripheral region with the expanded bit-band alias region is: 0X42000000 ~ 0X43FFFFFF.

SRAM bit-band region

Bit address of the SRAM region is: 0X2000 0000 ~ X200F 0000, a size of 1MB, the expanded bit-band alias region address: 0X2200 0000 ~ 0X23FF FFFF, a size of 32MB.

Bit-band region and the region address translation bit-band alias

It can achieve the effect of the operation of the bit-band bit-band alias address region accessed by the form of the bit pointer.

Peripheral bit-band alias region address

For a bit with a bit on-chip peripheral area, it referred to the byte address A, bit number is n (0 <= n <= 7), then
the bits in the address region is an alias:

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

0X42000000 bit start address is the peripheral band alias region, the starting address is 0x40000000 peripheral bit-band region, (A-0x40000000) indicates the number of bytes in front of the bit, a byte is 8 bits, so that 8, after a bit expansion is 4 bytes, 4, n represents the number of bits in the address a, because the expansion is one bit words after
section, so it * 4.

SRAM bit-band alias region address

For certain bit SRAM bit-band regions, in which it is referred to as the byte address A, bit number is n (0 <= n <= 7), then

The bits in the address region is an alias:

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

Unified Formula

Two equations combined into one formula, the "bit address + bit number" into the alias address unified into a macro region.

// 把“位带地址+位序号”转换成别名地址的宏 
#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x02000000+((addr &0x000FFFFF)<<5)+(bitnum<<2))

addr & 0xF0000000 to distinguish the peripheral or SRAM, the actual effect is taken 2 or 4, if the peripheral, it is extracted 4, after 0X02000000 is equal to + 0X42000000, 0X42000000 is the starting address of the peripheral alias region. If SRAM, 2 is then withdrawn, after 0X02000000 + 0X22000000,0X22000000 it is equal to the start address of the SRAM alias region.

addr & 0x000FFFFF high three blocked, equivalent to minus 0X20000000 or 0X40000000, but why is shielded high three? Because the highest peripheral address is: 0X4010 0000, with the start time address 0X40000000 subtraction, always low 5 are valid, so simply put high three masked to achieve the effect of subtracting the starting address, block out specific how many bits with the highest address related. SRAM The same analysis can be. << equivalent to 5 8 4 4 << 2 equivalent *, we have analyzed the two above.

PS: bit indicates the minimum unit region is a peripheral or SRAM address register, we generally arranged or read and write operations are directed to this address content. And bit-band alias region, this address extension amplification, so that we can operate directly on the corresponding bit register achieve a minimum bit operations, such as data output register GPIOA, 0x40020016, we read port 0, the address conversion needs 0x01 read pointer of the appropriate type, and the bit-band alias region the extended position, can be directly read and write bit, not an indirect reference.

Guess you like

Origin www.cnblogs.com/luoxiao23/p/11209472.html