你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【经验分享】STM32F2位带操作

[复制链接]
STMCU小助手 发布时间:2021-12-2 14:55
手册中说:
2 @6 a7 g3 }! n. Q0 mIn 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.
- {; C; L; U) _2 ^8 c在这两个系列中外设和SRAM都有各自映射的位带区,以实现对位的单独操作。8 L' i, v! e% P# a  K- t
The operations are only available for Cortex ® -M3 accesses, and not from other bus masters (e.g. DMA).
- }+ \+ [1 `/ a0 ^5 j8 E1 f$ a$ a使用局限于M3内核。
% C: }' d- e! e9 d$ d, d* ^1 kA 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:" n6 F! A8 W% j" S5 q
地址映射公式如下% I( f( B8 i8 L
                               bit_word_addr  =  bit_band_base  + ( byte_offset  x 32) + ( bit_number  × 4)! o7 K/ n' v, e; j* j
where:
) C* r2 U$ n2 {5 [; K/ U' M/ `–  bit_word_addr  is the address of the word in the alias memory region that maps to the targeted bit
. o& b, {" F! A+ \% V9 V一位扩展成了一个字。( Y* Y! U" @) q/ h. {9 \. R5 d( F
–  bit_band_base  is the starting address of the alias region
$ }9 d$ k/ c  b5 T; R% |8 G位带基地址是对应位带的起始地址。
  e6 U+ D6 P1 R8 J  q& D9 _–  byte_offset  is the number of the byte in the bit-band region that contains the targeted bit+ }3 ~! M( |. V9 f
这里的偏移值为包含操作位的寄存器偏移值。9 W; R" k% p4 S  d
–  bit_number  is the bit position (0-7) of the targeted bit  
3 c4 O; l) ^0 B这里的位就是目标位。4 B; m( u7 `& }. W9 v

- ^! |* r0 ?7 _5 _  L  ^位带区在 SRAM上的地址范围:0x20000000 ~ 0x200FFFFF(SRAM区中最低1MB)
7 B( o0 a; K. b9 w/ T  U位带识别区在SRAM上的地址范围:   0x22000000 ~ 0x220FFFFF
3 q1 L; z) z  @/ @位带区在片上外设的地址范围:0x4000 0000-0x400F FFFF(片上外设区中的最低1MB),
" v& S9 @/ g( ^* a位带识别区在片上外设的地址范围:0x4200 0000~0x42FF FFFF;6 {) d; Z9 i$ n3 ]$ ?3 U' R, q
对应关系:位带区的每个bit位的值 对应 位带识别区1个 32位的地址的内容;
$ K* z3 G+ d3 ^+ ^, }, i所以位带操作是:当你通过位带别名区访问这些32位的地址的内容时,就可以达到访
$ [5 ]) S- E: U( o9 ~/ U! k问位带区对应的比特位。
/ w  ?8 I+ |, ^, [' r+ S1 L4 y: s' A8 A, l1 I* \* H
举例:# D: C; V: I4 G% q' Y! a) n
要给GPIO PC15做拉高拉低操作。
9 e. ^; M+ W$ H* g2 [" M# e4 M首先找到操作寄存器的地址:
" ]0 j. _- S' U* E* A6 Y% ~  ^GPIO为外设,故需用外设的基地址:                   PERIPH_BASE               ((uint32_t)0x40000000)/ Z: G2 w5 q1 F& d( \3 B, s/ g
GPIOC在AH1外设上,故在之前基础上再做偏移:AHB1PERIPH_BASE       (PERIPH_BASE + 0x00020000)1 k* n$ d1 h1 a. K3 E' @" f
同时需要再加上GPIOC的偏移:                         GPIOC_BASE                  (AHB1PERIPH_BASE + 0x0800)
* V- m( O+ ^/ x  j然后找到位设置寄存器:                                   GPIOC_BSRR                  (GPIOC_BASE + 0x18): q/ S+ V" P9 R+ `3 f( `# M. g/ @; Q; q$ \
( G! d) R+ f' }0 h
最终得到的地址为 :0x40020818
- P3 ~: H4 Y' S. D& ~通常情况下向这个地址赋值即可实现指定位拉高拉低操作:# X" `6 f- Q$ n
*((volatile unsigned long *) 0x40020818) = 0x80000000  //!<拉高' S; M8 q2 G1 ]! i& V0 t2 L
*((volatile unsigned long *) 0x40020818) = 0x00008000  //!<拉低7 l9 x$ \9 n9 V  W* J' a, U/ L
: o. |6 J9 a1 S
但通过位带,按照公式获取位带操作地址:' c; b& q9 i: D9 G
/*这是拉高时寄存器地址*/0 d6 f2 o  F! X; @- \) _! [6 H
AddrH =  *((volatile unsigned long *)(( 0x40020818  & 0xF0000000)+0x2000000+(( 0x40020818  & 0xFFFFF)<<5)+(15 << 2)))  
- N, ]/ c6 ^! r2 |) @AddrH = 1;   //!<置1就拉高 , s1 N7 l; Z4 g' _- j1 h, A0 l
/*这是拉低时寄存器地址*/! ?' }% s3 ^; M4 ~1 |
AddrL =  *((volatile unsigned long *)(( 0x40020818  & 0xF0000000)+0x2000000+(( 0x40020818  & 0xFFFFF)<<5)+((15+16) << 2))) 6 l, B" p% x0 l" h' x  A# N
AddrL = 1;   //!<置1就拉低    ) p' ?6 R/ q  y. d! n! @
6 |- W; |4 J' D; p$ F
使用宏定义,即:(Addr为 GPIOC_BSRR  拉高时 BitNum为15 拉低时 BitNum是(15+16) )
. M6 a" N2 Q2 o* X( m. W% j7 b, h& c#define BitBand(Addr,BitNum) *((volatile unsigned long *)((Addr & 0xF0000000)+0x2000000+((Addr & 0xFFFFF)<<5)+(BitNum << 2))) 4 Y2 B7 U7 Y! f9 r5 z
) c( l8 i# A- ]& C+ `. P3 W
精简之后 ,位带操作   :1 @5 J% E8 r9 D' v
#define BitBand(Addr,BitNum) *((volatile unsigned long *)(PERIPH_BB_BASE|((Addr-PERIPH_BASE)<<5)|(BitNum << 2)))
" w; R0 p7 A9 S, d8 U- s( ^) h/ Z) L2 o; B. M7 w; }: R
1 s2 t) d. a+ v9 C" D- J
收藏 评论0 发布时间:2021-12-2 14:55

举报

0个回答
关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新和工艺
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版