STMCU小助手
发布时间:2021-12-1 21:47
|
手册中说: In the STM32F20x and STM32F21x both the peripheral registers and the SRAM are mapped to a bit-band region, so that single bit-band write and read operations are allowed. 在这两个系列中外设和SRAM都有各自映射的位带区,以实现对位的单独操作。3 m! ^+ k- ?) P# ~ The operations are only available for Cortex ® -M3 accesses, and not from other bus masters (e.g. DMA). 使用局限于M3内核。 A mapping formula shows how to reference each word in the alias region to a corresponding bit in the bit-band region. The mapping formula is:5 O1 o3 U H5 w& u1 f2 a1 O 地址映射公式如下8 O1 @5 {% J1 y N: l bit_word_addr = bit_band_base + ( byte_offset x 32) + ( bit_number × 4) where:1 Z6 W! _% s* w1 V) o' Q – bit_word_addr is the address of the word in the alias memory region that maps to the targeted bit" t* X$ j: P" }- X9 ?9 s$ B t" I 一位扩展成了一个字。9 h; [% u! p8 U& g! ] ] – bit_band_base is the starting address of the alias region0 t0 [8 F5 d8 r% `' V, `9 i5 u 位带基地址是对应位带的起始地址。 – byte_offset is the number of the byte in the bit-band region that contains the targeted bit+ `% R |8 a; g4 [! l9 A( M 这里的偏移值为包含操作位的寄存器偏移值。7 q7 a# F9 v; J- W. y – bit_number is the bit position (0-7) of the targeted bit 5 }* I! o9 O" Q3 x2 x 这里的位就是目标位。% G+ ~7 w# _; x6 T1 n # D9 ?! c. k- o 位带区在 SRAM上的地址范围:0x20000000 ~ 0x200FFFFF(SRAM区中最低1MB)9 ?8 e6 y" O6 f7 |. m 位带识别区在SRAM上的地址范围: 0x22000000 ~ 0x220FFFFF. E$ u( C1 Y c4 m" k* u 位带区在片上外设的地址范围:0x4000 0000-0x400F FFFF(片上外设区中的最低1MB), 位带识别区在片上外设的地址范围:0x4200 0000~0x42FF FFFF;# v4 B h: Q c 对应关系:位带区的每个bit位的值 对应 位带识别区1个 32位的地址的内容; 所以位带操作是:当你通过位带别名区访问这些32位的地址的内容时,就可以达到访1 \2 S1 e. o6 Z, t: r% R" @1 h7 N 问位带区对应的比特位。: Z8 t" |9 F! ?5 @9 m; K 举例:# l5 f; q9 {: l7 g# N+ G 要给GPIO PC15做拉高拉低操作。8 R0 L1 ^: a5 z 首先找到操作寄存器的地址:* B) ^6 Z, L0 x$ I GPIO为外设,故需用外设的基地址: PERIPH_BASE ((uint32_t)0x40000000) GPIOC在AH1外设上,故在之前基础上再做偏移:AHB1PERIPH_BASE (PERIPH_BASE + 0x00020000) 同时需要再加上GPIOC的偏移: GPIOC_BASE (AHB1PERIPH_BASE + 0x0800) 然后找到位设置寄存器: GPIOC_BSRR (GPIOC_BASE + 0x18)# ?# |/ Q/ Y/ i3 H7 k. y P 最终得到的地址为 :0x40020818! z5 U" r0 y# T) x e( W$ V 通常情况下向这个地址赋值即可实现指定位拉高拉低操作: *((volatile unsigned long *) 0x40020818) = 0x80000000 //!<拉高 *((volatile unsigned long *) 0x40020818) = 0x00008000 //!<拉低/ Q8 I# ~3 p: C- m( L 但通过位带,按照公式获取位带操作地址: /*这是拉高时寄存器地址*/' q" }( d# c/ ~0 M AddrH = *((volatile unsigned long *)(( 0x40020818 & 0xF0000000)+0x2000000+(( 0x40020818 & 0xFFFFF)<<5)+(15 << 2))) , K! o- y& ^' s( d+ Z AddrH = 1; //!<置1就拉高 , W# C; I' w) W$ @ /*这是拉低时寄存器地址*/7 I: x3 d$ [6 ?0 q) |5 M AddrL = *((volatile unsigned long *)(( 0x40020818 & 0xF0000000)+0x2000000+(( 0x40020818 & 0xFFFFF)<<5)+((15+16) << 2))) AddrL = 1; //!<置1就拉低 使用宏定义,即:(Addr为 GPIOC_BSRR 拉高时 BitNum为15 拉低时 BitNum是(15+16) )- G0 ]6 I2 }" h" B #define BitBand(Addr,BitNum) *((volatile unsigned long *)((Addr & 0xF0000000)+0x2000000+((Addr & 0xFFFFF)<<5)+(BitNum << 2))) 精简之后 ,位带操作 : #define BitBand(Addr,BitNum) *((volatile unsigned long *)(PERIPH_BB_BASE|((Addr-PERIPH_BASE)<<5)|(BitNum << 2))) 8 ]( W9 L+ t! z+ |: C% U 5 T, C; U& w) R |
微信公众号
手机版