STMCU小助手
发布时间:2021-12-1 21:47
|
手册中说:0 {, w6 f/ n- v0 L; L0 K6 j0 M; l 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都有各自映射的位带区,以实现对位的单独操作。" p. g% s$ k, V$ a/ k3 i# V 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: 地址映射公式如下 bit_word_addr = bit_band_base + ( byte_offset x 32) + ( bit_number × 4) where: – bit_word_addr is the address of the word in the alias memory region that maps to the targeted bit& m' X. I. h/ {& z0 `2 s5 ] 一位扩展成了一个字。2 @9 m$ \$ S/ M) V5 ^ – bit_band_base is the starting address of the alias region( \$ O+ y) R0 o( p) k8 k 位带基地址是对应位带的起始地址。/ s) F/ z$ M" m7 s7 n3 ` – byte_offset is the number of the byte in the bit-band region that contains the targeted bit 这里的偏移值为包含操作位的寄存器偏移值。+ d1 I- C4 ^/ ? – bit_number is the bit position (0-7) of the targeted bit % A- C* [0 W& G, i/ J 这里的位就是目标位。3 ?5 h0 J* A0 }6 Q / R, O6 [1 _, r 位带区在 SRAM上的地址范围:0x20000000 ~ 0x200FFFFF(SRAM区中最低1MB)( f$ u( C, V5 I0 e# n% z 位带识别区在SRAM上的地址范围: 0x22000000 ~ 0x220FFFFF$ n- A4 W _. U0 r2 w2 Y 位带区在片上外设的地址范围:0x4000 0000-0x400F FFFF(片上外设区中的最低1MB),) L% r' @; W0 G; d2 z 位带识别区在片上外设的地址范围:0x4200 0000~0x42FF FFFF;+ y. H" [( x9 K) ^* C 对应关系:位带区的每个bit位的值 对应 位带识别区1个 32位的地址的内容;# h. y. Q4 J6 m% f5 z/ Y6 g 所以位带操作是:当你通过位带别名区访问这些32位的地址的内容时,就可以达到访+ m) |" `. p" h U5 F9 q 问位带区对应的比特位。 举例: 要给GPIO PC15做拉高拉低操作。 首先找到操作寄存器的地址:7 E1 N9 z! n3 i! U/ X: q9 h( Q v1 G GPIO为外设,故需用外设的基地址: PERIPH_BASE ((uint32_t)0x40000000)* ]3 `2 A* r3 K1 L GPIOC在AH1外设上,故在之前基础上再做偏移:AHB1PERIPH_BASE (PERIPH_BASE + 0x00020000) 同时需要再加上GPIOC的偏移: GPIOC_BASE (AHB1PERIPH_BASE + 0x0800) 然后找到位设置寄存器: GPIOC_BSRR (GPIOC_BASE + 0x18) . v5 B) U( z2 {/ F! E: R& l# i 最终得到的地址为 :0x40020818 通常情况下向这个地址赋值即可实现指定位拉高拉低操作: *((volatile unsigned long *) 0x40020818) = 0x80000000 //!<拉高 *((volatile unsigned long *) 0x40020818) = 0x00008000 //!<拉低 但通过位带,按照公式获取位带操作地址: /*这是拉高时寄存器地址*/' t3 ?0 e7 I1 E; `' [) n6 F AddrH = *((volatile unsigned long *)(( 0x40020818 & 0xF0000000)+0x2000000+(( 0x40020818 & 0xFFFFF)<<5)+(15 << 2))) AddrH = 1; //!<置1就拉高 & R. D B2 ?8 J3 p% y5 { /*这是拉低时寄存器地址*/ AddrL = *((volatile unsigned long *)(( 0x40020818 & 0xF0000000)+0x2000000+(( 0x40020818 & 0xFFFFF)<<5)+((15+16) << 2))) + D+ s! G9 `! i2 L( b: V AddrL = 1; //!<置1就拉低 7 g+ x' z" {. m: m4 r 使用宏定义,即:(Addr为 GPIOC_BSRR 拉高时 BitNum为15 拉低时 BitNum是(15+16) ). [" Q$ Y! N, z/ @, K [' j: V3 P #define BitBand(Addr,BitNum) *((volatile unsigned long *)((Addr & 0xF0000000)+0x2000000+((Addr & 0xFFFFF)<<5)+(BitNum << 2))) / w: H0 g# L. V- C) c1 _- o6 S # }. y! Y7 ?* I5 B* T9 X) J+ N 精简之后 ,位带操作 :% r9 o+ w& y- Y* E #define BitBand(Addr,BitNum) *((volatile unsigned long *)(PERIPH_BB_BASE|((Addr-PERIPH_BASE)<<5)|(BitNum << 2)))7 O( b% n! ~. n" ^% i ! L* y& a8 v$ |" u4 M4 Q+ s& @# V |
微信公众号
手机版