请选择 进入手机版 | 继续访问电脑版

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

【经验分享】STM32F2位带操作

[复制链接]
STMCU小助手 发布时间:2021-12-1 21:47
手册中说:6 b* r8 R& M3 Q6 }0 n' a5 S) `0 [
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.' ~) u+ R# p0 s1 q) r
在这两个系列中外设和SRAM都有各自映射的位带区,以实现对位的单独操作。
3 }8 Q) N" F: I0 `( WThe operations are only available for Cortex ® -M3 accesses, and not from other bus masters (e.g. DMA).
! u9 ]9 i- c" A: W$ @使用局限于M3内核。6 r! j8 Y5 M: e2 |
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:
, A' N$ j" T8 q地址映射公式如下
& B4 h/ d; n  Z                               bit_word_addr  =  bit_band_base  + ( byte_offset  x 32) + ( bit_number  × 4)! x8 S  d* B% V! W
where:
: A- H6 l, ~5 p7 o–  bit_word_addr  is the address of the word in the alias memory region that maps to the targeted bit( k+ l. n3 c) |: B( t
一位扩展成了一个字。: m) s, z- `- L3 ?
–  bit_band_base  is the starting address of the alias region& n& N! {5 T3 W2 H/ g; Y8 r8 g
位带基地址是对应位带的起始地址。) \( p$ t& J4 d4 w/ j
–  byte_offset  is the number of the byte in the bit-band region that contains the targeted bit, L" ~. h5 f) p; n# p  U
这里的偏移值为包含操作位的寄存器偏移值。
. |! b# `& p9 g9 B2 J; s) y–  bit_number  is the bit position (0-7) of the targeted bit  1 H. \; C8 C. m
这里的位就是目标位。) N& o5 J4 S; ]
& {: S. z& v% R; o% z9 ~, t) c
位带区在 SRAM上的地址范围:0x20000000 ~ 0x200FFFFF(SRAM区中最低1MB)7 g( ^/ A- b) s  u7 \# I
位带识别区在SRAM上的地址范围:   0x22000000 ~ 0x220FFFFF
, Q! n4 b0 U0 C( L! p( U+ \) j位带区在片上外设的地址范围:0x4000 0000-0x400F FFFF(片上外设区中的最低1MB),
  F) P6 p) l# w, J位带识别区在片上外设的地址范围:0x4200 0000~0x42FF FFFF;
5 i2 y$ N# |" N$ N7 p对应关系:位带区的每个bit位的值 对应 位带识别区1个 32位的地址的内容;; O# r/ L& o; {* x
所以位带操作是:当你通过位带别名区访问这些32位的地址的内容时,就可以达到访
2 S/ [' ]5 Y! X9 O" @  B; \8 L, Y) b问位带区对应的比特位。
0 ^5 R8 {6 \: Z0 V5 |1 `8 Y8 o. ?0 P  U
举例:; o9 K: J% q4 B# M8 Y+ E1 b
要给GPIO PC15做拉高拉低操作。5 b$ e/ E: J3 X, B0 n0 g( K
首先找到操作寄存器的地址:  N: u2 i8 t( D
GPIO为外设,故需用外设的基地址:                   PERIPH_BASE               ((uint32_t)0x40000000)
7 [+ ]" U; Z: \GPIOC在AH1外设上,故在之前基础上再做偏移:AHB1PERIPH_BASE       (PERIPH_BASE + 0x00020000)
2 M- }* L; H7 Z6 V3 p  l. y: A: [同时需要再加上GPIOC的偏移:                         GPIOC_BASE                  (AHB1PERIPH_BASE + 0x0800)9 e& l+ Q; i5 r0 G+ Z2 B
然后找到位设置寄存器:                                   GPIOC_BSRR                  (GPIOC_BASE + 0x18)+ D" p/ Z# I6 f! X2 u
/ R) R! ?& W4 |9 R+ E2 ^% k
最终得到的地址为 :0x40020818
7 o$ y# H$ b1 y% N5 \4 d通常情况下向这个地址赋值即可实现指定位拉高拉低操作:
; E8 L' i% Q; M! B; J*((volatile unsigned long *) 0x40020818) = 0x80000000  //!<拉高! O! m5 F+ L0 ]2 f  V
*((volatile unsigned long *) 0x40020818) = 0x00008000  //!<拉低
" d9 E4 P. @9 P! T& Z: v5 l8 c1 W+ n6 J  U% F( N$ v
但通过位带,按照公式获取位带操作地址:
% ^4 }/ |$ T: k: d/*这是拉高时寄存器地址*/: B( `% ~1 P# \1 h: q/ N6 C  R
AddrH =  *((volatile unsigned long *)(( 0x40020818  & 0xF0000000)+0x2000000+(( 0x40020818  & 0xFFFFF)<<5)+(15 << 2)))  
& _3 j$ [2 ?! H& h2 IAddrH = 1;   //!<置1就拉高 6 V2 K8 B  P7 y3 u
/*这是拉低时寄存器地址*/
$ F2 Z- }/ M0 p1 F! a7 e/ p: @ AddrL =  *((volatile unsigned long *)(( 0x40020818  & 0xF0000000)+0x2000000+(( 0x40020818  & 0xFFFFF)<<5)+((15+16) << 2))) ) o6 u; _+ v7 R9 ?# q9 n% X
AddrL = 1;   //!<置1就拉低   
  Z& W# F8 M9 o; V+ H: y! O5 k8 _
使用宏定义,即:(Addr为 GPIOC_BSRR  拉高时 BitNum为15 拉低时 BitNum是(15+16) )
1 c0 z1 I8 I  |% N1 p#define BitBand(Addr,BitNum) *((volatile unsigned long *)((Addr & 0xF0000000)+0x2000000+((Addr & 0xFFFFF)<<5)+(BitNum << 2)))
+ @' r0 p# u$ m- x  ^; @  o5 \# W" I8 _( l" y" Z9 s' M1 `$ B
精简之后 ,位带操作   :/ _( u* f- g: X1 t" J, c1 f
#define BitBand(Addr,BitNum) *((volatile unsigned long *)(PERIPH_BB_BASE|((Addr-PERIPH_BASE)<<5)|(BitNum << 2)))
- x: v; M/ I) ~, n' X$ Q
! q3 \3 T# w/ n
( B4 w) A9 |, H" e! H$ w0 a- u
收藏 评论0 发布时间:2021-12-1 21:47

举报

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